This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOObjectGraphContext.m
More file actions
412 lines (342 loc) · 10.3 KB
/
COObjectGraphContext.m
File metadata and controls
412 lines (342 loc) · 10.3 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#import "COObjectGraphContext.h"
#import "COEditingContextPrivate.h"
#import "COItemGraph.h"
#import <EtoileFoundation/Macros.h>
#import "CORelationshipCache.h"
/**
* COEditingContext semantics:
*
* A mutable view on a set of COItem objects which materializes the relationships
* as actual ObjC object references.
*
* The main feature is that the underlying objects can be changed arbitrairly and
* the view will update accordingly.
*
* The COItems are garbage collected. See discussion in COItemTree.h.
* The main motivation is to avoid having to store semantically redundant
* "explicit delete" operations in diffs.
*
* Garbage collection shouldn't need to be invoked by the user. It happens
* at commit time, and when loading a new object graph.
*
* TODO: Fit in change notifications
*
* Behaviours:
* - record which objects were edited/inserted
* - maintain consistency of composite relationship, for edits
* made through the COObject api (but not through addItem: api)
* - maintain relationship cache, for all edits
* - post notifications
*/
@implementation COObjectGraphContext
#pragma mark Creation
- (id) initWithSchemaRegistry: (COSchemaRegistry *)aRegistry
{
SUPERINIT;
objectsByUUID_ = [[NSMutableDictionary alloc] init];
insertedObjects_ = [[NSMutableSet alloc] init];
modifiedObjects_ = [[NSMutableSet alloc] init];
schemaRegistry_ = [aRegistry retain];
relationshipCache_ = [[CORelationshipCache alloc] init];
return self;
}
- (id) init
{
return [self initWithSchemaRegistry: nil];
}
+ (COObjectGraphContext *) editingContext
{
return [[[self alloc] init] autorelease];
}
+ (COObjectGraphContext *) editingContextWithSchemaRegistry: (COSchemaRegistry *)aRegistry
{
return [[[self alloc] initWithSchemaRegistry: aRegistry] autorelease];
}
- (void) dealloc
{
[objectsByUUID_ release];
[rootObjectUUID_ release];
[insertedObjects_ release];
[modifiedObjects_ release];
[schemaRegistry_ release];
[relationshipCache_ release];
[super dealloc];
}
#pragma mark Schema
- (COSchemaRegistry *) schemaRegistry
{
return schemaRegistry_;
}
#pragma mark begin COItemGraph protocol
- (ETUUID *) rootItemUUID
{
return rootObjectUUID_;
}
/**
* Returns immutable item
*/
- (COItem *) itemForUUID: (ETUUID *)aUUID
{
COObject *object = [objectsByUUID_ objectForKey: aUUID];
if (nil != object)
{
return [object serializedItem];
}
return nil;
}
- (NSArray *) itemUUIDs
{
return [objectsByUUID_ allKeys];
}
/**
* Insert or update an item.
*/
- (void) addItem: (COItem *)item markAsInserted: (BOOL)markInserted
{
NSParameterAssert(item != nil);
ETUUID *uuid = [item UUID];
COObject *currentObject = [objectsByUUID_ objectForKey: uuid];
if (currentObject == nil)
{
currentObject = [[[COObject alloc] initWithItem: item
parentContext: self] autorelease];
[objectsByUUID_ setObject: currentObject forKey: uuid];
[relationshipCache_ addItem: item];
if (markInserted)
{
[insertedObjects_ addObject: uuid];
}
}
else
{
[relationshipCache_ removeItem: [currentObject seriailzedItem]];
[currentObject setItem: item];
[relationshipCache_ addItem: item];
[modifiedObjects_ addObject: uuid];
}
}
- (void) addItem: (COItem *)item
{
[self addItem: item markAsInserted: YES];
}
#pragma mark end COItemGraph protocol
/**
* Replaces the editing context.
*
* There are 3 kinds of change:
* - New objects are inserted
* - Removed objects are removed
* - Changed objects are updated. (sub-case: identical objects)
*/
- (void) setItemTree: (id <COItemGraph>)aTree
{
[self clearChangeTracking];
// 1. Do updates.
ASSIGN(rootObjectUUID_, [aTree rootItemUUID]);
for (ETUUID *uuid in [aTree itemUUIDs])
{
[self addItem: [aTree itemForUUID: uuid] markAsInserted: NO];
}
// 3. Do GC
[self gc_];
}
- (COObject *) rootObject
{
return [self objectForUUID: [self rootItemUUID]];
}
- (void) setRootObject: (COObject *)anObject
{
NSParameterAssert([anObject editingContext] == self);
ASSIGN(rootObjectUUID_, [anObject UUID]);
}
#pragma mark change tracking
/**
* Returns the set of objects inserted since change tracking was cleared
*/
- (NSSet *) insertedObjectUUIDs
{
return insertedObjects_;
}
/**
* Returns the set of objects modified since change tracking was cleared
*/
- (NSSet *) modifiedObjectUUIDs
{
return modifiedObjects_;
}
- (NSSet *) insertedOrModifiedObjectUUIDs
{
return [insertedObjects_ setByAddingObjectsFromSet: modifiedObjects_];
}
- (void) clearChangeTracking
{
[insertedObjects_ removeAllObjects];
[modifiedObjects_ removeAllObjects];
}
- (COObject *) insertObjectWithSchemaName: (NSString *)aSchemaName
{
COMutableItem *item = [COMutableItem item];
// FIXME: Decide between this and the schemaName_ ivar in COItem
if (aSchemaName != nil)
{
[item setValue: aSchemaName forAttribute: kCOSchemaName type: kCOStringType];
}
[self addItem: item];
return [objectsByUUID_ objectForKey: [item UUID]];
}
- (COObject *) insertObject
{
return [self insertObjectWithSchemaName: nil];
}
#pragma mark access
- (COObject *) objectForUUID: (ETUUID *)aUUID
{
return [objectsByUUID_ objectForKey: aUUID];
}
/**
* @return If there exists an object which has a [COType embeddedObjectType] reference to
* anObject, return that object. Otherwise return nil.
*/
- (COObject *) embeddedObjectParent: (COObject *)anObject
{
CORelationshipRecord *record = [relationshipCache_ parentForUUID: [anObject UUID]];
return [objectsByUUID_ objectForKey: record.uuid];
}
- (NSSet *) objectsWithReferencesToObject: (COObject*)anObject
inAttribute: (NSString*)anAttribute
{
NSSet *uuids = [relationshipCache_ referrersForUUID: [anObject UUID]
propertyInParent: anAttribute];
NSMutableSet *objs = [NSMutableSet setWithCapacity: [uuids count]];
for (ETUUID *uuid in uuids)
{
[objs addObject: [objectsByUUID_ objectForKey: uuid]];
}
return objs;
}
#pragma mark garbage collection
/**
* Call to update the view to reflect one object becoming unavailable.
*
* Preconditions:
* - No objects in the context should have composite relationsips
* to uuid.
*
* Postconditions:
* - objectForUUID: will return nil
* - the COObject previously held by the context will be turned into a "zombie"
* and the COEditingContext will release it, so it will be deallocated if
* no user code holds a reference to it.
*/
- (void) removeSingleObject_: (ETUUID *)uuid
{
COObject *anObject = [objectsByUUID_ objectForKey: uuid];
COItem *item = [anObject seriailzedItem];
// Update relationship cache
[relationshipCache_ removeItem: item];
// Update change tracking
[insertedObjects_ removeObject: uuid];
[modifiedObjects_ removeObject: uuid];
// Mark the object as a "zombie"
[anObject markAsRemovedFromContext];
// Release it from the objects dictionary (may release it)
[objectsByUUID_ removeObjectForKey: uuid];
anObject = nil;
}
- (void) gcDeadObjects: (NSSet *)dead
{
for (ETUUID *deadUUID in dead)
{
[self removeSingleObject_: deadUUID];
}
}
- (void) gcDfs_: (COObject *)anObject uuids: (NSMutableSet *)set
{
ETUUID *uuid = [anObject UUID];
if ([set containsObject: uuid])
{
return;
}
[set addObject: uuid];
// Call recursively on all composite and referenced objects
for (COObject *obj in [anObject embeddedOrReferencedObjects])
{
[self gcDfs_: obj uuids: set];
}
}
- (void) gc_
{
NSArray *allKeys = [objectsByUUID_ allKeys];
NSMutableSet *live = [NSMutableSet setWithCapacity: [allKeys count]];
[self gcDfs_: [self rootObject] uuids: live];
NSMutableSet *dead = [NSMutableSet setWithArray: allKeys];
[dead minusSet: live];
[self gcDeadObjects: dead];
}
#pragma mark copy, equality, hash
/**
* Returns a copy of the reciever, not including any change tracking
* information.
*/
- (id) copyWithZone: (NSZone *)aZone
{
COObjectGraphContext *aCopy = [COObjectGraphContext editingContextWithSchemaRegistry: schemaRegistry_];
[aCopy setItemTree: self];
return aCopy;
}
- (BOOL) isEqual:(id)object
{
if (object == self)
{
return YES;
}
if (![object isKindOfClass: [self class]])
{
return NO;
}
COObjectGraphContext *otherContext = (COObjectGraphContext *)object;
if (!((rootObjectUUID_ == nil && otherContext->rootObjectUUID_ == nil)
|| [rootObjectUUID_ isEqual: otherContext->rootObjectUUID_]))
{
return NO;
}
if (![[NSSet setWithArray: [self itemUUIDs]]
isEqual: [NSSet setWithArray: [otherContext itemUUIDs]]])
{
return NO;
}
for (ETUUID *aUUID in [self itemUUIDs])
{
COItem *selfItem = [[self objectForUUID: aUUID] seriailzedItem];
COItem *otherItem = [[otherContext objectForUUID: aUUID] seriailzedItem];
if (![selfItem isEqual: otherItem])
{
return NO;
}
}
return YES;
}
- (NSUInteger) hash
{
return [rootObjectUUID_ hash] ^ 13803254444065375360ULL;
}
// Relationship cache
- (void) updateRelationshipIntegrityWithOldValue: (id)oldVal
oldType: (COType)oldType
newValue: (id)newVal
newType: (COType)newType
forProperty: (NSString *)aProperty
ofObject: (ETUUID *)anObject
{
[relationshipCache_ updateRelationshipCacheWithOldValue: oldVal
oldType: oldType
newValue: newVal
newType: newType
forProperty: aProperty
ofObject: anObject];
if (![insertedObjects_ containsObject: anObject])
{
[modifiedObjects_ addObject: anObject];
}
}
@end