Skip to content

Commit 58c706b

Browse files
Update LocalStoreTestCase to validate overlays (#3332)
1 parent 96a4b39 commit 58c706b

File tree

3 files changed

+38
-104
lines changed

3 files changed

+38
-104
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ DocumentOverlayCache getDocumentOverlayCache() {
7676
return documentOverlayCache;
7777
}
7878

79-
@VisibleForTesting
80-
IndexManager getIndexManager() {
81-
return indexManager;
82-
}
83-
8479
/**
8580
* Returns the the local view of the document identified by {@code key}.
8681
*

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

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

1717
import androidx.annotation.Nullable;
18-
import com.google.firebase.Timestamp;
1918
import com.google.firebase.database.collection.ImmutableSortedMap;
2019
import com.google.firebase.database.collection.ImmutableSortedSet;
2120
import com.google.firebase.firestore.core.Query;
@@ -26,10 +25,8 @@
2625
import com.google.firebase.firestore.model.ResourcePath;
2726
import com.google.firebase.firestore.model.SnapshotVersion;
2827
import com.google.firebase.firestore.model.mutation.Mutation;
29-
import com.google.firebase.firestore.model.mutation.MutationBatch;
30-
import com.google.protobuf.ByteString;
28+
import com.google.firebase.firestore.model.mutation.Overlay;
3129
import java.util.Collection;
32-
import java.util.List;
3330
import java.util.Map;
3431

3532
/**
@@ -39,8 +36,8 @@
3936
class CountingQueryEngine extends QueryEngine {
4037
private final QueryEngine queryEngine;
4138

42-
private final int[] mutationsReadByCollection = new int[] {0};
43-
private final int[] mutationsReadByKey = new int[] {0};
39+
private final int[] overlaysReadByCollection = new int[] {0};
40+
private final int[] overlaysReadByKey = new int[] {0};
4441
private final int[] documentsReadByCollection = new int[] {0};
4542
private final int[] documentsReadByKey = new int[] {0};
4643

@@ -49,8 +46,8 @@ class CountingQueryEngine extends QueryEngine {
4946
}
5047

5148
void resetCounts() {
52-
mutationsReadByCollection[0] = 0;
53-
mutationsReadByKey[0] = 0;
49+
overlaysReadByCollection[0] = 0;
50+
overlaysReadByKey[0] = 0;
5451
documentsReadByCollection[0] = 0;
5552
documentsReadByKey[0] = 0;
5653
}
@@ -60,9 +57,9 @@ public void initialize(LocalDocumentsView localDocuments, IndexManager indexMana
6057
LocalDocumentsView wrappedView =
6158
new LocalDocumentsView(
6259
wrapRemoteDocumentCache(localDocuments.getRemoteDocumentCache()),
63-
wrapMutationQueue(localDocuments.getMutationQueue()),
64-
localDocuments.getDocumentOverlayCache(),
65-
localDocuments.getIndexManager());
60+
localDocuments.getMutationQueue(),
61+
wrapOverlayCache(localDocuments.getDocumentOverlayCache()),
62+
indexManager);
6663
queryEngine.initialize(wrappedView, indexManager);
6764
}
6865

@@ -96,20 +93,19 @@ int getDocumentsReadByKey() {
9693
}
9794

9895
/**
99-
* Returns the number of mutations returned by the MutationQueue's
100-
* `getAllMutationBatchesAffectingQuery()` API (since the last call to `resetCounts()`)
96+
* Returns the number of mutations returned by the OverlayCache's `getOverlays()` API (since the
97+
* last call to `resetCounts()`)
10198
*/
102-
int getMutationsReadByCollection() {
103-
return mutationsReadByCollection[0];
99+
int getOverlaysReadByCollection() {
100+
return overlaysReadByCollection[0];
104101
}
105102

106103
/**
107-
* Returns the number of mutations returned by the MutationQueue's
108-
* `getAllMutationBatchesAffectingDocumentKey()` and
109-
* `getAllMutationBatchesAffectingDocumentKeys()` APIs (since the last call to `resetCounts()`)
104+
* Returns the number of mutations returned by the OverlayCache's `getOverlay()` API (since the
105+
* last call to `resetCounts()`)
110106
*/
111-
int getMutationsReadByKey() {
112-
return mutationsReadByKey[0];
107+
int getOverlaysReadByKey() {
108+
return overlaysReadByKey[0];
113109
}
114110

115111
private RemoteDocumentCache wrapRemoteDocumentCache(RemoteDocumentCache subject) {
@@ -168,96 +164,40 @@ public SnapshotVersion getLatestReadTime() {
168164
};
169165
}
170166

171-
private MutationQueue wrapMutationQueue(MutationQueue subject) {
172-
return new MutationQueue() {
173-
@Override
174-
public void start() {
175-
subject.start();
176-
}
177-
178-
@Override
179-
public boolean isEmpty() {
180-
return subject.isEmpty();
181-
}
182-
183-
@Override
184-
public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
185-
subject.acknowledgeBatch(batch, streamToken);
186-
}
187-
188-
@Override
189-
public ByteString getLastStreamToken() {
190-
return subject.getLastStreamToken();
191-
}
192-
193-
@Override
194-
public void setLastStreamToken(ByteString streamToken) {
195-
subject.setLastStreamToken(streamToken);
196-
}
197-
198-
@Override
199-
public MutationBatch addMutationBatch(
200-
Timestamp localWriteTime, List<Mutation> baseMutations, List<Mutation> mutations) {
201-
return subject.addMutationBatch(localWriteTime, baseMutations, mutations);
202-
}
203-
167+
private DocumentOverlayCache wrapOverlayCache(DocumentOverlayCache subject) {
168+
return new DocumentOverlayCache() {
204169
@Nullable
205170
@Override
206-
public MutationBatch lookupMutationBatch(int batchId) {
207-
return subject.lookupMutationBatch(batchId);
171+
public Overlay getOverlay(DocumentKey key) {
172+
++overlaysReadByKey[0];
173+
return subject.getOverlay(key);
208174
}
209175

210-
@Nullable
211176
@Override
212-
public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
213-
return subject.getNextMutationBatchAfterBatchId(batchId);
177+
public void saveOverlays(int largestBatchId, Map<DocumentKey, Mutation> overlays) {
178+
subject.saveOverlays(largestBatchId, overlays);
214179
}
215180

216181
@Override
217-
public int getHighestUnacknowledgedBatchId() {
218-
return subject.getHighestUnacknowledgedBatchId();
182+
public void removeOverlaysForBatchId(int batchId) {
183+
subject.removeOverlaysForBatchId(batchId);
219184
}
220185

221186
@Override
222-
public List<MutationBatch> getAllMutationBatches() {
223-
List<MutationBatch> result = subject.getAllMutationBatches();
224-
mutationsReadByKey[0] += result.size();
187+
public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceBatchId) {
188+
Map<DocumentKey, Overlay> result = subject.getOverlays(collection, sinceBatchId);
189+
overlaysReadByCollection[0] += result.size();
225190
return result;
226191
}
227192

228193
@Override
229-
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(
230-
DocumentKey documentKey) {
231-
List<MutationBatch> result = subject.getAllMutationBatchesAffectingDocumentKey(documentKey);
232-
mutationsReadByKey[0] += result.size();
194+
public Map<DocumentKey, Overlay> getOverlays(
195+
String collectionGroup, int sinceBatchId, int count) {
196+
Map<DocumentKey, Overlay> result =
197+
subject.getOverlays(collectionGroup, sinceBatchId, count);
198+
overlaysReadByCollection[0] += result.size();
233199
return result;
234200
}
235-
236-
@Override
237-
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(
238-
Iterable<DocumentKey> documentKeys) {
239-
List<MutationBatch> result =
240-
subject.getAllMutationBatchesAffectingDocumentKeys(documentKeys);
241-
mutationsReadByKey[0] += result.size();
242-
return result;
243-
}
244-
245-
@Override
246-
public List<MutationBatch> getAllMutationBatchesAffectingQuery(Query query) {
247-
List<MutationBatch> result = subject.getAllMutationBatchesAffectingQuery(query);
248-
mutationsReadByCollection[0] += result.size();
249-
return result;
250-
}
251-
252-
@Override
253-
public void removeMutationBatch(MutationBatch batch) {
254-
subject.removeMutationBatch(batch);
255-
}
256-
257-
@Override
258-
public void performConsistencyCheck() {
259-
subject.performConsistencyCheck();
260-
}
261201
};
262202
}
263203
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ private void assertHasNamedQuery(NamedQuery expectedNamedQuery) {
324324
}
325325

326326
/**
327-
* Asserts the expected numbers of mutations read by the MutationQueue since the last call to
327+
* Asserts the expected numbers of mutations read by the OverlayQueue since the last call to
328328
* `resetPersistenceStats()`.
329329
*/
330-
private void assertMutationsRead(int byKey, int byCollection) {
330+
private void assertOverlaysRead(int byKey, int byCollection) {
331+
assertEquals("Overlays read (by key)", byKey, queryEngine.getOverlaysReadByKey());
331332
assertEquals(
332-
"Mutations read (by collection)", byCollection, queryEngine.getMutationsReadByCollection());
333-
assertEquals("Mutations read (by key)", byKey, queryEngine.getMutationsReadByKey());
333+
"Overlays read (by collection)", byCollection, queryEngine.getOverlaysReadByCollection());
334334
}
335335

336336
/**
@@ -975,8 +975,7 @@ public void testReadsAllDocumentsForInitialCollectionQueries() {
975975

976976
localStore.executeQuery(query, /* usePreviousResults= */ true);
977977
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
978-
// No mutations are read because only overlay is needed.
979-
assertMutationsRead(/* byKey= */ 0, /* byCollection= */ 0);
978+
assertOverlaysRead(/* byKey= */ 0, /* byCollection= */ 1);
980979
}
981980

982981
@Test

0 commit comments

Comments
 (0)