FSTLevelDB.mm 19 KB

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