nmdb: Add LevelDB support
[nmdb.git] / nmdb / cache.h
blob9d70c005d85723d5327bb51f6a7eca71c714f6ed
2 #ifndef _CACHE_H
3 #define _CACHE_H
5 /* Generic cache layer. See cache.c for more information. */
7 #include <sys/types.h> /* for size_t */
8 #include <stdint.h> /* for int64_t */
11 #define CHAINLEN 4
13 struct cache {
14 /* set directly by initialization */
15 size_t numobjs;
16 unsigned int flags;
18 /* calculated */
19 size_t hashlen;
21 /* the cache data itself */
22 struct cache_chain *table;
25 struct cache_entry {
26 unsigned char *key;
27 unsigned char *val;
28 size_t ksize;
29 size_t vsize;
31 struct cache_entry *prev;
32 struct cache_entry *next;
35 struct cache_chain {
36 size_t len;
38 /* the entries live inside the chain, to avoid allocation costs and
39 * make it more cache-friendly */
40 struct cache_entry entries[CHAINLEN];
42 /* these point to elements of the entries array */
43 struct cache_entry *first;
44 struct cache_entry *last;
48 struct cache *cache_create(size_t numobjs, unsigned int flags);
49 int cache_free(struct cache *cd);
50 int cache_get(struct cache *cd, const unsigned char *key, size_t ksize,
51 unsigned char **val, size_t *vsize);
52 int cache_set(struct cache *cd, const unsigned char *k, size_t ksize,
53 const unsigned char *v, size_t vsize);
54 int cache_del(struct cache *cd, const unsigned char *key, size_t ksize);
55 int cache_cas(struct cache *cd, const unsigned char *key, size_t ksize,
56 const unsigned char *oldval, size_t ovsize,
57 const unsigned char *newval, size_t nvsize);
58 int cache_incr(struct cache *cd, const unsigned char *key, size_t ksize,
59 int64_t increment, int64_t *newval);
61 #endif