Search in sources :

Example 46 with RuntimeEnvironment

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

the class IndexDatabase method markProjectIndexed.

private void markProjectIndexed(Project project) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // refresh.
    if (project == null) {
        return;
    }
    // Also need to store the correct value in configuration
    // when indexer writes it to a file.
    project.setIndexed(true);
    if (env.getConfigURI() == null) {
        return;
    }
    Response response;
    try {
        response = ClientBuilder.newBuilder().connectTimeout(env.getConnectTimeout(), TimeUnit.SECONDS).build().target(env.getConfigURI()).path("api").path("v1").path("projects").path(Util.uriEncode(project.getName())).path("indexed").request().headers(getWebAppHeaders()).put(Entity.text(""));
    } catch (RuntimeException e) {
        LOGGER.log(Level.WARNING, String.format("Could not notify the webapp that project %s was indexed", project), e);
        return;
    }
    if (response.getStatus() == Response.Status.ACCEPTED.getStatusCode()) {
        try {
            response = waitForAsyncApi(response);
        } catch (InterruptedException e) {
            LOGGER.log(Level.WARNING, "interrupted while waiting for API response", e);
        }
    }
    if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
        LOGGER.log(Level.WARNING, "Could not notify the webapp that project {0} was indexed: {1}", new Object[] { project, response });
    }
}
Also used : Response(jakarta.ws.rs.core.Response) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment)

Example 47 with RuntimeEnvironment

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

the class IndexDatabase method checkSettings.

/**
 * Verify TABSIZE, and evaluate AnalyzerGuru version together with ZVER --
 * or return a value to indicate mismatch.
 * @param file the source file object
 * @param path the source file path
 * @return {@code false} if a mismatch is detected
 */
private boolean checkSettings(File file, String path) throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // potential xref writer
    boolean outIsXrefWriter = false;
    int reqTabSize = project != null && project.hasTabSizeSetting() ? project.getTabSize() : 0;
    Integer actTabSize = settings.getTabSize();
    if (actTabSize != null && !actTabSize.equals(reqTabSize)) {
        LOGGER.log(Level.FINE, "Tabsize mismatch: {0}", path);
        return false;
    }
    int n = 0;
    postsIter = uidIter.postings(postsIter);
    while (postsIter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
        ++n;
        // Read a limited-fields version of the document.
        Document doc = reader.document(postsIter.docID(), CHECK_FIELDS);
        if (doc == null) {
            LOGGER.log(Level.FINER, "No Document: {0}", path);
            continue;
        }
        long reqGuruVersion = AnalyzerGuru.getVersionNo();
        Long actGuruVersion = settings.getAnalyzerGuruVersion();
        /*
             * For an older OpenGrok index that does not yet have a defined,
             * stored analyzerGuruVersion, break so that no extra work is done.
             * After a re-index, the guru version check will be active.
             */
        if (actGuruVersion == null) {
            break;
        }
        AbstractAnalyzer fa = null;
        String fileTypeName;
        if (actGuruVersion.equals(reqGuruVersion)) {
            fileTypeName = doc.get(QueryBuilder.TYPE);
            if (fileTypeName == null) {
                // (Should not get here, but break just in case.)
                LOGGER.log(Level.FINEST, "Missing TYPE field: {0}", path);
                break;
            }
            AnalyzerFactory fac = AnalyzerGuru.findByFileTypeName(fileTypeName);
            if (fac != null) {
                fa = fac.getAnalyzer();
            }
        } else {
            /*
                 * If the stored guru version does not match, re-verify the
                 * selection of analyzer or return a value to indicate the
                 * analyzer is now mis-matched.
                 */
            LOGGER.log(Level.FINER, "Guru version mismatch: {0}", path);
            fa = getAnalyzerFor(file, path);
            fileTypeName = fa.getFileTypeName();
            String oldTypeName = doc.get(QueryBuilder.TYPE);
            if (!fileTypeName.equals(oldTypeName)) {
                if (LOGGER.isLoggable(Level.FINE)) {
                    LOGGER.log(Level.FINE, "Changed {0} to {1}: {2}", new Object[] { oldTypeName, fileTypeName, path });
                }
                return false;
            }
        }
        // Verify Analyzer version, or return a value to indicate mismatch.
        long reqVersion = AnalyzerGuru.getAnalyzerVersionNo(fileTypeName);
        Long actVersion = settings.getAnalyzerVersion(fileTypeName);
        if (actVersion == null || !actVersion.equals(reqVersion)) {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.log(Level.FINE, "{0} version mismatch: {1}", new Object[] { fileTypeName, path });
            }
            return false;
        }
        if (fa != null) {
            outIsXrefWriter = true;
        }
        // The versions checks have passed.
        break;
    }
    if (n < 1) {
        LOGGER.log(Level.FINER, "Missing index Documents: {0}", path);
        return false;
    }
    // If the economy mode is on, this should be treated as a match.
    if (!env.isGenerateHtml()) {
        if (xrefExistsFor(path)) {
            LOGGER.log(Level.FINEST, "Extraneous {0} , removing its xref file", path);
            removeXrefFile(path);
        }
        return true;
    }
    return (!outIsXrefWriter || xrefExistsFor(path));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) Document(org.apache.lucene.document.Document) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory)

Example 48 with RuntimeEnvironment

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

the class IndexDatabase method getIndexReader.

/**
 * 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.
 */
@SuppressWarnings("java:S2095")
public static IndexReader getIndexReader(String path) {
    IndexReader ret = null;
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    File indexDir = new File(env.getDataRootFile(), INDEX_DIR);
    if (env.hasProjects()) {
        Project p = Project.getProject(path);
        if (p == null) {
            return null;
        }
        indexDir = new File(indexDir, p.getPath());
    }
    try {
        FSDirectory fdir = FSDirectory.open(indexDir.toPath(), NoLockFactory.INSTANCE);
        if (indexDir.exists() && DirectoryReader.indexExists(fdir)) {
            ret = DirectoryReader.open(fdir);
        }
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Failed to open index: {0}", indexDir.getAbsolutePath());
        LOGGER.log(Level.FINE, "Stack Trace: ", ex);
    }
    return ret;
}
Also used : Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) IndexReader(org.apache.lucene.index.IndexReader) FSDirectory(org.apache.lucene.store.FSDirectory) File(java.io.File) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 49 with RuntimeEnvironment

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

the class IndexDatabase method getDocument.

/**
 * @param file File object of a file under source root
 * @return Document object for the file or {@code null}
 * @throws IOException on I/O error
 * @throws ParseException on problem with building Query
 */
public static Document getDocument(File file) throws IOException, ParseException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String path;
    try {
        path = env.getPathRelativeToSourceRoot(file);
    } catch (ForbiddenSymlinkException e) {
        LOGGER.log(Level.FINER, e.getMessage());
        return null;
    }
    // Sanitize Windows path delimiters in order not to conflict with Lucene escape character.
    path = path.replace("\\", "/");
    try (IndexReader ireader = getIndexReader(path)) {
        if (ireader == null) {
            // No index, no document..
            return null;
        }
        Document doc;
        Query q = new QueryBuilder().setPath(path).build();
        IndexSearcher searcher = new IndexSearcher(ireader);
        Statistics stat = new Statistics();
        TopDocs top = searcher.search(q, 1);
        stat.report(LOGGER, Level.FINEST, "search via getDocument done", "search.latency", new String[] { "category", "getdocument", "outcome", top.totalHits.value == 0 ? "empty" : "success" });
        if (top.totalHits.value == 0) {
            // No hits, no document...
            return null;
        }
        doc = searcher.doc(top.scoreDocs[0].doc);
        String foundPath = doc.get(QueryBuilder.PATH);
        // Only use the document if we found an exact match.
        if (!path.equals(foundPath)) {
            return null;
        }
        return doc;
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) QueryBuilder(org.opengrok.indexer.search.QueryBuilder) Document(org.apache.lucene.document.Document) Statistics(org.opengrok.indexer.util.Statistics)

Example 50 with RuntimeEnvironment

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

the class IndexDatabase method accept.

/**
 * Check if I should accept this file into the index database.
 *
 * @param file the file to check
 * @param ret defined instance whose {@code localRelPath} property will be
 * non-null afterward if and only if {@code file} is a symlink that targets
 * either a {@link Repository}-local filesystem object or the same object
 * as a previously-detected and allowed symlink. N.b. method will return
 * {@code false} if {@code ret.localRelPath} is set non-null.
 * @return a value indicating if {@code file} should be included in index
 */
private boolean accept(File file, AcceptSymlinkRet ret) {
    ret.localRelPath = null;
    String absolutePath = file.getAbsolutePath();
    if (!pathAccepter.accept(file)) {
        return false;
    }
    if (!file.canRead()) {
        LOGGER.log(Level.WARNING, "Could not read {0}", absolutePath);
        return false;
    }
    try {
        Path absolute = Paths.get(absolutePath);
        if (Files.isSymbolicLink(absolute)) {
            File canonical = file.getCanonicalFile();
            if (!absolutePath.equals(canonical.getPath()) && !acceptSymlink(absolute, canonical, ret)) {
                if (ret.localRelPath == null) {
                    LOGGER.log(Level.FINE, "Skipped symlink ''{0}'' -> ''{1}''", new Object[] { absolutePath, canonical });
                }
                return false;
            }
        }
        // below will only let go files and directories, anything else is considered special and is not added
        if (!file.isFile() && !file.isDirectory()) {
            LOGGER.log(Level.WARNING, "Ignored special file {0}", absolutePath);
            return false;
        }
    } catch (IOException exp) {
        LOGGER.log(Level.WARNING, "Failed to resolve name: {0}", absolutePath);
        LOGGER.log(Level.FINE, "Stack Trace: ", exp);
    }
    if (file.isDirectory()) {
        // always accept directories so that their files can be examined
        return true;
    }
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // Skip the lookup entirely (which is expensive) if unversioned files are allowed
    if (env.isIndexVersionedFilesOnly()) {
        if (HistoryGuru.getInstance().hasHistory(file)) {
            // versioned files should always be accepted
            return true;
        }
        LOGGER.log(Level.FINER, "not accepting unversioned {0}", absolutePath);
        return false;
    }
    // unversioned files are allowed
    return true;
}
Also used : Path(java.nio.file.Path) TandemPath(org.opengrok.indexer.util.TandemPath) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) IOException(java.io.IOException) File(java.io.File)

Aggregations

RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)81 File (java.io.File)26 Project (org.opengrok.indexer.configuration.Project)24 Test (org.junit.jupiter.api.Test)22 IOException (java.io.IOException)18 BeforeAll (org.junit.jupiter.api.BeforeAll)13 ArrayList (java.util.ArrayList)12 TestRepository (org.opengrok.indexer.util.TestRepository)12 Path (java.nio.file.Path)8 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)8 Document (org.apache.lucene.document.Document)6 Ctags (org.opengrok.indexer.analysis.Ctags)6 Executor (org.opengrok.indexer.util.Executor)6 BufferedReader (java.io.BufferedReader)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)5 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)4