Search in sources :

Example 36 with RuntimeEnvironment

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

the class IndexDatabase method isLocal.

/**
     * Check if a file is local to the current project. If we don't have
     * projects, check if the file is in the source root.
     *
     * @param path the path to a file
     * @return true if the file is local to the current repository
     */
private boolean isLocal(String path) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String srcRoot = env.getSourceRootPath();
    boolean local = false;
    if (path.startsWith(srcRoot)) {
        if (env.hasProjects()) {
            String relPath = path.substring(srcRoot.length());
            if (project.equals(Project.getProject(relPath))) {
                // File is under the current project, so it's local.
                local = true;
            }
        } else {
            // File is under source root, and we don't have projects, so
            // consider it local.
            local = true;
        }
    }
    return local;
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment)

Example 37 with RuntimeEnvironment

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

the class IndexDatabase method listAllFiles.

/**
     * List all files in some of the index databases
     *
     * @param subFiles Subdirectories for the various projects to list the files
     * for (or null or an empty list to dump all projects)
     * @throws IOException if an error occurs
     */
public static void listAllFiles(List<String> subFiles) throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.hasProjects()) {
        if (subFiles == null || subFiles.isEmpty()) {
            for (Project project : env.getProjects()) {
                IndexDatabase db = new IndexDatabase(project);
                db.listFiles();
            }
        } else {
            for (String path : subFiles) {
                Project project = Project.getProject(path);
                if (project == null) {
                    LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
                } else {
                    IndexDatabase db = new IndexDatabase(project);
                    db.listFiles();
                }
            }
        }
    } else {
        IndexDatabase db = new IndexDatabase();
        db.listFiles();
    }
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment)

Example 38 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 Exuberant ctags, generating xrefs
     * and storing data from the source files in the index (along with history,
     * if any).
     */
public void doIndexerExecution(final boolean update, int noThreads, List<String> subFiles, IndexChangedListener progress) throws IOException {
    Statistics elapsed = new Statistics();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance().register();
    LOGGER.info("Starting indexing");
    ExecutorService executor = Executors.newFixedThreadPool(noThreads);
    if (subFiles == null || subFiles.isEmpty()) {
        if (update) {
            IndexDatabase.updateAll(executor, progress);
        } else if (env.isOptimizeDatabase()) {
            IndexDatabase.optimizeAll(executor);
        }
    } 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);
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (update) {
                            db.update();
                        } else if (optimize) {
                            db.optimize();
                        }
                    } catch (Throwable e) {
                        LOGGER.log(Level.SEVERE, "An error occured while " + (update ? "updating" : "optimizing") + " index", e);
                    }
                }
            });
        }
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
        try {
            // Wait forever
            executor.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);
    }
    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) Project(org.opensolaris.opengrok.configuration.Project) ExecutorService(java.util.concurrent.ExecutorService)

Example 39 with RuntimeEnvironment

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

the class Util method readableLine.

public static void readableLine(int num, Writer out, Annotation annotation, String userPageLink, String userPageSuffix, String project, boolean skipNewline) throws IOException {
    // this method should go to JFlexXref
    String snum = String.valueOf(num);
    if (num > 1 && !skipNewline) {
        out.write("\n");
    }
    out.write(anchorClassStart);
    out.write(num % 10 == 0 ? "hl" : "l");
    out.write("\" name=\"");
    out.write(snum);
    out.write("\" href=\"#");
    out.write(snum);
    out.write(closeQuotedTag);
    out.write(snum);
    out.write(anchorEnd);
    if (annotation != null) {
        String r = annotation.getRevision(num);
        boolean enabled = annotation.isEnabled(num);
        out.write("<span class=\"blame\">");
        if (enabled) {
            out.write(anchorClassStart);
            out.write("r");
            if (annotation.getFileVersion(r) != 0) {
                /*
                        version number, 1 is the most recent
                        generates css classes version_color_n
                     */
                int versionNumber = Math.max(1, annotation.getFileVersionsCount() - annotation.getFileVersion(r) + 1);
                out.write(" version_color_" + versionNumber);
            }
            out.write("\" href=\"");
            out.write(URIEncode(annotation.getFilename()));
            out.write("?a=true&amp;r=");
            out.write(URIEncode(r));
            String msg = annotation.getDesc(r);
            out.write("\" title=\"");
            if (msg != null) {
                out.write(msg);
            }
            if (annotation.getFileVersion(r) != 0) {
                out.write("&lt;br/&gt;version: " + annotation.getFileVersion(r) + "/" + annotation.getRevisions().size());
            }
            out.write(closeQuotedTag);
        }
        StringBuilder buf = new StringBuilder();
        htmlize(r, buf);
        out.write(buf.toString());
        buf.setLength(0);
        if (enabled) {
            RuntimeEnvironment env = RuntimeEnvironment.getInstance();
            out.write(anchorEnd);
            // Write link to search the revision in current project.
            out.write(anchorClassStart);
            out.write("search\" href=\"" + env.getUrlPrefix());
            out.write("defs=&amp;refs=&amp;path=");
            out.write(project);
            out.write("&amp;hist=" + URIEncode(r));
            out.write("&amp;type=\" title=\"Search history for this changeset");
            out.write(closeQuotedTag);
            out.write("S");
            out.write(anchorEnd);
        }
        String a = annotation.getAuthor(num);
        if (userPageLink == null) {
            out.write("<span class=\"a\">");
            htmlize(a, buf);
            out.write(buf.toString());
            out.write("</span>");
            buf.setLength(0);
        } else {
            out.write(anchorClassStart);
            out.write("a\" href=\"");
            out.write(userPageLink);
            out.write(URIEncode(a));
            if (userPageSuffix != null) {
                out.write(userPageSuffix);
            }
            out.write(closeQuotedTag);
            htmlize(a, buf);
            out.write(buf.toString());
            buf.setLength(0);
            out.write(anchorEnd);
        }
        out.write("</span>");
    }
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment)

Example 40 with RuntimeEnvironment

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

the class IndexDatabase method optimizeAll.

/**
     * Optimize all index databases
     *
     * @param executor An executor to run the job
     * @throws IOException if an error occurs
     */
static void optimizeAll(ExecutorService executor) throws IOException {
    List<IndexDatabase> dbs = new ArrayList<>();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.hasProjects()) {
        for (Project project : env.getProjects()) {
            dbs.add(new IndexDatabase(project));
        }
    } else {
        dbs.add(new IndexDatabase());
    }
    for (IndexDatabase d : dbs) {
        final IndexDatabase db = d;
        if (db.isDirty()) {
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        db.update();
                    } catch (Throwable e) {
                        LOGGER.log(Level.SEVERE, "Problem updating lucene index database: ", e);
                    }
                }
            });
        }
    }
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) ArrayList(java.util.ArrayList)

Aggregations

RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)45 File (java.io.File)20 IOException (java.io.IOException)17 Project (org.opensolaris.opengrok.configuration.Project)12 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 FileNotFoundException (java.io.FileNotFoundException)5 ParseException (java.text.ParseException)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 DateFormat (java.text.DateFormat)4 TestRepository (org.opensolaris.opengrok.util.TestRepository)4 Date (java.util.Date)3 IndexReader (org.apache.lucene.index.IndexReader)3 BeforeClass (org.junit.BeforeClass)3 OutputStreamWriter (java.io.OutputStreamWriter)2 HistoryException (org.opensolaris.opengrok.history.HistoryException)2 Repository (org.opensolaris.opengrok.history.Repository)2 Executor (org.opensolaris.opengrok.util.Executor)2 BufferedWriter (java.io.BufferedWriter)1