Skip to content

Commit 93a1ece

Browse files
Feedback
1 parent 1b03511 commit 93a1ece

Some content is hidden

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

42 files changed

+330
-310
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static QuerySnapshot querySnapshot(
7777
for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
7878
String docKey = path + "/" + pair.getKey();
7979
Document doc = doc(docKey, 1L, pair.getValue());
80-
if (hasPendingWrites) doc.withLocalMutations();
80+
if (hasPendingWrites) doc.setLocalMutations();
8181
oldDocuments = oldDocuments.add(doc);
8282
if (hasPendingWrites) {
8383
mutatedKeys = mutatedKeys.insert(key(docKey));
@@ -88,7 +88,7 @@ public static QuerySnapshot querySnapshot(
8888
for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
8989
String docKey = path + "/" + pair.getKey();
9090
Document docToAdd = doc(docKey, 1L, pair.getValue());
91-
if (hasPendingWrites) docToAdd.withLocalMutations();
91+
if (hasPendingWrites) docToAdd.setLocalMutations();
9292
newDocuments = newDocuments.add(docToAdd);
9393
documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));
9494

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public static SnapshotVersion version(long versionMicros) {
6363
}
6464

6565
public static Document doc(String key, long version, Map<String, Object> data) {
66-
return new Document(key(key)).asFoundDocument(version(version), wrapObject(data));
66+
return new Document(key(key)).setFoundDocument(version(version), wrapObject(data));
6767
}
6868

6969
public static Document doc(DocumentKey key, long version, Map<String, Object> data) {
70-
return new Document(key).asFoundDocument(version(version), wrapObject(data));
70+
return new Document(key).setFoundDocument(version(version), wrapObject(data));
7171
}
7272

7373
public static Document doc(String key, long version, ObjectValue data) {
74-
return new Document(key(key)).asFoundDocument(version(version), data);
74+
return new Document(key(key)).setFoundDocument(version(version), data);
7575
}
7676

7777
public static DocumentSet docSet(Comparator<Document> comparator, Document... documents) {

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

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

1717
import static com.google.firebase.firestore.util.Assert.fail;
18-
import static com.google.firebase.firestore.util.Assert.hardAssert;
1918
import static com.google.firebase.firestore.util.Preconditions.checkNotNull;
2019

2120
import androidx.annotation.NonNull;
@@ -199,15 +198,16 @@ private Task<DocumentSnapshot> getAsync(DocumentReference documentRef) {
199198
throw fail("Mismatch in docs returned from document lookup.");
200199
}
201200
Document doc = docs.get(0);
202-
if (doc.exists()) {
201+
if (doc.isFoundDocument()) {
203202
return DocumentSnapshot.fromDocument(
204203
firestore, (Document) doc, /*fromCache=*/ false, /*hasPendingWrites=*/ false);
205-
} else {
206-
hardAssert(
207-
doc.isMissing(),
208-
"BatchGetDocumentsRequest returned unexpected document: " + doc);
204+
} else if (doc.isNoDocument()) {
209205
return DocumentSnapshot.fromNoDocument(
210206
firestore, doc.getKey(), /*fromCache=*/ false, /*hasPendingWrites=*/ false);
207+
} else {
208+
throw fail(
209+
"BatchGetDocumentsRequest returned unexpected document type: "
210+
+ doc.getClass().getCanonicalName());
211211
}
212212
});
213213
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
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.emptyDocumentMap;
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;
2625
import com.google.firebase.firestore.util.Preconditions;
2726
import java.util.ArrayList;
2827
import java.util.HashMap;
@@ -39,15 +38,15 @@ public class BundleLoader {
3938
private final List<NamedQuery> queries;
4039
private final Map<DocumentKey, BundledDocumentMetadata> documentsMetadata;
4140

42-
private ImmutableSortedMap<DocumentKey, MaybeDocument> documents;
41+
private ImmutableSortedMap<DocumentKey, Document> documents;
4342
private long bytesLoaded;
4443
@Nullable private DocumentKey currentDocument;
4544

4645
public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata) {
4746
this.bundleCallback = bundleCallback;
4847
this.bundleMetadata = bundleMetadata;
4948
this.queries = new ArrayList<>();
50-
this.documents = emptyMaybeDocumentMap();
49+
this.documents = emptyDocumentMap();
5150
this.documentsMetadata = new HashMap<>();
5251
}
5352

@@ -73,10 +72,8 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
7372
documents =
7473
documents.insert(
7574
bundledDocumentMetadata.getKey(),
76-
new NoDocument(
77-
bundledDocumentMetadata.getKey(),
78-
bundledDocumentMetadata.getReadTime(),
79-
/* hasCommittedMutations= */ false));
75+
new Document(bundledDocumentMetadata.getKey())
76+
.setNoDocument(bundledDocumentMetadata.getReadTime()));
8077
currentDocument = null;
8178
}
8279
} else if (bundleElement instanceof BundleDocument) {
@@ -103,7 +100,7 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
103100
}
104101

105102
/** Applies the loaded documents and queries to local store. Returns the document view changes. */
106-
public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
103+
public ImmutableSortedMap<DocumentKey, Document> applyChanges() {
107104
Preconditions.checkArgument(
108105
currentDocument == null,
109106
"Bundled documents end with a document metadata element instead of a document.");
@@ -114,7 +111,7 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
114111
bundleMetadata.getTotalDocuments(),
115112
documents.size());
116113

117-
ImmutableSortedMap<DocumentKey, MaybeDocument> changes =
114+
ImmutableSortedMap<DocumentKey, Document> changes =
118115
bundleCallback.applyBundledDocuments(documents, bundleMetadata.getBundleId());
119116

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

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ 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 Document(key)
115+
.setFoundDocument(updateTime, ObjectValue.fromMap(value.getMapValue().getFieldsMap())));
119116
}
120117

121118
private ResourcePath decodeName(String name) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ public Task<Document> getDocumentFromLocalCache(DocumentKey docKey) {
178178
.continueWith(
179179
(result) -> {
180180
Document document = result.getResult();
181-
if (document.exists()) {
181+
if (document.isFoundDocument()) {
182182
return document;
183-
} else if (document.isMissing()) {
183+
} else if (document.isNoDocument()) {
184184
return null;
185185
} else {
186186
throw new FirebaseFirestoreException(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public void handleRejectedListen(int targetId, Status error) {
406406
// It's a limbo doc. Create a synthetic event saying it was deleted. This is kind of a hack.
407407
// Ideally, we would have a method in the local store to purge a document. However, it would
408408
// be tricky to keep all of the local store's invariants with another method.
409-
Document result = new Document(limboKey).asMissingDocument(SnapshotVersion.NONE);
409+
Document result = new Document(limboKey).setNoDocument(SnapshotVersion.NONE);
410410
Map<DocumentKey, Document> documentUpdates = Collections.singletonMap(limboKey, result);
411411
Set<DocumentKey> limboDocuments = Collections.singleton(limboKey);
412412
RemoteEvent event =
@@ -536,7 +536,7 @@ public void loadBundle(BundleReader bundleReader, LoadBundleTask resultTask) {
536536
}
537537
}
538538

539-
ImmutableSortedMap<DocumentKey, MaybeDocument> changes = bundleLoader.applyChanges();
539+
ImmutableSortedMap<DocumentKey, Document> changes = bundleLoader.applyChanges();
540540

541541
// TODO(b/160876443): This currently raises snapshots with `fromCache=false` if users already
542542
// listen to some queries and bundles has newer version.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
import static com.google.firebase.firestore.util.Assert.fail;
1718
import static com.google.firebase.firestore.util.Assert.hardAssert;
1819

1920
import androidx.annotation.Nullable;
@@ -170,12 +171,13 @@ private static Executor createDefaultExecutor() {
170171

171172
private void recordVersion(Document doc) throws FirebaseFirestoreException {
172173
SnapshotVersion docVersion;
173-
if (doc.exists()) {
174+
if (doc.isFoundDocument()) {
174175
docVersion = doc.getVersion();
175-
} else {
176-
hardAssert(doc.isMissing(), "Unexpected document in transaction: " + doc);
176+
} else if (doc.isNoDocument()) {
177177
// For nonexistent docs, we must use precondition with version 0 when we overwrite them.
178178
docVersion = SnapshotVersion.NONE;
179+
} else {
180+
throw fail("Unexpected document type in transaction: " + doc);
179181
}
180182

181183
if (readVersions.containsKey(doc.getKey())) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public DocumentChanges computeDocChanges(
160160
Document oldDoc = oldDocumentSet.getDocument(key);
161161
Document newDoc = null;
162162

163-
if (entry.getValue().exists()) {
163+
if (entry.getValue().isFoundDocument()) {
164164
hardAssert(
165165
key.equals(entry.getKey()),
166166
"Mismatching key in doc change %s != %s",

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Document getDocument(DocumentKey key) {
8282
private Document getDocument(DocumentKey key, List<MutationBatch> inBatches) {
8383
Document document = remoteDocumentCache.get(key);
8484
for (MutationBatch batch : inBatches) {
85-
batch.applyToLocalView(key, document);
85+
batch.applyToLocalView(document);
8686
}
8787

8888
return document;
@@ -94,7 +94,7 @@ private void applyLocalMutationsToDocuments(
9494
Map<DocumentKey, Document> docs, List<MutationBatch> batches) {
9595
for (Map.Entry<DocumentKey, Document> base : docs.entrySet()) {
9696
for (MutationBatch batch : batches) {
97-
batch.applyToLocalView(base.getKey(), base.getValue());
97+
batch.applyToLocalView(base.getValue());
9898
}
9999
}
100100
}
@@ -157,7 +157,7 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingDocumentQu
157157
ImmutableSortedMap<DocumentKey, Document> result = emptyDocumentMap();
158158
// Just do a simple document lookup.
159159
Document doc = getDocument(DocumentKey.fromPath(path));
160-
if (doc.exists()) {
160+
if (doc.isFoundDocument()) {
161161
result = result.insert(doc.getKey(), doc);
162162
}
163163
return result;
@@ -205,11 +205,12 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
205205
DocumentKey key = mutation.getKey();
206206
Document document = results.get(key);
207207
if (document == null) {
208-
document = new Document(key);
208+
document = new Document(key); // Create invalid document to apply mutations on top of
209+
results = results.insert(key, document);
209210
}
210211
mutation.applyToLocalView(document, batch.getLocalWriteTime());
211-
if (document.isValid()) {
212-
results = results.insert(key, document);
212+
if (!document.isFoundDocument()) {
213+
results = results.remove(key);
213214
}
214215
}
215216
}
@@ -246,7 +247,7 @@ private ImmutableSortedMap<DocumentKey, Document> addMissingBaseDocuments(
246247
ImmutableSortedMap<DocumentKey, Document> mergedDocs = existingDocs;
247248
Map<DocumentKey, Document> missingDocs = remoteDocumentCache.getAll(missingDocKeys);
248249
for (Map.Entry<DocumentKey, Document> entry : missingDocs.entrySet()) {
249-
if (entry.getValue().exists()) {
250+
if (entry.getValue().isFoundDocument()) {
250251
mergedDocs = mergedDocs.insert(entry.getKey(), entry.getValue());
251252
}
252253
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ com.google.firebase.firestore.proto.MaybeDocument encodeMaybeDocument(Document d
4949
com.google.firebase.firestore.proto.MaybeDocument.Builder builder =
5050
com.google.firebase.firestore.proto.MaybeDocument.newBuilder();
5151

52-
if (document.exists()) {
53-
builder.setDocument(encodeDocument(document));
54-
} else if (document.isMissing()) {
52+
if (document.isNoDocument()) {
5553
builder.setNoDocument(encodeNoDocument(document));
56-
} else if (document.isUnknown()) {
54+
} else if (document.isFoundDocument()) {
55+
builder.setDocument(encodeDocument(document));
56+
} else if (document.isUnknownDocument()) {
5757
builder.setUnknownDocument(encodeUnknownDocument(document));
5858
} else {
5959
throw fail("Unknown Document %s", document);
@@ -101,8 +101,8 @@ private Document decodeDocument(
101101
DocumentKey key = rpcSerializer.decodeKey(document.getName());
102102
SnapshotVersion version = rpcSerializer.decodeVersion(document.getUpdateTime());
103103
Document result =
104-
new Document(key).asFoundDocument(version, ObjectValue.fromMap(document.getFieldsMap()));
105-
return hasCommittedMutations ? result.withCommittedMutations() : result;
104+
new Document(key).setFoundDocument(version, ObjectValue.fromMap(document.getFieldsMap()));
105+
return hasCommittedMutations ? result.setCommittedMutations() : result;
106106
}
107107

108108
/** Encodes a NoDocument value to the equivalent proto. */
@@ -119,8 +119,8 @@ private Document decodeNoDocument(
119119
com.google.firebase.firestore.proto.NoDocument proto, boolean hasCommittedMutations) {
120120
DocumentKey key = rpcSerializer.decodeKey(proto.getName());
121121
SnapshotVersion version = rpcSerializer.decodeVersion(proto.getReadTime());
122-
Document result = new Document(key).asMissingDocument(version);
123-
return hasCommittedMutations ? result.withCommittedMutations() : result;
122+
Document result = new Document(key).setNoDocument(version);
123+
return hasCommittedMutations ? result.setCommittedMutations() : result;
124124
}
125125

126126
/** Encodes a UnknownDocument value to the equivalent proto. */
@@ -138,7 +138,7 @@ private Document decodeUnknownDocument(
138138
com.google.firebase.firestore.proto.UnknownDocument proto) {
139139
DocumentKey key = rpcSerializer.decodeKey(proto.getName());
140140
SnapshotVersion version = rpcSerializer.decodeVersion(proto.getVersion());
141-
return new Document(key).asUnknownDocument(version);
141+
return new Document(key).setUnknownDocument(version);
142142
}
143143

144144
/** Encodes a MutationBatch model for local storage in the mutation queue. */

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,12 @@ private Map<DocumentKey, Document> populateDocumentChanges(
440440
// Note: The order of the steps below is important, since we want to ensure that
441441
// rejected limbo resolutions (which fabricate NoDocuments with SnapshotVersion.NONE)
442442
// never add documents to cache.
443-
if (!doc.exists() && doc.getVersion().equals(SnapshotVersion.NONE)) {
443+
if (doc.isNoDocument() && doc.getVersion().equals(SnapshotVersion.NONE)) {
444444
// NoDocuments with SnapshotVersion.NONE are used in manufactured events. We remove
445445
// these documents from cache since we lost access.
446446
remoteDocuments.remove(doc.getKey());
447447
changedDocs.put(key, doc);
448-
} else if (!existingDoc.isValid()
448+
} else if (!existingDoc.isValidDocument()
449449
|| doc.getVersion().compareTo(existingDoc.getVersion()) > 0
450450
|| (doc.getVersion().compareTo(existingDoc.getVersion()) == 0
451451
&& existingDoc.hasPendingWrites())) {
@@ -648,7 +648,7 @@ public ImmutableSortedMap<DocumentKey, Document> applyBundledDocuments(
648648
DocumentKey documentKey = entry.getKey();
649649
Document document = entry.getValue();
650650

651-
if (document.exists()) {
651+
if (document.isFoundDocument()) {
652652
documentKeys = documentKeys.insert(documentKey);
653653
}
654654
documentMap.put(documentKey, document);
@@ -776,7 +776,7 @@ private void applyWriteToRemoteDocuments(MutationBatchResult batchResult) {
776776

777777
if (doc.getVersion().compareTo(ackVersion) < 0) {
778778
batch.applyToRemoteDocument(docKey, doc, batchResult);
779-
if (doc.isValid()) {
779+
if (doc.isValidDocument()) {
780780
remoteDocuments.add(doc, batchResult.getCommitVersion());
781781
}
782782
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(
9696
}
9797

9898
Document doc = entry.getValue().first;
99-
if (doc.exists()) {
99+
if (doc.isFoundDocument()) {
100100
SnapshotVersion readTime = entry.getValue().second;
101101
if (readTime.compareTo(sinceReadTime) <= 0) {
102102
continue;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ interface RemoteDocumentCache {
4747
* Looks up an entry in the cache.
4848
*
4949
* @param documentKey The key of the entry to look up.
50-
* @return The cached Document or NoDocument entry, or an InvalidDocument if nothing is cached.
50+
* @return The cached Document or NoDocument entry, or an invalid document if nothing is cached.
5151
*/
5252
Document get(DocumentKey documentKey);
5353

@@ -56,7 +56,7 @@ interface RemoteDocumentCache {
5656
*
5757
* @param documentKeys The keys of the entries to look up.
5858
* @return The cached Document or NoDocument entries indexed by key. If an entry is not cached, an
59-
* InvalidDocument is returned.
59+
* invalid document is returned.
6060
*/
6161
Map<DocumentKey, Document> getAll(Iterable<DocumentKey> documentKeys);
6262

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(
182182
executor.execute(
183183
() -> {
184184
Document document = decodeMaybeDocument(rawDocument);
185-
if (document.exists() && query.matches(document)) {
185+
if (document.isFoundDocument() && query.matches(document)) {
186186
synchronized (SQLiteRemoteDocumentCache.this) {
187187
matchingDocuments[0] = matchingDocuments[0].insert(document.getKey(), document);
188188
}

0 commit comments

Comments
 (0)