NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN

Open a connection to a Store. More...

+ Collaboration diagram for open():

Functions

static void * store_bdb_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_gdbm_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_kyotocabinet_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_lmdb_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_qdbm_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_rocksdb_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_tokyocabinet_open (const char *path)
 Implements StoreOps::open() -. More...
 
static void * store_tdb_open (const char *path)
 Implements StoreOps::open() -. More...
 

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 context information on success.

Function Documentation

◆ store_bdb_open()

static void* store_bdb_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 89 of file bdb.c.

90 {
91  if (!path)
92  return NULL;
93 
94  struct stat sb;
95  int ret;
96  uint32_t createflags = DB_CREATE;
97 
98  struct StoreDbCtx *ctx = mutt_mem_malloc(sizeof(struct StoreDbCtx));
99 
100  const int pagesize = 512;
101 
102  ctx->lockfile = mutt_buffer_make(128);
103  mutt_buffer_printf(&ctx->lockfile, "%s-lock-hack", path);
104 
105  ctx->fd = open(mutt_buffer_string(&ctx->lockfile), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
106  if (ctx->fd < 0)
107  {
108  FREE(&ctx);
109  return NULL;
110  }
111 
112  if (mutt_file_lock(ctx->fd, true, true))
113  goto fail_close;
114 
115  ret = db_env_create(&ctx->env, 0);
116  if (ret)
117  goto fail_unlock;
118 
119  ret = (*ctx->env->open)(ctx->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
120  if (ret)
121  goto fail_env;
122 
123  ctx->db = NULL;
124  ret = db_create(&ctx->db, ctx->env, 0);
125  if (ret)
126  goto fail_env;
127 
128  if ((stat(path, &sb) != 0) && (errno == ENOENT))
129  {
130  createflags |= DB_EXCL;
131  ctx->db->set_pagesize(ctx->db, pagesize);
132  }
133 
134  ret = (*ctx->db->open)(ctx->db, NULL, path, NULL, DB_BTREE, createflags, 0600);
135  if (ret)
136  goto fail_db;
137 
138  return ctx;
139 
140 fail_db:
141  ctx->db->close(ctx->db, 0);
142 fail_env:
143  ctx->env->close(ctx->env, 0);
144 fail_unlock:
145  mutt_file_unlock(ctx->fd);
146 fail_close:
147  close(ctx->fd);
148  unlink(mutt_buffer_string(&ctx->lockfile));
150  FREE(&ctx);
151 
152  return NULL;
153 }
Berkeley DB context.
Definition: bdb.c:48
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
DB * db
Definition: bdb.c:51
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1226
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
int mutt_file_lock(int fd, bool excl, bool timeout)
(try to) lock a file using fcntl()
Definition: file.c:1178
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
struct Buffer lockfile
Definition: bdb.c:53
#define FREE(x)
Definition: memory.h:40
int fd
Definition: bdb.c:52
DB_ENV * env
Definition: bdb.c:50
+ Here is the call graph for this function:

◆ store_gdbm_open()

static void* store_gdbm_open ( const char *  path)
static

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, GDBM_WRCREAT, 00600, NULL);
50  if (db)
51  return db;
52 
53  /* if rw failed try ro */
54  return gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
55 }
DB * db
Definition: bdb.c:51

◆ store_kyotocabinet_open()

static void* store_kyotocabinet_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 42 of file kc.c.

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 }
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
DB * db
Definition: bdb.c:51
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
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
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
+ Here is the call graph for this function:

◆ store_lmdb_open()

static void* store_lmdb_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 122 of file lmdb.c.

123 {
124  if (!path)
125  return NULL;
126 
127  int rc;
128 
129  struct StoreLmdbCtx *ctx = mutt_mem_calloc(1, sizeof(struct StoreLmdbCtx));
130 
131  rc = mdb_env_create(&ctx->env);
132  if (rc != MDB_SUCCESS)
133  {
134  mutt_debug(LL_DEBUG2, "mdb_env_create: %s\n", mdb_strerror(rc));
135  FREE(&ctx);
136  return NULL;
137  }
138 
139  mdb_env_set_mapsize(ctx->env, LMDB_DB_SIZE);
140 
141  rc = mdb_env_open(ctx->env, path, MDB_NOSUBDIR, 0644);
142  if (rc != MDB_SUCCESS)
143  {
144  mutt_debug(LL_DEBUG2, "mdb_env_open: %s\n", mdb_strerror(rc));
145  goto fail_env;
146  }
147 
148  rc = mdb_get_r_txn(ctx);
149  if (rc != MDB_SUCCESS)
150  {
151  mutt_debug(LL_DEBUG2, "mdb_txn_begin: %s\n", mdb_strerror(rc));
152  goto fail_env;
153  }
154 
155  rc = mdb_dbi_open(ctx->txn, NULL, MDB_CREATE, &ctx->db);
156  if (rc != MDB_SUCCESS)
157  {
158  mutt_debug(LL_DEBUG2, "mdb_dbi_open: %s\n", mdb_strerror(rc));
159  goto fail_dbi;
160  }
161 
162  mdb_txn_reset(ctx->txn);
164  return ctx;
165 
166 fail_dbi:
167  mdb_txn_abort(ctx->txn);
169  ctx->txn = NULL;
170 
171 fail_env:
172  mdb_env_close(ctx->env);
173  FREE(&ctx);
174  return NULL;
175 }
enum MdbTxnMode txn_mode
Definition: lmdb.c:61
MDB_env * env
Definition: lmdb.c:58
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
MDB_txn * txn
Definition: lmdb.c:59
LMDB context.
Definition: lmdb.c:56
Log at debug level 2.
Definition: logging.h:41
static int mdb_get_r_txn(struct StoreLmdbCtx *ctx)
Get an LMDB read transaction.
Definition: lmdb.c:69
const size_t LMDB_DB_SIZE
The maximum size of the database file (2GiB).
Definition: lmdb.c:41
MDB_dbi db
Definition: lmdb.c:60
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Transaction is uninitialised.
Definition: lmdb.c:48
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:

◆ store_qdbm_open()

static void* store_qdbm_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 44 of file qdbm.c.

45 {
46  if (!path)
47  return NULL;
48 
49  return vlopen(path, VL_OWRITER | VL_OCREAT, VL_CMPLEX);
50 }

◆ store_rocksdb_open()

static void* store_rocksdb_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 51 of file rocksdb.c.

52 {
53  if (!path)
54  return NULL;
55 
56  struct RocksDbCtx *ctx = mutt_mem_malloc(sizeof(struct RocksDbCtx));
57 
58  /* RocksDB store errors in form of strings */
59  ctx->err = NULL;
60 
61  /* setup generic options, create new db and limit log to one file */
62  ctx->options = rocksdb_options_create();
63  rocksdb_options_set_create_if_missing(ctx->options, 1);
64  rocksdb_options_set_keep_log_file_num(ctx->options, 1);
65 
66  /* setup read options, we verify with checksums */
67  ctx->read_options = rocksdb_readoptions_create();
68  rocksdb_readoptions_set_verify_checksums(ctx->read_options, 1);
69 
70  /* setup write options, no sync needed, disable WAL */
71  ctx->write_options = rocksdb_writeoptions_create();
72  rocksdb_writeoptions_set_sync(ctx->write_options, 0);
73  rocksdb_writeoptions_disable_WAL(ctx->write_options, 1);
74 
75  rocksdb_options_set_compression(ctx->options, rocksdb_no_compression);
76 
77  /* open database and check for error in ctx->error */
78  ctx->db = rocksdb_open(ctx->options, path, &ctx->err);
79  if (ctx->err)
80  {
81  rocksdb_free(ctx->err);
82  FREE(&ctx);
83  return NULL;
84  }
85 
86  return ctx;
87 }
rocksdb_options_t * options
Definition: rocksdb.c:42
rocksdb_t * db
Definition: rocksdb.c:41
rocksdb_readoptions_t * read_options
Definition: rocksdb.c:43
char * err
Definition: rocksdb.c:45
rocksdb_writeoptions_t * write_options
Definition: rocksdb.c:44
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
Berkeley DB context.
Definition: rocksdb.c:39
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:

◆ store_tokyocabinet_open()

static void* store_tokyocabinet_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 43 of file tc.c.

44 {
45  if (!path)
46  return NULL;
47 
48  TCBDB *db = tcbdbnew();
49  if (!db)
50  return NULL;
51  if (tcbdbopen(db, path, BDBOWRITER | BDBOCREAT))
52  return db;
53 
54  int ecode = tcbdbecode(db);
55  mutt_debug(LL_DEBUG2, "tcbdbopen failed for %s: %s (ecode %d)\n", path,
56  tcbdberrmsg(ecode), ecode);
57  tcbdbdel(db);
58  return NULL;
59 }
rocksdb_t * db
Definition: rocksdb.c:41
Log at debug level 2.
Definition: logging.h:41
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

◆ store_tdb_open()

static void* store_tdb_open ( const char *  path)
static

Implements StoreOps::open() -.

Definition at line 40 of file tdb.c.

41 {
42  if (!path)
43  return NULL;
44 
45  /* TDB_NOLOCK - Don't do any locking
46  * TDB_NOSYNC - Don't use synchronous transactions
47  * TDB_INCOMPATIBLE_HASH - Better hashing
48  */
49  const int flags = TDB_NOLOCK | TDB_INCOMPATIBLE_HASH | TDB_NOSYNC;
50  const int hash_size = 33533; // Based on test timings for 100K emails
51  return tdb_open(path, hash_size, flags, O_CREAT | O_RDWR, 00600);
52 }