15
15
package com .google .firebase .firestore .local ;
16
16
17
17
import androidx .annotation .Nullable ;
18
- import com .google .firebase .Timestamp ;
19
18
import com .google .firebase .database .collection .ImmutableSortedMap ;
20
19
import com .google .firebase .database .collection .ImmutableSortedSet ;
21
20
import com .google .firebase .firestore .core .Query ;
26
25
import com .google .firebase .firestore .model .ResourcePath ;
27
26
import com .google .firebase .firestore .model .SnapshotVersion ;
28
27
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 ;
31
29
import java .util .Collection ;
32
- import java .util .List ;
33
30
import java .util .Map ;
34
31
35
32
/**
39
36
class CountingQueryEngine extends QueryEngine {
40
37
private final QueryEngine queryEngine ;
41
38
42
- private final int [] mutationsReadByCollection = new int [] {0 };
43
- private final int [] mutationsReadByKey = new int [] {0 };
44
39
private final int [] overlaysReadByCollection = new int [] {0 };
45
40
private final int [] overlaysReadByKey = new int [] {0 };
46
41
private final int [] documentsReadByCollection = new int [] {0 };
@@ -51,10 +46,8 @@ class CountingQueryEngine extends QueryEngine {
51
46
}
52
47
53
48
void resetCounts () {
54
- mutationsReadByCollection [0 ] = 0 ;
55
- mutationsReadByKey [0 ] = 0 ;
56
49
overlaysReadByCollection [0 ] = 0 ;
57
- mutationsReadByKey [0 ] = 0 ;
50
+ overlaysReadByKey [0 ] = 0 ;
58
51
documentsReadByCollection [0 ] = 0 ;
59
52
documentsReadByKey [0 ] = 0 ;
60
53
}
@@ -64,9 +57,9 @@ public void initialize(LocalDocumentsView localDocuments, IndexManager indexMana
64
57
LocalDocumentsView wrappedView =
65
58
new LocalDocumentsView (
66
59
wrapRemoteDocumentCache (localDocuments .getRemoteDocumentCache ()),
67
- wrapMutationQueue ( localDocuments .getMutationQueue () ),
68
- localDocuments .getDocumentOverlayCache (),
69
- localDocuments . getIndexManager () );
60
+ localDocuments .getMutationQueue (),
61
+ wrapOverlayCache ( localDocuments .getDocumentOverlayCache () ),
62
+ indexManager );
70
63
queryEngine .initialize (wrappedView , indexManager );
71
64
}
72
65
@@ -78,6 +71,11 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
78
71
return queryEngine .getDocumentsMatchingQuery (query , lastLimboFreeSnapshotVersion , remoteKeys );
79
72
}
80
73
74
+ /** Returns the query engine that is used as the backing implementation. */
75
+ QueryEngine getSubject () {
76
+ return queryEngine ;
77
+ }
78
+
81
79
/**
82
80
* Returns the number of documents returned by the RemoteDocumentCache's `getAll()` API (since the
83
81
* last call to `resetCounts()`)
@@ -95,20 +93,19 @@ int getDocumentsReadByKey() {
95
93
}
96
94
97
95
/**
98
- * Returns the number of mutations returned by the MutationQueue's
99
- * `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()`)
100
98
*/
101
- int getMutationsReadByCollection () {
102
- return mutationsReadByCollection [0 ];
99
+ int getOverlaysReadByCollection () {
100
+ return overlaysReadByCollection [0 ];
103
101
}
104
102
105
103
/**
106
- * Returns the number of mutations returned by the MutationQueue's
107
- * `getAllMutationBatchesAffectingDocumentKey()` and
108
- * `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()`)
109
106
*/
110
- int getMutationsReadByKey () {
111
- return mutationsReadByKey [0 ];
107
+ int getOverlaysReadByKey () {
108
+ return overlaysReadByKey [0 ];
112
109
}
113
110
114
111
private RemoteDocumentCache wrapRemoteDocumentCache (RemoteDocumentCache subject ) {
@@ -167,96 +164,40 @@ public SnapshotVersion getLatestReadTime() {
167
164
};
168
165
}
169
166
170
- private MutationQueue wrapMutationQueue (MutationQueue subject ) {
171
- return new MutationQueue () {
172
- @ Override
173
- public void start () {
174
- subject .start ();
175
- }
176
-
177
- @ Override
178
- public boolean isEmpty () {
179
- return subject .isEmpty ();
180
- }
181
-
182
- @ Override
183
- public void acknowledgeBatch (MutationBatch batch , ByteString streamToken ) {
184
- subject .acknowledgeBatch (batch , streamToken );
185
- }
186
-
187
- @ Override
188
- public ByteString getLastStreamToken () {
189
- return subject .getLastStreamToken ();
190
- }
191
-
192
- @ Override
193
- public void setLastStreamToken (ByteString streamToken ) {
194
- subject .setLastStreamToken (streamToken );
195
- }
196
-
197
- @ Override
198
- public MutationBatch addMutationBatch (
199
- Timestamp localWriteTime , List <Mutation > baseMutations , List <Mutation > mutations ) {
200
- return subject .addMutationBatch (localWriteTime , baseMutations , mutations );
201
- }
202
-
167
+ private DocumentOverlayCache wrapOverlayCache (DocumentOverlayCache subject ) {
168
+ return new DocumentOverlayCache () {
203
169
@ Nullable
204
170
@ Override
205
- public MutationBatch lookupMutationBatch (int batchId ) {
206
- return subject .lookupMutationBatch (batchId );
207
- }
208
-
209
- @ Nullable
210
- @ Override
211
- public MutationBatch getNextMutationBatchAfterBatchId (int batchId ) {
212
- return subject .getNextMutationBatchAfterBatchId (batchId );
213
- }
214
-
215
- @ Override
216
- public int getHighestUnacknowledgedBatchId () {
217
- return subject .getHighestUnacknowledgedBatchId ();
171
+ public Overlay getOverlay (DocumentKey key ) {
172
+ ++overlaysReadByKey [0 ];
173
+ return subject .getOverlay (key );
218
174
}
219
175
220
176
@ Override
221
- public List <MutationBatch > getAllMutationBatches () {
222
- List <MutationBatch > result = subject .getAllMutationBatches ();
223
- mutationsReadByKey [0 ] += result .size ();
224
- return result ;
177
+ public void saveOverlays (int largestBatchId , Map <DocumentKey , Mutation > overlays ) {
178
+ subject .saveOverlays (largestBatchId , overlays );
225
179
}
226
180
227
181
@ Override
228
- public List <MutationBatch > getAllMutationBatchesAffectingDocumentKey (
229
- DocumentKey documentKey ) {
230
- List <MutationBatch > result = subject .getAllMutationBatchesAffectingDocumentKey (documentKey );
231
- mutationsReadByKey [0 ] += result .size ();
232
- return result ;
182
+ public void removeOverlaysForBatchId (int batchId ) {
183
+ subject .removeOverlaysForBatchId (batchId );
233
184
}
234
185
235
186
@ Override
236
- public List <MutationBatch > getAllMutationBatchesAffectingDocumentKeys (
237
- Iterable <DocumentKey > documentKeys ) {
238
- List <MutationBatch > result =
239
- subject .getAllMutationBatchesAffectingDocumentKeys (documentKeys );
240
- 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 ();
241
190
return result ;
242
191
}
243
192
244
193
@ Override
245
- public List <MutationBatch > getAllMutationBatchesAffectingQuery (Query query ) {
246
- List <MutationBatch > result = subject .getAllMutationBatchesAffectingQuery (query );
247
- mutationsReadByCollection [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 ();
248
199
return result ;
249
200
}
250
-
251
- @ Override
252
- public void removeMutationBatch (MutationBatch batch ) {
253
- subject .removeMutationBatch (batch );
254
- }
255
-
256
- @ Override
257
- public void performConsistencyCheck () {
258
- subject .performConsistencyCheck ();
259
- }
260
201
};
261
202
}
262
203
}
0 commit comments