use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class LocalStoreTestCase method assertNotContains.
/**
* Asserts that the given local store does not contain the given document.
*/
private void assertNotContains(String keyPathString) {
DocumentKey key = DocumentKey.fromPathString(keyPathString);
Document actual = localStore.readDocument(key);
assertFalse(actual.isValidDocument());
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class SQLiteQueryEngineTest method combinesIndexedWithNonIndexedResults.
@Test
public void combinesIndexedWithNonIndexedResults() throws Exception {
MutableDocument doc1 = doc("coll/a", 1, map("foo", true));
MutableDocument doc2 = doc("coll/b", 2, map("foo", true));
MutableDocument doc3 = doc("coll/c", 3, map("foo", true));
MutableDocument doc4 = doc("coll/d", 3, map("foo", true));
indexManager.addFieldIndex(fieldIndex("coll", "foo", Kind.ASCENDING));
addDocument(doc1);
addDocument(doc2);
indexManager.updateIndexEntries(docMap(doc1, doc2));
indexManager.updateCollectionGroup("coll", IndexOffset.fromDocument(doc2));
addDocument(doc3);
addMutation(setMutation("coll/d", map("foo", true)));
Query queryWithFilter = query("coll").filter(filter("foo", "==", true));
ImmutableSortedMap<DocumentKey, Document> results = expectOptimizedCollectionScan(() -> queryEngine.getDocumentsMatchingQuery(queryWithFilter, SnapshotVersion.NONE, DocumentKey.emptyKeySet()));
assertTrue(results.containsKey(doc1.getKey()));
assertTrue(results.containsKey(doc2.getKey()));
assertTrue(results.containsKey(doc3.getKey()));
assertTrue(results.containsKey(doc4.getKey()));
}
use of com.google.firebase.firestore.model.Document 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;
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class SQLiteIndexManager method updateIndexEntries.
@Override
public void updateIndexEntries(ImmutableSortedMap<DocumentKey, Document> documents) {
hardAssert(started, "IndexManager not started");
for (Map.Entry<DocumentKey, Document> entry : documents) {
Collection<FieldIndex> fieldIndexes = getFieldIndexes(entry.getKey().getCollectionGroup());
for (FieldIndex fieldIndex : fieldIndexes) {
SortedSet<IndexEntry> existingEntries = getExistingIndexEntries(entry.getKey(), fieldIndex);
SortedSet<IndexEntry> newEntries = computeIndexEntries(entry.getValue(), fieldIndex);
if (!existingEntries.equals(newEntries)) {
updateEntries(entry.getValue(), existingEntries, newEntries);
}
}
}
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class MemoryLruReferenceDelegate method removeOrphanedDocuments.
@Override
public int removeOrphanedDocuments(long upperBound) {
MemoryRemoteDocumentCache cache = persistence.getRemoteDocumentCache();
List<DocumentKey> docsToRemove = new ArrayList<>();
for (Document doc : cache.getDocuments()) {
DocumentKey key = doc.getKey();
if (!isPinned(key, upperBound)) {
docsToRemove.add(key);
orphanedSequenceNumbers.remove(key);
}
}
cache.removeAll(docsToRemove);
return docsToRemove.size();
}
Aggregations