Skip to content

Commit f585af6

Browse files
committed
Update Readme for version 0.4.0
- Swift support - RocksDB Env & Thread Status - Prefix Seek - Column Family Metadata
1 parent fd013fb commit f585af6

File tree

1 file changed

+108
-6
lines changed

1 file changed

+108
-6
lines changed

README.md

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ ObjectiveRocks provides an easy interface to RocksDB and an Objective-C friendly
1010

1111
If you are interested in the internals of RocksDB, please refer to the [RocksDB Wiki](https://github.com/facebook/rocksdb/wiki).
1212

13+
## Swift
14+
15+
ObjectiveRocks has a pure Objective-C interface and can be used in Swift projects. Additionally, all ObjectiveRocks tests are also ported to Swift. You can check the `Tests` targets and source code which also contain bridging headers for OSX and iOS ObjectiveRocks implementations.
16+
1317
## The Minimum You Need to Know
1418

1519
* Keys and values are byte arrays
@@ -45,6 +49,7 @@ These features are only available in OSX:
4549
* Database Backups
4650
* Database Statistics
4751
* Database Properties
52+
* Thread Status
4853

4954
## Installation
5055

@@ -355,29 +360,107 @@ or use one of the provided enumeration-blocks:
355360
356361
RocksDBIterator *iterator = [db iterator];
357362
363+
/* Keys Enumeration */
358364
[db enumerateKeysUsingBlock:^(id key, BOOL *stop) {
359-
NSLog(@"%@: %@", key, [db objectForKey:key]);
365+
NSLog(@"%@", key);
360366
// A, B, C, D
361367
}];
362368
363369
// reverse enumeration
364370
[db enumerateKeysInReverse:YES usingBlock:^(id key, BOOL *stop) {
365-
NSLog(@"%@: %@", key, [db objectForKey:key]);
371+
NSLog(@"%@", key);
366372
// D, C, B, A
367373
}];
368374
369375
// Enumeration in a given key-range [start, end)
370-
RocksDBIteratorKeyRange range = RocksDBMakeKeyRange(@"C", @"A");
376+
RocksDBIteratorKeyRange range = RocksDBMakeKeyRange(@"A", @"C");
377+
378+
[db enumerateKeysInRange:range reverse:NO usingBlock:^(id key, BOOL *stop) {
379+
NSLog(@"%@", key, [db objectForKey:key]);
380+
// B, C
381+
}];
371382
372-
[db enumerateKeysInRange:range reverse:YES usingBlock:^(id key, BOOL *stop) {
383+
/* Key-Value Enumeration */
384+
[db enumerateKeysAndValuesUsingBlock:^(id key, id value BOOL *stop) {
385+
NSLog(@"%@:%@", key, value);
386+
// A:1, B:2, C:3, D:4
387+
}];
388+
389+
[db enumerateKeysAndValuesInReverse:YES usingBlock:^(id key, BOOL *stop) {
373390
NSLog(@"%@: %@", key, [db objectForKey:key]);
374-
// C, B
391+
// D:4, C:3, B:2, A:1
392+
}];
393+
394+
// Enumeration in a given key-range [start, end)
395+
RocksDBIteratorKeyRange range = RocksDBMakeKeyRange(@"A", @"C");
396+
397+
[db enumerateKeysAndValuesInRange:range reverse:YES usingBlock:^(id key, BOOL *stop) {
398+
NSLog(@"%@:%@", key, [db objectForKey:key]);
399+
// B:2, C:3
375400
}];
376401
```
377402

378403
## Prefix-Seek Iteration
379404

380-
TBD
405+
`RocksDBIterator` supports iterating inside a key-prefix by providing a `RocksDBPrefixExtractor`. One such extractor is built-in and it extracts a fixed-length prefix for each key:
406+
407+
```objective-c
408+
RocksDB *db = [[RocksDB alloc] initWithPath:_path andDBOptions:^(RocksDBOptions *options) {
409+
options.createIfMissing = YES;
410+
options.prefixExtractor = [RocksDBPrefixExtractor prefixExtractorWithType:RocksDBPrefixFixedLength length:2];
411+
412+
options.keyType = RocksDBTypeNSString;
413+
options.valueType = RocksDBTypeNSString;
414+
}];
415+
416+
[db setObject:@"a" forKey:@"10.1"];
417+
[db setObject:@"b" forKey:@"10.2"];
418+
[db setObject:@"b" forKey:@"10.3"];
419+
[db setObject:@"c" forKey:@"11.1"];
420+
[db setObject:@"d" forKey:@"11.2"];
421+
[db setObject:@"d" forKey:@"11.3"];
422+
423+
RocksDBIterator *iterator = [db iterator];
424+
425+
// Enumeration starts with the key that is Greater-Than-Or-Equal to a key
426+
// with the given "prefix" parameter
427+
[iterator enumerateKeysWithPrefix:@"10" usingBlock:^(id key, BOOL *stop) {
428+
NSLog(@"%@", key);
429+
// 10.1, 10.2, 10.3
430+
}];
431+
432+
// .. so in this case the enumeration starts at key "10.2", even if "10.1"
433+
// has the same prefix
434+
[iterator enumerateKeysWithPrefix:@"10.2" usingBlock:^(id key, BOOL *stop) {
435+
NSLog(@"%@", key);
436+
// 10.2, 10.3
437+
}];
438+
```
439+
440+
You can also define your own Prefix Extractor:
441+
442+
```objective-c
443+
RocksDBPrefixExtractor *extractor = [[RocksDBPrefixExtractor alloc] initWithName:@"custom_prefix"
444+
transformBlock:^id (id key) {
445+
// Apply your key transformation to extract the prefix part
446+
id prefix = extractPrefixFromKey(key);
447+
return prefix;
448+
}
449+
prefixCandidateBlock:^BOOL (id key) {
450+
// You can filter out keys that are not viable candidates for
451+
// your custom prefix format, e.g. key length is smaller than
452+
// the target prefix length
453+
BOOL isCandidate = canExtractPrefixFromKey(key);
454+
return isCandidate;
455+
}
456+
validPrefixBlock:^BOOL (id prefix) {
457+
// After a prefix is extracted you can perform extra
458+
// checks here to verify that the prefix is valid
459+
BOOL isValid = isExtractedPrefixValid(prefix);
460+
return isValid;
461+
}
462+
];
463+
```
381464

382465
## Snapshot
383466

@@ -561,6 +644,25 @@ result = @{@"Key 1" : @"Value 1 New",
561644
*/
562645
```
563646

647+
## Env & Thread Status
648+
649+
The `RocksDBEnv` allows for modifying the thread pool for backgrond jobs. RocksDB uses this thread pool for compactions and memtable flushes.
650+
651+
```objective-c
652+
RocksDBEnv *dbEnv = [RocksDBEnv envWithLowPriorityThreadCount:12 andHighPriorityThreadCount:4];
653+
RocksDB *db = [[RocksDB alloc] initWithPath:@"path/to/db" andDBOptions:^(RocksDBOptions *options) {
654+
options.env = dbEnv;
655+
}];
656+
657+
// To get a list of all threads
658+
NSArray *threads = dbEnv.threadList;
659+
660+
// "threads" array contains objects of type RocksDBThreadStatus
661+
RocksDBThreadStatus *status = threads[0];
662+
```
663+
664+
> Thread Status API is currently a WIP.
665+
564666
## Backup & Restore
565667
566668
To backup a database use the `RocksDBBackupEngine`:

0 commit comments

Comments
 (0)