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