NeoMutt  2024-04-16-36-g75b6fb
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)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_gdbm_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_kyotocabinet_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_lmdb_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_qdbm_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_rocksdb_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_tokyocabinet_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 
static StoreHandlestore_tdb_open (const char *path)
 Open a connection to a Store - Implements StoreOps::open() -.
 

Detailed Description

Open a connection to a Store.

Parameters
[in]pathPath to the database file
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)
static

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

Definition at line 114 of file bdb.c.

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

◆ store_gdbm_open()

static StoreHandle * store_gdbm_open ( const char *  path)
static

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

Definition at line 41 of file gdbm.c.

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

◆ store_kyotocabinet_open()

static StoreHandle * store_kyotocabinet_open ( const char *  path)
static

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

Definition at line 40 of file kc.c.

41{
42 if (!path)
43 return NULL;
44
45 KCDB *db = kcdbnew();
46 if (!db)
47 return NULL;
48
49 struct Buffer *kcdbpath = buf_pool_get();
50
51 buf_printf(kcdbpath, "%s#type=kct#opts=l#rcomp=lex", path);
52
53 if (!kcdbopen(db, buf_string(kcdbpath), KCOWRITER | KCOCREATE))
54 {
55 int ecode = kcdbecode(db);
56 mutt_debug(LL_DEBUG2, "kcdbopen failed for %s: %s (ecode %d)\n",
57 buf_string(kcdbpath), kcdbemsg(db), ecode);
58 kcdbdel(db);
59 db = NULL;
60 }
61
62 buf_pool_release(&kcdbpath);
63 // Return an opaque pointer
64 return (StoreHandle *) db;
65}
#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:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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)
static

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

Definition at line 148 of file lmdb.c.

149{
150 if (!path)
151 return NULL;
152
153 struct LmdbStoreData *sdata = lmdb_sdata_new();
154
155 int rc = mdb_env_create(&sdata->env);
156 if (rc != MDB_SUCCESS)
157 {
158 mutt_debug(LL_DEBUG2, "mdb_env_create: %s\n", mdb_strerror(rc));
159 lmdb_sdata_free(&sdata);
160 return NULL;
161 }
162
163 mdb_env_set_mapsize(sdata->env, LMDB_DB_SIZE);
164
165 rc = mdb_env_open(sdata->env, path, MDB_NOSUBDIR, 0644);
166 if (rc != MDB_SUCCESS)
167 {
168 mutt_debug(LL_DEBUG2, "mdb_env_open: %s\n", mdb_strerror(rc));
169 goto fail_env;
170 }
171
172 rc = lmdb_get_read_txn(sdata);
173 if (rc != MDB_SUCCESS)
174 {
175 mutt_debug(LL_DEBUG2, "mdb_txn_begin: %s\n", mdb_strerror(rc));
176 goto fail_env;
177 }
178
179 rc = mdb_dbi_open(sdata->txn, NULL, MDB_CREATE, &sdata->db);
180 if (rc != MDB_SUCCESS)
181 {
182 mutt_debug(LL_DEBUG2, "mdb_dbi_open: %s\n", mdb_strerror(rc));
183 goto fail_dbi;
184 }
185
186 mdb_txn_reset(sdata->txn);
188 // Return an opaque pointer
189 return (StoreHandle *) sdata;
190
191fail_dbi:
192 mdb_txn_abort(sdata->txn);
194 sdata->txn = NULL;
195
196fail_env:
197 mdb_env_close(sdata->env);
198 lmdb_sdata_free(&sdata);
199 return NULL;
200}
static struct LmdbStoreData * lmdb_sdata_new(void)
Create new Lmdb Store Data.
Definition: lmdb.c:85
static void lmdb_sdata_free(struct LmdbStoreData **ptr)
Free Lmdb Store Data.
Definition: lmdb.c:76
static int lmdb_get_read_txn(struct LmdbStoreData *sdata)
Get an LMDB read transaction.
Definition: lmdb.c:95
@ TXN_UNINITIALIZED
Transaction is uninitialised.
Definition: lmdb.c:56
LMDB store.
Definition: lmdb.c:65
MDB_txn * txn
Definition: lmdb.c:67
MDB_env * env
Definition: lmdb.c:66
MDB_dbi db
Definition: lmdb.c:68
enum LmdbTxnMode txn_mode
Definition: lmdb.c:69
+ Here is the call graph for this function:

◆ store_qdbm_open()

static StoreHandle * store_qdbm_open ( const char *  path)
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 | VL_OCREAT, VL_CMPLEX);
48
49 // Return an opaque pointer
50 return (StoreHandle *) db;
51}

◆ store_rocksdb_open()

static StoreHandle * store_rocksdb_open ( const char *  path)
static

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

Definition at line 76 of file rocksdb.c.

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

◆ store_tokyocabinet_open()

static StoreHandle * store_tokyocabinet_open ( const char *  path)
static

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

Definition at line 41 of file tc.c.

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

◆ store_tdb_open()

static StoreHandle * store_tdb_open ( const char *  path)
static

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

Definition at line 41 of file tdb.c.

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}