/
vlmap.h
117 lines (95 loc) · 2.95 KB
/
vlmap.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifndef VLMAP_H
#define VLMAP_H
struct vlmap_node {
uint8_t* key;
int keylength;
uint8_t* value;
int valuelength;
uint64_t created;
uint64_t removed;
int level;
struct vlmap_node** next;
};
typedef struct vlmap_node vlnode_t;
typedef struct {
uint64_t version;
uint64_t oldest;
int levels;
vlnode_t** root;
} vlmap;
typedef struct {
uint64_t version;
vlnode_t* root;
uint8_t* startkey;
int startkeylen;
uint8_t* endkey;
int endkeylen;
} vlmap_iterator;
// vlmap_create returns a pointer to a new vlmap.
vlmap*
vlmap_create();
// vlmap_destroy destroys a vlmap and all of
// its nodes.
void
vlmap_destroy(vlmap* m);
// vlmap_insert inserts a key-value pair into the map at a certain version.
int
vlmap_insert(vlmap* m, uint64_t version, uint8_t* key, int keylength,
uint8_t* value, int valuelength);
// vlmap_remove logically removes a node from the map, if it matches
// the given key, at a certain version. Returns 0 on success.
int
vlmap_remove(vlmap* m, uint64_t version, uint8_t* key, int keylength);
// vlmap_get gets a key-value at a certain version.
// On success, 0 is returned and value and valuelength are filled
// with the respective data.
// **It's the caller's responsibility to free the value**.
// On error, value and valuelength will not be modified.
int
vlmap_get(vlmap* m, uint64_t version, uint8_t* key, int keylength,
uint8_t** value, int* valuelength);
// vlmap_version returns the current version of the map.
uint64_t
vlmap_version(vlmap* m);
// vlmap_version_increment increments the current version.
void
vlmap_version_increment(vlmap* m);
void
vlmap_print(vlmap* m, int version);
// vlmap_clean removes any node in the map
// older than version.
void
vlmap_clean(vlmap* m, uint64_t version);
// vlmap_iterator_create creates a new iterator,
// which must later be destroyed with vlmap_iterator_destroy,
// at a version. This allows for snapshot range reads.
//
// A start key and an end key are required, but one can
// use `\x00' and `\xff' as the start and end, respectively,
// to essentially read from the entire range.
vlmap_iterator*
vlmap_iterator_create(vlmap* m, uint64_t version,
uint8_t* startkey, int startkeylen,
uint8_t* endkey, int endkeylen);
// vlmap_iterator_destroy frees the space allocated
// to a vlmap_iterator.
void
vlmap_iterator_destroy(vlmap_iterator* i);
// vlmap_iterator_get_value works the same way as calling
// vlmap_get on the current node of the iterator.
// This returns 0 on success, and value must be freed by
// the caller.
int
vlmap_iterator_get_value(vlmap_iterator* i, uint8_t** value, int* valuelength);
// vlmap_iterator_next moves to the next node in the map.
// Map elements not in the current snapshot are ignored.
// Returns NULL when out of range, so it's important to
// store the original pointer to the iterator to free it
// using vlmap_iterator_destroy.
vlmap_iterator*
vlmap_iterator_next(vlmap_iterator* i);
#endif