use of com.google.firestore.v1.DocumentTransform.FieldTransform in project firebase-android-sdk by firebase.
the class LocalSerializer method decodeMutationBatch.
/**
* Decodes a WriteBatch proto into a MutationBatch model.
*/
MutationBatch decodeMutationBatch(com.google.firebase.firestore.proto.WriteBatch batch) {
int batchId = batch.getBatchId();
Timestamp localWriteTime = rpcSerializer.decodeTimestamp(batch.getLocalWriteTime());
int baseMutationsCount = batch.getBaseWritesCount();
List<Mutation> baseMutations = new ArrayList<>(baseMutationsCount);
for (int i = 0; i < baseMutationsCount; i++) {
baseMutations.add(rpcSerializer.decodeMutation(batch.getBaseWrites(i)));
}
List<Mutation> mutations = new ArrayList<>(batch.getWritesCount());
// TODO(b/174608374): Remove this code once we perform a schema migration.
for (int i = 0; i < batch.getWritesCount(); ++i) {
Write currentMutation = batch.getWrites(i);
boolean hasTransform = i + 1 < batch.getWritesCount() && batch.getWrites(i + 1).hasTransform();
if (hasTransform) {
hardAssert(batch.getWrites(i).hasUpdate(), "TransformMutation should be preceded by a patch or set mutation");
Builder newMutationBuilder = Write.newBuilder(currentMutation);
Write transformMutation = batch.getWrites(i + 1);
for (FieldTransform fieldTransform : transformMutation.getTransform().getFieldTransformsList()) {
newMutationBuilder.addUpdateTransforms(fieldTransform);
}
mutations.add(rpcSerializer.decodeMutation(newMutationBuilder.build()));
++i;
} else {
mutations.add(rpcSerializer.decodeMutation(currentMutation));
}
}
return new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
}
use of com.google.firestore.v1.DocumentTransform.FieldTransform in project firebase-android-sdk by firebase.
the class RemoteSerializer method decodeMutation.
public Mutation decodeMutation(com.google.firestore.v1.Write mutation) {
Precondition precondition = mutation.hasCurrentDocument() ? decodePrecondition(mutation.getCurrentDocument()) : Precondition.NONE;
List<FieldTransform> fieldTransforms = new ArrayList<>();
for (DocumentTransform.FieldTransform fieldTransform : mutation.getUpdateTransformsList()) {
fieldTransforms.add(decodeFieldTransform(fieldTransform));
}
switch(mutation.getOperationCase()) {
case UPDATE:
if (mutation.hasUpdateMask()) {
return new PatchMutation(decodeKey(mutation.getUpdate().getName()), ObjectValue.fromMap(mutation.getUpdate().getFieldsMap()), decodeDocumentMask(mutation.getUpdateMask()), precondition, fieldTransforms);
} else {
return new SetMutation(decodeKey(mutation.getUpdate().getName()), ObjectValue.fromMap(mutation.getUpdate().getFieldsMap()), precondition, fieldTransforms);
}
case DELETE:
return new DeleteMutation(decodeKey(mutation.getDelete()), precondition);
case VERIFY:
return new VerifyMutation(decodeKey(mutation.getVerify()), precondition);
default:
throw fail("Unknown mutation operation: %d", mutation.getOperationCase());
}
}
use of com.google.firestore.v1.DocumentTransform.FieldTransform 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.firestore.v1.DocumentTransform.FieldTransform in project firebase-android-sdk by firebase.
the class Mutation method localTransformResults.
/**
* Creates a list of "transform results" (a transform result is a field value representing the
* result of applying a transform) for use when applying a transform locally.
*
* @param localWriteTime The local time of the mutation (used to generate ServerTimestampValues).
* @param mutableDocument The document to apply transforms on.
* @return A map of fields to transform results.
*/
protected Map<FieldPath, Value> localTransformResults(Timestamp localWriteTime, MutableDocument mutableDocument) {
Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
for (FieldTransform fieldTransform : fieldTransforms) {
TransformOperation transform = fieldTransform.getOperation();
Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
transformResults.put(fieldTransform.getFieldPath(), transform.applyToLocalView(previousValue, localWriteTime));
}
return transformResults;
}
use of com.google.firestore.v1.DocumentTransform.FieldTransform in project java-firestore by googleapis.
the class LocalFirestoreHelper method transform.
public static List<FieldTransform> transform(String fieldPath, FieldTransform fieldTransform, Object... fieldPathOrTransform) {
List<FieldTransform> transforms = new ArrayList<>();
FieldTransform.Builder transformBuilder = FieldTransform.newBuilder();
transformBuilder.setFieldPath(fieldPath).mergeFrom(fieldTransform);
transforms.add(transformBuilder.build());
for (int i = 0; i < fieldPathOrTransform.length; i += 2) {
String path = (String) fieldPathOrTransform[i];
FieldTransform transform = (FieldTransform) fieldPathOrTransform[i + 1];
transforms.add(FieldTransform.newBuilder().setFieldPath(path).mergeFrom(transform).build());
}
return transforms;
}
Aggregations