41
41
import org .eclipse .rdf4j .query .MalformedQueryException ;
42
42
import org .eclipse .rdf4j .query .algebra .Var ;
43
43
import org .eclipse .rdf4j .query .algebra .evaluation .QueryBindingSet ;
44
+ import org .eclipse .rdf4j .sail .Sail ;
44
45
import org .eclipse .rdf4j .sail .SailException ;
45
46
import org .eclipse .rdf4j .sail .lucene .util .MapOfListMaps ;
46
47
import org .locationtech .spatial4j .context .SpatialContext ;
@@ -65,8 +66,8 @@ public abstract class AbstractSearchIndex implements SearchIndex {
65
66
REJECTED_DATATYPES .add ("http://www.w3.org/2001/XMLSchema#float" );
66
67
}
67
68
68
- protected int defaultNumDocs ;
69
- protected int maxDocs ;
69
+ protected int defaultNumDocs = - 1 ;
70
+ protected int maxDocs = Integer . MAX_VALUE ;
70
71
71
72
protected Set <String > wktFields = Collections .singleton (SearchFields .getPropertyField (GEO .AS_WKT ));
72
73
@@ -77,9 +78,28 @@ public abstract class AbstractSearchIndex implements SearchIndex {
77
78
@ Override
78
79
public void initialize (Properties parameters ) throws Exception {
79
80
String maxDocumentsParam = parameters .getProperty (LuceneSail .MAX_DOCUMENTS_KEY );
80
- maxDocs = (maxDocumentsParam != null ) ? Integer .parseInt (maxDocumentsParam ) : -1 ;
81
81
String defaultNumDocsParam = parameters .getProperty (LuceneSail .DEFAULT_NUM_DOCS_KEY );
82
- defaultNumDocs = (defaultNumDocsParam != null ) ? Integer .parseInt (defaultNumDocsParam ) : defaultNumDocs ;
82
+
83
+ if ((maxDocumentsParam != null )) {
84
+ maxDocs = Integer .parseInt (maxDocumentsParam );
85
+
86
+ // if maxDocs is set then defaultNumDocs is set to maxDocs if it is not set, because we now have a known
87
+ // upper limit
88
+ defaultNumDocs = (defaultNumDocsParam != null ) ? Math .min (maxDocs , Integer .parseInt (defaultNumDocsParam ))
89
+ : maxDocs ;
90
+ } else {
91
+ // we can never return more than Integer.MAX_VALUE documents
92
+ maxDocs = Integer .MAX_VALUE ;
93
+
94
+ // legacy behaviour is to return the number of documents that the query would return if there was no limit,
95
+ // so if the defaultNumDocs is not set, we set it to -1 to signal that there is no limit
96
+ defaultNumDocs = (defaultNumDocsParam != null ) ? Integer .parseInt (defaultNumDocsParam ) : -1 ;
97
+ }
98
+
99
+ if (defaultNumDocs > maxDocs ) {
100
+ throw new IllegalArgumentException (LuceneSail .DEFAULT_NUM_DOCS_KEY + " must be less than or equal to "
101
+ + LuceneSail .MAX_DOCUMENTS_KEY + " (" + defaultNumDocs + " > " + maxDocs + ")" );
102
+ }
83
103
84
104
String wktFieldParam = parameters .getProperty (LuceneSail .WKT_FIELDS );
85
105
if (wktFieldParam != null ) {
@@ -149,7 +169,7 @@ public boolean accept(Literal literal) {
149
169
150
170
// we reject literals that aren't in the list of the indexed lang
151
171
if (indexedLangs != null
152
- && (! literal .getLanguage ().isPresent ()
172
+ && (literal .getLanguage ().isEmpty ()
153
173
|| !indexedLangs .contains (literal .getLanguage ().get ().toLowerCase ()
154
174
))) {
155
175
return false ;
@@ -356,11 +376,8 @@ public final synchronized void addRemoveStatements(Collection<Statement> added,
356
376
// remove value from both property field and the
357
377
// corresponding text field
358
378
String field = SearchFields .getPropertyField (r .getPredicate ());
359
- Set <String > removedValues = removedOfResource .get (field );
360
- if (removedValues == null ) {
361
- removedValues = new HashSet <>();
362
- removedOfResource .put (field , removedValues );
363
- }
379
+ Set <String > removedValues = removedOfResource .computeIfAbsent (field ,
380
+ k -> new HashSet <>());
364
381
removedValues .add (val );
365
382
}
366
383
}
@@ -548,7 +565,8 @@ private Iterable<? extends DocumentScore> evaluateQuery(QuerySpec query) {
548
565
hits = query (query .getSubject (), query );
549
566
}
550
567
} catch (Exception e ) {
551
- logger .error ("There was a problem evaluating query '" + query .getCatQuery () + "'!" , e );
568
+ logger .error ("There was a problem evaluating query '{}'!" , query .getCatQuery (), e );
569
+ assert false : "There was a problem evaluating query '" + query .getCatQuery () + "'!" ;
552
570
}
553
571
554
572
return hits ;
@@ -720,6 +738,8 @@ private Iterable<? extends DocumentDistance> evaluateQuery(DistanceQuerySpec que
720
738
} catch (Exception e ) {
721
739
logger .error ("There was a problem evaluating distance query 'within " + distance + getUnitSymbol (units )
722
740
+ " of " + from .getLabel () + "'!" , e );
741
+ assert false : "There was a problem evaluating distance query 'within " + distance + getUnitSymbol (units )
742
+ + " of " + from .getLabel () + "'!" ;
723
743
}
724
744
725
745
return hits ;
@@ -828,6 +848,8 @@ private Iterable<? extends DocumentResult> evaluateQuery(GeoRelationQuerySpec qu
828
848
} catch (Exception e ) {
829
849
logger .error ("There was a problem evaluating spatial relation query '" + query .getRelation () + " "
830
850
+ qgeom .getLabel () + "'!" , e );
851
+ assert false : "There was a problem evaluating spatial relation query '" + query .getRelation () + " "
852
+ + qgeom .getLabel () + "'!" ;
831
853
}
832
854
833
855
return hits ;
0 commit comments