Skip to content

Commit 788bc04

Browse files
Update LocalStoreTestCase to validate overlays
This updates the LocalStore tests to be able to verify how many overlays were processed. This will be used in the Mutation Backfill PR.
1 parent 275b3eb commit 788bc04

File tree

2 files changed

+34
-94
lines changed

2 files changed

+34
-94
lines changed

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

Lines changed: 29 additions & 88 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,8 +57,8 @@ public void initialize(LocalDocumentsView localDocuments, IndexManager indexMana
6057
LocalDocumentsView wrappedView =
6158
new LocalDocumentsView(
6259
wrapRemoteDocumentCache(localDocuments.getRemoteDocumentCache()),
63-
wrapMutationQueue(localDocuments.getMutationQueue()),
64-
localDocuments.getDocumentOverlayCache(),
60+
localDocuments.getMutationQueue(),
61+
wrapOverlayCache(localDocuments.getDocumentOverlayCache()),
6562
localDocuments.getIndexManager());
6663
queryEngine.initialize(wrappedView, indexManager);
6764
}
@@ -96,20 +93,20 @@ int getDocumentsReadByKey() {
9693
}
9794

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

106103
/**
107104
* Returns the number of mutations returned by the MutationQueue's
108105
* `getAllMutationBatchesAffectingDocumentKey()` and
109106
* `getAllMutationBatchesAffectingDocumentKeys()` APIs (since the last call to `resetCounts()`)
110107
*/
111-
int getMutationsReadByKey() {
112-
return mutationsReadByKey[0];
108+
int getOverlaysReadByKey() {
109+
return overlaysReadByKey[0];
113110
}
114111

115112
private RemoteDocumentCache wrapRemoteDocumentCache(RemoteDocumentCache subject) {
@@ -168,96 +165,40 @@ public SnapshotVersion getLatestReadTime() {
168165
};
169166
}
170167

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-
168+
private DocumentOverlayCache wrapOverlayCache(DocumentOverlayCache subject) {
169+
return new DocumentOverlayCache() {
204170
@Nullable
205171
@Override
206-
public MutationBatch lookupMutationBatch(int batchId) {
207-
return subject.lookupMutationBatch(batchId);
172+
public Overlay getOverlay(DocumentKey key) {
173+
overlaysReadByKey[0] += 1;
174+
return subject.getOverlay(key);
208175
}
209176

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

216182
@Override
217-
public int getHighestUnacknowledgedBatchId() {
218-
return subject.getHighestUnacknowledgedBatchId();
183+
public void removeOverlaysForBatchId(int batchId) {
184+
subject.removeOverlaysForBatchId(batchId);
219185
}
220186

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

228194
@Override
229-
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(
230-
DocumentKey documentKey) {
231-
List<MutationBatch> result = subject.getAllMutationBatchesAffectingDocumentKey(documentKey);
232-
mutationsReadByKey[0] += result.size();
195+
public Map<DocumentKey, Overlay> getOverlays(
196+
String collectionGroup, int sinceBatchId, int count) {
197+
Map<DocumentKey, Overlay> result =
198+
subject.getOverlays(collectionGroup, sinceBatchId, count);
199+
overlaysReadByCollection[0] += result.size();
233200
return result;
234201
}
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-
}
261202
};
262203
}
263204
}

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)