MDB_cursor *cursor, int modify,
MDB_pageparent *mpp);
-static int mdbenv_read_header(MDB_env *env, MDB_meta *meta);
-static int mdbenv_read_meta(MDB_env *env, int *which);
-static int mdbenv_write_meta(MDB_txn *txn);
+static int mdb_env_read_header(MDB_env *env, MDB_meta *meta);
+static int mdb_env_read_meta(MDB_env *env, int *which);
+static int mdb_env_write_meta(MDB_txn *txn);
static MDB_page *mdb_get_page(MDB_txn *txn, pgno_t pgno);
static MDB_node *mdb_search_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp,
}
int
-mdbenv_sync(MDB_env *env)
+mdb_env_sync(MDB_env *env)
{
int rc = 0;
if (!F_ISSET(env->me_flags, MDB_NOSYNC)) {
txn->mt_env = env;
- if ((rc = mdbenv_read_meta(env, &toggle)) != MDB_SUCCESS) {
+ if ((rc = mdb_env_read_meta(env, &toggle)) != MDB_SUCCESS) {
mdb_txn_abort(txn);
return rc;
}
free(dp);
}
- if ((n = mdbenv_sync(env)) != 0 ||
- (n = mdbenv_write_meta(txn)) != MDB_SUCCESS ||
- (n = mdbenv_sync(env)) != 0) {
+ if ((n = mdb_env_sync(env)) != 0 ||
+ (n = mdb_env_write_meta(txn)) != MDB_SUCCESS ||
+ (n = mdb_env_sync(env)) != 0) {
mdb_txn_abort(txn);
return n;
}
}
static int
-mdbenv_read_header(MDB_env *env, MDB_meta *meta)
+mdb_env_read_header(MDB_env *env, MDB_meta *meta)
{
char page[PAGESIZE];
MDB_page *p;
}
static int
-mdbenv_init_meta(MDB_env *env, MDB_meta *meta)
+mdb_env_init_meta(MDB_env *env, MDB_meta *meta)
{
MDB_page *p, *q;
MDB_meta *m;
}
static int
-mdbenv_write_meta(MDB_txn *txn)
+mdb_env_write_meta(MDB_txn *txn)
{
MDB_env *env;
MDB_meta meta;
}
static int
-mdbenv_read_meta(MDB_env *env, int *which)
+mdb_env_read_meta(MDB_env *env, int *which)
{
int toggle = 0;
}
int
-mdbenv_create(MDB_env **env)
+mdb_env_create(MDB_env **env)
{
MDB_env *e;
}
int
-mdbenv_set_mapsize(MDB_env *env, size_t size)
+mdb_env_set_mapsize(MDB_env *env, size_t size)
{
if (env->me_map)
return EINVAL;
}
int
-mdbenv_set_maxdbs(MDB_env *env, int dbs)
+mdb_env_set_maxdbs(MDB_env *env, int dbs)
{
env->me_maxdbs = dbs;
return MDB_SUCCESS;
}
int
-mdbenv_set_maxreaders(MDB_env *env, int readers)
+mdb_env_set_maxreaders(MDB_env *env, int readers)
{
env->me_maxreaders = readers;
return MDB_SUCCESS;
}
int
-mdbenv_get_maxreaders(MDB_env *env, int *readers)
+mdb_env_get_maxreaders(MDB_env *env, int *readers)
{
if (!env || !readers)
return EINVAL;
}
static int
-mdbenv_open2(MDB_env *env, unsigned int flags)
+mdb_env_open2(MDB_env *env, unsigned int flags)
{
int i, newenv = 0;
MDB_meta meta;
memset(&meta, 0, sizeof(meta));
- if ((i = mdbenv_read_header(env, &meta)) != 0) {
+ if ((i = mdb_env_read_header(env, &meta)) != 0) {
if (i != ENOENT)
return i;
DPRINTF("new mdbenv");
meta.mm_mapsize = env->me_mapsize;
if (flags & MDB_FIXEDMAP)
meta.mm_address = env->me_map;
- i = mdbenv_init_meta(env, &meta);
+ i = mdb_env_init_meta(env, &meta);
if (i != MDB_SUCCESS) {
munmap(env->me_map, env->me_mapsize);
return i;
env->me_metas[0] = METADATA(p);
env->me_metas[1] = (MDB_meta *)((char *)env->me_metas[0] + meta.mm_psize);
- if ((i = mdbenv_read_meta(env, NULL)) != 0)
+ if ((i = mdb_env_read_meta(env, NULL)) != 0)
return i;
DPRINTF("opened database version %u, pagesize %u",
}
static void
-mdbenv_reader_dest(void *ptr)
+mdb_env_reader_dest(void *ptr)
{
MDB_reader *reader = ptr;
/* downgrade the exclusive lock on the region back to shared */
static void
-mdbenv_share_locks(MDB_env *env)
+mdb_env_share_locks(MDB_env *env)
{
struct flock lock_info;
}
static int
-mdbenv_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
+mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
{
int rc;
off_t size, rsize;
#define LOCKNAME "/lock.mdb"
#define DATANAME "/data.mdb"
int
-mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
+mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
{
int oflags, rc, len, excl;
char *lpath, *dpath;
sprintf(lpath, "%s" LOCKNAME, path);
sprintf(dpath, "%s" DATANAME, path);
- rc = mdbenv_setup_locks(env, lpath, mode, &excl);
+ rc = mdb_env_setup_locks(env, lpath, mode, &excl);
if (rc)
goto leave;
if ((env->me_fd = open(dpath, oflags, mode)) == -1)
return errno;
- if ((rc = mdbenv_open2(env, flags)) != MDB_SUCCESS) {
+ if ((rc = mdb_env_open2(env, flags)) != MDB_SUCCESS) {
close(env->me_fd);
env->me_fd = -1;
} else {
env->me_path = strdup(path);
DPRINTF("opened dbenv %p", (void *) env);
- pthread_key_create(&env->me_txkey, mdbenv_reader_dest);
+ pthread_key_create(&env->me_txkey, mdb_env_reader_dest);
if (excl)
- mdbenv_share_locks(env);
+ mdb_env_share_locks(env);
env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
env->me_dbs[0] = calloc(env->me_maxdbs, sizeof(MDB_db));
env->me_dbs[1] = calloc(env->me_maxdbs, sizeof(MDB_db));
}
void
-mdbenv_close(MDB_env *env)
+mdb_env_close(MDB_env *env)
{
if (env == NULL)
return;
free(env->me_dbxs);
free(env->me_path);
+ pthread_key_delete(env->me_txkey);
+
if (env->me_map) {
munmap(env->me_map, env->me_mapsize);
}
close(env->me_fd);
if (env->me_txns) {
+ pid_t pid = getpid();
size_t size = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
+ int i;
+ for (i=0; i<env->me_txns->mti_numreaders; i++)
+ if (env->me_txns->mti_readers[i].mr_pid == pid)
+ env->me_txns->mti_readers[i].mr_pid = 0;
munmap(env->me_txns, size);
}
close(env->me_lfd);
}
int
-mdbenv_get_flags(MDB_env *env, unsigned int *arg)
+mdb_env_get_flags(MDB_env *env, unsigned int *arg)
{
if (!env || !arg)
return EINVAL;
}
int
-mdbenv_get_path(MDB_env *env, const char **arg)
+mdb_env_get_path(MDB_env *env, const char **arg)
{
if (!env || !arg)
return EINVAL;
return MDB_SUCCESS;
}
int
-mdbenv_stat(MDB_env *env, MDB_stat *arg)
+mdb_env_stat(MDB_env *env, MDB_stat *arg)
{
if (env == NULL || arg == NULL)
return EINVAL;
unsigned long ms_entries;
} MDB_stat;
-int mdbenv_create(MDB_env **env);
-int mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
-int mdbenv_stat(MDB_env *env, MDB_stat *stat);
-int mdbenv_sync(MDB_env *env);
-void mdbenv_close(MDB_env *env);
-int mdbenv_get_flags(MDB_env *env, unsigned int *flags);
-int mdbenv_get_path(MDB_env *env, const char **path);
-int mdbenv_set_mapsize(MDB_env *env, size_t size);
-int mdbenv_set_maxreaders(MDB_env *env, int readers);
-int mdbenv_get_maxreaders(MDB_env *env, int *readers);
-int mdbenv_set_maxdbs(MDB_env *env, int dbs);
+int mdb_env_create(MDB_env **env);
+int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
+int mdb_env_stat(MDB_env *env, MDB_stat *stat);
+int mdb_env_sync(MDB_env *env);
+void mdb_env_close(MDB_env *env);
+int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
+int mdb_env_get_path(MDB_env *env, const char **path);
+int mdb_env_set_mapsize(MDB_env *env, size_t size);
+int mdb_env_set_maxreaders(MDB_env *env, int readers);
+int mdb_env_get_maxreaders(MDB_env *env, int *readers);
+int mdb_env_set_maxdbs(MDB_env *env, int dbs);
int mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
int mdb_txn_commit(MDB_txn *txn);
char *envname = argv[1];
char *subname = NULL;
- rc = mdbenv_create(&env);
+ rc = mdb_env_create(&env);
if (argc > 2) {
- mdbenv_set_maxdbs(env, 4);
+ mdb_env_set_maxdbs(env, 4);
subname = argv[2];
}
- rc = mdbenv_open(env, envname, MDB_RDONLY, 0);
+ rc = mdb_env_open(env, envname, MDB_RDONLY, 0);
if (rc) {
- printf("mdbenv_open failed, error %d\n", rc);
+ printf("mdb_env_open failed, error %d\n", rc);
exit(1);
}
rc = mdb_txn_begin(env, 1, &txn);
printf("Entries: %lu\n", mst.ms_entries);
mdb_close(txn, dbi);
mdb_txn_abort(txn);
- mdbenv_close(env);
+ mdb_env_close(env);
return 0;
}
values[i] = random()%1024;
}
- rc = mdbenv_create(&env);
- rc = mdbenv_set_mapsize(env, 10485760);
- rc = mdbenv_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
+ rc = mdb_env_create(&env);
+ rc = mdb_env_set_mapsize(env, 10485760);
+ rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
rc = mdb_txn_begin(env, 0, &txn);
rc = mdb_open(txn, NULL, 0, &dbi);
}
if (j) printf("%d duplicates skipped\n", j);
rc = mdb_txn_commit(txn);
- rc = mdbenv_stat(env, &mst);
+ rc = mdb_env_stat(env, &mst);
rc = mdb_txn_begin(env, 1, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
free(values);
printf("Deleted %d values\n", j);
- rc = mdbenv_stat(env, &mst);
+ rc = mdb_env_stat(env, &mst);
rc = mdb_txn_begin(env, 1, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
printf("Cursor next\n");
mdb_close(txn, dbi);
mdb_txn_abort(txn);
- mdbenv_close(env);
+ mdb_env_close(env);
return 0;
}
values[i] = random()%1024;
}
- rc = mdbenv_create(&env);
- rc = mdbenv_set_mapsize(env, 10485760);
- rc = mdbenv_set_maxdbs(env, 4);
- rc = mdbenv_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
+ rc = mdb_env_create(&env);
+ rc = mdb_env_set_mapsize(env, 10485760);
+ rc = mdb_env_set_maxdbs(env, 4);
+ rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
rc = mdb_txn_begin(env, 0, &txn);
rc = mdb_open(txn, "id1", MDB_CREATE, &dbi);
}
if (j) printf("%d duplicates skipped\n", j);
rc = mdb_txn_commit(txn);
- rc = mdbenv_stat(env, &mst);
+ rc = mdb_env_stat(env, &mst);
rc = mdb_txn_begin(env, 1, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
free(values);
printf("Deleted %d values\n", j);
- rc = mdbenv_stat(env, &mst);
+ rc = mdb_env_stat(env, &mst);
rc = mdb_txn_begin(env, 1, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
printf("Cursor next\n");
mdb_close(txn, dbi);
mdb_txn_abort(txn);
- mdbenv_close(env);
+ mdb_env_close(env);
return 0;
}
values[i] = random()%1024;
}
- rc = mdbenv_create(&env);
- rc = mdbenv_set_mapsize(env, 10485760);
- rc = mdbenv_set_maxdbs(env, 4);
- rc = mdbenv_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
+ rc = mdb_env_create(&env);
+ rc = mdb_env_set_mapsize(env, 10485760);
+ rc = mdb_env_set_maxdbs(env, 4);
+ rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
rc = mdb_txn_begin(env, 0, &txn);
rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi);
}
if (j) printf("%d duplicates skipped\n", j);
rc = mdb_txn_commit(txn);
- rc = mdbenv_stat(env, &mst);
+ rc = mdb_env_stat(env, &mst);
rc = mdb_txn_begin(env, 1, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
free(values);
printf("Deleted %d values\n", j);
- rc = mdbenv_stat(env, &mst);
+ rc = mdb_env_stat(env, &mst);
rc = mdb_txn_begin(env, 1, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
printf("Cursor next\n");
mdb_close(txn, dbi);
mdb_txn_abort(txn);
- mdbenv_close(env);
+ mdb_env_close(env);
return 0;
}