NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lmdb.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <lmdb.h>
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdint.h>
37#include <unistd.h>
38#include "mutt/lib.h"
39#include "lib.h"
40
43#if (UINTPTR_MAX == 0xffffffff)
45static const size_t LMDB_DB_SIZE = 2147483648;
46#elif (UINTPTR_MAX == 0xffffffffffffffff)
48static const size_t LMDB_DB_SIZE = 107374182400;
49#else
50#error
51#endif
52
57{
61};
62
67{
68 MDB_env *env;
69 MDB_txn *txn;
70 MDB_dbi db;
72};
73
78static void lmdb_sdata_free(struct LmdbStoreData **ptr)
79{
80 FREE(ptr);
81}
82
87static struct LmdbStoreData *lmdb_sdata_new(void)
88{
89 return mutt_mem_calloc(1, sizeof(struct LmdbStoreData));
90}
91
97static int lmdb_get_read_txn(struct LmdbStoreData *sdata)
98{
99 int rc;
100
101 if (sdata->txn && ((sdata->txn_mode == TXN_READ) || (sdata->txn_mode == TXN_WRITE)))
102 return MDB_SUCCESS;
103
104 if (sdata->txn)
105 rc = mdb_txn_renew(sdata->txn);
106 else
107 rc = mdb_txn_begin(sdata->env, NULL, MDB_RDONLY, &sdata->txn);
108
109 if (rc == MDB_SUCCESS)
110 {
111 sdata->txn_mode = TXN_READ;
112 }
113 else
114 {
115 mutt_debug(LL_DEBUG2, "%s: %s\n",
116 sdata->txn ? "mdb_txn_renew" : "mdb_txn_begin", mdb_strerror(rc));
117 }
118
119 return rc;
120}
121
127static int lmdb_get_write_txn(struct LmdbStoreData *sdata)
128{
129 if (sdata->txn)
130 {
131 if (sdata->txn_mode == TXN_WRITE)
132 return MDB_SUCCESS;
133
134 /* Free up the memory for readonly or reset transactions */
135 mdb_txn_abort(sdata->txn);
136 }
137
138 int rc = mdb_txn_begin(sdata->env, NULL, 0, &sdata->txn);
139 if (rc == MDB_SUCCESS)
140 sdata->txn_mode = TXN_WRITE;
141 else
142 mutt_debug(LL_DEBUG2, "mdb_txn_begin: %s\n", mdb_strerror(rc));
143
144 return rc;
145}
146
150static StoreHandle *store_lmdb_open(const char *path, bool create)
151{
152 if (!path)
153 return NULL;
154
155 if (!create && access(path, F_OK) != 0)
156 {
157 return NULL;
158 }
159
160 struct LmdbStoreData *sdata = lmdb_sdata_new();
161
162 int rc = mdb_env_create(&sdata->env);
163 if (rc != MDB_SUCCESS)
164 {
165 mutt_debug(LL_DEBUG2, "mdb_env_create: %s\n", mdb_strerror(rc));
166 lmdb_sdata_free(&sdata);
167 return NULL;
168 }
169
170 mdb_env_set_mapsize(sdata->env, LMDB_DB_SIZE);
171
172 rc = mdb_env_open(sdata->env, path, MDB_NOSUBDIR, 0644);
173 if (rc != MDB_SUCCESS)
174 {
175 mutt_debug(LL_DEBUG2, "mdb_env_open: %s\n", mdb_strerror(rc));
176 goto fail_env;
177 }
178
179 rc = lmdb_get_read_txn(sdata);
180 if (rc != MDB_SUCCESS)
181 {
182 mutt_debug(LL_DEBUG2, "mdb_txn_begin: %s\n", mdb_strerror(rc));
183 goto fail_env;
184 }
185
186 rc = mdb_dbi_open(sdata->txn, NULL, MDB_CREATE, &sdata->db);
187 if (rc != MDB_SUCCESS)
188 {
189 mutt_debug(LL_DEBUG2, "mdb_dbi_open: %s\n", mdb_strerror(rc));
190 goto fail_dbi;
191 }
192
193 mdb_txn_reset(sdata->txn);
195 // Return an opaque pointer
196 return (StoreHandle *) sdata;
197
198fail_dbi:
199 mdb_txn_abort(sdata->txn);
201 sdata->txn = NULL;
202
203fail_env:
204 mdb_env_close(sdata->env);
205 lmdb_sdata_free(&sdata);
206 return NULL;
207}
208
212static void *store_lmdb_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
213{
214 if (!store)
215 return NULL;
216
217 MDB_val dkey = { 0 };
218 MDB_val data = { 0 };
219
220 // Decloak an opaque pointer
221 struct LmdbStoreData *sdata = store;
222
223 dkey.mv_data = (void *) key;
224 dkey.mv_size = klen;
225 data.mv_data = NULL;
226 data.mv_size = 0;
227 int rc = lmdb_get_read_txn(sdata);
228 if (rc != MDB_SUCCESS)
229 {
230 sdata->txn = NULL;
231 mutt_debug(LL_DEBUG2, "txn_renew: %s\n", mdb_strerror(rc));
232 return NULL;
233 }
234 rc = mdb_get(sdata->txn, sdata->db, &dkey, &data);
235 if (rc == MDB_NOTFOUND)
236 {
237 return NULL;
238 }
239 if (rc != MDB_SUCCESS)
240 {
241 mutt_debug(LL_DEBUG2, "mdb_get: %s\n", mdb_strerror(rc));
242 return NULL;
243 }
244
245 *vlen = data.mv_size;
246 return data.mv_data;
247}
248
252static void store_lmdb_free(StoreHandle *store, void **ptr)
253{
254 /* LMDB data is owned by the database */
255}
256
260static int store_lmdb_store(StoreHandle *store, const char *key, size_t klen,
261 void *value, size_t vlen)
262{
263 if (!store)
264 return -1;
265
266 MDB_val dkey = { 0 };
267 MDB_val databuf = { 0 };
268
269 // Decloak an opaque pointer
270 struct LmdbStoreData *sdata = store;
271
272 dkey.mv_data = (void *) key;
273 dkey.mv_size = klen;
274 databuf.mv_data = value;
275 databuf.mv_size = vlen;
276 int rc = lmdb_get_write_txn(sdata);
277 if (rc != MDB_SUCCESS)
278 {
279 mutt_debug(LL_DEBUG2, "lmdb_get_write_txn: %s\n", mdb_strerror(rc));
280 return rc;
281 }
282 rc = mdb_put(sdata->txn, sdata->db, &dkey, &databuf, 0);
283 if (rc != MDB_SUCCESS)
284 {
285 mutt_debug(LL_DEBUG2, "mdb_put: %s\n", mdb_strerror(rc));
286 mdb_txn_abort(sdata->txn);
288 sdata->txn = NULL;
289 }
290 return rc;
291}
292
296static int store_lmdb_delete_record(StoreHandle *store, const char *key, size_t klen)
297{
298 if (!store)
299 return -1;
300
301 MDB_val dkey = { 0 };
302
303 // Decloak an opaque pointer
304 struct LmdbStoreData *sdata = store;
305
306 dkey.mv_data = (void *) key;
307 dkey.mv_size = klen;
308 int rc = lmdb_get_write_txn(sdata);
309 if (rc != MDB_SUCCESS)
310 {
311 mutt_debug(LL_DEBUG2, "lmdb_get_write_txn: %s\n", mdb_strerror(rc));
312 return rc;
313 }
314 rc = mdb_del(sdata->txn, sdata->db, &dkey, NULL);
315 if ((rc != MDB_SUCCESS) && (rc != MDB_NOTFOUND))
316 {
317 mutt_debug(LL_DEBUG2, "mdb_del: %s\n", mdb_strerror(rc));
318 mdb_txn_abort(sdata->txn);
320 sdata->txn = NULL;
321 }
322
323 return rc;
324}
325
330{
331 if (!ptr || !*ptr)
332 return;
333
334 // Decloak an opaque pointer
335 struct LmdbStoreData *sdata = *ptr;
336
337 if (sdata->txn)
338 {
339 if (sdata->txn_mode == TXN_WRITE)
340 mdb_txn_commit(sdata->txn);
341 else
342 mdb_txn_abort(sdata->txn);
343
345 sdata->txn = NULL;
346 }
347
348 mdb_env_close(sdata->env);
349 lmdb_sdata_free((struct LmdbStoreData **) ptr);
350}
351
355static const char *store_lmdb_version(void)
356{
357 return "lmdb " MDB_VERSION_STRING;
358}
359
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static void store_lmdb_close(StoreHandle **ptr)
Close a Store connection - Implements StoreOps::close() -.
Definition: lmdb.c:329
static int store_lmdb_delete_record(StoreHandle *store, const char *key, size_t klen)
Delete a record from the Store - Implements StoreOps::delete_record() -.
Definition: lmdb.c:296
static void * store_lmdb_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
Fetch a Value from the Store - Implements StoreOps::fetch() -.
Definition: lmdb.c:212
static void store_lmdb_free(StoreHandle *store, void **ptr)
Free a Value returned by fetch() - Implements StoreOps::free() -.
Definition: lmdb.c:252
static StoreHandle * store_lmdb_open(const char *path, bool create)
Open a connection to a Store - Implements StoreOps::open() -.
Definition: lmdb.c:150
static int store_lmdb_store(StoreHandle *store, const char *key, size_t klen, void *value, size_t vlen)
Write a Value to the Store - Implements StoreOps::store() -.
Definition: lmdb.c:260
static const char * store_lmdb_version(void)
Get a Store version string - Implements StoreOps::version() -.
Definition: lmdb.c:355
static struct LmdbStoreData * lmdb_sdata_new(void)
Create new Lmdb Store Data.
Definition: lmdb.c:87
static int lmdb_get_write_txn(struct LmdbStoreData *sdata)
Get an LMDB write transaction.
Definition: lmdb.c:127
static void lmdb_sdata_free(struct LmdbStoreData **ptr)
Free Lmdb Store Data.
Definition: lmdb.c:78
static int lmdb_get_read_txn(struct LmdbStoreData *sdata)
Get an LMDB read transaction.
Definition: lmdb.c:97
LmdbTxnMode
The maximum size of the database file (2GiB).
Definition: lmdb.c:57
@ TXN_READ
Read transaction in progress.
Definition: lmdb.c:59
@ TXN_WRITE
Write transaction in progress.
Definition: lmdb.c:60
@ TXN_UNINITIALIZED
Transaction is uninitialised.
Definition: lmdb.c:58
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
Key value store.
void StoreHandle
Opaque type for store backend.
Definition: lib.h:61
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:163
LMDB store.
Definition: lmdb.c:67
MDB_txn * txn
Definition: lmdb.c:69
MDB_env * env
Definition: lmdb.c:68
MDB_dbi db
Definition: lmdb.c:70
enum LmdbTxnMode txn_mode
Definition: lmdb.c:71