Permalink
Browse files

more fixes for new _cleanup method.

  • Loading branch information...
1 parent 8f5e557 commit b7c592f407e5e087f57d61531039f7b09fb152e7 Robert Newson committed Jan 31, 2010
View
@@ -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()
@@ -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 @@
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,15 +138,19 @@ 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));
final Set<String> dbKeep = new HashSet<String>();
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<String> viewKeep = new HashSet<String>();
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);
}
}
@@ -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();
}
@@ -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<DesignDocument> 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 {

0 comments on commit b7c592f

Please sign in to comment.