NeoMutt  2024-03-23-23-gec7045
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
tdb.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stddef.h>
33#include <fcntl.h>
34#include <tdb.h>
35#include "mutt/lib.h"
36#include "lib.h"
37
41static StoreHandle *store_tdb_open(const char *path)
42{
43 if (!path)
44 return NULL;
45
46 /* TDB_NOLOCK - Don't do any locking
47 * TDB_NOSYNC - Don't use synchronous transactions
48 * TDB_INCOMPATIBLE_HASH - Better hashing
49 */
50 const int flags = TDB_NOLOCK | TDB_INCOMPATIBLE_HASH | TDB_NOSYNC;
51 const int hash_size = 33533; // Based on test timings for 100K emails
52
53 struct tdb_context *db = tdb_open(path, hash_size, flags, O_CREAT | O_RDWR, 00600);
54
55 // Return an opaque pointer
56 return (StoreHandle *) db;
57}
58
62static void *store_tdb_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
63{
64 if (!store)
65 return NULL;
66
67 // Decloak an opaque pointer
68 TDB_CONTEXT *db = store;
69 TDB_DATA dkey;
70 TDB_DATA data;
71
72 dkey.dptr = (unsigned char *) key;
73 dkey.dsize = klen;
74 data = tdb_fetch(db, dkey);
75
76 *vlen = data.dsize;
77 return data.dptr;
78}
79
83static void store_tdb_free(StoreHandle *store, void **ptr)
84{
85 FREE(ptr);
86}
87
91static int store_tdb_store(StoreHandle *store, const char *key, size_t klen,
92 void *value, size_t vlen)
93{
94 if (!store)
95 return -1;
96
97 // Decloak an opaque pointer
98 TDB_CONTEXT *db = store;
99 TDB_DATA dkey;
100 TDB_DATA databuf;
101
102 dkey.dptr = (unsigned char *) key;
103 dkey.dsize = klen;
104
105 databuf.dsize = vlen;
106 databuf.dptr = value;
107
108 return tdb_store(db, dkey, databuf, TDB_INSERT);
109}
110
114static int store_tdb_delete_record(StoreHandle *store, const char *key, size_t klen)
115{
116 if (!store)
117 return -1;
118
119 // Decloak an opaque pointer
120 TDB_CONTEXT *db = store;
121 TDB_DATA dkey;
122
123 dkey.dptr = (unsigned char *) key;
124 dkey.dsize = klen;
125
126 return tdb_delete(db, dkey);
127}
128
132static void store_tdb_close(StoreHandle **ptr)
133{
134 if (!ptr || !*ptr)
135 return;
136
137 // Decloak an opaque pointer
138 TDB_CONTEXT *db = *ptr;
139 tdb_close(db);
140 *ptr = NULL;
141}
142
146static const char *store_tdb_version(void)
147{
148 // TDB doesn't supply any version info
149 return "tdb";
150}
151
static void store_tdb_close(StoreHandle **ptr)
Close a Store connection - Implements StoreOps::close() -.
Definition: tdb.c:132
static int store_tdb_delete_record(StoreHandle *store, const char *key, size_t klen)
Delete a record from the Store - Implements StoreOps::delete_record() -.
Definition: tdb.c:114
static void * store_tdb_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
Fetch a Value from the Store - Implements StoreOps::fetch() -.
Definition: tdb.c:62
static void store_tdb_free(StoreHandle *store, void **ptr)
Free a Value returned by fetch() - Implements StoreOps::free() -.
Definition: tdb.c:83
static StoreHandle * store_tdb_open(const char *path)
Open a connection to a Store - Implements StoreOps::open() -.
Definition: tdb.c:41
static int store_tdb_store(StoreHandle *store, const char *key, size_t klen, void *value, size_t vlen)
Write a Value to the Store - Implements StoreOps::store() -.
Definition: tdb.c:91
static const char * store_tdb_version(void)
Get a Store version string - Implements StoreOps::version() -.
Definition: tdb.c:146
#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:162