use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class LocalDocumentsView method getDocumentsMatchingDocumentQuery.
/**
* Performs a simple document lookup for the given path.
*/
private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingDocumentQuery(ResourcePath path) {
ImmutableSortedMap<DocumentKey, Document> result = emptyDocumentMap();
// Just do a simple document lookup.
Document doc = getDocument(DocumentKey.fromPath(path));
if (doc.isFoundDocument()) {
result = result.insert(doc.getKey(), doc);
}
return result;
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class SQLiteOverlayMigrationManagerTest method assertContains.
/**
* Asserts that the given local store contains the given document.
*/
private void assertContains(MutableDocument expected) {
Document actual = localStore.readDocument(expected.getKey());
assertEquals(expected, actual);
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class DocumentChangeTest method validatePositions.
private static void validatePositions(com.google.firebase.firestore.core.Query query, Collection<MutableDocument> initialDocsList, Collection<MutableDocument> addedList, Collection<MutableDocument> modifiedList, Collection<MutableDocument> removedList) {
ImmutableSortedMap<DocumentKey, Document> initialDocs = docUpdates(initialDocsList.toArray(new MutableDocument[] {}));
ImmutableSortedMap<DocumentKey, Document> updates = emptyDocumentMap();
for (MutableDocument doc : addedList) {
updates = updates.insert(doc.getKey(), doc);
}
for (MutableDocument doc : modifiedList) {
updates = updates.insert(doc.getKey(), doc);
}
for (MutableDocument doc : removedList) {
updates = updates.insert(doc.getKey(), doc);
}
View view = new View(query, DocumentKey.emptyKeySet());
View.DocumentChanges initialChanges = view.computeDocChanges(initialDocs);
TargetChange initialTargetChange = ackTarget(initialDocsList.toArray(new MutableDocument[] {}));
ViewSnapshot initialSnapshot = view.applyChanges(initialChanges, initialTargetChange).getSnapshot();
View.DocumentChanges updateChanges = view.computeDocChanges(updates);
TargetChange updateTargetChange = targetChange(ByteString.EMPTY, true, addedList, modifiedList, removedList);
ViewSnapshot updatedSnapshot = view.applyChanges(updateChanges, updateTargetChange).getSnapshot();
if (updatedSnapshot == null) {
// Nothing changed, no positions to verify
return;
}
List<Document> expected = new ArrayList<>(updatedSnapshot.getDocuments().toList());
List<Document> actual = new ArrayList<>(initialSnapshot.getDocuments().toList());
FirebaseFirestore firestore = mock(FirebaseFirestore.class);
List<DocumentChange> changes = DocumentChange.changesFromSnapshot(firestore, MetadataChanges.EXCLUDE, updatedSnapshot);
for (DocumentChange change : changes) {
if (change.getType() != Type.ADDED) {
actual.remove(change.getOldIndex());
}
if (change.getType() != Type.REMOVED) {
actual.add(change.getNewIndex(), change.getDocument().getDocument());
}
}
assertEquals(expected, actual);
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class DocumentReference method addSnapshotListenerInternal.
/**
* Internal helper method to create add a snapshot listener.
*
* <p>Will be Activity scoped if the activity parameter is non-{@code null}.
*
* @param userExecutor The executor to use to call the listener.
* @param options The options to use for this listen.
* @param activity Optional activity this listener is scoped to.
* @param userListener The user-supplied event listener that will be called with document
* snapshots.
* @return A registration object that can be used to remove the listener.
*/
private ListenerRegistration addSnapshotListenerInternal(Executor userExecutor, ListenOptions options, @Nullable Activity activity, EventListener<DocumentSnapshot> userListener) {
// Convert from ViewSnapshots to DocumentSnapshots.
EventListener<ViewSnapshot> viewListener = (snapshot, error) -> {
if (error != null) {
userListener.onEvent(null, error);
return;
}
Assert.hardAssert(snapshot != null, "Got event without value or error set");
Assert.hardAssert(snapshot.getDocuments().size() <= 1, "Too many documents returned on a document query");
Document document = snapshot.getDocuments().getDocument(key);
DocumentSnapshot documentSnapshot;
if (document != null) {
boolean hasPendingWrites = snapshot.getMutatedKeys().contains(document.getKey());
documentSnapshot = DocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), hasPendingWrites);
} else {
// We don't raise `hasPendingWrites` for deleted documents.
documentSnapshot = DocumentSnapshot.fromNoDocument(firestore, key, snapshot.isFromCache());
}
userListener.onEvent(documentSnapshot, null);
};
// Call the viewListener on the userExecutor.
AsyncEventListener<ViewSnapshot> asyncListener = new AsyncEventListener<>(userExecutor, viewListener);
com.google.firebase.firestore.core.Query query = asQuery();
QueryListener queryListener = firestore.getClient().listen(query, options, asyncListener);
return ActivityScope.bind(activity, new ListenerRegistrationImpl(firestore.getClient(), queryListener, asyncListener));
}
use of com.google.firebase.firestore.model.Document in project firebase-android-sdk by firebase.
the class DocumentReference method get.
/**
* Reads the document referenced by this {@code DocumentReference}.
*
* <p>By default, {@code get()} attempts to provide up-to-date data when possible by waiting for
* data from the server, but it may return cached data or fail if you are offline and the server
* cannot be reached. This behavior can be altered via the {@code Source} parameter.
*
* @param source A value to configure the get behavior.
* @return A Task that will be resolved with the contents of the Document at this {@code
* DocumentReference}.
*/
@NonNull
public Task<DocumentSnapshot> get(@NonNull Source source) {
if (source == Source.CACHE) {
return firestore.getClient().getDocumentFromLocalCache(key).continueWith(Executors.DIRECT_EXECUTOR, (Task<Document> task) -> {
Document doc = task.getResult();
boolean hasPendingWrites = doc != null && doc.hasLocalMutations();
return new DocumentSnapshot(firestore, key, doc, /*isFromCache=*/
true, hasPendingWrites);
});
} else {
return getViaSnapshotListener(source);
}
}
Aggregations