Search in sources :

Example 1 with FieldMask

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);
    }
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MutableDocument(com.google.firebase.firestore.model.MutableDocument) TreeMap(java.util.TreeMap) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) DocumentKey(com.google.firebase.firestore.model.DocumentKey) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap) FieldMask(com.google.firebase.firestore.model.mutation.FieldMask) HashSet(java.util.HashSet)

Example 2 with FieldMask

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);
}
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 3 with FieldMask

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);
}
Also used : FieldMask(com.google.firebase.firestore.model.mutation.FieldMask) Test(org.junit.Test)

Aggregations

FieldMask (com.google.firebase.firestore.model.mutation.FieldMask)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)1 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 Mutation (com.google.firebase.firestore.model.mutation.Mutation)1 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)1 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)1 MapValue (com.google.firestore.v1.MapValue)1 Value (com.google.firestore.v1.Value)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1