darling-cocotron/CoreData/NSPersistentStoreCoordinator.m

292 lines
9.6 KiB
Mathematica
Raw Permalink Normal View History

/* Copyright (c)2008 Dan Knapp
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import "NSInMemoryPersistentStore.h"
#import "NSManagedObjectID-Private.h"
#import "NSXMLPersistentStore.h"
#import <CoreData/NSManagedObject.h>
#import <CoreData/NSManagedObjectModel.h>
#import <CoreData/NSPersistentStoreCoordinator.h>
#import <Foundation/NSRaise.h>
NSString *const NSStoreTypeKey = @"NSStoreTypeKey";
NSString *const NSStoreUUIDKey = @"NSStoreUUIDKey";
NSString *const NSXMLStoreType = @"XML";
NSString *const NSInMemoryStoreType = @"Memory";
NSString *const NSSQLiteStoreType = @"SQLite";
NSString *const NSBinaryStoreType = @"Binary";
NSString *const NSMigratePersistentStoresAutomaticallyOption =
2020-05-12 18:51:39 +00:00
@"NSMigratePersistentStoresAutomaticallyOption";
NSString *const NSPersistentStoreCoordinatorStoresDidChangeNotification =
2020-05-12 18:51:39 +00:00
@"NSPersistentStoreCoordinatorStoresDidChangeNotification";
NSString *const NSAddedPersistentStoresKey = @"NSAddedPersistentStoresKey";
NSString *const NSRemovedPersistentStoresKey = @"NSRemovedPersistentStoresKey";
NSString *const NSUUIDChangedPersistentStoresKey =
2020-05-12 18:51:39 +00:00
@"NSUUIDChangedPersistentStoresKey";
NSString *const NSPersistentStoreSaveConflictsErrorKey = @"conflictList";
NSString *const NSInferMappingModelAutomaticallyOption =
2020-05-12 18:51:39 +00:00
@"NSInferMappingModelAutomaticallyOption";
NSString *const NSReadOnlyPersistentStoreOption =
2020-05-12 18:51:39 +00:00
@"NSReadOnlyPersistentStoreOption";
2024-03-10 20:35:07 +00:00
NSString *const NSSQLiteManualVacuumOption = @"NSSQLiteManualVacuumOption";
2021-03-05 03:00:28 +00:00
NSString *const NSStoreModelVersionHashesKey =
@"NSStoreModelVersionHashesKey";
@implementation NSPersistentStoreCoordinator
static NSMutableDictionary *_storeTypes = nil;
+ (void) initialize {
if (self == [NSPersistentStoreCoordinator class]) {
_storeTypes = [NSMutableDictionary new];
[_storeTypes setObject: [NSInMemoryPersistentStore class]
forKey: NSInMemoryStoreType];
[_storeTypes setObject: [NSXMLPersistentStore class]
forKey: NSXMLStoreType];
}
}
+ (NSDictionary *) registeredStoreTypes {
return _storeTypes;
}
+ (void) registerStoreClass: (Class) storeClass
2020-05-12 00:04:26 +00:00
forStoreType: (NSString *) storeType
{
[_storeTypes setObject: storeClass forKey: storeType];
}
- initWithManagedObjectModel: (NSManagedObjectModel *) model {
_lock = [[NSLock alloc] init];
_model = [model retain];
_stores = [[NSMutableArray alloc] init];
return self;
}
- (void) dealloc {
[_lock release];
[_model release];
[_stores release];
[super dealloc];
}
- (NSManagedObjectModel *) managedObjectModel {
return _model;
}
- (NSPersistentStore *) addPersistentStoreWithType: (NSString *) storeType
configuration: (NSString *) configuration
URL: (NSURL *) storeURL
options: (NSDictionary *) options
2020-05-12 00:04:26 +00:00
error: (NSError **) error
{
/* It appears that this method will determine the store type if it is nil */
if (storeType == nil) {
for (Class class in [_storeTypes allValues]) {
NSDictionary *metadata =
2020-05-12 18:51:39 +00:00
[class metadataForPersistentStoreWithURL: storeURL
error: nil];
if ((storeType = [metadata objectForKey: NSStoreTypeKey]) != nil)
break;
}
}
Class class = [[[self class] registeredStoreTypes] objectForKey: storeType];
NSAtomicStore *store = [[[class alloc]
2020-05-12 18:51:39 +00:00
initWithPersistentStoreCoordinator: self
configurationName: configuration
URL: storeURL
options: options] autorelease];
if (![store load: error])
return nil;
[_stores addObject: store];
return store;
}
- (BOOL) setURL: (NSURL *) url forPersistentStore: (NSPersistentStore *) store {
[store setURL: url];
return YES;
}
- (BOOL) removePersistentStore: (NSPersistentStore *) store
2020-05-12 00:04:26 +00:00
error: (NSError **) error
{
NSArray *remove = [NSArray arrayWithObject: store];
NSDictionary *userInfo =
2020-05-12 18:51:39 +00:00
[NSDictionary dictionaryWithObject: remove
forKey: NSRemovedPersistentStoresKey];
[store willRemoveFromPersistentStoreCoordinator: self];
[[NSNotificationCenter defaultCenter]
2020-05-12 18:51:39 +00:00
postNotificationName:
NSPersistentStoreCoordinatorStoresDidChangeNotification
object: self
userInfo: userInfo];
[_stores removeObjectIdenticalTo: store];
return YES;
}
- (NSPersistentStore *) migratePersistentStore: (NSPersistentStore *) store
toURL: (NSURL *) URL
options: (NSDictionary *) options
withType: (NSString *) storeType
2020-05-12 00:04:26 +00:00
error: (NSError **) error
{
NSUnimplementedMethod();
return nil;
}
- (NSArray *) persistentStores {
return _stores;
}
- (NSPersistentStore *) persistentStoreForURL: (NSURL *) URL {
/* This only returns a store if it exists in the coordinator */
for (NSPersistentStore *check in _stores) {
if ([[check URL] isEqual: URL])
return check;
}
return nil;
}
- (NSURL *) URLForPersistentStore: (NSPersistentStore *) store {
return [store URL];
}
- (void) lock {
[_lock lock];
}
- (BOOL) tryLock {
return [_lock tryLock];
}
- (void) unlock {
[_lock unlock];
}
- (NSDictionary *) metadataForPersistentStore: (NSPersistentStore *) store {
return [store metadata];
}
- (void) setMetadata: (NSDictionary *) value
2020-05-12 18:51:39 +00:00
forPersistentStore: (NSPersistentStore *) store
2020-05-12 00:04:26 +00:00
{
[store setMetadata: value];
}
+ (BOOL) setMetadata: (NSDictionary *) metadata
2020-05-12 18:51:39 +00:00
forPersistentStoreOfType: (NSString *) storeType
URL: (NSURL *) url
error: (NSError **) error
2020-05-12 00:04:26 +00:00
{
Class check = [[self registeredStoreTypes] objectForKey: storeType];
return [check setMetadata: metadata
2020-05-12 18:51:39 +00:00
forPersistentStoreWithURL: url
error: error];
}
+ (NSDictionary *) metadataForPersistentStoreOfType: (NSString *) storeType
URL: (NSURL *) url
2020-05-12 00:04:26 +00:00
error: (NSError **) error
{
Class check = [[self registeredStoreTypes] objectForKey: storeType];
return [check metadataForPersistentStoreWithURL: url error: error];
}
2020-05-12 00:04:26 +00:00
- (NSPersistentStore *) _persistentStoreWithIdentifier: (NSString *) identifier
{
for (NSPersistentStore *check in _stores)
if ([[check identifier] isEqualToString: identifier])
return check;
return nil;
}
- (NSPersistentStore *) _persistentStoreForObjectID:
2020-05-12 18:51:39 +00:00
(NSManagedObjectID *) objectID
2020-05-12 00:04:26 +00:00
{
NSEntityDescription *entity = [objectID entity];
NSString *storeIdentifier = [objectID storeIdentifier];
NSPersistentStore *check =
2020-05-12 18:51:39 +00:00
[self _persistentStoreWithIdentifier: storeIdentifier];
if (check != nil)
return check;
NSManagedObjectModel *model = [self managedObjectModel];
if ([_stores count] == 0) {
[NSException
2020-05-12 18:51:39 +00:00
raise: NSInvalidArgumentException
format: @"-[%@ %s] no persistent stores", [self class], _cmd];
return nil;
}
for (check in _stores) {
NSString *configurationName = [check configurationName];
if (configurationName == nil) {
NSArray *entities =
2020-05-12 18:51:39 +00:00
[model entitiesForConfiguration: configurationName];
if ([entities containsObject: entity])
return check;
}
}
return [_stores objectAtIndex: 0];
}
- (NSPersistentStore *) _persistentStoreForObject: (NSManagedObject *) object {
return [self _persistentStoreForObjectID: [object objectID]];
}
- (NSManagedObjectID *) managedObjectIDForURIRepresentation: (NSURL *) URL {
NSString *scheme = [URL scheme];
NSString *host = [URL host];
NSString *path = [URL path];
NSString *referenceObject = [path lastPathComponent];
NSString *entityName =
2020-05-12 18:51:39 +00:00
[[path stringByDeletingLastPathComponent] lastPathComponent];
NSManagedObjectModel *model = [self managedObjectModel];
NSEntityDescription *entity =
2020-05-12 18:51:39 +00:00
[[model entitiesByName] objectForKey: entityName];
return [(NSAtomicStore *) [self _persistentStoreWithIdentifier: host]
2020-05-12 18:51:39 +00:00
objectIDForEntity: entity
referenceObject: referenceObject];
}
@end