use of org.opengrok.indexer.history.HistoryEntry in project OpenGrok by OpenGrok.
the class AnalyzerGuru method populateDocument.
/**
* Populate a Lucene document with the required fields.
*
* @param doc The document to populate
* @param file The file to index
* @param path Where the file is located (from source root)
* @param fa The analyzer to use on the file
* @param xrefOut Where to write the xref (possibly {@code null})
* @throws IOException If an exception occurs while collecting the data
* @throws InterruptedException if a timeout occurs
*/
public void populateDocument(Document doc, File file, String path, AbstractAnalyzer fa, Writer xrefOut) throws IOException, InterruptedException {
String date = DateTools.timeToString(file.lastModified(), DateTools.Resolution.MILLISECOND);
path = Util.fixPathIfWindows(path);
doc.add(new Field(QueryBuilder.U, Util.path2uid(path, date), string_ft_stored_nanalyzed_norms));
doc.add(new Field(QueryBuilder.FULLPATH, file.getAbsolutePath(), string_ft_nstored_nanalyzed_norms));
doc.add(new SortedDocValuesField(QueryBuilder.FULLPATH, new BytesRef(file.getAbsolutePath())));
if (RuntimeEnvironment.getInstance().isHistoryEnabled()) {
try {
HistoryGuru histGuru = HistoryGuru.getInstance();
HistoryReader hr = histGuru.getHistoryReader(file);
if (hr != null) {
doc.add(new TextField(QueryBuilder.HIST, hr));
History history;
if ((history = histGuru.getHistory(file)) != null) {
List<HistoryEntry> historyEntries = history.getHistoryEntries(1, 0);
if (!historyEntries.isEmpty()) {
HistoryEntry histEntry = historyEntries.get(0);
doc.add(new TextField(QueryBuilder.LASTREV, histEntry.getRevision(), Store.YES));
}
}
}
} catch (HistoryException e) {
LOGGER.log(Level.WARNING, "An error occurred while reading history: ", e);
}
}
doc.add(new Field(QueryBuilder.DATE, date, string_ft_stored_nanalyzed_norms));
doc.add(new SortedDocValuesField(QueryBuilder.DATE, new BytesRef(date)));
// `path' is not null, as it was passed to Util.path2uid() above.
doc.add(new TextField(QueryBuilder.PATH, path, Store.YES));
Project project = Project.getProject(path);
if (project != null) {
doc.add(new TextField(QueryBuilder.PROJECT, project.getPath(), Store.YES));
}
/*
* Use the parent of the path -- not the absolute file as is done for
* FULLPATH -- so that DIRPATH is the same convention as for PATH
* above. A StringField, however, is used instead of a TextField.
*/
File fpath = new File(path);
String fileParent = fpath.getParent();
if (fileParent != null && fileParent.length() > 0) {
String normalizedPath = QueryBuilder.normalizeDirPath(fileParent);
StringField npstring = new StringField(QueryBuilder.DIRPATH, normalizedPath, Store.NO);
doc.add(npstring);
}
if (fa != null) {
AbstractAnalyzer.Genre g = fa.getGenre();
if (g == AbstractAnalyzer.Genre.PLAIN || g == AbstractAnalyzer.Genre.XREFABLE || g == AbstractAnalyzer.Genre.HTML) {
doc.add(new Field(QueryBuilder.T, g.typeName(), string_ft_stored_nanalyzed_norms));
}
fa.analyze(doc, StreamSource.fromFile(file), xrefOut);
String type = fa.getFileTypeName();
doc.add(new StringField(QueryBuilder.TYPE, type, Store.YES));
}
}
use of org.opengrok.indexer.history.HistoryEntry in project OpenGrok by OpenGrok.
the class HistoryControllerTest method testHistoryDTOEquals.
@Test
public void testHistoryDTOEquals() {
HistoryEntry historyEntry = new HistoryEntry("1", new Date(1245446973L / 60 * 60 * 1000), "xyz", "foo", true);
HistoryEntryDTO entry1 = new HistoryEntryDTO(historyEntry);
historyEntry.setAuthor("abc");
HistoryEntryDTO entry2 = new HistoryEntryDTO(historyEntry);
assertEquals(entry1, entry1);
assertNotEquals(entry1, entry2);
HistoryDTO history1 = new HistoryDTO(Collections.singletonList(entry1), 0, 1, 1);
HistoryDTO history2 = new HistoryDTO(Collections.singletonList(entry2), 0, 1, 1);
assertEquals(history1, history1);
assertNotEquals(history1, history2);
}
use of org.opengrok.indexer.history.HistoryEntry in project OpenGrok by OpenGrok.
the class HistoryContext method getHistoryContext.
/**
* Writes matching History log entries from 'in' to 'out' or to 'hits'.
* @param in the history to fetch entries from
* @param out to write matched context
* @param path path to the file
* @param hits list of hits
* @param wcontext web context - beginning of url
*/
private boolean getHistoryContext(History in, String path, Writer out, List<Hit> hits, String wcontext) {
if (in == null) {
throw new IllegalArgumentException("`in' is null");
}
if ((out == null) == (hits == null)) {
// none or both are specified, it's a bug.
throw new IllegalArgumentException("Exactly one of out and hits should be non-null");
}
if (m == null) {
return false;
}
int matchedLines = 0;
Iterator<HistoryEntry> it = in.getHistoryEntries().iterator();
try {
HistoryEntry he;
HistoryEntry nhe = null;
String nrev;
while ((it.hasNext() || (nhe != null)) && matchedLines < 10) {
if (nhe == null) {
he = it.next();
} else {
// nhe is the lookahead revision
he = nhe;
}
String line = he.getLine();
String rev = he.getRevision();
if (it.hasNext()) {
nhe = it.next();
} else {
// this prefetch mechanism is here because of the diff link generation
// we currently generate the diff to previous revision
nhe = null;
}
if (nhe == null) {
nrev = null;
} else {
nrev = nhe.getRevision();
}
tokens.reInit(line);
String token;
int matchState;
long start = -1;
while ((token = tokens.next()) != null) {
for (LineMatcher lineMatcher : m) {
matchState = lineMatcher.match(token);
if (matchState == LineMatcher.MATCHED) {
if (start < 0) {
start = tokens.getMatchStart();
}
long end = tokens.getMatchEnd();
if (start > Integer.MAX_VALUE || end > Integer.MAX_VALUE) {
LOGGER.log(Level.INFO, "Unexpected out of bounds for {0}", path);
} else if (out == null) {
StringBuilder sb = new StringBuilder();
writeMatch(sb, line, (int) start, (int) end, true, path, wcontext, nrev, rev);
hits.add(new Hit(path, sb.toString(), "", false, false));
} else {
writeMatch(out, line, (int) start, (int) end, false, path, wcontext, nrev, rev);
}
matchedLines++;
break;
} else if (matchState == LineMatcher.WAIT) {
if (start < 0) {
start = tokens.getMatchStart();
}
} else {
start = -1;
}
}
}
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Could not get history context for " + path, e);
}
return matchedLines > 0;
}
use of org.opengrok.indexer.history.HistoryEntry in project OpenGrok by OpenGrok.
the class PageConfig method getLastRevFromHistory.
@Nullable
private String getLastRevFromHistory() throws HistoryException {
File file = new File(getEnv().getSourceRootFile(), getPath());
HistoryEntry he = HistoryGuru.getInstance().getLastHistoryEntry(file, true);
if (he != null) {
return he.getRevision();
}
return null;
}
Aggregations