FSTLevelDB.mm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/Local/FSTLevelDBMigrations.h"
  21. #import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
  22. #import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
  23. #import "Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h"
  24. #import "Firestore/Source/Remote/FSTSerializerBeta.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_transaction.h"
  28. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  29. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  30. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  31. #include "absl/memory/memory.h"
  32. #include "leveldb/db.h"
  33. namespace util = firebase::firestore::util;
  34. using firebase::firestore::auth::User;
  35. using firebase::firestore::core::DatabaseInfo;
  36. using firebase::firestore::model::DatabaseId;
  37. NS_ASSUME_NONNULL_BEGIN
  38. static NSString *const kReservedPathComponent = @"firestore";
  39. using firebase::firestore::local::LevelDbTransaction;
  40. using leveldb::DB;
  41. using leveldb::Options;
  42. using leveldb::ReadOptions;
  43. using leveldb::Status;
  44. using leveldb::WriteOptions;
  45. @interface FSTLevelDB ()
  46. @property(nonatomic, copy) NSString *directory;
  47. @property(nonatomic, assign, getter=isStarted) BOOL started;
  48. @property(nonatomic, strong, readonly) FSTLocalSerializer *serializer;
  49. @end
  50. @implementation FSTLevelDB {
  51. std::unique_ptr<LevelDbTransaction> _transaction;
  52. std::unique_ptr<leveldb::DB> _ptr;
  53. FSTTransactionRunner _transactionRunner;
  54. }
  55. /**
  56. * For now this is paranoid, but perhaps disable that in production builds.
  57. */
  58. + (const ReadOptions)standardReadOptions {
  59. ReadOptions options;
  60. options.verify_checksums = true;
  61. return options;
  62. }
  63. - (instancetype)initWithDirectory:(NSString *)directory
  64. serializer:(FSTLocalSerializer *)serializer {
  65. if (self = [super init]) {
  66. _directory = [directory copy];
  67. _serializer = serializer;
  68. _transactionRunner.SetBackingPersistence(self);
  69. }
  70. return self;
  71. }
  72. - (leveldb::DB *)ptr {
  73. return _ptr.get();
  74. }
  75. - (const FSTTransactionRunner &)run {
  76. return _transactionRunner;
  77. }
  78. + (NSString *)documentsDirectory {
  79. #if TARGET_OS_IPHONE
  80. NSArray<NSString *> *directories =
  81. NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  82. return [directories[0] stringByAppendingPathComponent:kReservedPathComponent];
  83. #elif TARGET_OS_MAC
  84. NSString *dotPrefixed = [@"." stringByAppendingString:kReservedPathComponent];
  85. return [NSHomeDirectory() stringByAppendingPathComponent:dotPrefixed];
  86. #else
  87. #error "local storage on tvOS"
  88. // TODO(mcg): Writing to NSDocumentsDirectory on tvOS will fail; we need to write to Caches
  89. // https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/
  90. #endif
  91. }
  92. + (NSString *)storageDirectoryForDatabaseInfo:(const DatabaseInfo &)databaseInfo
  93. documentsDirectory:(NSString *)documentsDirectory {
  94. // Use two different path formats:
  95. //
  96. // * persistenceKey / projectID . databaseID / name
  97. // * persistenceKey / projectID / name
  98. //
  99. // projectIDs are DNS-compatible names and cannot contain dots so there's
  100. // no danger of collisions.
  101. NSString *directory = documentsDirectory;
  102. directory =
  103. [directory stringByAppendingPathComponent:util::WrapNSString(databaseInfo.persistence_key())];
  104. NSString *segment = util::WrapNSString(databaseInfo.database_id().project_id());
  105. if (!databaseInfo.database_id().IsDefaultDatabase()) {
  106. segment = [NSString
  107. stringWithFormat:@"%@.%s", segment, databaseInfo.database_id().database_id().c_str()];
  108. }
  109. directory = [directory stringByAppendingPathComponent:segment];
  110. // Reserve one additional path component to allow multiple physical databases
  111. directory = [directory stringByAppendingPathComponent:@"main"];
  112. return directory;
  113. }
  114. #pragma mark - Startup
  115. - (BOOL)start:(NSError **)error {
  116. HARD_ASSERT(!self.isStarted, "FSTLevelDB double-started!");
  117. self.started = YES;
  118. NSString *directory = self.directory;
  119. if (![self ensureDirectory:directory error:error]) {
  120. return NO;
  121. }
  122. DB *database = [self createDBWithDirectory:directory error:error];
  123. if (!database) {
  124. return NO;
  125. }
  126. _ptr.reset(database);
  127. [FSTLevelDBMigrations runMigrationsWithDatabase:_ptr.get()];
  128. return YES;
  129. }
  130. /** Creates the directory at @a directory and marks it as excluded from iCloud backup. */
  131. - (BOOL)ensureDirectory:(NSString *)directory error:(NSError **)error {
  132. NSError *localError;
  133. NSFileManager *files = [NSFileManager defaultManager];
  134. BOOL success = [files createDirectoryAtPath:directory
  135. withIntermediateDirectories:YES
  136. attributes:nil
  137. error:&localError];
  138. if (!success) {
  139. *error =
  140. [NSError errorWithDomain:FIRFirestoreErrorDomain
  141. code:FIRFirestoreErrorCodeInternal
  142. userInfo:@{
  143. NSLocalizedDescriptionKey : @"Failed to create persistence directory",
  144. NSUnderlyingErrorKey : localError
  145. }];
  146. return NO;
  147. }
  148. NSURL *dirURL = [NSURL fileURLWithPath:directory];
  149. success = [dirURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&localError];
  150. if (!success) {
  151. *error = [NSError errorWithDomain:FIRFirestoreErrorDomain
  152. code:FIRFirestoreErrorCodeInternal
  153. userInfo:@{
  154. NSLocalizedDescriptionKey :
  155. @"Failed mark persistence directory as excluded from backups",
  156. NSUnderlyingErrorKey : localError
  157. }];
  158. return NO;
  159. }
  160. return YES;
  161. }
  162. /** Opens the database within the given directory. */
  163. - (nullable DB *)createDBWithDirectory:(NSString *)directory error:(NSError **)error {
  164. Options options;
  165. options.create_if_missing = true;
  166. DB *database;
  167. Status status = DB::Open(options, [directory UTF8String], &database);
  168. if (!status.ok()) {
  169. if (error) {
  170. NSString *name = [directory lastPathComponent];
  171. *error =
  172. [FSTLevelDB errorWithStatus:status
  173. description:@"Failed to create database %@ at path %@", name, directory];
  174. }
  175. return nullptr;
  176. }
  177. return database;
  178. }
  179. - (LevelDbTransaction *)currentTransaction {
  180. HARD_ASSERT(_transaction != nullptr, "Attempting to access transaction before one has started");
  181. return _transaction.get();
  182. }
  183. #pragma mark - Persistence Factory methods
  184. - (id<FSTMutationQueue>)mutationQueueForUser:(const User &)user {
  185. return [FSTLevelDBMutationQueue mutationQueueWithUser:user db:self serializer:self.serializer];
  186. }
  187. - (id<FSTQueryCache>)queryCache {
  188. return [[FSTLevelDBQueryCache alloc] initWithDB:self serializer:self.serializer];
  189. }
  190. - (id<FSTRemoteDocumentCache>)remoteDocumentCache {
  191. return [[FSTLevelDBRemoteDocumentCache alloc] initWithDB:self serializer:self.serializer];
  192. }
  193. - (void)startTransaction:(absl::string_view)label {
  194. HARD_ASSERT(_transaction == nullptr, "Starting a transaction while one is already outstanding");
  195. _transaction = absl::make_unique<LevelDbTransaction>(_ptr.get(), label);
  196. }
  197. - (void)commitTransaction {
  198. HARD_ASSERT(_transaction != nullptr, "Committing a transaction before one is started");
  199. _transaction->Commit();
  200. _transaction.reset();
  201. }
  202. - (void)shutdown {
  203. HARD_ASSERT(self.isStarted, "FSTLevelDB shutdown without start!");
  204. self.started = NO;
  205. _ptr.reset();
  206. }
  207. - (_Nullable id<FSTReferenceDelegate>)referenceDelegate {
  208. return nil;
  209. }
  210. #pragma mark - Error and Status
  211. + (nullable NSError *)errorWithStatus:(Status)status description:(NSString *)description, ... {
  212. if (status.ok()) {
  213. return nil;
  214. }
  215. va_list args;
  216. va_start(args, description);
  217. NSString *message = [[NSString alloc] initWithFormat:description arguments:args];
  218. NSString *reason = [self descriptionOfStatus:status];
  219. NSError *result = [NSError errorWithDomain:FIRFirestoreErrorDomain
  220. code:FIRFirestoreErrorCodeInternal
  221. userInfo:@{
  222. NSLocalizedDescriptionKey : message,
  223. NSLocalizedFailureReasonErrorKey : reason
  224. }];
  225. va_end(args);
  226. return result;
  227. }
  228. + (NSString *)descriptionOfStatus:(Status)status {
  229. return [NSString stringWithCString:status.ToString().c_str() encoding:NSUTF8StringEncoding];
  230. }
  231. @end
  232. NS_ASSUME_NONNULL_END