use of com.google.firebase.firestore.model.MutableDocument 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(), FieldMask.EMPTY, 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.MutableDocument in project firebase-android-sdk by firebase.
the class LocalDocumentsView method getDocument.
/**
* Returns the the local view of the document identified by {@code key}.
*
* @return Local view of the document or a an invalid document if we don't have any cached state
* for it.
*/
Document getDocument(DocumentKey key) {
Overlay overlay = documentOverlayCache.getOverlay(key);
MutableDocument document = getBaseDocument(key, overlay);
if (overlay != null) {
overlay.getMutation().applyToLocalView(document, FieldMask.EMPTY, Timestamp.now());
}
return document;
}
use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.
the class LocalDocumentsView method computeViews.
/**
* Computes the local view for the given documents.
*
* @param docs The documents to compute views for. It also has the base version of the documents.
* @param overlays The overlays that need to be applied to the given base version of the
* documents.
* @param existenceStateChanged A set of documents whose existence states might have changed. This
* is used to determine if we need to re-calculate overlays from mutation queues.
* @return A map represents the local documents view.
*/
private Map<DocumentKey, OverlayedDocument> computeViews(Map<DocumentKey, MutableDocument> docs, Map<DocumentKey, Overlay> overlays, Set<DocumentKey> existenceStateChanged) {
Map<DocumentKey, MutableDocument> recalculateDocuments = new HashMap<>();
Map<DocumentKey, FieldMask> mutatedFields = 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) {
mutatedFields.put(doc.getKey(), overlay.getMutation().getFieldMask());
overlay.getMutation().applyToLocalView(doc, overlay.getMutation().getFieldMask(), Timestamp.now());
}
}
Map<DocumentKey, FieldMask> recalculatedFields = recalculateAndSaveOverlays(recalculateDocuments);
mutatedFields.putAll(recalculatedFields);
Map<DocumentKey, OverlayedDocument> result = new HashMap<>();
for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
result.put(entry.getKey(), new OverlayedDocument(entry.getValue(), mutatedFields.get(entry.getKey())));
}
return result;
}
use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.
the class LocalSerializer method decodeDocument.
/**
* Decodes a Document proto to the equivalent model.
*/
private MutableDocument decodeDocument(com.google.firestore.v1.Document document, boolean hasCommittedMutations) {
DocumentKey key = rpcSerializer.decodeKey(document.getName());
SnapshotVersion version = rpcSerializer.decodeVersion(document.getUpdateTime());
MutableDocument result = MutableDocument.newFoundDocument(key, version, ObjectValue.fromMap(document.getFieldsMap()));
return hasCommittedMutations ? result.setHasCommittedMutations() : result;
}
use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.
the class QueryListenerTest method testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges.
@Test
public void testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges() {
List<ViewSnapshot> fullAccum = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros")).setHasLocalMutations();
MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades")).setHasLocalMutations();
MutableDocument doc1Prime = doc("rooms/eros", 1, map("name", "eros"));
MutableDocument doc2Prime = doc("rooms/hades", 2, map("name", "hades"));
MutableDocument doc3 = doc("rooms/other", 3, map("name", "other"));
ListenOptions options = new ListenOptions();
options.includeQueryMetadataChanges = true;
QueryListener fullListener = queryListener(query, options, fullAccum);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view, doc1, doc2);
ViewSnapshot snap2 = applyChanges(view, doc1Prime);
ViewSnapshot snap3 = applyChanges(view, doc3);
ViewSnapshot snap4 = applyChanges(view, doc2Prime);
fullListener.onViewSnapshot(snap1);
// Emits no events
fullListener.onViewSnapshot(snap2);
fullListener.onViewSnapshot(snap3);
// Metadata change event
fullListener.onViewSnapshot(snap4);
ViewSnapshot expectedSnapshot4 = new ViewSnapshot(snap4.getQuery(), snap4.getDocuments(), snap3.getDocuments(), asList(), snap4.isFromCache(), snap4.getMutatedKeys(), snap4.didSyncStateChange(), /* excludeMetadataChanges= */
true);
// This test excludes document metadata changes
assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE), expectedSnapshot4), fullAccum);
}
Aggregations