Search in sources :

Example 1 with TargetIndexMatcher

use of com.google.firebase.firestore.model.TargetIndexMatcher in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method getFieldIndex.

@Nullable
@Override
public FieldIndex getFieldIndex(Target target) {
    hardAssert(started, "IndexManager not started");
    TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(target);
    String collectionGroup = target.getCollectionGroup() != null ? target.getCollectionGroup() : target.getPath().getLastSegment();
    Collection<FieldIndex> collectionIndexes = getFieldIndexes(collectionGroup);
    if (collectionIndexes.isEmpty()) {
        return null;
    }
    List<FieldIndex> matchingIndexes = new ArrayList<>();
    for (FieldIndex fieldIndex : collectionIndexes) {
        boolean matches = targetIndexMatcher.servedByIndex(fieldIndex);
        if (matches) {
            matchingIndexes.add(fieldIndex);
        }
    }
    if (matchingIndexes.isEmpty()) {
        return null;
    }
    // Return the index with the most number of segments
    return Collections.max(matchingIndexes, (l, r) -> Integer.compare(l.getSegments().size(), r.getSegments().size()));
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) ArrayList(java.util.ArrayList) TargetIndexMatcher(com.google.firebase.firestore.model.TargetIndexMatcher) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)1 FieldIndex (com.google.firebase.firestore.model.FieldIndex)1 TargetIndexMatcher (com.google.firebase.firestore.model.TargetIndexMatcher)1 ArrayList (java.util.ArrayList)1