Search in sources :

Example 86 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class Datastore method lookup.

public Task<List<MutableDocument>> lookup(List<DocumentKey> keys) {
    BatchGetDocumentsRequest.Builder builder = BatchGetDocumentsRequest.newBuilder();
    builder.setDatabase(serializer.databaseName());
    for (DocumentKey key : keys) {
        builder.addDocuments(serializer.encodeKey(key));
    }
    return channel.runStreamingResponseRpc(FirestoreGrpc.getBatchGetDocumentsMethod(), builder.build()).continueWith(workerQueue.getExecutor(), task -> {
        if (!task.isSuccessful()) {
            if (task.getException() instanceof FirebaseFirestoreException && ((FirebaseFirestoreException) task.getException()).getCode() == FirebaseFirestoreException.Code.UNAUTHENTICATED) {
                channel.invalidateToken();
            }
        }
        Map<DocumentKey, MutableDocument> resultMap = new HashMap<>();
        List<BatchGetDocumentsResponse> responses = task.getResult();
        for (BatchGetDocumentsResponse response : responses) {
            MutableDocument doc = serializer.decodeMaybeDocument(response);
            resultMap.put(doc.getKey(), doc);
        }
        List<MutableDocument> results = new ArrayList<>();
        for (DocumentKey key : keys) {
            results.add(resultMap.get(key));
        }
        return results;
    });
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) BatchGetDocumentsResponse(com.google.firestore.v1.BatchGetDocumentsResponse) ArrayList(java.util.ArrayList) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest)

Example 87 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class QueryEngineTestCase method addMutation.

/**
 * Adds a mutation to the mutation queue.
 */
protected void addMutation(Mutation mutation) {
    persistence.runTransaction("addMutation", () -> {
        MutationBatch batch = mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), Collections.singletonList(mutation));
        Map<DocumentKey, Mutation> overlayMap = Collections.singletonMap(mutation.getKey(), mutation);
        documentOverlayCache.saveOverlays(batch.getBatchId(), overlayMap);
    });
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) DocumentKey(com.google.firebase.firestore.model.DocumentKey) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation)

Example 88 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class QueryEngineTestCase method doesNotIncludeDocumentsDeletedByMutation.

@Test
public void doesNotIncludeDocumentsDeletedByMutation() throws Exception {
    Query query = query("coll");
    addDocument(MATCHING_DOC_A, MATCHING_DOC_B);
    persistQueryMapping(MATCHING_DOC_A.getKey(), MATCHING_DOC_B.getKey());
    // Add an unacknowledged mutation
    addMutation(new DeleteMutation(key("coll/b"), Precondition.NONE));
    ImmutableSortedMap<DocumentKey, Document> docs = expectFullCollectionScan(() -> queryEngine.getDocumentsMatchingQuery(query, LAST_LIMBO_FREE_SNAPSHOT, targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID)));
    assertEquals(emptyMutableDocumentMap().insert(MATCHING_DOC_A.getKey(), MATCHING_DOC_A), docs);
}
Also used : Query(com.google.firebase.firestore.core.Query) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 89 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class QueryEngineTestCase method persistQueryMapping.

/**
 * Adds the provided documents to the query target mapping.
 */
private void persistQueryMapping(DocumentKey... documentKeys) {
    persistence.runTransaction("persistQueryMapping", () -> {
        ImmutableSortedSet<DocumentKey> remoteKeys = DocumentKey.emptyKeySet();
        for (DocumentKey documentKey : documentKeys) {
            remoteKeys = remoteKeys.insert(documentKey);
        }
        targetCache.addMatchingKeys(remoteKeys, TEST_TARGET_ID);
    });
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 90 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class QueryEngineTestCase method runQuery.

private DocumentSet runQuery(Query query, SnapshotVersion lastLimboFreeSnapshotVersion) {
    Preconditions.checkNotNull(expectFullCollectionScan, "Encountered runQuery() call not wrapped in expectOptimizedCollectionQuery()/expectFullCollectionQuery()");
    ImmutableSortedMap<DocumentKey, Document> docs = queryEngine.getDocumentsMatchingQuery(query, lastLimboFreeSnapshotVersion, targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID));
    View view = new View(query, new ImmutableSortedSet<>(Collections.emptyList(), DocumentKey::compareTo));
    View.DocumentChanges viewDocChanges = view.computeDocChanges(docs);
    return view.applyChanges(viewDocChanges).getSnapshot().getDocuments();
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) View(com.google.firebase.firestore.core.View)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)134 MutableDocument (com.google.firebase.firestore.model.MutableDocument)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)29 ArrayList (java.util.ArrayList)25 Document (com.google.firebase.firestore.model.Document)23 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 Mutation (com.google.firebase.firestore.model.mutation.Mutation)15 Map (java.util.Map)15 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 HashSet (java.util.HashSet)11 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)9 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)7 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)7 ByteString (com.google.protobuf.ByteString)7 Query (com.google.firebase.firestore.core.Query)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 Timestamp (com.google.firebase.Timestamp)5