1 #include "iwkv.h"
2 #include <string.h>
3 #include <stdlib.h>
4
main()5 int main() {
6 IWKV_OPTS opts = {
7 .path = "example1.db",
8 .oflags = IWKV_TRUNC // Cleanup database before open
9 };
10 IWKV iwkv;
11 IWDB mydb;
12 iwrc rc = iwkv_open(&opts, &iwkv);
13 if (rc) {
14 iwlog_ecode_error3(rc);
15 return 1;
16 }
17 // Now open mydb
18 // - Database id: 1
19 // - Using key/value as char data
20 rc = iwkv_db(iwkv, 1, 0, &mydb);
21 if (rc) {
22 iwlog_ecode_error2(rc, "Failed to open mydb");
23 return 1;
24 }
25 // Work with db: put/get value
26 IWKV_val key, val;
27 key.data = "foo";
28 key.size = strlen(key.data);
29 val.data = "bar";
30 val.size = strlen(val.data);
31
32 fprintf(stdout, "put: %.*s => %.*s\n",
33 (int) key.size, (char *) key.data,
34 (int) val.size, (char *) val.data);
35
36 rc = iwkv_put(mydb, &key, &val, 0);
37 if (rc) {
38 iwlog_ecode_error3(rc);
39 return rc;
40 }
41 // Retrive value associated with `foo` key
42 val.data = 0;
43 val.size = 0;
44 rc = iwkv_get(mydb, &key, &val);
45 if (rc) {
46 iwlog_ecode_error3(rc);
47 return rc;
48 }
49
50 fprintf(stdout, "get: %.*s => %.*s\n",
51 (int) key.size, (char *) key.data,
52 (int) val.size, (char *) val.data);
53
54 iwkv_val_dispose(&val);
55 iwkv_close(&iwkv);
56 return 0;
57 }
58