NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
kc.c
Go to the documentation of this file.
1 
33 #include "config.h"
34 #include <kclangc.h>
35 #include <stdio.h>
36 #include "mutt/lib.h"
37 #include "lib.h"
38 
42 static void *store_kyotocabinet_open(const char *path)
43 {
44  if (!path)
45  return NULL;
46 
47  KCDB *db = kcdbnew();
48  if (!db)
49  return NULL;
50 
51  struct Buffer kcdbpath = mutt_buffer_make(1024);
52 
53  mutt_buffer_printf(&kcdbpath, "%s#type=kct#opts=l#rcomp=lex", path);
54 
55  if (!kcdbopen(db, mutt_buffer_string(&kcdbpath), KCOWRITER | KCOCREATE))
56  {
57  int ecode = kcdbecode(db);
58  mutt_debug(LL_DEBUG2, "kcdbopen failed for %s: %s (ecode %d)\n",
59  mutt_buffer_string(&kcdbpath), kcdbemsg(db), ecode);
60  kcdbdel(db);
61  db = NULL;
62  }
63 
64  mutt_buffer_dealloc(&kcdbpath);
65  return db;
66 }
67 
71 static void *store_kyotocabinet_fetch(void *store, const char *key, size_t klen, size_t *vlen)
72 {
73  if (!store)
74  return NULL;
75 
76  KCDB *db = store;
77  return kcdbget(db, key, klen, vlen);
78 }
79 
83 static void store_kyotocabinet_free(void *store, void **ptr)
84 {
85  if (!ptr || !*ptr)
86  return;
87 
88  kcfree(*ptr);
89  *ptr = NULL;
90 }
91 
95 static int store_kyotocabinet_store(void *store, const char *key, size_t klen,
96  void *value, size_t vlen)
97 {
98  if (!store)
99  return -1;
100 
101  KCDB *db = store;
102  if (!kcdbset(db, key, klen, value, vlen))
103  {
104  int ecode = kcdbecode(db);
105  return ecode ? ecode : -1;
106  }
107  return 0;
108 }
109 
113 static int store_kyotocabinet_delete_record(void *store, const char *key, size_t klen)
114 {
115  if (!store)
116  return -1;
117 
118  KCDB *db = store;
119  if (!kcdbremove(db, key, klen))
120  {
121  int ecode = kcdbecode(db);
122  return ecode ? ecode : -1;
123  }
124  return 0;
125 }
126 
130 static void store_kyotocabinet_close(void **ptr)
131 {
132  if (!ptr || !*ptr)
133  return;
134 
135  KCDB *db = *ptr;
136  if (!kcdbclose(db))
137  {
138  int ecode = kcdbecode(db);
139  mutt_debug(LL_DEBUG2, "kcdbclose failed: %s (ecode %d)\n", kcdbemsg(db), ecode);
140  }
141  kcdbdel(db);
142  *ptr = NULL;
143 }
144 
148 static const char *store_kyotocabinet_version(void)
149 {
150  static char version_cache[128] = { 0 };
151  if (version_cache[0] == '\0')
152  snprintf(version_cache, sizeof(version_cache), "kyotocabinet %s", KCVERSION);
153 
154  return version_cache;
155 }
156 
157 STORE_BACKEND_OPS(kyotocabinet)
static void * store_kyotocabinet_open(const char *path)
Implements StoreOps::open() -.
Definition: kc.c:42
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:157
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
static void * store_kyotocabinet_fetch(void *store, const char *key, size_t klen, size_t *vlen)
Implements StoreOps::fetch() -.
Definition: kc.c:71
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
Log at debug level 2.
Definition: logging.h:41
static void store_kyotocabinet_close(void **ptr)
Implements StoreOps::close() -.
Definition: kc.c:130
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
static int store_kyotocabinet_delete_record(void *store, const char *key, size_t klen)
Implements StoreOps::delete_record() -.
Definition: kc.c:113
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Convenience wrapper for the library headers.
static const char * store_kyotocabinet_version(void)
Implements StoreOps::version() -.
Definition: kc.c:148
static void store_kyotocabinet_free(void *store, void **ptr)
Implements StoreOps::free() -.
Definition: kc.c:83
static int store_kyotocabinet_store(void *store, const char *key, size_t klen, void *value, size_t vlen)
Implements StoreOps::store() -.
Definition: kc.c:95