Search in sources :

Example 1 with IndexOffset

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

the class IndexBackfiller method writeEntriesForCollectionGroup.

/**
 * Writes entries for the provided collection group. Returns the number of documents processed.
 */
private int writeEntriesForCollectionGroup(String collectionGroup, int documentsRemainingUnderCap) {
    // Use the earliest offset of all field indexes to query the local cache.
    IndexOffset existingOffset = indexManager.getMinOffset(collectionGroup);
    LocalDocumentsResult nextBatch = localDocumentsView.getNextDocuments(collectionGroup, existingOffset, documentsRemainingUnderCap);
    indexManager.updateIndexEntries(nextBatch.getDocuments());
    IndexOffset newOffset = getNewOffset(existingOffset, nextBatch);
    Logger.debug(LOG_TAG, "Updating offset: %s", newOffset);
    indexManager.updateCollectionGroup(collectionGroup, newOffset);
    return nextBatch.getDocuments().size();
}
Also used : IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset)

Example 2 with IndexOffset

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

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

the class SQLiteIndexManagerTest method testPersistsIndexOffset.

@Test
public void testPersistsIndexOffset() {
    indexManager.addFieldIndex(fieldIndex("coll1", "value", Kind.ASCENDING));
    IndexOffset offset = IndexOffset.create(version(20), key("coll/doc"), 42);
    indexManager.updateCollectionGroup("coll1", offset);
    indexManager = persistence.getIndexManager(User.UNAUTHENTICATED);
    indexManager.start();
    Collection<FieldIndex> indexes = indexManager.getFieldIndexes("coll1");
    assertEquals(indexes.size(), 1);
    FieldIndex index = indexes.iterator().next();
    assertEquals(offset, index.getIndexState().getOffset());
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset) Test(org.junit.Test)

Example 4 with IndexOffset

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

the class CountingQueryEngine method wrapRemoteDocumentCache.

private RemoteDocumentCache wrapRemoteDocumentCache(RemoteDocumentCache subject) {
    return new RemoteDocumentCache() {

        @Override
        public void setIndexManager(IndexManager indexManager) {
        // Not implemented.
        }

        @Override
        public void add(MutableDocument document, SnapshotVersion readTime) {
            subject.add(document, readTime);
        }

        @Override
        public void removeAll(Collection<DocumentKey> keys) {
            subject.removeAll(keys);
        }

        @Override
        public MutableDocument get(DocumentKey documentKey) {
            MutableDocument result = subject.get(documentKey);
            documentsReadByKey[0] += result.isValidDocument() ? 1 : 0;
            return result;
        }

        @Override
        public Map<DocumentKey, MutableDocument> getAll(Iterable<DocumentKey> documentKeys) {
            Map<DocumentKey, MutableDocument> result = subject.getAll(documentKeys);
            for (MutableDocument document : result.values()) {
                documentsReadByKey[0] += document.isValidDocument() ? 1 : 0;
            }
            return result;
        }

        @Override
        public Map<DocumentKey, MutableDocument> getAll(String collectionGroup, IndexOffset offset, int limit) {
            Map<DocumentKey, MutableDocument> result = subject.getAll(collectionGroup, offset, limit);
            documentsReadByCollection[0] += result.size();
            return result;
        }

        @Override
        public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
            Map<DocumentKey, MutableDocument> result = subject.getAll(collection, offset);
            documentsReadByCollection[0] += result.size();
            return result;
        }
    };
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Collection(java.util.Collection) IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset)

Example 5 with IndexOffset

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

the class FieldIndexTest method indexOffsetComparator.

@Test
public void indexOffsetComparator() {
    IndexOffset docAOffset = IndexOffset.create(version(1), key("foo/a"), -1);
    IndexOffset docBOffset = IndexOffset.create(version(1), key("foo/b"), -1);
    IndexOffset version1Offset = IndexOffset.createSuccessor(version(1), -1);
    IndexOffset docCOffset = IndexOffset.create(version(2), key("foo/c"), -1);
    IndexOffset version2Offset = IndexOffset.createSuccessor(version(2), -1);
    assertEquals(-1, docAOffset.compareTo(docBOffset));
    assertEquals(-1, docAOffset.compareTo(version1Offset));
    assertEquals(-1, version1Offset.compareTo(docCOffset));
    assertEquals(-1, version1Offset.compareTo(version2Offset));
    assertEquals(-1, docCOffset.compareTo(version2Offset));
}
Also used : IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset) Test(org.junit.Test)

Aggregations

IndexOffset (com.google.firebase.firestore.model.FieldIndex.IndexOffset)6 Test (org.junit.Test)3 FieldIndex (com.google.firebase.firestore.model.FieldIndex)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1 Collection (java.util.Collection)1