-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLevelDB.mm
More file actions
180 lines (141 loc) · 4.65 KB
/
LevelDB.mm
File metadata and controls
180 lines (141 loc) · 4.65 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
//
// LevelDB.m
//
// Copyright 2011 Pave Labs. All rights reserved.
// See LICENCE for details.
//
#import "LevelDB.h"
#import <leveldb/db.h>
#import <leveldb/options.h>
#define SliceFromString(_string_) (Slice((char *)[_string_ UTF8String], [_string_ lengthOfBytesUsingEncoding:NSUTF8StringEncoding]))
#define StringFromSlice(_slice_) ([[[NSString alloc] initWithBytes:_slice_.data() length:_slice_.size() encoding:NSUTF8StringEncoding] autorelease])
using namespace leveldb;
static Slice SliceFromObject(id object) {
NSMutableData *d = [[[NSMutableData alloc] init] autorelease];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:d];
[archiver encodeObject:object forKey:@"object"];
[archiver finishEncoding];
[archiver release];
return Slice((const char *)[d bytes], (size_t)[d length]);
}
static id ObjectFromSlice(Slice v) {
NSData *data = [NSData dataWithBytes:v.data() length:v.size()];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id object = [[unarchiver decodeObjectForKey:@"object"] retain];
[unarchiver finishDecoding];
[unarchiver release];
return object;
}
@implementation LevelDB
@synthesize path=_path;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (id) initWithPath:(NSString *)path {
self = [super init];
if (self) {
_path = path;
Options options;
options.create_if_missing = true;
Status status = leveldb::DB::Open(options, [_path UTF8String], &db);
readOptions.fill_cache = false;
writeOptions.sync = false;
if(!status.ok()) {
NSLog(@"Problem creating LevelDB database: %s", status.ToString().c_str());
}
}
return self;
}
+ (NSString *)libraryPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
+ (LevelDB *)databaseInLibraryWithName:(NSString *)name {
NSString *path = [[LevelDB libraryPath] stringByAppendingPathComponent:name];
LevelDB *ldb = [[[LevelDB alloc] initWithPath:path] autorelease];
return ldb;
}
- (void) putObject:(id)value forKey:(NSString *)key {
Slice k = SliceFromString(key);
Slice v = SliceFromObject(value);
Status status = db->Put(writeOptions, k, v);
if(!status.ok()) {
NSLog(@"Problem storing key/value pair in database: %s", status.ToString().c_str());
}
}
- (id) getObject:(NSString *)key {
std::string v_string;
Slice k = SliceFromString(key);
Status status = db->Get(readOptions, k, &v_string);
if(!status.ok()) {
if(!status.IsNotFound())
NSLog(@"Problem retrieving value for key '%@' from database: %s", key, status.ToString().c_str());
return nil;
}
return ObjectFromSlice(v_string);
}
- (NSString *) getString:(NSString *)key {
return (NSString *)[self getObject:key];
}
- (NSDictionary *) getDictionary:(NSString *)key {
return (NSDictionary *)[self getObject:key];
}
- (NSArray *) getArray:(NSString *)key {
return (NSArray *)[self getObject:key];
}
- (void)deleteObject:(NSString *)key {
Slice k = SliceFromString(key);
Status status = db->Delete(writeOptions, k);
if(!status.ok()) {
NSLog(@"Problem deleting key/value pair in database: %s", status.ToString().c_str());
}
}
- (void) clear {
NSArray *keys = [self allKeys];
for (NSString *k in keys) {
[self deleteObject:k];
}
}
- (NSArray *)allKeys {
NSMutableArray *keys = [[[NSMutableArray alloc] init] autorelease];
//test iteration
[self iterateKeys:^BOOL(NSString *key) {
[keys addObject:key];
return TRUE;
}];
return keys;
}
- (void) iterate:(KeyValueBlock)block {
Iterator* iter = db->NewIterator(ReadOptions());
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
Slice key = iter->key(), value = iter->value();
NSString *k = StringFromSlice(key);
id v = ObjectFromSlice(value);
if (!block(k, v)) {
break;
}
}
delete iter;
}
- (void) iterateKeys:(KeyBlock)block {
Iterator* iter = db->NewIterator(ReadOptions());
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
Slice key = iter->key();
NSString *k = StringFromSlice(key);
if (!block(k)) {
break;
}
}
delete iter;
}
- (void) deleteDatabase {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:_path error:&error];
}
@end