Search in sources :

Example 1 with FieldIndex

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

the class SQLiteIndexManager method updateCollectionGroup.

@Override
public void updateCollectionGroup(String collectionGroup, IndexOffset offset) {
    hardAssert(started, "IndexManager not started");
    ++memoizedMaxSequenceNumber;
    for (FieldIndex fieldIndex : getFieldIndexes(collectionGroup)) {
        FieldIndex updatedIndex = FieldIndex.create(fieldIndex.getIndexId(), fieldIndex.getCollectionGroup(), fieldIndex.getSegments(), FieldIndex.IndexState.create(memoizedMaxSequenceNumber, offset));
        db.execute("REPLACE INTO index_state (index_id, uid,  sequence_number, " + "read_time_seconds, read_time_nanos, document_key, largest_batch_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)", fieldIndex.getIndexId(), uid, memoizedMaxSequenceNumber, offset.getReadTime().getTimestamp().getSeconds(), offset.getReadTime().getTimestamp().getNanoseconds(), EncodedPath.encode(offset.getDocumentKey().getPath()), offset.getLargestBatchId());
        memoizeIndex(updatedIndex);
    }
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex)

Example 2 with FieldIndex

use of com.google.firebase.firestore.model.FieldIndex 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)

Example 3 with FieldIndex

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

the class SQLiteIndexManager method getMinOffset.

private IndexOffset getMinOffset(Collection<FieldIndex> fieldIndexes) {
    hardAssert(!fieldIndexes.isEmpty(), "Found empty index group when looking for least recent index offset.");
    Iterator<FieldIndex> it = fieldIndexes.iterator();
    IndexOffset minOffset = it.next().getIndexState().getOffset();
    int minBatchId = minOffset.getLargestBatchId();
    while (it.hasNext()) {
        IndexOffset newOffset = it.next().getIndexState().getOffset();
        if (newOffset.compareTo(minOffset) < 0) {
            minOffset = newOffset;
        }
        minBatchId = Math.max(newOffset.getLargestBatchId(), minBatchId);
    }
    return IndexOffset.create(minOffset.getReadTime(), minOffset.getDocumentKey(), minBatchId);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset)

Example 4 with FieldIndex

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

the class SQLiteIndexManager method getNextCollectionGroupToUpdate.

@Override
@Nullable
public String getNextCollectionGroupToUpdate() {
    hardAssert(started, "IndexManager not started");
    FieldIndex nextIndex = nextIndexToUpdate.peek();
    return nextIndex != null ? nextIndex.getCollectionGroup() : null;
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Nullable(androidx.annotation.Nullable)

Example 5 with FieldIndex

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

the class SQLiteIndexManager method encodeValues.

/**
 * Encodes the given field values according to the specification in {@code target}. For IN
 * queries, a list of possible values is returned.
 */
@Nullable
private Object[] encodeValues(FieldIndex fieldIndex, Target target, @Nullable Collection<Value> values) {
    if (values == null)
        return null;
    List<IndexByteEncoder> encoders = new ArrayList<>();
    encoders.add(new IndexByteEncoder());
    Iterator<Value> position = values.iterator();
    for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
        Value value = position.next();
        for (IndexByteEncoder encoder : encoders) {
            if (isInFilter(target, segment.getFieldPath()) && isArray(value)) {
                encoders = expandIndexValues(encoders, segment, value);
            } else {
                DirectionalIndexByteEncoder directionalEncoder = encoder.forKind(segment.getKind());
                FirestoreIndexValueWriter.INSTANCE.writeIndexValue(value, directionalEncoder);
            }
        }
    }
    return getEncodedBytes(encoders);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) ArrayList(java.util.ArrayList) Value(com.google.firestore.v1.Value) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) IndexByteEncoder(com.google.firebase.firestore.index.IndexByteEncoder) Nullable(androidx.annotation.Nullable)

Aggregations

FieldIndex (com.google.firebase.firestore.model.FieldIndex)49 Test (org.junit.Test)32 Nullable (androidx.annotation.Nullable)8 Value (com.google.firestore.v1.Value)8 Query (com.google.firebase.firestore.core.Query)6 ArrayList (java.util.ArrayList)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)3 DirectionalIndexByteEncoder (com.google.firebase.firestore.index.DirectionalIndexByteEncoder)2 IndexByteEncoder (com.google.firebase.firestore.index.IndexByteEncoder)2 IndexEntry (com.google.firebase.firestore.index.IndexEntry)2 IndexOffset (com.google.firebase.firestore.model.FieldIndex.IndexOffset)2 HashMap (java.util.HashMap)2 VisibleForTesting (androidx.annotation.VisibleForTesting)1 Timestamp (com.google.firebase.Timestamp)1 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)1 Bound (com.google.firebase.firestore.core.Bound)1 Target (com.google.firebase.firestore.core.Target)1 Document (com.google.firebase.firestore.model.Document)1 FieldPath (com.google.firebase.firestore.model.FieldPath)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1