diff --git a/Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm b/Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm index 93f1968323d..296791b1ce9 100644 --- a/Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm +++ b/Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm @@ -26,12 +26,12 @@ #import "Firestore/Source/Local/FSTMutationQueue.h" #import "Firestore/Source/Local/FSTPersistence.h" #import "Firestore/Source/Local/FSTQueryCache.h" -#import "Firestore/Source/Local/FSTRemoteDocumentCache.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTFieldValue.h" #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Util/FSTClasses.h" #include "Firestore/core/src/firebase/firestore/auth/user.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/document_key_set.h" #include "Firestore/core/src/firebase/firestore/model/precondition.h" #include "Firestore/core/src/firebase/firestore/model/types.h" @@ -42,6 +42,7 @@ using firebase::firestore::auth::User; using firebase::firestore::local::LruParams; using firebase::firestore::local::LruResults; +using firebase::firestore::local::RemoteDocumentCache; using firebase::firestore::model::DocumentKey; using firebase::firestore::model::DocumentKeyHash; using firebase::firestore::model::DocumentKeySet; @@ -58,7 +59,7 @@ @implementation FSTLRUGarbageCollectorTests { FSTObjectValue *_bigObjectValue; id _persistence; id _queryCache; - id _documentCache; + RemoteDocumentCache *_documentCache; id _mutationQueue; id _lruDelegate; FSTLRUGarbageCollector *_gc; @@ -209,7 +210,7 @@ - (void)removeDocument:(const DocumentKey &)docKey fromTarget:(TargetId)targetId */ - (FSTDocument *)cacheADocumentInTransaction { FSTDocument *doc = [self nextTestDocument]; - [_documentCache addEntry:doc]; + _documentCache->Add(doc); return doc; } @@ -461,12 +462,11 @@ - (void)testRemoveOrphanedDocuments { XCTAssertEqual(toBeRemoved.size(), removed); _persistence.run("verify", [&]() { for (const DocumentKey &key : toBeRemoved) { - XCTAssertNil([_documentCache entryForKey:key]); + XCTAssertNil(_documentCache->Get(key)); XCTAssertFalse([_queryCache containsKey:key]); } for (const DocumentKey &key : expectedRetained) { - XCTAssertNotNil([_documentCache entryForKey:key], @"Missing document %s", - key.ToString().c_str()); + XCTAssertNotNil(_documentCache->Get(key), @"Missing document %s", key.ToString().c_str()); } }); [_persistence shutdown]; @@ -618,7 +618,7 @@ - (void)testRemoveTargetsThenGC { key:middleDocToUpdate version:testutil::Version(version) state:FSTDocumentStateSynced]; - [_documentCache addEntry:doc]; + _documentCache->Add(doc); [self updateTargetInTransaction:middleTarget]; }); @@ -643,14 +643,14 @@ - (void)testRemoveTargetsThenGC { XCTAssertEqual(expectedRemoved.size(), docsRemoved); _persistence.run("verify results", [&]() { for (const DocumentKey &key : expectedRemoved) { - XCTAssertNil([_documentCache entryForKey:key], @"Did not expect to find %s in document cache", + XCTAssertNil(_documentCache->Get(key), @"Did not expect to find %s in document cache", key.ToString().c_str()); XCTAssertFalse([_queryCache containsKey:key], @"Did not expect to find %s in queryCache", key.ToString().c_str()); [self expectSentinelRemoved:key]; } for (const DocumentKey &key : expectedRetained) { - XCTAssertNotNil([_documentCache entryForKey:key], @"Expected to find %s in document cache", + XCTAssertNotNil(_documentCache->Get(key), @"Expected to find %s in document cache", key.ToString().c_str()); } }); diff --git a/Firestore/Example/Tests/Local/FSTLevelDBRemoteDocumentCacheTests.mm b/Firestore/Example/Tests/Local/FSTLevelDBRemoteDocumentCacheTests.mm index 78e105c9dc7..ac106caad0d 100644 --- a/Firestore/Example/Tests/Local/FSTLevelDBRemoteDocumentCacheTests.mm +++ b/Firestore/Example/Tests/Local/FSTLevelDBRemoteDocumentCacheTests.mm @@ -21,6 +21,7 @@ #import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h" #import "Firestore/Source/Local/FSTLevelDB.h" #include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/util/ordered_code.h" #include "absl/memory/memory.h" @@ -30,6 +31,7 @@ using leveldb::WriteOptions; using firebase::firestore::local::LevelDbRemoteDocumentCache; +using firebase::firestore::local::RemoteDocumentCache; using firebase::firestore::util::OrderedCode; // A dummy document value, useful for testing code that's known to examine only document keys. @@ -61,7 +63,7 @@ - (void)setUp { [self writeDummyRowWithSegments:@[ @"remote_documentsa", @"foo", @"bar" ]]; } -- (LevelDbRemoteDocumentCache *_Nullable)remoteDocumentCache { +- (RemoteDocumentCache *_Nullable)remoteDocumentCache { return _cache.get(); } diff --git a/Firestore/Example/Tests/Local/FSTMemoryRemoteDocumentCacheTests.mm b/Firestore/Example/Tests/Local/FSTMemoryRemoteDocumentCacheTests.mm index c33ce4b63ee..c39a15f1fbf 100644 --- a/Firestore/Example/Tests/Local/FSTMemoryRemoteDocumentCacheTests.mm +++ b/Firestore/Example/Tests/Local/FSTMemoryRemoteDocumentCacheTests.mm @@ -18,12 +18,14 @@ #import "Firestore/Source/Local/FSTMemoryPersistence.h" #include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "absl/memory/memory.h" #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h" #import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h" using firebase::firestore::local::MemoryRemoteDocumentCache; +using firebase::firestore::local::RemoteDocumentCache; @interface FSTMemoryRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests @end @@ -45,7 +47,7 @@ - (void)setUp { _cache = absl::make_unique(); } -- (MemoryRemoteDocumentCache *)remoteDocumentCache { +- (RemoteDocumentCache *)remoteDocumentCache { return _cache.get(); } diff --git a/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h b/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h index 71e0073f79a..8b660eae0ef 100644 --- a/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h +++ b/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h @@ -14,8 +14,6 @@ * limitations under the License. */ -#import "Firestore/Source/Local/FSTRemoteDocumentCache.h" - #import #include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" diff --git a/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.mm b/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.mm index e94224cdbdb..c13d967bf89 100644 --- a/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.mm +++ b/Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.mm @@ -21,7 +21,6 @@ #import "Firestore/Source/Core/FSTQuery.h" #import "Firestore/Source/Local/FSTPersistence.h" #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentSet.h" #import "Firestore/Example/Tests/Util/FSTHelpers.h" @@ -127,7 +126,7 @@ - (void)testSetAndReadDeletedDocument { self.persistence.run("testSetAndReadDeletedDocument", [&]() { FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc(kDocPath, kVersion, NO); - self.remoteDocumentCache->AddEntry(deletedDoc); + self.remoteDocumentCache->Add(deletedDoc); XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), deletedDoc); }); @@ -139,7 +138,7 @@ - (void)testSetDocumentToNewValue { self.persistence.run("testSetDocumentToNewValue", [&]() { [self setTestDocumentAtPath:kDocPath]; FSTDocument *newDoc = FSTTestDoc(kDocPath, kVersion, @{@"data" : @2}, FSTDocumentStateSynced); - self.remoteDocumentCache->AddEntry(newDoc); + self.remoteDocumentCache->Add(newDoc); XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), newDoc); }); } @@ -149,7 +148,7 @@ - (void)testRemoveDocument { self.persistence.run("testRemoveDocument", [&]() { [self setTestDocumentAtPath:kDocPath]; - self.remoteDocumentCache->RemoveEntry(testutil::Key(kDocPath)); + self.remoteDocumentCache->Remove(testutil::Key(kDocPath)); XCTAssertNil(self.remoteDocumentCache->Get(testutil::Key(kDocPath))); }); @@ -160,7 +159,7 @@ - (void)testRemoveNonExistentDocument { self.persistence.run("testRemoveNonExistentDocument", [&]() { // no-op, but make sure it doesn't throw. - XCTAssertNoThrow(self.remoteDocumentCache->RemoveEntry(testutil::Key(kDocPath))); + XCTAssertNoThrow(self.remoteDocumentCache->Remove(testutil::Key(kDocPath))); }); } @@ -177,7 +176,7 @@ - (void)testDocumentsMatchingQuery { [self setTestDocumentAtPath:"c/1"]; FSTQuery *query = FSTTestQuery("b"); - DocumentMap results = self.remoteDocumentCache->GetMatchingDocuments(query); + DocumentMap results = self.remoteDocumentCache->GetMatching(query); [self expectMap:results.underlying_map() hasDocsInArray:@[ FSTTestDoc("b/1", kVersion, _kDocData, FSTDocumentStateSynced), @@ -188,11 +187,9 @@ - (void)testDocumentsMatchingQuery { } #pragma mark - Helpers -// TODO(gsoltis): reevaluate if any of these helpers are still needed - - (FSTDocument *)setTestDocumentAtPath:(const absl::string_view)path { FSTDocument *doc = FSTTestDoc(path, kVersion, _kDocData, FSTDocumentStateSynced); - self.remoteDocumentCache->AddEntry(doc); + self.remoteDocumentCache->Add(doc); return doc; } diff --git a/Firestore/Source/Local/FSTLevelDB.h b/Firestore/Source/Local/FSTLevelDB.h index de78f724629..2ef3f6eb47e 100644 --- a/Firestore/Source/Local/FSTLevelDB.h +++ b/Firestore/Source/Local/FSTLevelDB.h @@ -50,7 +50,7 @@ NS_ASSUME_NONNULL_BEGIN serializer:(FSTLocalSerializer *)serializer lruParams: (firebase::firestore::local::LruParams)lruParams - ptr:(FSTLevelDB **)ptr; + ptr:(FSTLevelDB *_Nullable *_Nonnull)ptr; - (instancetype)init NS_UNAVAILABLE; diff --git a/Firestore/Source/Local/FSTLevelDB.mm b/Firestore/Source/Local/FSTLevelDB.mm index cce7d313b35..04caa3830d3 100644 --- a/Firestore/Source/Local/FSTLevelDB.mm +++ b/Firestore/Source/Local/FSTLevelDB.mm @@ -24,7 +24,6 @@ #import "Firestore/Source/Local/FSTLRUGarbageCollector.h" #import "Firestore/Source/Local/FSTLevelDBMutationQueue.h" #import "Firestore/Source/Local/FSTLevelDBQueryCache.h" -#import "Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h" #import "Firestore/Source/Local/FSTReferenceSet.h" #import "Firestore/Source/Remote/FSTSerializerBeta.h" @@ -33,8 +32,10 @@ #include "Firestore/core/src/firebase/firestore/core/database_info.h" #include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" #include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h" +#include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h" #include "Firestore/core/src/firebase/firestore/local/leveldb_util.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/database_id.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/resource_path.h" @@ -60,8 +61,10 @@ using firebase::firestore::local::LevelDbDocumentTargetKey; using firebase::firestore::local::LevelDbMigrations; using firebase::firestore::local::LevelDbMutationKey; +using firebase::firestore::local::LevelDbRemoteDocumentCache; using firebase::firestore::local::LevelDbTransaction; using firebase::firestore::local::LruParams; +using firebase::firestore::local::RemoteDocumentCache; using firebase::firestore::model::DatabaseId; using firebase::firestore::model::DocumentKey; using firebase::firestore::model::ListenSequenceNumber; @@ -210,7 +213,7 @@ - (int)removeOrphanedDocumentsThroughSequenceNumber:(ListenSequenceNumber)upperB if (sequenceNumber <= upperBound) { if (![self isPinned:docKey]) { count++; - [self->_db.remoteDocumentCache removeEntryForKey:docKey]; + self->_db.remoteDocumentCache->Remove(docKey); [self removeSentinel:docKey]; } } @@ -266,6 +269,7 @@ @implementation FSTLevelDB { Path _directory; std::unique_ptr _transaction; std::unique_ptr _ptr; + std::unique_ptr _documentCache; FSTTransactionRunner _transactionRunner; FSTLevelDBLRUDelegate *_referenceDelegate; FSTLevelDBQueryCache *_queryCache; @@ -336,6 +340,7 @@ - (instancetype)initWithLevelDB:(std::unique_ptr)db _directory = std::move(directory); _serializer = serializer; _queryCache = [[FSTLevelDBQueryCache alloc] initWithDB:self serializer:self.serializer]; + _documentCache = absl::make_unique(self, _serializer); _referenceDelegate = [[FSTLevelDBLRUDelegate alloc] initWithPersistence:self lruParams:lruParams]; _transactionRunner.SetBackingPersistence(self); @@ -460,8 +465,8 @@ - (LevelDbTransaction *)currentTransaction { return _queryCache; } -- (id)remoteDocumentCache { - return [[FSTLevelDBRemoteDocumentCache alloc] initWithDB:self serializer:self.serializer]; +- (RemoteDocumentCache *)remoteDocumentCache { + return _documentCache.get(); } - (void)startTransaction:(absl::string_view)label { diff --git a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h deleted file mode 100644 index 381d308d203..00000000000 --- a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import - -#include - -#import "Firestore/Source/Local/FSTRemoteDocumentCache.h" - -@class FSTLevelDB; -@class FSTLocalSerializer; - -NS_ASSUME_NONNULL_BEGIN - -/** Cached Remote Documents backed by leveldb. */ -@interface FSTLevelDBRemoteDocumentCache : NSObject - -- (instancetype)init NS_UNAVAILABLE; - -/** - * Creates a new remote documents cache in the given leveldb. - * - * @param db The leveldb in which to create the cache. - */ -- (instancetype)initWithDB:(FSTLevelDB *)db - serializer:(FSTLocalSerializer *)serializer NS_DESIGNATED_INITIALIZER; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm deleted file mode 100644 index f4a4a3bdbf7..00000000000 --- a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import "Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h" - -#include - -#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h" -#import "Firestore/Source/Core/FSTQuery.h" -#import "Firestore/Source/Local/FSTLevelDB.h" -#import "Firestore/Source/Local/FSTLocalSerializer.h" -#import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentSet.h" - -#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" -#include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h" -#include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h" -#include "Firestore/core/src/firebase/firestore/model/document_key.h" -#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" -#include "absl/memory/memory.h" -#include "leveldb/db.h" -#include "leveldb/write_batch.h" - -NS_ASSUME_NONNULL_BEGIN - -using firebase::firestore::local::LevelDbRemoteDocumentCache; -using firebase::firestore::local::LevelDbRemoteDocumentKey; -using firebase::firestore::local::LevelDbTransaction; -using firebase::firestore::model::DocumentKey; -using firebase::firestore::model::DocumentKeySet; -using firebase::firestore::model::DocumentMap; -using firebase::firestore::model::MaybeDocumentMap; -using leveldb::DB; -using leveldb::Status; - -@implementation FSTLevelDBRemoteDocumentCache { - std::unique_ptr _cache; -} - -- (instancetype)initWithDB:(FSTLevelDB *)db serializer:(FSTLocalSerializer *)serializer { - if (self = [super init]) { - _cache = absl::make_unique(db, serializer); - } - return self; -} - -- (void)addEntry:(FSTMaybeDocument *)document { - _cache->AddEntry(document); -} - -- (void)removeEntryForKey:(const DocumentKey &)documentKey { - _cache->RemoveEntry(documentKey); -} - -- (nullable FSTMaybeDocument *)entryForKey:(const DocumentKey &)documentKey { - return _cache->Get(documentKey); -} - -- (MaybeDocumentMap)entriesForKeys:(const DocumentKeySet &)keys { - return _cache->GetAll(keys); -} - -- (DocumentMap)documentsMatchingQuery:(FSTQuery *)query { - return _cache->GetMatchingDocuments(query); -} - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Local/FSTLocalDocumentsView.h b/Firestore/Source/Local/FSTLocalDocumentsView.h index 6ec6570e548..a559b7c2136 100644 --- a/Firestore/Source/Local/FSTLocalDocumentsView.h +++ b/Firestore/Source/Local/FSTLocalDocumentsView.h @@ -16,6 +16,7 @@ #import +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/document_key_set.h" #include "Firestore/core/src/firebase/firestore/model/document_map.h" @@ -23,7 +24,6 @@ @class FSTMaybeDocument; @class FSTQuery; @protocol FSTMutationQueue; -@protocol FSTRemoteDocumentCache; NS_ASSUME_NONNULL_BEGIN @@ -34,7 +34,8 @@ NS_ASSUME_NONNULL_BEGIN */ @interface FSTLocalDocumentsView : NSObject -+ (instancetype)viewWithRemoteDocumentCache:(id)remoteDocumentCache ++ (instancetype)viewWithRemoteDocumentCache: + (firebase::firestore::local::RemoteDocumentCache *)remoteDocumentCache mutationQueue:(id)mutationQueue; - (instancetype)init __attribute__((unavailable("Use a static constructor"))); diff --git a/Firestore/Source/Local/FSTLocalDocumentsView.mm b/Firestore/Source/Local/FSTLocalDocumentsView.mm index e1820df16e5..bdc92a8dc08 100644 --- a/Firestore/Source/Local/FSTLocalDocumentsView.mm +++ b/Firestore/Source/Local/FSTLocalDocumentsView.mm @@ -18,17 +18,18 @@ #import "Firestore/Source/Core/FSTQuery.h" #import "Firestore/Source/Local/FSTMutationQueue.h" -#import "Firestore/Source/Local/FSTRemoteDocumentCache.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Model/FSTMutationBatch.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/document_map.h" #include "Firestore/core/src/firebase/firestore/model/resource_path.h" #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h" #include "Firestore/core/src/firebase/firestore/util/hard_assert.h" +using firebase::firestore::local::RemoteDocumentCache; using firebase::firestore::model::DocumentKey; using firebase::firestore::model::DocumentKeySet; using firebase::firestore::model::DocumentMap; @@ -39,22 +40,24 @@ NS_ASSUME_NONNULL_BEGIN @interface FSTLocalDocumentsView () -- (instancetype)initWithRemoteDocumentCache:(id)remoteDocumentCache +- (instancetype)initWithRemoteDocumentCache:(RemoteDocumentCache *)remoteDocumentCache mutationQueue:(id)mutationQueue NS_DESIGNATED_INITIALIZER; -@property(nonatomic, strong, readonly) id remoteDocumentCache; + @property(nonatomic, strong, readonly) id mutationQueue; @end -@implementation FSTLocalDocumentsView +@implementation FSTLocalDocumentsView { + RemoteDocumentCache *_remoteDocumentCache; +} -+ (instancetype)viewWithRemoteDocumentCache:(id)remoteDocumentCache ++ (instancetype)viewWithRemoteDocumentCache:(RemoteDocumentCache *)remoteDocumentCache mutationQueue:(id)mutationQueue { return [[FSTLocalDocumentsView alloc] initWithRemoteDocumentCache:remoteDocumentCache mutationQueue:mutationQueue]; } -- (instancetype)initWithRemoteDocumentCache:(id)remoteDocumentCache +- (instancetype)initWithRemoteDocumentCache:(RemoteDocumentCache *)remoteDocumentCache mutationQueue:(id)mutationQueue { if (self = [super init]) { _remoteDocumentCache = remoteDocumentCache; @@ -72,7 +75,7 @@ - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key { // Internal version of documentForKey: which allows reusing `batches`. - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key inBatches:(NSArray *)batches { - FSTMaybeDocument *_Nullable document = [self.remoteDocumentCache entryForKey:key]; + FSTMaybeDocument *_Nullable document = _remoteDocumentCache->Get(key); for (FSTMutationBatch *batch in batches) { document = [batch applyToLocalDocument:document documentKey:key]; } @@ -98,7 +101,7 @@ - (MaybeDocumentMap)applyLocalMutationsToDocuments:(const MaybeDocumentMap &)doc } - (MaybeDocumentMap)documentsForKeys:(const DocumentKeySet &)keys { - MaybeDocumentMap docs = [self.remoteDocumentCache entriesForKeys:keys]; + MaybeDocumentMap docs = _remoteDocumentCache->GetAll(keys); return [self localViewsForDocuments:docs]; } @@ -153,7 +156,7 @@ - (DocumentMap)documentsMatchingDocumentQuery:(const ResourcePath &)docPath { } - (DocumentMap)documentsMatchingCollectionQuery:(FSTQuery *)query { - DocumentMap results = [self.remoteDocumentCache documentsMatchingQuery:query]; + DocumentMap results = _remoteDocumentCache->GetMatching(query); // Get locally persisted mutation batches. NSArray *matchingBatches = [self.mutationQueue allMutationBatchesAffectingQuery:query]; diff --git a/Firestore/Source/Local/FSTLocalStore.mm b/Firestore/Source/Local/FSTLocalStore.mm index 7d04c080f4a..684ce15543b 100644 --- a/Firestore/Source/Local/FSTLocalStore.mm +++ b/Firestore/Source/Local/FSTLocalStore.mm @@ -31,7 +31,6 @@ #import "Firestore/Source/Local/FSTQueryCache.h" #import "Firestore/Source/Local/FSTQueryData.h" #import "Firestore/Source/Local/FSTReferenceSet.h" -#import "Firestore/Source/Local/FSTRemoteDocumentCache.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Model/FSTMutationBatch.h" @@ -40,6 +39,7 @@ #include "Firestore/core/src/firebase/firestore/auth/user.h" #include "Firestore/core/src/firebase/firestore/core/target_id_generator.h" #include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h" #include "Firestore/core/src/firebase/firestore/util/hard_assert.h" #include "Firestore/core/src/firebase/firestore/util/log.h" @@ -47,6 +47,7 @@ using firebase::firestore::auth::User; using firebase::firestore::core::TargetIdGenerator; using firebase::firestore::local::LruResults; +using firebase::firestore::local::RemoteDocumentCache; using firebase::firestore::model::BatchId; using firebase::firestore::model::DocumentKey; using firebase::firestore::model::DocumentKeySet; @@ -75,9 +76,6 @@ @interface FSTLocalStore () /** The set of all mutations that have been sent but not yet been applied to the backend. */ @property(nonatomic, strong) id mutationQueue; -/** The set of all cached remote documents. */ -@property(nonatomic, strong) id remoteDocumentCache; - /** The "local" view of all documents (layering mutationQueue on top of remoteDocumentCache). */ @property(nonatomic, strong) FSTLocalDocumentsView *localDocuments; @@ -95,6 +93,8 @@ @interface FSTLocalStore () @implementation FSTLocalStore { /** Used to generate targetIDs for queries tracked locally. */ TargetIdGenerator _targetIDGenerator; + /** The set of all cached remote documents. */ + RemoteDocumentCache *_remoteDocumentCache; } - (instancetype)initWithPersistence:(id)persistence @@ -140,9 +140,8 @@ - (MaybeDocumentMap)userDidChange:(const User &)user { NSArray *newBatches = [self.mutationQueue allMutationBatches]; // Recreate our LocalDocumentsView using the new MutationQueue. - self.localDocuments = - [FSTLocalDocumentsView viewWithRemoteDocumentCache:self.remoteDocumentCache - mutationQueue:self.mutationQueue]; + self.localDocuments = [FSTLocalDocumentsView viewWithRemoteDocumentCache:_remoteDocumentCache + mutationQueue:self.mutationQueue]; // Union the old/new changed keys. DocumentKeySet changedKeys; @@ -272,7 +271,7 @@ - (MaybeDocumentMap)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent { } // Each loop iteration only affects its "own" doc, so it's safe to get all the remote // documents in advance in a single call. - MaybeDocumentMap existingDocs = [self.remoteDocumentCache entriesForKeys:updatedKeys]; + MaybeDocumentMap existingDocs = _remoteDocumentCache->GetAll(updatedKeys); for (const auto &kv : remoteEvent.documentUpdates) { const DocumentKey &key = kv.first; @@ -289,7 +288,7 @@ - (MaybeDocumentMap)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent { if (!existingDoc || doc.version == SnapshotVersion::None() || (authoritativeUpdates.contains(doc.key) && !existingDoc.hasPendingWrites) || doc.version >= existingDoc.version) { - [self.remoteDocumentCache addEntry:doc]; + _remoteDocumentCache->Add(doc); changedDocs = changedDocs.insert(key, doc); } else { LOG_DEBUG( @@ -453,7 +452,7 @@ - (void)applyBatchResult:(FSTMutationBatchResult *)batchResult { DocumentKeySet docKeys = batch.keys; const DocumentVersionMap &versions = batchResult.docVersions; for (const DocumentKey &docKey : docKeys) { - FSTMaybeDocument *_Nullable remoteDoc = [self.remoteDocumentCache entryForKey:docKey]; + FSTMaybeDocument *_Nullable remoteDoc = _remoteDocumentCache->Get(docKey); FSTMaybeDocument *_Nullable doc = remoteDoc; auto ackVersionIter = versions.find(docKey); @@ -466,7 +465,7 @@ - (void)applyBatchResult:(FSTMutationBatchResult *)batchResult { HARD_ASSERT(!remoteDoc, "Mutation batch %s applied to document %s resulted in nil.", batch, remoteDoc); } else { - [self.remoteDocumentCache addEntry:doc]; + _remoteDocumentCache->Add(doc); } } } diff --git a/Firestore/Source/Local/FSTMemoryPersistence.mm b/Firestore/Source/Local/FSTMemoryPersistence.mm index 6aa8d6738e8..d246f2a9c60 100644 --- a/Firestore/Source/Local/FSTMemoryPersistence.mm +++ b/Firestore/Source/Local/FSTMemoryPersistence.mm @@ -24,17 +24,18 @@ #import "Firestore/Source/Core/FSTListenSequence.h" #import "Firestore/Source/Local/FSTMemoryMutationQueue.h" #import "Firestore/Source/Local/FSTMemoryQueryCache.h" -#import "Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h" #import "Firestore/Source/Local/FSTReferenceSet.h" #include "absl/memory/memory.h" #include "Firestore/core/src/firebase/firestore/auth/user.h" +#include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/util/hard_assert.h" using firebase::firestore::auth::HashUser; using firebase::firestore::auth::User; using firebase::firestore::local::LruParams; +using firebase::firestore::local::MemoryRemoteDocumentCache; using firebase::firestore::model::DocumentKey; using firebase::firestore::model::DocumentKeyHash; using firebase::firestore::model::ListenSequenceNumber; @@ -48,7 +49,7 @@ @interface FSTMemoryPersistence () - (FSTMemoryQueryCache *)queryCache; -- (FSTMemoryRemoteDocumentCache *)remoteDocumentCache; +- (MemoryRemoteDocumentCache *)remoteDocumentCache; @property(nonatomic, readonly) MutationQueues &mutationQueues; @@ -70,8 +71,8 @@ @implementation FSTMemoryPersistence { */ FSTMemoryQueryCache *_queryCache; - /** The FSTRemoteDocumentCache representing the persisted cache of remote documents. */ - FSTMemoryRemoteDocumentCache *_remoteDocumentCache; + /** The RemoteDocumentCache representing the persisted cache of remote documents. */ + MemoryRemoteDocumentCache _remoteDocumentCache; FSTTransactionRunner _transactionRunner; @@ -98,7 +99,6 @@ + (instancetype)persistenceWithLruParams:(firebase::firestore::local::LruParams) - (instancetype)init { if (self = [super init]) { _queryCache = [[FSTMemoryQueryCache alloc] initWithPersistence:self]; - _remoteDocumentCache = [[FSTMemoryRemoteDocumentCache alloc] init]; self.started = YES; } return self; @@ -143,8 +143,8 @@ - (FSTMemoryQueryCache *)queryCache { return _queryCache; } -- (id)remoteDocumentCache { - return _remoteDocumentCache; +- (MemoryRemoteDocumentCache *)remoteDocumentCache { + return &_remoteDocumentCache; } @end @@ -249,9 +249,7 @@ - (int32_t)sequenceNumberCount { - (int)removeOrphanedDocumentsThroughSequenceNumber:(ListenSequenceNumber)upperBound { std::vector removed = - [(FSTMemoryRemoteDocumentCache *)_persistence.remoteDocumentCache - removeOrphanedDocuments:self - throughSequenceNumber:upperBound]; + _persistence.remoteDocumentCache->RemoveOrphanedDocuments(self, upperBound); for (const auto &key : removed) { _sequenceNumbers.erase(key); } @@ -304,7 +302,7 @@ - (size_t)byteSize { // and count bytes) is inefficient and inexact, but won't run in production. size_t count = 0; count += [_persistence.queryCache byteSizeWithSerializer:_serializer]; - count += [_persistence.remoteDocumentCache byteSizeWithSerializer:_serializer]; + count += _persistence.remoteDocumentCache->CalculateByteSize(_serializer); const MutationQueues &queues = [_persistence mutationQueues]; for (const auto &entry : queues) { count += [entry.second byteSizeWithSerializer:_serializer]; @@ -397,7 +395,7 @@ - (BOOL)mutationQueuesContainKey:(const DocumentKey &)key { - (void)commitTransaction { for (const auto &key : *_orphaned) { if (![self isReferenced:key]) { - [[_persistence remoteDocumentCache] removeEntryForKey:key]; + _persistence.remoteDocumentCache->Remove(key); } } _orphaned.reset(); diff --git a/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h b/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h deleted file mode 100644 index 1f0884fe33a..00000000000 --- a/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import - -#include - -#import "Firestore/Source/Local/FSTRemoteDocumentCache.h" - -#include "Firestore/core/src/firebase/firestore/model/document_key.h" -#include "Firestore/core/src/firebase/firestore/model/types.h" - -NS_ASSUME_NONNULL_BEGIN - -@class FSTLocalSerializer; -@class FSTMemoryLRUReferenceDelegate; - -@interface FSTMemoryRemoteDocumentCache : NSObject - -- (std::vector) - removeOrphanedDocuments:(FSTMemoryLRUReferenceDelegate *)referenceDelegate - throughSequenceNumber:(firebase::firestore::model::ListenSequenceNumber)upperBound; - -- (size_t)byteSizeWithSerializer:(FSTLocalSerializer *)serializer; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm b/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm deleted file mode 100644 index 4f4070141aa..00000000000 --- a/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import "Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h" - -#import -#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h" -#import "Firestore/Source/Core/FSTQuery.h" -#import "Firestore/Source/Local/FSTMemoryPersistence.h" -#import "Firestore/Source/Model/FSTDocument.h" - -#include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h" -#include "Firestore/core/src/firebase/firestore/model/document_key.h" -#include "Firestore/core/src/firebase/firestore/model/document_map.h" - -using firebase::firestore::local::MemoryRemoteDocumentCache; -using firebase::firestore::model::DocumentKey; -using firebase::firestore::model::DocumentKeySet; -using firebase::firestore::model::ListenSequenceNumber; -using firebase::firestore::model::DocumentMap; -using firebase::firestore::model::MaybeDocumentMap; - -NS_ASSUME_NONNULL_BEGIN - -@implementation FSTMemoryRemoteDocumentCache { - MemoryRemoteDocumentCache _cache; -} - -- (void)addEntry:(FSTMaybeDocument *)document { - _cache.AddEntry(document); -} - -- (void)removeEntryForKey:(const DocumentKey &)key { - _cache.RemoveEntry(key); -} - -- (nullable FSTMaybeDocument *)entryForKey:(const DocumentKey &)key { - return _cache.Get(key); -} - -- (MaybeDocumentMap)entriesForKeys:(const DocumentKeySet &)keys { - return _cache.GetAll(keys); -} - -- (DocumentMap)documentsMatchingQuery:(FSTQuery *)query { - return _cache.GetMatchingDocuments(query); -} - -- (std::vector)removeOrphanedDocuments: - (FSTMemoryLRUReferenceDelegate *)referenceDelegate - throughSequenceNumber:(ListenSequenceNumber)upperBound { - return _cache.RemoveOrphanedDocuments(referenceDelegate, upperBound); -} - -- (size_t)byteSizeWithSerializer:(FSTLocalSerializer *)serializer { - return _cache.CalculateByteSize(serializer); -} - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Local/FSTPersistence.h b/Firestore/Source/Local/FSTPersistence.h index 4b106af5080..8c9b7a62136 100644 --- a/Firestore/Source/Local/FSTPersistence.h +++ b/Firestore/Source/Local/FSTPersistence.h @@ -17,6 +17,7 @@ #import #include "Firestore/core/src/firebase/firestore/auth/user.h" +#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/types.h" #include "Firestore/core/src/firebase/firestore/util/hard_assert.h" @@ -27,7 +28,6 @@ @protocol FSTMutationQueue; @protocol FSTQueryCache; @protocol FSTReferenceDelegate; -@protocol FSTRemoteDocumentCache; struct FSTTransactionRunner; @@ -82,7 +82,7 @@ NS_ASSUME_NONNULL_BEGIN - (id)queryCache; /** Creates an FSTRemoteDocumentCache representing the persisted cache of remote documents. */ -- (id)remoteDocumentCache; +- (firebase::firestore::local::RemoteDocumentCache *)remoteDocumentCache; @property(nonatomic, readonly, assign) const FSTTransactionRunner &run; diff --git a/Firestore/Source/Local/FSTRemoteDocumentCache.h b/Firestore/Source/Local/FSTRemoteDocumentCache.h deleted file mode 100644 index 51a6cf1b72e..00000000000 --- a/Firestore/Source/Local/FSTRemoteDocumentCache.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import - -#include "Firestore/core/src/firebase/firestore/model/document_key.h" -#include "Firestore/core/src/firebase/firestore/model/document_key_set.h" -#include "Firestore/core/src/firebase/firestore/model/document_map.h" - -@class FSTMaybeDocument; -@class FSTQuery; - -NS_ASSUME_NONNULL_BEGIN - -/** - * Represents cached documents received from the remote backend. - * - * The cache is keyed by DocumentKey and entries in the cache are FSTMaybeDocument instances, - * meaning we can cache both FSTDocument instances (an actual document with data) as well as - * FSTDeletedDocument instances (indicating that the document is known to not exist). - */ -@protocol FSTRemoteDocumentCache - -/** - * Adds or replaces an entry in the cache. - * - * The cache key is extracted from `maybeDocument.key`. If there is already a cache entry for - * the key, it will be replaced. - * - * @param maybeDocument A FSTDocument or FSTDeletedDocument to put in the cache. - */ -- (void)addEntry:(FSTMaybeDocument *)maybeDocument; - -/** Removes the cached entry for the given key (no-op if no entry exists). */ -- (void)removeEntryForKey:(const firebase::firestore::model::DocumentKey &)documentKey; - -/** - * Looks up an entry in the cache. - * - * @param documentKey The key of the entry to look up. - * @return The cached FSTDocument or FSTDeletedDocument entry, or nil if we have nothing cached. - */ -- (nullable FSTMaybeDocument *)entryForKey: - (const firebase::firestore::model::DocumentKey &)documentKey; - -/** - * Looks up a set of entries in the cache. - * - * @param documentKeys The keys of the entries to look up. - * @return The cached Document or NoDocument entries indexed by key. If an entry is not cached, - * the corresponding key will be mapped to a null value. - */ -- (firebase::firestore::model::MaybeDocumentMap)entriesForKeys: - (const firebase::firestore::model::DocumentKeySet &)documentKeys; - -/** - * Executes a query against the cached FSTDocument entries - * - * Implementations may return extra documents if convenient. The results should be re-filtered - * by the consumer before presenting them to the user. - * - * Cached FSTDeletedDocument entries have no bearing on query results. - * - * @param query The query to match documents against. - * @return The set of matching documents. - */ -- (firebase::firestore::model::DocumentMap)documentsMatchingQuery:(FSTQuery *)query; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h b/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h index ef8a83f429c..536d74c1d33 100644 --- a/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h +++ b/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h @@ -46,12 +46,12 @@ class LevelDbRemoteDocumentCache : public RemoteDocumentCache { public: LevelDbRemoteDocumentCache(FSTLevelDB* db, FSTLocalSerializer* serializer); - void AddEntry(FSTMaybeDocument* document) override; - void RemoveEntry(const model::DocumentKey& key) override; + void Add(FSTMaybeDocument* document) override; + void Remove(const model::DocumentKey& key) override; FSTMaybeDocument* _Nullable Get(const model::DocumentKey& key) override; model::MaybeDocumentMap GetAll(const model::DocumentKeySet& keys) override; - model::DocumentMap GetMatchingDocuments(FSTQuery* query) override; + model::DocumentMap GetMatching(FSTQuery* query) override; private: FSTMaybeDocument* DecodeMaybeDocument(absl::string_view encoded, diff --git a/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.mm b/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.mm index 341ce12fb09..1a8bbeaecc6 100644 --- a/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.mm +++ b/Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.mm @@ -43,13 +43,13 @@ : db_(db), serializer_(serializer) { } -void LevelDbRemoteDocumentCache::AddEntry(FSTMaybeDocument* document) { +void LevelDbRemoteDocumentCache::Add(FSTMaybeDocument* document) { std::string ldb_key = LevelDbRemoteDocumentKey::Key(document.key); db_.currentTransaction->Put(ldb_key, [serializer_ encodedMaybeDocument:document]); } -void LevelDbRemoteDocumentCache::RemoveEntry(const DocumentKey& key) { +void LevelDbRemoteDocumentCache::Remove(const DocumentKey& key) { std::string ldb_key = LevelDbRemoteDocumentKey::Key(key); db_.currentTransaction->Delete(ldb_key); } @@ -89,7 +89,7 @@ return results; } -DocumentMap LevelDbRemoteDocumentCache::GetMatchingDocuments(FSTQuery* query) { +DocumentMap LevelDbRemoteDocumentCache::GetMatching(FSTQuery* query) { DocumentMap results; // Documents are ordered by key, so we can use a prefix scan to narrow down diff --git a/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h b/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h index ed9175bbd93..d1eebd08e99 100644 --- a/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h +++ b/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h @@ -42,12 +42,12 @@ namespace local { class MemoryRemoteDocumentCache : public RemoteDocumentCache { public: - void AddEntry(FSTMaybeDocument *document) override; - void RemoveEntry(const model::DocumentKey &key) override; + void Add(FSTMaybeDocument *document) override; + void Remove(const model::DocumentKey &key) override; FSTMaybeDocument *_Nullable Get(const model::DocumentKey &key) override; model::MaybeDocumentMap GetAll(const model::DocumentKeySet &keys) override; - model::DocumentMap GetMatchingDocuments(FSTQuery *query) override; + model::DocumentMap GetMatching(FSTQuery *query) override; std::vector RemoveOrphanedDocuments( FSTMemoryLRUReferenceDelegate *reference_delegate, diff --git a/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.mm b/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.mm index b7535309e83..2514eaaa16d 100644 --- a/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.mm +++ b/Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.mm @@ -36,32 +36,32 @@ * document key in memory. This is only an estimate and includes the size * of the segments of the path, but not any object overhead or path separators. */ -size_t DocumentKeyByteSize(const DocumentKey &key) { +size_t DocumentKeyByteSize(const DocumentKey& key) { size_t count = 0; - for (const auto &segment : key.path()) { + for (const auto& segment : key.path()) { count += segment.size(); } return count; } } // namespace -void MemoryRemoteDocumentCache::AddEntry(FSTMaybeDocument *document) { +void MemoryRemoteDocumentCache::Add(FSTMaybeDocument* document) { docs_ = docs_.insert(document.key, document); } -void MemoryRemoteDocumentCache::RemoveEntry(const DocumentKey &key) { +void MemoryRemoteDocumentCache::Remove(const DocumentKey& key) { docs_ = docs_.erase(key); } -FSTMaybeDocument *_Nullable MemoryRemoteDocumentCache::Get( - const DocumentKey &key) { +FSTMaybeDocument* _Nullable MemoryRemoteDocumentCache::Get( + const DocumentKey& key) { auto found = docs_.find(key); return found != docs_.end() ? found->second : nil; } -MaybeDocumentMap MemoryRemoteDocumentCache::GetAll(const DocumentKeySet &keys) { +MaybeDocumentMap MemoryRemoteDocumentCache::GetAll(const DocumentKeySet& keys) { MaybeDocumentMap results; - for (const DocumentKey &key : keys) { + for (const DocumentKey& key : keys) { // Make sure each key has a corresponding entry, which is null in case the // document is not found. // TODO(http://b/32275378): Don't conflate missing / deleted. @@ -70,22 +70,22 @@ size_t DocumentKeyByteSize(const DocumentKey &key) { return results; } -DocumentMap MemoryRemoteDocumentCache::GetMatchingDocuments(FSTQuery *query) { +DocumentMap MemoryRemoteDocumentCache::GetMatching(FSTQuery* query) { DocumentMap results; // Documents are ordered by key, so we can use a prefix scan to narrow down // the documents we need to match the query against. DocumentKey prefix{query.path.Append("")}; for (auto it = docs_.lower_bound(prefix); it != docs_.end(); ++it) { - const DocumentKey &key = it->first; + const DocumentKey& key = it->first; if (!query.path.IsPrefixOf(key.path())) { break; } - FSTMaybeDocument *maybeDoc = it->second; + FSTMaybeDocument* maybeDoc = it->second; if (![maybeDoc isKindOfClass:[FSTDocument class]]) { continue; } - FSTDocument *doc = static_cast(maybeDoc); + FSTDocument* doc = static_cast(maybeDoc); if ([query matchesDocument:doc]) { results = results.insert(key, doc); } @@ -94,12 +94,12 @@ size_t DocumentKeyByteSize(const DocumentKey &key) { } std::vector MemoryRemoteDocumentCache::RemoveOrphanedDocuments( - FSTMemoryLRUReferenceDelegate *reference_delegate, + FSTMemoryLRUReferenceDelegate* reference_delegate, ListenSequenceNumber upper_bound) { std::vector removed; MaybeDocumentMap updated_docs = docs_; - for (const auto &kv : docs_) { - const DocumentKey &key = kv.first; + for (const auto& kv : docs_) { + const DocumentKey& key = kv.first; if (![reference_delegate isPinnedAtSequenceNumber:upper_bound document:key]) { updated_docs = updated_docs.erase(key); @@ -111,9 +111,9 @@ size_t DocumentKeyByteSize(const DocumentKey &key) { } size_t MemoryRemoteDocumentCache::CalculateByteSize( - FSTLocalSerializer *serializer) { + FSTLocalSerializer* serializer) { size_t count = 0; - for (const auto &kv : docs_) { + for (const auto& kv : docs_) { count += DocumentKeyByteSize(kv.first); count += [[serializer encodedMaybeDocument:kv.second] serializedSize]; } diff --git a/Firestore/core/src/firebase/firestore/local/remote_document_cache.h b/Firestore/core/src/firebase/firestore/local/remote_document_cache.h index b9721281a35..f1ff54c354f 100644 --- a/Firestore/core/src/firebase/firestore/local/remote_document_cache.h +++ b/Firestore/core/src/firebase/firestore/local/remote_document_cache.h @@ -58,15 +58,15 @@ class RemoteDocumentCache { * * @param document A FSTDocument or FSTDeletedDocument to put in the cache. */ - virtual void AddEntry(FSTMaybeDocument* document) = 0; + virtual void Add(FSTMaybeDocument* document) = 0; /** Removes the cached entry for the given key (no-op if no entry exists). */ - virtual void RemoveEntry(const model::DocumentKey& key) = 0; + virtual void Remove(const model::DocumentKey& key) = 0; /** * Looks up an entry in the cache. * - * @param documentKey The key of the entry to look up. + * @param key The key of the entry to look up. * @return The cached FSTDocument or FSTDeletedDocument entry, or nil if we * have nothing cached. */ @@ -92,7 +92,7 @@ class RemoteDocumentCache { * @param query The query to match documents against. * @return The set of matching documents. */ - virtual model::DocumentMap GetMatchingDocuments(FSTQuery* query) = 0; + virtual model::DocumentMap GetMatching(FSTQuery* query) = 0; }; } // namespace local