forked from yuyuyu101/C-Buffered-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffered_tree.h
73 lines (59 loc) · 2.03 KB
/
buffered_tree.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
#ifndef WHEAT_BUFFERED_TREE
#define WHEAT_BUFFERED_TREE
#include <stdint.h>
// Normally, L2 Cache line size is 128/256 bytes. 16 containers can
// occupy 128 bytes
#define BFTREE_DEFAULT_CONTAINER 16
#define BFTREE_CONTAINER_THRESHOLD 16
#define BFTREE_PAYLOAD_THRESHOLD 100
#define BF_OK 0
#define BF_WRONG (-1)
struct bftree;
typedef int (*key_compare_func)(const void *key1, const void *key2);
enum payload_type {
Put,
Del,
};
struct payload {
uint8_t *key;
uint8_t *val;
struct payload *next;
enum payload_type type;
};
struct bftree_opts {
void *(*key_dup)(const void *key);
void *(*val_dup)(const void *obj);
key_compare_func key_compare;
void (*key_destructor)(void *key);
void (*val_destructor)(void *obj);
};
struct bftree *bftree_create(struct bftree_opts *opts);
void bftree_free(struct bftree *tree);
int bftree_put(struct bftree *tree, void *key, void *val);
void *bftree_get(struct bftree *tree, void *key);
int bftree_del(struct bftree *tree, void *key);
// Ineffective count implementation!!!! Don't call it if big tree
int bftree_count(struct bftree *tree);
// ================== Iterator Area ========================
// Iterator isn't effective as insert or delete operator, it may
// slower than normal dictionary implementation.
// If modify payload when iterate buffer tree, you should call
// bftree_iter_set_val and bftree_iter_set_del to avoid unexpected
// things
struct bftree_iterator *bftree_get_iterator(struct bftree *tree);
struct payload *bftree_next(struct bftree_iterator *iter);
void bftree_free_iterator(struct bftree_iterator *iter);
#define payload_getkey(payload) ((payload)->key)
#define payload_getval(payload) ((payload)->val)
static inline void bftree_iter_set_del(struct bftree_iterator *iter,
struct payload *payload)
{
payload->type = Del;
}
static inline void bftree_iter_set_val(struct bftree_iterator *iter,
struct payload *payload,
void *val)
{
payload->val = val;
}
#endif