NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
rocksdb.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stddef.h>
32#include <rocksdb/c.h>
33#include "mutt/lib.h"
34#include "lib.h"
35
40{
41 rocksdb_t *db;
42 rocksdb_options_t *options;
43 rocksdb_readoptions_t *read_options;
44 rocksdb_writeoptions_t *write_options;
45 char *err;
46};
47
51static void *store_rocksdb_open(const char *path)
52{
53 if (!path)
54 return NULL;
55
56 struct RocksDbCtx *ctx = mutt_mem_malloc(sizeof(struct RocksDbCtx));
57
58 /* RocksDB store errors in form of strings */
59 ctx->err = NULL;
60
61 /* setup generic options, create new db and limit log to one file */
62 ctx->options = rocksdb_options_create();
63 rocksdb_options_set_create_if_missing(ctx->options, 1);
64 rocksdb_options_set_keep_log_file_num(ctx->options, 1);
65
66 /* setup read options, we verify with checksums */
67 ctx->read_options = rocksdb_readoptions_create();
68 rocksdb_readoptions_set_verify_checksums(ctx->read_options, 1);
69
70 /* setup write options, no sync needed, disable WAL */
71 ctx->write_options = rocksdb_writeoptions_create();
72 rocksdb_writeoptions_set_sync(ctx->write_options, 0);
73 rocksdb_writeoptions_disable_WAL(ctx->write_options, 1);
74
75 rocksdb_options_set_compression(ctx->options, rocksdb_no_compression);
76
77 /* open database and check for error in ctx->error */
78 ctx->db = rocksdb_open(ctx->options, path, &ctx->err);
79 if (ctx->err)
80 {
81 rocksdb_free(ctx->err);
82 FREE(&ctx);
83 return NULL;
84 }
85
86 return ctx;
87}
88
92static void *store_rocksdb_fetch(void *store, const char *key, size_t klen, size_t *vlen)
93{
94 if (!store)
95 return NULL;
96
97 struct RocksDbCtx *ctx = store;
98
99 void *rv = rocksdb_get(ctx->db, ctx->read_options, key, klen, vlen, &ctx->err);
100 if (ctx->err)
101 {
102 rocksdb_free(ctx->err);
103 ctx->err = NULL;
104 return NULL;
105 }
106
107 return rv;
108}
109
113static void store_rocksdb_free(void *store, void **ptr)
114{
115 FREE(ptr);
116}
117
121static int store_rocksdb_store(void *store, const char *key, size_t klen,
122 void *value, size_t vlen)
123{
124 if (!store)
125 return -1;
126
127 struct RocksDbCtx *ctx = store;
128
129 rocksdb_put(ctx->db, ctx->write_options, key, klen, value, vlen, &ctx->err);
130 if (ctx->err)
131 {
132 rocksdb_free(ctx->err);
133 ctx->err = NULL;
134 return -1;
135 }
136
137 return 0;
138}
139
143static int store_rocksdb_delete_record(void *store, const char *key, size_t klen)
144{
145 if (!store)
146 return -1;
147
148 struct RocksDbCtx *ctx = store;
149
150 rocksdb_delete(ctx->db, ctx->write_options, key, klen, &ctx->err);
151 if (ctx->err)
152 {
153 rocksdb_free(ctx->err);
154 ctx->err = NULL;
155 return -1;
156 }
157
158 return 0;
159}
160
164static void store_rocksdb_close(void **ptr)
165{
166 if (!ptr || !*ptr)
167 return;
168
169 struct RocksDbCtx *ctx = *ptr;
170
171 /* close database and free resources */
172 rocksdb_close(ctx->db);
173 rocksdb_options_destroy(ctx->options);
174 rocksdb_readoptions_destroy(ctx->read_options);
175 rocksdb_writeoptions_destroy(ctx->write_options);
176
177 FREE(ptr);
178 *ptr = NULL;
179}
180
184static const char *store_rocksdb_version(void)
185{
186/* return sth. like "RocksDB 6.7.3" */
187#define RDBVER(major, minor, patch) #major "." #minor "." #patch
188 return "RocksDB " RDBVER(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH);
189}
190
191STORE_BACKEND_OPS(rocksdb)
static void store_rocksdb_close(void **ptr)
Implements StoreOps::close() -.
Definition: rocksdb.c:164
static int store_rocksdb_delete_record(void *store, const char *key, size_t klen)
Implements StoreOps::delete_record() -.
Definition: rocksdb.c:143
static void * store_rocksdb_fetch(void *store, const char *key, size_t klen, size_t *vlen)
Implements StoreOps::fetch() -.
Definition: rocksdb.c:92
static void store_rocksdb_free(void *store, void **ptr)
Implements StoreOps::free() -.
Definition: rocksdb.c:113
static void * store_rocksdb_open(const char *path)
Implements StoreOps::open() -.
Definition: rocksdb.c:51
static int store_rocksdb_store(void *store, const char *key, size_t klen, void *value, size_t vlen)
Implements StoreOps::store() -.
Definition: rocksdb.c:121
static const char * store_rocksdb_version(void)
Implements StoreOps::version() -.
Definition: rocksdb.c:184
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define RDBVER(major, minor, patch)
Key value store.
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:159
Berkeley DB context.
Definition: rocksdb.c:40
rocksdb_writeoptions_t * write_options
Definition: rocksdb.c:44
rocksdb_readoptions_t * read_options
Definition: rocksdb.c:43
rocksdb_t * db
Definition: rocksdb.c:41
char * err
Definition: rocksdb.c:45
rocksdb_options_t * options
Definition: rocksdb.c:42