Search in sources :

Example 1 with History

use of org.opengrok.indexer.history.History 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 History

use of org.opengrok.indexer.history.History in project OpenGrok by OpenGrok.

the class HistoryControllerTest method testHistoryGet.

@Test
public void testHistoryGet() throws Exception {
    final String path = "git";
    int size = 5;
    int start = 2;
    Response response = target("history").queryParam("path", path).queryParam("max", size).queryParam("start", start).request().get();
    HistoryDTO history = response.readEntity(new GenericType<>() {
    });
    assertEquals(size, history.getEntries().size());
    assertEquals("Kryštof Tulinger <krystof.tulinger@oracle.com>", history.getEntries().get(0).getAuthor());
    History repoHistory = HistoryGuru.getInstance().getHistory(new File(repository.getSourceRoot(), path));
    assertEquals(history, getHistoryDTO(repoHistory.getHistoryEntries(size, start), repoHistory.getTags(), start, size, repoHistory.getHistoryEntries().size()));
}
Also used : Response(jakarta.ws.rs.core.Response) HistoryDTO(org.opengrok.web.api.v1.controller.HistoryController.HistoryDTO) HistoryController.getHistoryDTO(org.opengrok.web.api.v1.controller.HistoryController.getHistoryDTO) History(org.opengrok.indexer.history.History) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with History

use of org.opengrok.indexer.history.History in project OpenGrok by OpenGrok.

the class HistoryContext method getContext.

public boolean getContext(String filename, String path, List<Hit> hits) throws HistoryException {
    if (m == null) {
        return false;
    }
    File f = new File(filename);
    History history = HistoryGuru.getInstance().getHistory(f);
    if (history == null) {
        LOGGER.log(Level.INFO, "Null history got for {0}", f);
        return false;
    }
    return getHistoryContext(history, path, null, hits, null);
}
Also used : History(org.opengrok.indexer.history.History) File(java.io.File)

Example 4 with History

use of org.opengrok.indexer.history.History in project OpenGrok by OpenGrok.

the class HistoryController method get.

@GET
@CorsEnable
@PathAuthorized
@Produces(MediaType.APPLICATION_JSON)
public HistoryDTO get(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("path") final String path, @QueryParam("withFiles") final boolean withFiles, @QueryParam("max") @DefaultValue(MAX_RESULTS + "") final int maxEntries, @QueryParam("start") @DefaultValue(0 + "") final int startIndex) throws HistoryException, IOException, NoPathParameterException {
    File file = toFile(path);
    History history = HistoryGuru.getInstance().getHistory(file, withFiles, true);
    if (history == null) {
        return null;
    }
    return getHistoryDTO(history.getHistoryEntries(maxEntries, startIndex), history.getTags(), startIndex, maxEntries, history.getHistoryEntries().size());
}
Also used : History(org.opengrok.indexer.history.History) FileUtil.toFile(org.opengrok.web.util.FileUtil.toFile) File(java.io.File) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET) PathAuthorized(org.opengrok.web.api.v1.filter.PathAuthorized)

Aggregations

File (java.io.File)4 History (org.opengrok.indexer.history.History)4 GET (jakarta.ws.rs.GET)1 Produces (jakarta.ws.rs.Produces)1 Response (jakarta.ws.rs.core.Response)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 Test (org.junit.jupiter.api.Test)1 Project (org.opengrok.indexer.configuration.Project)1 HistoryEntry (org.opengrok.indexer.history.HistoryEntry)1 HistoryException (org.opengrok.indexer.history.HistoryException)1 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)1 HistoryReader (org.opengrok.indexer.history.HistoryReader)1 HistoryDTO (org.opengrok.web.api.v1.controller.HistoryController.HistoryDTO)1 HistoryController.getHistoryDTO (org.opengrok.web.api.v1.controller.HistoryController.getHistoryDTO)1 CorsEnable (org.opengrok.web.api.v1.filter.CorsEnable)1 PathAuthorized (org.opengrok.web.api.v1.filter.PathAuthorized)1