Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Optimize FieldMask instantiation #1536

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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