NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches

Open a connection to a Store. More...

+ Collaboration diagram for open():

Functions

static StoreHandlestore_bdb_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_gdbm_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_kyotocabinet_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_lmdb_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_qdbm_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_rocksdb_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_tokyocabinet_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_tdb_open (const char *path, bool create)
 Open a connection to a Store - Implements StoreOps::open() -.
 

Detailed Description

Open a connection to a Store.

Parameters
[in]pathPath to the database file
[in]createCreate the file if it's not there?
Return values
ptrSuccess, Store pointer
NULLFailure

The open function has the purpose of opening a backend-specific connection to the database file specified by the path parameter. Backends MUST return non-NULL specific handle information on success.

Function Documentation

◆ store_bdb_open()

static StoreHandle * store_bdb_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 115 of file bdb.c.

116{
117 if (!path)
118 return NULL;
119
120 struct BdbStoreData *sdata = bdb_sdata_new();
121
122 const int pagesize = 512;
123
124 buf_printf(&sdata->lockfile, "%s-lock-hack", path);
125
126 sdata->fd = open(buf_string(&sdata->lockfile),
127 O_WRONLY | (create ? O_CREAT : 0), S_IRUSR | S_IWUSR);
128 if (sdata->fd < 0)
129 {
130 bdb_sdata_free(&sdata);
131 return NULL;
132 }
133
134 if (mutt_file_lock(sdata->fd, true, true))
135 goto fail_close;
136
137 int rc = db_env_create(&sdata->env, 0);
138 if (rc)
139 goto fail_unlock;
140
141 rc = (*sdata->env->open)(sdata->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
142 if (rc)
143 goto fail_env;
144
145 sdata->db = NULL;
146 rc = db_create(&sdata->db, sdata->env, 0);
147 if (rc)
148 goto fail_env;
149
150 uint32_t createflags = DB_CREATE;
151 struct stat st = { 0 };
152
153 if ((stat(path, &st) != 0) && (errno == ENOENT))
154 {
155 createflags |= DB_EXCL;
156 sdata->db->set_pagesize(sdata->db, pagesize);
157 }
158
159 rc = (*sdata->db->open)(sdata->db, NULL, path, NULL, DB_BTREE, createflags, 0600);
160 if (rc)
161 goto fail_db;
162
163 // Return an opaque pointer
164 return (StoreHandle *) sdata;
165
166fail_db:
167 sdata->db->close(sdata->db, 0);
168fail_env:
169 sdata->env->close(sdata->env, 0);
170fail_unlock:
171 mutt_file_unlock(sdata->fd);
172fail_close:
173 close(sdata->fd);
174 unlink(buf_string(&sdata->lockfile));
175 bdb_sdata_free(&sdata);
176
177 return NULL;
178}
static void bdb_sdata_free(struct BdbStoreData **ptr)
Free Bdb Store Data.
Definition: bdb.c:58
static struct BdbStoreData * bdb_sdata_new(void)
Create new Bdb Store Data.
Definition: bdb.c:73
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
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1202
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1249
void StoreHandle
Opaque type for store backend.
Definition: lib.h:61
Berkeley DB Store.
Definition: bdb.c:47
DB * db
Definition: bdb.c:49
DB_ENV * env
Definition: bdb.c:48
int fd
Definition: bdb.c:50
struct Buffer lockfile
Definition: bdb.c:51
+ Here is the call graph for this function:

◆ store_gdbm_open()

static StoreHandle * store_gdbm_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 42 of file gdbm.c.

43{
44 if (!path)
45 return NULL;
46
47 const int pagesize = 4096;
48
49 GDBM_FILE db = gdbm_open((char *) path, pagesize,
50 create ? GDBM_WRCREAT : GDBM_WRITER, 00600, NULL);
51 if (!db)
52 {
53 /* if rw failed try ro */
54 db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
55 }
56
57 // Return an opaque pointer
58 return (StoreHandle *) db;
59}

◆ store_kyotocabinet_open()

static StoreHandle * store_kyotocabinet_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 41 of file kc.c.

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}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
String manipulation buffer.
Definition: buffer.h:36
+ Here is the call graph for this function:

◆ store_lmdb_open()

static StoreHandle * store_lmdb_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 150 of file lmdb.c.

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}
static struct LmdbStoreData * lmdb_sdata_new(void)
Create new Lmdb Store Data.
Definition: lmdb.c:87
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
@ TXN_UNINITIALIZED
Transaction is uninitialised.
Definition: lmdb.c:58
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
+ Here is the call graph for this function:

◆ store_qdbm_open()

static StoreHandle * store_qdbm_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 42 of file qdbm.c.

43{
44 if (!path)
45 return NULL;
46
47 VILLA *db = vlopen(path, VL_OWRITER | (create ? VL_OCREAT : 0), VL_CMPLEX);
48
49 // Return an opaque pointer
50 return (StoreHandle *) db;
51}

◆ store_rocksdb_open()

static StoreHandle * store_rocksdb_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 77 of file rocksdb.c.

78{
79 if (!path)
80 return NULL;
81
82 struct RocksDbStoreData *sdata = rocksdb_sdata_new();
83
84 /* RocksDB stores errors in form of strings */
85 sdata->err = NULL;
86
87 /* setup generic options, create new db and limit log to one file */
88 sdata->options = rocksdb_options_create();
89 if (create)
90 {
91 rocksdb_options_set_create_if_missing(sdata->options, 1);
92 }
93 rocksdb_options_set_keep_log_file_num(sdata->options, 1);
94
95 /* setup read options, we verify with checksums */
96 sdata->read_options = rocksdb_readoptions_create();
97 rocksdb_readoptions_set_verify_checksums(sdata->read_options, 1);
98
99 /* setup write options, no sync needed, disable WAL */
100 sdata->write_options = rocksdb_writeoptions_create();
101 rocksdb_writeoptions_set_sync(sdata->write_options, 0);
102 rocksdb_writeoptions_disable_WAL(sdata->write_options, 1);
103
104 rocksdb_options_set_compression(sdata->options, rocksdb_no_compression);
105
106 /* open database and check for error in sdata->error */
107 sdata->db = rocksdb_open(sdata->options, path, &sdata->err);
108 if (sdata->err)
109 {
110 rocksdb_options_destroy(sdata->options);
111 rocksdb_readoptions_destroy(sdata->read_options);
112 rocksdb_writeoptions_destroy(sdata->write_options);
113 rocksdb_sdata_free(&sdata);
114 return NULL;
115 }
116
117 // Return an opaque pointer
118 return (StoreHandle *) sdata;
119}
static struct RocksDbStoreData * rocksdb_sdata_new(void)
Create new RocksDb Store Data.
Definition: rocksdb.c:69
static void rocksdb_sdata_free(struct RocksDbStoreData **ptr)
Free RocksDb Store Data.
Definition: rocksdb.c:54
RocksDB store.
Definition: rocksdb.c:42
rocksdb_options_t * options
Definition: rocksdb.c:44
rocksdb_t * db
Definition: rocksdb.c:43
rocksdb_readoptions_t * read_options
Definition: rocksdb.c:45
rocksdb_writeoptions_t * write_options
Definition: rocksdb.c:46
char * err
Definition: rocksdb.c:47
+ Here is the call graph for this function:

◆ store_tokyocabinet_open()

static StoreHandle * store_tokyocabinet_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 42 of file tc.c.

43{
44 if (!path)
45 return NULL;
46
47 TCBDB *db = tcbdbnew();
48 if (!db)
49 return NULL;
50 if (!tcbdbopen(db, path, BDBOWRITER | (create ? BDBOCREAT : 0)))
51 {
52 int ecode = tcbdbecode(db);
53 mutt_debug(LL_DEBUG2, "tcbdbopen failed for %s: %s (ecode %d)\n", path,
54 tcbdberrmsg(ecode), ecode);
55 tcbdbdel(db);
56 return NULL;
57 }
58
59 // Return an opaque pointer
60 return (StoreHandle *) db;
61}

◆ store_tdb_open()

static StoreHandle * store_tdb_open ( const char *  path,
bool  create 
)
static

Open a connection to a Store - Implements StoreOps::open() -.

Definition at line 42 of file tdb.c.

43{
44 if (!path)
45 return NULL;
46
47 /* TDB_NOLOCK - Don't do any locking
48 * TDB_NOSYNC - Don't use synchronous transactions
49 * TDB_INCOMPATIBLE_HASH - Better hashing
50 */
51 const int flags = TDB_NOLOCK | TDB_INCOMPATIBLE_HASH | TDB_NOSYNC;
52 const int hash_size = 33533; // Based on test timings for 100K emails
53
54 struct tdb_context *db = tdb_open(path, hash_size, flags,
55 (create ? O_CREAT : 0) | O_RDWR, 00600);
56
57 // Return an opaque pointer
58 return (StoreHandle *) db;
59}