RLMRealmConfiguration.mm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMRealmConfiguration_Private.h"
  19. #import "RLMObjectSchema_Private.hpp"
  20. #import "RLMRealm_Private.h"
  21. #import "RLMSchema_Private.hpp"
  22. #import "RLMUtil.hpp"
  23. #import "schema.hpp"
  24. #import "shared_realm.hpp"
  25. #if REALM_ENABLE_SYNC
  26. #import "sync/sync_config.hpp"
  27. #else
  28. @class RLMSyncConfiguration;
  29. #endif
  30. static NSString *const c_RLMRealmConfigurationProperties[] = {
  31. @"fileURL",
  32. @"inMemoryIdentifier",
  33. @"encryptionKey",
  34. @"readOnly",
  35. @"schemaVersion",
  36. @"migrationBlock",
  37. @"deleteRealmIfMigrationNeeded",
  38. @"shouldCompactOnLaunch",
  39. @"dynamic",
  40. @"customSchema",
  41. };
  42. static NSString *const c_defaultRealmFileName = @"default.realm";
  43. RLMRealmConfiguration *s_defaultConfiguration;
  44. NSString *RLMRealmPathForFileAndBundleIdentifier(NSString *fileName, NSString *bundleIdentifier) {
  45. return [RLMDefaultDirectoryForBundleIdentifier(bundleIdentifier)
  46. stringByAppendingPathComponent:fileName];
  47. }
  48. NSString *RLMRealmPathForFile(NSString *fileName) {
  49. static NSString *directory = RLMDefaultDirectoryForBundleIdentifier(nil);
  50. return [directory stringByAppendingPathComponent:fileName];
  51. }
  52. @implementation RLMRealmConfiguration {
  53. realm::Realm::Config _config;
  54. }
  55. - (realm::Realm::Config&)config {
  56. return _config;
  57. }
  58. + (instancetype)defaultConfiguration {
  59. return [[self rawDefaultConfiguration] copy];
  60. }
  61. + (void)setDefaultConfiguration:(RLMRealmConfiguration *)configuration {
  62. if (!configuration) {
  63. @throw RLMException(@"Cannot set the default configuration to nil.");
  64. }
  65. @synchronized(c_defaultRealmFileName) {
  66. s_defaultConfiguration = [configuration copy];
  67. }
  68. }
  69. + (RLMRealmConfiguration *)rawDefaultConfiguration {
  70. RLMRealmConfiguration *configuration;
  71. @synchronized(c_defaultRealmFileName) {
  72. if (!s_defaultConfiguration) {
  73. s_defaultConfiguration = [[RLMRealmConfiguration alloc] init];
  74. }
  75. configuration = s_defaultConfiguration;
  76. }
  77. return configuration;
  78. }
  79. + (void)resetRealmConfigurationState {
  80. @synchronized(c_defaultRealmFileName) {
  81. s_defaultConfiguration = nil;
  82. }
  83. }
  84. - (instancetype)init {
  85. self = [super init];
  86. if (self) {
  87. static NSURL *defaultRealmURL = [NSURL fileURLWithPath:RLMRealmPathForFile(c_defaultRealmFileName)];
  88. self.fileURL = defaultRealmURL;
  89. self.schemaVersion = 0;
  90. self.cache = YES;
  91. // We have our own caching of RLMRealm instances, so the ObjectStore
  92. // cache is at best pointless, and may result in broken behavior when
  93. // a realm::Realm instance outlives the RLMRealm (due to collection
  94. // notifiers being in the middle of running when the RLMRealm is
  95. // dealloced) and then reused for a new RLMRealm
  96. _config.cache = false;
  97. }
  98. return self;
  99. }
  100. - (instancetype)copyWithZone:(NSZone *)zone {
  101. RLMRealmConfiguration *configuration = [[[self class] allocWithZone:zone] init];
  102. configuration->_config = _config;
  103. configuration->_cache = _cache;
  104. configuration->_dynamic = _dynamic;
  105. configuration->_migrationBlock = _migrationBlock;
  106. configuration->_shouldCompactOnLaunch = _shouldCompactOnLaunch;
  107. configuration->_customSchema = _customSchema;
  108. return configuration;
  109. }
  110. - (NSString *)description {
  111. NSMutableString *string = [NSMutableString stringWithFormat:@"%@ {\n", self.class];
  112. for (NSString *key : c_RLMRealmConfigurationProperties) {
  113. NSString *description = [[self valueForKey:key] description];
  114. description = [description stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
  115. [string appendFormat:@"\t%@ = %@;\n", key, description];
  116. }
  117. return [string stringByAppendingString:@"}"];
  118. }
  119. static void RLMNSStringToStdString(std::string &out, NSString *in) {
  120. out.resize([in maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
  121. if (out.empty()) {
  122. return;
  123. }
  124. NSUInteger size = out.size();
  125. [in getBytes:&out[0]
  126. maxLength:size
  127. usedLength:&size
  128. encoding:NSUTF8StringEncoding
  129. options:0 range:{0, in.length} remainingRange:nullptr];
  130. out.resize(size);
  131. }
  132. - (NSURL *)fileURL {
  133. if (_config.in_memory || _config.sync_config) {
  134. return nil;
  135. }
  136. return [NSURL fileURLWithPath:@(_config.path.c_str())];
  137. }
  138. - (void)setFileURL:(NSURL *)fileURL {
  139. NSString *path = fileURL.path;
  140. if (path.length == 0) {
  141. @throw RLMException(@"Realm path must not be empty");
  142. }
  143. _config.sync_config = nullptr;
  144. RLMNSStringToStdString(_config.path, path);
  145. _config.in_memory = false;
  146. }
  147. - (NSString *)inMemoryIdentifier {
  148. if (!_config.in_memory) {
  149. return nil;
  150. }
  151. return [@(_config.path.c_str()) lastPathComponent];
  152. }
  153. - (void)setInMemoryIdentifier:(NSString *)inMemoryIdentifier {
  154. if (inMemoryIdentifier.length == 0) {
  155. @throw RLMException(@"In-memory identifier must not be empty");
  156. }
  157. _config.sync_config = nullptr;
  158. RLMNSStringToStdString(_config.path, [NSTemporaryDirectory() stringByAppendingPathComponent:inMemoryIdentifier]);
  159. _config.in_memory = true;
  160. }
  161. - (NSData *)encryptionKey {
  162. return _config.encryption_key.empty() ? nil : [NSData dataWithBytes:_config.encryption_key.data() length:_config.encryption_key.size()];
  163. }
  164. - (void)setEncryptionKey:(NSData * __nullable)encryptionKey {
  165. if (NSData *key = RLMRealmValidatedEncryptionKey(encryptionKey)) {
  166. auto bytes = static_cast<const char *>(key.bytes);
  167. _config.encryption_key.assign(bytes, bytes + key.length);
  168. #if REALM_ENABLE_SYNC
  169. if (_config.sync_config) {
  170. auto& sync_encryption_key = self.config.sync_config->realm_encryption_key;
  171. sync_encryption_key = std::array<char, 64>();
  172. std::copy_n(_config.encryption_key.begin(), 64, sync_encryption_key->begin());
  173. }
  174. #endif
  175. }
  176. else {
  177. _config.encryption_key.clear();
  178. #if REALM_ENABLE_SYNC
  179. if (_config.sync_config)
  180. _config.sync_config->realm_encryption_key = realm::util::none;
  181. #endif
  182. }
  183. }
  184. - (BOOL)readOnly {
  185. return _config.immutable();
  186. }
  187. - (void)setReadOnly:(BOOL)readOnly {
  188. if (readOnly) {
  189. if (self.deleteRealmIfMigrationNeeded) {
  190. @throw RLMException(@"Cannot set `readOnly` when `deleteRealmIfMigrationNeeded` is set.");
  191. } else if (self.shouldCompactOnLaunch) {
  192. @throw RLMException(@"Cannot set `readOnly` when `shouldCompactOnLaunch` is set.");
  193. }
  194. _config.schema_mode = realm::SchemaMode::Immutable;
  195. }
  196. else if (self.readOnly) {
  197. _config.schema_mode = realm::SchemaMode::Automatic;
  198. }
  199. }
  200. - (uint64_t)schemaVersion {
  201. return _config.schema_version;
  202. }
  203. - (void)setSchemaVersion:(uint64_t)schemaVersion {
  204. if (schemaVersion == RLMNotVersioned) {
  205. @throw RLMException(@"Cannot set schema version to %llu (RLMNotVersioned)", RLMNotVersioned);
  206. }
  207. _config.schema_version = schemaVersion;
  208. }
  209. - (BOOL)deleteRealmIfMigrationNeeded {
  210. return _config.schema_mode == realm::SchemaMode::ResetFile;
  211. }
  212. - (void)setDeleteRealmIfMigrationNeeded:(BOOL)deleteRealmIfMigrationNeeded {
  213. if (deleteRealmIfMigrationNeeded) {
  214. if (self.readOnly) {
  215. @throw RLMException(@"Cannot set `deleteRealmIfMigrationNeeded` when `readOnly` is set.");
  216. }
  217. _config.schema_mode = realm::SchemaMode::ResetFile;
  218. }
  219. else if (self.deleteRealmIfMigrationNeeded) {
  220. _config.schema_mode = realm::SchemaMode::Automatic;
  221. }
  222. }
  223. - (NSArray *)objectClasses {
  224. return [_customSchema.objectSchema valueForKeyPath:@"objectClass"];
  225. }
  226. - (void)setObjectClasses:(NSArray *)objectClasses {
  227. self.customSchema = [RLMSchema schemaWithObjectClasses:objectClasses];
  228. }
  229. - (void)setDynamic:(bool)dynamic {
  230. _dynamic = dynamic;
  231. self.cache = !dynamic;
  232. }
  233. - (bool)disableFormatUpgrade {
  234. return _config.disable_format_upgrade;
  235. }
  236. - (void)setDisableFormatUpgrade:(bool)disableFormatUpgrade {
  237. _config.disable_format_upgrade = disableFormatUpgrade;
  238. }
  239. - (realm::SchemaMode)schemaMode {
  240. return _config.schema_mode;
  241. }
  242. - (void)setSchemaMode:(realm::SchemaMode)mode {
  243. _config.schema_mode = mode;
  244. }
  245. - (NSString *)pathOnDisk {
  246. return @(_config.path.c_str());
  247. }
  248. - (void)setShouldCompactOnLaunch:(RLMShouldCompactOnLaunchBlock)shouldCompactOnLaunch {
  249. if (shouldCompactOnLaunch) {
  250. if (self.readOnly) {
  251. @throw RLMException(@"Cannot set `shouldCompactOnLaunch` when `readOnly` is set.");
  252. }
  253. _config.should_compact_on_launch_function = [=](size_t totalBytes, size_t usedBytes) {
  254. return shouldCompactOnLaunch(totalBytes, usedBytes);
  255. };
  256. }
  257. else {
  258. _config.should_compact_on_launch_function = nullptr;
  259. }
  260. _shouldCompactOnLaunch = shouldCompactOnLaunch;
  261. }
  262. - (void)setCustomSchemaWithoutCopying:(RLMSchema *)schema {
  263. _customSchema = schema;
  264. }
  265. #if !REALM_ENABLE_SYNC
  266. - (RLMSyncConfiguration *)syncConfiguration {
  267. return nil;
  268. }
  269. #endif
  270. @end