IndexDatabase.java revision 1327
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/*
1051N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
207N/A */
1051N/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;
1327N/A
1318N/Aimport org.apache.lucene.analysis.Analyzer;
1318N/Aimport org.apache.lucene.analysis.standard.StandardAnalyzer;
207N/Aimport org.apache.lucene.document.DateTools;
207N/Aimport org.apache.lucene.document.Document;
1127N/Aimport org.apache.lucene.document.Fieldable;
1327N/Aimport org.apache.lucene.index.IndexReader;
1327N/Aimport org.apache.lucene.index.IndexWriter;
1327N/Aimport org.apache.lucene.index.IndexWriterConfig;
1318N/Aimport org.apache.lucene.index.IndexWriterConfig.OpenMode;
1327N/Aimport org.apache.lucene.index.Term;
1327N/Aimport org.apache.lucene.index.TermEnum;
1127N/Aimport org.apache.lucene.queryParser.ParseException;
1127N/Aimport org.apache.lucene.search.IndexSearcher;
1127N/Aimport org.apache.lucene.search.Query;
1127N/Aimport org.apache.lucene.search.TopDocs;
207N/Aimport org.apache.lucene.search.spell.LuceneDictionary;
207N/Aimport org.apache.lucene.search.spell.SpellChecker;
207N/Aimport org.apache.lucene.store.FSDirectory;
928N/Aimport org.apache.lucene.store.LockFactory;
928N/Aimport org.apache.lucene.store.NoLockFactory;
928N/Aimport org.apache.lucene.store.SimpleFSLockFactory;
207N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
656N/Aimport org.opensolaris.opengrok.analysis.Ctags;
1127N/Aimport org.opensolaris.opengrok.analysis.Definitions;
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;
678N/Aimport org.opensolaris.opengrok.history.HistoryException;
480N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
1127N/Aimport org.opensolaris.opengrok.search.QueryBuilder;
1318N/Aimport org.opensolaris.opengrok.search.SearchEngine;
1327N/Aimport org.opensolaris.opengrok.util.IOUtils;
207N/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
1190N/A * one index database per project.
1190N/A *
207N/A * @author Trond Norbye
928N/A * @author Lubos Kosco , update for lucene 3.0.0
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 TermEnum uidIter;
207N/A private IgnoredNames ignoredNames;
1026N/A private Filter includedNames;
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;
1327N/A static final Logger logger = Logger.getLogger(IndexDatabase.class.getName());
656N/A private Ctags ctags;
928N/A private LockFactory lockfact;
656N/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
1190N/A *
207N/A * @throws java.io.IOException if an error occurs while creating directories
207N/A */
207N/A public IndexDatabase() throws IOException {
1190N/A this(null);
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 */
1190N/A public IndexDatabase(Project project) throws IOException {
207N/A this.project = project;
928N/A lockfact = new SimpleFSLockFactory();
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 IOException if an error occurs
207N/A */
459N/A public static void updateAll(ExecutorService executor) throws IOException {
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 IOException 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 List<IndexDatabase> dbs = new ArrayList<IndexDatabase>();
1190N/A
207N/A if (env.hasProjects()) {
207N/A for (Project project : env.getProjects()) {
564N/A dbs.add(new IndexDatabase(project));
207N/A }
207N/A } else {
564N/A dbs.add(new IndexDatabase());
564N/A }
1190N/A
564N/A for (IndexDatabase d : dbs) {
564N/A final IndexDatabase db = d;
207N/A if (listener != null) {
207N/A db.addIndexChangedListener(listener);
207N/A }
1190N/A
261N/A executor.submit(new Runnable() {
261N/A
1054N/A @Override
261N/A public void run() {
261N/A try {
261N/A db.update();
1264N/A } catch (Throwable e) {
1327N/A logger.warning("Problem updating " + db + ": "+ e.getMessage());
1327N/A logger.log(Level.FINE, "updateAll", e);
261N/A }
261N/A }
261N/A });
207N/A }
207N/A }
207N/A
668N/A /**
668N/A * Update the index database for a number of sub-directories
668N/A * @param executor An executor to run the job
668N/A * @param listener where to signal the changes to the database
668N/A * @param paths
668N/A */
1327N/A public static void update(ExecutorService executor, IndexChangedListener listener, List<String> paths) {
668N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
668N/A List<IndexDatabase> dbs = new ArrayList<IndexDatabase>();
668N/A
668N/A for (String path : paths) {
668N/A Project project = Project.getProject(path);
668N/A if (project == null && env.hasProjects()) {
1327N/A logger.warning("Could not find a project for '" + path + "'");
668N/A } else {
1327N/A IndexDatabase db = null;
668N/A
668N/A try {
668N/A if (project == null) {
668N/A db = new IndexDatabase();
668N/A } else {
668N/A db = new IndexDatabase(project);
668N/A }
668N/A
668N/A int idx = dbs.indexOf(db);
668N/A if (idx != -1) {
668N/A db = dbs.get(idx);
668N/A }
668N/A
668N/A if (db.addDirectory(path)) {
668N/A if (idx == -1) {
668N/A dbs.add(db);
668N/A }
668N/A } else {
1327N/A logger.warning("Directory does not exist '" + path + "'");
668N/A }
668N/A } catch (IOException e) {
1327N/A logger.warning("An error occured while updating "
1327N/A + db + ": " + e.getMessage());
1327N/A logger.log(Level.FINE, "update", e);
668N/A }
668N/A }
668N/A
668N/A for (final IndexDatabase db : dbs) {
668N/A db.addIndexChangedListener(listener);
668N/A executor.submit(new Runnable() {
668N/A
1054N/A @Override
668N/A public void run() {
668N/A try {
668N/A db.update();
1264N/A } catch (Throwable e) {
1327N/A logger.warning("An error occured while updating "
1327N/A + db + ": " + e.getLocalizedMessage());
1327N/A logger.log(Level.FINE, "run", e);
668N/A }
668N/A }
668N/A });
668N/A }
668N/A }
668N/A }
668N/A
460N/A @SuppressWarnings("PMD.CollapsibleIfStatements")
580N/A private void initialize() throws IOException {
580N/A synchronized (this) {
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
580N/A if (!indexDir.exists() && !indexDir.mkdirs()) {
580N/A // to avoid race conditions, just recheck..
580N/A if (!indexDir.exists()) {
1327N/A throw new FileNotFoundException("Failed to create root directory '"
1327N/A + indexDir.getAbsolutePath() + "'");
580N/A }
580N/A }
207N/A
580N/A if (!spellDir.exists() && !spellDir.mkdirs()) {
580N/A if (!spellDir.exists()) {
1327N/A throw new FileNotFoundException("Failed to create root directory '"
1327N/A + spellDir.getAbsolutePath() + "'");
580N/A }
580N/A }
1190N/A
580N/A if (!env.isUsingLuceneLocking()) {
928N/A lockfact = NoLockFactory.getNoLockFactory();
207N/A }
928N/A indexDirectory = FSDirectory.open(indexDir,lockfact);
928N/A spellDirectory = FSDirectory.open(spellDir,lockfact);
580N/A ignoredNames = env.getIgnoredNames();
1026N/A includedNames = env.getIncludedNames();
580N/A analyzerGuru = new AnalyzerGuru();
580N/A if (env.isGenerateHtml()) {
580N/A xrefDir = new File(env.getDataRootFile(), "xref");
580N/A }
580N/A listeners = new ArrayList<IndexChangedListener>();
580N/A dirtyFile = new File(indexDir, "dirty");
580N/A dirty = dirtyFile.exists();
580N/A directories = new ArrayList<String>();
359N/A }
207N/A }
207N/A
207N/A /**
274N/A * By default the indexer will traverse all directories in the project.
274N/A * If you add directories with this function update will just process
274N/A * the specified directories.
1190N/A *
274N/A * @param dir The directory to scan
297N/A * @return <code>true</code> if the file is added, false oth
274N/A */
464N/A @SuppressWarnings("PMD.UseStringBufferForStringAppends")
274N/A public boolean addDirectory(String dir) {
439N/A String directory = dir;
439N/A if (directory.startsWith("\\")) {
439N/A directory = directory.replace('\\', '/');
464N/A } else if (directory.charAt(0) != '/') {
439N/A directory = "/" + directory;
297N/A }
439N/A File file = new File(RuntimeEnvironment.getInstance().getSourceRootFile(), directory);
460N/A if (file.exists()) {
439N/A directories.add(directory);
274N/A return true;
274N/A }
1185N/A return false;
274N/A }
1190N/A
274N/A /**
207N/A * Update the content of this index database
459N/A * @throws IOException if an error occurs
678N/A * @throws HistoryException if an error occurs when accessing the history
207N/A */
678N/A public void update() throws IOException, HistoryException {
359N/A synchronized (lock) {
359N/A if (running) {
1327N/A throw new IOException("Indexer already running");
359N/A }
359N/A running = true;
359N/A interrupted = false;
359N/A }
656N/A
694N/A String ctgs = RuntimeEnvironment.getInstance().getCtags();
694N/A if (ctgs != null) {
656N/A ctags = new Ctags();
694N/A ctags.setBinary(ctgs);
656N/A }
656N/A if (ctags == null) {
1327N/A logger.warning("Unable to run ctags! Searching definitions will not work!");
656N/A }
656N/A
207N/A try {
816N/A //TODO we might need to add writer.commit after certain phases of index generation, right now it will only happen in the end
1318N/A Analyzer analyzer = AnalyzerGuru.getAnalyzer();
1318N/A IndexWriterConfig iwc = new IndexWriterConfig(SearchEngine.LUCENE_VERSION, analyzer);
1318N/A iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
1318N/A //iwc.setRAMBufferSizeMB(256.0); //TODO check what is the sweet spot
1318N/A writer = new IndexWriter(indexDirectory, iwc);
1318N/A writer.commit(); // to make sure index exists on the disk
1318N/A //writer.setMaxFieldLength(RuntimeEnvironment.getInstance().getIndexWordLimit());
207N/A
456N/A if (directories.isEmpty()) {
460N/A if (project == null) {
460N/A directories.add("");
460N/A } else {
274N/A directories.add(project.getPath());
274N/A }
207N/A }
651N/A
274N/A for (String dir : directories) {
274N/A File sourceRoot;
456N/A if ("".equals(dir)) {
274N/A sourceRoot = RuntimeEnvironment.getInstance().getSourceRootFile();
274N/A } else {
274N/A sourceRoot = new File(RuntimeEnvironment.getInstance().getSourceRootFile(), dir);
274N/A }
1190N/A
651N/A HistoryGuru.getInstance().ensureHistoryCacheExists(sourceRoot);
651N/A
1185N/A String startuid = Util.path2uid(dir, "");
972N/A IndexReader reader = IndexReader.open(indexDirectory,false); // open existing index
457N/A try {
457N/A uidIter = reader.terms(new Term("u", startuid)); // init uid iterator
207N/A
1112N/A //TODO below should be optional, since it traverses the tree once more to get total count! :(
1108N/A int file_cnt = 0;
1114N/A if (RuntimeEnvironment.getInstance().isPrintProgress()) {
1327N/A logger.log(Level.INFO, "Counting files in ''{0}'' ...", dir);
1327N/A file_cnt = indexDown(sourceRoot, dir, true, 0, 0);
1327N/A if (logger.isLoggable(Level.INFO)) {
1327N/A logger.log(Level.INFO, "Need to process {0} files for ''{1}''",
1327N/A new Object[] { file_cnt, dir });
1327N/A }
1112N/A }
1108N/A
1108N/A indexDown(sourceRoot, dir, false, 0, file_cnt);
207N/A
1327N/A while (uidIter.term() != null && uidIter.term().field().equals("u")
1327N/A && uidIter.term().text().startsWith(startuid))
1327N/A {
457N/A removeFile();
457N/A uidIter.next();
457N/A }
457N/A } finally {
457N/A reader.close();
274N/A }
207N/A }
207N/A } finally {
1327N/A IOUtils.close(writer);
656N/A if (ctags != null) {
1327N/A ctags.close();
656N/A }
656N/A
359N/A synchronized (lock) {
359N/A running = false;
359N/A }
207N/A }
207N/A
359N/A if (!isInterrupted() && isDirty()) {
253N/A if (RuntimeEnvironment.getInstance().isOptimizeDatabase()) {
253N/A optimize();
253N/A }
207N/A createSpellingSuggestions();
667N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
667N/A File timestamp = new File(env.getDataRootFile(), "timestamp");
667N/A if (timestamp.exists()) {
672N/A if (!timestamp.setLastModified(System.currentTimeMillis())) {
1327N/A logger.warning("Failed to set last modified time on '"
1327N/A + timestamp.getAbsolutePath()
1327N/A + "'used for timestamping the index database");
672N/A }
1327N/A } else if (!timestamp.createNewFile()) {
1327N/A logger.warning("Failed to create file '"
1327N/A + timestamp.getAbsolutePath()
1327N/A + "', used for timestamping the index database");
667N/A }
207N/A }
207N/A }
207N/A
207N/A /**
270N/A * Optimize all index databases
270N/A * @param executor An executor to run the job
459N/A * @throws IOException if an error occurs
270N/A */
312N/A static void optimizeAll(ExecutorService executor) throws IOException {
1190N/A List<IndexDatabase> dbs = new ArrayList<IndexDatabase>();
270N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
270N/A if (env.hasProjects()) {
270N/A for (Project project : env.getProjects()) {
564N/A dbs.add(new IndexDatabase(project));
270N/A }
270N/A } else {
564N/A dbs.add(new IndexDatabase());
564N/A }
564N/A
564N/A for (IndexDatabase d : dbs) {
564N/A final IndexDatabase db = d;
359N/A if (db.isDirty()) {
270N/A executor.submit(new Runnable() {
270N/A
1054N/A @Override
270N/A public void run() {
270N/A try {
270N/A db.update();
1264N/A } catch (Throwable e) {
1327N/A logger.warning("Problem updating " + db + ": "
1327N/A + e.getMessage());
1327N/A logger.log(Level.FINE, "optimizeAll", e);
270N/A }
270N/A }
270N/A });
270N/A }
270N/A }
270N/A }
1190N/A
270N/A /**
207N/A * Optimize the index database
207N/A */
207N/A public void optimize() {
359N/A synchronized (lock) {
359N/A if (running) {
1327N/A logger.warning("Optimize terminated... Someone else is updating / optimizing it!");
359N/A return ;
359N/A }
359N/A running = true;
359N/A }
207N/A IndexWriter wrt = null;
1190N/A try {
1327N/A logger.info("Optimizing " + this + " ...");
1318N/A Analyzer analyzer = new StandardAnalyzer(SearchEngine.LUCENE_VERSION);
1318N/A IndexWriterConfig conf = new IndexWriterConfig(SearchEngine.LUCENE_VERSION,analyzer);
1318N/A conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
1318N/A
1318N/A wrt = new IndexWriter(indexDirectory, conf);
1318N/A wrt.forceMerge(1); // this is deprecated and not needed anymore
1327N/A logger.info("Optimizing " + this + " done");
359N/A synchronized (lock) {
460N/A if (dirtyFile.exists() && !dirtyFile.delete()) {
1327N/A logger.log(Level.FINE, "Failed to remove \"dirty-file\" ''{0}''",
1327N/A dirtyFile.getAbsolutePath());
359N/A }
359N/A dirty = false;
359N/A }
207N/A } catch (IOException e) {
1327N/A logger.warning(this + " optimizing problem: " + e.getMessage());
1327N/A logger.log(Level.FINE, "optimize", e);
207N/A } finally {
1327N/A IOUtils.close(wrt);
359N/A synchronized (lock) {
359N/A running = false;
359N/A }
207N/A }
207N/A }
207N/A
207N/A /**
207N/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
1190N/A try {
1327N/A logger.info("Generating spelling suggestions for " + this + " ...");
928N/A indexReader = IndexReader.open(indexDirectory,false);
207N/A checker = new SpellChecker(spellDirectory);
830N/A //TODO below seems only to index "defs" , possible bug ?
1318N/A Analyzer analyzer = AnalyzerGuru.getAnalyzer();
1318N/A IndexWriterConfig iwc = new IndexWriterConfig(SearchEngine.LUCENE_VERSION, analyzer);
1318N/A iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
1318N/A checker.indexDictionary(new LuceneDictionary(indexReader, "defs"),iwc,false);
1327N/A logger.info("Generating spelling suggestions for " + this + " done");
207N/A } catch (IOException e) {
1327N/A logger.warning("Problem when generating spelling suggestions for " + this
1327N/A + ": " + e.getMessage());
1327N/A logger.log(Level.FINE, "createSpellingSuggestions", e);
207N/A } finally {
1327N/A IOUtils.close(indexReader);
1327N/A IOUtils.close(spellDirectory);
207N/A }
207N/A }
207N/A
359N/A private boolean isDirty() {
359N/A synchronized (lock) {
359N/A return dirty;
359N/A }
359N/A }
359N/A
359N/A private void setDirty() {
359N/A synchronized (lock) {
359N/A try {
460N/A if (!dirty && !dirtyFile.createNewFile()) {
1327N/A if (!dirtyFile.exists() && logger.isLoggable(Level.FINE)) {
1327N/A logger.fine("Failed to create \"dirty-file\" '" +
1327N/A dirtyFile.getAbsolutePath() + "'");
359N/A }
359N/A dirty = true;
359N/A }
359N/A } catch (IOException e) {
1327N/A logger.log(Level.FINE,"unable to creating dirty file", e);
253N/A }
253N/A }
253N/A }
207N/A /**
1190N/A * Remove a stale file (uidIter.term().text()) from the index database
207N/A * (and the xref file)
207N/A * @throws java.io.IOException if an error occurs
207N/A */
207N/A private void removeFile() throws IOException {
207N/A String path = Util.uid2url(uidIter.term().text());
207N/A
207N/A for (IndexChangedListener listener : listeners) {
1054N/A listener.fileRemove(path);
207N/A }
207N/A writer.deleteDocuments(uidIter.term());
207N/A
515N/A File xrefFile;
515N/A if (RuntimeEnvironment.getInstance().isCompressXref()) {
515N/A xrefFile = new File(xrefDir, path + ".gz");
515N/A } else {
515N/A xrefFile = new File(xrefDir, path);
515N/A }
359N/A File parent = xrefFile.getParentFile();
359N/A
602N/A if (!xrefFile.delete() && xrefFile.exists()) {
1327N/A logger.log(Level.INFO, "Failed to remove obsolete xref-file ''{0}''",
1327N/A xrefFile.getAbsolutePath());
359N/A }
359N/A
506N/A // Remove the parent directory if it's empty
506N/A if (parent.delete()) {
1327N/A logger.log(Level.FINE, "Removed empty xref dir ''{0}''",
1327N/A parent.getAbsolutePath());
506N/A }
253N/A setDirty();
1054N/A for (IndexChangedListener listener : listeners) {
1054N/A listener.fileRemoved(path);
1190N/A }
207N/A }
207N/A
207N/A /**
207N/A * Add a file to the Lucene index (and generate a xref file)
207N/A * @param file The file to add
207N/A * @param path The path to the file (from source root)
207N/A * @throws java.io.IOException if an error occurs
207N/A */
594N/A private void addFile(File file, String path) throws IOException {
594N/A final InputStream in =
594N/A new BufferedInputStream(new FileInputStream(file));
212N/A try {
1190N/A FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, path);
1054N/A for (IndexChangedListener listener : listeners) {
1054N/A listener.fileAdd(path, fa.getClass().getSimpleName());
1054N/A }
656N/A fa.setCtags(ctags);
922N/A fa.setProject(Project.getProject(path));
207N/A
889N/A Document d;
889N/A try {
889N/A d = analyzerGuru.getDocument(file, in, path, fa);
889N/A } catch (Exception e) {
1327N/A logger.log(Level.INFO,
1327N/A "Skipped file ''{0}'' because the analyzer didn''t " +
1327N/A "understand it.", path);
1327N/A logger.log(Level.FINE, "addFile", e);
889N/A return;
889N/A }
889N/A
889N/A writer.addDocument(d, fa);
889N/A Genre g = fa.getFactory().getGenre();
889N/A if (xrefDir != null && (g == Genre.PLAIN || g == Genre.XREFABLE)) {
889N/A File xrefFile = new File(xrefDir, path);
889N/A // If mkdirs() returns false, the failure is most likely
889N/A // because the file already exists. But to check for the
889N/A // file first and only add it if it doesn't exists would
889N/A // only increase the file IO...
889N/A if (!xrefFile.getParentFile().mkdirs()) {
889N/A assert xrefFile.getParentFile().exists();
506N/A }
889N/A fa.writeXref(xrefDir, path);
889N/A }
889N/A setDirty();
889N/A for (IndexChangedListener listener : listeners) {
889N/A listener.fileAdded(path, fa.getClass().getSimpleName());
207N/A }
553N/A } finally {
594N/A in.close();
508N/A }
207N/A }
207N/A
207N/A /**
207N/A * Check if I should accept this file into the index database
207N/A * @param file the file to check
207N/A * @return true if the file should be included, false otherwise
207N/A */
207N/A private boolean accept(File file) {
1028N/A
1028N/A if (!includedNames.isEmpty() &&
1028N/A // the filter should not affect directory names
1028N/A (!(file.isDirectory() || includedNames.match(file))) ) {
1026N/A return false;
1026N/A }
207N/A if (ignoredNames.ignore(file)) {
207N/A return false;
207N/A }
207N/A
1016N/A String absolutePath = file.getAbsolutePath();
1016N/A
207N/A if (!file.canRead()) {
1327N/A logger.warning("Could not read " + absolutePath);
207N/A return false;
207N/A }
207N/A
207N/A try {
1016N/A String canonicalPath = file.getCanonicalPath();
1016N/A if (!absolutePath.equals(canonicalPath) && !acceptSymlink(absolutePath, canonicalPath)) {
1327N/A logger.log(Level.FINE, "Skipped symlink ''{0}'' -> ''{1}''",
1327N/A new Object[]{absolutePath, canonicalPath});
1016N/A return false;
207N/A }
876N/A //below will only let go files and directories, anything else is considered special and is not added
872N/A if (!file.isFile() && !file.isDirectory()) {
1327N/A logger.warning("Ignored special file '" + absolutePath + "'");
1327N/A return false;
872N/A }
207N/A } catch (IOException exp) {
1327N/A logger.warning("Failed to resolve name '" + absolutePath + "'");
1327N/A logger.log(Level.FINE, "accept", exp);
207N/A }
207N/A
504N/A if (file.isDirectory()) {
504N/A // always accept directories so that their files can be examined
504N/A return true;
480N/A }
480N/A
504N/A if (HistoryGuru.getInstance().hasHistory(file)) {
504N/A // versioned files should always be accepted
504N/A return true;
504N/A }
504N/A
504N/A // this is an unversioned file, check if it should be indexed
504N/A return !RuntimeEnvironment.getInstance().isIndexVersionedFilesOnly();
207N/A }
207N/A
1192N/A boolean accept(File parent, File file) {
1192N/A try {
1192N/A File f1 = parent.getCanonicalFile();
1192N/A File f2 = file.getCanonicalFile();
1192N/A if (f1.equals(f2)) {
1327N/A logger.log(Level.INFO, "Skipping links to itself (''{0}'' ''{1}'')",
1327N/A new Object[]{parent.getAbsolutePath(), file.getAbsolutePath()});
1192N/A return false;
1192N/A }
1192N/A
1192N/A // Now, let's verify that it's not a link back up the chain...
1192N/A File t1 = f1;
1192N/A while ((t1 = t1.getParentFile()) != null) {
1192N/A if (f2.equals(t1)) {
1327N/A logger.log(Level.INFO, "Skipping links to parent (''{0}'' ''{1}'')",
1327N/A new Object[]{parent.getAbsolutePath(), file.getAbsolutePath()});
1192N/A return false;
1192N/A }
1192N/A }
1192N/A
1192N/A return accept(file);
1192N/A } catch (IOException ex) {
1327N/A logger.log(Level.WARNING, "Failed to resolve name (''{0}'' ''{1}'')",
1327N/A new Object[]{parent.getAbsolutePath(), file.getAbsolutePath()});
1192N/A }
1192N/A return false;
1192N/A }
1192N/A
207N/A /**
1016N/A * Check if I should accept the path containing a symlink
1016N/A * @param absolutePath the path with a symlink to check
1016N/A * @param canonicalPath the canonical path to the file
1016N/A * @return true if the file should be accepted, false otherwise
1016N/A */
1016N/A private boolean acceptSymlink(String absolutePath, String canonicalPath) throws IOException {
1051N/A // Always accept local symlinks
1051N/A if (isLocal(canonicalPath)) {
1051N/A return true;
1051N/A }
1051N/A
1016N/A for (String allowedSymlink : RuntimeEnvironment.getInstance().getAllowedSymlinks()) {
1016N/A if (absolutePath.startsWith(allowedSymlink)) {
1016N/A String allowedTarget = new File(allowedSymlink).getCanonicalPath();
1016N/A if (canonicalPath.startsWith(allowedTarget) &&
1016N/A absolutePath.substring(allowedSymlink.length()).equals(canonicalPath.substring(allowedTarget.length()))) {
1016N/A return true;
1016N/A }
1016N/A }
1016N/A }
1016N/A return false;
1016N/A }
1016N/A
1016N/A /**
1051N/A * Check if a file is local to the current project. If we don't have
1051N/A * projects, check if the file is in the source root.
1051N/A *
1051N/A * @param path the path to a file
1051N/A * @return true if the file is local to the current repository
1051N/A */
1051N/A private boolean isLocal(String path) {
1051N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1051N/A String srcRoot = env.getSourceRootPath();
1051N/A
1051N/A boolean local = false;
1051N/A
1051N/A if (path.startsWith(srcRoot)) {
1051N/A if (env.hasProjects()) {
1051N/A String relPath = path.substring(srcRoot.length());
1051N/A if (project.equals(Project.getProject(relPath))) {
1051N/A // File is under the current project, so it's local.
1051N/A local = true;
1051N/A }
1051N/A } else {
1051N/A // File is under source root, and we don't have projects, so
1051N/A // consider it local.
1051N/A local = true;
1051N/A }
1051N/A }
1051N/A
1051N/A return local;
1051N/A }
1051N/A
1051N/A /**
207N/A * Generate indexes recursively
207N/A * @param dir the root indexDirectory to generate indexes for
207N/A * @param path the path
1112N/A * @param count_only if true will just traverse the source root and count files
1112N/A * @param cur_count current count during the traversal of the tree
1112N/A * @param est_total estimate total files to process
1112N/A *
207N/A */
1108N/A private int indexDown(File dir, String parent, boolean count_only, int cur_count, int est_total) throws IOException {
1112N/A int lcur_count=cur_count;
359N/A if (isInterrupted()) {
1112N/A return lcur_count;
207N/A }
207N/A
207N/A if (!accept(dir)) {
1112N/A return lcur_count;
207N/A }
207N/A
207N/A File[] files = dir.listFiles();
207N/A if (files == null) {
1327N/A logger.severe("Failed to get file listing for '" + dir.getAbsolutePath() + "'");
1112N/A return lcur_count;
207N/A }
282N/A Arrays.sort(files, new Comparator<File>() {
1054N/A @Override
282N/A public int compare(File p1, File p2) {
282N/A return p1.getName().compareTo(p2.getName());
282N/A }
282N/A });
207N/A
207N/A for (File file : files) {
1192N/A if (accept(dir, file)) {
207N/A String path = parent + '/' + file.getName();
1108N/A
207N/A if (file.isDirectory()) {
1112N/A lcur_count = indexDown(file, path, count_only, lcur_count, est_total);
207N/A } else {
1112N/A lcur_count++;
1112N/A if (count_only) {
1108N/A continue;
1112N/A }
1108N/A
1327N/A if (RuntimeEnvironment.getInstance().isPrintProgress()
1327N/A && est_total > 0 && logger.isLoggable(Level.INFO) )
1190N/A {
1327N/A logger.log(Level.INFO, "Progress: {0} ({1}%)",
1327N/A new Object[] { lcur_count,
1327N/A (lcur_count * 100.0f / est_total) });
1108N/A }
1108N/A
594N/A if (uidIter != null) {
1327N/A String uid = Util.path2uid(path, DateTools
1327N/A .timeToString(file.lastModified(),
1327N/A 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();
207N/A }
207N/A
207N/A if (uidIter.term() != null && uidIter.term().field().equals("u") &&
207N/A uidIter.term().text().compareTo(uid) == 0) {
972N/A uidIter.next(); // keep matching docs
594N/A continue;
207N/A }
207N/A }
594N/A try {
594N/A addFile(file, path);
594N/A } catch (Exception e) {
1327N/A logger.warning("Failed to add file '"
1327N/A + file.getAbsolutePath() + "': " + e.getMessage());
594N/A }
207N/A }
207N/A }
207N/A }
1108N/A
1112N/A return lcur_count;
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 */
207N/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;
1190N/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.
1190N/A *
207N/A * @param listener the object to receive the events
207N/A */
316N/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
1190N/A *
207N/A * @param listener the object to remove
207N/A */
316N/A public void removeIndexChangedListener(IndexChangedListener listener) {
207N/A listeners.remove(listener);
207N/A }
207N/A
207N/A /**
207N/A * List all files in all of the index databases
359N/A * @throws IOException if an error occurs
207N/A */
312N/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
207N/A * for (or null or an empty list to dump all projects)
359N/A * @throws IOException if an error occurs
207N/A */
312N/A public static void listAllFiles(List<String> subFiles) throws IOException {
207N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
460N/A if (env.hasProjects()) {
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 }
207N/A } else {
207N/A for (String path : subFiles) {
207N/A Project project = Project.getProject(path);
207N/A if (project == null) {
1327N/A logger.warning("Could not find a project for '"
1327N/A + path + "'");
207N/A } else {
207N/A IndexDatabase db = new IndexDatabase(project);
207N/A db.listFiles();
207N/A }
207N/A }
207N/A }
460N/A } else {
460N/A IndexDatabase db = new IndexDatabase();
460N/A db.listFiles();
207N/A }
207N/A }
207N/A
207N/A /**
207N/A * List all of the files in this index database
1190N/A *
359N/A * @throws IOException If an IO error occurs while reading from the database
207N/A */
312N/A public void listFiles() throws IOException {
207N/A IndexReader ireader = null;
207N/A TermEnum iter = null;
207N/A
207N/A try {
972N/A ireader = IndexReader.open(indexDirectory,false); // open existing index
207N/A iter = ireader.terms(new Term("u", "")); // init uid iterator
1327N/A if (logger.isLoggable(Level.FINE)) {
1327N/A while (iter.term() != null) {
1327N/A logger.fine(Util.uid2url(iter.term().text()));
1327N/A iter.next();
1327N/A }
207N/A }
207N/A } finally {
1327N/A IOUtils.close(iter);
1327N/A IOUtils.close(ireader);
207N/A }
207N/A }
207N/A
459N/A static void listFrequentTokens() throws IOException {
207N/A listFrequentTokens(null);
207N/A }
207N/A
457N/A static void listFrequentTokens(List<String> subFiles) throws IOException {
207N/A final int limit = 4;
207N/A
207N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
460N/A if (env.hasProjects()) {
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) {
207N/A Project project = Project.getProject(path);
207N/A if (project == null) {
1327N/A logger.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 }
207N/A }
207N/A }
460N/A } else {
460N/A IndexDatabase db = new IndexDatabase();
460N/A db.listTokens(limit);
207N/A }
207N/A }
207N/A
312N/A public void listTokens(int freq) throws IOException {
207N/A IndexReader ireader = null;
207N/A TermEnum iter = null;
207N/A
207N/A try {
928N/A ireader = IndexReader.open(indexDirectory,false);
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) {
1327N/A logger.warning(iter.term().text());
207N/A }
207N/A iter.next();
207N/A } else {
207N/A break;
207N/A }
1190N/A }
207N/A } finally {
1327N/A IOUtils.close(iter);
1327N/A IOUtils.close(ireader);
207N/A }
207N/A }
1190N/A
208N/A /**
208N/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);
460N/A if (p == null) {
460N/A return null;
208N/A }
1185N/A indexDir = new File(indexDir, p.getPath());
208N/A }
1185N/A try {
1185N/A FSDirectory fdir=FSDirectory.open(indexDir,NoLockFactory.getNoLockFactory());
1185N/A if (indexDir.exists() && IndexReader.indexExists(fdir)) {
1185N/A ret = IndexReader.open(fdir,false);
208N/A }
1185N/A } catch (Exception ex) {
1327N/A logger.warning("Failed to open index '" + indexDir.getAbsolutePath() + "'");
1327N/A logger.log(Level.FINE, "getIndexReader", ex);
1185N/A }
208N/A return ret;
208N/A }
274N/A
1127N/A /**
1127N/A * Get the latest definitions for a file from the index.
1127N/A *
1127N/A * @param file the file whose definitions to find
1127N/A * @return definitions for the file, or {@code null} if they could not
1127N/A * be found
1127N/A * @throws IOException if an error happens when accessing the index
1127N/A * @throws ParseException if an error happens when building the Lucene query
1127N/A * @throws ClassNotFoundException if the class for the stored definitions
1127N/A * instance cannot be found
1127N/A */
1127N/A public static Definitions getDefinitions(File file)
1127N/A throws IOException, ParseException, ClassNotFoundException {
1127N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1127N/A String path = env.getPathRelativeToSourceRoot(file, 0);
1127N/A
1127N/A IndexReader ireader = getIndexReader(path);
1127N/A
1127N/A if (ireader == null) {
1127N/A // No index, no definitions...
1127N/A return null;
1127N/A }
1127N/A
1127N/A try {
1127N/A Query q = new QueryBuilder().setPath(path).build();
1127N/A IndexSearcher searcher = new IndexSearcher(ireader);
1127N/A try {
1127N/A TopDocs top = searcher.search(q, 1);
1127N/A if (top.totalHits == 0) {
1127N/A // No hits, no definitions...
1127N/A return null;
1127N/A }
1127N/A Document doc = searcher.doc(top.scoreDocs[0].doc);
1127N/A String foundPath = doc.get("path");
1127N/A
1127N/A // Only use the definitions if we found an exact match.
1127N/A if (path.equals(foundPath)) {
1127N/A Fieldable tags = doc.getFieldable("tags");
1127N/A if (tags != null) {
1127N/A return Definitions.deserialize(tags.getBinaryValue());
1127N/A }
1127N/A }
1127N/A } finally {
1127N/A searcher.close();
1127N/A }
1127N/A } finally {
1127N/A ireader.close();
1127N/A }
1127N/A
1127N/A // Didn't find any definitions.
1127N/A return null;
1127N/A }
1127N/A
274N/A @Override
274N/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;
460N/A hash = 41 * hash + (this.project == null ? 0 : this.project.hashCode());
274N/A return hash;
274N/A }
1190N/A
1327N/A /**
1327N/A * {@inheritDoc}
1327N/A */
1327N/A @Override
1327N/A public String toString() {
1327N/A return (project == null ? "" : project.getDescription()) + " Lucene IndexDB";
1327N/A }
207N/A}