forked from emnl/leveldb-c-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leveldb_iterate.c
58 lines (47 loc) · 1.27 KB
/
leveldb_iterate.c
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
#include <leveldb/c.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char* argv[]){
leveldb_t *db;
leveldb_options_t *options;
leveldb_readoptions_t *roptions;
char *err = NULL;
char* db_path = argv[1];
if (db_path == NULL) {
printf("usage: %s [db-path]\n", argv[0]);
return 1;
}
options = leveldb_options_create();
roptions = leveldb_readoptions_create();
// open
db = leveldb_open(options, db_path, &err);
if (err != NULL) {
fprintf(stderr, "Open fail.\n");
return 1;
}
// iterate
leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions);
leveldb_iter_seek_to_first(iter);
size_t klen, vlen;
const char *k, *v;
for (int i=0; leveldb_iter_valid(iter) && i<100; i++) {
k = leveldb_iter_key(iter, &klen);
v = leveldb_iter_value(iter, &vlen);
// print hex bytes
// for (int j = 0; j < vlen; j++)
// {
// if (j > 0) printf(" ");
// printf("%02X", (unsigned char)v[j]);
// }
// printf("\n");
// print string value
printf("key: %.*s, len: %ld, strlen: %ld\n", (int)klen, k, klen, strlen(k));
printf("val: %.*s, len: %ld, strlen: %ld\n", (int)vlen, v, vlen, strlen(v));
leveldb_iter_next(iter);
}
// close
leveldb_close(db);
return 0;
}