Skip to content

Commit e7143c0

Browse files
committed
Remove getAll change and use None for local mutations
1 parent b48e6b4 commit e7143c0

File tree

16 files changed

+106
-168
lines changed

16 files changed

+106
-168
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
import com.google.firebase.firestore.model.FieldIndex.IndexOffset;
2727
import com.google.firebase.firestore.model.MutableDocument;
2828
import com.google.firebase.firestore.model.ResourcePath;
29-
import com.google.firebase.firestore.model.mutation.DeleteMutation;
3029
import com.google.firebase.firestore.model.mutation.FieldMask;
3130
import com.google.firebase.firestore.model.mutation.Mutation;
3231
import com.google.firebase.firestore.model.mutation.MutationBatch;
3332
import com.google.firebase.firestore.model.mutation.PatchMutation;
34-
import com.google.firebase.firestore.model.mutation.SetMutation;
3533
import java.util.HashMap;
3634
import java.util.HashSet;
3735
import java.util.List;
@@ -259,18 +257,9 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
259257
/** Queries the remote documents and overlays by doing a full collection scan. */
260258
private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionQuery(
261259
Query query, IndexOffset offset) {
262-
Map<DocumentKey, Mutation> overlays = documentOverlayCache.getOverlays(query.getPath(), -1);
263-
264-
// Remote document elision: avoid reading remote documents if their overlay is Set or Delete.
265-
Set<DocumentKey> ignore = new HashSet<>();
266-
for (Map.Entry<DocumentKey, Mutation> entry : overlays.entrySet()) {
267-
if (entry.getValue() instanceof SetMutation || entry.getValue() instanceof DeleteMutation) {
268-
ignore.add(entry.getKey());
269-
}
270-
}
271-
272260
Map<DocumentKey, MutableDocument> remoteDocuments =
273-
remoteDocumentCache.getAll(query.getPath(), offset, ignore);
261+
remoteDocumentCache.getAll(query.getPath(), offset);
262+
Map<DocumentKey, Mutation> overlays = documentOverlayCache.getOverlays(query.getPath(), -1);
274263

275264
// As documents might match the query because of their overlay we need to include documents
276265
// for all overlays in the initial document set.

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private com.google.firestore.v1.Document encodeDocument(Document document) {
9494
com.google.firestore.v1.Document.newBuilder();
9595
builder.setName(rpcSerializer.encodeKey(document.getKey()));
9696
builder.putAllFields(document.getData().getFieldsMap());
97-
Timestamp updateTime = document.getInternalReference().getVersion().getTimestamp();
97+
Timestamp updateTime = document.getVersion().getTimestamp();
9898
builder.setUpdateTime(rpcSerializer.encodeTimestamp(updateTime));
9999
return builder.build();
100100
}
@@ -115,8 +115,7 @@ private com.google.firebase.firestore.proto.NoDocument encodeNoDocument(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()));
118-
builder.setReadTime(
119-
rpcSerializer.encodeTimestamp(document.getInternalReference().getVersion().getTimestamp()));
118+
builder.setReadTime(rpcSerializer.encodeTimestamp(document.getVersion().getTimestamp()));
120119
return builder.build();
121120
}
122121

@@ -135,8 +134,7 @@ private com.google.firebase.firestore.proto.UnknownDocument encodeUnknownDocumen
135134
com.google.firebase.firestore.proto.UnknownDocument.Builder builder =
136135
com.google.firebase.firestore.proto.UnknownDocument.newBuilder();
137136
builder.setName(rpcSerializer.encodeKey(document.getKey()));
138-
builder.setVersion(
139-
rpcSerializer.encodeTimestamp(document.getInternalReference().getVersion().getTimestamp()));
137+
builder.setVersion(rpcSerializer.encodeTimestamp(document.getVersion().getTimestamp()));
140138
return builder.build();
141139
}
142140

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.HashMap;
3030
import java.util.Iterator;
3131
import java.util.Map;
32-
import java.util.Set;
3332

3433
/** In-memory cache of remote documents. */
3534
final class MemoryRemoteDocumentCache implements RemoteDocumentCache {
@@ -79,9 +78,7 @@ public void removeAll(Collection<DocumentKey> keys) {
7978
@Override
8079
public MutableDocument get(DocumentKey key) {
8180
Document doc = docs.get(key);
82-
return doc != null
83-
? doc.getInternalReference().mutableCopy()
84-
: MutableDocument.newInvalidDocument(key);
81+
return doc != null ? doc.mutableCopy() : MutableDocument.newInvalidDocument(key);
8582
}
8683

8784
@Override
@@ -101,8 +98,7 @@ public Map<DocumentKey, MutableDocument> getAll(
10198
}
10299

103100
@Override
104-
public Map<DocumentKey, MutableDocument> getAll(
105-
ResourcePath collection, IndexOffset offset, Set<DocumentKey> ignoreSet) {
101+
public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
106102
Map<DocumentKey, MutableDocument> result = new HashMap<>();
107103

108104
// Documents are ordered by key, so we can use a prefix scan to narrow down the documents
@@ -112,10 +108,6 @@ public Map<DocumentKey, MutableDocument> getAll(
112108

113109
while (iterator.hasNext()) {
114110
Map.Entry<DocumentKey, Document> entry = iterator.next();
115-
if (ignoreSet.contains(entry.getKey())) {
116-
continue;
117-
}
118-
119111
Document doc = entry.getValue();
120112

121113
DocumentKey key = entry.getKey();
@@ -134,7 +126,7 @@ public Map<DocumentKey, MutableDocument> getAll(
134126
continue;
135127
}
136128

137-
result.put(doc.getKey(), doc.getInternalReference().mutableCopy());
129+
result.put(doc.getKey(), doc.mutableCopy());
138130
}
139131

140132
return result;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,7 @@ private boolean needsRefill(
215215
return false;
216216
}
217217
return documentAtLimitEdge.hasPendingWrites()
218-
|| documentAtLimitEdge
219-
.getInternalReference()
220-
.getVersion()
221-
.compareTo(limboFreeSnapshotVersion)
222-
> 0;
218+
|| documentAtLimitEdge.getVersion().compareTo(limboFreeSnapshotVersion) > 0;
223219
}
224220

225221
private ImmutableSortedMap<DocumentKey, Document> executeFullCollectionScan(Query query) {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.firebase.firestore.model.SnapshotVersion;
2222
import java.util.Collection;
2323
import java.util.Map;
24-
import java.util.Set;
2524

2625
/**
2726
* Represents cached documents received from the remote backend.
@@ -82,11 +81,9 @@ interface RemoteDocumentCache {
8281
*
8382
* @param collection The collection to read.
8483
* @param offset The read time and document key to start scanning at (exclusive).
85-
* @param ignoreSet The set of document keys to ignore from the result.
8684
* @return A newly created map with the set of documents in the collection.
8785
*/
88-
Map<DocumentKey, MutableDocument> getAll(
89-
ResourcePath collection, IndexOffset offset, Set<DocumentKey> ignoreSet);
86+
Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset);
9087

9188
/** Returns the latest read time of any document in the cache. */
9289
SnapshotVersion getLatestReadTime();

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
import java.util.Collection;
3838
import java.util.Collections;
3939
import java.util.HashMap;
40-
import java.util.HashSet;
4140
import java.util.List;
4241
import java.util.Map;
43-
import java.util.Set;
4442
import java.util.concurrent.Executor;
4543

4644
final class SQLiteRemoteDocumentCache implements RemoteDocumentCache {
@@ -166,18 +164,15 @@ public Map<DocumentKey, MutableDocument> getAll(
166164
if (collections.isEmpty()) {
167165
return Collections.emptyMap();
168166
} else if (BINDS_PER_STATEMENT * collections.size() < SQLitePersistence.MAX_ARGS) {
169-
return getAll(collections, offset, limit, Collections.emptySet());
167+
return getAll(collections, offset, limit);
170168
} else {
171169
// We need to fan out our collection scan since SQLite only supports 999 binds per statement.
172170
Map<DocumentKey, MutableDocument> results = new HashMap<>();
173171
int pageSize = SQLitePersistence.MAX_ARGS / BINDS_PER_STATEMENT;
174172
for (int i = 0; i < collections.size(); i += pageSize) {
175173
results.putAll(
176174
getAll(
177-
collections.subList(i, Math.min(collections.size(), i + pageSize)),
178-
offset,
179-
limit,
180-
new HashSet<>()));
175+
collections.subList(i, Math.min(collections.size(), i + pageSize)), offset, limit));
181176
}
182177
return firstNEntries(results, limit, IndexOffset.DOCUMENT_COMPARATOR);
183178
}
@@ -187,7 +182,7 @@ public Map<DocumentKey, MutableDocument> getAll(
187182
* Returns the next {@code count} documents from the provided collections, ordered by read time.
188183
*/
189184
private Map<DocumentKey, MutableDocument> getAll(
190-
List<ResourcePath> collections, IndexOffset offset, int count, Set<DocumentKey> ignoreSet) {
185+
List<ResourcePath> collections, IndexOffset offset, int count) {
191186
Timestamp readTime = offset.getReadTime().getTimestamp();
192187
DocumentKey documentKey = offset.getDocumentKey();
193188

@@ -223,18 +218,10 @@ private Map<DocumentKey, MutableDocument> getAll(
223218
Map<DocumentKey, MutableDocument>[] results =
224219
(HashMap<DocumentKey, MutableDocument>[]) (new HashMap[] {new HashMap()});
225220

226-
Set<String> ignoredPath = new HashSet<>();
227-
for (DocumentKey key : ignoreSet) {
228-
ignoredPath.add(EncodedPath.encode(key.getPath()));
229-
}
230221
db.query(sql.toString())
231222
.binding(bindVars)
232223
.forEach(
233224
row -> {
234-
if (ignoredPath.contains(row.getString(3))) {
235-
return;
236-
}
237-
238225
// Store row values in array entries to provide the correct context inside the
239226
// executor.
240227
final byte[] rawDocument = row.getBlob(0);
@@ -264,9 +251,8 @@ private Map<DocumentKey, MutableDocument> getAll(
264251
}
265252

266253
@Override
267-
public Map<DocumentKey, MutableDocument> getAll(
268-
ResourcePath collection, IndexOffset offset, Set<DocumentKey> ignoreSet) {
269-
return getAll(Collections.singletonList(collection), offset, Integer.MAX_VALUE, ignoreSet);
254+
public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
255+
return getAll(Collections.singletonList(collection), offset, Integer.MAX_VALUE);
270256
}
271257

272258
@Override

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.Comparator;
2020

2121
/**
22-
* Represents a document in Firestore with a key, data and whether the data has local mutations
23-
* applied to it.
22+
* Represents a document in Firestore with a key, version, data and whether the data has local
23+
* mutations applied to it.
2424
*/
2525
public interface Document {
2626
/** A document comparator that returns document by key and key only. */
@@ -29,6 +29,18 @@ public interface Document {
2929
/** The key for this document */
3030
DocumentKey getKey();
3131

32+
/**
33+
* Returns the version of this document if it exists or a version at which this document was
34+
* guaranteed to not exist.
35+
*/
36+
SnapshotVersion getVersion();
37+
38+
/**
39+
* Returns the timestamp at which this document was read from the remote server. Returns
40+
* `SnapshotVersion.NONE` for documents created by the user.
41+
*/
42+
SnapshotVersion getReadTime();
43+
3244
/**
3345
* Returns whether this document is valid (i.e. it is an entry in the RemoteDocumentCache, was
3446
* created by a mutation or read from the backend).
@@ -62,5 +74,6 @@ public interface Document {
6274
*/
6375
boolean hasPendingWrites();
6476

65-
InternalDocument getInternalReference();
77+
/** Creates a mutable copy of this document. */
78+
MutableDocument mutableCopy();
6679
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ public static IndexOffset create(SnapshotVersion readTime) {
150150

151151
/** Creates a new offset based on the provided document. */
152152
public static IndexOffset fromDocument(Document document) {
153-
return new AutoValue_FieldIndex_IndexOffset(
154-
document.getInternalReference().getReadTime(), document.getKey());
153+
return new AutoValue_FieldIndex_IndexOffset(document.getReadTime(), document.getKey());
155154
}
156155

157156
/**

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

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

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* one of these states even after all mutations have been applied, {@link #isValidDocument} returns
2727
* false and the document should be removed from all views.
2828
*/
29-
public final class MutableDocument implements InternalDocument {
29+
public final class MutableDocument implements Document {
3030

3131
private enum DocumentType {
3232
/**
@@ -159,6 +159,7 @@ public MutableDocument setHasCommittedMutations() {
159159

160160
public MutableDocument setHasLocalMutations() {
161161
this.documentState = DocumentState.HAS_LOCAL_MUTATIONS;
162+
this.version = SnapshotVersion.NONE;
162163
return this;
163164
}
164165

@@ -197,11 +198,6 @@ public boolean hasPendingWrites() {
197198
return hasLocalMutations() || hasCommittedMutations();
198199
}
199200

200-
@Override
201-
public InternalDocument getInternalReference() {
202-
return (InternalDocument) this;
203-
}
204-
205201
@Override
206202
public ObjectValue getData() {
207203
return value;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.Collection;
3232
import java.util.List;
3333
import java.util.Map;
34-
import java.util.Set;
3534

3635
/**
3736
* A test-only QueryEngine that forwards all API calls and exposes the number of documents and
@@ -156,9 +155,8 @@ public Map<DocumentKey, MutableDocument> getAll(
156155
}
157156

158157
@Override
159-
public Map<DocumentKey, MutableDocument> getAll(
160-
ResourcePath collection, IndexOffset offset, Set<DocumentKey> ignoreSet) {
161-
Map<DocumentKey, MutableDocument> result = subject.getAll(collection, offset, ignoreSet);
158+
public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
159+
Map<DocumentKey, MutableDocument> result = subject.getAll(collection, offset);
162160
documentsReadByCollection[0] += result.size();
163161
return result;
164162
}

0 commit comments

Comments
 (0)