Search in sources :

Example 21 with Mutation

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

the class LruGarbageCollectorTestCase method testRemoveOrphanedDocuments.

@Test
public void testRemoveOrphanedDocuments() {
    // Track documents we expect to be retained so we can verify post-GC.
    // This will contain documents associated with targets that survive GC, as well
    // as any documents with pending mutations.
    Set<DocumentKey> expectedRetained = new HashSet<>();
    // we add two mutations later, for now track them in an array.
    List<Mutation> mutations = new ArrayList<>();
    persistence.runTransaction("add a target and add two documents to it", () -> {
        // Add two documents to first target, queue a mutation on the second document
        TargetData targetData = addNextQueryInTransaction();
        MutableDocument doc1 = cacheADocumentInTransaction();
        addDocumentToTarget(doc1.getKey(), targetData.getTargetId());
        expectedRetained.add(doc1.getKey());
        MutableDocument doc2 = cacheADocumentInTransaction();
        addDocumentToTarget(doc2.getKey(), targetData.getTargetId());
        expectedRetained.add(doc2.getKey());
        mutations.add(mutation(doc2.getKey()));
    });
    // Add a second query and register a third document on it
    persistence.runTransaction("second query", () -> {
        TargetData targetData = addNextQueryInTransaction();
        MutableDocument doc3 = cacheADocumentInTransaction();
        addDocumentToTarget(doc3.getKey(), targetData.getTargetId());
        expectedRetained.add(doc3.getKey());
    });
    // cache another document and prepare a mutation on it.
    persistence.runTransaction("queue a mutation", () -> {
        MutableDocument doc4 = cacheADocumentInTransaction();
        mutations.add(mutation(doc4.getKey()));
        expectedRetained.add(doc4.getKey());
    });
    // Insert the mutations. These operations don't have a sequence number, they just
    // serve to keep the mutated documents from being GC'd while the mutations are outstanding.
    persistence.runTransaction("actually register the mutations", () -> {
        Timestamp writeTime = Timestamp.now();
        mutationQueue.addMutationBatch(writeTime, Collections.emptyList(), mutations);
    });
    // Mark 5 documents eligible for GC. This simulates documents that were mutated then ack'd.
    // Since they were ack'd, they are no longer in a mutation queue, and there is nothing keeping
    // them alive.
    Set<DocumentKey> toBeRemoved = new HashSet<>();
    persistence.runTransaction("add orphaned docs (previously mutated, then ack'd)", () -> {
        for (int i = 0; i < 5; i++) {
            MutableDocument doc = cacheADocumentInTransaction();
            toBeRemoved.add(doc.getKey());
            markDocumentEligibleForGcInTransaction(doc.getKey());
        }
    });
    // We expect only the orphaned documents, those not in a mutation or a target, to be removed.
    // use a large sequence number to remove as much as possible
    int removed = garbageCollector.removeOrphanedDocuments(1000);
    assertEquals(toBeRemoved.size(), removed);
    persistence.runTransaction("verify", () -> {
        for (DocumentKey key : toBeRemoved) {
            assertFalse(documentCache.get(key).isValidDocument());
            assertFalse(targetCache.containsKey(key));
        }
        for (DocumentKey key : expectedRetained) {
            assertTrue(documentCache.get(key).isValidDocument());
        }
    });
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) Timestamp(com.google.firebase.Timestamp) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with Mutation

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

the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentKeys.

@Test
public void testAllMutationBatchesAffectingDocumentKeys() {
    List<Mutation> mutations = asList(setMutation("fob/bar", map("a", 1)), setMutation("foo/bar", map("a", 1)), patchMutation("foo/bar", map("b", 1)), setMutation("foo/bar/suffix/key", map("a", 1)), setMutation("foo/baz", map("a", 1)), setMutation("food/bar", map("a", 1)));
    // Store all the mutations.
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        for (Mutation mutation : mutations) {
            batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
        }
    });
    ImmutableSortedSet<DocumentKey> keys = DocumentKey.emptyKeySet().insert(key("foo/bar")).insert(key("foo/baz"));
    List<MutationBatch> expected = asList(batches.get(1), batches.get(2), batches.get(4));
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKeys(keys);
    assertEquals(expected, matches);
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Test(org.junit.Test)

Example 23 with Mutation

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

the class MutationQueueTestCase method testAllMutationBatchesAffectingQuery.

@Test
public void testAllMutationBatchesAffectingQuery() {
    List<Mutation> mutations = asList(setMutation("fob/bar", map("a", 1)), setMutation("foo/bar", map("a", 1)), patchMutation("foo/bar", map("b", 1)), setMutation("foo/bar/suffix/key", map("a", 1)), setMutation("foo/baz", map("a", 1)), setMutation("food/bar", map("a", 1)));
    // Store all the mutations.
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        for (Mutation mutation : mutations) {
            batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
        }
    });
    List<MutationBatch> expected = asList(batches.get(1), batches.get(2), batches.get(4));
    Query query = Query.atPath(path("foo"));
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingQuery(query);
    assertEquals(expected, matches);
}
Also used : Query(com.google.firebase.firestore.core.Query) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Test(org.junit.Test)

Example 24 with Mutation

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

the class Datastore method commit.

public Task<List<MutationResult>> commit(List<Mutation> mutations) {
    CommitRequest.Builder builder = CommitRequest.newBuilder();
    builder.setDatabase(serializer.databaseName());
    for (Mutation mutation : mutations) {
        builder.addWrites(serializer.encodeMutation(mutation));
    }
    return channel.runRpc(FirestoreGrpc.getCommitMethod(), builder.build()).continueWith(workerQueue.getExecutor(), task -> {
        if (!task.isSuccessful()) {
            if (task.getException() instanceof FirebaseFirestoreException && ((FirebaseFirestoreException) task.getException()).getCode() == FirebaseFirestoreException.Code.UNAUTHENTICATED) {
                channel.invalidateToken();
            }
            throw task.getException();
        }
        CommitResponse response = task.getResult();
        SnapshotVersion commitVersion = serializer.decodeVersion(response.getCommitTime());
        int count = response.getWriteResultsCount();
        ArrayList<MutationResult> results = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            com.google.firestore.v1.WriteResult result = response.getWriteResults(i);
            results.add(serializer.decodeMutationResult(result, commitVersion));
        }
        return results;
    });
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) ArrayList(java.util.ArrayList) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) CommitResponse(com.google.firestore.v1.CommitResponse) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Mutation(com.google.firebase.firestore.model.mutation.Mutation) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 25 with Mutation

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

the class SQLiteDocumentOverlayCache method saveOverlays.

@Override
public void saveOverlays(int largestBatchId, Map<DocumentKey, Mutation> overlays) {
    for (Map.Entry<DocumentKey, Mutation> entry : overlays.entrySet()) {
        DocumentKey key = entry.getKey();
        Mutation overlay = checkNotNull(entry.getValue(), "null value for key: %s", key);
        saveOverlay(largestBatchId, key, overlay);
    }
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Mutation (com.google.firebase.firestore.model.mutation.Mutation)46 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)29 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)27 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)25 Test (org.junit.Test)25 DocumentKey (com.google.firebase.firestore.model.DocumentKey)18 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)13 Write (com.google.firestore.v1.Write)12 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)8 TestUtil.verifyMutation (com.google.firebase.firestore.testutil.TestUtil.verifyMutation)8 ArrayList (java.util.ArrayList)8 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)7 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)5 Map (java.util.Map)4 Timestamp (com.google.firebase.Timestamp)3 MutableDocument (com.google.firebase.firestore.model.MutableDocument)3 Overlay (com.google.firebase.firestore.model.mutation.Overlay)3 SQLiteStatement (android.database.sqlite.SQLiteStatement)2