IndexDatabase.java revision 359
207N/A/*
207N/A * CDDL HEADER START
207N/A *
207N/A * The contents of this file are subject to the terms of the
207N/A * Common Development and Distribution License (the "License").
207N/A * You may not use this file except in compliance with the License.
207N/A *
207N/A * See LICENSE.txt included in this distribution for the specific
207N/A * language governing permissions and limitations under the License.
207N/A *
207N/A * When distributing Covered Code, include this CDDL HEADER in each
207N/A * file and include the License file at LICENSE.txt.
207N/A * If applicable, add the following below this CDDL HEADER, with the
207N/A * fields enclosed by brackets "[]" replaced with your own identifying
207N/A * information: Portions Copyright [yyyy] [name of copyright owner]
207N/A *
207N/A * CDDL HEADER END
207N/A */
207N/A
207N/A/*
207N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
207N/A * Use is subject to license terms.
207N/A */
207N/Apackage org.opensolaris.opengrok.index;
207N/A
207N/Aimport java.io.BufferedInputStream;
207N/Aimport java.io.File;
207N/Aimport java.io.FileInputStream;
207N/Aimport java.io.FileNotFoundException;
207N/Aimport java.io.IOException;
207N/Aimport java.io.InputStream;
207N/Aimport java.util.ArrayList;
207N/Aimport java.util.Arrays;
282N/Aimport java.util.Comparator;
207N/Aimport java.util.List;
261N/Aimport java.util.concurrent.ExecutorService;
320N/Aimport java.util.logging.Level;
312N/Aimport java.util.logging.Logger;
207N/Aimport org.apache.lucene.document.DateTools;
207N/Aimport org.apache.lucene.document.Document;
207N/Aimport org.apache.lucene.index.IndexReader;
207N/Aimport org.apache.lucene.index.IndexWriter;
207N/Aimport org.apache.lucene.index.Term;
207N/Aimport org.apache.lucene.index.TermEnum;
207N/Aimport org.apache.lucene.search.spell.LuceneDictionary;
207N/Aimport org.apache.lucene.search.spell.SpellChecker;
207N/Aimport org.apache.lucene.store.FSDirectory;
207N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
207N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer;
207N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer.Genre;
207N/Aimport org.opensolaris.opengrok.configuration.Project;
207N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
480N/Aimport org.opensolaris.opengrok.web.Util;
207N/A
207N/A/**
207N/A * This class is used to create / update the index databases. Currently we use
207N/A * one index database per project.
207N/A *
207N/A * @author Trond Norbye
207N/A */
207N/Apublic class IndexDatabase {
207N/A
207N/A private Project project;
207N/A private FSDirectory indexDirectory;
207N/A private FSDirectory spellDirectory;
207N/A private IndexWriter writer;
207N/A private IndexReader reader;
207N/A private TermEnum uidIter;
207N/A private IgnoredNames ignoredNames;
207N/A private AnalyzerGuru analyzerGuru;
207N/A private File xrefDir;
207N/A private boolean interrupted;
207N/A private List<IndexChangedListener> listeners;
253N/A private File dirtyFile;
359N/A private final Object lock = new Object();
207N/A private boolean dirty;
359N/A private boolean running;
274N/A private List<String> directories;
320N/A private static final Logger log = Logger.getLogger(IndexDatabase.class.getName());
274N/A
207N/A /**
207N/A * Create a new instance of the Index Database. Use this constructor if
207N/A * you don't use any projects
207N/A *
207N/A * @throws java.io.IOException if an error occurs while creating directories
207N/A */
207N/A public IndexDatabase() throws IOException {
207N/A initialize();
207N/A }
207N/A
207N/A /**
207N/A * Create a new instance of an Index Database for a given project
207N/A * @param project the project to create the database for
207N/A * @throws java.io.IOException if an errror occurs while creating directories
207N/A */
207N/A public IndexDatabase(Project project) throws IOException {
207N/A this.project = project;
207N/A initialize();
207N/A }
207N/A
207N/A /**
207N/A * Update the index database for all of the projects. Print progress to
207N/A * standard out.
261N/A * @param executor An executor to run the job
459N/A * @throws java.lang.Exception if an error occurs
207N/A */
459N/A public static void updateAll(ExecutorService executor) throws Exception {
261N/A updateAll(executor, null);
207N/A }
207N/A
207N/A /**
207N/A * Update the index database for all of the projects
261N/A * @param executor An executor to run the job
207N/A * @param listener where to signal the changes to the database
459N/A * @throws java.lang.Exception if an error occurs
207N/A */
312N/A static void updateAll(ExecutorService executor, IndexChangedListener listener) throws IOException {
207N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
564N/A if (env.hasProjects()) {
564N/A for (Project project : env.getProjects()) {
207N/A final IndexDatabase db = new IndexDatabase(project);
207N/A if (listener != null) {
564N/A db.addIndexChangedListener(listener);
207N/A }
207N/A executor.submit(new Runnable() {
564N/A
564N/A public void run() {
564N/A try {
564N/A db.update();
564N/A } catch (Exception e) {
207N/A log.log(Level.FINE,"Problem updating lucene index database: ",e);
207N/A }
207N/A }
261N/A });
261N/A }
261N/A } else {
261N/A final IndexDatabase db = new IndexDatabase();
261N/A if (listener != null) {
261N/A db.addIndexChangedListener(listener);
261N/A }
320N/A
261N/A executor.submit(new Runnable() {
261N/A
261N/A public void run() {
207N/A try {
207N/A db.update();
207N/A } catch (Exception e) {
460N/A log.log(Level.FINE,"Problem updating lucene index database: ",e);
580N/A }
580N/A }
580N/A });
580N/A }
580N/A
580N/A }
580N/A
580N/A private synchronized void initialize() throws IOException {
580N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
580N/A File indexDir = new File(env.getDataRootFile(), "index");
580N/A File spellDir = new File(env.getDataRootFile(), "spellIndex");
580N/A if (project != null) {
580N/A indexDir = new File(indexDir, project.getPath());
580N/A spellDir = new File(spellDir, project.getPath());
580N/A }
580N/A
207N/A if (!indexDir.exists()) {
580N/A if (!indexDir.mkdirs()) {
580N/A // to avoid race conditions, just recheck..
580N/A if (!indexDir.exists()) {
580N/A throw new FileNotFoundException("Failed to create root directory [" + indexDir.getAbsolutePath() + "]");
580N/A }
580N/A }
580N/A }
580N/A
207N/A if (!spellDir.exists()) {
580N/A if (!spellDir.mkdirs()) {
580N/A if (!spellDir.exists()) {
580N/A throw new FileNotFoundException("Failed to create root directory [" + spellDir.getAbsolutePath() + "]");
580N/A }
580N/A }
580N/A }
580N/A
580N/A if (!env.isUsingLuceneLocking()) {
580N/A FSDirectory.setDisableLocks(true);
580N/A }
580N/A indexDirectory = FSDirectory.getDirectory(indexDir);
359N/A spellDirectory = FSDirectory.getDirectory(spellDir);
207N/A ignoredNames = env.getIgnoredNames();
207N/A analyzerGuru = new AnalyzerGuru();
207N/A if (env.isGenerateHtml()) {
274N/A xrefDir = new File(env.getDataRootFile(), "xref");
274N/A }
274N/A listeners = new ArrayList<IndexChangedListener>();
274N/A dirtyFile = new File(indexDir, "dirty");
274N/A dirty = dirtyFile.exists();
297N/A directories = new ArrayList<String>();
274N/A }
464N/A
274N/A /**
439N/A * By default the indexer will traverse all directories in the project.
439N/A * If you add directories with this function update will just process
439N/A * the specified directories.
464N/A *
439N/A * @param dir The directory to scan
297N/A * @return <code>true</code> if the file is added, false oth
439N/A */
460N/A public boolean addDirectory(String dir) {
439N/A if (dir.startsWith("\\")) {
274N/A dir = dir.replace('\\', '/');
460N/A } else if (!dir.startsWith("/")) {
460N/A dir = "/" + dir;
274N/A }
274N/A File file = new File(RuntimeEnvironment.getInstance().getSourceRootFile(), dir);
274N/A if (!file.exists()) {
274N/A return false;
207N/A } else {
459N/A directories.add(dir);
207N/A return true;
459N/A }
359N/A }
359N/A
459N/A /**
359N/A * Update the content of this index database
359N/A * @throws java.lang.Exception if an error occurs
359N/A */
359N/A public void update() throws Exception {
207N/A synchronized (lock) {
207N/A if (running) {
255N/A throw new Exception("Indexer already running!");
207N/A }
456N/A running = true;
460N/A interrupted = false;
460N/A }
460N/A try {
274N/A writer = new IndexWriter(indexDirectory, AnalyzerGuru.getAnalyzer());
274N/A writer.setMaxFieldLength(RuntimeEnvironment.getInstance().getIndexWordLimit());
207N/A
274N/A if (directories.size() == 0) {
274N/A if (project != null) {
274N/A directories.add(project.getPath());
456N/A } else {
274N/A directories.add("");
274N/A }
274N/A }
274N/A
274N/A for (String dir : directories) {
274N/A File sourceRoot;
457N/A if (dir.equals("")) {
457N/A sourceRoot = RuntimeEnvironment.getInstance().getSourceRootFile();
457N/A } else {
207N/A sourceRoot = new File(RuntimeEnvironment.getInstance().getSourceRootFile(), dir);
457N/A }
207N/A
457N/A String startuid = Util.uid(dir, "");
457N/A reader = IndexReader.open(indexDirectory); // open existing index
457N/A uidIter = reader.terms(new Term("u", startuid)); // init uid iterator
457N/A
457N/A indexDown(sourceRoot, dir);
457N/A
274N/A while (uidIter.term() != null && uidIter.term().field().equals("u") && uidIter.term().text().startsWith(startuid)) {
207N/A removeFile();
207N/A uidIter.next();
207N/A }
207N/A reader.close();
207N/A }
207N/A } finally {
508N/A if (reader != null) {
207N/A try {
207N/A reader.close();
359N/A } catch (IOException e) {
359N/A }
359N/A }
207N/A if (writer != null) {
207N/A try {
359N/A writer.close();
253N/A } catch (IOException e) {
253N/A }
253N/A }
207N/A synchronized (lock) {
207N/A running = false;
207N/A }
207N/A }
207N/A
270N/A if (!isInterrupted() && isDirty()) {
270N/A if (RuntimeEnvironment.getInstance().isOptimizeDatabase()) {
459N/A optimize();
270N/A }
312N/A createSpellingSuggestions();
564N/A }
270N/A }
270N/A
270N/A /**
564N/A * Optimize all index databases
270N/A * @param executor An executor to run the job
270N/A * @throws java.lang.Exception if an error occurs
564N/A */
564N/A static void optimizeAll(ExecutorService executor) throws IOException {
564N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
564N/A if (env.hasProjects()) {
564N/A for (Project project : env.getProjects()) {
359N/A final IndexDatabase db = new IndexDatabase(project);
270N/A if (db.isDirty()) {
270N/A executor.submit(new Runnable() {
270N/A
270N/A public void run() {
270N/A try {
459N/A db.optimize();
320N/A } catch (Exception e) {
270N/A log.log(Level.FINE,"Problem optimizing lucene index database: ",e);
270N/A }
270N/A }
270N/A });
270N/A }
270N/A }
270N/A } else {
270N/A final IndexDatabase db = new IndexDatabase();
207N/A if (db.isDirty()) {
207N/A executor.submit(new Runnable() {
207N/A
359N/A public void run() {
359N/A try {
359N/A db.update();
359N/A } catch (Exception e) {
359N/A log.log(Level.FINE,"Problem updating lucene index database: ",e);
359N/A }
359N/A }
207N/A });
207N/A }
207N/A }
320N/A }
207N/A
207N/A /**
207N/A * Optimize the index database
207N/A */
320N/A public void optimize() {
207N/A synchronized (lock) {
359N/A if (running) {
460N/A log.warning("Optimize terminated... Someone else is updating / optimizing it!");
460N/A return ;
460N/A }
359N/A running = true;
359N/A }
359N/A IndexWriter wrt = null;
207N/A try {
320N/A if (RuntimeEnvironment.getInstance().isVerbose()) {
207N/A log.info("Optimizing the index ... ");
207N/A }
207N/A wrt = new IndexWriter(indexDirectory, null, false);
207N/A wrt.optimize();
207N/A if (RuntimeEnvironment.getInstance().isVerbose()) {
508N/A log.info("done");
207N/A }
207N/A synchronized (lock) {
359N/A if (dirtyFile.exists()) {
359N/A if (!dirtyFile.delete()) {
359N/A log.fine("Failed to remove \"dirty-file\": " +
207N/A dirtyFile.getAbsolutePath());
207N/A }
207N/A }
207N/A dirty = false;
207N/A }
207N/A } catch (IOException e) {
207N/A log.severe("ERROR: optimizing index: " + e);
207N/A } finally {
207N/A if (wrt != null) {
207N/A try {
207N/A wrt.close();
207N/A } catch (IOException e) {
320N/A }
207N/A }
207N/A synchronized (lock) {
207N/A running = false;
207N/A }
207N/A }
320N/A }
207N/A
207N/A /**
320N/A * Generate a spelling suggestion for the definitions stored in defs
207N/A */
207N/A public void createSpellingSuggestions() {
207N/A IndexReader indexReader = null;
207N/A SpellChecker checker = null;
207N/A
508N/A try {
207N/A if (RuntimeEnvironment.getInstance().isVerbose()) {
207N/A log.info("Generating spelling suggestion index ... ");
207N/A }
207N/A indexReader = IndexReader.open(indexDirectory);
207N/A checker = new SpellChecker(spellDirectory);
207N/A checker.indexDictionary(new LuceneDictionary(indexReader, "defs"));
207N/A if (RuntimeEnvironment.getInstance().isVerbose()) {
207N/A log.info("done");
359N/A }
359N/A } catch (IOException e) {
359N/A log.severe("ERROR: Generating spelling: " + e);
359N/A } finally {
359N/A if (indexReader != null) {
359N/A try {
359N/A indexReader.close();
359N/A } catch (IOException e) {
359N/A }
460N/A }
460N/A if (spellDirectory != null) {
460N/A spellDirectory.close();
359N/A }
359N/A }
359N/A }
359N/A
359N/A private boolean isDirty() {
253N/A synchronized (lock) {
253N/A return dirty;
253N/A }
207N/A }
207N/A
207N/A private void setDirty() {
207N/A synchronized (lock) {
207N/A try {
207N/A if (!dirty) {
207N/A if (!dirtyFile.createNewFile()) {
207N/A if (!dirtyFile.exists()) {
207N/A log.log(Level.FINE, "Failed to create \"dirty-file\": ", dirtyFile.getAbsolutePath());
207N/A }
207N/A }
207N/A dirty = true;
207N/A }
515N/A } catch (IOException e) {
515N/A log.log(Level.FINE,"When creating dirty file: ",e);
515N/A }
515N/A }
515N/A }
515N/A /**
359N/A * Remove a stale file (uidIter.term().text()) from the index database
359N/A * (and the xref file)
602N/A * @throws java.io.IOException if an error occurs
359N/A */
359N/A private void removeFile() throws IOException {
359N/A String path = Util.uid2url(uidIter.term().text());
359N/A
506N/A for (IndexChangedListener listener : listeners) {
506N/A listener.fileRemoved(path);
506N/A }
506N/A writer.deleteDocuments(uidIter.term());
359N/A
253N/A File xrefFile = new File(xrefDir, path);
207N/A File parent = xrefFile.getParentFile();
207N/A
207N/A if (!xrefFile.delete()) {
207N/A log.info("Failed to remove obsolete xref-file: " +
207N/A xrefFile.getAbsolutePath());
207N/A }
207N/A
207N/A if (!parent.delete()) {
594N/A // Ignore. The directory is most likely not empty, but to check
594N/A // would just increase the disk IO even more..
594N/A }
212N/A
553N/A setDirty();
207N/A }
553N/A
553N/A /**
553N/A * Add a file to the Lucene index (and generate a xref file)
553N/A * @param file The file to add
553N/A * @param path The path to the file (from source root)
553N/A * @throws java.io.IOException if an error occurs
553N/A */
553N/A private void addFile(File file, String path) throws IOException {
553N/A InputStream in;
553N/A try {
553N/A in = new BufferedInputStream(new FileInputStream(file));
553N/A } catch (IOException ex) {
553N/A log.warning("Warning: " + ex.getMessage());
553N/A return;
553N/A }
553N/A FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, path);
506N/A
553N/A for (IndexChangedListener listener : listeners) {
593N/A listener.fileAdded(path, fa.getClass().getSimpleName());
593N/A }
593N/A
207N/A Document d = analyzerGuru.getDocument(file, in, path, fa);
553N/A if (d != null) {
594N/A writer.addDocument(d, fa);
508N/A Genre g = fa.getFactory().getGenre();
207N/A if (xrefDir != null && (g == Genre.PLAIN || g == Genre.XREFABLE)) {
207N/A File xrefFile = new File(xrefDir, path);
207N/A if (!xrefFile.getParentFile().mkdirs()) {
207N/A // The failure was most likely because the file already
207N/A // existed. But to check for the file first and only
207N/A // add it if it doesn't exists would only increase the
207N/A // file IO...
207N/A }
207N/A fa.writeXref(xrefDir, path);
207N/A }
207N/A setDirty();
207N/A } else {
207N/A log.warning("Warning: did not add " + path);
320N/A }
207N/A
207N/A try { in.close(); } catch (Exception e) {}
207N/A }
207N/A
207N/A /**
310N/A * Check if I should accept this file into the index database
310N/A * @param file the file to check
310N/A * @return true if the file should be included, false otherwise
310N/A */
310N/A private boolean accept(File file) {
320N/A if (ignoredNames.ignore(file)) {
310N/A return false;
310N/A }
310N/A
207N/A if (!file.canRead()) {
207N/A log.warning("Warning: could not read " + file.getAbsolutePath());
320N/A return false;
320N/A }
207N/A
207N/A try {
504N/A if (!file.getAbsolutePath().equals(file.getCanonicalPath())) {
504N/A if (file.getParentFile().equals(file.getCanonicalFile().getParentFile())) {
504N/A // Lets support symlinks within the same directory, this
480N/A // should probably be extended to within the same repository
480N/A return true;
504N/A } else {
504N/A log.warning("Warning: ignored non-local symlink " + file.getAbsolutePath() +
504N/A " -> " + file.getCanonicalPath());
504N/A return false;
504N/A }
504N/A }
504N/A } catch (IOException exp) {
207N/A log.warning("Warning: Failed to resolve name: " + file.getAbsolutePath());
207N/A log.log(Level.FINE,"Stack Trace: ",exp);
207N/A }
207N/A
207N/A return true;
207N/A }
207N/A
207N/A /**
359N/A * Generate indexes recursively
207N/A * @param dir the root indexDirectory to generate indexes for
207N/A * @param path the path
207N/A */
207N/A private void indexDown(File dir, String parent) throws IOException {
207N/A if (isInterrupted()) {
207N/A return;
207N/A }
207N/A
207N/A if (!accept(dir)) {
320N/A return;
207N/A }
207N/A
282N/A File[] files = dir.listFiles();
282N/A if (files == null) {
282N/A log.severe("Failed to get file listing for: " + dir.getAbsolutePath());
282N/A return;
282N/A }
282N/A Arrays.sort(files, new Comparator<File>() {
207N/A
207N/A public int compare(File p1, File p2) {
207N/A return p1.getName().compareTo(p2.getName());
207N/A }
207N/A });
207N/A
207N/A for (File file : files) {
594N/A if (accept(file)) {
207N/A String path = parent + '/' + file.getName();
207N/A if (file.isDirectory()) {
207N/A indexDown(file, path);
207N/A } else {
207N/A if (uidIter != null) {
207N/A String uid = Util.uid(path, DateTools.timeToString(file.lastModified(), DateTools.Resolution.MILLISECOND)); // construct uid for doc
207N/A while (uidIter.term() != null && uidIter.term().field().equals("u") &&
207N/A uidIter.term().text().compareTo(uid) < 0) {
207N/A removeFile();
207N/A uidIter.next();
594N/A }
207N/A
207N/A if (uidIter.term() != null && uidIter.term().field().equals("u") &&
594N/A uidIter.term().text().compareTo(uid) == 0) {
594N/A uidIter.next(); // keep matching docs
594N/A } else {
594N/A addFile(file, path);
594N/A }
594N/A } else {
594N/A addFile(file, path);
207N/A }
207N/A }
207N/A }
207N/A }
207N/A }
207N/A
207N/A /**
207N/A * Interrupt the index generation (and the index generation will stop as
207N/A * soon as possible)
207N/A */
359N/A public void interrupt() {
359N/A synchronized (lock) {
359N/A interrupted = true;
359N/A }
359N/A }
359N/A
359N/A private boolean isInterrupted() {
359N/A synchronized (lock) {
359N/A return interrupted;
207N/A }
207N/A }
207N/A
207N/A /**
207N/A * Register an object to receive events when modifications is done to the
207N/A * index database.
207N/A *
207N/A * @param listener the object to receive the events
316N/A */
207N/A public void addIndexChangedListener(IndexChangedListener listener) {
207N/A listeners.add(listener);
207N/A }
207N/A
207N/A /**
207N/A * Remove an object from the lists of objects to receive events when
207N/A * modifications is done to the index database
207N/A *
207N/A * @param listener the object to remove
316N/A */
207N/A public void removeIndexChangedListener(IndexChangedListener listener) {
207N/A listeners.remove(listener);
207N/A }
207N/A
207N/A /**
359N/A * List all files in all of the index databases
207N/A * @throws IOException if an error occurs
312N/A */
207N/A public static void listAllFiles() throws IOException {
207N/A listAllFiles(null);
207N/A }
207N/A
207N/A /**
207N/A * List all files in some of the index databases
207N/A * @param subFiles Subdirectories for the various projects to list the files
359N/A * for (or null or an empty list to dump all projects)
207N/A * @throws IOException if an error occurs
312N/A */
207N/A public static void listAllFiles(List<String> subFiles) throws IOException {
460N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
207N/A if (!env.hasProjects()) {
207N/A IndexDatabase db = new IndexDatabase();
207N/A db.listFiles();
207N/A } else {
207N/A if (subFiles == null || subFiles.isEmpty()) {
207N/A for (Project project : env.getProjects()) {
207N/A IndexDatabase db = new IndexDatabase(project);
207N/A db.listFiles();
207N/A }
320N/A } else {
207N/A for (String path : subFiles) {
207N/A Project project = Project.getProject(path);
207N/A if (project == null) {
207N/A log.warning("Warning: Could not find a project for \"" + path + "\"");
207N/A } else {
207N/A IndexDatabase db = new IndexDatabase(project);
460N/A db.listFiles();
460N/A }
460N/A }
207N/A }
207N/A }
207N/A }
207N/A
207N/A /**
207N/A * List all of the files in this index database
359N/A *
207N/A * @throws IOException If an IO error occurs while reading from the database
312N/A */
207N/A public void listFiles() throws IOException {
207N/A IndexReader ireader = null;
207N/A TermEnum iter = null;
207N/A
207N/A try {
207N/A ireader = IndexReader.open(indexDirectory); // open existing index
207N/A iter = ireader.terms(new Term("u", "")); // init uid iterator
320N/A while (iter.term() != null) {
207N/A log.info(Util.uid2url(iter.term().text()));
207N/A iter.next();
207N/A }
207N/A } finally {
207N/A if (iter != null) {
207N/A try {
459N/A iter.close();
580N/A } catch (Exception e) {
207N/A }
207N/A }
207N/A
207N/A if (ireader != null) {
207N/A try {
207N/A ireader.close();
459N/A } catch (Exception e) {
508N/A }
207N/A }
207N/A }
207N/A }
207N/A
207N/A static void listFrequentTokens() throws Exception {
459N/A listFrequentTokens(null);
207N/A }
207N/A
207N/A static void listFrequentTokens(ArrayList<String> subFiles) throws IOException {
457N/A final int limit = 4;
207N/A
207N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
207N/A if (!env.hasProjects()) {
460N/A IndexDatabase db = new IndexDatabase();
207N/A db.listTokens(limit);
207N/A } else {
207N/A if (subFiles == null || subFiles.isEmpty()) {
207N/A for (Project project : env.getProjects()) {
207N/A IndexDatabase db = new IndexDatabase(project);
207N/A db.listTokens(4);
207N/A }
207N/A } else {
207N/A for (String path : subFiles) {
320N/A Project project = Project.getProject(path);
207N/A if (project == null) {
207N/A log.warning("Warning: Could not find a project for \"" + path + "\"");
207N/A } else {
207N/A IndexDatabase db = new IndexDatabase(project);
207N/A db.listTokens(4);
207N/A }
460N/A }
460N/A }
460N/A }
207N/A }
207N/A
207N/A public void listTokens(int freq) throws IOException {
312N/A IndexReader ireader = null;
207N/A TermEnum iter = null;
207N/A
207N/A try {
207N/A ireader = IndexReader.open(indexDirectory);
207N/A iter = ireader.terms(new Term("defs", ""));
207N/A while (iter.term() != null) {
207N/A if (iter.term().field().startsWith("f")) {
207N/A if (iter.docFreq() > 16 && iter.term().text().length() > freq) {
207N/A log.warning(iter.term().text());
320N/A }
207N/A iter.next();
207N/A } else {
207N/A break;
207N/A }
207N/A }
207N/A } finally {
207N/A if (iter != null) {
207N/A try {
207N/A iter.close();
207N/A } catch (Exception e) {
459N/A }
580N/A }
207N/A
207N/A if (ireader != null) {
207N/A try {
207N/A ireader.close();
207N/A } catch (Exception e) {
207N/A }
459N/A }
508N/A }
207N/A }
207N/A
207N/A /**
207N/A * Get an indexReader for the Index database where a given file
208N/A * @param path the file to get the database for
208N/A * @return The index database where the file should be located or null if
208N/A * it cannot be located.
208N/A */
208N/A public static IndexReader getIndexReader(String path) {
208N/A IndexReader ret = null;
208N/A
208N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
208N/A File indexDir = new File(env.getDataRootFile(), "index");
208N/A
208N/A if (env.hasProjects()) {
208N/A Project p = Project.getProject(path);
208N/A if (p != null) {
208N/A indexDir = new File(indexDir, p.getPath());
208N/A } else {
460N/A return null;
460N/A }
460N/A }
208N/A
208N/A if (indexDir.exists() && IndexReader.indexExists(indexDir)) {
208N/A try {
208N/A ret = IndexReader.open(indexDir);
208N/A } catch (Exception ex) {
208N/A log.severe("Failed to open index: " + indexDir.getAbsolutePath());
208N/A log.log(Level.FINE,"Stack Trace: ",ex);
208N/A }
320N/A }
320N/A
208N/A return ret;
208N/A }
208N/A
208N/A @Override
208N/A public boolean equals(Object obj) {
274N/A if (obj == null) {
274N/A return false;
274N/A }
274N/A if (getClass() != obj.getClass()) {
274N/A return false;
274N/A }
274N/A final IndexDatabase other = (IndexDatabase) obj;
274N/A if (this.project != other.project && (this.project == null || !this.project.equals(other.project))) {
274N/A return false;
274N/A }
274N/A return true;
274N/A }
274N/A
274N/A @Override
274N/A public int hashCode() {
274N/A int hash = 7;
274N/A hash = 41 * hash + (this.project != null ? this.project.hashCode() : 0);
274N/A return hash;
274N/A }
460N/A
274N/A}
274N/A