use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class View method applyChanges.
/**
* Updates the view with the given ViewDocumentChanges and updates limbo docs and sync state from
* the given (optional) target change.
*
* @param docChanges The set of changes to make to the view's docs.
* @param targetChange A target change to apply for computing limbo docs and sync state.
* @return A new ViewChange with the given docs, changes, and sync state.
*/
public ViewChange applyChanges(DocumentChanges docChanges, TargetChange targetChange) {
hardAssert(!docChanges.needsRefill, "Cannot apply changes that need a refill");
DocumentSet oldDocumentSet = documentSet;
documentSet = docChanges.documentSet;
mutatedKeys = docChanges.mutatedKeys;
// Sort changes based on type and query comparator.
List<DocumentViewChange> viewChanges = docChanges.changeSet.getChanges();
Collections.sort(viewChanges, (DocumentViewChange o1, DocumentViewChange o2) -> {
int typeComp = compareIntegers(View.changeTypeOrder(o1), View.changeTypeOrder(o2));
o1.getType().compareTo(o2.getType());
if (typeComp != 0) {
return typeComp;
}
return query.comparator().compare(o1.getDocument(), o2.getDocument());
});
applyTargetChange(targetChange);
List<LimboDocumentChange> limboDocumentChanges = updateLimboDocuments();
boolean synced = limboDocuments.size() == 0 && current;
SyncState newSyncState = synced ? SyncState.SYNCED : SyncState.LOCAL;
boolean syncStatedChanged = newSyncState != syncState;
syncState = newSyncState;
ViewSnapshot snapshot = null;
if (viewChanges.size() != 0 || syncStatedChanged) {
boolean fromCache = newSyncState == SyncState.LOCAL;
snapshot = new ViewSnapshot(query, docChanges.documentSet, oldDocumentSet, viewChanges, fromCache, docChanges.mutatedKeys, syncStatedChanged, /* excludesMetadataChanges= */
false);
}
return new ViewChange(snapshot, limboDocumentChanges);
}
use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class ViewSnapshotTest method testConstructor.
@Test
public void testConstructor() {
Query query = Query.atPath(ResourcePath.fromString("a"));
DocumentSet docs = DocumentSet.emptySet(Document.KEY_COMPARATOR).add(doc("c/foo", 1, map()));
DocumentSet oldDocs = DocumentSet.emptySet(Document.KEY_COMPARATOR);
List<DocumentViewChange> changes = Arrays.asList(DocumentViewChange.create(Type.ADDED, doc("c/foo", 1, map())));
ImmutableSortedSet<DocumentKey> mutatedKeys = keySet(key("c/foo"));
boolean fromCache = true;
boolean hasPendingWrites = true;
boolean syncStateChanges = true;
boolean excludesMetadataChanges = true;
ViewSnapshot snapshot = new ViewSnapshot(query, docs, oldDocs, changes, fromCache, mutatedKeys, syncStateChanges, excludesMetadataChanges);
assertEquals(query, snapshot.getQuery());
assertEquals(docs, snapshot.getDocuments());
assertEquals(oldDocs, snapshot.getOldDocuments());
assertEquals(changes, snapshot.getChanges());
assertEquals(fromCache, snapshot.isFromCache());
assertEquals(mutatedKeys, snapshot.getMutatedKeys());
assertEquals(hasPendingWrites, snapshot.hasPendingWrites());
assertEquals(syncStateChanges, snapshot.didSyncStateChange());
assertEquals(excludesMetadataChanges, snapshot.excludesMetadataChanges());
}
use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class QuerySnapshotTest method testIncludeMetadataChanges.
@Test
public void testIncludeMetadataChanges() {
MutableDocument doc1Old = doc("foo/bar", 1, wrapObject("a", "b")).setHasLocalMutations();
MutableDocument doc1New = doc("foo/bar", 1, wrapObject("a", "b"));
MutableDocument doc2Old = doc("foo/baz", 1, wrapObject("a", "b"));
MutableDocument doc2New = doc("foo/baz", 1, wrapObject("a", "c"));
DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR, doc1Old, doc2Old);
DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR, doc1New, doc2New);
List<DocumentViewChange> documentChanges = Arrays.asList(DocumentViewChange.create(DocumentViewChange.Type.METADATA, doc1New), DocumentViewChange.create(DocumentViewChange.Type.MODIFIED, doc2New));
FirebaseFirestore firestore = TestUtil.firestore();
com.google.firebase.firestore.core.Query fooQuery = query("foo");
ViewSnapshot viewSnapshot = new ViewSnapshot(fooQuery, newDocuments, oldDocuments, documentChanges, /*isFromCache=*/
false, /*mutatedKeys=*/
keySet(), /*didSyncStateChange=*/
true, /* excludesMetadataChanges= */
false);
QuerySnapshot snapshot = new QuerySnapshot(new Query(fooQuery, firestore), viewSnapshot, firestore);
QueryDocumentSnapshot doc1Snap = QueryDocumentSnapshot.fromDocument(firestore, doc1New, /*fromCache=*/
false, /*hasPendingWrites=*/
false);
QueryDocumentSnapshot doc2Snap = QueryDocumentSnapshot.fromDocument(firestore, doc2New, /*fromCache=*/
false, /*hasPendingWrites=*/
false);
assertEquals(1, snapshot.getDocumentChanges().size());
List<DocumentChange> changesWithoutMetadata = Arrays.asList(new DocumentChange(doc2Snap, DocumentChange.Type.MODIFIED, /*oldIndex=*/
1, /*newIndex=*/
1));
assertEquals(changesWithoutMetadata, snapshot.getDocumentChanges());
List<DocumentChange> changesWithMetadata = Arrays.asList(new DocumentChange(doc1Snap, DocumentChange.Type.MODIFIED, /*oldIndex=*/
0, /*newIndex=*/
0), new DocumentChange(doc2Snap, DocumentChange.Type.MODIFIED, /*oldIndex=*/
1, /*newIndex=*/
1));
assertEquals(changesWithMetadata, snapshot.getDocumentChanges(MetadataChanges.INCLUDE));
}
use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class TestUtil method querySnapshot.
/**
* A convenience method for creating a particular query snapshot for tests.
*
* @param path To be used in constructing the query.
* @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each entry maps to a
* document, with the key being the document id, and the value being the document contents.
* @param docsToAdd Specifies data to be added into the query snapshot as of now. Each entry maps
* to a document, with the key being the document id, and the value being the document
* contents.
* @param isFromCache Whether the query snapshot is cache result.
* @return A query snapshot that consists of both sets of documents.
*/
public static QuerySnapshot querySnapshot(String path, Map<String, ObjectValue> oldDocs, Map<String, ObjectValue> docsToAdd, boolean hasPendingWrites, boolean isFromCache) {
DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR);
ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
String docKey = path + "/" + pair.getKey();
MutableDocument doc = doc(docKey, 1L, pair.getValue());
if (hasPendingWrites) {
doc.setHasCommittedMutations();
mutatedKeys = mutatedKeys.insert(key(docKey));
}
oldDocuments = oldDocuments.add(doc);
}
DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR);
List<DocumentViewChange> documentChanges = new ArrayList<>();
for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
String docKey = path + "/" + pair.getKey();
MutableDocument docToAdd = doc(docKey, 1L, pair.getValue());
if (hasPendingWrites) {
docToAdd.setHasCommittedMutations();
mutatedKeys = mutatedKeys.insert(key(docKey));
}
newDocuments = newDocuments.add(docToAdd);
documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));
}
ViewSnapshot viewSnapshot = new ViewSnapshot(com.google.firebase.firestore.testutil.TestUtil.query(path), newDocuments, oldDocuments, documentChanges, isFromCache, mutatedKeys, /* didSyncStateChange= */
true, /* excludesMetadataChanges= */
false);
return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);
}
use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class QueryEngineTestCase method doesNotUseInitialResultsForLimitToLastQueryWhenLastDocumentHasPendingWrite.
@Test
public void doesNotUseInitialResultsForLimitToLastQueryWhenLastDocumentHasPendingWrite() throws Exception {
Query query = query("coll").filter(filter("matches", "==", true)).orderBy(orderBy("order", "asc")).limitToLast(1);
// Add a query mapping for a document that matches, but that sorts below another document due to
// a pending write.
addDocumentWithEventVersion(version(1), MATCHING_DOC_A);
addMutation(DOC_A_EMPTY_PATCH);
persistQueryMapping(MATCHING_DOC_A.getKey());
addDocument(MATCHING_DOC_B);
DocumentSet docs = expectFullCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
assertEquals(docSet(query.comparator(), MATCHING_DOC_B), docs);
}
Aggregations