NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
tdb.c
Go to the documentation of this file.
1 
30 #include "config.h"
31 #include <stddef.h>
32 #include <fcntl.h>
33 #include <tdb.h>
34 #include "mutt/lib.h"
35 #include "lib.h"
36 
40 static void *store_tdb_open(const char *path)
41 {
42  if (!path)
43  return NULL;
44 
45  /* TDB_NOLOCK - Don't do any locking
46  * TDB_NOSYNC - Don't use synchronous transactions
47  * TDB_INCOMPATIBLE_HASH - Better hashing
48  */
49  const int flags = TDB_NOLOCK | TDB_INCOMPATIBLE_HASH | TDB_NOSYNC;
50  const int hash_size = 33533; // Based on test timings for 100K emails
51  return tdb_open(path, hash_size, flags, O_CREAT | O_RDWR, 00600);
52 }
53 
57 static void *store_tdb_fetch(void *store, const char *key, size_t klen, size_t *vlen)
58 {
59  if (!store)
60  return NULL;
61 
62  TDB_CONTEXT *db = store;
63  TDB_DATA dkey;
64  TDB_DATA data;
65 
66  dkey.dptr = (unsigned char *) key;
67  dkey.dsize = klen;
68  data = tdb_fetch(db, dkey);
69 
70  *vlen = data.dsize;
71  return data.dptr;
72 }
73 
77 static void store_tdb_free(void *store, void **ptr)
78 {
79  FREE(ptr);
80 }
81 
85 static int store_tdb_store(void *store, const char *key, size_t klen, void *value, size_t vlen)
86 {
87  if (!store)
88  return -1;
89 
90  TDB_CONTEXT *db = store;
91  TDB_DATA dkey;
92  TDB_DATA databuf;
93 
94  dkey.dptr = (unsigned char *) key;
95  dkey.dsize = klen;
96 
97  databuf.dsize = vlen;
98  databuf.dptr = value;
99 
100  return tdb_store(db, dkey, databuf, TDB_INSERT);
101 }
102 
106 static int store_tdb_delete_record(void *store, const char *key, size_t klen)
107 {
108  if (!store)
109  return -1;
110 
111  TDB_CONTEXT *db = store;
112  TDB_DATA dkey;
113 
114  dkey.dptr = (unsigned char *) key;
115  dkey.dsize = klen;
116 
117  return tdb_delete(db, dkey);
118 }
119 
123 static void store_tdb_close(void **ptr)
124 {
125  if (!ptr || !*ptr)
126  return;
127 
128  TDB_CONTEXT *db = *ptr;
129  tdb_close(db);
130  *ptr = NULL;
131 }
132 
136 static const char *store_tdb_version(void)
137 {
138  // TDB doesn't supply any version info
139  return "tdb";
140 }
141 
static const char * store_tdb_version(void)
Implements StoreOps::version() -.
Definition: tdb.c:136
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:157
static void store_tdb_close(void **ptr)
Implements StoreOps::close() -.
Definition: tdb.c:123
int(* store)(void *store, const char *key, size_t klen, void *value, size_t vlen)
Definition: lib.h:119
static int store_tdb_store(void *store, const char *key, size_t klen, void *value, size_t vlen)
Implements StoreOps::store() -.
Definition: tdb.c:85
static void * store_tdb_open(const char *path)
Implements StoreOps::open() -.
Definition: tdb.c:40
static void * store_tdb_fetch(void *store, const char *key, size_t klen, size_t *vlen)
Implements StoreOps::fetch() -.
Definition: tdb.c:57
#define FREE(x)
Definition: memory.h:40
Convenience wrapper for the library headers.
static void store_tdb_free(void *store, void **ptr)
Implements StoreOps::free() -.
Definition: tdb.c:77
static int store_tdb_delete_record(void *store, const char *key, size_t klen)
Implements StoreOps::delete_record() -.
Definition: tdb.c:106