This repository has been 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
/
COPersistentRoot.m
184 lines (152 loc) · 4.66 KB
/
COPersistentRoot.m
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
#import "COPersistentRoot.h"
#import "COStore.h"
#import "COBranch.h"
#import <EtoileFoundation/Macros.h>
#import "COPersistentRootPrivate.h"
#import "COSQLiteStore.h"
#import "COEditingContext.h"
@implementation COPersistentRoot (Private)
- (id)initWithStoreEditQueue: (COStore *)aRootStore
persistentRoot: (COPersistentRootState *)metadata
{
SUPERINIT;
rootStore_ = aRootStore;
savedState_ = [[COPersistentRootState alloc] initWithPersistentRootPlist: metadata];
branchForUUID_ = [[NSMutableDictionary alloc] init];
return self;
}
- (COSQLiteStore *) store
{
return [rootStore_ store];
}
- (COPersistentRootState *) savedState
{
return savedState_;
}
@end
@implementation COPersistentRoot
NSString *kCOPersistentRootName = @"COPersistentRootName";
- (void) dealloc
{
[savedState_ release];
[branchForUUID_ release];
[super dealloc];
}
- (COUUID *) UUID
{
return [savedState_ UUID];
}
// metadata & convenience
- (NSDictionary *) metadata
{
NSDictionary *result = [savedState_ metadata];
if (result == nil)
{
return [NSDictionary dictionary];
}
return result;
}
- (void) setMetadata: (NSDictionary *)theMetadata
{
BOOL ok = [[self store] setMetadata: theMetadata
forPersistentRoot: [self UUID]];
assert(ok);
// FIXME: Throw exception if not ok?
[savedState_ setMetadata: theMetadata];
}
- (NSString *)name
{
return [[self metadata] objectForKey: kCOPersistentRootName];
}
- (void) setName: (NSString *)aName
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary: [self metadata]];
[dict setObject: aName forKey: kCOPersistentRootName];
[self setMetadata: dict];
}
// branches
- (NSSet *) branches
{
NSMutableSet *result = [NSMutableSet set];
for (COUUID *uuid in [savedState_ branchUUIDs])
{
[result addObject: [self branchWithUUID: uuid]];
}
return [NSSet setWithSet: result];
}
- (NSArray *) revisionIDs
{
return [savedState_ revisionIDs];
}
- (COBranch *) createBranchAtRevision: (CORevisionID *)aRevision
setCurrent: (BOOL)setCurrent
{
COUUID *aBranch = [[self store] createBranchWithInitialRevision: aRevision
setCurrent: setCurrent
forPersistentRoot: [self UUID]];
// FIXME: Overly coarse
ASSIGN(savedState_, [[self store] persistentRootWithUUID: [self UUID]]);
// Update current branch edit queue
if (currentBranch_ != nil)
{
[currentBranch_ setBranch: aBranch];
}
return [self branchWithUUID: aBranch];
}
- (COBranch *) currentBranch
{
if (currentBranch_ == nil)
{
currentBranch_ = [[COBranch alloc] initWithPersistentRoot: self
branch: [savedState_ currentBranchUUID]
trackCurrentBranch: YES];
}
return currentBranch_;
}
- (void) setCurrentBranch: (COBranch *)aBranch
{
BOOL ok = [[self store] setCurrentBranch: [aBranch UUID]
forPersistentRoot: [self UUID]];
assert(ok);
// FIXME: Throw exception if not ok?
[savedState_ setCurrentBranchUUID: [aBranch UUID]];
if (currentBranch_ != nil)
{
[currentBranch_ setBranch: [aBranch UUID]];
}
}
- (void) removeBranch: (COBranch *)aBranch
{
NSParameterAssert(![[self currentBranch] isEqual: aBranch]);
BOOL ok = [[self store] deleteBranch: [aBranch UUID]
ofPersistentRoot: [self UUID]];
assert(ok);
// FIXME: Throw exception if not ok?
[savedState_ removeBranchForUUID: [aBranch UUID]];
}
- (COBranch *) branchWithUUID: (COUUID *)aUUID
{
COBranch *cached = [branchForUUID_ objectForKey: aUUID];
if (cached != nil)
{
return cached;
}
if ([[savedState_ branchUUIDs] containsObject: aUUID])
{
COBranch *branch = [[[COBranch alloc] initWithPersistentRoot: self
branch: aUUID
trackCurrentBranch: NO] autorelease];
[branchForUUID_ setObject: branch forKey: aUUID];
return branch;
}
// FIXME: not really an error, just for debugging
assert(0);
return nil;
}
- (COEditingContext *) readonlyContextForViewingRevision: (CORevisionID *)aRevision
{
COEditingContext *ctx = [[COEditingContext alloc] init];
[ctx setItemTree: [[self store] contentsForRevisionID: aRevision]];
return [ctx autorelease];
}
@end