@@ -110,8 +110,8 @@ public void addFieldIndex(FieldIndex index) {
110
110
111
111
@ Override
112
112
public void addIndexEntries (Document document ) {
113
- ResourcePath documentPath = document .getKey (). getPath ();
114
- String collectionGroup = documentPath . getSegment ( documentPath . length () - 2 );
113
+ DocumentKey documentKey = document .getKey ();
114
+ String collectionGroup = documentKey . getCollectionGroup ( );
115
115
db .query (
116
116
"SELECT index_id, index_proto FROM index_configuration WHERE collection_group = ? AND active = 1" )
117
117
.binding (collectionGroup )
@@ -127,10 +127,10 @@ public void addIndexEntries(Document document) {
127
127
if (values == null ) return ;
128
128
129
129
if (Logger .isDebugEnabled ()) {
130
- Logger .warn (
130
+ Logger .debug (
131
131
TAG ,
132
132
"Adding index values for document '%s' to index '%s'" ,
133
- documentPath ,
133
+ documentKey ,
134
134
fieldIndex );
135
135
}
136
136
@@ -144,7 +144,7 @@ public void addIndexEntries(Document document) {
144
144
+ "document_name) VALUES(?, ?, ?)" ,
145
145
indexId ,
146
146
encoded ,
147
- documentPath . canonicalString ());
147
+ documentKey . toString ());
148
148
}
149
149
} catch (InvalidProtocolBufferException e ) {
150
150
throw fail ("Invalid index: " + e );
@@ -176,6 +176,8 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
176
176
if (fieldIndex == null ) return null ;
177
177
178
178
Bound lowerBound = target .getLowerBound (fieldIndex );
179
+ String lowerBoundOp = lowerBound .isBefore () ? ">=" : ">" ; // `startAt()` versus `startAfter()`
180
+
179
181
@ Nullable Bound upperBound = target .getUpperBound (fieldIndex );
180
182
181
183
if (Logger .isDebugEnabled ()) {
@@ -200,13 +202,15 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
200
202
lowerBoundValues .size () == upperBoundValues .size (),
201
203
"Expected upper and lower bound size to match" );
202
204
205
+ String upperBoundOp = upperBound .isBefore () ? "<" : "<=" ; // `endBefore()` versus `endAt()`
206
+
203
207
// TODO(indexing): To avoid reading the same documents multiple times, we should ideally only
204
208
// send one query that combines all clauses.
205
209
for (int i = 0 ; i < lowerBoundValues .size (); ++i ) {
206
210
db .query (
207
211
String .format (
208
212
"SELECT document_name from index_entries WHERE index_id = ? AND index_value %s ? AND index_value %s ?" ,
209
- lowerBound . isBefore () ? ">=" : ">" , upperBound . isBefore () ? "<=" : "<" ))
213
+ lowerBoundOp , upperBoundOp ))
210
214
.binding (fieldIndex .getIndexId (), lowerBoundValues .get (i ), upperBoundValues .get (i ))
211
215
.forEach (
212
216
row -> result .add (DocumentKey .fromPath (ResourcePath .fromString (row .getString (0 )))));
@@ -218,7 +222,7 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
218
222
db .query (
219
223
String .format (
220
224
"SELECT document_name from index_entries WHERE index_id = ? AND index_value %s ?" ,
221
- lowerBound . isBefore () ? ">=" : ">" ))
225
+ lowerBoundOp ))
222
226
.binding (fieldIndex .getIndexId (), lowerBoundValue )
223
227
.forEach (
224
228
row -> result .add (DocumentKey .fromPath (ResourcePath .fromString (row .getString (0 )))));
@@ -259,15 +263,14 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
259
263
throw fail ("Failed to decode index: " + e );
260
264
}
261
265
});
262
- ;
263
266
264
267
if (activeIndices .isEmpty ()) {
265
268
return null ;
266
269
}
267
270
268
271
// Return the index with the most number of segments
269
- Collections .sort ( activeIndices , ( l , r ) -> Integer . compare ( r . segmentCount (), l . segmentCount ()));
270
- return activeIndices . get ( 0 );
272
+ return Collections .max (
273
+ activeIndices , ( l , r ) -> Integer . compare ( l . segmentCount (), r . segmentCount ()) );
271
274
}
272
275
273
276
/**
0 commit comments