NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
kc.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <kclangc.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include "mutt/lib.h"
36#include "lib.h"
37
41static StoreHandle *store_kyotocabinet_open(const char *path, bool create)
42{
43 if (!path)
44 return NULL;
45
46 KCDB *db = kcdbnew();
47 if (!db)
48 return NULL;
49
50 struct Buffer *kcdbpath = buf_pool_get();
51
52 buf_printf(kcdbpath, "%s#type=kct#opts=l#rcomp=lex", path);
53
54 if (!kcdbopen(db, buf_string(kcdbpath), KCOWRITER | (create ? KCOCREATE : 0)))
55 {
56 int ecode = kcdbecode(db);
57 mutt_debug(LL_DEBUG2, "kcdbopen failed for %s: %s (ecode %d)\n",
58 buf_string(kcdbpath), kcdbemsg(db), ecode);
59 kcdbdel(db);
60 db = NULL;
61 }
62
63 buf_pool_release(&kcdbpath);
64 // Return an opaque pointer
65 return (StoreHandle *) db;
66}
67
71static void *store_kyotocabinet_fetch(StoreHandle *store, const char *key,
72 size_t klen, size_t *vlen)
73{
74 if (!store)
75 return NULL;
76
77 // Decloak an opaque pointer
78 KCDB *db = store;
79 return kcdbget(db, key, klen, vlen);
80}
81
85static void store_kyotocabinet_free(StoreHandle *store, void **ptr)
86{
87 if (!ptr || !*ptr)
88 return;
89
90 kcfree(*ptr);
91 *ptr = NULL;
92}
93
97static int store_kyotocabinet_store(StoreHandle *store, const char *key,
98 size_t klen, void *value, size_t vlen)
99{
100 if (!store)
101 return -1;
102
103 // Decloak an opaque pointer
104 KCDB *db = store;
105 if (!kcdbset(db, key, klen, value, vlen))
106 {
107 int ecode = kcdbecode(db);
108 return ecode ? ecode : -1;
109 }
110 return 0;
111}
112
116static int store_kyotocabinet_delete_record(StoreHandle *store, const char *key, size_t klen)
117{
118 if (!store)
119 return -1;
120
121 // Decloak an opaque pointer
122 KCDB *db = store;
123 if (!kcdbremove(db, key, klen))
124 {
125 int ecode = kcdbecode(db);
126 return ecode ? ecode : -1;
127 }
128 return 0;
129}
130
135{
136 if (!ptr || !*ptr)
137 return;
138
139 // Decloak an opaque pointer
140 KCDB *db = *ptr;
141 if (!kcdbclose(db))
142 {
143 int ecode = kcdbecode(db);
144 mutt_debug(LL_DEBUG2, "kcdbclose failed: %s (ecode %d)\n", kcdbemsg(db), ecode);
145 }
146 kcdbdel(db);
147 *ptr = NULL;
148}
149
153static const char *store_kyotocabinet_version(void)
154{
155 static char version_cache[128] = { 0 };
156 if (version_cache[0] == '\0')
157 snprintf(version_cache, sizeof(version_cache), "kyotocabinet %s", KCVERSION);
158
159 return version_cache;
160}
161
162STORE_BACKEND_OPS(kyotocabinet)
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static void store_kyotocabinet_close(StoreHandle **ptr)
Close a Store connection - Implements StoreOps::close() -.
Definition: kc.c:134
static int store_kyotocabinet_delete_record(StoreHandle *store, const char *key, size_t klen)
Delete a record from the Store - Implements StoreOps::delete_record() -.
Definition: kc.c:116
static void * store_kyotocabinet_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
Fetch a Value from the Store - Implements StoreOps::fetch() -.
Definition: kc.c:71
static void store_kyotocabinet_free(StoreHandle *store, void **ptr)
Free a Value returned by fetch() - Implements StoreOps::free() -.
Definition: kc.c:85
static StoreHandle * store_kyotocabinet_open(const char *path, bool create)
Open a connection to a Store - Implements StoreOps::open() -.
Definition: kc.c:41
static int store_kyotocabinet_store(StoreHandle *store, const char *key, size_t klen, void *value, size_t vlen)
Write a Value to the Store - Implements StoreOps::store() -.
Definition: kc.c:97
static const char * store_kyotocabinet_version(void)
Get a Store version string - Implements StoreOps::version() -.
Definition: kc.c:153
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
Convenience wrapper for the library headers.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
Key value store.
void StoreHandle
Opaque type for store backend.
Definition: lib.h:61
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:163
String manipulation buffer.
Definition: buffer.h:36