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(ResourcePath collection, int sinceBatchId) {
Map<DocumentKey, Overlay> result = new HashMap<>();
int immediateChildrenPathLength = collection.length() + 1;
DocumentKey prefix = DocumentKey.fromPath(collection.append(""));
Map<DocumentKey, Overlay> view = overlays.tailMap(prefix);
for (Overlay overlay : view.values()) {
DocumentKey key = overlay.getKey();
if (!collection.isPrefixOf(key.getPath())) {
break;
}
// Documents from sub-collections
if (key.getPath().length() != immediateChildrenPathLength) {
continue;
}
if (overlay.getLargestBatchId() > sinceBatchId) {
result.put(overlay.getKey(), overlay);
}
}
return result;
}
use of com.google.firebase.firestore.model.mutation.Overlay in project firebase-android-sdk by firebase.
the class LocalDocumentsView method computeViews.
/*Computes the local view for doc */
private ImmutableSortedMap<DocumentKey, Document> computeViews(Map<DocumentKey, MutableDocument> docs, Map<DocumentKey, Overlay> overlays, Set<DocumentKey> existenceStateChanged) {
ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
Map<DocumentKey, MutableDocument> recalculateDocuments = new HashMap<>();
for (MutableDocument doc : docs.values()) {
Overlay overlay = overlays.get(doc.getKey());
// but would now match.
if (existenceStateChanged.contains(doc.getKey()) && (overlay == null || overlay.getMutation() instanceof PatchMutation)) {
recalculateDocuments.put(doc.getKey(), doc);
} else if (overlay != null) {
overlay.getMutation().applyToLocalView(doc, null, Timestamp.now());
}
}
recalculateAndSaveOverlays(recalculateDocuments);
for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
results = results.insert(entry.getKey(), entry.getValue());
}
return results;
}
use of com.google.firebase.firestore.model.mutation.Overlay in project firebase-android-sdk by firebase.
the class LocalDocumentsView method getDocumentsMatchingCollectionQuery.
private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionQuery(Query query, IndexOffset offset) {
Map<DocumentKey, MutableDocument> remoteDocuments = remoteDocumentCache.getAll(query.getPath(), offset);
Map<DocumentKey, Overlay> overlays = documentOverlayCache.getOverlays(query.getPath(), offset.getLargestBatchId());
// for all overlays in the initial document set.
for (Map.Entry<DocumentKey, Overlay> entry : overlays.entrySet()) {
if (!remoteDocuments.containsKey(entry.getKey())) {
remoteDocuments.put(entry.getKey(), MutableDocument.newInvalidDocument(entry.getKey()));
}
}
// Apply the overlays and match against the query.
ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
for (Map.Entry<DocumentKey, MutableDocument> docEntry : remoteDocuments.entrySet()) {
Overlay overlay = overlays.get(docEntry.getKey());
if (overlay != null) {
overlay.getMutation().applyToLocalView(docEntry.getValue(), null, Timestamp.now());
}
// Finally, insert the documents that still match the query
if (query.matches(docEntry.getValue())) {
results = results.insert(docEntry.getKey(), docEntry.getValue());
}
}
return results;
}
use of com.google.firebase.firestore.model.mutation.Overlay in project firebase-android-sdk by firebase.
the class DocumentOverlayCacheTestCase method testGetAllOverlaysForCollection.
@Test
public void testGetAllOverlaysForCollection() {
Mutation m1 = patchMutation("coll/doc1", map("foo", "bar"));
Mutation m2 = setMutation("coll/doc2", map("foo", "bar"));
Mutation m3 = deleteMutation("coll/doc3");
// m4 and m5 are not under "coll"
Mutation m4 = setMutation("coll/doc1/sub/sub_doc", map("foo", "bar"));
Mutation m5 = setMutation("other/doc1", map("foo", "bar"));
saveOverlays(3, m1, m2, m3, m4, m5);
Map<DocumentKey, Overlay> overlays = cache.getOverlays(path("coll"), -1);
verifyOverlayContains(overlays, "coll/doc1", "coll/doc2", "coll/doc3");
}
Aggregations