Skip to content

Commit

Permalink
GH-5149 code simplification and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Nov 20, 2024
1 parent 08b898c commit 7c42527
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface Model extends Set<Statement>, Serializable, NamespaceAware {
*/
default Namespace setNamespace(String prefix, String name) {
Optional<? extends Namespace> result = getNamespace(prefix);
if (!result.isPresent() || !result.get().getName().equals(name)) {
if (result.isEmpty() || !result.get().getName().equals(name)) {
result = Optional.of(new ModelNamespace(prefix, name));
setNamespace(result.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public static Optional<Resource> getSubjectByType(Model model, IRI type, IRI leg

private static void logDiscrepancyWarning(Optional<? extends Value> preferred,
Optional<? extends Value> fallback) {
if (!fallback.isEmpty() && !preferred.equals(fallback)) {
if (fallback.isPresent() && !preferred.equals(fallback)) {
var msg = "Discrepancy between use of the old and new config vocabulary.";
// depending on whether preferred is set, we log on warn or debug
if (preferred.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private static boolean isomorphicSingleContext(Model model1, Model model2) {

// Because we have previously already checked that the models are the same size, we don't have to check both
// ways to establish model equality.
return !missingInModel2.isPresent();
return missingInModel2.isEmpty();
}

private static boolean mappingsIncompatible(Map<BNode, HashCode> mapping1, Map<BNode, HashCode> mapping2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueEx
Literal rightLit = (Literal) rightVal;

if (leftLit.getLanguage().isPresent()) {
if (!rightLit.getLanguage().isPresent() || rightLit.getLanguage().equals(leftLit.getLanguage())) {
if (rightLit.getLanguage().isEmpty() || rightLit.getLanguage().equals(leftLit.getLanguage())) {

String leftLexVal = leftLit.getLabel();
String rightLexVal = rightLit.getLabel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static Literal createLiteral(String label, String lang, IRI datatype, Par
try {
// Removes datatype for langString datatype with no language tag when VERIFY_DATATYPE_VALUES is False.
if ((workingDatatype == null || RDF.LANGSTRING.equals(workingDatatype))
&& (!workingLang.isPresent() || workingLang.get().isEmpty())
&& (workingLang.isEmpty() || workingLang.get().isEmpty())
&& !parserConfig.get(BasicParserSettings.VERIFY_DATATYPE_VALUES)) {
workingLang = Optional.ofNullable(null);
workingDatatype = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void handleStatement(RDFDataset result, Statement nextStatement) {

// In RDF-1.1, RDF-1.0 Plain Literals are now Typed Literals with
// type xsd:String
if (!literal.getLanguage().isPresent() && datatype == null) {
if (literal.getLanguage().isEmpty() && datatype == null) {
datatype = XSD.STRING.stringValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,8 @@ protected SearchDocument getDocument(String id) throws IOException {
@Override
protected Iterable<? extends SearchDocument> getDocuments(String resourceId) throws IOException {
SearchHits hits = getDocuments(QueryBuilders.termQuery(SearchFields.URI_FIELD_NAME, resourceId));
return Iterables.transform(hits, new Function<>() {

@Override
public SearchDocument apply(SearchHit hit) {
return new ElasticsearchDocument(hit, geoContextMapper);
}
});
return Iterables.transform(hits,
(Function<SearchHit, SearchDocument>) hit -> new ElasticsearchDocument(hit, geoContextMapper));
}

@Override
Expand Down Expand Up @@ -579,18 +574,14 @@ protected Iterable<? extends DocumentScore> query(Resource subject, QuerySpec sp

SearchHits hits;
int numDocs = Objects.requireNonNullElse(spec.getNumDocs(), -1);

if (subject != null) {
hits = search(subject, request, qb, numDocs);
} else {
hits = search(request, qb, numDocs);
}
return Iterables.transform(hits, new Function<>() {

@Override
public DocumentScore apply(SearchHit hit) {
return new ElasticsearchDocumentScore(hit, geoContextMapper);
}
});
return Iterables.transform(hits,
(Function<SearchHit, DocumentScore>) hit -> new ElasticsearchDocumentScore(hit, geoContextMapper));
}

/**
Expand Down Expand Up @@ -701,13 +692,8 @@ protected Iterable<? extends DocumentResult> geoRelationQuery(String relation, I

SearchRequestBuilder request = client.prepareSearch();
SearchHits hits = search(request, QueryBuilders.boolQuery().must(qb).filter(fb));
return Iterables.transform(hits, new Function<>() {

@Override
public DocumentResult apply(SearchHit hit) {
return new ElasticsearchDocumentResult(hit, geoContextMapper);
}
});
return Iterables.transform(hits,
(Function<SearchHit, DocumentResult>) hit -> new ElasticsearchDocumentResult(hit, geoContextMapper));
}

private ShapeRelation toSpatialOp(String relation) {
Expand Down Expand Up @@ -735,30 +721,31 @@ public SearchHits search(SearchRequestBuilder request, QueryBuilder query) {
*/
public SearchHits search(SearchRequestBuilder request, QueryBuilder query, int numDocs) {
String[] types = getTypes();
int nDocs;
if (numDocs > 0) {
if (maxDocs > 0 && maxDocs < numDocs) {
nDocs = maxDocs;
} else {
nDocs = numDocs;
}
} else if (defaultNumDocs > 0) {
nDocs = defaultNumDocs;
} else {

assert numDocs >= -1 : "numDocs is -1 when undefined and >= 0 when defined";

int size = defaultNumDocs;
if (numDocs >= 0) {
size = Math.min(maxDocs, numDocs);
}

if (size < 0) {
// defaultNumDocs is not set
long docCount = client.prepareSearch(indexName)
.setTypes(types)
.setSource(new SearchSourceBuilder().size(0).query(query))
.get()
.getHits()
.getTotalHits().value;
nDocs = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1);
size = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1);
}

SearchResponse response = request.setIndices(indexName)
.setTypes(types)
.setVersion(false)
.seqNoAndPrimaryTerm(true)
.setQuery(query)
.setSize(nDocs)
.setSize(size)
.execute()
.actionGet();
return response.getHits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public abstract class AbstractSearchIndex implements SearchIndex {
REJECTED_DATATYPES.add("http://www.w3.org/2001/XMLSchema#float");
}

protected int defaultNumDocs;
protected int maxDocs;
protected int defaultNumDocs = -1;
protected int maxDocs = -1;

protected Set<String> wktFields = Collections.singleton(SearchFields.getPropertyField(GEO.AS_WKT));

Expand All @@ -79,7 +79,8 @@ public void initialize(Properties parameters) throws Exception {
String maxDocumentsParam = parameters.getProperty(LuceneSail.MAX_DOCUMENTS_KEY);
maxDocs = (maxDocumentsParam != null) ? Integer.parseInt(maxDocumentsParam) : -1;
String defaultNumDocsParam = parameters.getProperty(LuceneSail.DEFAULT_NUM_DOCS_KEY);
defaultNumDocs = (defaultNumDocsParam != null) ? Integer.parseInt(defaultNumDocsParam) : defaultNumDocs;
defaultNumDocs = (defaultNumDocsParam != null) ? Math.min(maxDocs, Integer.parseInt(defaultNumDocsParam))
: maxDocs;

String wktFieldParam = parameters.getProperty(LuceneSail.WKT_FIELDS);
if (wktFieldParam != null) {
Expand Down Expand Up @@ -149,7 +150,7 @@ public boolean accept(Literal literal) {

// we reject literals that aren't in the list of the indexed lang
if (indexedLangs != null
&& (!literal.getLanguage().isPresent()
&& (literal.getLanguage().isEmpty()
|| !indexedLangs.contains(literal.getLanguage().get().toLowerCase()
))) {
return false;
Expand Down Expand Up @@ -356,11 +357,8 @@ public final synchronized void addRemoveStatements(Collection<Statement> added,
// remove value from both property field and the
// corresponding text field
String field = SearchFields.getPropertyField(r.getPredicate());
Set<String> removedValues = removedOfResource.get(field);
if (removedValues == null) {
removedValues = new HashSet<>();
removedOfResource.put(field, removedValues);
}
Set<String> removedValues = removedOfResource.computeIfAbsent(field,
k -> new HashSet<>());
removedValues.add(val);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,19 +1004,16 @@ public synchronized TopDocs search(Query query) throws IOException {
* @throws IOException
*/
public synchronized TopDocs search(Query query, int numDocs) throws IOException {
int nDocs;
if (numDocs > 0) {
if (maxDocs > 0 && maxDocs < numDocs) {
nDocs = maxDocs;
} else {
nDocs = numDocs;
}
} else if (defaultNumDocs > 0) {
nDocs = defaultNumDocs;
} else {
nDocs = Math.max(getIndexReader().numDocs(), 1);
assert numDocs >= -1 : "numDocs is -1 when undefined and >= 0 when defined";

int size = defaultNumDocs;
if (numDocs >= 0) {
size = Math.min(maxDocs, numDocs);
}
if (size < 0) {
size = Math.max(getIndexReader().numDocs(), 1);
}
return getIndexSearcher().search(query, nDocs);
return getIndexSearcher().search(query, size);
}

private QueryParser getQueryParser(IRI propertyURI) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public PlanNode generateTransactionalValidationPlan(ConnectionsGroup connections
connectionsGroup.getRdfsSubClassOfReasoner(), stableRandomVariableProvider);
Optional<Path> path = getTargetChain().getPath();

if (!path.isPresent() || scope != Scope.propertyShape) {
if (path.isEmpty() || scope != Scope.propertyShape) {
throw new IllegalStateException("UniqueLang only operates on paths");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void calculateNext() {
if (value.isLiteral()) {
Optional<String> lang = ((Literal) value).getLanguage();

if (!lang.isPresent()) {
if (lang.isEmpty()) {
next = null;
} else if (!seenLanguages.contains(lang.get())) {
seenLanguages.add(lang.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,20 +583,17 @@ public QueryResponse search(SolrQuery query) throws SolrServerException, IOExcep
* @throws IOException
*/
public QueryResponse search(SolrQuery query, int numDocs) throws SolrServerException, IOException {
int nDocs;
if (numDocs > 0) {
if (maxDocs > 0 && maxDocs < numDocs) {
nDocs = maxDocs;
} else {
nDocs = numDocs;
}
} else if (defaultNumDocs > 0) {
nDocs = defaultNumDocs;
} else {
assert numDocs >= -1 : "numDocs is -1 when undefined and >= 0 when defined";

int size = defaultNumDocs;
if (numDocs >= 0) {
size = Math.min(maxDocs, numDocs);
}
if (size < 0) {
long docCount = client.query(query.setRows(0)).getResults().getNumFound();
nDocs = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1);
size = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1);
}
return client.query(query.setRows(nDocs));
return client.query(query.setRows(size));
}

private SolrQuery prepareQuery(IRI propertyURI, SolrQuery query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void convert(String fileFrom, String fileTo) {
return;
}
Optional<RDFFormat> fmtFrom = Rio.getParserFormatForFileName(fileFrom);
if (!fmtFrom.isPresent()) {
if (fmtFrom.isEmpty()) {
writeError("No RDF parser for " + fileFrom);
return;
}
Expand All @@ -116,7 +116,7 @@ private void convert(String fileFrom, String fileTo) {
return;
}
Optional<RDFFormat> fmtTo = Rio.getWriterFormatForFileName(fileTo);
if (!fmtTo.isPresent()) {
if (fmtTo.isEmpty()) {
writeError("No RDF writer for " + fileTo);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private QueryResultWriter getQueryResultWriter(Path path, OutputStream out) thro
w = new ConsoleQueryResultWriter(consoleIO, getConsoleWidth());
} else {
Optional<QueryResultFormat> fmt = QueryResultIO.getWriterFormatForFileName(path.toFile().toString());
if (!fmt.isPresent()) {
if (fmt.isEmpty()) {
throw new IllegalArgumentException("No suitable result writer found");
}
w = QueryResultIO.createWriter(fmt.get(), out);
Expand All @@ -342,7 +342,7 @@ private RDFWriter getRDFWriter(Path path, OutputStream out) throws IllegalArgume
w = new ConsoleRDFWriter(consoleIO, getConsoleWidth());
} else {
Optional<RDFFormat> fmt = Rio.getWriterFormatForFileName(path.toFile().toString());
if (!fmt.isPresent()) {
if (fmt.isEmpty()) {
throw new IllegalArgumentException("No suitable result writer found");
}
w = Rio.createWriter(fmt.get(), out);
Expand Down

0 comments on commit 7c42527

Please sign in to comment.