Search in sources :

Example 1 with MapValue

use of com.google.firestore.v1.MapValue in project firebase-android-sdk by firebase.

the class Values method objectEquals.

private static boolean objectEquals(Value left, Value right) {
    MapValue leftMap = left.getMapValue();
    MapValue rightMap = right.getMapValue();
    if (leftMap.getFieldsCount() != rightMap.getFieldsCount()) {
        return false;
    }
    for (Map.Entry<String, Value> entry : leftMap.getFieldsMap().entrySet()) {
        Value otherEntry = rightMap.getFieldsMap().get(entry.getKey());
        if (!equals(entry.getValue(), otherEntry)) {
            return false;
        }
    }
    return true;
}
Also used : MapValue(com.google.firestore.v1.MapValue) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) NullValue(com.google.protobuf.NullValue) MapValue(com.google.firestore.v1.MapValue) ByteString(com.google.protobuf.ByteString) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 2 with MapValue

use of com.google.firestore.v1.MapValue in project firebase-android-sdk by firebase.

the class FirestoreIndexValueWriter method writeIndexMap.

private void writeIndexMap(MapValue mapIndexValue, DirectionalIndexByteEncoder encoder) {
    writeValueTypeLabel(encoder, INDEX_TYPE_MAP);
    for (Map.Entry<String, Value> entry : mapIndexValue.getFieldsMap().entrySet()) {
        String key = entry.getKey();
        Value value = entry.getValue();
        writeIndexString(key, encoder);
        writeIndexValueAux(value, encoder);
    }
}
Also used : Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) MapValue(com.google.firestore.v1.MapValue) Map(java.util.Map)

Example 3 with MapValue

use of com.google.firestore.v1.MapValue in project firebase-android-sdk by firebase.

the class ObjectValue method buildProto.

/**
 * Returns the Protobuf that backs this ObjectValue.
 *
 * <p>This method applies any outstanding modifications and memoizes the result. Further
 * invocations are based on this memoized result.
 */
private Value buildProto() {
    synchronized (overlayMap) {
        MapValue mergedResult = applyOverlay(FieldPath.EMPTY_PATH, overlayMap);
        if (mergedResult != null) {
            partialValue = Value.newBuilder().setMapValue(mergedResult).build();
            overlayMap.clear();
        }
    }
    return partialValue;
}
Also used : MapValue(com.google.firestore.v1.MapValue)

Example 4 with MapValue

use of com.google.firestore.v1.MapValue in project firebase-android-sdk by firebase.

the class ObjectValue method extractFieldMask.

private FieldMask extractFieldMask(MapValue value) {
    Set<FieldPath> fields = new HashSet<>();
    for (Map.Entry<String, Value> entry : value.getFieldsMap().entrySet()) {
        FieldPath currentPath = FieldPath.fromSingleSegment(entry.getKey());
        if (Values.isMapValue(entry.getValue())) {
            FieldMask nestedMask = extractFieldMask(entry.getValue().getMapValue());
            Set<FieldPath> nestedFields = nestedMask.getMask();
            if (nestedFields.isEmpty()) {
                // Preserve the empty map by adding it to the FieldMask.
                fields.add(currentPath);
            } else {
                // For nested and non-empty ObjectValues, add the FieldPath of the leaf nodes.
                for (FieldPath nestedPath : nestedFields) {
                    fields.add(currentPath.append(nestedPath));
                }
            }
        } else {
            fields.add(currentPath);
        }
    }
    return FieldMask.fromSet(fields);
}
Also used : Value(com.google.firestore.v1.Value) MapValue(com.google.firestore.v1.MapValue) Map(java.util.Map) HashMap(java.util.HashMap) FieldMask(com.google.firebase.firestore.model.mutation.FieldMask) HashSet(java.util.HashSet)

Example 5 with MapValue

use of com.google.firestore.v1.MapValue in project firebase-android-sdk by firebase.

the class ObjectValue method applyOverlay.

/**
 * Applies any overlays from {@code currentOverlays} that exist at `currentPath` and returns the
 * merged data at {@code currentPath} (or {@code null} if there were no changes).
 *
 * @param currentPath The path at the current nesting level. Can be set to {@code
 *     FieldValue.EMPTY_PATH} to represent the root.
 * @param currentOverlays The overlays at the current nesting level in the same format as {@code
 *     overlayMap}.
 * @return The merged data at `currentPath` or null if no modifications were applied.
 */
@Nullable
private MapValue applyOverlay(FieldPath currentPath, Map<String, Object> currentOverlays) {
    boolean modified = false;
    @Nullable Value existingValue = extractNestedValue(partialValue, currentPath);
    MapValue.Builder resultAtPath = Values.isMapValue(existingValue) ? // of the existing data.
    existingValue.getMapValue().toBuilder() : MapValue.newBuilder();
    for (Map.Entry<String, Object> entry : currentOverlays.entrySet()) {
        String pathSegment = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof Map) {
            @Nullable MapValue nested = applyOverlay(currentPath.append(pathSegment), (Map<String, Object>) value);
            if (nested != null) {
                resultAtPath.putFields(pathSegment, Value.newBuilder().setMapValue(nested).build());
                modified = true;
            }
        } else if (value instanceof Value) {
            resultAtPath.putFields(pathSegment, (Value) value);
            modified = true;
        } else if (resultAtPath.containsFields(pathSegment)) {
            hardAssert(value == null, "Expected entry to be a Map, a Value or null");
            resultAtPath.removeFields(pathSegment);
            modified = true;
        }
    }
    return modified ? resultAtPath.build() : null;
}
Also used : Value(com.google.firestore.v1.Value) MapValue(com.google.firestore.v1.MapValue) MapValue(com.google.firestore.v1.MapValue) Map(java.util.Map) HashMap(java.util.HashMap) Nullable(androidx.annotation.Nullable) Nullable(androidx.annotation.Nullable)

Aggregations

MapValue (com.google.firestore.v1.MapValue)6 Value (com.google.firestore.v1.Value)5 Map (java.util.Map)4 ArrayValue (com.google.firestore.v1.ArrayValue)3 NullValue (com.google.protobuf.NullValue)2 HashMap (java.util.HashMap)2 Nullable (androidx.annotation.Nullable)1 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 FieldMask (com.google.firebase.firestore.model.mutation.FieldMask)1 ByteString (com.google.protobuf.ByteString)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1