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);
});
}
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);
}
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)));
}
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);
}
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);
}
Aggregations