use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class QueryEngineTestCase method doesNotUseInitialResultsForLimitQueryWhenLastDocumentHasPendingWrite.
@Test
public void doesNotUseInitialResultsForLimitQueryWhenLastDocumentHasPendingWrite() throws Exception {
Query query = query("coll").filter(filter("matches", "==", true)).orderBy(orderBy("order", "desc")).limitToFirst(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);
}
use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class QueryEngineTestCase method includesChangesSinceInitialResults.
@Test
public void includesChangesSinceInitialResults() throws Exception {
Query query = query("coll").filter(filter("matches", "==", true));
addDocument(MATCHING_DOC_A, MATCHING_DOC_B);
persistQueryMapping(MATCHING_DOC_A.getKey(), MATCHING_DOC_B.getKey());
DocumentSet docs = expectOptimizedCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
assertEquals(docSet(query.comparator(), MATCHING_DOC_A, MATCHING_DOC_B), docs);
addDocument(UPDATED_MATCHING_DOC_B);
docs = expectOptimizedCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
assertEquals(docSet(query.comparator(), MATCHING_DOC_A, UPDATED_MATCHING_DOC_B), docs);
}
use of com.google.firebase.firestore.model.DocumentSet in project firebase-android-sdk by firebase.
the class DocumentChange method changesFromSnapshot.
/**
* Creates the list of document changes from a {@code ViewSnapshot}.
*/
static List<DocumentChange> changesFromSnapshot(FirebaseFirestore firestore, MetadataChanges metadataChanges, ViewSnapshot snapshot) {
List<DocumentChange> documentChanges = new ArrayList<>();
if (snapshot.getOldDocuments().isEmpty()) {
// Special case the first snapshot because index calculation is easy and fast. Also all
// changes on the first snapshot are adds so there are also no metadata-only changes to filter
// out.
int index = 0;
Document lastDoc = null;
for (DocumentViewChange change : snapshot.getChanges()) {
Document document = change.getDocument();
QueryDocumentSnapshot documentSnapshot = QueryDocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), snapshot.getMutatedKeys().contains(document.getKey()));
hardAssert(change.getType() == DocumentViewChange.Type.ADDED, "Invalid added event for first snapshot");
hardAssert(lastDoc == null || snapshot.getQuery().comparator().compare(lastDoc, document) < 0, "Got added events in wrong order");
documentChanges.add(new DocumentChange(documentSnapshot, Type.ADDED, -1, index++));
lastDoc = document;
}
} else {
// A DocumentSet that is updated incrementally as changes are applied to use to lookup the
// index of a document.
DocumentSet indexTracker = snapshot.getOldDocuments();
for (DocumentViewChange change : snapshot.getChanges()) {
if (metadataChanges == MetadataChanges.EXCLUDE && change.getType() == DocumentViewChange.Type.METADATA) {
continue;
}
Document document = change.getDocument();
QueryDocumentSnapshot documentSnapshot = QueryDocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), snapshot.getMutatedKeys().contains(document.getKey()));
int oldIndex, newIndex;
Type type = getType(change);
if (type != Type.ADDED) {
oldIndex = indexTracker.indexOf(document.getKey());
hardAssert(oldIndex >= 0, "Index for document not found");
indexTracker = indexTracker.remove(document.getKey());
} else {
oldIndex = -1;
}
if (type != Type.REMOVED) {
indexTracker = indexTracker.add(document);
newIndex = indexTracker.indexOf(document.getKey());
hardAssert(newIndex >= 0, "Index for document not found");
} else {
newIndex = -1;
}
documentChanges.add(new DocumentChange(documentSnapshot, type, oldIndex, newIndex));
}
}
return documentChanges;
}
Aggregations