Search in sources :

Example 1 with HistoryEntry

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));
    }
}
Also used : HistoryException(org.opengrok.indexer.history.HistoryException) History(org.opengrok.indexer.history.History) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) TextField(org.apache.lucene.document.TextField) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) Project(org.opengrok.indexer.configuration.Project) StringField(org.apache.lucene.document.StringField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) TextField(org.apache.lucene.document.TextField) HistoryEntry(org.opengrok.indexer.history.HistoryEntry) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) File(java.io.File) BytesRef(org.apache.lucene.util.BytesRef) HistoryReader(org.opengrok.indexer.history.HistoryReader)

Example 2 with HistoryEntry

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);
}
Also used : HistoryDTO(org.opengrok.web.api.v1.controller.HistoryController.HistoryDTO) HistoryController.getHistoryDTO(org.opengrok.web.api.v1.controller.HistoryController.getHistoryDTO) HistoryEntry(org.opengrok.indexer.history.HistoryEntry) Date(java.util.Date) HistoryEntryDTO(org.opengrok.web.api.v1.controller.HistoryController.HistoryEntryDTO) Test(org.junit.jupiter.api.Test)

Example 3 with HistoryEntry

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;
}
Also used : Hit(org.opengrok.indexer.search.Hit) HistoryEntry(org.opengrok.indexer.history.HistoryEntry) HistoryException(org.opengrok.indexer.history.HistoryException) IOException(java.io.IOException)

Example 4 with HistoryEntry

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;
}
Also used : HistoryEntry(org.opengrok.indexer.history.HistoryEntry) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

HistoryEntry (org.opengrok.indexer.history.HistoryEntry)4 File (java.io.File)2 HistoryException (org.opengrok.indexer.history.HistoryException)2 IOException (java.io.IOException)1 Date (java.util.Date)1 Field (org.apache.lucene.document.Field)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 StringField (org.apache.lucene.document.StringField)1 TextField (org.apache.lucene.document.TextField)1 BytesRef (org.apache.lucene.util.BytesRef)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.junit.jupiter.api.Test)1 Project (org.opengrok.indexer.configuration.Project)1 History (org.opengrok.indexer.history.History)1 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)1 HistoryReader (org.opengrok.indexer.history.HistoryReader)1 Hit (org.opengrok.indexer.search.Hit)1 HistoryDTO (org.opengrok.web.api.v1.controller.HistoryController.HistoryDTO)1 HistoryEntryDTO (org.opengrok.web.api.v1.controller.HistoryController.HistoryEntryDTO)1 HistoryController.getHistoryDTO (org.opengrok.web.api.v1.controller.HistoryController.getHistoryDTO)1