Search in sources :

Example 91 with MutableDocument

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

the class DocumentChangeTest method testModifications.

@Test
public void testModifications() {
    Query query = Query.atPath(path("c"));
    List<MutableDocument> initialDocs = asList(doc("c/a", 1, map("value", "a-1")), doc("c/b", 1, map("value", "b-1")), doc("c/c", 1, map("value", "c-1")));
    List<MutableDocument> updates = asList(doc("c/a", 2, map("value", "a-2")), doc("c/c", 2, map("value", "c-2")));
    validatePositions(query, initialDocs, Collections.emptyList(), updates, Collections.emptyList());
}
Also used : Query(com.google.firebase.firestore.core.Query) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 92 with MutableDocument

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

the class DocumentChangeTest method testAdditions.

@Test
public void testAdditions() {
    Query query = Query.atPath(path("c"));
    List<MutableDocument> initialDocs = asList(doc("c/a", 1, map()), doc("c/c", 1, map()), doc("c/e", 1, map()));
    List<MutableDocument> adds = asList(doc("c/d", 2, map()), doc("c/b", 2, map()));
    validatePositions(query, initialDocs, adds, asList(), Collections.emptyList());
}
Also used : Query(com.google.firebase.firestore.core.Query) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 93 with MutableDocument

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

the class DocumentChangeTest method randomTests.

@Test
public void randomTests() {
    for (int run = 0; run < 100; run++) {
        Query query = Query.atPath(path("c")).orderBy(orderBy("sort"));
        Map<DocumentKey, MutableDocument> initialDocs = new HashMap<>();
        List<MutableDocument> adds = new ArrayList<>();
        List<MutableDocument> updates = new ArrayList<>();
        List<MutableDocument> deletes = new ArrayList<>();
        int numDocs = 100;
        for (int i = 0; i < numDocs; i++) {
            String docKey = "c/test-doc-" + i;
            // Skip 20% of the docs
            if (Math.random() > 0.8) {
                initialDocs.put(key(docKey), doc(docKey, 1, map("sort", Math.random())));
            }
        }
        for (int i = 0; i < numDocs; i++) {
            String docKey = "c/test-doc-" + i;
            // Only update 20% of the docs
            if (Math.random() < 0.2) {
                // 30% deletes, rest updates and/or additions
                if (Math.random() < 0.3) {
                    deletes.add(deletedDoc(docKey, 2));
                } else {
                    if (initialDocs.containsKey(key(docKey))) {
                        updates.add(doc(docKey, 2, map("sort", Math.random())));
                    } else {
                        adds.add(doc(docKey, 2, map("sort", Math.random())));
                    }
                }
            }
        }
        validatePositions(query, initialDocs.values(), adds, updates, deletes);
    }
}
Also used : Query(com.google.firebase.firestore.core.Query) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 94 with MutableDocument

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

the class SQLiteRemoteDocumentCache method getAll.

@Override
public Map<DocumentKey, MutableDocument> getAll(Iterable<DocumentKey> documentKeys) {
    Map<DocumentKey, MutableDocument> results = new HashMap<>();
    List<Object> bindVars = new ArrayList<>();
    for (DocumentKey key : documentKeys) {
        bindVars.add(EncodedPath.encode(key.getPath()));
        // Make sure each key has a corresponding entry, which is null in case the document is not
        // found.
        results.put(key, MutableDocument.newInvalidDocument(key));
    }
    SQLitePersistence.LongQuery longQuery = new SQLitePersistence.LongQuery(db, "SELECT contents, read_time_seconds, read_time_nanos FROM remote_documents " + "WHERE path IN (", bindVars, ") ORDER BY path");
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    while (longQuery.hasMoreSubqueries()) {
        longQuery.performNextSubquery().forEach(row -> processRowInBackground(backgroundQueue, results, row));
    }
    backgroundQueue.drain();
    return results;
}
Also used : BackgroundQueue(com.google.firebase.firestore.util.BackgroundQueue) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList)

Example 95 with MutableDocument

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

the class SyncEngine method handleRejectedListen.

/**
 * Called by FirestoreClient to notify us of a rejected listen.
 */
@Override
public void handleRejectedListen(int targetId, Status error) {
    assertCallback("handleRejectedListen");
    LimboResolution limboResolution = activeLimboResolutionsByTarget.get(targetId);
    DocumentKey limboKey = limboResolution != null ? limboResolution.key : null;
    if (limboKey != null) {
        // Since this query failed, we won't want to manually unlisten to it.
        // So go ahead and remove it from bookkeeping.
        activeLimboTargetsByKey.remove(limboKey);
        activeLimboResolutionsByTarget.remove(targetId);
        pumpEnqueuedLimboResolutions();
        // TODO: Retry on transient errors?
        // It's a limbo doc. Create a synthetic event saying it was deleted. This is kind of a hack.
        // Ideally, we would have a method in the local store to purge a document. However, it would
        // be tricky to keep all of the local store's invariants with another method.
        MutableDocument result = MutableDocument.newNoDocument(limboKey, SnapshotVersion.NONE);
        Map<DocumentKey, MutableDocument> documentUpdates = Collections.singletonMap(limboKey, result);
        Set<DocumentKey> limboDocuments = Collections.singleton(limboKey);
        RemoteEvent event = new RemoteEvent(SnapshotVersion.NONE, /* targetChanges= */
        Collections.emptyMap(), /* targetMismatches= */
        Collections.emptySet(), documentUpdates, limboDocuments);
        handleRemoteEvent(event);
    } else {
        localStore.releaseTarget(targetId);
        removeAndCleanupTarget(targetId, error);
    }
}
Also used : RemoteEvent(com.google.firebase.firestore.remote.RemoteEvent) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Aggregations

MutableDocument (com.google.firebase.firestore.model.MutableDocument)166 Test (org.junit.Test)125 DocumentKey (com.google.firebase.firestore.model.DocumentKey)43 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)30 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)30 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)30 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)30 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)30 HashMap (java.util.HashMap)22 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)18 ArrayList (java.util.ArrayList)18 TargetData (com.google.firebase.firestore.local.TargetData)15 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)14 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)13 ResourcePath (com.google.firebase.firestore.model.ResourcePath)12 Query (com.google.firebase.firestore.core.Query)10 Map (java.util.Map)10 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)8 Document (com.google.firebase.firestore.model.Document)7 Timestamp (com.google.firebase.Timestamp)6