Search in sources :

Example 6 with Document

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;
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 7 with Document

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);
}
Also used : Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 8 with Document

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);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) View(com.google.firebase.firestore.core.View) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) TargetChange(com.google.firebase.firestore.remote.TargetChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 9 with Document

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));
}
Also used : ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) AsyncEventListener(com.google.firebase.firestore.core.AsyncEventListener) NonNull(androidx.annotation.NonNull) Task(com.google.android.gms.tasks.Task) Collections.singletonList(java.util.Collections.singletonList) Code(com.google.firebase.firestore.FirebaseFirestoreException.Code) Map(java.util.Map) ParsedSetData(com.google.firebase.firestore.core.UserData.ParsedSetData) Util.voidErrorTransformer(com.google.firebase.firestore.util.Util.voidErrorTransformer) Assert(com.google.firebase.firestore.util.Assert) ListenerRegistrationImpl(com.google.firebase.firestore.core.ListenerRegistrationImpl) ParsedUpdateData(com.google.firebase.firestore.core.UserData.ParsedUpdateData) Precondition(com.google.firebase.firestore.model.mutation.Precondition) ActivityScope(com.google.firebase.firestore.core.ActivityScope) ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) Util(com.google.firebase.firestore.util.Util) Preconditions.checkNotNull(com.google.firebase.firestore.util.Preconditions.checkNotNull) Document(com.google.firebase.firestore.model.Document) Executor(java.util.concurrent.Executor) Executors(com.google.firebase.firestore.util.Executors) ResourcePath(com.google.firebase.firestore.model.ResourcePath) QueryListener(com.google.firebase.firestore.core.QueryListener) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation) ExecutionException(java.util.concurrent.ExecutionException) Nullable(androidx.annotation.Nullable) Tasks(com.google.android.gms.tasks.Tasks) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) Assert.fail(com.google.firebase.firestore.util.Assert.fail) Activity(android.app.Activity) Collections(java.util.Collections) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) ListenerRegistrationImpl(com.google.firebase.firestore.core.ListenerRegistrationImpl) QueryListener(com.google.firebase.firestore.core.QueryListener) AsyncEventListener(com.google.firebase.firestore.core.AsyncEventListener)

Example 10 with Document

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);
    }
}
Also used : Task(com.google.android.gms.tasks.Task) Document(com.google.firebase.firestore.model.Document) NonNull(androidx.annotation.NonNull)

Aggregations

Document (com.google.firebase.firestore.model.Document)29 DocumentKey (com.google.firebase.firestore.model.DocumentKey)23 MutableDocument (com.google.firebase.firestore.model.MutableDocument)21 Map (java.util.Map)8 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)4 Query (com.google.firebase.firestore.core.Query)3 Overlay (com.google.firebase.firestore.model.mutation.Overlay)3 TargetChange (com.google.firebase.firestore.remote.TargetChange)3 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 Task (com.google.android.gms.tasks.Task)2 Timestamp (com.google.firebase.Timestamp)2 View (com.google.firebase.firestore.core.View)2 ViewSnapshot (com.google.firebase.firestore.core.ViewSnapshot)2 DocumentSet (com.google.firebase.firestore.model.DocumentSet)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)2 DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)2