Skip to content

Commit

Permalink
GH-96 Replace MapTable for primary keys to Dictionary with unique id …
Browse files Browse the repository at this point in the history
…as a keys
  • Loading branch information
Dima Zen committed May 19, 2017
1 parent 10a0c83 commit c00f392
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion FastEasyMapping/Source/Cache/FEMObjectCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef _Nonnull id<NSFastEnumeration> (^FEMObjectCacheSource)(FEMMapping *mappi
@interface FEMObjectCache (CoreData)

- (instancetype)initWithContext:(NSManagedObjectContext *)context
presentedPrimaryKeys:(nullable NSMapTable<FEMMapping *, NSSet<id> *> *)presentedPrimaryKeys;
presentedPrimaryKeys:(nullable NSDictionary<NSNumber *, NSSet<id> *> *)presentedPrimaryKeys;

@end

Expand Down
4 changes: 2 additions & 2 deletions FastEasyMapping/Source/Cache/FEMObjectCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ - (NSDictionary *)objectsForMapping:(FEMMapping *)mapping {
@implementation FEMObjectCache (CoreData)

- (instancetype)initWithContext:(NSManagedObjectContext *)context
presentedPrimaryKeys:(nullable NSMapTable<FEMMapping *, NSSet<id> *> *)presentedPrimaryKeys
presentedPrimaryKeys:(nullable NSDictionary<NSNumber *, NSSet<id> *> *)presentedPrimaryKeys
{
return [self initWithSource:^id<NSFastEnumeration> (FEMMapping *mapping) {
NSSet *primaryKeys = [presentedPrimaryKeys objectForKey:mapping];
NSSet *primaryKeys = presentedPrimaryKeys[mapping.uniqueIdentifier];
if (primaryKeys.count > 0) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:mapping.entityName];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@", mapping.primaryKey, primaryKeys];
Expand Down
2 changes: 1 addition & 1 deletion FastEasyMapping/Source/Deserializer/FEMDeserializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ - (NSArray *)_collectionFromRepresentation:(NSArray *)representation mapping:(FE
}

- (void)beginTransactionForMapping:(FEMMapping *)mapping representation:(NSArray<id> *)representation {
NSMapTable *presentedPrimaryKeys = nil;
NSDictionary<NSNumber *, NSSet<id> *> *presentedPrimaryKeys = nil;
if ([[self.store class] requiresPrefetch]) {
presentedPrimaryKeys = FEMRepresentationCollectPresentedPrimaryKeys(representation, mapping);
}
Expand Down
2 changes: 1 addition & 1 deletion FastEasyMapping/Source/Store/FEMManagedObjectStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (instancetype)initWithContext:(NSManagedObjectContext *)context {

#pragma mark - Transaction

- (void)beginTransaction:(nullable NSMapTable<FEMMapping *, NSSet<id> *> *)presentedPrimaryKeys {
- (void)beginTransaction:(nullable NSDictionary<NSNumber *, NSSet<id> *> *)presentedPrimaryKeys {
_cache = [[FEMObjectCache alloc] initWithContext:self.context presentedPrimaryKeys:presentedPrimaryKeys];
}

Expand Down
4 changes: 2 additions & 2 deletions FastEasyMapping/Source/Store/FEMObjectStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ NS_ASSUME_NONNULL_BEGIN
@discussion Invoked by FEMDeserializer at the very beginning of deserialization.
Custom implementation may want to begin write transaction or similar. Default implementation does nothing.
@param presentedPrimaryKeys when `+[YourObjectStoreSubclass requiresPrefetch]` returns `YES` then `presentedPrimaryKeys contains a non-nil MapTable with FEMMapping to Set of primary keys pairs. In case +requiresPrefetch returns NO - nil value passed.
@param presentedPrimaryKeys when `+[YourObjectStoreSubclass requiresPrefetch]` returns `YES` then `presentedPrimaryKeys contains a non-nil Dictionary with `-[FEMMapping uniqueIdentifier]` to Set of primary keys pairs. In case +requiresPrefetch returns NO - nil value passed.
*/
- (void)beginTransaction:(nullable NSMapTable<FEMMapping *, NSSet<id> *> *)presentedPrimaryKeys;
- (void)beginTransaction:(nullable NSDictionary<NSNumber *, NSSet<id> *> *)presentedPrimaryKeys;

/**
@discussion Invoked by FEMDeserializer after all data has been deserialized.
Expand Down
2 changes: 1 addition & 1 deletion FastEasyMapping/Source/Store/FEMObjectStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@implementation FEMObjectStore

- (void)beginTransaction:(nullable NSMapTable<FEMMapping *, NSSet<id> *> *)presentedPrimaryKeys {
- (void)beginTransaction:(nullable NSDictionary<NSNumber *, NSSet<id> *> *)presentedPrimaryKeys {
// no-op
}

Expand Down
3 changes: 2 additions & 1 deletion FastEasyMapping/Source/Utility/FEMRepresentationUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ NS_ASSUME_NONNULL_BEGIN

FOUNDATION_EXTERN id FEMRepresentationRootForKeyPath(id representation, NSString *keyPath);

FOUNDATION_EXTERN NSMapTable<FEMMapping *, NSSet<id> *> *FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping);
/// Returns map of primary keys per mapping. Note that key represented by the `-[FEMMapping uniqueIdentifier]`.
FOUNDATION_EXTERN NSDictionary<NSNumber *, NSSet<id> *> *FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping);

FOUNDATION_EXTERN _Nullable id FEMRepresentationValueForAttribute(id representation, FEMAttribute *attribute);

Expand Down
22 changes: 13 additions & 9 deletions FastEasyMapping/Source/Utility/FEMRepresentationUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ id FEMRepresentationRootForKeyPath(id representation, NSString *keyPath) {
return representation;
}

void _FEMRepresentationCollectPresentedPrimaryKeys(id, FEMMapping *, NSMapTable<FEMMapping *, NSMutableSet<id> *> *);
void _FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping, NSMutableDictionary<NSNumber *, NSMutableSet<id> *> *container);

void _FEMRepresentationCollectObjectPrimaryKeys(id object, FEMMapping *mapping, NSMapTable<FEMMapping *, NSMutableSet<id> *> *container) {
void _FEMRepresentationCollectObjectPrimaryKeys(id object, FEMMapping *mapping, NSMutableDictionary<NSNumber *, NSMutableSet<id> *> *container) {
if (mapping.primaryKeyAttribute) {
id value = FEMRepresentationValueForAttribute(object, mapping.primaryKeyAttribute);
if (value && value != NSNull.null) {
[[container objectForKey:mapping] addObject:value];
[[container objectForKey:mapping.uniqueIdentifier] addObject:value];
}
}

Expand All @@ -30,7 +30,7 @@ void _FEMRepresentationCollectObjectPrimaryKeys(id object, FEMMapping *mapping,
}
}

void _FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping, NSMapTable<FEMMapping *, NSMutableSet<id> *> *container) {
void _FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping, NSMutableDictionary<NSNumber *, NSMutableSet<id> *> *container) {
if ([representation isKindOfClass:[NSArray class]]) {
for (id object in (id<NSFastEnumeration>)representation) {
_FEMRepresentationCollectObjectPrimaryKeys(object, mapping, container);
Expand All @@ -48,14 +48,18 @@ void _FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping
}
};

NSMapTable<FEMMapping *, NSSet<id> *> *FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping) {
NSDictionary<NSNumber *, NSSet<id> *> *FEMRepresentationCollectPresentedPrimaryKeys(id representation, FEMMapping *mapping) {
NSSet<FEMMapping *> *flattenMappings = [mapping flatten];

NSPointerFunctionsOptions options = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPointerPersonality;
NSMapTable *map = [[NSMapTable alloc] initWithKeyOptions:options valueOptions:options capacity:flattenMappings.count];

NSMutableDictionary<NSNumber *, NSMutableSet<id> *> *map = [[NSMutableDictionary alloc] initWithCapacity:flattenMappings.count];

for (FEMMapping *key in flattenMappings) {
[map setObject:[NSMutableSet new] forKey:key];
// When we have a set of different mappings that describes same entity / objectClass (indirect recursive relationship)
// then store interested in unified map where all of the primary keys from the different mappings but identical entity
// has the same key
if ([map objectForKey:key.uniqueIdentifier] == nil) {
[map setObject:[NSMutableSet new] forKey:key.uniqueIdentifier];
}
}

id root = FEMRepresentationRootForKeyPath(representation, mapping.rootPath);
Expand Down

0 comments on commit c00f392

Please sign in to comment.