From b7c592f407e5e087f57d61531039f7b09fb152e7 Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Sun, 31 Jan 2010 20:50:21 +0000 Subject: [PATCH] more fixes for new _cleanup method. --- couchdb-external-hook.py | 2 +- .../rnewson/couchdb/lucene/AdminServlet.java | 20 ++++++++++++++----- .../rnewson/couchdb/lucene/ViewIndexer.java | 6 ++---- .../couchdb/lucene/couchdb/Database.java | 19 ++++++++++++++---- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/couchdb-external-hook.py b/couchdb-external-hook.py index 37c4fe28..99a73997 100755 --- a/couchdb-external-hook.py +++ b/couchdb-external-hook.py @@ -82,7 +82,7 @@ def respond(res, req, key): method = req["method"] else: method = req["verb"] - sys.stderr.write(path) + res.request(method, path, headers=req_headers) resp = res.getresponse() diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/AdminServlet.java b/src/main/java/com/github/rnewson/couchdb/lucene/AdminServlet.java index df9d81f1..6773eab5 100644 --- a/src/main/java/com/github/rnewson/couchdb/lucene/AdminServlet.java +++ b/src/main/java/com/github/rnewson/couchdb/lucene/AdminServlet.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.HashSet; import java.util.Set; +import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -31,6 +32,7 @@ import org.apache.commons.configuration.HierarchicalINIConfiguration; import org.apache.commons.io.FileUtils; import org.apache.http.client.HttpClient; +import org.apache.log4j.Logger; import org.apache.lucene.index.IndexWriter; import com.github.rnewson.couchdb.lucene.Lucene.WriterCallback; @@ -61,6 +63,8 @@ public final class AdminServlet extends HttpServlet { private static final JSONObject JSON_SUCCESS = JSONObject.fromObject("{\"ok\":true}"); + private static final Logger LOG = Logger.getLogger(AdminServlet.class); + private Lucene lucene; private HierarchicalINIConfiguration configuration; @@ -78,11 +82,9 @@ protected void doPost(final HttpServletRequest req, final HttpServletResponse re final IndexPath path = IndexPath.parse(configuration, req); String command = req.getPathInfo(); command = command.substring(command.lastIndexOf("/") + 1); - if (path == null) { - // generalize path handling. final String[] parts = IndexPath.parts(req); - if (parts.length == 2) { + if (parts.length == 3) { if ("_cleanup".equals(command)) { cleanup(parts[0]); resp.setStatus(202); @@ -97,6 +99,7 @@ protected void doPost(final HttpServletRequest req, final HttpServletResponse re lucene.startIndexing(path, true); if ("_expunge".equals(command)) { + LOG.info("Expunging deletes from " + path); lucene.withWriter(path, new WriterCallback() { public boolean callback(final IndexWriter writer) throws IOException { writer.expungeDeletes(false); @@ -114,6 +117,7 @@ public void onMissing() throws IOException { } if ("_optimize".equals(command)) { + LOG.info("Optimizing " + path); lucene.withWriter(path, new WriterCallback() { public boolean callback(final IndexWriter writer) throws IOException { writer.optimize(false); @@ -134,7 +138,6 @@ public void onMissing() throws IOException { } private void cleanup(final String key) throws IOException { - // TODO tidy this. final HttpClient client = HttpClientFactory.getInstance(); final Couch couch = Couch.getInstance(client, IndexPath.url(configuration, key)); @@ -142,7 +145,12 @@ private void cleanup(final String key) throws IOException { for (final String dbname : couch.getAllDatabases()) { final Database db = couch.getDatabase(dbname); - dbKeep.add(db.getUuid().toString()); + final UUID uuid = db.getUuid(); + if (uuid == null) { + continue; + } + + dbKeep.add(uuid.toString()); final Set viewKeep = new HashSet(); for (final DesignDocument ddoc : db.getAllDesignDocuments()) { @@ -153,6 +161,7 @@ private void cleanup(final String key) throws IOException { // Delete all indexes except the keepers. for (final File dir : lucene.getUuidDir(db.getUuid()).listFiles()) { if (!viewKeep.contains(dir.getName())) { + LOG.info("Cleaning old index at " + dir); FileUtils.deleteDirectory(dir); } } @@ -161,6 +170,7 @@ private void cleanup(final String key) throws IOException { // Delete all directories except the keepers. for (final File dir : lucene.getRootDir().listFiles()) { if (!dbKeep.contains(dir.getName())) { + LOG.info("Cleaning old index at " + dir); FileUtils.deleteDirectory(dir); } } diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/ViewIndexer.java b/src/main/java/com/github/rnewson/couchdb/lucene/ViewIndexer.java index ca81d0dc..834e8d10 100644 --- a/src/main/java/com/github/rnewson/couchdb/lucene/ViewIndexer.java +++ b/src/main/java/com/github/rnewson/couchdb/lucene/ViewIndexer.java @@ -336,10 +336,8 @@ public void run() { } private void index() throws IOException { - UUID uuid = null; - try { - uuid = database.getUuid(); - } catch (final IOException e) { + UUID uuid = database.getUuid(); + if (uuid == null) { database.createUuid(); uuid = database.getUuid(); } diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/couchdb/Database.java b/src/main/java/com/github/rnewson/couchdb/lucene/couchdb/Database.java index e77f48be..7b2c80fa 100644 --- a/src/main/java/com/github/rnewson/couchdb/lucene/couchdb/Database.java +++ b/src/main/java/com/github/rnewson/couchdb/lucene/couchdb/Database.java @@ -24,7 +24,9 @@ import net.sf.json.JSONArray; import net.sf.json.JSONObject; +import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; +import org.apache.http.client.HttpResponseException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; @@ -51,8 +53,8 @@ public boolean delete() throws IOException { } public List getAllDesignDocuments() throws IOException { - final String body = HttpUtils.get(httpClient, url - + "_all_docs?startkey=%%22_design%%22&endkey=%%22_design9%%22&include_docs=true"); + final String body = HttpUtils.get(httpClient, String.format("%s_all_docs?startkey=%s&endkey=%s&include_docs=true", url, + Utils.urlEncode("\"_design\""), Utils.urlEncode("\"_design0\""))); final JSONObject json = JSONObject.fromObject(body); return toDesignDocuments(json); } @@ -98,8 +100,17 @@ public boolean saveDocument(final String id, final String body) throws IOExcepti } public UUID getUuid() throws IOException { - final CouchDocument local = getDocument("_local/lucene"); - return UUID.fromString(local.asJson().getString("uuid")); + try { + final CouchDocument local = getDocument("_local/lucene"); + return UUID.fromString(local.asJson().getString("uuid")); + } catch (final HttpResponseException e) { + switch (e.getStatusCode()) { + case HttpStatus.SC_NOT_FOUND: + return null; + default: + throw e; + } + } } public void createUuid() throws IOException {