use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class PatchMutation method applyToRemoteDocument.
@Override
public void applyToRemoteDocument(MutableDocument document, MutationResult mutationResult) {
verifyKeyMatches(document);
if (!this.getPrecondition().isValidFor(document)) {
// Since the mutation was not rejected, we know that the precondition matched on the backend.
// We therefore must not have the expected version of the document in our cache and return an
// UnknownDocument with the known updateTime.
document.convertToUnknownDocument(mutationResult.getVersion());
return;
}
Map<FieldPath, Value> transformResults = serverTransformResults(document, mutationResult.getTransformResults());
ObjectValue value = document.getData();
value.setAll(getPatch());
value.setAll(transformResults);
document.convertToFoundDocument(mutationResult.getVersion(), document.getData()).setHasCommittedMutations();
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class PatchMutation method applyToLocalView.
@Override
@Nullable
public FieldMask applyToLocalView(MutableDocument document, @Nullable FieldMask previousMask, Timestamp localWriteTime) {
verifyKeyMatches(document);
if (!getPrecondition().isValidFor(document)) {
return previousMask;
}
Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
Map<FieldPath, Value> patches = getPatch();
ObjectValue value = document.getData();
value.setAll(patches);
value.setAll(transformResults);
document.convertToFoundDocument(document.getVersion(), document.getData()).setHasLocalMutations();
if (previousMask == null) {
return null;
}
HashSet<FieldPath> mergedMaskSet = new HashSet<>(previousMask.getMask());
mergedMaskSet.addAll(this.mask.getMask());
mergedMaskSet.addAll(getFieldTransformPaths());
return FieldMask.fromSet(mergedMaskSet);
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class Mutation method extractTransformBaseValue.
public ObjectValue extractTransformBaseValue(Document document) {
ObjectValue baseObject = null;
for (FieldTransform transform : fieldTransforms) {
Value existingValue = document.getField(transform.getFieldPath());
Value coercedValue = transform.getOperation().computeBaseValue(existingValue);
if (coercedValue != null) {
if (baseObject == null) {
baseObject = new ObjectValue();
}
baseObject.set(transform.getFieldPath(), coercedValue);
}
}
return baseObject;
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class Mutation method calculateOverlayMutation.
/**
* A utility method to calculate an {@link Mutation} representing the overlay from the final state
* of the document, and a {@link FieldMask} representing the fields that are mutated by the local
* mutations.
*/
public static Mutation calculateOverlayMutation(MutableDocument doc, @Nullable FieldMask mask) {
if ((!doc.hasLocalMutations()) || (mask != null && mask.getMask().isEmpty())) {
return null;
}
// mask == null when there are Set or Delete being applied to get to the current document.
if (mask == null) {
if (doc.isNoDocument()) {
return new DeleteMutation(doc.getKey(), Precondition.NONE);
} else {
return new SetMutation(doc.getKey(), doc.getData(), Precondition.NONE);
}
} else {
ObjectValue docValue = doc.getData();
ObjectValue patchValue = new ObjectValue();
HashSet<FieldPath> maskSet = new HashSet<>();
for (FieldPath path : mask.getMask()) {
if (!maskSet.contains(path)) {
Value value = docValue.get(path);
// result, `foo` is not in `mask`, and the resulting mutation would miss `foo`.
if (value == null && path.length() > 1) {
path = path.popLast();
}
patchValue.set(path, docValue.get(path));
maskSet.add(path);
}
}
return new PatchMutation(doc.getKey(), patchValue, FieldMask.fromSet(maskSet), Precondition.NONE);
}
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class SetMutation method applyToRemoteDocument.
@Override
public void applyToRemoteDocument(MutableDocument document, MutationResult mutationResult) {
verifyKeyMatches(document);
// Unlike applyToLocalView, if we're applying a mutation to a remote document the server has
// accepted the mutation so the precondition must have held.
ObjectValue newData = value.clone();
Map<FieldPath, Value> transformResults = serverTransformResults(document, mutationResult.getTransformResults());
newData.setAll(transformResults);
document.convertToFoundDocument(mutationResult.getVersion(), newData).setHasCommittedMutations();
}
Aggregations