diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/LuceneServlet.java b/src/main/java/com/github/rnewson/couchdb/lucene/LuceneServlet.java index d8c70398..999cb6a3 100644 --- a/src/main/java/com/github/rnewson/couchdb/lucene/LuceneServlet.java +++ b/src/main/java/com/github/rnewson/couchdb/lucene/LuceneServlet.java @@ -48,54 +48,53 @@ public final class LuceneServlet extends HttpServlet { - private static final Logger LOG = Logger.getLogger(LuceneServlet.class); - - private static final long serialVersionUID = 1L; - - private final HttpClient client; - - private final Map indexers = new HashMap(); - - private final HierarchicalINIConfiguration ini; - - private final File root; - - private final Map threads = new HashMap(); - - public LuceneServlet() throws ConfigurationException, IOException { - final Config config = new Config(); - this.client = config.getClient(); - this.root = config.getDir(); - this.ini = config.getConfiguration(); - } - - public LuceneServlet(final HttpClient client, final File root, - final HierarchicalINIConfiguration ini) { - this.client = client; - this.root = root; - this.ini = ini; - } - - private void cleanup(final HttpServletRequest req, - final HttpServletResponse resp) throws IOException, JSONException { - final Couch couch = getCouch(req); - final Set dbKeep = new HashSet(); - final JSONArray databases = couch.getAllDatabases(); - for (int i=0; i< databases.length(); i++) { - final Database db = couch.getDatabase(databases.getString(i)); - final UUID uuid = db.getUuid(); - if (uuid == null) { - continue; - } - dbKeep.add(uuid.toString()); - - final Set viewKeep = new HashSet(); - - for (final DesignDocument ddoc : db.getAllDesignDocuments()) { - for (final View view : ddoc.getAllViews().values()) { - viewKeep.add(view.getDigest()); - } - } + private static final Logger LOG = Logger.getLogger(LuceneServlet.class); + + private static final long serialVersionUID = 1L; + + private final HttpClient client; + + private final Map indexers = new HashMap(); + + private final HierarchicalINIConfiguration ini; + + private final File root; + + private final Map threads = new HashMap(); + + public LuceneServlet() throws ConfigurationException, IOException { + final Config config = new Config(); + this.client = config.getClient(); + this.root = config.getDir(); + this.ini = config.getConfiguration(); + } + public LuceneServlet(final HttpClient client, final File root, + final HierarchicalINIConfiguration ini) { + this.client = client; + this.root = root; + this.ini = ini; + } + + private void cleanup(final HttpServletRequest req, + final HttpServletResponse resp) throws IOException, JSONException { + final Couch couch = getCouch(req); + final Set dbKeep = new HashSet(); + final JSONArray databases = couch.getAllDatabases(); + for (int i=0; i< databases.length(); i++) { + final Database db = couch.getDatabase(databases.getString(i)); + final UUID uuid = db.getUuid(); + if (uuid == null) { + continue; + } + dbKeep.add(uuid.toString()); + + final Set viewKeep = new HashSet(); + + for (final DesignDocument ddoc : db.getAllDesignDocuments()) { + for (final View view : ddoc.getAllViews().values()) { + viewKeep.add(view.getDigest()); + } + } // Delete all indexes except the keepers. final File[] dirs = DatabaseIndexer.uuidDir(root, db.getUuid()).listFiles(); @@ -112,127 +111,127 @@ private void cleanup(final HttpServletRequest req, } } } - } - - // Delete all directories except the keepers. - for (final File dir : root.listFiles()) { - if (!dbKeep.contains(dir.getName())) { - LOG.info("Cleaning old index at " + dir); - FileUtils.deleteDirectory(dir); - } - } - - resp.setStatus(202); - ServletUtils.sendJsonSuccess(req, resp); - } - - private Couch getCouch(final HttpServletRequest req) throws IOException { - final Configuration section = ini.getSection(new PathParts(req) - .getKey()); - final String url = section.containsKey("url") ? section - .getString("url") : null; - return new Couch(client, url); - } - - private synchronized DatabaseIndexer getIndexer(final Database database) - throws IOException, JSONException { - DatabaseIndexer result = indexers.get(database); - Thread thread = threads.get(database); - if (result == null || thread == null || !thread.isAlive()) { - result = new DatabaseIndexer(client, root, database, ini); - thread = new Thread(result); - thread.start(); - result.awaitInitialization(); - if (result.isClosed()) { - return null; - } else { - indexers.put(database, result); - threads.put(database, thread); - } - } - - return result; - } - - private DatabaseIndexer getIndexer(final HttpServletRequest req) - throws IOException, JSONException { - final Couch couch = getCouch(req); - final Database database = couch.getDatabase(new PathParts(req) - .getDatabaseName()); - return getIndexer(database); - } - - private void handleWelcomeReq(final HttpServletRequest req, - final HttpServletResponse resp) throws ServletException, - IOException, JSONException { - final Package p = this.getClass().getPackage(); - final JSONObject welcome = new JSONObject(); - welcome.put("couchdb-lucene", "Welcome"); - welcome.put("version", p.getImplementationVersion()); - ServletUtils.sendJson(req, resp, welcome); - } - - @Override - protected void doGet(final HttpServletRequest req, - final HttpServletResponse resp) throws ServletException, - IOException { - try { + } + + // Delete all directories except the keepers. + for (final File dir : root.listFiles()) { + if (!dbKeep.contains(dir.getName())) { + LOG.info("Cleaning old index at " + dir); + FileUtils.deleteDirectory(dir); + } + } + + resp.setStatus(202); + ServletUtils.sendJsonSuccess(req, resp); + } + + private Couch getCouch(final HttpServletRequest req) throws IOException { + final Configuration section = ini.getSection(new PathParts(req) + .getKey()); + final String url = section.containsKey("url") ? section + .getString("url") : null; + return new Couch(client, url); + } + + private synchronized DatabaseIndexer getIndexer(final Database database) + throws IOException, JSONException { + DatabaseIndexer result = indexers.get(database); + Thread thread = threads.get(database); + if (result == null || thread == null || !thread.isAlive()) { + result = new DatabaseIndexer(client, root, database, ini); + thread = new Thread(result); + thread.start(); + result.awaitInitialization(); + if (result.isClosed()) { + return null; + } else { + indexers.put(database, result); + threads.put(database, thread); + } + } + + return result; + } + + private DatabaseIndexer getIndexer(final HttpServletRequest req) + throws IOException, JSONException { + final Couch couch = getCouch(req); + final Database database = couch.getDatabase(new PathParts(req) + .getDatabaseName()); + return getIndexer(database); + } + + private void handleWelcomeReq(final HttpServletRequest req, + final HttpServletResponse resp) throws ServletException, + IOException, JSONException { + final Package p = this.getClass().getPackage(); + final JSONObject welcome = new JSONObject(); + welcome.put("couchdb-lucene", "Welcome"); + welcome.put("version", p.getImplementationVersion()); + ServletUtils.sendJson(req, resp, welcome); + } + + @Override + protected void doGet(final HttpServletRequest req, + final HttpServletResponse resp) throws ServletException, + IOException { + try { doGetInternal(req, resp); } catch (final JSONException e) { resp.sendError(500); } - } + } private void doGetInternal(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException, JSONException { switch (StringUtils.countMatches(req.getRequestURI(), "/")) { - case 1: - handleWelcomeReq(req, resp); - return; - case 5: - final DatabaseIndexer indexer = getIndexer(req); - if (indexer == null) { - ServletUtils.sendJsonError(req, resp, 500, "error_creating_index"); - return; - } - - if (req.getParameter("q") == null) { - indexer.info(req, resp); - } else { - indexer.search(req, resp); - } - return; - } - - ServletUtils.sendJsonError(req, resp, 400, "bad_request"); + case 1: + handleWelcomeReq(req, resp); + return; + case 5: + final DatabaseIndexer indexer = getIndexer(req); + if (indexer == null) { + ServletUtils.sendJsonError(req, resp, 500, "error_creating_index"); + return; + } + + if (req.getParameter("q") == null) { + indexer.info(req, resp); + } else { + indexer.search(req, resp); + } + return; + } + + ServletUtils.sendJsonError(req, resp, 400, "bad_request"); } - @Override - protected void doPost(final HttpServletRequest req, - final HttpServletResponse resp) throws ServletException, - IOException { - try { + @Override + protected void doPost(final HttpServletRequest req, + final HttpServletResponse resp) throws ServletException, + IOException { + try { doPostInternal(req, resp); } catch (final JSONException e) { resp.sendError(500); } - } + } private void doPostInternal(final HttpServletRequest req, final HttpServletResponse resp) throws IOException, JSONException { switch (StringUtils.countMatches(req.getRequestURI(), "/")) { - case 3: - if (req.getPathInfo().endsWith("/_cleanup")) { - cleanup(req, resp); - return; - } - break; - case 6: - final DatabaseIndexer indexer = getIndexer(req); - indexer.admin(req, resp); - return; - } - ServletUtils.sendJsonError(req, resp, 400, "bad_request"); + case 3: + if (req.getPathInfo().endsWith("/_cleanup")) { + cleanup(req, resp); + return; + } + break; + case 6: + final DatabaseIndexer indexer = getIndexer(req); + indexer.admin(req, resp); + return; + } + ServletUtils.sendJsonError(req, resp, 400, "bad_request"); } }