Search in sources :

Example 41 with FieldIndex

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

the class SQLiteIndexManager method encodeDirectionalElements.

/**
 * Returns the byte encoded form of the directional values in the field index. Returns {@code
 * null} if the document does not have all fields specified in the index.
 */
@Nullable
private byte[] encodeDirectionalElements(FieldIndex fieldIndex, Document document) {
    IndexByteEncoder encoder = new IndexByteEncoder();
    for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
        Value field = document.getField(segment.getFieldPath());
        if (field == null) {
            return null;
        }
        DirectionalIndexByteEncoder directionalEncoder = encoder.forKind(segment.getKind());
        FirestoreIndexValueWriter.INSTANCE.writeIndexValue(field, directionalEncoder);
    }
    return encoder.getEncodedBytes();
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) Value(com.google.firestore.v1.Value) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) IndexByteEncoder(com.google.firebase.firestore.index.IndexByteEncoder) Nullable(androidx.annotation.Nullable)

Example 42 with FieldIndex

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

the class SQLiteIndexManager method updateIndexEntries.

@Override
public void updateIndexEntries(ImmutableSortedMap<DocumentKey, Document> documents) {
    hardAssert(started, "IndexManager not started");
    for (Map.Entry<DocumentKey, Document> entry : documents) {
        Collection<FieldIndex> fieldIndexes = getFieldIndexes(entry.getKey().getCollectionGroup());
        for (FieldIndex fieldIndex : fieldIndexes) {
            SortedSet<IndexEntry> existingEntries = getExistingIndexEntries(entry.getKey(), fieldIndex);
            SortedSet<IndexEntry> newEntries = computeIndexEntries(entry.getValue(), fieldIndex);
            if (!existingEntries.equals(newEntries)) {
                updateEntries(entry.getValue(), existingEntries, newEntries);
            }
        }
    }
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) DocumentKey(com.google.firebase.firestore.model.DocumentKey) IndexEntry(com.google.firebase.firestore.index.IndexEntry) Document(com.google.firebase.firestore.model.Document) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap)

Example 43 with FieldIndex

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

the class IndexBackfillerTest method testBackfillMutationsUpToDocumentLimitAndUpdatesBatchIdOnIndex.

@Test
public void testBackfillMutationsUpToDocumentLimitAndUpdatesBatchIdOnIndex() {
    backfiller.setMaxDocumentsToProcess(2);
    addFieldIndex("coll1", "foo");
    addDoc("coll1/docA", version(10), "foo", 1);
    addSetMutationsToOverlay(2, "coll1/docB");
    addSetMutationsToOverlay(3, "coll1/docC");
    addSetMutationsToOverlay(4, "coll1/docD");
    int documentsProcessed = backfiller.backfill();
    assertEquals(2, documentsProcessed);
    verifyQueryResults("coll1", "coll1/docA", "coll1/docB");
    FieldIndex fieldIndex = indexManager.getFieldIndexes("coll1").iterator().next();
    assertEquals(2, fieldIndex.getIndexState().getOffset().getLargestBatchId());
    documentsProcessed = backfiller.backfill();
    assertEquals(2, documentsProcessed);
    verifyQueryResults("coll1", "coll1/docA", "coll1/docB", "coll1/docC", "coll1/docD");
    fieldIndex = indexManager.getFieldIndexes("coll1").iterator().next();
    assertEquals(4, fieldIndex.getIndexState().getOffset().getLargestBatchId());
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Test(org.junit.Test)

Example 44 with FieldIndex

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

the class IndexBackfillerTest method addFieldIndex.

private void addFieldIndex(String collectionGroup, String fieldName) {
    FieldIndex fieldIndex = fieldIndex(collectionGroup, fieldName, FieldIndex.Segment.Kind.ASCENDING);
    indexManager.addFieldIndex(fieldIndex);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex)

Example 45 with FieldIndex

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

the class TargetTest method partialIndexMatchQueryBound.

@Test
public void partialIndexMatchQueryBound() {
    Target target = query("c").filter(filter("a", "==", "a")).filter(filter("b", "==", "b")).toTarget();
    FieldIndex index = fieldIndex("c", "a", FieldIndex.Segment.Kind.ASCENDING);
    Bound lowerBound = target.getLowerBound(index);
    verifyBound(lowerBound, true, "a");
    Bound upperBound = target.getUpperBound(index);
    verifyBound(upperBound, true, "a");
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Test(org.junit.Test)

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