-
Notifications
You must be signed in to change notification settings - Fork 36
/
IGKFrecencyStore.m
197 lines (146 loc) · 4.52 KB
/
IGKFrecencyStore.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
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// IGKFrecencyStore.m
// Ingredients
//
// Created by Alex Gordon on 01/07/2010.
// Written in 2010 by Fileability.
//
#import "IGKFrecencyStore.h"
typedef struct {
int64_t timestamp;
NSString *item;
// IF YOU MODIFY THIS STRUCT YOU ******MUST****** INCREMENT IGKFrecencyStoreBufferRecordVersion !!!!!!1!1!!!!1!ONE!1!!ELEVENTY1
} IGKFrecencyStoreBufferRecord;
// vvvvv THAT'S THIS THING vvvvv
const int IGKFrecencyStoreBufferRecordVersion = 1;
// ^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^
@interface IGKFrecencyStore ()
- (NSString *)storeDirectory;
- (NSString *)storeExtension;
- (NSString *)path;
- (void)readFromDisk;
- (void)writeToDisk;
- (void)heartbeat;
@end
@implementation IGKFrecencyStore
#pragma mark Life Cycle
static NSMutableDictionary *stores = nil;
+ (id)storeWithIdentifier:(NSString *)ident
{
return nil;
if ([stores objectForKey:ident])
return [stores objectForKey:ident];
if (!stores)
stores = [[NSMutableDictionary alloc] initWithCapacity:5];
IGKFrecencyStore *store = [[[self alloc] initWithIdentifier:ident] autorelease];
[stores setValue:store forKey:ident];
return store;
}
- (id)initWithIdentifier:(NSString *)ident
{
if (self = [super init])
{
identifier = [ident copy];
[self readFromDisk];
[self heartbeat];
}
return self;
}
- (void)finalize
{
IGKCircularBufferFree(buffer);
[super finalize];
}
#pragma mark Recording and Reading
- (void)recordItem:(NSString *)item
{
int64_t timestamp = (int64_t)[NSDate timeIntervalSinceReferenceDate];
IGKFrecencyStoreBufferRecord record;
record.timestamp = timestamp;
record.item = item;
IGKCircularBufferAdd(buffer, &record);
hasChanges = YES;
}
- (NSArray *)timestampsForItem:(NSString *)item count:(uint64_t *)count
{
//If no variable to put count in is specified, they can't do anything but crash, so return NULL
if (!count)
return NULL;
CFIndex length = IGKCircularBufferRawDataLength(buffer);
IGKFrecencyStoreBufferRecord* data = (IGKFrecencyStoreBufferRecord*)IGKCircularBufferRawData(buffer);
NSMutableArray *timestamps = [[NSMutableArray alloc] initWithCapacity:100];
if (buffer.elementCount > 0)
{
for (CFIndex i = buffer.oldestElement; ; i = (i + 1) % buffer.elementCount)
{
if (data + i == NULL)
continue;
IGKFrecencyStoreBufferRecord record = data[i];
if ([record.item isEqual:item])
{
[timestamps addObject:[NSNumber numberWithLongLong:record.timestamp]];
}
if (i == buffer.youngestElement)
break;
}
}
return timestamps;
}
#pragma mark File IO
const CFIndex IGKFrecencyStoreMaximumCount = 1000;
const CFIndex IGKFrecencyStoreInitialCount = 100;
- (NSString *)storeDirectory
{
// <appsupport>/Frecency/<identifier>
NSString *appSupport = [[[NSApp delegate] kitController] applicationSupportDirectory];
return [appSupport stringByAppendingPathComponent:@"Frecency"];
}
- (NSString *)storeExtension
{
return [NSString stringWithFormat:@"igkfrecencystore%d", IGKFrecencyStoreBufferRecordVersion];
}
- (NSString *)path
{
return [[[self storeDirectory] stringByAppendingPathComponent:identifier] stringByAppendingPathExtension:[self storeExtension]];
}
- (void)readFromDisk
{
NSString *path = [self path];
NSError *err = nil;
NSData *data = [[NSData alloc] initWithContentsOfFile:path options:NSDataReadingUncached error:&err];
if (!data || err)
{
buffer = IGKCircularBufferCreate(IGKFrecencyStoreMaximumCount, sizeof(IGKFrecencyStoreBufferRecord), IGKFrecencyStoreInitialCount);
return;
}
//Read from data
buffer = IGKCircularBufferCreateFromData([data bytes], [data length], IGKFrecencyStoreMaximumCount, sizeof(IGKFrecencyStoreBufferRecord));
}
- (void)writeToDisk
{
NSLog(@"Writing IGKFrecencyStore %@ to disk. It has%@ changes", identifier, hasChanges ? @"" : @" no");
if (!hasChanges)
return;
hasChanges = NO;
NSString *path = [self path];
NSLog(@"\t path = '%@'", path);
//Create the directory
[[NSFileManager defaultManager] createDirectoryAtPath:[self storeDirectory] withIntermediateDirectories:YES attributes:nil error:nil];
//Get data from buffer
if (buffer.elementCount == 0)
return;
NSData *data = IGKCircularBufferOrderedData(buffer);
NSLog(@"\t data [%ud] %@", [data length], data);
//Write to disk
NSError *err = nil;
[data writeToFile:path options:NSDataWritingAtomic error:&err];
NSLog(@"\t err = %@", err);
}
- (void)heartbeat
{
NSLog(@"Heartbeat on IGKFrecencyStore %@", identifier);
[self writeToDisk];
//We want to do a write every 20 seconds
[self performSelector:@selector(heartbeat) withObject:nil afterDelay:5.0];
}
@end