-
Notifications
You must be signed in to change notification settings - Fork 1
/
unqlite_ht.c
90 lines (61 loc) · 1.8 KB
/
unqlite_ht.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
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
/* vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
#include <stdlib.h>
#include <stdint.h>
#include "unqlite.h"
#include "ht.h"
#include "MKPlugin.h"
struct node_t {
void *data;
int len;
};
struct table_t {
unqlite *db;
};
struct table_t * table_alloc() {
struct table_t *table = malloc(sizeof(struct table_t));
int rc = unqlite_open(&table->db, ":mem:",UNQLITE_OPEN_CREATE);
mk_bug(rc != UNQLITE_OK );
return table;
}
void table_free(struct table_t *table) {
unqlite_close(table->db);
free(table);
}
void *table_each(struct table_t *table, table_cb_t cb, void *state) {
struct node_t node;
unqlite_kv_cursor *cursor; /* Cursor handle */
mk_bug(unqlite_kv_cursor_init(table->db, &cursor) != UNQLITE_OK);
unqlite_kv_cursor_first_entry(cursor);
int len = 1024;
char key[len];
while( unqlite_kv_cursor_valid_entry(cursor) ){
unqlite_int64 data_len = sizeof(node);
unqlite_kv_cursor_key(cursor, key, &len);
unqlite_kv_cursor_data(cursor, &node, &data_len);
unqlite_kv_cursor_next_entry(cursor);
state = cb(key, node.data, state);
}
unqlite_kv_cursor_release(table->db, cursor);
return state;
}
void * table_get(struct table_t *table, const char *key) {
struct node_t node;
unqlite_int64 data_len = sizeof(node);
if (unqlite_kv_fetch(table->db, key, -1, &node, &data_len) != UNQLITE_OK) {
return NULL;
}
return node.data;
}
int table_add(struct table_t *table, const char *key, void *val) {
struct node_t node;
node.data = val;
if (unqlite_kv_store(table->db, key, -1, &node, sizeof(node)) != UNQLITE_OK) {
return -1;
}
return 0;
}
void *table_del(struct table_t *table, const char *key) {
void *data = table_get(table, key);
unqlite_kv_delete(table->db, key,-1);
return data;
}