-
Notifications
You must be signed in to change notification settings - Fork 90
/
LevelDB.h
341 lines (255 loc) · 11.3 KB
/
LevelDB.h
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
//
// LevelDB.h
//
// Copyright 2011 Pave Labs.
// See LICENCE for details.
//
#import <Foundation/Foundation.h>
@class LDBSnapshot;
@class LDBWritebatch;
typedef struct LevelDBOptions {
BOOL createIfMissing ;
BOOL createIntermediateDirectories;
BOOL errorIfExists ;
BOOL paranoidCheck ;
BOOL compression ;
int filterPolicy ;
size_t cacheSize;
} LevelDBOptions;
typedef struct {
const char * data;
NSUInteger length;
} LevelDBKey;
typedef NSData * (^LevelDBEncoderBlock) (LevelDBKey * key, id object);
typedef id (^LevelDBDecoderBlock) (LevelDBKey * key, id data);
typedef void (^LevelDBKeyBlock) (LevelDBKey * key, BOOL *stop);
typedef void (^LevelDBKeyValueBlock)(LevelDBKey * key, id value, BOOL *stop);
typedef id (^LevelDBValueGetterBlock) (void);
typedef void (^LevelDBLazyKeyValueBlock) (LevelDBKey * key, LevelDBValueGetterBlock lazyValue, BOOL *stop);
FOUNDATION_EXPORT NSString * const kLevelDBChangeType;
FOUNDATION_EXPORT NSString * const kLevelDBChangeTypePut;
FOUNDATION_EXPORT NSString * const kLevelDBChangeTypeDelete;
FOUNDATION_EXPORT NSString * const kLevelDBChangeValue;
FOUNDATION_EXPORT NSString * const kLevelDBChangeKey;
#ifdef __cplusplus
extern "C" {
#endif
NSString * NSStringFromLevelDBKey(LevelDBKey * key);
NSData * NSDataFromLevelDBKey (LevelDBKey * key);
#ifdef __cplusplus
}
#endif
@interface LevelDB : NSObject
///------------------------------------------------------------------------
/// @name A LevelDB object, used to query to the database instance on disk
///------------------------------------------------------------------------
/**
The path of the database on disk
*/
@property (nonatomic, copy) NSString *path;
/**
The name of the database.
*/
@property (nonatomic, copy) NSString *name;
/**
A boolean value indicating whether write operations should be synchronous (flush to disk before returning).
*/
@property (nonatomic) BOOL safe;
/**
A boolean value indicating whether read operations should try to use the configured cache (defaults to true).
*/
@property (nonatomic) BOOL useCache;
/**
A boolean readonly value indicating whether the database is closed or not.
*/
@property (readonly) BOOL closed;
/**
The data encoding block.
*/
@property (nonatomic, copy) LevelDBEncoderBlock encoder;
/**
The data decoding block.
*/
@property (nonatomic, copy) LevelDBDecoderBlock decoder;
/**
A class method that returns a LevelDBOptions struct, which can be modified to finetune leveldb
*/
+ (LevelDBOptions) makeOptions;
/**
A class method that returns an autoreleased instance of LevelDB with the given name, inside the Library folder
@param name The database's filename
*/
+ (id) databaseInLibraryWithName:(NSString *)name;
/**
A class method that returns an autoreleased instance of LevelDB with the given name and options, inside the Library folder
@param name The database's filename
@param opts A LevelDBOptions struct with options for fine tuning leveldb
*/
+ (id) databaseInLibraryWithName:(NSString *)name andOptions:(LevelDBOptions)opts;
/**
Initialize a leveldb instance
@param path The parent directory of the database file on disk
@param name the filename of the database file on disk
*/
- (id) initWithPath:(NSString *)path andName:(NSString *)name;
/**
Initialize a leveldb instance
@param path The parent directory of the database file on disk
@param name the filename of the database file on disk
@param opts A LevelDBOptions struct with options for fine tuning leveldb
*/
- (id) initWithPath:(NSString *)path name:(NSString *)name andOptions:(LevelDBOptions)opts;
/**
Delete the database file on disk
*/
- (void) deleteDatabaseFromDisk;
/**
Close the database.
@warning The instance cannot be used to perform any query after it has been closed.
*/
- (void) close;
#pragma mark - Setters
/**
Set the value associated with a key in the database
The instance's encoder block will be used to produce a NSData instance from the provided value.
@param value The value to put in the database
@param key The key at which the value can be found
*/
- (void) setObject:(id)value forKey:(id)key;
/**
Same as `[self setObject:forKey:]`
*/
- (void) setObject:(id)value forKeyedSubscript:(id)key;
/**
Same as `[self setObject:forKey:]`
*/
- (void) setValue:(id)value forKey:(NSString *)key ;
/**
Take all key-value pairs in the provided dictionary and insert them in the database
@param dictionary A dictionary from which key-value pairs will be inserted
*/
- (void) addEntriesFromDictionary:(NSDictionary *)dictionary;
#pragma mark - Write batches
/**
Return an retained LDBWritebatch instance for this database
*/
- (LDBWritebatch *) newWritebatch;
/**
Apply the operations from a writebatch into the current database
*/
- (void) applyWritebatch:(LDBWritebatch *)writeBatch;
/**
Create new writebatch, apply the operations in block from a writebatch into the current database
*/
- (void) performWritebatch:(void (^)(LDBWritebatch *wb))block;
#pragma mark - Getters
/**
Return the value associated with a key
@param key The key to retrieve from the database
*/
- (id) objectForKey:(id)key;
/**
Same as `[self objectForKey:]`
*/
- (id) objectForKeyedSubscript:(id)key;
/**
Same as `[self objectForKey:]`
*/
- (id) valueForKey:(NSString *)key;
/**
Return an array containing the values associated with the provided list of keys.
For keys that can't be found in the database, the `marker` value is used in place.
@warning marker should not be `nil`
@param keys The list of keys to fetch from the database
@param marker The value to associate to missing keys
*/
- (id) objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker;
/**
Return a boolean value indicating whether or not the key exists in the database
@param key The key to check for existence
*/
- (BOOL) objectExistsForKey:(id)key;
#pragma mark - Removers
/**
Remove a key (and its associated value) from the database
@param key The key to remove from the database
*/
- (void) removeObjectForKey:(id)key;
/**
Remove a set of keys (and their associated values) from the database
@param keyArray An array of keys to remove from the database
*/
- (void) removeObjectsForKeys:(NSArray *)keyArray;
/**
Remove all objects from the database
*/
- (void) removeAllObjects;
/**
Remove all objects prefixed with a given value (`NSString` or `NSData`)
@param prefix The key prefix used to remove all matching keys (of type `NSString` or `NSData`)
*/
- (void) removeAllObjectsWithPrefix:(id)prefix;
#pragma mark - Selection
/**
Return an array containing all the keys of the database
@warning This shouldn't be used with very large databases, since every key will be stored in memory
*/
- (NSArray *) allKeys;
/**
Return an array of key for which the value match the given predicate
@param predicate A `NSPredicate` instance tested against the database's values to retrieve the corresponding keys
*/
- (NSArray *) keysByFilteringWithPredicate:(NSPredicate *)predicate;
/**
Return a dictionary with all key-value pairs, where values match the given predicate
@param predicate A `NSPredicate` instance tested against the database's values to retrieve the corresponding key-value pairs
*/
- (NSDictionary *) dictionaryByFilteringWithPredicate:(NSPredicate *)predicate;
/**
Return an retained LDBSnapshot instance for this database
LDBSnapshots are a way to "freeze" the state of the database. Write operation applied to the database after the
snapshot was taken do not affect the snapshot. Most *read* methods available in the LevelDB class are also
available in the LDBSnapshot class.
*/
- (LDBSnapshot *) newSnapshot;
#pragma mark - Enumeration
/**
Enumerate over the keys in the database, in order.
Same as `[self enumerateKeysBackward:FALSE startingAtKey:nil filteredByPredicate:nil andPrefix:nil usingBlock:block]`
@param block The enumeration block used when iterating over all the keys.
*/
- (void) enumerateKeysUsingBlock:(LevelDBKeyBlock)block;
/**
Enumerate over the keys in the database, in direct or backward order, with some options to control the keys iterated over
@param backward A boolean value indicating whether the enumeration happens in direct or backward order
@param key (optional) The key at which to start iteration. If the key isn't present in the database, the enumeration starts at the key immediately greater than the provided one. The key can be a `NSData` or `NSString`
@param predicate A `NSPredicate` instance tested against the values. The iteration block will only be called for keys associated to values matching the predicate. If `nil`, this is ignored.
@param prefix A `NSString` or `NSData` prefix used to filter the keys. If provided, only the keys prefixed with this value will be iterated over.
@param block The enumeration block used when iterating over all the keys. It takes two arguments: the first is a pointer to a `LevelDBKey` struct. You can convert this to a `NSString` or `NSData` instance, using `NSDataFromLevelDBKey(LevelDBKey *key)` and `NSStringFromLevelDBKey(LevelDBKey *key)` respectively. The second arguments to the block is a `BOOL *` that can be used to stop enumeration at any time (e.g. `*stop = TRUE;`).
*/
- (void) enumerateKeysBackward:(BOOL)backward
startingAtKey:(id)key
filteredByPredicate:(NSPredicate *)predicate
andPrefix:(id)prefix
usingBlock:(LevelDBKeyBlock)block;
/**
Enumerate over the key value pairs in the database, in order.
Same as `[self enumerateKeysAndObjectsBackward:FALSE startingAtKey:nil filteredByPredicate:nil andPrefix:nil usingBlock:block]`
@param block The enumeration block used when iterating over all the key value pairs.
*/
- (void) enumerateKeysAndObjectsUsingBlock:(LevelDBKeyValueBlock)block;
/**
Enumerate over the keys in the database, in direct or backward order, with some options to control the keys iterated over
@param backward A boolean value indicating whether the enumeration happens in direct or backward order
@param key (optional) The key at which to start iteration. If the key isn't present in the database, the enumeration starts at the key immediately greater than the provided one. The key can be a `NSData` or `NSString`
@param predicate A `NSPredicate` instance tested against the values. The iteration block will only be called for keys associated to values matching the predicate. If `nil`, this is ignored.
@param prefix A `NSString` or `NSData` prefix used to filter the keys. If provided, only the keys prefixed with this value will be iterated over.
@param block The enumeration block used when iterating over all the keys. It takes three arguments: the first is a pointer to a `LevelDBKey` struct. You can convert this to a `NSString` or `NSData` instance, using `NSDataFromLevelDBKey(LevelDBKey *key)` and `NSStringFromLevelDBKey(LevelDBKey *key)` respectively. The second argument is the value associated with the key. The third arguments to the block is a `BOOL *` that can be used to stop enumeration at any time (e.g. `*stop = TRUE;`).
*/
- (void) enumerateKeysAndObjectsBackward:(BOOL)backward
lazily:(BOOL)lazily
startingAtKey:(id)key
filteredByPredicate:(NSPredicate *)predicate
andPrefix:(id)prefix
usingBlock:(id)block;
@end