Commit f0bea621 authored by Rich Prohaska's avatar Rich Prohaska

port from branch 0.4.2 to main. closes #362

git-svn-id: file:///svn/tokudb@2196 c7de825b-a66e-492c-adef-691d508d4ae1
parent d61cd8a3
......@@ -39,6 +39,14 @@ int Dbc::count(db_recno_t *count, u_int32_t flags) {
return env->maybe_throw_error(ret);
}
int Dbc::put(Dbt* key, Dbt *data, u_int32_t flags) {
DBC *dbc = this;
int ret = dbc->c_put(dbc, key, data, flags);
DB_ENV *dbenv_c=dbc->dbp->dbenv;
DbEnv *env = (DbEnv*)dbenv_c->api1_internal;
return env->maybe_throw_error(ret);
}
// Not callable, but some compilers require it to be defined anyway.
Dbc::~Dbc()
{
......
......@@ -83,7 +83,7 @@ void DbEnv::set_errpfx(const char *errpfx) {
}
int DbEnv::maybe_throw_error(int err, DbEnv *env, int no_exceptions) throw (DbException) {
if (err==0) return 0;
if (err==0 || err==DB_NOTFOUND || err==DB_KEYEXIST || err==DB_KEYEMPTY) return err;
if (no_exceptions) return err;
if (err==DB_LOCK_DEADLOCK) {
DbDeadlockException e(env);
......
......@@ -46,6 +46,7 @@ check: $(TARGETS) \
$(VGRIND) ./test_error_stream
$(VGRIND) ./test_reverse_compare_fun
$(VGRIND) ./test_db_delete
$(VGRIND) ./test_get_not_found
check_db_create_1:
$(VGRIND) ./db_create; let exitcode=$$?; if [ $$exitcode -ne 0 ] ; then exit 0; else exit 1; fi
......
#include <assert.h>
#include <db_cxx.h>
#define USE_ENV 1
#ifndef DB_YESOVERWRITE
#define DB_YESOVERWRITE 0
#endif
char *data_dir;
int dbcreate(char *dbfile, char *dbname, int dbflags, int argc, char *argv[]) {
int r;
#if USE_ENV
DbEnv *env = new DbEnv(DB_CXX_NO_EXCEPTIONS);
if (data_dir) {
r = env->set_data_dir(data_dir); assert(r == 0);
}
r = env->open(".", DB_INIT_MPOOL + DB_CREATE + DB_PRIVATE, 0777); assert(r == 0);
#else
DbEnv *env = 0;
#endif
Db *db = new Db(env, DB_CXX_NO_EXCEPTIONS);
r = db->set_flags(dbflags);
assert(r == 0);
r = db->open(0, dbfile, dbname, DB_BTREE, DB_CREATE, 0777);
assert(r == 0);
r = db->set_flags(dbflags); assert(r == 0);
r = db->open(0, dbfile, dbname, DB_BTREE, DB_CREATE, 0777); assert(r == 0);
int i = 0;
while (i < argc) {
......@@ -23,23 +25,21 @@ int dbcreate(char *dbfile, char *dbname, int dbflags, int argc, char *argv[]) {
if (i < argc) {
char *v = argv[i++];
Dbt key(k, strlen(k)); Dbt val(v, strlen(v));
r = db->put(0, &key, &val, 0); assert(r == 0);
r = db->put(0, &key, &val, DB_YESOVERWRITE); assert(r == 0);
}
}
r = db->close(0); assert(r == 0);
delete db;
#if USE_ENV
if (env) {
r = env->close(0); assert(r == 0);
delete env;
}
#endif
r = env->close(0); assert(r == 0);
delete env;
return 0;
}
int usage() {
printf("db_create [-s DBNAME] [-D] [-S] DBFILE [KEY VAL]*\n");
printf("[--set_data_dir DIRNAME]\n");
return 1;
}
......@@ -66,6 +66,12 @@ int main(int argc, char *argv[]) {
dbflags += DB_DUPSORT;
continue;
}
if (0 == strcmp(arg, "--set_data_dir")) {
if (i+1 >= argc)
return usage();
data_dir = argv[++i];
continue;
}
break;
}
......
......@@ -23,7 +23,7 @@ int dbdump(char *dbfile, char *dbname) {
Db db(0, 0);
#endif
try {
r = db.open(0, dbfile, dbname, DB_UNKNOWN, 0, 0777);
r = db.open(0, dbfile, dbname, DB_BTREE, 0, 0777);
assert(r==0);
} catch (DbException e) {
printf("Cannot open %s:%s due to error %d\n", dbfile, dbname, e.get_errno());
......@@ -36,22 +36,23 @@ int dbdump(char *dbfile, char *dbname) {
Dbc *cursor;
r = db.cursor(0, &cursor, 0); assert(r == 0);
Dbt key; key.set_flags(DB_DBT_REALLOC);
Dbt val; val.set_flags(DB_DBT_REALLOC);
try {
for (;;) {
Dbt key; key.set_flags(DB_DBT_MALLOC);
Dbt val; val.set_flags(DB_DBT_MALLOC);
r = cursor->get(&key, &val, DB_NEXT);
if (r == DB_NOTFOUND) break;
assert(r==0);
// printf("%.*s\n", key.get_size(), (char *)key.get_data());
hexdump(&key);
free(key.get_data());
// printf("%.*s\n", val.get_size(), (char *)val.get_data());
hexdump(&val);
free(val.get_data());
}
}
} catch (DbException ) {
/* Nothing, that's just how we got out of the loop. */
}
free(key.get_data());
free(val.get_data());
r = cursor->close(); assert(r == 0);
r = db.close(0); assert(r == 0);
......
......@@ -21,6 +21,17 @@ int verbose = 0;
} \
})
#define TCRET(expr, expect) ({ \
if (verbose) printf("%s expect %d\n", #expr, expect); \
try { \
int r = expr; \
assert(expect==r); \
} catch (DbException e) { \
if (e.get_errno()!=expect) fprintf(stderr, "err=%d %s\n", e.get_errno(), db_strerror(e.get_errno())); \
assert(e.get_errno()==expect); \
} \
})
void test_env_exceptions (void) {
{
DbEnv env(0);
......@@ -42,7 +53,7 @@ void test_env_exceptions (void) {
DbTxn *txn;
TC(env.txn_begin(0, &txn, 0), 0);
TC(txn->commit(0), 0);
delete txn;
delete txn;
}
{
......@@ -51,7 +62,7 @@ void test_env_exceptions (void) {
DbTxn *txn;
TC(env.txn_begin(0, &txn, 0), 0);
TC(txn->commit(-1), EINVAL);
delete txn;
delete txn;
}
}
......@@ -100,7 +111,7 @@ void test_db_exceptions (void) {
Dbc *curs;
TC(db.cursor(0, &curs, 0), 0);
Dbt key,val;
TC(curs->get(&key, &val, DB_FIRST), DB_NOTFOUND);
// TC(curs->get(&key, &val, DB_FIRST), DB_NOTFOUND);
TC(curs->get(&key, &val, -1), EINVAL); // bad flags
curs->close(); // no deleting cursors.
}
......@@ -140,13 +151,20 @@ void test_dbc_exceptions () {
free(key.get_data());
free(val.get_data());
TC(curs->del(DB_DELETE_ANY), 0);
TC(curs->get(&key, &val, DB_CURRENT), DB_KEYEMPTY);
TC(curs->del(0), DB_KEYEMPTY);
TC(curs->del(DB_DELETE_ANY), 0);
TCRET(curs->get(&key, &val, DB_CURRENT), DB_KEYEMPTY);
TCRET(curs->del(0), DB_KEYEMPTY);
TCRET(curs->del(DB_DELETE_ANY), 0);
curs->close(); // no deleting cursors.
}
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
if (0 == strcmp(arg, "-v")) {
verbose++;
continue;
}
}
test_env_exceptions();
test_db_exceptions();
test_dbc_exceptions();
......
This diff is collapsed.
......@@ -224,6 +224,7 @@ class Dbc : protected DBC {
int pget(Dbt *, Dbt *, Dbt *, u_int32_t);
int del(u_int32_t);
int count(db_recno_t *, u_int32_t);
int put(Dbt *, Dbt *, u_int32_t);
private:
Dbc(); // User may not call it.
~Dbc(); // User may not delete it.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment