FSTLevelDB.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Local/FSTLevelDB.h"
  17. #include <memory>
  18. #include <utility>
  19. #import "FIRFirestoreErrors.h"
  20. #import "Firestore/Source/Core/FSTListenSequence.h"
  21. #import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
  22. #import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
  23. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  24. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  25. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  26. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  27. #include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
  28. #include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h"
  29. #include "Firestore/core/src/firebase/firestore/local/leveldb_query_cache.h"
  30. #include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h"
  31. #include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h"
  32. #include "Firestore/core/src/firebase/firestore/local/leveldb_util.h"
  33. #include "Firestore/core/src/firebase/firestore/local/reference_set.h"
  34. #include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
  35. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  36. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  37. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  38. #include "Firestore/core/src/firebase/firestore/util/filesystem.h"
  39. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  40. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  41. #include "Firestore/core/src/firebase/firestore/util/statusor.h"
  42. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  43. #include "Firestore/core/src/firebase/firestore/util/string_util.h"
  44. #include "absl/memory/memory.h"
  45. #include "absl/strings/match.h"
  46. #include "absl/strings/str_cat.h"
  47. #include "leveldb/db.h"
  48. NS_ASSUME_NONNULL_BEGIN
  49. namespace util = firebase::firestore::util;
  50. using firebase::firestore::FirestoreErrorCode;
  51. using firebase::firestore::auth::User;
  52. using firebase::firestore::core::DatabaseInfo;
  53. using firebase::firestore::local::ConvertStatus;
  54. using firebase::firestore::local::LevelDbDocumentMutationKey;
  55. using firebase::firestore::local::LevelDbDocumentTargetKey;
  56. using firebase::firestore::local::LevelDbMigrations;
  57. using firebase::firestore::local::LevelDbMutationKey;
  58. using firebase::firestore::local::LevelDbQueryCache;
  59. using firebase::firestore::local::LevelDbRemoteDocumentCache;
  60. using firebase::firestore::local::LevelDbTransaction;
  61. using firebase::firestore::local::LruParams;
  62. using firebase::firestore::local::ReferenceSet;
  63. using firebase::firestore::local::RemoteDocumentCache;
  64. using firebase::firestore::model::DatabaseId;
  65. using firebase::firestore::model::DocumentKey;
  66. using firebase::firestore::model::ListenSequenceNumber;
  67. using firebase::firestore::model::ResourcePath;
  68. using firebase::firestore::util::OrderedCode;
  69. using firebase::firestore::util::Path;
  70. using firebase::firestore::util::Status;
  71. using firebase::firestore::util::StatusOr;
  72. using firebase::firestore::util::StringFormat;
  73. using leveldb::DB;
  74. using leveldb::Options;
  75. using leveldb::ReadOptions;
  76. using leveldb::WriteOptions;
  77. static const char *kReservedPathComponent = "firestore";
  78. @interface FSTLevelDB ()
  79. - (size_t)byteSize;
  80. @property(nonatomic, assign, getter=isStarted) BOOL started;
  81. - (firebase::firestore::local::LevelDbQueryCache *)queryCache;
  82. @end
  83. /**
  84. * Provides LRU functionality for leveldb persistence.
  85. *
  86. * Although this could implement FSTTransactional, it doesn't because it is not directly tied to
  87. * a transaction runner, it just happens to be called from FSTLevelDB, which is FSTTransactional.
  88. */
  89. @interface FSTLevelDBLRUDelegate ()
  90. - (void)transactionWillStart;
  91. - (void)transactionWillCommit;
  92. - (void)start;
  93. @end
  94. @implementation FSTLevelDBLRUDelegate {
  95. FSTLRUGarbageCollector *_gc;
  96. // This delegate should have the same lifetime as the persistence layer, but mark as
  97. // weak to avoid retain cycle.
  98. __weak FSTLevelDB *_db;
  99. ReferenceSet *_additionalReferences;
  100. ListenSequenceNumber _currentSequenceNumber;
  101. FSTListenSequence *_listenSequence;
  102. }
  103. - (instancetype)initWithPersistence:(FSTLevelDB *)persistence lruParams:(LruParams)lruParams {
  104. if (self = [super init]) {
  105. _gc = [[FSTLRUGarbageCollector alloc] initWithDelegate:self params:lruParams];
  106. _db = persistence;
  107. _currentSequenceNumber = kFSTListenSequenceNumberInvalid;
  108. }
  109. return self;
  110. }
  111. - (void)start {
  112. ListenSequenceNumber highestSequenceNumber = _db.queryCache->highest_listen_sequence_number();
  113. _listenSequence = [[FSTListenSequence alloc] initStartingAfter:highestSequenceNumber];
  114. }
  115. - (void)transactionWillStart {
  116. HARD_ASSERT(_currentSequenceNumber == kFSTListenSequenceNumberInvalid,
  117. "Previous sequence number is still in effect");
  118. _currentSequenceNumber = [_listenSequence next];
  119. }
  120. - (void)transactionWillCommit {
  121. _currentSequenceNumber = kFSTListenSequenceNumberInvalid;
  122. }
  123. - (ListenSequenceNumber)currentSequenceNumber {
  124. HARD_ASSERT(_currentSequenceNumber != kFSTListenSequenceNumberInvalid,
  125. "Asking for a sequence number outside of a transaction");
  126. return _currentSequenceNumber;
  127. }
  128. - (void)addInMemoryPins:(ReferenceSet *)set {
  129. // We should be able to assert that _additionalReferences is nil, but due to restarts in spec
  130. // tests it would fail.
  131. _additionalReferences = set;
  132. }
  133. - (void)removeTarget:(FSTQueryData *)queryData {
  134. FSTQueryData *updated =
  135. [queryData queryDataByReplacingSnapshotVersion:queryData.snapshotVersion
  136. resumeToken:queryData.resumeToken
  137. sequenceNumber:[self currentSequenceNumber]];
  138. _db.queryCache->UpdateTarget(updated);
  139. }
  140. - (void)addReference:(const DocumentKey &)key {
  141. [self writeSentinelForKey:key];
  142. }
  143. - (void)removeReference:(const DocumentKey &)key {
  144. [self writeSentinelForKey:key];
  145. }
  146. - (BOOL)mutationQueuesContainKey:(const DocumentKey &)docKey {
  147. const std::set<std::string> &users = _db.users;
  148. const ResourcePath &path = docKey.path();
  149. std::string buffer;
  150. auto it = _db.currentTransaction->NewIterator();
  151. // For each user, if there is any batch that contains this document in any batch, we know it's
  152. // pinned.
  153. for (const std::string &user : users) {
  154. std::string mutationKey = LevelDbDocumentMutationKey::KeyPrefix(user, path);
  155. it->Seek(mutationKey);
  156. if (it->Valid() && absl::StartsWith(it->key(), mutationKey)) {
  157. return YES;
  158. }
  159. }
  160. return NO;
  161. }
  162. - (BOOL)isPinned:(const DocumentKey &)docKey {
  163. if (_additionalReferences->ContainsKey(docKey)) {
  164. return YES;
  165. }
  166. if ([self mutationQueuesContainKey:docKey]) {
  167. return YES;
  168. }
  169. return NO;
  170. }
  171. - (void)enumerateTargetsUsingBlock:(void (^)(FSTQueryData *queryData, BOOL *stop))block {
  172. _db.queryCache->EnumerateTargets(block);
  173. }
  174. - (void)enumerateMutationsUsingBlock:
  175. (void (^)(const DocumentKey &key, ListenSequenceNumber sequenceNumber, BOOL *stop))block {
  176. _db.queryCache->EnumerateOrphanedDocuments(block);
  177. }
  178. - (int)removeOrphanedDocumentsThroughSequenceNumber:(ListenSequenceNumber)upperBound {
  179. __block int count = 0;
  180. _db.queryCache->EnumerateOrphanedDocuments(
  181. ^(const DocumentKey &docKey, ListenSequenceNumber sequenceNumber, BOOL *stop) {
  182. if (sequenceNumber <= upperBound) {
  183. if (![self isPinned:docKey]) {
  184. count++;
  185. self->_db.remoteDocumentCache->Remove(docKey);
  186. [self removeSentinel:docKey];
  187. }
  188. }
  189. });
  190. return count;
  191. }
  192. - (void)removeSentinel:(const DocumentKey &)key {
  193. _db.currentTransaction->Delete(LevelDbDocumentTargetKey::SentinelKey(key));
  194. }
  195. - (int)removeTargetsThroughSequenceNumber:(ListenSequenceNumber)sequenceNumber
  196. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries {
  197. return _db.queryCache->RemoveTargets(sequenceNumber, liveQueries);
  198. }
  199. - (size_t)sequenceNumberCount {
  200. __block size_t totalCount = _db.queryCache->size();
  201. [self enumerateMutationsUsingBlock:^(const DocumentKey &key, ListenSequenceNumber sequenceNumber,
  202. BOOL *stop) {
  203. totalCount++;
  204. }];
  205. return totalCount;
  206. }
  207. - (FSTLRUGarbageCollector *)gc {
  208. return _gc;
  209. }
  210. - (void)writeSentinelForKey:(const DocumentKey &)key {
  211. std::string sentinelKey = LevelDbDocumentTargetKey::SentinelKey(key);
  212. std::string encodedSequenceNumber =
  213. LevelDbDocumentTargetKey::EncodeSentinelValue([self currentSequenceNumber]);
  214. _db.currentTransaction->Put(sentinelKey, encodedSequenceNumber);
  215. }
  216. - (void)removeMutationReference:(const DocumentKey &)key {
  217. [self writeSentinelForKey:key];
  218. }
  219. - (void)limboDocumentUpdated:(const DocumentKey &)key {
  220. [self writeSentinelForKey:key];
  221. }
  222. - (size_t)byteSize {
  223. return [_db byteSize];
  224. }
  225. @end
  226. @implementation FSTLevelDB {
  227. Path _directory;
  228. std::unique_ptr<LevelDbTransaction> _transaction;
  229. std::unique_ptr<leveldb::DB> _ptr;
  230. std::unique_ptr<LevelDbRemoteDocumentCache> _documentCache;
  231. FSTTransactionRunner _transactionRunner;
  232. FSTLevelDBLRUDelegate *_referenceDelegate;
  233. std::unique_ptr<LevelDbQueryCache> _queryCache;
  234. std::set<std::string> _users;
  235. }
  236. /**
  237. * For now this is paranoid, but perhaps disable that in production builds.
  238. */
  239. + (const ReadOptions)standardReadOptions {
  240. ReadOptions options;
  241. options.verify_checksums = true;
  242. return options;
  243. }
  244. + (std::set<std::string>)collectUserSet:(LevelDbTransaction *)transaction {
  245. std::set<std::string> users;
  246. std::string tablePrefix = LevelDbMutationKey::KeyPrefix();
  247. auto it = transaction->NewIterator();
  248. it->Seek(tablePrefix);
  249. LevelDbMutationKey rowKey;
  250. while (it->Valid() && absl::StartsWith(it->key(), tablePrefix) && rowKey.Decode(it->key())) {
  251. users.insert(rowKey.user_id());
  252. auto userEnd = LevelDbMutationKey::KeyPrefix(rowKey.user_id());
  253. userEnd = util::PrefixSuccessor(userEnd);
  254. it->Seek(userEnd);
  255. }
  256. return users;
  257. }
  258. + (firebase::firestore::util::Status)dbWithDirectory:(firebase::firestore::util::Path)directory
  259. serializer:(FSTLocalSerializer *)serializer
  260. lruParams:
  261. (firebase::firestore::local::LruParams)lruParams
  262. ptr:(FSTLevelDB **)ptr {
  263. Status status = [self ensureDirectory:directory];
  264. if (!status.ok()) return status;
  265. StatusOr<std::unique_ptr<DB>> database = [self createDBWithDirectory:directory];
  266. if (!database.status().ok()) {
  267. return database.status();
  268. }
  269. std::unique_ptr<DB> ldb = std::move(database.ValueOrDie());
  270. LevelDbMigrations::RunMigrations(ldb.get());
  271. LevelDbTransaction transaction(ldb.get(), "Start LevelDB");
  272. std::set<std::string> users = [self collectUserSet:&transaction];
  273. transaction.Commit();
  274. FSTLevelDB *db = [[self alloc] initWithLevelDB:std::move(ldb)
  275. users:users
  276. directory:directory
  277. serializer:serializer
  278. lruParams:lruParams];
  279. *ptr = db;
  280. return Status::OK();
  281. }
  282. - (instancetype)initWithLevelDB:(std::unique_ptr<leveldb::DB>)db
  283. users:(std::set<std::string>)users
  284. directory:(firebase::firestore::util::Path)directory
  285. serializer:(FSTLocalSerializer *)serializer
  286. lruParams:(firebase::firestore::local::LruParams)lruParams {
  287. if (self = [super init]) {
  288. self.started = YES;
  289. _ptr = std::move(db);
  290. _directory = std::move(directory);
  291. _serializer = serializer;
  292. _queryCache = absl::make_unique<LevelDbQueryCache>(self, _serializer);
  293. _documentCache = absl::make_unique<LevelDbRemoteDocumentCache>(self, _serializer);
  294. _referenceDelegate = [[FSTLevelDBLRUDelegate alloc] initWithPersistence:self
  295. lruParams:lruParams];
  296. _transactionRunner.SetBackingPersistence(self);
  297. _users = std::move(users);
  298. // TODO(gsoltis): set up a leveldb transaction for these operations.
  299. _queryCache->Start();
  300. [_referenceDelegate start];
  301. }
  302. return self;
  303. }
  304. - (size_t)byteSize {
  305. int64_t count = 0;
  306. auto iter = util::DirectoryIterator::Create(_directory);
  307. for (; iter->Valid(); iter->Next()) {
  308. int64_t fileSize = util::FileSize(iter->file()).ValueOrDie();
  309. count += fileSize;
  310. }
  311. HARD_ASSERT(iter->status().ok(), "Failed to iterate leveldb directory: %s",
  312. iter->status().error_message().c_str());
  313. HARD_ASSERT(count <= SIZE_MAX, "Overflowed counting bytes cached");
  314. return count;
  315. }
  316. - (const std::set<std::string> &)users {
  317. return _users;
  318. }
  319. - (leveldb::DB *)ptr {
  320. return _ptr.get();
  321. }
  322. - (const FSTTransactionRunner &)run {
  323. return _transactionRunner;
  324. }
  325. + (Path)documentsDirectory {
  326. #if TARGET_OS_IPHONE
  327. NSArray<NSString *> *directories =
  328. NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  329. return Path::FromNSString(directories[0]).AppendUtf8(kReservedPathComponent);
  330. #elif TARGET_OS_OSX
  331. std::string dotPrefixed = absl::StrCat(".", kReservedPathComponent);
  332. return Path::FromNSString(NSHomeDirectory()).AppendUtf8(dotPrefixed);
  333. #else
  334. #error "local storage on tvOS"
  335. // TODO(mcg): Writing to NSDocumentsDirectory on tvOS will fail; we need to write to Caches
  336. // https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/
  337. #endif
  338. }
  339. + (Path)storageDirectoryForDatabaseInfo:(const DatabaseInfo &)databaseInfo
  340. documentsDirectory:(const Path &)documentsDirectory {
  341. // Use two different path formats:
  342. //
  343. // * persistenceKey / projectID . databaseID / name
  344. // * persistenceKey / projectID / name
  345. //
  346. // projectIDs are DNS-compatible names and cannot contain dots so there's
  347. // no danger of collisions.
  348. std::string project_key = databaseInfo.database_id().project_id();
  349. if (!databaseInfo.database_id().IsDefaultDatabase()) {
  350. absl::StrAppend(&project_key, ".", databaseInfo.database_id().database_id());
  351. }
  352. // Reserve one additional path component to allow multiple physical databases
  353. return Path::JoinUtf8(documentsDirectory, databaseInfo.persistence_key(), project_key, "main");
  354. }
  355. #pragma mark - Startup
  356. /** Creates the directory at @a directory and marks it as excluded from iCloud backup. */
  357. + (Status)ensureDirectory:(const Path &)directory {
  358. Status status = util::RecursivelyCreateDir(directory);
  359. if (!status.ok()) {
  360. return Status{FirestoreErrorCode::Internal, "Failed to create persistence directory"}.CausedBy(
  361. status);
  362. }
  363. NSURL *dirURL = [NSURL fileURLWithPath:directory.ToNSString()];
  364. NSError *localError = nil;
  365. if (![dirURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&localError]) {
  366. return Status{FirestoreErrorCode::Internal,
  367. "Failed to mark persistence directory as excluded from backups"}
  368. .CausedBy(Status::FromNSError(localError));
  369. }
  370. return Status::OK();
  371. }
  372. /** Opens the database within the given directory. */
  373. + (StatusOr<std::unique_ptr<DB>>)createDBWithDirectory:(const Path &)directory {
  374. Options options;
  375. options.create_if_missing = true;
  376. DB *database = nullptr;
  377. leveldb::Status status = DB::Open(options, directory.ToUtf8String(), &database);
  378. if (!status.ok()) {
  379. return Status{FirestoreErrorCode::Internal,
  380. StringFormat("Failed to open LevelDB database at %s", directory.ToUtf8String())}
  381. .CausedBy(ConvertStatus(status));
  382. }
  383. return std::unique_ptr<DB>(database);
  384. }
  385. - (LevelDbTransaction *)currentTransaction {
  386. HARD_ASSERT(_transaction != nullptr, "Attempting to access transaction before one has started");
  387. return _transaction.get();
  388. }
  389. #pragma mark - Persistence Factory methods
  390. - (id<FSTMutationQueue>)mutationQueueForUser:(const User &)user {
  391. _users.insert(user.uid());
  392. return [FSTLevelDBMutationQueue mutationQueueWithUser:user db:self serializer:self.serializer];
  393. }
  394. - (LevelDbQueryCache *)queryCache {
  395. return _queryCache.get();
  396. }
  397. - (RemoteDocumentCache *)remoteDocumentCache {
  398. return _documentCache.get();
  399. }
  400. - (void)startTransaction:(absl::string_view)label {
  401. HARD_ASSERT(_transaction == nullptr, "Starting a transaction while one is already outstanding");
  402. _transaction = absl::make_unique<LevelDbTransaction>(_ptr.get(), label);
  403. [_referenceDelegate transactionWillStart];
  404. }
  405. - (void)commitTransaction {
  406. HARD_ASSERT(_transaction != nullptr, "Committing a transaction before one is started");
  407. [_referenceDelegate transactionWillCommit];
  408. _transaction->Commit();
  409. _transaction.reset();
  410. }
  411. - (void)shutdown {
  412. HARD_ASSERT(self.isStarted, "FSTLevelDB shutdown without start!");
  413. self.started = NO;
  414. _ptr.reset();
  415. }
  416. - (id<FSTReferenceDelegate>)referenceDelegate {
  417. return _referenceDelegate;
  418. }
  419. - (ListenSequenceNumber)currentSequenceNumber {
  420. return [_referenceDelegate currentSequenceNumber];
  421. }
  422. @end
  423. NS_ASSUME_NONNULL_END