/** * Optimize all index databases * * @param executor An executor to run the job * @throws IOException if an error occurs */ static void optimizeAll(ExecutorService executor) throws IOException { List<IndexDatabase> dbs = new ArrayList<IndexDatabase>(); RuntimeEnvironment env = RuntimeEnvironment.getInstance(); if (env.hasProjects()) { for (Project project : env.getProjects()) { dbs.add(new IndexDatabase(project)); } } else { dbs.add(new IndexDatabase()); } for (IndexDatabase d : dbs) { final IndexDatabase db = d; if (db.isDirty()) { executor.submit( new Runnable() { @Override public void run() { try { db.update(); } catch (Throwable e) { log.log(Level.SEVERE, "Problem updating lucene index database: ", e); } } }); } } }
/** * Update the index database for a number of sub-directories * * @param executor An executor to run the job * @param listener where to signal the changes to the database * @param paths * @throws IOException if an error occurs */ public static void update( ExecutorService executor, IndexChangedListener listener, List<String> paths) throws IOException { RuntimeEnvironment env = RuntimeEnvironment.getInstance(); List<IndexDatabase> dbs = new ArrayList<IndexDatabase>(); for (String path : paths) { Project project = Project.getProject(path); if (project == null && env.hasProjects()) { log.log(Level.WARNING, "Could not find a project for \"{0}\"", path); } else { IndexDatabase db; try { if (project == null) { db = new IndexDatabase(); } else { db = new IndexDatabase(project); } int idx = dbs.indexOf(db); if (idx != -1) { db = dbs.get(idx); } if (db.addDirectory(path)) { if (idx == -1) { dbs.add(db); } } else { log.log(Level.WARNING, "Directory does not exist \"{0}\"", path); } } catch (IOException e) { log.log(Level.WARNING, "An error occured while updating index", e); } } for (final IndexDatabase db : dbs) { db.addIndexChangedListener(listener); executor.submit( new Runnable() { @Override public void run() { try { db.update(); } catch (Throwable e) { log.log(Level.SEVERE, "An error occured while updating index", e); } } }); } } }
static void listFrequentTokens(List<String> subFiles) throws IOException { final int limit = 4; RuntimeEnvironment env = RuntimeEnvironment.getInstance(); if (env.hasProjects()) { if (subFiles == null || subFiles.isEmpty()) { for (Project project : env.getProjects()) { IndexDatabase db = new IndexDatabase(project); db.listTokens(4); } } else { for (String path : subFiles) { Project project = Project.getProject(path); if (project == null) { log.log(Level.WARNING, "Warning: Could not find a project for \"{0}\"", path); } else { IndexDatabase db = new IndexDatabase(project); db.listTokens(4); } } } } else { IndexDatabase db = new IndexDatabase(); db.listTokens(limit); } }