FSTLevelDB.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/Local/FSTLevelDBQueryCache.h"
  24. #import "Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h"
  25. #import "Firestore/Source/Local/FSTReferenceSet.h"
  26. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  27. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  28. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  29. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  30. #include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
  31. #include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h"
  32. #include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h"
  33. #include "Firestore/core/src/firebase/firestore/local/leveldb_util.h"
  34. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  35. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  36. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  37. #include "Firestore/core/src/firebase/firestore/util/filesystem.h"
  38. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  39. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  40. #include "Firestore/core/src/firebase/firestore/util/statusor.h"
  41. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  42. #include "Firestore/core/src/firebase/firestore/util/string_util.h"
  43. #include "absl/memory/memory.h"
  44. #include "absl/strings/match.h"
  45. #include "absl/strings/str_cat.h"
  46. #include "leveldb/db.h"
  47. NS_ASSUME_NONNULL_BEGIN
  48. namespace util = firebase::firestore::util;
  49. using firebase::firestore::FirestoreErrorCode;
  50. using firebase::firestore::auth::User;
  51. using firebase::firestore::core::DatabaseInfo;
  52. using firebase::firestore::local::ConvertStatus;
  53. using firebase::firestore::local::LevelDbDocumentMutationKey;
  54. using firebase::firestore::local::LevelDbDocumentTargetKey;
  55. using firebase::firestore::local::LevelDbMigrations;
  56. using firebase::firestore::local::LevelDbMutationKey;
  57. using firebase::firestore::local::LevelDbTransaction;
  58. using firebase::firestore::model::DatabaseId;
  59. using firebase::firestore::model::DocumentKey;
  60. using firebase::firestore::model::ListenSequenceNumber;
  61. using firebase::firestore::model::ResourcePath;
  62. using firebase::firestore::util::OrderedCode;
  63. using firebase::firestore::util::Path;
  64. using firebase::firestore::util::Status;
  65. using firebase::firestore::util::StatusOr;
  66. using firebase::firestore::util::StringFormat;
  67. using leveldb::DB;
  68. using leveldb::Options;
  69. using leveldb::ReadOptions;
  70. using leveldb::WriteOptions;
  71. static const char *kReservedPathComponent = "firestore";
  72. /**
  73. * Provides LRU functionality for leveldb persistence.
  74. *
  75. * Although this could implement FSTTransactional, it doesn't because it is not directly tied to
  76. * a transaction runner, it just happens to be called from FSTLevelDB, which is FSTTransactional.
  77. */
  78. @interface FSTLevelDBLRUDelegate : NSObject <FSTReferenceDelegate, FSTLRUDelegate>
  79. - (void)transactionWillStart;
  80. - (void)transactionWillCommit;
  81. - (void)start;
  82. @end
  83. @implementation FSTLevelDBLRUDelegate {
  84. FSTLRUGarbageCollector *_gc;
  85. // This delegate should have the same lifetime as the persistence layer, but mark as
  86. // weak to avoid retain cycle.
  87. __weak FSTLevelDB *_db;
  88. FSTReferenceSet *_additionalReferences;
  89. ListenSequenceNumber _currentSequenceNumber;
  90. FSTListenSequence *_listenSequence;
  91. }
  92. - (instancetype)initWithPersistence:(FSTLevelDB *)persistence {
  93. if (self = [super init]) {
  94. _gc =
  95. [[FSTLRUGarbageCollector alloc] initWithQueryCache:[persistence queryCache] delegate:self];
  96. _db = persistence;
  97. _currentSequenceNumber = kFSTListenSequenceNumberInvalid;
  98. }
  99. return self;
  100. }
  101. - (void)start {
  102. ListenSequenceNumber highestSequenceNumber = _db.queryCache.highestListenSequenceNumber;
  103. _listenSequence = [[FSTListenSequence alloc] initStartingAfter:highestSequenceNumber];
  104. }
  105. - (void)transactionWillStart {
  106. HARD_ASSERT(_currentSequenceNumber == kFSTListenSequenceNumberInvalid,
  107. "Previous sequence number is still in effect");
  108. _currentSequenceNumber = [_listenSequence next];
  109. }
  110. - (void)transactionWillCommit {
  111. _currentSequenceNumber = kFSTListenSequenceNumberInvalid;
  112. }
  113. - (ListenSequenceNumber)currentSequenceNumber {
  114. HARD_ASSERT(_currentSequenceNumber != kFSTListenSequenceNumberInvalid,
  115. "Asking for a sequence number outside of a transaction");
  116. return _currentSequenceNumber;
  117. }
  118. - (void)addInMemoryPins:(FSTReferenceSet *)set {
  119. // We should be able to assert that _additionalReferences is nil, but due to restarts in spec
  120. // tests it would fail.
  121. _additionalReferences = set;
  122. }
  123. - (void)removeTarget:(FSTQueryData *)queryData {
  124. FSTQueryData *updated =
  125. [queryData queryDataByReplacingSnapshotVersion:queryData.snapshotVersion
  126. resumeToken:queryData.resumeToken
  127. sequenceNumber:[self currentSequenceNumber]];
  128. [_db.queryCache updateQueryData:updated];
  129. }
  130. - (void)addReference:(const DocumentKey &)key {
  131. [self writeSentinelForKey:key];
  132. }
  133. - (void)removeReference:(const DocumentKey &)key {
  134. [self writeSentinelForKey:key];
  135. }
  136. - (BOOL)mutationQueuesContainKey:(const DocumentKey &)docKey {
  137. const std::set<std::string> &users = _db.users;
  138. const ResourcePath &path = docKey.path();
  139. std::string buffer;
  140. auto it = _db.currentTransaction->NewIterator();
  141. // For each user, if there is any batch that contains this document in any batch, we know it's
  142. // pinned.
  143. for (const std::string &user : users) {
  144. std::string mutationKey = LevelDbDocumentMutationKey::KeyPrefix(user, path);
  145. it->Seek(mutationKey);
  146. if (it->Valid() && absl::StartsWith(it->key(), mutationKey)) {
  147. return YES;
  148. }
  149. }
  150. return NO;
  151. }
  152. - (BOOL)isPinned:(const DocumentKey &)docKey {
  153. if ([_additionalReferences containsKey:docKey]) {
  154. return YES;
  155. }
  156. if ([self mutationQueuesContainKey:docKey]) {
  157. return YES;
  158. }
  159. return NO;
  160. }
  161. - (void)enumerateTargetsUsingBlock:(void (^)(FSTQueryData *queryData, BOOL *stop))block {
  162. FSTLevelDBQueryCache *queryCache = _db.queryCache;
  163. [queryCache enumerateTargetsUsingBlock:block];
  164. }
  165. - (void)enumerateMutationsUsingBlock:
  166. (void (^)(const DocumentKey &key, ListenSequenceNumber sequenceNumber, BOOL *stop))block {
  167. FSTLevelDBQueryCache *queryCache = _db.queryCache;
  168. [queryCache enumerateOrphanedDocumentsUsingBlock:block];
  169. }
  170. - (int)removeOrphanedDocumentsThroughSequenceNumber:(ListenSequenceNumber)upperBound {
  171. FSTLevelDBQueryCache *queryCache = _db.queryCache;
  172. __block int count = 0;
  173. [queryCache enumerateOrphanedDocumentsUsingBlock:^(
  174. const DocumentKey &docKey, ListenSequenceNumber sequenceNumber, BOOL *stop) {
  175. if (sequenceNumber <= upperBound) {
  176. if (![self isPinned:docKey]) {
  177. count++;
  178. [self->_db.remoteDocumentCache removeEntryForKey:docKey];
  179. }
  180. }
  181. }];
  182. return count;
  183. }
  184. - (int)removeTargetsThroughSequenceNumber:(ListenSequenceNumber)sequenceNumber
  185. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries {
  186. FSTLevelDBQueryCache *queryCache = _db.queryCache;
  187. return [queryCache removeQueriesThroughSequenceNumber:sequenceNumber liveQueries:liveQueries];
  188. }
  189. - (FSTLRUGarbageCollector *)gc {
  190. return _gc;
  191. }
  192. - (void)writeSentinelForKey:(const DocumentKey &)key {
  193. std::string encodedSequenceNumber;
  194. OrderedCode::WriteSignedNumIncreasing(&encodedSequenceNumber, [self currentSequenceNumber]);
  195. std::string sentinelKey = LevelDbDocumentTargetKey::SentinelKey(key);
  196. _db.currentTransaction->Put(sentinelKey, encodedSequenceNumber);
  197. }
  198. - (void)removeMutationReference:(const DocumentKey &)key {
  199. [self writeSentinelForKey:key];
  200. }
  201. - (void)limboDocumentUpdated:(const DocumentKey &)key {
  202. [self writeSentinelForKey:key];
  203. }
  204. @end
  205. @interface FSTLevelDB ()
  206. @property(nonatomic, assign, getter=isStarted) BOOL started;
  207. @property(nonatomic, strong, readonly) FSTLocalSerializer *serializer;
  208. @end
  209. @implementation FSTLevelDB {
  210. Path _directory;
  211. std::unique_ptr<LevelDbTransaction> _transaction;
  212. std::unique_ptr<leveldb::DB> _ptr;
  213. FSTTransactionRunner _transactionRunner;
  214. FSTLevelDBLRUDelegate *_referenceDelegate;
  215. FSTLevelDBQueryCache *_queryCache;
  216. std::set<std::string> _users;
  217. }
  218. /**
  219. * For now this is paranoid, but perhaps disable that in production builds.
  220. */
  221. + (const ReadOptions)standardReadOptions {
  222. ReadOptions options;
  223. options.verify_checksums = true;
  224. return options;
  225. }
  226. + (std::set<std::string>)collectUserSet:(LevelDbTransaction *)transaction {
  227. std::set<std::string> users;
  228. std::string tablePrefix = LevelDbMutationKey::KeyPrefix();
  229. auto it = transaction->NewIterator();
  230. it->Seek(tablePrefix);
  231. LevelDbMutationKey rowKey;
  232. while (it->Valid() && absl::StartsWith(it->key(), tablePrefix) && rowKey.Decode(it->key())) {
  233. users.insert(rowKey.user_id());
  234. auto userEnd = LevelDbMutationKey::KeyPrefix(rowKey.user_id());
  235. userEnd = util::PrefixSuccessor(userEnd);
  236. it->Seek(userEnd);
  237. }
  238. return users;
  239. }
  240. - (instancetype)initWithDirectory:(Path)directory serializer:(FSTLocalSerializer *)serializer {
  241. if (self = [super init]) {
  242. _directory = std::move(directory);
  243. _serializer = serializer;
  244. _queryCache = [[FSTLevelDBQueryCache alloc] initWithDB:self serializer:self.serializer];
  245. _referenceDelegate = [[FSTLevelDBLRUDelegate alloc] initWithPersistence:self];
  246. _transactionRunner.SetBackingPersistence(self);
  247. }
  248. return self;
  249. }
  250. - (const std::set<std::string> &)users {
  251. return _users;
  252. }
  253. - (leveldb::DB *)ptr {
  254. return _ptr.get();
  255. }
  256. - (const FSTTransactionRunner &)run {
  257. return _transactionRunner;
  258. }
  259. + (Path)documentsDirectory {
  260. #if TARGET_OS_IPHONE
  261. NSArray<NSString *> *directories =
  262. NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  263. return Path::FromNSString(directories[0]).AppendUtf8(kReservedPathComponent);
  264. #elif TARGET_OS_MAC
  265. std::string dotPrefixed = absl::StrCat(".", kReservedPathComponent);
  266. return Path::FromNSString(NSHomeDirectory()).AppendUtf8(dotPrefixed);
  267. #else
  268. #error "local storage on tvOS"
  269. // TODO(mcg): Writing to NSDocumentsDirectory on tvOS will fail; we need to write to Caches
  270. // https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/
  271. #endif
  272. }
  273. + (Path)storageDirectoryForDatabaseInfo:(const DatabaseInfo &)databaseInfo
  274. documentsDirectory:(const Path &)documentsDirectory {
  275. // Use two different path formats:
  276. //
  277. // * persistenceKey / projectID . databaseID / name
  278. // * persistenceKey / projectID / name
  279. //
  280. // projectIDs are DNS-compatible names and cannot contain dots so there's
  281. // no danger of collisions.
  282. std::string project_key = databaseInfo.database_id().project_id();
  283. if (!databaseInfo.database_id().IsDefaultDatabase()) {
  284. absl::StrAppend(&project_key, ".", databaseInfo.database_id().database_id());
  285. }
  286. // Reserve one additional path component to allow multiple physical databases
  287. return Path::JoinUtf8(documentsDirectory, databaseInfo.persistence_key(), project_key, "main");
  288. }
  289. #pragma mark - Startup
  290. - (Status)start {
  291. HARD_ASSERT(!self.isStarted, "FSTLevelDB double-started!");
  292. self.started = YES;
  293. Status status = [self ensureDirectory:_directory];
  294. if (!status.ok()) return status;
  295. StatusOr<std::unique_ptr<DB>> database = [self createDBWithDirectory:_directory];
  296. if (!database.status().ok()) {
  297. return database.status();
  298. }
  299. _ptr = std::move(database).ValueOrDie();
  300. LevelDbMigrations::RunMigrations(_ptr.get());
  301. LevelDbTransaction transaction(_ptr.get(), "Start LevelDB");
  302. _users = [FSTLevelDB collectUserSet:&transaction];
  303. transaction.Commit();
  304. [_queryCache start];
  305. [_referenceDelegate start];
  306. return Status::OK();
  307. }
  308. /** Creates the directory at @a directory and marks it as excluded from iCloud backup. */
  309. - (Status)ensureDirectory:(const Path &)directory {
  310. Status status = util::RecursivelyCreateDir(directory);
  311. if (!status.ok()) {
  312. return Status{FirestoreErrorCode::Internal, "Failed to create persistence directory"}.CausedBy(
  313. status);
  314. }
  315. NSURL *dirURL = [NSURL fileURLWithPath:directory.ToNSString()];
  316. NSError *localError = nil;
  317. if (![dirURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&localError]) {
  318. return Status{FirestoreErrorCode::Internal,
  319. "Failed to mark persistence directory as excluded from backups"}
  320. .CausedBy(Status::FromNSError(localError));
  321. }
  322. return Status::OK();
  323. }
  324. /** Opens the database within the given directory. */
  325. - (StatusOr<std::unique_ptr<DB>>)createDBWithDirectory:(const Path &)directory {
  326. Options options;
  327. options.create_if_missing = true;
  328. DB *database = nullptr;
  329. leveldb::Status status = DB::Open(options, directory.ToUtf8String(), &database);
  330. if (!status.ok()) {
  331. return Status{FirestoreErrorCode::Internal,
  332. StringFormat("Failed to open LevelDD database at %s", directory.ToUtf8String())}
  333. .CausedBy(ConvertStatus(status));
  334. }
  335. return std::unique_ptr<DB>(database);
  336. }
  337. - (LevelDbTransaction *)currentTransaction {
  338. HARD_ASSERT(_transaction != nullptr, "Attempting to access transaction before one has started");
  339. return _transaction.get();
  340. }
  341. #pragma mark - Persistence Factory methods
  342. - (id<FSTMutationQueue>)mutationQueueForUser:(const User &)user {
  343. _users.insert(user.uid());
  344. return [FSTLevelDBMutationQueue mutationQueueWithUser:user db:self serializer:self.serializer];
  345. }
  346. - (id<FSTQueryCache>)queryCache {
  347. return _queryCache;
  348. }
  349. - (id<FSTRemoteDocumentCache>)remoteDocumentCache {
  350. return [[FSTLevelDBRemoteDocumentCache alloc] initWithDB:self serializer:self.serializer];
  351. }
  352. - (void)startTransaction:(absl::string_view)label {
  353. HARD_ASSERT(_transaction == nullptr, "Starting a transaction while one is already outstanding");
  354. _transaction = absl::make_unique<LevelDbTransaction>(_ptr.get(), label);
  355. [_referenceDelegate transactionWillStart];
  356. }
  357. - (void)commitTransaction {
  358. HARD_ASSERT(_transaction != nullptr, "Committing a transaction before one is started");
  359. [_referenceDelegate transactionWillCommit];
  360. _transaction->Commit();
  361. _transaction.reset();
  362. }
  363. - (void)shutdown {
  364. HARD_ASSERT(self.isStarted, "FSTLevelDB shutdown without start!");
  365. self.started = NO;
  366. _ptr.reset();
  367. }
  368. - (id<FSTReferenceDelegate>)referenceDelegate {
  369. return _referenceDelegate;
  370. }
  371. - (ListenSequenceNumber)currentSequenceNumber {
  372. return [_referenceDelegate currentSequenceNumber];
  373. }
  374. @end
  375. NS_ASSUME_NONNULL_END