FSTLevelDB.mm 8.7 KB

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