Search in sources :

Example 66 with RuntimeEnvironment

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

the class GitHistoryParser method process.

private void process(BufferedReader in) throws IOException {
    DateFormat df = repository.getDateFormat();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    entries = new ArrayList<>();
    HistoryEntry entry = null;
    ParseState state = ParseState.HEADER;
    String s = in.readLine();
    while (s != null) {
        if (state == ParseState.HEADER) {
            if (s.startsWith("commit")) {
                if (entry != null) {
                    entries.add(entry);
                }
                entry = new HistoryEntry();
                entry.setActive(true);
                String commit = s.substring("commit".length()).trim();
                entry.setRevision(commit);
            } else if (s.startsWith("Author:") && entry != null) {
                entry.setAuthor(s.substring("Author:".length()).trim());
            } else if (s.startsWith("AuthorDate:") && entry != null) {
                String dateString = s.substring("AuthorDate:".length()).trim();
                try {
                    entry.setDate(df.parse(dateString));
                } catch (ParseException pe) {
                    // 
                    throw new IOException("Failed to parse author date: " + s, pe);
                }
            } else if (StringUtils.isOnlyWhitespace(s)) {
                // We are done reading the heading, start to read the message
                state = ParseState.MESSAGE;
                // The current line is empty - the message starts on the next line (to be parsed below).
                s = in.readLine();
            }
        }
        if (state == ParseState.MESSAGE) {
            if ((s.length() == 0) || Character.isWhitespace(s.charAt(0))) {
                if (entry != null) {
                    entry.appendMessage(s);
                }
            } else {
                // This is the list of files after the message - add them
                state = ParseState.FILES;
            }
        }
        if (state == ParseState.FILES) {
            if (StringUtils.isOnlyWhitespace(s) || s.startsWith("commit")) {
                state = ParseState.HEADER;
                // Parse this line again - do not read a new line
                continue;
            }
            if (entry != null) {
                try {
                    File f = new File(myDir, s);
                    String path = env.getPathRelativeToSourceRoot(f);
                    entry.addFile(path.intern());
                } catch (ForbiddenSymlinkException e) {
                    LOGGER.log(Level.FINER, e.getMessage());
                // ignore
                } catch (FileNotFoundException e) {
                // NOPMD
                // If the file is not located under the source root,
                // ignore it (bug #11664).
                }
            }
        }
        s = in.readLine();
    }
    if (entry != null) {
        entries.add(entry);
    }
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) DateFormat(java.text.DateFormat) FileNotFoundException(java.io.FileNotFoundException) ParseException(java.text.ParseException) IOException(java.io.IOException) File(java.io.File)

Example 67 with RuntimeEnvironment

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

the class Indexer method doIndexerExecution.

/**
 * This is the second phase of the indexer which generates Lucene index
 * by passing source code files through ctags, generating xrefs
 * and storing data from the source files in the index (along with history,
 * if any).
 *
 * @param update if set to true, index database is updated, otherwise optimized
 * @param subFiles index just some subdirectories
 * @param progress object to receive notifications as indexer progress is made
 * @throws IOException if I/O exception occurred
 */
public void doIndexerExecution(final boolean update, List<String> subFiles, IndexChangedListener progress) throws IOException {
    Statistics elapsed = new Statistics();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance().register();
    LOGGER.info("Starting indexing");
    IndexerParallelizer parallelizer = new IndexerParallelizer(env);
    if (subFiles == null || subFiles.isEmpty()) {
        if (update) {
            IndexDatabase.updateAll(parallelizer, progress);
        } else if (env.isOptimizeDatabase()) {
            IndexDatabase.optimizeAll(parallelizer);
        }
    } else {
        List<IndexDatabase> dbs = new ArrayList<>();
        for (String path : subFiles) {
            Project project = Project.getProject(path);
            if (project == null && env.hasProjects()) {
                LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
            } else {
                IndexDatabase db;
                if (project == null) {
                    db = new IndexDatabase();
                } else {
                    db = new IndexDatabase(project);
                }
                int idx = dbs.indexOf(db);
                if (idx != -1) {
                    db = dbs.get(idx);
                }
                if (db.addDirectory(path)) {
                    if (idx == -1) {
                        dbs.add(db);
                    }
                } else {
                    LOGGER.log(Level.WARNING, "Directory does not exist \"{0}\"", path);
                }
            }
        }
        for (final IndexDatabase db : dbs) {
            final boolean optimize = env.isOptimizeDatabase();
            db.addIndexChangedListener(progress);
            parallelizer.getFixedExecutor().submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (update) {
                            db.update(parallelizer);
                        } else if (optimize) {
                            db.optimize();
                        }
                    } catch (Throwable e) {
                        LOGGER.log(Level.SEVERE, "An error occurred while " + (update ? "updating" : "optimizing") + " index", e);
                    }
                }
            });
        }
    }
    parallelizer.getFixedExecutor().shutdown();
    while (!parallelizer.getFixedExecutor().isTerminated()) {
        try {
            // Wait forever
            parallelizer.getFixedExecutor().awaitTermination(999, TimeUnit.DAYS);
        } catch (InterruptedException exp) {
            LOGGER.log(Level.WARNING, "Received interrupt while waiting for executor to finish", exp);
        }
    }
    try {
        // It can happen that history index is not done in prepareIndexer()
        // but via db.update() above in which case we must make sure the
        // thread pool for renamed file handling is destroyed.
        RuntimeEnvironment.destroyRenamedHistoryExecutor();
    } catch (InterruptedException ex) {
        LOGGER.log(Level.SEVERE, "destroying of renamed thread pool failed", ex);
    }
    try {
        parallelizer.close();
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "parallelizer.close() failed", ex);
    }
    elapsed.report(LOGGER, "Done indexing data of all repositories");
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) ArrayList(java.util.ArrayList) Statistics(org.opensolaris.opengrok.util.Statistics) HistoryException(org.opensolaris.opengrok.history.HistoryException) ConnectException(java.net.ConnectException) ParseException(java.text.ParseException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Project(org.opensolaris.opengrok.configuration.Project)

Example 68 with RuntimeEnvironment

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

the class HistoryGuruTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    repository = new TestRepository();
    repository.create(HistoryGuru.class.getResourceAsStream("repositories.zip"));
    RepositoryFactory.initializeIgnoredNames(env);
    FileUtilities.getAllFiles(new File(repository.getSourceRoot()), FILES, true);
    Assert.assertNotEquals(0, FILES.size());
    env.setVerbose(true);
    HistoryGuru histGuru = HistoryGuru.getInstance();
    assertNotNull(histGuru);
    Assert.assertEquals(0, histGuru.getRepositories().size());
    // Add initial set of repositories to HistoryGuru and RuntimeEnvironment.
    // This is a test in itself. While this makes the structure of the tests
    // a bit incomprehensible, it does not make sense to run the rest of tests
    // if the basic functionality does not work.
    env.setRepositories(repository.getSourceRoot());
    Assert.assertTrue(histGuru.getRepositories().size() > 0);
    Assert.assertEquals(histGuru.getRepositories().size(), env.getRepositories().size());
    // Create cache with initial set of repositories.
    histGuru.createCache();
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 69 with RuntimeEnvironment

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

the class IndexDatabaseTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    assertTrue("No ctags available", env.validateExuberantCtags());
    repository = new TestRepository();
    repository.create(HistoryGuru.class.getResourceAsStream("repositories.zip"));
    env.setSourceRoot(repository.getSourceRoot());
    env.setDataRoot(repository.getDataRoot());
    env.setHistoryEnabled(true);
    env.setProjectsEnabled(true);
    RepositoryFactory.initializeIgnoredNames(env);
    parallelizer = new IndexerParallelizer(env);
    // Note that all tests in this class share the index created below.
    // Ergo, if they need to modify it, this has to be done in such a way
    // so that it does not affect other tests, no matter in which order
    // the tests are run.
    Indexer indexer = Indexer.getInstance();
    indexer.prepareIndexer(env, true, true, new TreeSet<>(Arrays.asList(new String[] { "/c" })), false, false, null, null, new ArrayList<String>(), false);
    indexer.doIndexerExecution(true, null, null);
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) HistoryGuru(org.opensolaris.opengrok.history.HistoryGuru) BeforeClass(org.junit.BeforeClass)

Example 70 with RuntimeEnvironment

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

the class IndexDatabaseTest method checkDataExistence.

private void checkDataExistence(String fileName, boolean shouldExist) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    for (String dirName : new String[] { "historycache", IndexDatabase.XREF_DIR }) {
        File dataDir = new File(env.getDataRootFile(), dirName);
        File dataFile = new File(dataDir, fileName + ".gz");
        if (shouldExist) {
            Assert.assertTrue("file " + fileName + " not found in " + dirName, dataFile.exists());
        } else {
            Assert.assertFalse("file " + fileName + " found in " + dirName, dataFile.exists());
        }
    }
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) File(java.io.File)

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