Skip to content

Commit a7761a4

Browse files
Expose operation counts from Persistence layer (#595)
1 parent dd9b4ac commit a7761a4

16 files changed

+407
-151
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ final class MemoryMutationQueue implements MutationQueue {
6969
private ByteString lastStreamToken;
7070

7171
private final MemoryPersistence persistence;
72+
private final StatsCollector statsCollector;
7273

73-
MemoryMutationQueue(MemoryPersistence persistence) {
74+
MemoryMutationQueue(MemoryPersistence persistence, StatsCollector statsCollector) {
7475
this.persistence = persistence;
76+
this.statsCollector = statsCollector;
7577
queue = new ArrayList<>();
7678

7779
batchesByDocumentKey = new ImmutableSortedSet<>(emptyList(), DocumentReference.BY_KEY);
@@ -154,12 +156,16 @@ public MutationBatch addMutationBatch(
154156
.addToCollectionParentIndex(mutation.getKey().getPath().popLast());
155157
}
156158

159+
statsCollector.recordRowsWritten(STATS_TAG, 1);
160+
157161
return batch;
158162
}
159163

160164
@Nullable
161165
@Override
162166
public MutationBatch lookupMutationBatch(int batchId) {
167+
statsCollector.recordRowsRead(STATS_TAG, 1);
168+
163169
int index = indexOfBatchId(batchId);
164170
if (index < 0 || index >= queue.size()) {
165171
return null;
@@ -203,6 +209,8 @@ public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(DocumentKey
203209
result.add(batch);
204210
}
205211

212+
statsCollector.recordRowsRead(STATS_TAG, result.size());
213+
206214
return result;
207215
}
208216

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,38 @@ public final class MemoryPersistence extends Persistence {
3535
private final MemoryIndexManager indexManager;
3636
private final MemoryQueryCache queryCache;
3737
private final MemoryRemoteDocumentCache remoteDocumentCache;
38+
private final StatsCollector statsCollector;
3839
private ReferenceDelegate referenceDelegate;
3940

4041
private boolean started;
4142

4243
public static MemoryPersistence createEagerGcMemoryPersistence() {
43-
MemoryPersistence persistence = new MemoryPersistence();
44+
return createEagerGcMemoryPersistence(StatsCollector.NO_OP_STATS_COLLECTOR);
45+
}
46+
47+
public static MemoryPersistence createEagerGcMemoryPersistence(StatsCollector statsCollector) {
48+
MemoryPersistence persistence = new MemoryPersistence(statsCollector);
4449
persistence.setReferenceDelegate(new MemoryEagerReferenceDelegate(persistence));
4550
return persistence;
4651
}
4752

4853
public static MemoryPersistence createLruGcMemoryPersistence(
49-
LruGarbageCollector.Params params, LocalSerializer serializer) {
50-
MemoryPersistence persistence = new MemoryPersistence();
54+
LruGarbageCollector.Params params,
55+
StatsCollector statsCollector,
56+
LocalSerializer serializer) {
57+
MemoryPersistence persistence = new MemoryPersistence(statsCollector);
5158
persistence.setReferenceDelegate(
5259
new MemoryLruReferenceDelegate(persistence, params, serializer));
5360
return persistence;
5461
}
5562

5663
/** Use static helpers to instantiate */
57-
private MemoryPersistence() {
64+
private MemoryPersistence(StatsCollector statsCollector) {
65+
this.statsCollector = statsCollector;
5866
mutationQueues = new HashMap<>();
5967
indexManager = new MemoryIndexManager();
6068
queryCache = new MemoryQueryCache(this);
61-
remoteDocumentCache = new MemoryRemoteDocumentCache(this);
69+
remoteDocumentCache = new MemoryRemoteDocumentCache(this, statsCollector);
6270
}
6371

6472
@Override
@@ -93,7 +101,7 @@ private void setReferenceDelegate(ReferenceDelegate delegate) {
93101
MutationQueue getMutationQueue(User user) {
94102
MemoryMutationQueue queue = mutationQueues.get(user);
95103
if (queue == null) {
96-
queue = new MemoryMutationQueue(this);
104+
queue = new MemoryMutationQueue(this, statsCollector);
97105
mutationQueues.put(user, queue);
98106
}
99107
return queue;

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ final class MemoryRemoteDocumentCache implements RemoteDocumentCache {
3636
private ImmutableSortedMap<DocumentKey, MaybeDocument> docs;
3737

3838
private final MemoryPersistence persistence;
39+
private StatsCollector statsCollector;
3940

40-
MemoryRemoteDocumentCache(MemoryPersistence persistence) {
41+
MemoryRemoteDocumentCache(MemoryPersistence persistence, StatsCollector statsCollector) {
4142
docs = emptyMaybeDocumentMap();
43+
this.statsCollector = statsCollector;
4244
this.persistence = persistence;
4345
}
4446

@@ -51,12 +53,14 @@ public void add(MaybeDocument document) {
5153

5254
@Override
5355
public void remove(DocumentKey key) {
56+
statsCollector.recordRowsDeleted(STATS_TAG, 1);
5457
docs = docs.remove(key);
5558
}
5659

5760
@Nullable
5861
@Override
5962
public MaybeDocument get(DocumentKey key) {
63+
statsCollector.recordRowsRead(STATS_TAG, 1);
6064
return docs.get(key);
6165
}
6266

@@ -70,6 +74,7 @@ public Map<DocumentKey, MaybeDocument> getAll(Iterable<DocumentKey> keys) {
7074
result.put(key, get(key));
7175
}
7276

77+
statsCollector.recordRowsRead(STATS_TAG, result.size());
7378
return result;
7479
}
7580

@@ -85,8 +90,14 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
8590
ResourcePath queryPath = query.getPath();
8691
DocumentKey prefix = DocumentKey.fromPath(queryPath.append(""));
8792
Iterator<Map.Entry<DocumentKey, MaybeDocument>> iterator = docs.iteratorFrom(prefix);
93+
94+
int rowsRead = 0;
95+
8896
while (iterator.hasNext()) {
8997
Map.Entry<DocumentKey, MaybeDocument> entry = iterator.next();
98+
99+
++rowsRead;
100+
90101
DocumentKey key = entry.getKey();
91102
if (!queryPath.isPrefixOf(key.getPath())) {
92103
break;
@@ -103,6 +114,8 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(Qu
103114
}
104115
}
105116

117+
statsCollector.recordRowsRead(STATS_TAG, rowsRead);
118+
106119
return result;
107120
}
108121

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
/** A queue of mutations to apply to the remote store. */
2727
interface MutationQueue {
28+
/** The tag used by the StatsCollector. */
29+
String STATS_TAG = "mutations";
30+
2831
/**
2932
* Starts the mutation queue, performing any initial reads that might be required to establish
3033
* invariants, etc.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
* instances (indicating that the document is known to not exist).
3131
*/
3232
interface RemoteDocumentCache {
33+
/** The tag used by the StatsCollector. */
34+
String STATS_TAG = "remote_documents";
35+
3336
/**
3437
* Adds or replaces an entry in the cache.
3538
*

0 commit comments

Comments
 (0)