NeoMutt  2024-04-25-85-g27bab4
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), O_WRONLY | (create ? O_CREAT : 0), S_IRUSR | S_IWUSR);
127 if (sdata->fd < 0)
128 {
129 bdb_sdata_free(&sdata);
130 return NULL;
131 }
132
133 if (mutt_file_lock(sdata->fd, true, true))
134 goto fail_close;
135
136 int rc = db_env_create(&sdata->env, 0);
137 if (rc)
138 goto fail_unlock;
139
140 rc = (*sdata->env->open)(sdata->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
141 if (rc)
142 goto fail_env;
143
144 sdata->db = NULL;
145 rc = db_create(&sdata->db, sdata->env, 0);
146 if (rc)
147 goto fail_env;
148
149 uint32_t createflags = DB_CREATE;
150 struct stat st = { 0 };
151
152 if ((stat(path, &st) != 0) && (errno == ENOENT))
153 {
154 createflags |= DB_EXCL;
155 sdata->db->set_pagesize(sdata->db, pagesize);
156 }
157
158 rc = (*sdata->db->open)(sdata->db, NULL, path, NULL, DB_BTREE, createflags, 0600);
159 if (rc)
160 goto fail_db;
161
162 // Return an opaque pointer
163 return (StoreHandle *) sdata;
164
165fail_db:
166 sdata->db->close(sdata->db, 0);
167fail_env:
168 sdata->env->close(sdata->env, 0);
169fail_unlock:
170 mutt_file_unlock(sdata->fd);
171fail_close:
172 close(sdata->fd);
173 unlink(buf_string(&sdata->lockfile));
174 bdb_sdata_free(&sdata);
175
176 return NULL;
177}
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 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, create ? GDBM_WRCREAT : GDBM_WRITER, 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,
bool  create 
)
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 | (create ? KCOCREATE : 0)))
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,
bool  create 
)
static

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

Definition at line 149 of file lmdb.c.

150{
151 if (!path)
152 return NULL;
153
154 if (!create && access(path, F_OK) != 0)
155 {
156 return NULL;
157 }
158
159 struct LmdbStoreData *sdata = lmdb_sdata_new();
160
161 int rc = mdb_env_create(&sdata->env);
162 if (rc != MDB_SUCCESS)
163 {
164 mutt_debug(LL_DEBUG2, "mdb_env_create: %s\n", mdb_strerror(rc));
165 lmdb_sdata_free(&sdata);
166 return NULL;
167 }
168
169 mdb_env_set_mapsize(sdata->env, LMDB_DB_SIZE);
170
171 rc = mdb_env_open(sdata->env, path, MDB_NOSUBDIR, 0644);
172 if (rc != MDB_SUCCESS)
173 {
174 mutt_debug(LL_DEBUG2, "mdb_env_open: %s\n", mdb_strerror(rc));
175 goto fail_env;
176 }
177
178 rc = lmdb_get_read_txn(sdata);
179 if (rc != MDB_SUCCESS)
180 {
181 mutt_debug(LL_DEBUG2, "mdb_txn_begin: %s\n", mdb_strerror(rc));
182 goto fail_env;
183 }
184
185 rc = mdb_dbi_open(sdata->txn, NULL, MDB_CREATE, &sdata->db);
186 if (rc != MDB_SUCCESS)
187 {
188 mutt_debug(LL_DEBUG2, "mdb_dbi_open: %s\n", mdb_strerror(rc));
189 goto fail_dbi;
190 }
191
192 mdb_txn_reset(sdata->txn);
194 // Return an opaque pointer
195 return (StoreHandle *) sdata;
196
197fail_dbi:
198 mdb_txn_abort(sdata->txn);
200 sdata->txn = NULL;
201
202fail_env:
203 mdb_env_close(sdata->env);
204 lmdb_sdata_free(&sdata);
205 return NULL;
206}
static struct LmdbStoreData * lmdb_sdata_new(void)
Create new Lmdb Store Data.
Definition: lmdb.c:86
static void lmdb_sdata_free(struct LmdbStoreData **ptr)
Free Lmdb Store Data.
Definition: lmdb.c:77
static int lmdb_get_read_txn(struct LmdbStoreData *sdata)
Get an LMDB read transaction.
Definition: lmdb.c:96
@ TXN_UNINITIALIZED
Transaction is uninitialised.
Definition: lmdb.c:57
LMDB store.
Definition: lmdb.c:66
MDB_txn * txn
Definition: lmdb.c:68
MDB_env * env
Definition: lmdb.c:67
MDB_dbi db
Definition: lmdb.c:69
enum LmdbTxnMode txn_mode
Definition: lmdb.c:70
+ 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 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 if (create)
89 {
90 rocksdb_options_set_create_if_missing(sdata->options, 1);
91 }
92 rocksdb_options_set_keep_log_file_num(sdata->options, 1);
93
94 /* setup read options, we verify with checksums */
95 sdata->read_options = rocksdb_readoptions_create();
96 rocksdb_readoptions_set_verify_checksums(sdata->read_options, 1);
97
98 /* setup write options, no sync needed, disable WAL */
99 sdata->write_options = rocksdb_writeoptions_create();
100 rocksdb_writeoptions_set_sync(sdata->write_options, 0);
101 rocksdb_writeoptions_disable_WAL(sdata->write_options, 1);
102
103 rocksdb_options_set_compression(sdata->options, rocksdb_no_compression);
104
105 /* open database and check for error in sdata->error */
106 sdata->db = rocksdb_open(sdata->options, path, &sdata->err);
107 if (sdata->err)
108 {
109 rocksdb_options_destroy(sdata->options);
110 rocksdb_readoptions_destroy(sdata->read_options);
111 rocksdb_writeoptions_destroy(sdata->write_options);
112 rocksdb_sdata_free(&sdata);
113 return NULL;
114 }
115
116 // Return an opaque pointer
117 return (StoreHandle *) sdata;
118}
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,
bool  create 
)
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 | (create ? BDBOCREAT : 0)))
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,
bool  create 
)
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, (create ? O_CREAT : 0) | O_RDWR, 00600);
54
55 // Return an opaque pointer
56 return (StoreHandle *) db;
57}