Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
more fixes for new _cleanup method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Jan 31, 2010
1 parent 8f5e557 commit b7c592f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion couchdb-external-hook.py
Expand Up @@ -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()

Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/github/rnewson/couchdb/lucene/AdminServlet.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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()) {
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b7c592f

Please sign in to comment.