Skip to content

Commit a88a8ba

Browse files
committed
Not working yet.
1 parent 060f4d3 commit a88a8ba

File tree

17 files changed

+128
-317
lines changed

17 files changed

+128
-317
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/SetOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static SetOptions mergeFields(@NonNull List<String> fields) {
8484
fieldPaths.add(FieldPath.fromDotSeparatedPath(field).getInternalPath());
8585
}
8686

87-
return new SetOptions(true, FieldMask.fromSet(fieldPaths));
87+
return new SetOptions(true, FieldMask.someFieldsMask(fieldPaths));
8888
}
8989

9090
/**
@@ -105,7 +105,7 @@ public static SetOptions mergeFields(String... fields) {
105105
fieldPaths.add(FieldPath.fromDotSeparatedPath(field).getInternalPath());
106106
}
107107

108-
return new SetOptions(true, FieldMask.fromSet(fieldPaths));
108+
return new SetOptions(true, FieldMask.someFieldsMask(fieldPaths));
109109
}
110110

111111
/**
@@ -125,7 +125,7 @@ public static SetOptions mergeFieldPaths(@NonNull List<FieldPath> fields) {
125125
fieldPaths.add(field.getInternalPath());
126126
}
127127

128-
return new SetOptions(true, FieldMask.fromSet(fieldPaths));
128+
return new SetOptions(true, FieldMask.someFieldsMask(fieldPaths));
129129
}
130130

131131
@Override

firebase-firestore/src/main/java/com/google/firebase/firestore/core/UserData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void addToFieldTransforms(FieldPath fieldPath, TransformOperation transformOpera
138138
*/
139139
public ParsedSetData toMergeData(ObjectValue data) {
140140
return new ParsedSetData(
141-
data, FieldMask.fromSet(fieldMask), unmodifiableList(fieldTransforms));
141+
data, FieldMask.someFieldsMask(fieldMask), unmodifiableList(fieldTransforms));
142142
}
143143

144144
/**
@@ -184,7 +184,7 @@ public ParsedSetData toSetData(ObjectValue data) {
184184
*/
185185
public ParsedUpdateData toUpdateData(ObjectValue data) {
186186
return new ParsedUpdateData(
187-
data, FieldMask.fromSet(fieldMask), unmodifiableList(fieldTransforms));
187+
data, FieldMask.someFieldsMask(fieldMask), unmodifiableList(fieldTransforms));
188188
}
189189
}
190190

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
1919

2020
import androidx.annotation.VisibleForTesting;
21-
import com.google.firebase.Timestamp;
21+
2222
import com.google.firebase.database.collection.ImmutableSortedMap;
2323
import com.google.firebase.firestore.core.Query;
2424
import com.google.firebase.firestore.model.Document;
2525
import com.google.firebase.firestore.model.DocumentKey;
2626
import com.google.firebase.firestore.model.MutableDocument;
2727
import com.google.firebase.firestore.model.ResourcePath;
2828
import com.google.firebase.firestore.model.SnapshotVersion;
29-
import com.google.firebase.firestore.model.mutation.EmptyMutation;
29+
import com.google.firebase.firestore.model.mutation.FieldMask;
3030
import com.google.firebase.firestore.model.mutation.Mutation;
3131
import com.google.firebase.firestore.model.mutation.MutationBatch;
3232
import com.google.firebase.firestore.model.mutation.PatchMutation;
33-
import com.google.firebase.firestore.model.mutation.Precondition;
34-
import java.util.HashMap;
33+
3534
import java.util.HashSet;
3635
import java.util.List;
3736
import java.util.Map;
@@ -199,7 +198,6 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
199198

200199
remoteDocuments = addMissingBaseDocuments(matchingBatches, remoteDocuments);
201200

202-
Map<DocumentKey, Mutation> squashedMutations = new HashMap<>();
203201
for (MutationBatch batch : matchingBatches) {
204202
for (Mutation mutation : batch.getMutations()) {
205203
// Only process documents belonging to the collection.
@@ -214,19 +212,11 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
214212
document = MutableDocument.newInvalidDocument(key);
215213
remoteDocuments = remoteDocuments.insert(key, document);
216214
}
217-
Mutation squashed =
218-
squashedMutations.containsKey(key)
219-
? squashedMutations.get(key)
220-
: new EmptyMutation(key, Precondition.NONE);
221-
squashedMutations.put(key, mutation.squash(squashed, document, batch.getLocalWriteTime()));
222-
}
223-
}
224-
225-
for (Map.Entry<DocumentKey, Mutation> entry : squashedMutations.entrySet()) {
226-
MutableDocument document = remoteDocuments.get(entry.getKey());
227-
entry.getValue().applyToLocalView(document, Timestamp.now());
228-
if (!document.isFoundDocument()) {
229-
remoteDocuments = remoteDocuments.remove(entry.getKey());
215+
// TODO(Overlay): Here we should be reading squashed mutation and apply that instead.
216+
mutation.applyToLocalView(document, batch.getLocalWriteTime(), FieldMask.someFieldsMask(new HashSet<>()));
217+
if (!document.isFoundDocument()) {
218+
remoteDocuments = remoteDocuments.remove(key);
219+
}
230220
}
231221
}
232222

firebase-firestore/src/main/java/com/google/firebase/firestore/model/ObjectValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private FieldMask extractFieldMask(MapValue value) {
8989
fields.add(currentPath);
9090
}
9191
}
92-
return FieldMask.fromSet(fields);
92+
return FieldMask.someFieldsMask(fields);
9393
}
9494

9595
/**

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/DeleteMutation.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import androidx.annotation.Nullable;
2020
import com.google.firebase.Timestamp;
2121
import com.google.firebase.firestore.model.DocumentKey;
22-
import com.google.firebase.firestore.model.FieldPath;
2322
import com.google.firebase.firestore.model.MutableDocument;
2423
import com.google.firebase.firestore.model.ObjectValue;
2524
import com.google.firebase.firestore.model.SnapshotVersion;
@@ -71,27 +70,15 @@ public void applyToRemoteDocument(MutableDocument document, MutationResult mutat
7170
}
7271

7372
@Override
74-
public void applyToLocalView(MutableDocument document, Timestamp localWriteTime) {
73+
public FieldMask applyToLocalView(MutableDocument document, Timestamp localWriteTime, FieldMask mask) {
7574
verifyKeyMatches(document);
7675

7776
if (getPrecondition().isValidFor(document)) {
7877
document.convertToNoDocument(SnapshotVersion.NONE);
78+
return FieldMask.allFieldsMask();
7979
}
80-
}
8180

82-
@Override
83-
public Mutation squash(
84-
Mutation baseMutation, MutableDocument document, Timestamp localWriteTime) {
85-
if (getPrecondition().isValidFor(document)) {
86-
return this;
87-
} else {
88-
return baseMutation;
89-
}
90-
}
91-
92-
@Override
93-
protected FieldUpdate getFieldUpdate(FieldPath fieldPath) {
94-
return new FieldUpdate(FieldUpdate.Type.DELETE, null);
81+
return mask;
9582
}
9683

9784
@Nullable

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/EmptyMutation.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/FieldMask.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package com.google.firebase.firestore.model.mutation;
1616

1717
import com.google.firebase.firestore.model.FieldPath;
18+
19+
import java.util.HashSet;
1820
import java.util.Set;
1921

2022
/**
@@ -26,14 +28,29 @@
2628
* object foo. If foo is not an object, foo is replaced with an object containing foo.
2729
*/
2830
public final class FieldMask {
29-
public static FieldMask fromSet(Set<FieldPath> mask) {
30-
return new FieldMask(mask);
31+
public static FieldMask someFieldsMask(Set<FieldPath> mask) {
32+
return new FieldMask(mask, Scope.Some);
33+
}
34+
35+
public static FieldMask allFieldsMask() {
36+
return new FieldMask(new HashSet<FieldPath>(), Scope.All_Fields);
37+
}
38+
39+
public static FieldMask emptyMask() {
40+
return new FieldMask(new HashSet<FieldPath>(), Scope.None);
3141
}
3242

3343
private final Set<FieldPath> mask;
44+
private enum Scope{
45+
All_Fields,
46+
None,
47+
Some
48+
}
49+
private Scope scope = Scope.None;
3450

35-
private FieldMask(Set<FieldPath> mask) {
51+
private FieldMask(Set<FieldPath> mask, Scope scope) {
3652
this.mask = mask;
53+
this.scope = scope;
3754
}
3855

3956
@Override
@@ -77,4 +94,16 @@ public int hashCode() {
7794
public Set<FieldPath> getMask() {
7895
return mask;
7996
}
97+
98+
public boolean isAllFields() {
99+
return scope == Scope.All_Fields;
100+
}
101+
102+
public boolean isNoneFields() {
103+
return scope == Scope.None;
104+
}
105+
106+
public boolean isSomeFields() {
107+
return scope == Scope.Some;
108+
}
80109
}

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/Mutation.java

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,10 @@ public abstract void applyToRemoteDocument(
113113
* @param document The document to mutate.
114114
* @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
115115
* a part of.
116+
* @param mask The fields that have been updated before applying this mutation.
117+
* @return A {@code FieldMask} representing the fields that are changed by applying this mutation.
116118
*/
117-
public abstract void applyToLocalView(MutableDocument document, Timestamp localWriteTime);
118-
119-
/**
120-
* Builds a new mutation that would do the same as applying {@code baseMutation}, then {@code
121-
* this} on the given document. Consider this as a git rebase operation.
122-
*
123-
* <p>One thing to note is the returned mutation should never have any un-applied transforms, if
124-
* {@code this} has outstanding transforms, they need to be applied first, and their final value
125-
* should be saved in the value of the returning mutation.
126-
*
127-
* @param baseMutation The mutation to first apply to the document. This is either {@code
128-
* EmptyMutation} or from previous calls to {@code squash}.
129-
* @param document The document for which this squashing is applied to.
130-
* @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
131-
* a part of.
132-
* @return A new mutation squashed mutation.
133-
*/
134-
public abstract Mutation squash(
135-
Mutation baseMutation, MutableDocument document, Timestamp localWriteTime);
119+
public abstract FieldMask applyToLocalView(MutableDocument document, Timestamp localWriteTime, FieldMask mask);
136120

137121
/** Helper for derived classes to implement .equals(). */
138122
boolean hasSameKeyAndPrecondition(Mutation other) {
@@ -213,43 +197,12 @@ protected Map<FieldPath, Value> localTransformResults(
213197
for (FieldTransform fieldTransform : fieldTransforms) {
214198
TransformOperation transform = fieldTransform.getOperation();
215199
Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
216-
if (mutation != null && !(mutation instanceof EmptyMutation)) {
217-
FieldUpdate update = mutation.getFieldUpdate(fieldTransform.getFieldPath());
218-
if (update.type == FieldUpdate.Type.DELETE) {
219-
previousValue = null;
220-
} else if (update.type == FieldUpdate.Type.SET) {
221-
previousValue = update.value;
222-
}
223-
}
224200
transformResults.put(
225201
fieldTransform.getFieldPath(), transform.applyToLocalView(previousValue, localWriteTime));
226202
}
227203
return transformResults;
228204
}
229205

230-
/** A class specifying what a mutation would do to a field in a document. */
231-
protected static class FieldUpdate {
232-
protected enum Type {
233-
// This mutation would do nothing.
234-
ABSENT,
235-
// This mutation would delete the field in question.
236-
DELETE,
237-
// This mutation would set the field in question to a value.
238-
SET,
239-
}
240-
241-
private Type type;
242-
private Value value;
243-
244-
public FieldUpdate(Type type, Value value) {
245-
this.type = type;
246-
this.value = value;
247-
}
248-
}
249-
250-
/** Returns what this mutation would do to a given {@code FieldPath}. */
251-
protected abstract FieldUpdate getFieldUpdate(FieldPath fieldPath);
252-
253206
protected List<FieldPath> getFieldTransformPaths() {
254207
List<FieldPath> result = new ArrayList<>();
255208
for (FieldTransform fieldTransform : fieldTransforms) {

0 commit comments

Comments
 (0)