Skip to content

Commit 3a99c27

Browse files
author
Greg Soltis
authored
Port production usages of QueryCache (#2211)
* Porting production usages * Remove FSTQueryCache and implementations * Switch size() to size_t * Style
1 parent a987cf9 commit 3a99c27

19 files changed

+88
-617
lines changed

Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
#import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
2626
#import "Firestore/Source/Local/FSTMutationQueue.h"
2727
#import "Firestore/Source/Local/FSTPersistence.h"
28-
#import "Firestore/Source/Local/FSTQueryCache.h"
2928
#import "Firestore/Source/Model/FSTDocument.h"
3029
#import "Firestore/Source/Model/FSTFieldValue.h"
3130
#import "Firestore/Source/Model/FSTMutation.h"
3231
#import "Firestore/Source/Util/FSTClasses.h"
3332
#include "Firestore/core/src/firebase/firestore/auth/user.h"
33+
#include "Firestore/core/src/firebase/firestore/local/query_cache.h"
3434
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
3535
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
3636
#include "Firestore/core/src/firebase/firestore/model/precondition.h"
@@ -42,6 +42,7 @@
4242
using firebase::firestore::auth::User;
4343
using firebase::firestore::local::LruParams;
4444
using firebase::firestore::local::LruResults;
45+
using firebase::firestore::local::QueryCache;
4546
using firebase::firestore::local::RemoteDocumentCache;
4647
using firebase::firestore::model::DocumentKey;
4748
using firebase::firestore::model::DocumentKeyHash;
@@ -58,7 +59,7 @@ @implementation FSTLRUGarbageCollectorTests {
5859
FSTObjectValue *_testValue;
5960
FSTObjectValue *_bigObjectValue;
6061
id<FSTPersistence> _persistence;
61-
id<FSTQueryCache> _queryCache;
62+
QueryCache *_queryCache;
6263
RemoteDocumentCache *_documentCache;
6364
id<FSTMutationQueue> _mutationQueue;
6465
id<FSTLRUDelegate> _lruDelegate;
@@ -151,7 +152,7 @@ - (FSTQueryData *)nextTestQuery {
151152

152153
- (FSTQueryData *)addNextQueryInTransaction {
153154
FSTQueryData *queryData = [self nextTestQuery];
154-
[_queryCache addQueryData:queryData];
155+
_queryCache->AddTarget(queryData);
155156
return queryData;
156157
}
157158

@@ -161,7 +162,7 @@ - (void)updateTargetInTransaction:(FSTQueryData *)queryData {
161162
[queryData queryDataByReplacingSnapshotVersion:queryData.snapshotVersion
162163
resumeToken:token
163164
sequenceNumber:_persistence.currentSequenceNumber];
164-
[_queryCache updateQueryData:updated];
165+
_queryCache->UpdateTarget(updated);
165166
}
166167

167168
- (FSTQueryData *)addNextQuery {
@@ -195,11 +196,11 @@ - (void)markDocumentEligibleForGCInTransaction:(const DocumentKey &)docKey {
195196
}
196197

197198
- (void)addDocument:(const DocumentKey &)docKey toTarget:(TargetId)targetId {
198-
[_queryCache addMatchingKeys:DocumentKeySet{docKey} forTargetID:targetId];
199+
_queryCache->AddMatchingKeys(DocumentKeySet{docKey}, targetId);
199200
}
200201

201202
- (void)removeDocument:(const DocumentKey &)docKey fromTarget:(TargetId)targetId {
202-
[_queryCache removeMatchingKeys:DocumentKeySet{docKey} forTargetID:targetId];
203+
_queryCache->RemoveMatchingKeys(DocumentKeySet{docKey}, targetId);
203204
}
204205

205206
/**
@@ -388,9 +389,9 @@ - (void)testRemoveQueriesUpThroughSequenceNumber {
388389
XCTAssertEqual(10, removed);
389390
// Make sure we removed the even targets with targetID <= 20.
390391
_persistence.run("verify remaining targets are > 20 or odd", [&]() {
391-
[_queryCache enumerateTargetsUsingBlock:^(FSTQueryData *queryData, BOOL *stop) {
392+
_queryCache->EnumerateTargets(^(FSTQueryData *queryData, BOOL *stop) {
392393
XCTAssertTrue(queryData.targetID > 20 || queryData.targetID % 2 == 1);
393-
}];
394+
});
394395
});
395396
[_persistence shutdown];
396397
}
@@ -463,7 +464,7 @@ - (void)testRemoveOrphanedDocuments {
463464
_persistence.run("verify", [&]() {
464465
for (const DocumentKey &key : toBeRemoved) {
465466
XCTAssertNil(_documentCache->Get(key));
466-
XCTAssertFalse([_queryCache containsKey:key]);
467+
XCTAssertFalse(_queryCache->Contains(key));
467468
}
468469
for (const DocumentKey &key : expectedRetained) {
469470
XCTAssertNotNil(_documentCache->Get(key), @"Missing document %s", key.ToString().c_str());
@@ -645,7 +646,7 @@ - (void)testRemoveTargetsThenGC {
645646
for (const DocumentKey &key : expectedRemoved) {
646647
XCTAssertNil(_documentCache->Get(key), @"Did not expect to find %s in document cache",
647648
key.ToString().c_str());
648-
XCTAssertFalse([_queryCache containsKey:key], @"Did not expect to find %s in queryCache",
649+
XCTAssertFalse(_queryCache->Contains(key), @"Did not expect to find %s in queryCache",
649650
key.ToString().c_str());
650651
[self expectSentinelRemoved:key];
651652
}

Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
2525
#import "Firestore/Source/Local/FSTLevelDB.h"
2626
#import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
27-
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
2827

2928
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
3029
#include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h"
30+
#include "Firestore/core/src/firebase/firestore/local/leveldb_query_cache.h"
3131
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
3232
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
3333
#include "absl/strings/match.h"
@@ -43,6 +43,7 @@
4343
using firebase::firestore::local::LevelDbMigrations;
4444
using firebase::firestore::local::LevelDbMutationKey;
4545
using firebase::firestore::local::LevelDbMutationQueueKey;
46+
using firebase::firestore::local::LevelDbQueryCache;
4647
using firebase::firestore::local::LevelDbQueryTargetKey;
4748
using firebase::firestore::local::LevelDbRemoteDocumentKey;
4849
using firebase::firestore::local::LevelDbTargetDocumentKey;
@@ -86,11 +87,11 @@ - (void)tearDown {
8687
}
8788

8889
- (void)testAddsTargetGlobal {
89-
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
90+
FSTPBTargetGlobal *metadata = LevelDbQueryCache::ReadMetadata(_db.get());
9091
XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
9192
LevelDbMigrations::RunMigrations(_db.get());
9293

93-
metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
94+
metadata = LevelDbQueryCache::ReadMetadata(_db.get());
9495
XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
9596
}
9697

@@ -166,7 +167,7 @@ - (void)testDropsTheQueryCache {
166167
ASSERT_FOUND(transaction, key);
167168
}
168169

169-
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
170+
FSTPBTargetGlobal *metadata = LevelDbQueryCache::ReadMetadata(_db.get());
170171
XCTAssertNotNil(metadata, @"Metadata should have been added");
171172
XCTAssertEqual(metadata.targetCount, 0);
172173
}
@@ -209,7 +210,7 @@ - (void)testAddsSentinelRows {
209210
LevelDbTransaction transaction(_db.get(), "Setup");
210211

211212
// Set up target global
212-
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
213+
FSTPBTargetGlobal *metadata = LevelDbQueryCache::ReadMetadata(_db.get());
213214
// Expect that documents missing a row will get the new number
214215
metadata.highestListenSequenceNumber = new_sequence_number;
215216
transaction.Put(LevelDbTargetGlobalKey::Key(), metadata);

Firestore/Example/Tests/Local/FSTLevelDBQueryCacheTests.mm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
18-
1917
#import "Firestore/Source/Core/FSTQuery.h"
2018
#import "Firestore/Source/Local/FSTLevelDB.h"
2119
#import "Firestore/Source/Local/FSTQueryData.h"
@@ -55,7 +53,7 @@ @interface FSTLevelDBQueryCacheTests : FSTQueryCacheTests
5553
@implementation FSTLevelDBQueryCacheTests
5654

5755
- (LevelDbQueryCache *)getCache:(id<FSTPersistence>)persistence {
58-
return ((FSTLevelDBQueryCache *)([persistence queryCache])).cache;
56+
return static_cast<LevelDbQueryCache *>(persistence.queryCache);
5957
}
6058

6159
- (void)setUp {

Firestore/Example/Tests/Local/FSTLocalStoreTests.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#import "Firestore/Source/Core/FSTQuery.h"
2323
#import "Firestore/Source/Local/FSTLocalWriteResult.h"
2424
#import "Firestore/Source/Local/FSTPersistence.h"
25-
#import "Firestore/Source/Local/FSTQueryCache.h"
2625
#import "Firestore/Source/Local/FSTQueryData.h"
2726
#import "Firestore/Source/Model/FSTDocument.h"
2827
#import "Firestore/Source/Model/FSTDocumentSet.h"

Firestore/Example/Tests/Local/FSTMemoryQueryCacheTests.mm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "Firestore/Source/Local/FSTMemoryQueryCache.h"
18-
1917
#import "Firestore/Source/Local/FSTMemoryPersistence.h"
2018

2119
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
@@ -37,7 +35,7 @@ - (void)setUp {
3735
[super setUp];
3836

3937
self.persistence = [FSTPersistenceTestHelpers eagerGCMemoryPersistence];
40-
self.queryCache = ((FSTMemoryQueryCache *)([self.persistence queryCache])).cache;
38+
self.queryCache = self.persistence.queryCache;
4139
}
4240

4341
- (void)tearDown {

Firestore/Source/Local/FSTLRUGarbageCollector.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2424
#include "Firestore/core/src/firebase/firestore/model/types.h"
2525

26-
@protocol FSTQueryCache;
27-
2826
@class FSTLRUGarbageCollector;
2927

3028
extern const firebase::firestore::model::ListenSequenceNumber kFSTListenSequenceNumberInvalid;
@@ -108,7 +106,7 @@ struct LruResults {
108106
- (size_t)byteSize;
109107

110108
/** Returns the number of targets and orphaned documents cached. */
111-
- (int32_t)sequenceNumberCount;
109+
- (size_t)sequenceNumberCount;
112110

113111
/** Access to the underlying LRU Garbage collector instance. */
114112
@property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;

Firestore/Source/Local/FSTLRUGarbageCollector.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#import "Firestore/Source/Local/FSTMutationQueue.h"
2424
#import "Firestore/Source/Local/FSTPersistence.h"
25-
#import "Firestore/Source/Local/FSTQueryCache.h"
2625
#include "Firestore/core/include/firebase/firestore/timestamp.h"
2726
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2827
#include "Firestore/core/src/firebase/firestore/util/log.h"
@@ -148,7 +147,7 @@ - (LruResults)runGCWithLiveTargets:(NSDictionary<NSNumber *, FSTQueryData *> *)l
148147
}
149148

150149
- (int)queryCountForPercentile:(NSUInteger)percentile {
151-
int totalCount = [_delegate sequenceNumberCount];
150+
size_t totalCount = [_delegate sequenceNumberCount];
152151
int setSize = (int)((percentile / 100.0f) * totalCount);
153152
return setSize;
154153
}

Firestore/Source/Local/FSTLevelDB.mm

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#import "Firestore/Source/Core/FSTListenSequence.h"
2424
#import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
2525
#import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
26-
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
2726
#import "Firestore/Source/Local/FSTReferenceSet.h"
2827
#import "Firestore/Source/Remote/FSTSerializerBeta.h"
2928

@@ -32,6 +31,7 @@
3231
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
3332
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
3433
#include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h"
34+
#include "Firestore/core/src/firebase/firestore/local/leveldb_query_cache.h"
3535
#include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h"
3636
#include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h"
3737
#include "Firestore/core/src/firebase/firestore/local/leveldb_util.h"
@@ -61,6 +61,7 @@
6161
using firebase::firestore::local::LevelDbDocumentTargetKey;
6262
using firebase::firestore::local::LevelDbMigrations;
6363
using firebase::firestore::local::LevelDbMutationKey;
64+
using firebase::firestore::local::LevelDbQueryCache;
6465
using firebase::firestore::local::LevelDbRemoteDocumentCache;
6566
using firebase::firestore::local::LevelDbTransaction;
6667
using firebase::firestore::local::LruParams;
@@ -87,6 +88,8 @@ - (size_t)byteSize;
8788

8889
@property(nonatomic, assign, getter=isStarted) BOOL started;
8990

91+
- (firebase::firestore::local::LevelDbQueryCache *)queryCache;
92+
9093
@end
9194

9295
/**
@@ -125,7 +128,7 @@ - (instancetype)initWithPersistence:(FSTLevelDB *)persistence lruParams:(LruPara
125128
}
126129

127130
- (void)start {
128-
ListenSequenceNumber highestSequenceNumber = _db.queryCache.highestListenSequenceNumber;
131+
ListenSequenceNumber highestSequenceNumber = _db.queryCache->highest_listen_sequence_number();
129132
_listenSequence = [[FSTListenSequence alloc] initStartingAfter:highestSequenceNumber];
130133
}
131134

@@ -156,7 +159,7 @@ - (void)removeTarget:(FSTQueryData *)queryData {
156159
[queryData queryDataByReplacingSnapshotVersion:queryData.snapshotVersion
157160
resumeToken:queryData.resumeToken
158161
sequenceNumber:[self currentSequenceNumber]];
159-
[_db.queryCache updateQueryData:updated];
162+
_db.queryCache->UpdateTarget(updated);
160163
}
161164

162165
- (void)addReference:(const DocumentKey &)key {
@@ -195,29 +198,26 @@ - (BOOL)isPinned:(const DocumentKey &)docKey {
195198
}
196199

197200
- (void)enumerateTargetsUsingBlock:(void (^)(FSTQueryData *queryData, BOOL *stop))block {
198-
FSTLevelDBQueryCache *queryCache = _db.queryCache;
199-
[queryCache enumerateTargetsUsingBlock:block];
201+
_db.queryCache->EnumerateTargets(block);
200202
}
201203

202204
- (void)enumerateMutationsUsingBlock:
203205
(void (^)(const DocumentKey &key, ListenSequenceNumber sequenceNumber, BOOL *stop))block {
204-
FSTLevelDBQueryCache *queryCache = _db.queryCache;
205-
[queryCache enumerateOrphanedDocumentsUsingBlock:block];
206+
_db.queryCache->EnumerateOrphanedDocuments(block);
206207
}
207208

208209
- (int)removeOrphanedDocumentsThroughSequenceNumber:(ListenSequenceNumber)upperBound {
209-
FSTLevelDBQueryCache *queryCache = _db.queryCache;
210210
__block int count = 0;
211-
[queryCache enumerateOrphanedDocumentsUsingBlock:^(
212-
const DocumentKey &docKey, ListenSequenceNumber sequenceNumber, BOOL *stop) {
213-
if (sequenceNumber <= upperBound) {
214-
if (![self isPinned:docKey]) {
215-
count++;
216-
self->_db.remoteDocumentCache->Remove(docKey);
217-
[self removeSentinel:docKey];
218-
}
219-
}
220-
}];
211+
_db.queryCache->EnumerateOrphanedDocuments(
212+
^(const DocumentKey &docKey, ListenSequenceNumber sequenceNumber, BOOL *stop) {
213+
if (sequenceNumber <= upperBound) {
214+
if (![self isPinned:docKey]) {
215+
count++;
216+
self->_db.remoteDocumentCache->Remove(docKey);
217+
[self removeSentinel:docKey];
218+
}
219+
}
220+
});
221221
return count;
222222
}
223223

@@ -227,12 +227,11 @@ - (void)removeSentinel:(const DocumentKey &)key {
227227

228228
- (int)removeTargetsThroughSequenceNumber:(ListenSequenceNumber)sequenceNumber
229229
liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries {
230-
FSTLevelDBQueryCache *queryCache = _db.queryCache;
231-
return [queryCache removeQueriesThroughSequenceNumber:sequenceNumber liveQueries:liveQueries];
230+
return _db.queryCache->RemoveTargets(sequenceNumber, liveQueries);
232231
}
233232

234-
- (int32_t)sequenceNumberCount {
235-
__block int32_t totalCount = [_db.queryCache count];
233+
- (size_t)sequenceNumberCount {
234+
__block size_t totalCount = _db.queryCache->size();
236235
[self enumerateMutationsUsingBlock:^(const DocumentKey &key, ListenSequenceNumber sequenceNumber,
237236
BOOL *stop) {
238237
totalCount++;
@@ -272,7 +271,7 @@ @implementation FSTLevelDB {
272271
std::unique_ptr<LevelDbRemoteDocumentCache> _documentCache;
273272
FSTTransactionRunner _transactionRunner;
274273
FSTLevelDBLRUDelegate *_referenceDelegate;
275-
FSTLevelDBQueryCache *_queryCache;
274+
std::unique_ptr<LevelDbQueryCache> _queryCache;
276275
std::set<std::string> _users;
277276
}
278277

@@ -339,14 +338,14 @@ - (instancetype)initWithLevelDB:(std::unique_ptr<leveldb::DB>)db
339338
_ptr = std::move(db);
340339
_directory = std::move(directory);
341340
_serializer = serializer;
342-
_queryCache = [[FSTLevelDBQueryCache alloc] initWithDB:self serializer:self.serializer];
341+
_queryCache = absl::make_unique<LevelDbQueryCache>(self, _serializer);
343342
_documentCache = absl::make_unique<LevelDbRemoteDocumentCache>(self, _serializer);
344343
_referenceDelegate =
345344
[[FSTLevelDBLRUDelegate alloc] initWithPersistence:self lruParams:lruParams];
346345
_transactionRunner.SetBackingPersistence(self);
347346
_users = std::move(users);
348347
// TODO(gsoltis): set up a leveldb transaction for these operations.
349-
[_queryCache start];
348+
_queryCache->Start();
350349
[_referenceDelegate start];
351350
}
352351
return self;
@@ -462,8 +461,8 @@ - (LevelDbTransaction *)currentTransaction {
462461
return [FSTLevelDBMutationQueue mutationQueueWithUser:user db:self serializer:self.serializer];
463462
}
464463

465-
- (id<FSTQueryCache>)queryCache {
466-
return _queryCache;
464+
- (LevelDbQueryCache *)queryCache {
465+
return _queryCache.get();
467466
}
468467

469468
- (RemoteDocumentCache *)remoteDocumentCache {

0 commit comments

Comments
 (0)