|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore.model.protovalue; |
| 16 | + |
| 17 | +import static com.google.firebase.firestore.util.Assert.hardAssert; |
| 18 | +import static com.google.firebase.firestore.util.ValueUtil.isType; |
| 19 | + |
| 20 | +import androidx.annotation.Nullable; |
| 21 | +import com.google.firebase.firestore.model.FieldPath; |
| 22 | +import com.google.firebase.firestore.model.mutation.FieldMask; |
| 23 | +import com.google.firebase.firestore.model.value.FieldValue; |
| 24 | +import com.google.firestore.v1.MapValue; |
| 25 | +import com.google.firestore.v1.Value; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.SortedMap; |
| 31 | +import java.util.TreeMap; |
| 32 | + |
| 33 | +// TODO(mrschmidt): Rename to DocumentValue |
| 34 | +public class ObjectValue extends PrimitiveValue { |
| 35 | + private static final ObjectValue EMPTY_MAP_VALUE = |
| 36 | + new ObjectValue( |
| 37 | + com.google.firestore.v1.Value.newBuilder() |
| 38 | + .setMapValue(com.google.firestore.v1.MapValue.getDefaultInstance()) |
| 39 | + .build()); |
| 40 | + |
| 41 | + public static ObjectValue emptyObject() { |
| 42 | + return EMPTY_MAP_VALUE; |
| 43 | + } |
| 44 | + |
| 45 | + public ObjectValue(Value value) { |
| 46 | + super(value); |
| 47 | + hardAssert(isType(value, TYPE_ORDER_OBJECT), "ObjectValues must be backed by a MapValue"); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns the value at the given path or null. |
| 52 | + * |
| 53 | + * @param fieldPath the path to search |
| 54 | + * @return The value at the path or if there it doesn't exist. |
| 55 | + */ |
| 56 | + public @Nullable FieldValue get(FieldPath fieldPath) { |
| 57 | + Value value = internalValue; |
| 58 | + |
| 59 | + for (int i = 0; i < fieldPath.length() - 1; ++i) { |
| 60 | + value = value.getMapValue().getFieldsMap().get(fieldPath.getSegment(i)); |
| 61 | + if (!isType(value, TYPE_ORDER_OBJECT)) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return value.getMapValue().containsFields(fieldPath.getLastSegment()) |
| 67 | + ? FieldValue.of(value.getMapValue().getFieldsOrThrow(fieldPath.getLastSegment())) |
| 68 | + : null; |
| 69 | + } |
| 70 | + |
| 71 | + /** Recursively extracts the FieldPaths that are set in this ObjectValue. */ |
| 72 | + public FieldMask getFieldMask() { |
| 73 | + return extractFieldMask(internalValue.getMapValue()); |
| 74 | + } |
| 75 | + |
| 76 | + private FieldMask extractFieldMask(MapValue value) { |
| 77 | + Set<FieldPath> fields = new HashSet<>(); |
| 78 | + for (Map.Entry<String, Value> entry : value.getFieldsMap().entrySet()) { |
| 79 | + FieldPath currentPath = FieldPath.fromSingleSegment(entry.getKey()); |
| 80 | + Value child = entry.getValue(); |
| 81 | + if (isType(child, TYPE_ORDER_OBJECT)) { |
| 82 | + FieldMask nestedMask = extractFieldMask(child.getMapValue()); |
| 83 | + Set<FieldPath> nestedFields = nestedMask.getMask(); |
| 84 | + if (nestedFields.isEmpty()) { |
| 85 | + // Preserve the empty map by adding it to the FieldMask. |
| 86 | + fields.add(currentPath); |
| 87 | + } else { |
| 88 | + // For nested and non-empty ObjectValues, add the FieldPath of the leaf nodes. |
| 89 | + for (FieldPath nestedPath : nestedFields) { |
| 90 | + fields.add(currentPath.append(nestedPath)); |
| 91 | + } |
| 92 | + } |
| 93 | + } else { |
| 94 | + fields.add(currentPath); |
| 95 | + } |
| 96 | + } |
| 97 | + return FieldMask.fromSet(fields); |
| 98 | + } |
| 99 | + |
| 100 | + /** Creates a ObjectValue.Builder instance that is based on the current value. */ |
| 101 | + public ObjectValue.Builder toBuilder() { |
| 102 | + return new Builder(this); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * An ObjectValue.Builder provides APIs to set and delete fields from an ObjectValue. All |
| 107 | + * operations mutate the existing instance. |
| 108 | + */ |
| 109 | + public static class Builder { |
| 110 | + |
| 111 | + /** The existing data to mutate. */ |
| 112 | + private ObjectValue baseObject; |
| 113 | + |
| 114 | + /** |
| 115 | + * A list of FieldPath:Value pairs to apply to the base object. `null` values indicate field |
| 116 | + * deletes. All MapValues are expanded and contain an entry for each leaf node. |
| 117 | + */ |
| 118 | + private SortedMap<FieldPath, Value> overlayMap; |
| 119 | + |
| 120 | + Builder(ObjectValue baseObject) { |
| 121 | + this.baseObject = baseObject; |
| 122 | + this.overlayMap = new TreeMap<>(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Sets the field to the provided value. |
| 127 | + * |
| 128 | + * @param path The field path to set. |
| 129 | + * @param value The value to set. |
| 130 | + * @return The current Builder instance. |
| 131 | + */ |
| 132 | + public Builder set(FieldPath path, Value value) { |
| 133 | + hardAssert(!path.isEmpty(), "Cannot set field for empty path on ObjectValue"); |
| 134 | + removeConflictingOverlays(path); |
| 135 | + setOverlay(path, value); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Removes the field at the current path. If there is no field at the specified path nothing is |
| 141 | + * changed. |
| 142 | + * |
| 143 | + * @param path The field path to remove |
| 144 | + * @return The current Builder instance. |
| 145 | + */ |
| 146 | + public Builder delete(FieldPath path) { |
| 147 | + hardAssert(!path.isEmpty(), "Cannot delete field for empty path on ObjectValue"); |
| 148 | + removeConflictingOverlays(path); |
| 149 | + setOverlay(path, null); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** Remove any existing overlays that will be replaced by setting `path` to a new value. */ |
| 154 | + private void removeConflictingOverlays(FieldPath path) { |
| 155 | + Iterator<FieldPath> iterator = |
| 156 | + overlayMap.subMap(path, createSuccessor(path)).keySet().iterator(); |
| 157 | + while (iterator.hasNext()) { |
| 158 | + iterator.next(); |
| 159 | + iterator.remove(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Adds `value` to the overlay map at `path`. MapValues are recursively expanded into one |
| 165 | + * overlay per leaf node. |
| 166 | + */ |
| 167 | + private void setOverlay(FieldPath path, @Nullable Value value) { |
| 168 | + if (!isType(value, TYPE_ORDER_OBJECT)) { |
| 169 | + overlayMap.put(path, value); |
| 170 | + } else { |
| 171 | + for (Map.Entry<String, Value> entry : value.getMapValue().getFieldsMap().entrySet()) { |
| 172 | + setOverlay(path.append(entry.getKey()), entry.getValue()); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public ObjectValue build() { |
| 178 | + if (overlayMap.isEmpty()) { |
| 179 | + return baseObject; |
| 180 | + } else { |
| 181 | + MapValue.Builder result = baseObject.internalValue.getMapValue().toBuilder(); |
| 182 | + setNested(result, FieldPath.EMPTY_PATH); |
| 183 | + return new ObjectValue(Value.newBuilder().setMapValue(result).build()); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private boolean setNested(MapValue.Builder nestedResult, FieldPath currentPath) { |
| 188 | + SortedMap<FieldPath, Value> currentSlice = |
| 189 | + currentPath.isEmpty() |
| 190 | + ? overlayMap |
| 191 | + : overlayMap.subMap(currentPath, createSuccessor(currentPath)); |
| 192 | + |
| 193 | + boolean modified = false; |
| 194 | + |
| 195 | + while (!currentSlice.isEmpty()) { |
| 196 | + FieldPath fieldPath = currentSlice.firstKey(); |
| 197 | + Value value = currentSlice.get(fieldPath); |
| 198 | + |
| 199 | + if (fieldPath.length() == currentPath.length() + 1) { |
| 200 | + String fieldName = fieldPath.getLastSegment(); |
| 201 | + if (value != null) { |
| 202 | + nestedResult.putFields(fieldName, value); |
| 203 | + modified = true; |
| 204 | + } else if (nestedResult.containsFields(fieldName)) { |
| 205 | + nestedResult.removeFields(fieldName); |
| 206 | + modified = true; |
| 207 | + } |
| 208 | + } else { |
| 209 | + FieldPath nextSliceStart = fieldPath.keepFirst(currentPath.length() + 1); |
| 210 | + @Nullable FieldValue existingValue = baseObject.get(nextSliceStart); |
| 211 | + MapValue.Builder nextSliceBuilder = |
| 212 | + existingValue instanceof ObjectValue |
| 213 | + ? ((ObjectValue) existingValue).internalValue.getMapValue().toBuilder() |
| 214 | + : MapValue.newBuilder(); |
| 215 | + modified = setNested(nextSliceBuilder, nextSliceStart) || modified; |
| 216 | + if (modified) { |
| 217 | + nestedResult.putFields( |
| 218 | + nextSliceStart.getLastSegment(), |
| 219 | + Value.newBuilder().setMapValue(nextSliceBuilder).build()); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + currentSlice = currentSlice.tailMap(createSuccessor(fieldPath)); |
| 224 | + } |
| 225 | + |
| 226 | + return modified; |
| 227 | + } |
| 228 | + |
| 229 | + private FieldPath createSuccessor(FieldPath currentPath) { |
| 230 | + return currentPath.popLast().append(currentPath.getLastSegment() + '0'); |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments