use of com.google.firebase.firestore.model.mutation.FieldMask in project firebase-android-sdk by firebase.
the class LocalDocumentsView method recalculateAndSaveOverlays.
private void recalculateAndSaveOverlays(Map<DocumentKey, MutableDocument> docs) {
List<MutationBatch> batches = mutationQueue.getAllMutationBatchesAffectingDocumentKeys(docs.keySet());
Map<DocumentKey, FieldMask> masks = new HashMap<>();
// A reverse lookup map from batch id to the documents within that batch.
TreeMap<Integer, Set<DocumentKey>> documentsByBatchId = new TreeMap<>();
// along the way.
for (MutationBatch batch : batches) {
for (DocumentKey key : batch.getKeys()) {
MutableDocument baseDoc = docs.get(key);
if (baseDoc == null) {
// If this batch has documents not included in passed in `docs`, skip them.
continue;
}
FieldMask mask = masks.containsKey(key) ? masks.get(key) : FieldMask.EMPTY;
mask = batch.applyToLocalView(baseDoc, mask);
masks.put(key, mask);
int batchId = batch.getBatchId();
if (!documentsByBatchId.containsKey(batchId)) {
documentsByBatchId.put(batchId, new HashSet<>());
}
documentsByBatchId.get(batchId).add(key);
}
}
Set<DocumentKey> processed = new HashSet<>();
// Iterate in descending order of batch ids, skip documents that are already saved.
for (Map.Entry<Integer, Set<DocumentKey>> entry : documentsByBatchId.descendingMap().entrySet()) {
Map<DocumentKey, Mutation> overlays = new HashMap<>();
for (DocumentKey key : entry.getValue()) {
if (!processed.contains(key)) {
overlays.put(key, Mutation.calculateOverlayMutation(docs.get(key), masks.get(key)));
processed.add(key);
}
}
documentOverlayCache.saveOverlays(entry.getKey(), overlays);
}
}
use of com.google.firebase.firestore.model.mutation.FieldMask 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);
}
use of com.google.firebase.firestore.model.mutation.FieldMask in project firebase-android-sdk by firebase.
the class ObjectValueTest method testExtractsFieldMask.
@Test
public void testExtractsFieldMask() {
ObjectValue val = wrapObject("a", "b", "map", map("a", 1, "b", true, "c", "string", "nested", map("d", "e")), "emptymap", map());
FieldMask mask = val.getFieldMask();
assertEquals(fieldMask("a", "map.a", "map.b", "map.c", "map.nested.d", "emptymap"), mask);
}
Aggregations