Skip to content

Commit 979000c

Browse files
Mutable Documents (#2383)
1 parent 52b1dca commit 979000c

File tree

78 files changed

+1707
-1988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1707
-1988
lines changed

firebase-firestore/ktx/src/test/java/com/google/firebase/firestore/TestUtil.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.firebase.firestore.model.Document;
2727
import com.google.firebase.firestore.model.DocumentKey;
2828
import com.google.firebase.firestore.model.DocumentSet;
29+
import com.google.firebase.firestore.model.MutableDocument;
2930
import com.google.firebase.firestore.model.ObjectValue;
3031
import java.util.ArrayList;
3132
import java.util.List;
@@ -42,8 +43,7 @@ public static FirebaseFirestore firestore() {
4243
public static DocumentSnapshot documentSnapshot(
4344
String path, Map<String, Object> data, boolean isFromCache) {
4445
if (data == null) {
45-
return DocumentSnapshot.fromNoDocument(
46-
FIRESTORE, key(path), isFromCache, /*hasPendingWrites=*/ false);
46+
return DocumentSnapshot.fromNoDocument(FIRESTORE, key(path), isFromCache);
4747
} else {
4848
return DocumentSnapshot.fromDocument(
4949
FIRESTORE, doc(path, 1L, data), isFromCache, /*hasPendingWrites=*/ false);
@@ -72,36 +72,23 @@ public static QuerySnapshot querySnapshot(
7272
Map<String, ObjectValue> docsToAdd,
7373
boolean hasPendingWrites,
7474
boolean isFromCache) {
75-
DocumentSet oldDocuments = docSet(Document.keyComparator());
75+
DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR);
7676
ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
7777
for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
7878
String docKey = path + "/" + pair.getKey();
79-
oldDocuments =
80-
oldDocuments.add(
81-
doc(
82-
docKey,
83-
1L,
84-
pair.getValue(),
85-
hasPendingWrites
86-
? Document.DocumentState.SYNCED
87-
: Document.DocumentState.LOCAL_MUTATIONS));
88-
79+
MutableDocument doc = doc(docKey, 1L, pair.getValue());
80+
if (hasPendingWrites) doc.setHasLocalMutations();
81+
oldDocuments = oldDocuments.add(doc);
8982
if (hasPendingWrites) {
9083
mutatedKeys = mutatedKeys.insert(key(docKey));
9184
}
9285
}
93-
DocumentSet newDocuments = docSet(Document.keyComparator());
86+
DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR);
9487
List<DocumentViewChange> documentChanges = new ArrayList<>();
9588
for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
9689
String docKey = path + "/" + pair.getKey();
97-
Document docToAdd =
98-
doc(
99-
docKey,
100-
1L,
101-
pair.getValue(),
102-
hasPendingWrites
103-
? Document.DocumentState.SYNCED
104-
: Document.DocumentState.LOCAL_MUTATIONS);
90+
MutableDocument docToAdd = doc(docKey, 1L, pair.getValue());
91+
if (hasPendingWrites) docToAdd.setHasLocalMutations();
10592
newDocuments = newDocuments.add(docToAdd);
10693
documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));
10794

firebase-firestore/ktx/src/test/java/com/google/firebase/firestore/testutil/TestUtil.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.firebase.firestore.model.Document;
2222
import com.google.firebase.firestore.model.DocumentKey;
2323
import com.google.firebase.firestore.model.DocumentSet;
24+
import com.google.firebase.firestore.model.MutableDocument;
2425
import com.google.firebase.firestore.model.ObjectValue;
2526
import com.google.firebase.firestore.model.ResourcePath;
2627
import com.google.firebase.firestore.model.SnapshotVersion;
@@ -62,28 +63,25 @@ public static SnapshotVersion version(long versionMicros) {
6263
return new SnapshotVersion(new Timestamp(seconds, nanos));
6364
}
6465

65-
public static Document doc(String key, long version, Map<String, Object> data) {
66-
return new Document(
67-
key(key), version(version), wrapObject(data), Document.DocumentState.SYNCED);
66+
public static MutableDocument doc(String key, long version, Map<String, Object> data) {
67+
return doc(key(key), version, wrapObject(data));
6868
}
6969

70-
public static Document doc(DocumentKey key, long version, Map<String, Object> data) {
71-
return new Document(key, version(version), wrapObject(data), Document.DocumentState.SYNCED);
70+
public static MutableDocument doc(DocumentKey key, long version, Map<String, Object> data) {
71+
return doc(key, version, wrapObject(data));
7272
}
7373

74-
public static Document doc(
75-
String key, long version, ObjectValue data, Document.DocumentState documentState) {
76-
return new Document(key(key), version(version), data, documentState);
74+
public static MutableDocument doc(String key, long version, ObjectValue data) {
75+
return doc(key(key), version, data);
7776
}
7877

79-
public static Document doc(
80-
String key, long version, Map<String, Object> data, Document.DocumentState documentState) {
81-
return new Document(key(key), version(version), wrapObject(data), documentState);
78+
public static MutableDocument doc(DocumentKey key, long version, ObjectValue data) {
79+
return new MutableDocument(key).convertToFoundDocument(version(version), data);
8280
}
8381

84-
public static DocumentSet docSet(Comparator<Document> comparator, Document... documents) {
82+
public static DocumentSet docSet(Comparator<Document> comparator, MutableDocument... documents) {
8583
DocumentSet set = DocumentSet.emptySet(comparator);
86-
for (Document document : documents) {
84+
for (MutableDocument document : documents) {
8785
set = set.add(document);
8886
}
8987
return set;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public Type getType() {
9898
*
9999
* @return A snapshot of the new data (for {@link DocumentChange.Type#ADDED} or {@link
100100
* DocumentChange.Type#MODIFIED}) or the removed data (for {@link
101-
* DocumentChange.Type.REMOVED}).
101+
* DocumentChange.Type#REMOVED}).
102102
*/
103103
@NonNull
104104
public QueryDocumentSnapshot getDocument() {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ private ListenerRegistration addSnapshotListenerInternal(
499499
} else {
500500
// We don't raise `hasPendingWrites` for deleted documents.
501501
documentSnapshot =
502-
DocumentSnapshot.fromNoDocument(
503-
firestore, key, snapshot.isFromCache(), /* hasPendingWrites= */ false);
502+
DocumentSnapshot.fromNoDocument(firestore, key, snapshot.isFromCache());
504503
}
505504
userListener.onEvent(documentSnapshot, null);
506505
};

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ static DocumentSnapshot fromDocument(
9595
}
9696

9797
static DocumentSnapshot fromNoDocument(
98-
FirebaseFirestore firestore, DocumentKey key, boolean fromCache, boolean hasPendingWrites) {
99-
return new DocumentSnapshot(firestore, key, null, fromCache, hasPendingWrites);
98+
FirebaseFirestore firestore, DocumentKey key, boolean fromCache) {
99+
return new DocumentSnapshot(firestore, key, null, fromCache, /* hasPendingWrites= */ false);
100100
}
101101

102102
/** @return The id of the document. */
@@ -534,7 +534,8 @@ public boolean equals(@Nullable Object obj) {
534534
public int hashCode() {
535535
int hash = firestore.hashCode();
536536
hash = hash * 31 + key.hashCode();
537-
hash = hash * 31 + (doc != null ? doc.hashCode() : 0);
537+
hash = hash * 31 + (doc != null ? doc.getKey().hashCode() : 0);
538+
hash = hash * 31 + (doc != null ? doc.getData().hashCode() : 0);
538539
hash = hash * 31 + metadata.hashCode();
539540
return hash;
540541
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public class QuerySnapshot implements Iterable<QueryDocumentSnapshot> {
5555
}
5656

5757
private class QuerySnapshotIterator implements Iterator<QueryDocumentSnapshot> {
58-
private final Iterator<com.google.firebase.firestore.model.Document> it;
58+
private final Iterator<Document> it;
5959

60-
QuerySnapshotIterator(Iterator<com.google.firebase.firestore.model.Document> it) {
60+
QuerySnapshotIterator(Iterator<Document> it) {
6161
this.it = it;
6262
}
6363

@@ -133,7 +133,7 @@ public List<DocumentChange> getDocumentChanges(@NonNull MetadataChanges metadata
133133
@NonNull
134134
public List<DocumentSnapshot> getDocuments() {
135135
List<DocumentSnapshot> res = new ArrayList<>(snapshot.getDocuments().size());
136-
for (com.google.firebase.firestore.model.Document doc : snapshot.getDocuments()) {
136+
for (Document doc : snapshot.getDocuments()) {
137137
res.add(convertDocument(doc));
138138
}
139139
return res;

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import com.google.android.gms.tasks.Tasks;
2424
import com.google.firebase.firestore.core.UserData.ParsedSetData;
2525
import com.google.firebase.firestore.core.UserData.ParsedUpdateData;
26-
import com.google.firebase.firestore.model.Document;
27-
import com.google.firebase.firestore.model.MaybeDocument;
28-
import com.google.firebase.firestore.model.NoDocument;
26+
import com.google.firebase.firestore.model.MutableDocument;
2927
import com.google.firebase.firestore.util.Executors;
3028
import com.google.firebase.firestore.util.Util;
3129
import java.util.Collections;
@@ -195,17 +193,17 @@ private Task<DocumentSnapshot> getAsync(DocumentReference documentRef) {
195193
if (!task.isSuccessful()) {
196194
throw task.getException();
197195
}
198-
List<MaybeDocument> docs = task.getResult();
196+
List<MutableDocument> docs = task.getResult();
199197
if (docs.size() != 1) {
200198
throw fail("Mismatch in docs returned from document lookup.");
201199
}
202-
MaybeDocument doc = docs.get(0);
203-
if (doc instanceof Document) {
200+
MutableDocument doc = docs.get(0);
201+
if (doc.isFoundDocument()) {
204202
return DocumentSnapshot.fromDocument(
205-
firestore, (Document) doc, /*fromCache=*/ false, /*hasPendingWrites=*/ false);
206-
} else if (doc instanceof NoDocument) {
203+
firestore, doc, /*fromCache=*/ false, /*hasPendingWrites=*/ false);
204+
} else if (doc.isNoDocument()) {
207205
return DocumentSnapshot.fromNoDocument(
208-
firestore, doc.getKey(), /*fromCache=*/ false, /*hasPendingWrites=*/ false);
206+
firestore, doc.getKey(), /*fromCache=*/ false);
209207
} else {
210208
throw fail(
211209
"BatchGetDocumentsRequest returned unexpected document type: "

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public ParsedUpdateData parseUpdateData(Map<String, Object> data) {
109109

110110
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
111111
ParseContext context = accumulator.rootContext();
112-
ObjectValue.Builder updateData = ObjectValue.newBuilder();
112+
ObjectValue updateData = new ObjectValue();
113113

114114
for (Entry<String, Object> entry : data.entrySet()) {
115115
FieldPath fieldPath =
@@ -130,7 +130,7 @@ public ParsedUpdateData parseUpdateData(Map<String, Object> data) {
130130
}
131131
}
132132

133-
return accumulator.toUpdateData(updateData.build());
133+
return accumulator.toUpdateData(updateData);
134134
}
135135

136136
/**
@@ -146,7 +146,7 @@ public ParsedUpdateData parseUpdateData(List<Object> fieldsAndValues) {
146146

147147
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
148148
ParseContext context = accumulator.rootContext();
149-
ObjectValue.Builder updateData = ObjectValue.newBuilder();
149+
ObjectValue updateData = new ObjectValue();
150150

151151
Iterator<Object> iterator = fieldsAndValues.iterator();
152152
while (iterator.hasNext()) {
@@ -180,7 +180,7 @@ public ParsedUpdateData parseUpdateData(List<Object> fieldsAndValues) {
180180
}
181181
}
182182

183-
return accumulator.toUpdateData(updateData.build());
183+
return accumulator.toUpdateData(updateData);
184184
}
185185

186186
/** Parse a "query value" (e.g. value in a where filter or a value in a cursor bound). */

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleCallback.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
import com.google.firebase.database.collection.ImmutableSortedMap;
1818
import com.google.firebase.database.collection.ImmutableSortedSet;
19+
import com.google.firebase.firestore.model.Document;
1920
import com.google.firebase.firestore.model.DocumentKey;
20-
import com.google.firebase.firestore.model.MaybeDocument;
21+
import com.google.firebase.firestore.model.MutableDocument;
2122

2223
/** Interface implemented by components that can apply changes from a bundle to local storage. */
2324
public interface BundleCallback {
@@ -26,8 +27,8 @@ public interface BundleCallback {
2627
*
2728
* <p>LocalDocuments are re-calculated if there are remaining mutations in the queue.
2829
*/
29-
ImmutableSortedMap<DocumentKey, MaybeDocument> applyBundledDocuments(
30-
ImmutableSortedMap<DocumentKey, MaybeDocument> documents, String bundleId);
30+
ImmutableSortedMap<DocumentKey, Document> applyBundledDocuments(
31+
ImmutableSortedMap<DocumentKey, MutableDocument> documents, String bundleId);
3132

3233
/** Saves the given NamedQuery to local persistence. */
3334
void saveNamedQuery(NamedQuery namedQuery, ImmutableSortedSet<DocumentKey> documentKeys);

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleDocument.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
package com.google.firebase.firestore.bundle;
1616

17-
import com.google.firebase.firestore.model.Document;
1817
import com.google.firebase.firestore.model.DocumentKey;
18+
import com.google.firebase.firestore.model.MutableDocument;
1919

2020
/** A document that was saved to a bundle. */
2121
public class BundleDocument implements BundleElement {
22-
private Document document;
22+
private MutableDocument document;
2323

24-
public BundleDocument(Document document) {
24+
public BundleDocument(MutableDocument document) {
2525
this.document = document;
2626
}
2727

@@ -31,7 +31,7 @@ public DocumentKey getKey() {
3131
}
3232

3333
/** Returns the document. */
34-
public Document getDocument() {
34+
public MutableDocument getDocument() {
3535
return document;
3636
}
3737

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleLoader.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
package com.google.firebase.firestore.bundle;
1616

17-
import static com.google.firebase.firestore.model.DocumentCollections.emptyMaybeDocumentMap;
17+
import static com.google.firebase.firestore.model.DocumentCollections.emptyMutableDocumentMap;
1818

1919
import androidx.annotation.Nullable;
2020
import com.google.firebase.database.collection.ImmutableSortedMap;
2121
import com.google.firebase.database.collection.ImmutableSortedSet;
2222
import com.google.firebase.firestore.LoadBundleTaskProgress;
23+
import com.google.firebase.firestore.model.Document;
2324
import com.google.firebase.firestore.model.DocumentKey;
24-
import com.google.firebase.firestore.model.MaybeDocument;
25-
import com.google.firebase.firestore.model.NoDocument;
25+
import com.google.firebase.firestore.model.MutableDocument;
2626
import com.google.firebase.firestore.util.Preconditions;
2727
import java.util.ArrayList;
2828
import java.util.HashMap;
@@ -39,15 +39,15 @@ public class BundleLoader {
3939
private final List<NamedQuery> queries;
4040
private final Map<DocumentKey, BundledDocumentMetadata> documentsMetadata;
4141

42-
private ImmutableSortedMap<DocumentKey, MaybeDocument> documents;
42+
private ImmutableSortedMap<DocumentKey, MutableDocument> documents;
4343
private long bytesLoaded;
4444
@Nullable private DocumentKey currentDocument;
4545

4646
public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata) {
4747
this.bundleCallback = bundleCallback;
4848
this.bundleMetadata = bundleMetadata;
4949
this.queries = new ArrayList<>();
50-
this.documents = emptyMaybeDocumentMap();
50+
this.documents = emptyMutableDocumentMap();
5151
this.documentsMetadata = new HashMap<>();
5252
}
5353

@@ -73,10 +73,8 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
7373
documents =
7474
documents.insert(
7575
bundledDocumentMetadata.getKey(),
76-
new NoDocument(
77-
bundledDocumentMetadata.getKey(),
78-
bundledDocumentMetadata.getReadTime(),
79-
/* hasCommittedMutations= */ false));
76+
new MutableDocument(bundledDocumentMetadata.getKey())
77+
.convertToNoDocument(bundledDocumentMetadata.getReadTime()));
8078
currentDocument = null;
8179
}
8280
} else if (bundleElement instanceof BundleDocument) {
@@ -103,7 +101,7 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
103101
}
104102

105103
/** Applies the loaded documents and queries to local store. Returns the document view changes. */
106-
public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
104+
public ImmutableSortedMap<DocumentKey, Document> applyChanges() {
107105
Preconditions.checkArgument(
108106
currentDocument == null,
109107
"Bundled documents end with a document metadata element instead of a document.");
@@ -114,7 +112,7 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
114112
bundleMetadata.getTotalDocuments(),
115113
documents.size());
116114

117-
ImmutableSortedMap<DocumentKey, MaybeDocument> changes =
115+
ImmutableSortedMap<DocumentKey, Document> changes =
118116
bundleCallback.applyBundledDocuments(documents, bundleMetadata.getBundleId());
119117

120118
Map<String, ImmutableSortedSet<DocumentKey>> queryDocumentMap = getQueryDocumentMapping();

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleSerializer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import com.google.firebase.firestore.core.OrderBy;
2424
import com.google.firebase.firestore.core.Query;
2525
import com.google.firebase.firestore.core.Target;
26-
import com.google.firebase.firestore.model.Document;
2726
import com.google.firebase.firestore.model.DocumentKey;
2827
import com.google.firebase.firestore.model.FieldPath;
28+
import com.google.firebase.firestore.model.MutableDocument;
2929
import com.google.firebase.firestore.model.ObjectValue;
3030
import com.google.firebase.firestore.model.ResourcePath;
3131
import com.google.firebase.firestore.model.SnapshotVersion;
@@ -111,11 +111,9 @@ BundleDocument decodeDocument(JSONObject document) throws JSONException {
111111
decodeMapValue(value, document.getJSONObject("fields"));
112112

113113
return new BundleDocument(
114-
new Document(
115-
key,
116-
updateTime,
117-
ObjectValue.fromMap(value.getMapValue().getFieldsMap()),
118-
Document.DocumentState.SYNCED));
114+
new MutableDocument(key)
115+
.convertToFoundDocument(
116+
updateTime, ObjectValue.fromMap(value.getMapValue().getFieldsMap())));
119117
}
120118

121119
private ResourcePath decodeName(String name) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public boolean equals(Object o) {
6464
public int hashCode() {
6565
int res = 61;
6666
res = res * 31 + type.hashCode();
67-
res = res * 31 + document.hashCode();
67+
res = res * 31 + document.getKey().hashCode();
68+
res = res * 31 + document.getData().hashCode();
6869
return res;
6970
}
7071

0 commit comments

Comments
 (0)