Search in sources :

Example 1 with RuntimeEnvironment

use of org.opensolaris.opengrok.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class FileHistoryCache method storeFile.

/**
     * Store history object (encoded as XML and compressed with gzip) in a file.
     *
     * @param history history object to store
     * @param file file to store the history object into
     * @param repo repository for the file
     * @throws HistoryException
     */
private void storeFile(History histNew, File file, Repository repo) throws HistoryException {
    File cacheFile = getCachedFile(file);
    History history = histNew;
    File dir = cacheFile.getParentFile();
    if (!dir.isDirectory() && !dir.mkdirs()) {
        throw new HistoryException("Unable to create cache directory '" + dir + "'.");
    }
    // Incremental update of the history for this file.
    History histOld;
    try {
        histOld = readCache(cacheFile);
        // Merge old history with the new history.
        List<HistoryEntry> listOld = histOld.getHistoryEntries();
        if (!listOld.isEmpty()) {
            RuntimeEnvironment env = RuntimeEnvironment.getInstance();
            List<HistoryEntry> listNew = histNew.getHistoryEntries();
            ListIterator li = listNew.listIterator(listNew.size());
            while (li.hasPrevious()) {
                listOld.add(0, (HistoryEntry) li.previous());
            }
            history = new History(listOld);
            // to this somewhat crude solution.
            if (env.isTagsEnabled() && repo.hasFileBasedTags()) {
                for (HistoryEntry ent : history.getHistoryEntries()) {
                    ent.setTags(null);
                }
                repo.assignTagsInHistory(history);
            }
        }
    } catch (IOException ex) {
    // Ideally we would want to catch the case when incremental update
    // is done but the cached file is not there however we do not have
    // the data to do it here.
    }
    writeHistoryToFile(dir, history, cacheFile);
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) IOException(java.io.IOException) ListIterator(java.util.ListIterator) File(java.io.File)

Example 2 with RuntimeEnvironment

use of org.opensolaris.opengrok.configuration.RuntimeEnvironment 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 3 with RuntimeEnvironment

use of org.opensolaris.opengrok.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class IndexerTest method testRFE2575.

@Test
public void testRFE2575() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    env.setCtags(System.getProperty(ctagsProperty, "ctags"));
    env.setSourceRoot(repository.getSourceRoot());
    env.setDataRoot(repository.getDataRoot());
    HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
    List<RepositoryInfo> repos = env.getRepositories();
    Repository r = null;
    for (RepositoryInfo ri : repos) {
        if (ri.getDirectoryName().equals(repository.getSourceRoot() + "/rfe2575")) {
            r = RepositoryFactory.getRepository(ri);
            break;
        }
    }
    if (r != null && r.isWorking() && env.validateExuberantCtags()) {
        Project project = new Project();
        project.setPath("/rfe2575");
        IndexDatabase idb = new IndexDatabase(project);
        assertNotNull(idb);
        MyIndexChangeListener listener = new MyIndexChangeListener();
        idb.addIndexChangedListener(listener);
        idb.update();
        assertEquals(2, listener.files.size());
        repository.purgeData();
        RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(true);
        idb = new IndexDatabase(project);
        listener = new MyIndexChangeListener();
        idb.addIndexChangedListener(listener);
        idb.update();
        assertEquals(1, listener.files.size());
        RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(false);
    } else {
        System.out.println("Skipping test. Repository for rfe2575 not found or could not find a ctags or an sccs I could use in path.");
    }
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) Repository(org.opensolaris.opengrok.history.Repository) TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) RepositoryInfo(org.opensolaris.opengrok.history.RepositoryInfo) Test(org.junit.Test)

Example 4 with RuntimeEnvironment

use of org.opensolaris.opengrok.configuration.RuntimeEnvironment 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 5 with RuntimeEnvironment

use of org.opensolaris.opengrok.configuration.RuntimeEnvironment 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)

Aggregations

RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)81 File (java.io.File)31 Project (org.opensolaris.opengrok.configuration.Project)27 IOException (java.io.IOException)23 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)17 TestRepository (org.opensolaris.opengrok.util.TestRepository)13 ForbiddenSymlinkException (org.opensolaris.opengrok.util.ForbiddenSymlinkException)12 BeforeClass (org.junit.BeforeClass)11 Ctags (org.opensolaris.opengrok.analysis.Ctags)6 BufferedReader (java.io.BufferedReader)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 ParseException (java.text.ParseException)5 DateFormat (java.text.DateFormat)4 Date (java.util.Date)4 RepositoryInfo (org.opensolaris.opengrok.history.RepositoryInfo)4 OutputStreamWriter (java.io.OutputStreamWriter)3 ConnectException (java.net.ConnectException)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3