Search in sources :

Example 1 with Overlay

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

the class LocalDocumentsView method getNextDocuments.

/**
 * Given a collection group, returns the next documents that follow the provided offset, along
 * with an updated batch ID.
 *
 * <p>The documents returned by this method are ordered by remote version from the provided
 * offset. If there are no more remote documents after the provided offset, documents with
 * mutations in order of batch id from the offset are returned. Since all documents in a batch are
 * returned together, the total number of documents returned can exceed {@code count}.
 *
 * @param collectionGroup The collection group for the documents.
 * @param offset The offset to index into.
 * @param count The number of documents to return
 * @return A LocalDocumentsResult with the documents that follow the provided offset and the last
 *     processed batch id.
 */
LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset, int count) {
    Map<DocumentKey, MutableDocument> docs = remoteDocumentCache.getAll(collectionGroup, offset, count);
    Map<DocumentKey, Overlay> overlays = count - docs.size() > 0 ? documentOverlayCache.getOverlays(collectionGroup, offset.getLargestBatchId(), count - docs.size()) : Collections.emptyMap();
    int largestBatchId = FieldIndex.INITIAL_LARGEST_BATCH_ID;
    for (Overlay overlay : overlays.values()) {
        if (!docs.containsKey(overlay.getKey())) {
            docs.put(overlay.getKey(), getBaseDocument(overlay.getKey(), overlay));
        }
        // The callsite will use the largest batch ID together with the latest read time to create
        // a new index offset. Since we only process batch IDs if all remote documents have been read,
        // no overlay will increase the overall read time. This is why we only need to special case
        // the batch id.
        largestBatchId = Math.max(largestBatchId, overlay.getLargestBatchId());
    }
    populateOverlays(overlays, docs.keySet());
    ImmutableSortedMap<DocumentKey, Document> localDocs = computeViews(docs, overlays, Collections.emptySet());
    return new LocalDocumentsResult(largestBatchId, localDocs);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Overlay(com.google.firebase.firestore.model.mutation.Overlay) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 2 with Overlay

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

the class MemoryDocumentOverlayCache method saveOverlay.

private void saveOverlay(int largestBatchId, @Nullable Mutation mutation) {
    if (mutation == null) {
        return;
    }
    // Remove the association of the overlay to its batch id.
    Overlay existing = this.overlays.get(mutation.getKey());
    if (existing != null) {
        overlayByBatchId.get(existing.getLargestBatchId()).remove(mutation.getKey());
    }
    overlays.put(mutation.getKey(), Overlay.create(largestBatchId, mutation));
    // Create the associate of this overlay to the given largestBatchId.
    if (overlayByBatchId.get(largestBatchId) == null) {
        overlayByBatchId.put(largestBatchId, new HashSet<>());
    }
    overlayByBatchId.get(largestBatchId).add(mutation.getKey());
}
Also used : Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 3 with Overlay

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

the class MemoryDocumentOverlayCache method getOverlays.

@Override
public Map<DocumentKey, Overlay> getOverlays(String collectionGroup, int sinceBatchId, int count) {
    // NOTE: This method is only used by the backfiller, which will not run for memory persistence;
    // therefore, this method is being implemented only so that the test suite for
    // `LevelDbDocumentOverlayCache` can be re-used by the test suite for this class.
    SortedMap<Integer, Map<DocumentKey, Overlay>> batchIdToOverlays = new TreeMap<>();
    for (Overlay overlay : overlays.values()) {
        DocumentKey key = overlay.getKey();
        if (!key.getCollectionGroup().equals(collectionGroup)) {
            continue;
        }
        if (overlay.getLargestBatchId() > sinceBatchId) {
            Map<DocumentKey, Overlay> overlays = batchIdToOverlays.get(overlay.getLargestBatchId());
            if (overlays == null) {
                overlays = new HashMap<>();
                batchIdToOverlays.put(overlay.getLargestBatchId(), overlays);
            }
            overlays.put(overlay.getKey(), overlay);
        }
    }
    Map<DocumentKey, Overlay> result = new HashMap<>();
    for (Map<DocumentKey, Overlay> overlays : batchIdToOverlays.values()) {
        result.putAll(overlays);
        if (result.size() >= count) {
            break;
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) TreeMap(java.util.TreeMap) Overlay(com.google.firebase.firestore.model.mutation.Overlay) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) SortedMap(java.util.SortedMap)

Example 4 with Overlay

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

the class DocumentOverlayCacheTestCase method testUpdateDocumentOverlay.

@Test
public void testUpdateDocumentOverlay() {
    Mutation mutation1 = setMutation("coll/doc", map("foo", 1));
    Mutation mutation2 = setMutation("coll/doc", map("foo", 2));
    saveOverlays(1, mutation1);
    saveOverlays(2, mutation2);
    // Verify that `getOverlay()` returns the updated mutation.
    Overlay overlay = cache.getOverlay(DocumentKey.fromPathString("coll/doc"));
    assertEquals(overlay, Overlay.create(2, mutation2));
    // Verify that `removeOverlaysForBatchId()` removes the overlay completely.
    cache.removeOverlaysForBatchId(2);
    assertNull(cache.getOverlay(DocumentKey.fromPathString("coll/doc")));
}
Also used : Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Overlay(com.google.firebase.firestore.model.mutation.Overlay) Test(org.junit.Test)

Example 5 with Overlay

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

the class DocumentOverlayCacheTestCase method testCanReadSavedOverlaysInBatches.

@Test
public void testCanReadSavedOverlaysInBatches() {
    Mutation m1 = setMutation("coll1/a", map("a", 1));
    Mutation m2 = setMutation("coll1/b", map("b", 2));
    Mutation m3 = setMutation("coll2/c", map("c", 3));
    saveOverlays(3, m1, m2, m3);
    Map<DocumentKey, Overlay> overlays = cache.getOverlays(new TreeSet<>(Arrays.asList(key("coll1/a"), key("coll1/b"), key("coll2/c"))));
    assertEquals(m1, overlays.get(key("coll1/a")).getMutation());
    assertEquals(m2, overlays.get(key("coll1/b")).getMutation());
    assertEquals(m3, overlays.get(key("coll2/c")).getMutation());
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Overlay(com.google.firebase.firestore.model.mutation.Overlay) Test(org.junit.Test)

Aggregations

Overlay (com.google.firebase.firestore.model.mutation.Overlay)14 DocumentKey (com.google.firebase.firestore.model.DocumentKey)10 HashMap (java.util.HashMap)7 MutableDocument (com.google.firebase.firestore.model.MutableDocument)4 Document (com.google.firebase.firestore.model.Document)3 Mutation (com.google.firebase.firestore.model.mutation.Mutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)3 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Test (org.junit.Test)3 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 Executor (java.util.concurrent.Executor)1