use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.
the class ViewTest method testAddsDocumentsBasedOnQuery.
@Test
public void testAddsDocumentsBasedOnQuery() {
Query query = messageQuery();
View view = new View(query, DocumentKey.emptyKeySet());
MutableDocument doc1 = doc("rooms/eros/messages/1", 0, map("text", "msg1"));
MutableDocument doc2 = doc("rooms/eros/messages/2", 0, map("text", "msg2"));
MutableDocument doc3 = doc("rooms/other/messages/1", 0, map("text", "msg3"));
ImmutableSortedMap<DocumentKey, Document> updates = docUpdates(doc1, doc2, doc3);
View.DocumentChanges docViewChanges = view.computeDocChanges(updates);
TargetChange targetChange = ackTarget(doc1, doc2, doc3);
ViewSnapshot snapshot = view.applyChanges(docViewChanges, targetChange).getSnapshot();
assertEquals(query, snapshot.getQuery());
assertEquals(asList(doc1, doc2), snapshot.getDocuments().toList());
assertEquals(asList(DocumentViewChange.create(Type.ADDED, doc1), DocumentViewChange.create(Type.ADDED, doc2)), snapshot.getChanges());
assertFalse(snapshot.isFromCache());
assertTrue(snapshot.didSyncStateChange());
assertFalse(snapshot.hasPendingWrites());
}
use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.
the class SQLiteIndexManagerTest method verifyResults.
private void verifyResults(Query query, String... documents) {
Target target = query.toTarget();
List<DocumentKey> results = indexManager.getDocumentsMatchingTarget(target);
assertNotNull("Target cannot be served from index.", results);
List<DocumentKey> keys = Arrays.stream(documents).map(TestUtil::key).collect(Collectors.toList());
assertWithMessage("Result for %s", query).that(results).containsExactlyElementsIn(keys).inOrder();
}
use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.
the class LocalStoreTestCase method testMutationBatchKeys.
@Test
public void testMutationBatchKeys() {
SetMutation set1 = setMutation("foo/bar", map("foo", "bar"));
SetMutation set2 = setMutation("foo/baz", map("foo", "baz"));
MutationBatch batch = new MutationBatch(1, Timestamp.now(), Collections.emptyList(), asList(set1, set2));
Set<DocumentKey> keys = batch.getKeys();
assertEquals(2, keys.size());
}
use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.
the class LocalStoreTestCase method testRemoteDocumentKeysForTarget.
@Test
public void testRemoteDocumentKeysForTarget() {
Query query = query("foo");
allocateQuery(query);
assertTargetId(2);
applyRemoteEvent(addedRemoteEvent(doc("foo/baz", 10, map("a", "b")), asList(2), emptyList()));
applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 20, map("a", "b")), asList(2), emptyList()));
writeMutation(setMutation("foo/bonk", map("a", "b")));
ImmutableSortedSet<DocumentKey> keys = localStore.getRemoteDocumentKeys(2);
assertSetEquals(asList(key("foo/bar"), key("foo/baz")), keys);
keys = localStore.getRemoteDocumentKeys(2);
assertSetEquals(asList(key("foo/bar"), key("foo/baz")), keys);
}
use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.
the class LruGarbageCollectorTestCase method testRemoveOrphanedDocuments.
@Test
public void testRemoveOrphanedDocuments() {
// Track documents we expect to be retained so we can verify post-GC.
// This will contain documents associated with targets that survive GC, as well
// as any documents with pending mutations.
Set<DocumentKey> expectedRetained = new HashSet<>();
// we add two mutations later, for now track them in an array.
List<Mutation> mutations = new ArrayList<>();
persistence.runTransaction("add a target and add two documents to it", () -> {
// Add two documents to first target, queue a mutation on the second document
TargetData targetData = addNextQueryInTransaction();
MutableDocument doc1 = cacheADocumentInTransaction();
addDocumentToTarget(doc1.getKey(), targetData.getTargetId());
expectedRetained.add(doc1.getKey());
MutableDocument doc2 = cacheADocumentInTransaction();
addDocumentToTarget(doc2.getKey(), targetData.getTargetId());
expectedRetained.add(doc2.getKey());
mutations.add(mutation(doc2.getKey()));
});
// Add a second query and register a third document on it
persistence.runTransaction("second query", () -> {
TargetData targetData = addNextQueryInTransaction();
MutableDocument doc3 = cacheADocumentInTransaction();
addDocumentToTarget(doc3.getKey(), targetData.getTargetId());
expectedRetained.add(doc3.getKey());
});
// cache another document and prepare a mutation on it.
persistence.runTransaction("queue a mutation", () -> {
MutableDocument doc4 = cacheADocumentInTransaction();
mutations.add(mutation(doc4.getKey()));
expectedRetained.add(doc4.getKey());
});
// Insert the mutations. These operations don't have a sequence number, they just
// serve to keep the mutated documents from being GC'd while the mutations are outstanding.
persistence.runTransaction("actually register the mutations", () -> {
Timestamp writeTime = Timestamp.now();
mutationQueue.addMutationBatch(writeTime, Collections.emptyList(), mutations);
});
// Mark 5 documents eligible for GC. This simulates documents that were mutated then ack'd.
// Since they were ack'd, they are no longer in a mutation queue, and there is nothing keeping
// them alive.
Set<DocumentKey> toBeRemoved = new HashSet<>();
persistence.runTransaction("add orphaned docs (previously mutated, then ack'd)", () -> {
for (int i = 0; i < 5; i++) {
MutableDocument doc = cacheADocumentInTransaction();
toBeRemoved.add(doc.getKey());
markDocumentEligibleForGcInTransaction(doc.getKey());
}
});
// We expect only the orphaned documents, those not in a mutation or a target, to be removed.
// use a large sequence number to remove as much as possible
int removed = garbageCollector.removeOrphanedDocuments(1000);
assertEquals(toBeRemoved.size(), removed);
persistence.runTransaction("verify", () -> {
for (DocumentKey key : toBeRemoved) {
assertFalse(documentCache.get(key).isValidDocument());
assertFalse(targetCache.containsKey(key));
}
for (DocumentKey key : expectedRetained) {
assertTrue(documentCache.get(key).isValidDocument());
}
});
}
Aggregations