Search in sources :

Example 1 with Ctags

use of org.opensolaris.opengrok.analysis.Ctags in project OpenGrok by OpenGrok.

the class IndexDatabase method update.

/**
     * Update the content of this index database
     *
     * @throws IOException if an error occurs
     * @throws HistoryException if an error occurs when accessing the history
     */
public void update() throws IOException, HistoryException {
    synchronized (lock) {
        if (running) {
            throw new IOException("Indexer already running!");
        }
        running = true;
        interrupted = false;
    }
    String ctgs = RuntimeEnvironment.getInstance().getCtags();
    if (ctgs != null) {
        ctags = new Ctags();
        ctags.setBinary(ctgs);
    }
    if (ctags == null) {
        LOGGER.severe("Unable to run ctags! searching definitions will not work!");
    }
    if (ctags != null) {
        String filename = RuntimeEnvironment.getInstance().getCTagsExtraOptionsFile();
        if (filename != null) {
            ctags.setCTagsExtraOptionsFile(filename);
        }
    }
    try {
        Analyzer analyzer = AnalyzerGuru.getAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwc.setRAMBufferSizeMB(RuntimeEnvironment.getInstance().getRamBufferSize());
        writer = new IndexWriter(indexDirectory, iwc);
        // to make sure index exists on the disk            
        writer.commit();
        if (directories.isEmpty()) {
            if (project == null) {
                directories.add("");
            } else {
                directories.add(project.getPath());
            }
        }
        for (String dir : directories) {
            File sourceRoot;
            if ("".equals(dir)) {
                sourceRoot = RuntimeEnvironment.getInstance().getSourceRootFile();
            } else {
                sourceRoot = new File(RuntimeEnvironment.getInstance().getSourceRootFile(), dir);
            }
            HistoryGuru.getInstance().ensureHistoryCacheExists(sourceRoot);
            String startuid = Util.path2uid(dir, "");
            // open existing index
            IndexReader reader = DirectoryReader.open(indexDirectory);
            Terms terms = null;
            int numDocs = reader.numDocs();
            if (numDocs > 0) {
                //reader.getTermVectors(0);
                Fields uFields = MultiFields.getFields(reader);
                terms = uFields.terms(QueryBuilder.U);
            }
            try {
                if (numDocs > 0) {
                    uidIter = terms.iterator();
                    //init uid                        
                    TermsEnum.SeekStatus stat = uidIter.seekCeil(new BytesRef(startuid));
                    if (stat == TermsEnum.SeekStatus.END) {
                        uidIter = null;
                        LOGGER.log(Level.WARNING, "Couldn't find a start term for {0}, empty u field?", startuid);
                    }
                }
                // The code below traverses the tree to get total count.
                int file_cnt = 0;
                if (RuntimeEnvironment.getInstance().isPrintProgress()) {
                    LOGGER.log(Level.INFO, "Counting files in {0} ...", dir);
                    file_cnt = indexDown(sourceRoot, dir, true, 0, 0);
                    LOGGER.log(Level.INFO, "Need to process: {0} files for {1}", new Object[] { file_cnt, dir });
                }
                indexDown(sourceRoot, dir, false, 0, file_cnt);
                while (uidIter != null && uidIter.term() != null && uidIter.term().utf8ToString().startsWith(startuid)) {
                    removeFile();
                    BytesRef next = uidIter.next();
                    if (next == null) {
                        uidIter = null;
                    }
                }
            } finally {
                reader.close();
            }
        }
    } finally {
        if (writer != null) {
            try {
                writer.prepareCommit();
                writer.commit();
                writer.close();
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "An error occured while closing writer", e);
            }
        }
        if (ctags != null) {
            try {
                ctags.close();
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "An error occured while closing ctags process", e);
            }
        }
        synchronized (lock) {
            running = false;
        }
    }
    if (!isInterrupted() && isDirty()) {
        if (RuntimeEnvironment.getInstance().isOptimizeDatabase()) {
            optimize();
        }
        RuntimeEnvironment env = RuntimeEnvironment.getInstance();
        File timestamp = new File(env.getDataRootFile(), "timestamp");
        String purpose = "used for timestamping the index database.";
        if (timestamp.exists()) {
            if (!timestamp.setLastModified(System.currentTimeMillis())) {
                LOGGER.log(Level.WARNING, "Failed to set last modified time on ''{0}'', {1}", new Object[] { timestamp.getAbsolutePath(), purpose });
            }
        } else {
            if (!timestamp.createNewFile()) {
                LOGGER.log(Level.WARNING, "Failed to create file ''{0}'', {1}", new Object[] { timestamp.getAbsolutePath(), purpose });
            }
        }
    }
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Terms(org.apache.lucene.index.Terms) IOException(java.io.IOException) FileAnalyzer(org.opensolaris.opengrok.analysis.FileAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) TermsEnum(org.apache.lucene.index.TermsEnum) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) Ctags(org.opensolaris.opengrok.analysis.Ctags) File(java.io.File) BytesRef(org.apache.lucene.util.BytesRef) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with Ctags

use of org.opensolaris.opengrok.analysis.Ctags in project OpenGrok by OpenGrok.

the class CAnalyzerFactoryTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    ctags = new Ctags();
    ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
    repository = new TestRepository();
    repository.create(CAnalyzerFactoryTest.class.getResourceAsStream("/org/opensolaris/opengrok/index/source.zip"));
    CAnalyzerFactory analFact = new CAnalyzerFactory();
    analyzer = analFact.getAnalyzer();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.validateExuberantCtags()) {
        analyzer.setCtags(new Ctags());
    }
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Ctags(org.opensolaris.opengrok.analysis.Ctags) BeforeClass(org.junit.BeforeClass)

Example 3 with Ctags

use of org.opensolaris.opengrok.analysis.Ctags in project OpenGrok by OpenGrok.

the class CxxAnalyzerFactoryTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    ctags = new Ctags();
    ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
    repository = new TestRepository();
    repository.create(CxxAnalyzerFactoryTest.class.getResourceAsStream("/org/opensolaris/opengrok/index/source.zip"));
    CxxAnalyzerFactory analFact = new CxxAnalyzerFactory();
    analyzer = analFact.getAnalyzer();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.validateExuberantCtags()) {
        analyzer.setCtags(new Ctags());
    }
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Ctags(org.opensolaris.opengrok.analysis.Ctags) BeforeClass(org.junit.BeforeClass)

Example 4 with Ctags

use of org.opensolaris.opengrok.analysis.Ctags in project OpenGrok by OpenGrok.

the class ClojureAnalyzerFactoryTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    ctags = new Ctags();
    ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
    repository = new TestRepository();
    repository.create(ClojureAnalyzerFactoryTest.class.getResourceAsStream("/org/opensolaris/opengrok/index/source.zip"));
    ClojureAnalyzerFactory analFact = new ClojureAnalyzerFactory();
    analyzer = analFact.getAnalyzer();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.validateExuberantCtags()) {
        analyzer.setCtags(new Ctags());
    }
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Ctags(org.opensolaris.opengrok.analysis.Ctags) BeforeClass(org.junit.BeforeClass)

Example 5 with Ctags

use of org.opensolaris.opengrok.analysis.Ctags in project OpenGrok by OpenGrok.

the class CSharpAnalyzerFactoryTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    ctags = new Ctags();
    ctags.setBinary(RuntimeEnvironment.getInstance().getCtags());
    repository = new TestRepository();
    repository.create(CSharpAnalyzerFactoryTest.class.getResourceAsStream("/org/opensolaris/opengrok/index/source.zip"));
    CSharpAnalyzerFactory analFact = new CSharpAnalyzerFactory();
    analyzer = analFact.getAnalyzer();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.validateExuberantCtags()) {
        analyzer.setCtags(new Ctags());
    }
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Ctags(org.opensolaris.opengrok.analysis.Ctags) BeforeClass(org.junit.BeforeClass)

Aggregations

Ctags (org.opensolaris.opengrok.analysis.Ctags)9 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)7 BeforeClass (org.junit.BeforeClass)6 TestRepository (org.opensolaris.opengrok.util.TestRepository)6 IOException (java.io.IOException)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Fields (org.apache.lucene.index.Fields)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 MultiFields (org.apache.lucene.index.MultiFields)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1