Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,49 @@
*/
package org.opengrok.indexer.analysis;

import org.apache.lucene.document.Document;
import org.opengrok.indexer.configuration.RuntimeEnvironment;

import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
* Class to wrap Xref production with timeout. This should be used for all classes that override
* {@link FileAnalyzer#analyze(Document, StreamSource, Writer)}.
*/
public class XrefWork {
public Xrefer xrefer;
public Exception exception;
private Xrefer xrefer;
private Exception exception;
private final WriteXrefArgs args;
private final AbstractAnalyzer analyzer;

public XrefWork(Xrefer xrefer) {
this.xrefer = xrefer;
public XrefWork(WriteXrefArgs args, AbstractAnalyzer analyzer) {
this.args = args;
this.analyzer = analyzer;
}

public XrefWork(Exception e) {
this.exception = e;
public Xrefer getXrefer() throws ExecutionException, InterruptedException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();

CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
try {
xrefer = this.analyzer.writeXref(args);
} catch (IOException e) {
exception = e;
}
return this;
}, env.getIndexerParallelizer().getXrefWatcherExecutor()).
orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);

XrefWork xrefWork = future.get(); // Will throw ExecutionException wrapping TimeoutException on timeout.
if (xrefWork.xrefer != null) {
return xrefer;
} else {
// Re-throw the exception from writeXref().
throw new ExecutionException(xrefWork.exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*/

/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.document;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.ExecutionException;

import org.apache.lucene.document.Document;
import org.opengrok.indexer.analysis.AbstractAnalyzer;
import org.opengrok.indexer.analysis.AnalyzerFactory;
Expand All @@ -35,6 +37,7 @@
import org.opengrok.indexer.analysis.StreamSource;
import org.opengrok.indexer.analysis.TextAnalyzer;
import org.opengrok.indexer.analysis.WriteXrefArgs;
import org.opengrok.indexer.analysis.XrefWork;
import org.opengrok.indexer.analysis.Xrefer;
import org.opengrok.indexer.search.QueryBuilder;

Expand Down Expand Up @@ -73,8 +76,7 @@ protected int getSpecializedVersionNo() {
}

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut)
throws IOException {
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {

// this is to explicitly use appropriate analyzers tokenstream to
// workaround #1376 symbols search works like full text search
Expand All @@ -87,10 +89,15 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut)
try (Reader in = getReader(src.getStream())) {
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
args.setProject(project);
Xrefer xref = writeXref(args);
XrefWork xrefWork = new XrefWork(args, this);

String path = doc.get(QueryBuilder.PATH);
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
try {
Xrefer xref = xrefWork.getXrefer();
String path = doc.get(QueryBuilder.PATH);
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
} catch (ExecutionException e) {
throw new InterruptedException("failed to generate xref :" + e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*/

/*
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.document;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.ExecutionException;

import org.apache.lucene.document.Document;
import org.opengrok.indexer.analysis.AbstractAnalyzer;
import org.opengrok.indexer.analysis.AnalyzerFactory;
Expand All @@ -35,12 +37,12 @@
import org.opengrok.indexer.analysis.StreamSource;
import org.opengrok.indexer.analysis.TextAnalyzer;
import org.opengrok.indexer.analysis.WriteXrefArgs;
import org.opengrok.indexer.analysis.XrefWork;
import org.opengrok.indexer.analysis.Xrefer;
import org.opengrok.indexer.search.QueryBuilder;

/**
* Analyzes [tn]roff files.
*
* Created on September 30, 2005
* @author Chandan
*/
Expand Down Expand Up @@ -75,7 +77,7 @@ protected int getSpecializedVersionNo() {
}

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
//this is to explicitly use appropriate analyzers tokenstream to workaround #1376 symbols search works like full text search
JFlexTokenizer symbolTokenizer = symbolTokenizerFactory.get();
symbolTokenizer.setReader(getReader(src.getStream()));
Expand All @@ -86,18 +88,23 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
try (Reader in = getReader(src.getStream())) {
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
args.setProject(project);
Xrefer xref = writeXref(args);
XrefWork xrefWork = new XrefWork(args, this);

String path = doc.get(QueryBuilder.PATH);
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
try {
Xrefer xref = xrefWork.getXrefer();
String path = doc.get(QueryBuilder.PATH);
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
} catch (ExecutionException e) {
throw new InterruptedException("failed to generate xref :" + e);
}
}
}
}

/**
* Creates a wrapped {@link TroffXref} instance.
* @param reader the data to produce xref for
* @return an xref instance
* @return xref instance
*/
@Override
protected Xrefer newXref(Reader reader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.apache.lucene.document.Document;
Expand All @@ -48,7 +46,6 @@
import org.opengrok.indexer.analysis.WriteXrefArgs;
import org.opengrok.indexer.analysis.XrefWork;
import org.opengrok.indexer.analysis.Xrefer;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.search.QueryBuilder;
import org.opengrok.indexer.util.NullWriter;

Expand Down Expand Up @@ -149,34 +146,21 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc

if (xrefOut != null) {
try (Reader in = getReader(src.getStream())) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
args.setDefs(defs);
args.setProject(project);
CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
try {
return new XrefWork(writeXref(args));
} catch (IOException e) {
return new XrefWork(e);
}
}, env.getIndexerParallelizer().getXrefWatcherExecutor()).
orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);
XrefWork xrefWork = future.get(); // Will throw ExecutionException wrapping TimeoutException on timeout.
Xrefer xref = xrefWork.xrefer;
XrefWork xrefWork = new XrefWork(args, this);
Xrefer xref = xrefWork.getXrefer();

if (xref != null) {
Scopes scopes = xref.getScopes();
if (scopes.size() > 0) {
byte[] scopesSerialized = scopes.serialize();
doc.add(new StoredField(QueryBuilder.SCOPES,
scopesSerialized));
doc.add(new StoredField(QueryBuilder.SCOPES, scopesSerialized));
}

String path = doc.get(QueryBuilder.PATH);
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
} else {
// Re-throw the exception from writeXref().
throw new IOException(xrefWork.exception);
}
} catch (ExecutionException e) {
throw new InterruptedException("failed to generate xref :" + e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.apache.lucene.document.Document;
import org.opengrok.indexer.analysis.AnalyzerFactory;
Expand Down Expand Up @@ -87,23 +85,12 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
try (Reader in = getReader(src.getStream())) {
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
args.setProject(project);
CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
try {
return new XrefWork(writeXref(args));
} catch (IOException e) {
return new XrefWork(e);
}
}, env.getIndexerParallelizer().getXrefWatcherExecutor()).
orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);
XrefWork xrefWork = future.get(); // Will throw ExecutionException wrapping TimeoutException on timeout.
Xrefer xref = xrefWork.xrefer;
XrefWork xrefWork = new XrefWork(args, this);
Xrefer xref = xrefWork.getXrefer();

if (xref != null) {
String path = doc.get(QueryBuilder.PATH);
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
} else {
// Re-throw the exception from writeXref().
throw new IOException(xrefWork.exception);
}
} catch (ExecutionException e) {
throw new InterruptedException("failed to generate xref :" + e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*/

/*
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.uue;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.ExecutionException;

import org.apache.lucene.document.Document;
import org.opengrok.indexer.analysis.AbstractAnalyzer;
import org.opengrok.indexer.analysis.AnalyzerFactory;
Expand All @@ -35,6 +37,7 @@
import org.opengrok.indexer.analysis.StreamSource;
import org.opengrok.indexer.analysis.TextAnalyzer;
import org.opengrok.indexer.analysis.WriteXrefArgs;
import org.opengrok.indexer.analysis.XrefWork;
import org.opengrok.indexer.search.QueryBuilder;

/**
Expand Down Expand Up @@ -74,7 +77,7 @@ protected int getSpecializedVersionNo() {
}

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
//this is to explicitly use appropriate analyzers tokenstream to workaround #1376 symbols search works like full text search
JFlexTokenizer symbolTokenizer = symbolTokenizerFactory.get();
OGKTextField full = new OGKTextField(QueryBuilder.FULL, symbolTokenizer);
Expand All @@ -85,7 +88,12 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
try (Reader in = getReader(src.getStream())) {
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
args.setProject(project);
writeXref(args);
XrefWork xrefWork = new XrefWork(args, this);
try {
xrefWork.getXrefer();
} catch (ExecutionException e) {
throw new InterruptedException("failed to generate xref :" + e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2009, 2011, Jens Elkner.
*/
package org.opengrok.indexer.analysis.document;
Expand Down Expand Up @@ -96,7 +96,7 @@ public static void tearDownAfterClass() throws Exception {
* @throws IOException I/O exception
*/
@Test
void testAnalyze() throws IOException {
void testAnalyze() throws Exception {
Document doc = new Document();
StringWriter xrefOut = new StringWriter();
analyzer.analyze(doc, new StreamSource() {
Expand All @@ -106,5 +106,4 @@ public InputStream getStream() throws IOException {
}
}, xrefOut);
}

}