Skip to content

Commit ab05b96

Browse files
author
Greg Soltis
authored
Port FSTRemoteDocumentCache (#2196)
* Remove FSTRemoteDocumentCache
1 parent dd9c760 commit ab05b96

22 files changed

+98
-417
lines changed

Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
#import "Firestore/Source/Local/FSTMutationQueue.h"
2727
#import "Firestore/Source/Local/FSTPersistence.h"
2828
#import "Firestore/Source/Local/FSTQueryCache.h"
29-
#import "Firestore/Source/Local/FSTRemoteDocumentCache.h"
3029
#import "Firestore/Source/Model/FSTDocument.h"
3130
#import "Firestore/Source/Model/FSTFieldValue.h"
3231
#import "Firestore/Source/Model/FSTMutation.h"
3332
#import "Firestore/Source/Util/FSTClasses.h"
3433
#include "Firestore/core/src/firebase/firestore/auth/user.h"
34+
#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"
3737
#include "Firestore/core/src/firebase/firestore/model/types.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::RemoteDocumentCache;
4546
using firebase::firestore::model::DocumentKey;
4647
using firebase::firestore::model::DocumentKeyHash;
4748
using firebase::firestore::model::DocumentKeySet;
@@ -58,7 +59,7 @@ @implementation FSTLRUGarbageCollectorTests {
5859
FSTObjectValue *_bigObjectValue;
5960
id<FSTPersistence> _persistence;
6061
id<FSTQueryCache> _queryCache;
61-
id<FSTRemoteDocumentCache> _documentCache;
62+
RemoteDocumentCache *_documentCache;
6263
id<FSTMutationQueue> _mutationQueue;
6364
id<FSTLRUDelegate> _lruDelegate;
6465
FSTLRUGarbageCollector *_gc;
@@ -209,7 +210,7 @@ - (void)removeDocument:(const DocumentKey &)docKey fromTarget:(TargetId)targetId
209210
*/
210211
- (FSTDocument *)cacheADocumentInTransaction {
211212
FSTDocument *doc = [self nextTestDocument];
212-
[_documentCache addEntry:doc];
213+
_documentCache->Add(doc);
213214
return doc;
214215
}
215216

@@ -461,12 +462,11 @@ - (void)testRemoveOrphanedDocuments {
461462
XCTAssertEqual(toBeRemoved.size(), removed);
462463
_persistence.run("verify", [&]() {
463464
for (const DocumentKey &key : toBeRemoved) {
464-
XCTAssertNil([_documentCache entryForKey:key]);
465+
XCTAssertNil(_documentCache->Get(key));
465466
XCTAssertFalse([_queryCache containsKey:key]);
466467
}
467468
for (const DocumentKey &key : expectedRetained) {
468-
XCTAssertNotNil([_documentCache entryForKey:key], @"Missing document %s",
469-
key.ToString().c_str());
469+
XCTAssertNotNil(_documentCache->Get(key), @"Missing document %s", key.ToString().c_str());
470470
}
471471
});
472472
[_persistence shutdown];
@@ -618,7 +618,7 @@ - (void)testRemoveTargetsThenGC {
618618
key:middleDocToUpdate
619619
version:testutil::Version(version)
620620
state:FSTDocumentStateSynced];
621-
[_documentCache addEntry:doc];
621+
_documentCache->Add(doc);
622622
[self updateTargetInTransaction:middleTarget];
623623
});
624624

@@ -643,14 +643,14 @@ - (void)testRemoveTargetsThenGC {
643643
XCTAssertEqual(expectedRemoved.size(), docsRemoved);
644644
_persistence.run("verify results", [&]() {
645645
for (const DocumentKey &key : expectedRemoved) {
646-
XCTAssertNil([_documentCache entryForKey:key], @"Did not expect to find %s in document cache",
646+
XCTAssertNil(_documentCache->Get(key), @"Did not expect to find %s in document cache",
647647
key.ToString().c_str());
648648
XCTAssertFalse([_queryCache containsKey:key], @"Did not expect to find %s in queryCache",
649649
key.ToString().c_str());
650650
[self expectSentinelRemoved:key];
651651
}
652652
for (const DocumentKey &key : expectedRetained) {
653-
XCTAssertNotNil([_documentCache entryForKey:key], @"Expected to find %s in document cache",
653+
XCTAssertNotNil(_documentCache->Get(key), @"Expected to find %s in document cache",
654654
key.ToString().c_str());
655655
}
656656
});

Firestore/Example/Tests/Local/FSTLevelDBRemoteDocumentCacheTests.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
2222
#import "Firestore/Source/Local/FSTLevelDB.h"
2323
#include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h"
24+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
2425

2526
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
2627
#include "absl/memory/memory.h"
@@ -30,6 +31,7 @@
3031

3132
using leveldb::WriteOptions;
3233
using firebase::firestore::local::LevelDbRemoteDocumentCache;
34+
using firebase::firestore::local::RemoteDocumentCache;
3335
using firebase::firestore::util::OrderedCode;
3436

3537
// A dummy document value, useful for testing code that's known to examine only document keys.
@@ -61,7 +63,7 @@ - (void)setUp {
6163
[self writeDummyRowWithSegments:@[ @"remote_documentsa", @"foo", @"bar" ]];
6264
}
6365

64-
- (LevelDbRemoteDocumentCache *_Nullable)remoteDocumentCache {
66+
- (RemoteDocumentCache *_Nullable)remoteDocumentCache {
6567
return _cache.get();
6668
}
6769

Firestore/Example/Tests/Local/FSTMemoryRemoteDocumentCacheTests.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
#import "Firestore/Source/Local/FSTMemoryPersistence.h"
2020
#include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h"
21+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
2122
#include "absl/memory/memory.h"
2223

2324
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
2425
#import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
2526

2627
using firebase::firestore::local::MemoryRemoteDocumentCache;
28+
using firebase::firestore::local::RemoteDocumentCache;
2729

2830
@interface FSTMemoryRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
2931
@end
@@ -45,7 +47,7 @@ - (void)setUp {
4547
_cache = absl::make_unique<MemoryRemoteDocumentCache>();
4648
}
4749

48-
- (MemoryRemoteDocumentCache *)remoteDocumentCache {
50+
- (RemoteDocumentCache *)remoteDocumentCache {
4951
return _cache.get();
5052
}
5153

Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h

Lines changed: 0 additions & 2 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/FSTRemoteDocumentCache.h"
18-
1917
#import <XCTest/XCTest.h>
2018

2119
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"

Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.mm

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#import "Firestore/Source/Core/FSTQuery.h"
2222
#import "Firestore/Source/Local/FSTPersistence.h"
2323
#import "Firestore/Source/Model/FSTDocument.h"
24-
#import "Firestore/Source/Model/FSTDocumentSet.h"
2524

2625
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2726

@@ -127,7 +126,7 @@ - (void)testSetAndReadDeletedDocument {
127126

128127
self.persistence.run("testSetAndReadDeletedDocument", [&]() {
129128
FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc(kDocPath, kVersion, NO);
130-
self.remoteDocumentCache->AddEntry(deletedDoc);
129+
self.remoteDocumentCache->Add(deletedDoc);
131130

132131
XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), deletedDoc);
133132
});
@@ -139,7 +138,7 @@ - (void)testSetDocumentToNewValue {
139138
self.persistence.run("testSetDocumentToNewValue", [&]() {
140139
[self setTestDocumentAtPath:kDocPath];
141140
FSTDocument *newDoc = FSTTestDoc(kDocPath, kVersion, @{@"data" : @2}, FSTDocumentStateSynced);
142-
self.remoteDocumentCache->AddEntry(newDoc);
141+
self.remoteDocumentCache->Add(newDoc);
143142
XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), newDoc);
144143
});
145144
}
@@ -149,7 +148,7 @@ - (void)testRemoveDocument {
149148

150149
self.persistence.run("testRemoveDocument", [&]() {
151150
[self setTestDocumentAtPath:kDocPath];
152-
self.remoteDocumentCache->RemoveEntry(testutil::Key(kDocPath));
151+
self.remoteDocumentCache->Remove(testutil::Key(kDocPath));
153152

154153
XCTAssertNil(self.remoteDocumentCache->Get(testutil::Key(kDocPath)));
155154
});
@@ -160,7 +159,7 @@ - (void)testRemoveNonExistentDocument {
160159

161160
self.persistence.run("testRemoveNonExistentDocument", [&]() {
162161
// no-op, but make sure it doesn't throw.
163-
XCTAssertNoThrow(self.remoteDocumentCache->RemoveEntry(testutil::Key(kDocPath)));
162+
XCTAssertNoThrow(self.remoteDocumentCache->Remove(testutil::Key(kDocPath)));
164163
});
165164
}
166165

@@ -177,7 +176,7 @@ - (void)testDocumentsMatchingQuery {
177176
[self setTestDocumentAtPath:"c/1"];
178177

179178
FSTQuery *query = FSTTestQuery("b");
180-
DocumentMap results = self.remoteDocumentCache->GetMatchingDocuments(query);
179+
DocumentMap results = self.remoteDocumentCache->GetMatching(query);
181180
[self expectMap:results.underlying_map()
182181
hasDocsInArray:@[
183182
FSTTestDoc("b/1", kVersion, _kDocData, FSTDocumentStateSynced),
@@ -188,11 +187,9 @@ - (void)testDocumentsMatchingQuery {
188187
}
189188

190189
#pragma mark - Helpers
191-
// TODO(gsoltis): reevaluate if any of these helpers are still needed
192-
193190
- (FSTDocument *)setTestDocumentAtPath:(const absl::string_view)path {
194191
FSTDocument *doc = FSTTestDoc(path, kVersion, _kDocData, FSTDocumentStateSynced);
195-
self.remoteDocumentCache->AddEntry(doc);
192+
self.remoteDocumentCache->Add(doc);
196193
return doc;
197194
}
198195

Firestore/Source/Local/FSTLevelDB.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ NS_ASSUME_NONNULL_BEGIN
5050
serializer:(FSTLocalSerializer *)serializer
5151
lruParams:
5252
(firebase::firestore::local::LruParams)lruParams
53-
ptr:(FSTLevelDB **)ptr;
53+
ptr:(FSTLevelDB *_Nullable *_Nonnull)ptr;
5454

5555
- (instancetype)init NS_UNAVAILABLE;
5656

Firestore/Source/Local/FSTLevelDB.mm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
2525
#import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
2626
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
27-
#import "Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h"
2827
#import "Firestore/Source/Local/FSTReferenceSet.h"
2928
#import "Firestore/Source/Remote/FSTSerializerBeta.h"
3029

@@ -33,8 +32,10 @@
3332
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
3433
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
3534
#include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h"
35+
#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"
38+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
3839
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
3940
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
4041
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
@@ -60,8 +61,10 @@
6061
using firebase::firestore::local::LevelDbDocumentTargetKey;
6162
using firebase::firestore::local::LevelDbMigrations;
6263
using firebase::firestore::local::LevelDbMutationKey;
64+
using firebase::firestore::local::LevelDbRemoteDocumentCache;
6365
using firebase::firestore::local::LevelDbTransaction;
6466
using firebase::firestore::local::LruParams;
67+
using firebase::firestore::local::RemoteDocumentCache;
6568
using firebase::firestore::model::DatabaseId;
6669
using firebase::firestore::model::DocumentKey;
6770
using firebase::firestore::model::ListenSequenceNumber;
@@ -210,7 +213,7 @@ - (int)removeOrphanedDocumentsThroughSequenceNumber:(ListenSequenceNumber)upperB
210213
if (sequenceNumber <= upperBound) {
211214
if (![self isPinned:docKey]) {
212215
count++;
213-
[self->_db.remoteDocumentCache removeEntryForKey:docKey];
216+
self->_db.remoteDocumentCache->Remove(docKey);
214217
[self removeSentinel:docKey];
215218
}
216219
}
@@ -266,6 +269,7 @@ @implementation FSTLevelDB {
266269
Path _directory;
267270
std::unique_ptr<LevelDbTransaction> _transaction;
268271
std::unique_ptr<leveldb::DB> _ptr;
272+
std::unique_ptr<LevelDbRemoteDocumentCache> _documentCache;
269273
FSTTransactionRunner _transactionRunner;
270274
FSTLevelDBLRUDelegate *_referenceDelegate;
271275
FSTLevelDBQueryCache *_queryCache;
@@ -336,6 +340,7 @@ - (instancetype)initWithLevelDB:(std::unique_ptr<leveldb::DB>)db
336340
_directory = std::move(directory);
337341
_serializer = serializer;
338342
_queryCache = [[FSTLevelDBQueryCache alloc] initWithDB:self serializer:self.serializer];
343+
_documentCache = absl::make_unique<LevelDbRemoteDocumentCache>(self, _serializer);
339344
_referenceDelegate =
340345
[[FSTLevelDBLRUDelegate alloc] initWithPersistence:self lruParams:lruParams];
341346
_transactionRunner.SetBackingPersistence(self);
@@ -460,8 +465,8 @@ - (LevelDbTransaction *)currentTransaction {
460465
return _queryCache;
461466
}
462467

463-
- (id<FSTRemoteDocumentCache>)remoteDocumentCache {
464-
return [[FSTLevelDBRemoteDocumentCache alloc] initWithDB:self serializer:self.serializer];
468+
- (RemoteDocumentCache *)remoteDocumentCache {
469+
return _documentCache.get();
465470
}
466471

467472
- (void)startTransaction:(absl::string_view)label {

Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)