NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
rocksdb.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <rocksdb/c.h>
33#include <stdbool.h>
34#include <stddef.h>
35#include "mutt/lib.h"
36#include "lib.h"
37
42{
43 rocksdb_t *db;
44 rocksdb_options_t *options;
45 rocksdb_readoptions_t *read_options;
46 rocksdb_writeoptions_t *write_options;
47 char *err;
48};
49
54static void rocksdb_sdata_free(struct RocksDbStoreData **ptr)
55{
56 if (!ptr || !*ptr)
57 return;
58
59 struct RocksDbStoreData *sdata = *ptr;
60 FREE(&sdata->err);
61
62 FREE(ptr);
63}
64
70{
71 return MUTT_MEM_CALLOC(1, struct RocksDbStoreData);
72}
73
77static StoreHandle *store_rocksdb_open(const char *path, bool create)
78{
79 if (!path)
80 return NULL;
81
82 struct RocksDbStoreData *sdata = rocksdb_sdata_new();
83
84 /* RocksDB stores errors in form of strings */
85 sdata->err = NULL;
86
87 /* setup generic options, create new db and limit log to one file */
88 sdata->options = rocksdb_options_create();
89 if (create)
90 {
91 rocksdb_options_set_create_if_missing(sdata->options, 1);
92 }
93 rocksdb_options_set_keep_log_file_num(sdata->options, 1);
94
95 /* setup read options, we verify with checksums */
96 sdata->read_options = rocksdb_readoptions_create();
97 rocksdb_readoptions_set_verify_checksums(sdata->read_options, 1);
98
99 /* setup write options, no sync needed, disable WAL */
100 sdata->write_options = rocksdb_writeoptions_create();
101 rocksdb_writeoptions_set_sync(sdata->write_options, 0);
102 rocksdb_writeoptions_disable_WAL(sdata->write_options, 1);
103
104 rocksdb_options_set_compression(sdata->options, rocksdb_no_compression);
105
106 /* open database and check for error in sdata->error */
107 sdata->db = rocksdb_open(sdata->options, path, &sdata->err);
108 if (sdata->err)
109 {
110 rocksdb_options_destroy(sdata->options);
111 rocksdb_readoptions_destroy(sdata->read_options);
112 rocksdb_writeoptions_destroy(sdata->write_options);
113 rocksdb_sdata_free(&sdata);
114 return NULL;
115 }
116
117 // Return an opaque pointer
118 return (StoreHandle *) sdata;
119}
120
124static void *store_rocksdb_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
125{
126 if (!store)
127 return NULL;
128
129 // Decloak an opaque pointer
130 struct RocksDbStoreData *sdata = store;
131
132 void *rv = rocksdb_get(sdata->db, sdata->read_options, key, klen, vlen, &sdata->err);
133 if (sdata->err)
134 {
135 rocksdb_free(sdata->err);
136 sdata->err = NULL;
137 return NULL;
138 }
139
140 return rv;
141}
142
146static void store_rocksdb_free(StoreHandle *store, void **ptr)
147{
148 FREE(ptr);
149}
150
154static int store_rocksdb_store(StoreHandle *store, const char *key, size_t klen,
155 void *value, size_t vlen)
156{
157 if (!store)
158 return -1;
159
160 // Decloak an opaque pointer
161 struct RocksDbStoreData *sdata = store;
162
163 rocksdb_put(sdata->db, sdata->write_options, key, klen, value, vlen, &sdata->err);
164 if (sdata->err)
165 {
166 rocksdb_free(sdata->err);
167 sdata->err = NULL;
168 return -1;
169 }
170
171 return 0;
172}
173
177static int store_rocksdb_delete_record(StoreHandle *store, const char *key, size_t klen)
178{
179 if (!store)
180 return -1;
181
182 // Decloak an opaque pointer
183 struct RocksDbStoreData *sdata = store;
184
185 rocksdb_delete(sdata->db, sdata->write_options, key, klen, &sdata->err);
186 if (sdata->err)
187 {
188 rocksdb_free(sdata->err);
189 sdata->err = NULL;
190 return -1;
191 }
192
193 return 0;
194}
195
200{
201 if (!ptr || !*ptr)
202 return;
203
204 // Decloak an opaque pointer
205 struct RocksDbStoreData *sdata = *ptr;
206
207 /* close database and free resources */
208 rocksdb_close(sdata->db);
209 rocksdb_options_destroy(sdata->options);
210 rocksdb_readoptions_destroy(sdata->read_options);
211 rocksdb_writeoptions_destroy(sdata->write_options);
212
213 rocksdb_sdata_free((struct RocksDbStoreData **) ptr);
214}
215
219static const char *store_rocksdb_version(void)
220{
221/* return sth. like "RocksDB 6.7.3" */
222#define RDBVER(major, minor, patch) #major "." #minor "." #patch
223 return "RocksDB " RDBVER(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH);
224}
225
226STORE_BACKEND_OPS(rocksdb)
static void store_rocksdb_close(StoreHandle **ptr)
Close a Store connection - Implements StoreOps::close() -.
Definition: rocksdb.c:199
static int store_rocksdb_delete_record(StoreHandle *store, const char *key, size_t klen)
Delete a record from the Store - Implements StoreOps::delete_record() -.
Definition: rocksdb.c:177
static void * store_rocksdb_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
Fetch a Value from the Store - Implements StoreOps::fetch() -.
Definition: rocksdb.c:124
static void store_rocksdb_free(StoreHandle *store, void **ptr)
Free a Value returned by fetch() - Implements StoreOps::free() -.
Definition: rocksdb.c:146
static StoreHandle * store_rocksdb_open(const char *path, bool create)
Open a connection to a Store - Implements StoreOps::open() -.
Definition: rocksdb.c:77
static int store_rocksdb_store(StoreHandle *store, const char *key, size_t klen, void *value, size_t vlen)
Write a Value to the Store - Implements StoreOps::store() -.
Definition: rocksdb.c:154
static const char * store_rocksdb_version(void)
Get a Store version string - Implements StoreOps::version() -.
Definition: rocksdb.c:219
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Convenience wrapper for the library headers.
static struct RocksDbStoreData * rocksdb_sdata_new(void)
Create new RocksDb Store Data.
Definition: rocksdb.c:69
#define RDBVER(major, minor, patch)
static void rocksdb_sdata_free(struct RocksDbStoreData **ptr)
Free RocksDb Store Data.
Definition: rocksdb.c:54
Key value store.
void StoreHandle
Opaque type for store backend.
Definition: lib.h:61
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:163
RocksDB store.
Definition: rocksdb.c:42
rocksdb_options_t * options
Definition: rocksdb.c:44
rocksdb_t * db
Definition: rocksdb.c:43
rocksdb_readoptions_t * read_options
Definition: rocksdb.c:45
rocksdb_writeoptions_t * write_options
Definition: rocksdb.c:46
char * err
Definition: rocksdb.c:47