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 });
}
}
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));
}
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;
}
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;
}
}
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;
}
Aggregations