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);
}
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());
}
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;
}
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")));
}
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());
}
Aggregations