Skip to content

Commit be5589c

Browse files
Reduce diff
1 parent 2335298 commit be5589c

File tree

8 files changed

+45
-44
lines changed

8 files changed

+45
-44
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.firebase.firestore.bundle.BundledQuery;
2222
import com.google.firebase.firestore.core.Query.LimitType;
2323
import com.google.firebase.firestore.core.Target;
24-
import com.google.firebase.firestore.model.Document;
2524
import com.google.firebase.firestore.model.DocumentKey;
2625
import com.google.firebase.firestore.model.FieldIndex;
2726
import com.google.firebase.firestore.model.FieldPath;
@@ -49,7 +48,7 @@ public LocalSerializer(RemoteSerializer rpcSerializer) {
4948
}
5049

5150
/** Encodes a MaybeDocument model to the equivalent protocol buffer for local storage. */
52-
com.google.firebase.firestore.proto.MaybeDocument encodeMaybeDocument(Document document) {
51+
com.google.firebase.firestore.proto.MaybeDocument encodeMaybeDocument(MutableDocument document) {
5352
com.google.firebase.firestore.proto.MaybeDocument.Builder builder =
5453
com.google.firebase.firestore.proto.MaybeDocument.newBuilder();
5554

@@ -89,7 +88,7 @@ MutableDocument decodeMaybeDocument(com.google.firebase.firestore.proto.MaybeDoc
8988
* Encodes a Document for local storage. This differs from the v1 RPC serializer for Documents in
9089
* that it preserves the updateTime, which is considered an output only value by the server.
9190
*/
92-
private com.google.firestore.v1.Document encodeDocument(Document document) {
91+
private com.google.firestore.v1.Document encodeDocument(MutableDocument document) {
9392
com.google.firestore.v1.Document.Builder builder =
9493
com.google.firestore.v1.Document.newBuilder();
9594
builder.setName(rpcSerializer.encodeKey(document.getKey()));
@@ -111,7 +110,8 @@ private MutableDocument decodeDocument(
111110
}
112111

113112
/** Encodes a NoDocument value to the equivalent proto. */
114-
private com.google.firebase.firestore.proto.NoDocument encodeNoDocument(Document document) {
113+
private com.google.firebase.firestore.proto.NoDocument encodeNoDocument(
114+
MutableDocument document) {
115115
com.google.firebase.firestore.proto.NoDocument.Builder builder =
116116
com.google.firebase.firestore.proto.NoDocument.newBuilder();
117117
builder.setName(rpcSerializer.encodeKey(document.getKey()));
@@ -130,7 +130,7 @@ private MutableDocument decodeNoDocument(
130130

131131
/** Encodes a UnknownDocument value to the equivalent proto. */
132132
private com.google.firebase.firestore.proto.UnknownDocument encodeUnknownDocument(
133-
Document document) {
133+
MutableDocument document) {
134134
com.google.firebase.firestore.proto.UnknownDocument.Builder builder =
135135
com.google.firebase.firestore.proto.UnknownDocument.newBuilder();
136136
builder.setName(rpcSerializer.encodeKey(document.getKey()));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,8 @@ public QueryResult executeQuery(Query query, boolean usePreviousResults) {
848848
ImmutableSortedMap<DocumentKey, Document> documents =
849849
queryEngine.getDocumentsMatchingQuery(
850850
query,
851-
remoteKeys,
852-
usePreviousResults ? lastLimboFreeSnapshotVersion : SnapshotVersion.NONE);
851+
usePreviousResults ? lastLimboFreeSnapshotVersion : SnapshotVersion.NONE,
852+
remoteKeys);
853853
return new QueryResult(documents, remoteKeys);
854854
}
855855

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
final class MemoryRemoteDocumentCache implements RemoteDocumentCache {
3535

3636
/** Underlying cache of documents and their read times. */
37-
private ImmutableSortedMap<DocumentKey, Document> docs;
37+
private ImmutableSortedMap<DocumentKey, MutableDocument> docs;
3838
/** Manages the collection group index. */
3939
private IndexManager indexManager;
4040
/** The latest read time of any document in the cache. */
4141
private SnapshotVersion latestReadTime;
4242

4343
MemoryRemoteDocumentCache() {
44-
docs = emptyDocumentMap();
44+
docs = ImmutableSortedMap.Builder.emptyMap(DocumentKey.comparator());
4545
latestReadTime = SnapshotVersion.NONE;
4646
}
4747

@@ -77,7 +77,7 @@ public void removeAll(Collection<DocumentKey> keys) {
7777

7878
@Override
7979
public MutableDocument get(DocumentKey key) {
80-
Document doc = docs.get(key);
80+
MutableDocument doc = docs.get(key);
8181
return doc != null ? doc.clone() : MutableDocument.newInvalidDocument(key);
8282
}
8383

@@ -104,11 +104,11 @@ public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOf
104104
// Documents are ordered by key, so we can use a prefix scan to narrow down the documents
105105
// we need to match the query against.
106106
DocumentKey prefix = DocumentKey.fromPath(collection.append(""));
107-
Iterator<Map.Entry<DocumentKey, Document>> iterator = docs.iteratorFrom(prefix);
107+
Iterator<Map.Entry<DocumentKey, MutableDocument>> iterator = docs.iteratorFrom(prefix);
108108

109109
while (iterator.hasNext()) {
110-
Map.Entry<DocumentKey, Document> entry = iterator.next();
111-
Document doc = entry.getValue();
110+
Map.Entry<DocumentKey, MutableDocument> entry = iterator.next();
111+
MutableDocument doc = entry.getValue();
112112

113113
DocumentKey key = entry.getKey();
114114
if (!collection.isPrefixOf(key.getPath())) {
@@ -137,13 +137,13 @@ public SnapshotVersion getLatestReadTime() {
137137
return latestReadTime;
138138
}
139139

140-
Iterable<Document> getDocuments() {
140+
Iterable<MutableDocument> getDocuments() {
141141
return new DocumentIterable();
142142
}
143143

144144
long getByteSize(LocalSerializer serializer) {
145145
long count = 0;
146-
for (Document doc : new DocumentIterable()) {
146+
for (MutableDocument doc : new DocumentIterable()) {
147147
count += serializer.encodeMaybeDocument(doc).getSerializedSize();
148148
}
149149
return count;
@@ -152,20 +152,20 @@ long getByteSize(LocalSerializer serializer) {
152152
/**
153153
* A proxy that exposes an iterator over the current set of documents in the RemoteDocumentCache.
154154
*/
155-
private class DocumentIterable implements Iterable<Document> {
155+
private class DocumentIterable implements Iterable<MutableDocument> {
156156
@NonNull
157157
@Override
158-
public Iterator<Document> iterator() {
159-
Iterator<Map.Entry<DocumentKey, Document>> iterator =
158+
public Iterator<MutableDocument> iterator() {
159+
Iterator<Map.Entry<DocumentKey, MutableDocument>> iterator =
160160
MemoryRemoteDocumentCache.this.docs.iterator();
161-
return new Iterator<Document>() {
161+
return new Iterator<MutableDocument>() {
162162
@Override
163163
public boolean hasNext() {
164164
return iterator.hasNext();
165165
}
166166

167167
@Override
168-
public Document next() {
168+
public MutableDocument next() {
169169
return iterator.next().getValue();
170170
}
171171
};

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public void initialize(LocalDocumentsView localDocumentsView, IndexManager index
8181

8282
public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
8383
Query query,
84-
ImmutableSortedSet<DocumentKey> remoteKeys,
85-
SnapshotVersion lastLimboFreeSnapshotVersion) {
84+
SnapshotVersion lastLimboFreeSnapshotVersion,
85+
ImmutableSortedSet<DocumentKey> remoteKeys) {
8686
hardAssert(initialized, "initialize() not called");
8787

8888
ImmutableSortedMap<DocumentKey, Document> result =
@@ -118,7 +118,8 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
118118
Set<DocumentKey> keys = indexManager.getDocumentsMatchingTarget(fieldIndex, target);
119119
ImmutableSortedMap<DocumentKey, Document> indexedDocuments =
120120
localDocumentsView.getDocuments(keys);
121-
return mergeResults(values(indexedDocuments), query, fieldIndex.getIndexState().getOffset());
121+
return appendRemainingResults(
122+
values(indexedDocuments), query, fieldIndex.getIndexState().getOffset());
122123
}
123124

124125
/**
@@ -158,7 +159,8 @@ && needsRefill(
158159
query.toString());
159160
}
160161

161-
return mergeResults(previousResults, query, IndexOffset.create(lastLimboFreeSnapshotVersion));
162+
return appendRemainingResults(
163+
previousResults, query, IndexOffset.create(lastLimboFreeSnapshotVersion));
162164
}
163165

164166
/** Applies the query filter and sorting to the provided documents. */
@@ -223,15 +225,18 @@ private ImmutableSortedMap<DocumentKey, Document> executeFullCollectionScan(Quer
223225
return localDocumentsView.getDocumentsMatchingQuery(query, IndexOffset.NONE);
224226
}
225227

226-
private ImmutableSortedMap<DocumentKey, Document> mergeResults(
227-
Iterable<Document> before, Query query, IndexOffset offset) {
228-
// Retrieve all results for documents that were updated since the last limbo-document free
229-
// remote snapshot.
230-
ImmutableSortedMap<DocumentKey, Document> after =
228+
/**
229+
* Combines the results from an indexed execution with the remaining documents that have not yet
230+
* been indexed.
231+
*/
232+
private ImmutableSortedMap<DocumentKey, Document> appendRemainingResults(
233+
Iterable<Document> indexedResults, Query query, IndexOffset offset) {
234+
// Retrieve all results for documents that were updated since the offset.
235+
ImmutableSortedMap<DocumentKey, Document> remainingResults =
231236
localDocumentsView.getDocumentsMatchingQuery(query, offset);
232-
for (Document entry : before) {
233-
after = after.insert(entry.getKey(), entry);
237+
for (Document entry : indexedResults) {
238+
remainingResults = remainingResults.insert(entry.getKey(), entry);
234239
}
235-
return after;
240+
return remainingResults;
236241
}
237242
}

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

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

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

17-
import androidx.annotation.NonNull;
1817
import androidx.annotation.Nullable;
1918
import com.google.firestore.v1.Value;
2019
import java.util.Comparator;
@@ -74,7 +73,4 @@ public interface Document {
7473
* Whether this document has a local mutation applied that has not yet been acknowledged by Watch.
7574
*/
7675
boolean hasPendingWrites();
77-
78-
@NonNull
79-
MutableDocument clone();
8076
}

firebase-firestore/src/test/java/com/google/firebase/firestore/local/CountingQueryEngine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public void initialize(LocalDocumentsView localDocuments, IndexManager indexMana
6969
@Override
7070
public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
7171
Query query,
72-
ImmutableSortedSet<DocumentKey> remoteKeys,
73-
SnapshotVersion lastLimboFreeSnapshotVersion) {
74-
return queryEngine.getDocumentsMatchingQuery(query, remoteKeys, lastLimboFreeSnapshotVersion);
72+
SnapshotVersion lastLimboFreeSnapshotVersion,
73+
ImmutableSortedSet<DocumentKey> remoteKeys) {
74+
return queryEngine.getDocumentsMatchingQuery(query, lastLimboFreeSnapshotVersion, remoteKeys);
7575
}
7676

7777
/** Returns the query engine that is used as the backing implementation. */

firebase-firestore/src/test/java/com/google/firebase/firestore/local/IndexedQueryEngineTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void combinesIndexedWithNonIndexedResults() {
9090
Query queryWithFilter = query("coll").filter(filter("foo", "==", true));
9191
ImmutableSortedMap<DocumentKey, Document> results =
9292
queryEngine.getDocumentsMatchingQuery(
93-
queryWithFilter, DocumentKey.emptyKeySet(), SnapshotVersion.NONE);
93+
queryWithFilter, SnapshotVersion.NONE, DocumentKey.emptyKeySet());
9494

9595
assertTrue(results.containsKey(doc1.getKey()));
9696
assertTrue(results.containsKey(doc2.getKey()));

firebase-firestore/src/test/java/com/google/firebase/firestore/local/QueryEngineTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ private DocumentSet runQuery(Query query, SnapshotVersion lastLimboFreeSnapshotV
185185
ImmutableSortedMap<DocumentKey, Document> docs =
186186
queryEngine.getDocumentsMatchingQuery(
187187
query,
188-
targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID),
189-
lastLimboFreeSnapshotVersion);
188+
lastLimboFreeSnapshotVersion,
189+
targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID));
190190
View view =
191191
new View(query, new ImmutableSortedSet<>(Collections.emptyList(), DocumentKey::compareTo));
192192
View.DocumentChanges viewDocChanges = view.computeDocChanges(docs);
@@ -405,8 +405,8 @@ public void doesNotIncludeDocumentsDeletedByMutation() throws Exception {
405405
() ->
406406
queryEngine.getDocumentsMatchingQuery(
407407
query,
408-
targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID),
409-
LAST_LIMBO_FREE_SNAPSHOT));
408+
LAST_LIMBO_FREE_SNAPSHOT,
409+
targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID)));
410410
assertEquals(emptyMutableDocumentMap().insert(MATCHING_DOC_A.getKey(), MATCHING_DOC_A), docs);
411411
}
412412
}

0 commit comments

Comments
 (0)