23
23
import static java .lang .Math .max ;
24
24
25
25
import android .text .TextUtils ;
26
- import android .util .Pair ;
27
26
import androidx .annotation .Nullable ;
28
27
import com .google .firebase .Timestamp ;
29
28
import com .google .firebase .database .collection .ImmutableSortedMap ;
@@ -315,13 +314,12 @@ public IndexOffset getMinOffset(String collectionGroup) {
315
314
@ Override
316
315
public IndexType getIndexType (Target target ) {
317
316
for (Target subTarget : getSubTargets (target )) {
318
- Pair < FieldIndex , Integer > indexAndSegmentCount = getFieldIndexAndSegmentCount (subTarget );
319
- if (indexAndSegmentCount == null ) {
317
+ FieldIndex index = getFieldIndex (subTarget );
318
+ if (index == null ) {
320
319
return IndexType .NONE ;
321
320
}
322
321
323
- Integer indexSegmentCount = indexAndSegmentCount .second ;
324
- if (indexSegmentCount < subTarget .getSegmentCount ()) {
322
+ if (index .getSegments ().size () < subTarget .getSegmentCount ()) {
325
323
return IndexType .PARTIAL ;
326
324
}
327
325
}
@@ -332,9 +330,9 @@ public IndexType getIndexType(Target target) {
332
330
public IndexOffset getMinOffset (Target target ) {
333
331
List <FieldIndex > fieldIndexes = new ArrayList <>();
334
332
for (Target subTarget : getSubTargets (target )) {
335
- Pair < FieldIndex , Integer > indexAndSegmentCount = getFieldIndexAndSegmentCount (subTarget );
336
- if (indexAndSegmentCount != null ) {
337
- fieldIndexes .add (indexAndSegmentCount . first );
333
+ FieldIndex index = getFieldIndex (subTarget );
334
+ if (index != null ) {
335
+ fieldIndexes .add (index );
338
336
}
339
337
}
340
338
return getMinOffset (fieldIndexes );
@@ -468,12 +466,11 @@ public List<DocumentKey> getDocumentsMatchingTarget(Target target) {
468
466
List <Object > bindings = new ArrayList <>();
469
467
470
468
for (Target subTarget : getSubTargets (target )) {
471
- Pair < FieldIndex , Integer > indexAndSegmentCount = getFieldIndexAndSegmentCount (subTarget );
472
- if (indexAndSegmentCount == null ) {
469
+ FieldIndex fieldIndex = getFieldIndex (subTarget );
470
+ if (fieldIndex == null ) {
473
471
return null ;
474
472
}
475
473
476
- FieldIndex fieldIndex = indexAndSegmentCount .first ;
477
474
@ Nullable List <Value > arrayValues = subTarget .getArrayValues (fieldIndex );
478
475
@ Nullable Collection <Value > notInValues = subTarget .getNotInValues (fieldIndex );
479
476
@ Nullable Bound lowerBound = subTarget .getLowerBound (fieldIndex );
@@ -627,12 +624,11 @@ private Object[] fillBounds(
627
624
}
628
625
629
626
/**
630
- * Returns an index that can be used to serve the provided target, as well as the number of index
631
- * segments that matched the target filters and orderBys. Returns {@code null} if no index is
632
- * configured.
627
+ * Returns an index that can be used to serve the provided target. Returns {@code null} if no
628
+ * index is configured.
633
629
*/
634
630
@ Nullable
635
- public Pair < FieldIndex , Integer > getFieldIndexAndSegmentCount (Target target ) {
631
+ private FieldIndex getFieldIndex (Target target ) {
636
632
hardAssert (started , "IndexManager not started" );
637
633
638
634
TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher (target );
@@ -647,19 +643,16 @@ public Pair<FieldIndex, Integer> getFieldIndexAndSegmentCount(Target target) {
647
643
}
648
644
649
645
// Return the index with the most number of segments.
650
- int maxNumMatchingSegments = -1 ;
651
646
FieldIndex matchingIndex = null ;
652
647
for (FieldIndex fieldIndex : collectionIndexes ) {
653
- int numMatchingSegments = targetIndexMatcher .servedByIndex (fieldIndex );
654
- if (numMatchingSegments > maxNumMatchingSegments ) {
655
- maxNumMatchingSegments = numMatchingSegments ;
648
+ boolean matches = targetIndexMatcher .servedByIndex (fieldIndex );
649
+ if (matches
650
+ && (matchingIndex == null
651
+ || fieldIndex .getSegments ().size () > matchingIndex .getSegments ().size ())) {
656
652
matchingIndex = fieldIndex ;
657
653
}
658
654
}
659
- if (maxNumMatchingSegments == -1 ) {
660
- return null ;
661
- }
662
- return new Pair <>(matchingIndex , maxNumMatchingSegments );
655
+ return matchingIndex ;
663
656
}
664
657
665
658
/**
0 commit comments