-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCOCrossPersistentRootDeadRelationshipCache.m
More file actions
100 lines (80 loc) · 2.71 KB
/
COCrossPersistentRootDeadRelationshipCache.m
File metadata and controls
100 lines (80 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright (C) 2015 Quentin Mathe
Date: May 2015
License: MIT (see COPYING)
*/
#import "COCrossPersistentRootDeadRelationshipCache.h"
#import "COPath.h"
//#define MISSING_ZEROING_WEAK_REF
@implementation COCrossPersistentRootDeadRelationshipCache
- (instancetype)init
{
SUPERINIT;
_pathToReferringObjects = [NSMutableDictionary new];
_referringObjectToPaths = [NSMapTable weakToStrongObjectsMapTable];
return self;
}
- (void)addReferringObject: (COObject *)aReferrer
forPath: (COPath *)aPath
{
NSHashTable *referringObjects = _pathToReferringObjects[aPath];
NSMutableSet *paths = [_referringObjectToPaths objectForKey: aReferrer];
if (referringObjects == nil)
{
// FIXME: If we don't ditch 10.7 support, we need a reverse mapping
// from each referringObject to a path set, that can be used to remove
// the referring objects when their object graph context is discarded.
referringObjects = [NSHashTable weakObjectsHashTable];
_pathToReferringObjects[aPath] = referringObjects;
}
if (paths == nil)
{
paths = [NSMutableSet new];
[_referringObjectToPaths setObject: paths
forKey: aReferrer];
}
[paths addObject: aPath];
[referringObjects addObject: aReferrer];
}
- (NSHashTable *)referringObjectsForPath: (COPath *)aPath
{
return _pathToReferringObjects[aPath];
}
- (void)removeObjectFromPathsToReferringObjects: (COObject *)aReferrer forPath: (COPath *)path
{
NSHashTable *referringObjects = _pathToReferringObjects[path];
[referringObjects removeObject: aReferrer];
if (referringObjects.count == 0)
{
[_pathToReferringObjects removeObjectForKey: path];
}
}
- (void)removeReferringObject: (COObject *)aReferrer
forPath: (COPath *)aPath
{
NSMutableSet *paths = [_referringObjectToPaths objectForKey: aReferrer];
[paths removeObject: aPath];
[self removeObjectFromPathsToReferringObjects: aReferrer
forPath: aPath];
}
- (void)removeReferringObject: (COObject *)aReferrer
{
NSMutableSet *paths = [_referringObjectToPaths objectForKey: aReferrer];
if (paths == nil)
return;
[_referringObjectToPaths removeObjectForKey: aReferrer];
for (COPath *path in paths)
{
[self removeObjectFromPathsToReferringObjects: aReferrer
forPath: path];
}
}
- (void)removePath: (COPath *)aPath
{
NSHashTable *referringObjects = [_pathToReferringObjects[aPath] copy];
for (COObject *referrer in referringObjects)
{
[self removeReferringObject: referrer forPath: aPath];
}
}
@end