Skip to content

Commit

Permalink
refactor: Optimize FieldMask instantiation (#1536)
Browse files Browse the repository at this point in the history
* Optimize FieldMask instantiation

* Pretty
  • Loading branch information
tom-andersen committed Jan 19, 2024
1 parent 594761d commit f41adf7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package com.google.cloud.firestore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
Expand Down Expand Up @@ -47,7 +45,7 @@ private FieldMask(SortedSet<FieldPath> fieldPaths) {
*/
@Nonnull
public static FieldMask of(String... fieldPaths) {
List<FieldPath> paths = new ArrayList<>();
TreeSet<FieldPath> paths = new TreeSet<>();
for (String fieldPath : fieldPaths) {
paths.add(FieldPath.fromDotSeparatedString(fieldPath));
}
Expand All @@ -62,16 +60,18 @@ public static FieldMask of(String... fieldPaths) {
*/
@Nonnull
public static FieldMask of(FieldPath... fieldPaths) {
return new FieldMask(Arrays.asList(fieldPaths));
TreeSet<FieldPath> paths = new TreeSet<>();
Collections.addAll(paths, fieldPaths);
return new FieldMask(paths);
}

static FieldMask fromObject(Map<String, Object> values) {
List<FieldPath> fieldPaths = extractFromMap(values, FieldPath.empty());
TreeSet<FieldPath> fieldPaths = extractFromMap(values, FieldPath.empty());
return new FieldMask(fieldPaths);
}

private static List<FieldPath> extractFromMap(Map<String, Object> values, FieldPath path) {
List<FieldPath> fieldPaths = new ArrayList<>();
private static TreeSet<FieldPath> extractFromMap(Map<String, Object> values, FieldPath path) {
TreeSet<FieldPath> fieldPaths = new TreeSet<>();

for (Map.Entry<String, Object> entry : values.entrySet()) {
Object value = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.cloud.firestore;

import static com.google.common.base.Predicates.not;
import static java.util.stream.Collectors.toCollection;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.InternalExtensionOnly;
Expand Down Expand Up @@ -272,9 +275,11 @@ private T performSet(
DocumentTransform.fromFieldPathMap(documentReference, documentData);

if (options.getFieldMask() != null) {
List<FieldPath> fieldMask = new ArrayList<>(options.getFieldMask());
fieldMask.removeAll(documentTransform.getFields());
documentMask = new FieldMask(fieldMask);
TreeSet<FieldPath> fieldPaths =
options.getFieldMask().stream()
.filter(not(documentTransform.getFields()::contains))
.collect(toCollection(TreeSet::new));
documentMask = new FieldMask(fieldPaths);
} else if (options.isMerge()) {
documentMask = FieldMask.fromObject(fields);
}
Expand Down Expand Up @@ -544,10 +549,12 @@ public boolean allowTransform() {
return true;
}
});
List<FieldPath> fieldPaths = new ArrayList<>(fields.keySet());
DocumentTransform documentTransform =
DocumentTransform.fromFieldPathMap(documentReference, fields);
fieldPaths.removeAll(documentTransform.getFields());
TreeSet<FieldPath> fieldPaths =
fields.keySet().stream()
.filter(not(documentTransform.getFields()::contains))
.collect(toCollection(TreeSet::new));
FieldMask fieldMask = new FieldMask(fieldPaths);

Write.Builder write = documentSnapshot.toPb();
Expand Down

0 comments on commit f41adf7

Please sign in to comment.