Search in sources :

Example 1 with ObjectValue

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

the class LocalStore method writeLocally.

/**
 * Accepts locally generated Mutations and commits them to storage.
 */
public LocalDocumentsResult writeLocally(List<Mutation> mutations) {
    Timestamp localWriteTime = Timestamp.now();
    // TODO: Call queryEngine.handleDocumentChange() appropriately.
    Set<DocumentKey> keys = new HashSet<>();
    for (Mutation mutation : mutations) {
        keys.add(mutation.getKey());
    }
    return persistence.runTransaction("Locally write mutations", () -> {
        // Load and apply all existing mutations. This lets us compute the current base state for
        // all non-idempotent transforms before applying any additional user-provided writes.
        ImmutableSortedMap<DocumentKey, Document> documents = localDocuments.getDocuments(keys);
        // For non-idempotent mutations (such as `FieldValue.increment()`), we record the base
        // state in a separate patch mutation. This is later used to guarantee consistent values
        // and prevents flicker even if the backend sends us an update that already includes our
        // transform.
        List<Mutation> baseMutations = new ArrayList<>();
        for (Mutation mutation : mutations) {
            ObjectValue baseValue = mutation.extractTransformBaseValue(documents.get(mutation.getKey()));
            if (baseValue != null) {
                // NOTE: The base state should only be applied if there's some existing
                // document to override, so use a Precondition of exists=true
                baseMutations.add(new PatchMutation(mutation.getKey(), baseValue, baseValue.getFieldMask(), Precondition.exists(true)));
            }
        }
        MutationBatch batch = mutationQueue.addMutationBatch(localWriteTime, baseMutations, mutations);
        Map<DocumentKey, Mutation> overlays = batch.applyToLocalDocumentSet(documents);
        documentOverlayCache.saveOverlays(batch.getBatchId(), overlays);
        return new LocalDocumentsResult(batch.getBatchId(), documents);
    });
}
Also used : ArrayList(java.util.ArrayList) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Timestamp(com.google.firebase.Timestamp) ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) DocumentKey(com.google.firebase.firestore.model.DocumentKey) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation) HashSet(java.util.HashSet)

Example 2 with ObjectValue

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

the class MutationTest method testAppliesLocalServerTimestampTransformsToDocuments.

@Test
public void testAppliesLocalServerTimestampTransformsToDocuments() {
    Map<String, Object> data = map("foo", map("bar", "bar-value"), "baz", "baz-value");
    MutableDocument transformedDoc = doc("collection/key", 1, data);
    Timestamp timestamp = Timestamp.now();
    Mutation transform = patchMutation("collection/key", map("foo.bar", FieldValue.serverTimestamp()));
    transform.applyToLocalView(transformedDoc, /* previousMask= */
    null, timestamp);
    // Server timestamps aren't parsed, so we manually insert it.
    ObjectValue expectedData = wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
    Value fieldValue = ServerTimestamps.valueOf(timestamp, wrap("bar-value"));
    expectedData.set(field("foo.bar"), fieldValue);
    MutableDocument expectedDoc = doc("collection/key", 1, expectedData).setHasLocalMutations();
    assertEquals(expectedDoc, transformedDoc);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 3 with ObjectValue

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

the class MutationTest method testNumericIncrementBaseValue.

@Test
public void testNumericIncrementBaseValue() {
    Map<String, Object> allValues = map("ignore", "foo", "double", 42.0, "long", 42, "string", "foo", "map", map());
    allValues.put("nested", new HashMap<>(allValues));
    MutableDocument baseDoc = doc("collection/key", 1, allValues);
    Map<String, Object> allTransforms = map("double", FieldValue.increment(1), "long", FieldValue.increment(1), "string", FieldValue.increment(1), "map", FieldValue.increment(1), "missing", FieldValue.increment(1));
    allTransforms.put("nested", new HashMap<>(allTransforms));
    Mutation mutation = patchMutation("collection/key", allTransforms);
    ObjectValue baseValue = mutation.extractTransformBaseValue(baseDoc);
    Value expected = wrap(map("double", 42.0, "long", 42, "string", 0, "map", 0, "missing", 0, "nested", map("double", 42.0, "long", 42, "string", 0, "map", 0, "missing", 0)));
    assertTrue(Values.equals(expected, baseValue.get(FieldPath.EMPTY_PATH)));
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Example 4 with ObjectValue

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

the class QuerySnapshotTest method testToObjects.

@Test
public void testToObjects() {
    // Prevent NPE on trying to access non-existent settings on the mock.
    when(TestUtil.firestore().getFirestoreSettings()).thenReturn(new FirebaseFirestoreSettings.Builder().build());
    ObjectValue objectData = ObjectValue.fromMap(map("timestamp", ServerTimestamps.valueOf(Timestamp.now(), null)));
    QuerySnapshot foo = TestUtil.querySnapshot("foo", map(), map("a", objectData), true, false);
    List<POJO> docs = foo.toObjects(POJO.class);
    assertEquals(1, docs.size());
    assertNull(docs.get(0).timestamp);
    docs = foo.toObjects(POJO.class, ServerTimestampBehavior.ESTIMATE);
    assertEquals(1, docs.size());
    assertNotNull(docs.get(0).timestamp);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) Test(org.junit.Test)

Example 5 with ObjectValue

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

the class UserDataWriterTest method testConvertsNestedObjects.

@Test
public void testConvertsNestedObjects() {
    ObjectValue actual = wrapObject("a", map("b", map("c", "foo"), "d", true));
    ObjectValue expected = new ObjectValue();
    expected.set(field("a.b.c"), wrap("foo"));
    expected.set(field("a.d"), wrap(true));
    assertEquals(expected, actual);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) Test(org.junit.Test)

Aggregations

ObjectValue (com.google.firebase.firestore.model.ObjectValue)24 Value (com.google.firestore.v1.Value)12 FieldPath (com.google.firebase.firestore.model.FieldPath)8 Test (org.junit.Test)8 MutableDocument (com.google.firebase.firestore.model.MutableDocument)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)4 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)4 ArrayValue (com.google.firestore.v1.ArrayValue)4 MapValue (com.google.firestore.v1.MapValue)4 NullValue (com.google.protobuf.NullValue)4 ArrayRemoveFieldValue (com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)3 ArrayUnionFieldValue (com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue)3 DeleteFieldValue (com.google.firebase.firestore.FieldValue.DeleteFieldValue)3 ServerTimestampFieldValue (com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)3 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3