1515package com .google .firebase .firestore .local ;
1616
1717import androidx .annotation .Nullable ;
18- import com .google .firebase .Timestamp ;
1918import com .google .firebase .database .collection .ImmutableSortedMap ;
2019import com .google .firebase .database .collection .ImmutableSortedSet ;
2120import com .google .firebase .firestore .core .Query ;
2625import com .google .firebase .firestore .model .ResourcePath ;
2726import com .google .firebase .firestore .model .SnapshotVersion ;
2827import 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 ;
3129import java .util .Collection ;
32- import java .util .List ;
3330import java .util .Map ;
3431
3532/**
3936class 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}
0 commit comments