use of com.google.firebase.firestore.model.FieldPath 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.firebase.firestore.model.FieldPath 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.
*/
@Nullable
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.FieldPath in project firebase-android-sdk by firebase.
the class Mutation method serverTransformResults.
/**
* Creates a list of "transform results" (a transform result is a field value representing the
* result of applying a transform) for use after a mutation containing transforms has been
* acknowledged by the server.
*
* @param mutableDocument The current state of the document after applying all previous mutations.
* @param serverTransformResults The transform results received by the server.
* @return A map of fields to transform results.
*/
protected Map<FieldPath, Value> serverTransformResults(MutableDocument mutableDocument, List<Value> serverTransformResults) {
Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
hardAssert(fieldTransforms.size() == serverTransformResults.size(), "server transform count (%d) should match field transform count (%d)", serverTransformResults.size(), fieldTransforms.size());
for (int i = 0; i < serverTransformResults.size(); i++) {
FieldTransform fieldTransform = fieldTransforms.get(i);
TransformOperation transform = fieldTransform.getOperation();
Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
transformResults.put(fieldTransform.getFieldPath(), transform.applyToRemoteDocument(previousValue, serverTransformResults.get(i)));
}
return transformResults;
}
use of com.google.firebase.firestore.model.FieldPath in project firebase-android-sdk by firebase.
the class BundleSerializer method decodeUnaryFilter.
private void decodeUnaryFilter(List<Filter> result, JSONObject unaryFilter) throws JSONException {
FieldPath fieldPath = decodeFieldReference(unaryFilter.getJSONObject("field"));
String operator = unaryFilter.getString("op");
switch(operator) {
case "IS_NAN":
result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.EQUAL, Values.NAN_VALUE));
break;
case "IS_NULL":
result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.EQUAL, Values.NULL_VALUE));
break;
case "IS_NOT_NAN":
result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.NOT_EQUAL, Values.NAN_VALUE));
break;
case "IS_NOT_NULL":
result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.NOT_EQUAL, Values.NULL_VALUE));
break;
default:
throw new IllegalArgumentException("Unexpected unary filter: " + operator);
}
}
use of com.google.firebase.firestore.model.FieldPath in project firebase-android-sdk by firebase.
the class BundleSerializer method decodeOrderBy.
private List<OrderBy> decodeOrderBy(@Nullable JSONArray orderBys) throws JSONException {
List<OrderBy> result = new ArrayList<>();
if (orderBys != null) {
for (int i = 0; i < orderBys.length(); ++i) {
JSONObject orderBy = orderBys.getJSONObject(i);
FieldPath fieldPath = decodeFieldReference(orderBy.getJSONObject("field"));
String directionString = orderBy.optString("direction", "ASCENDING");
OrderBy.Direction direction = directionString.equals("ASCENDING") ? OrderBy.Direction.ASCENDING : OrderBy.Direction.DESCENDING;
result.add(OrderBy.getInstance(direction, fieldPath));
}
}
return result;
}
Aggregations