FSTLevelDB.mm 16 KB

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