Commit 37b42e21 authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul

Make db_create work under c++. Addresses #197.

git-svn-id: file:///svn/tokudb@1194 c7de825b-a66e-492c-adef-691d508d4ae1
parent b817a0e0
...@@ -43,6 +43,7 @@ typedef enum { ...@@ -43,6 +43,7 @@ typedef enum {
#define DB_ARCH_ABS 1 #define DB_ARCH_ABS 1
#define DB_ARCH_LOG 4 #define DB_ARCH_LOG 4
#define DB_CREATE 1 #define DB_CREATE 1
#define DB_CXX_NO_EXCEPTIONS 1
#define DB_EXCL 8192 #define DB_EXCL 8192
#define DB_PRIVATE 1048576 #define DB_PRIVATE 1048576
#define DB_RDONLY 16 #define DB_RDONLY 16
......
...@@ -44,6 +44,7 @@ void print_defines (void) { ...@@ -44,6 +44,7 @@ void print_defines (void) {
dodefine(DB_ARCH_LOG); dodefine(DB_ARCH_LOG);
dodefine(DB_CREATE); dodefine(DB_CREATE);
dodefine(DB_CXX_NO_EXCEPTIONS);
dodefine(DB_EXCL); dodefine(DB_EXCL);
dodefine(DB_PRIVATE); dodefine(DB_PRIVATE);
dodefine(DB_RDONLY); dodefine(DB_RDONLY);
......
CPPFLAGS = -I../include CPPFLAGS = -I../include
CC = c++ CC = c++
test1: test1.o dbt.o db.o LDFLAGS = -lz
test1: test1.o dbt.o db.o dbenv.o ../lib/libdb.a
#include <assert.h>
#include <db.h>
#include <errno.h>
#include "db_cxx.h" #include "db_cxx.h"
Db::Db(DbEnv *env, u_int32_t flags) {
the_db = 0;
the_Env = env;
if (the_Env == 0) {
is_private_env = 1;
}
DB *tmp_db;
int ret = db_create(&tmp_db, the_Env->get_DB_ENV(), flags & !(DB_CXX_NO_EXCEPTIONS));
if (ret!=0) {
assert(0); // make an error
}
the_db = tmp_db;
tmp_db->api_internal = this;
if (is_private_env) {
the_Env = new DbEnv(tmp_db->dbenv, flags & DB_CXX_NO_EXCEPTIONS);
}
}
Db::~Db() { Db::~Db() {
if (!the_db) { if (!the_db) {
close(0); // the user should have called close, but we do it here if not done. close(0); // the user should have called close, but we do it here if not done.
...@@ -10,13 +31,24 @@ int Db::close (u_int32_t flags) { ...@@ -10,13 +31,24 @@ int Db::close (u_int32_t flags) {
if (!the_db) { if (!the_db) {
return EINVAL; return EINVAL;
} }
the_db->toku_internal = 0; the_db->api_internal = 0;
int ret = the_db->close(flags); int ret = the_db->close(the_db, flags);
the_db = 0; the_db = 0;
// Do we need to clean up "private environments"? // Do we need to clean up "private environments"?
// What about cursors? They should be cleaned up already. // What about cursors? They should be cleaned up already, but who did it?
return ret; return ret;
} }
int Db::open(DbTxn *txn, const char *filename, const char *subname, DBTYPE typ, u_int32_t flags, int mode) {
int ret = the_db->open(the_db, txn->get_DB_TXN(), filename, subname, typ, flags, mode);
return ret;
}
int Db::put(DbTxn *txn, Dbt *key, Dbt *data, u_int32_t flags) {
int ret = the_db->put(the_db, txn->get_DB_TXN(), key->get_DBT(), data->get_DBT(), flags);
return ret;
}
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
#include <string.h> #include <string.h>
class Dbt; class Dbt;
class DbEnv;
class DbTxn;
class Dbc;
// DBT and Dbt objects are the same pointers. So watch out if you use Dbt to make other classes (e.g., with subclassing). // DBT and Dbt objects are the same pointers. So watch out if you use Dbt to make other classes (e.g., with subclassing).
class Dbt : private DBT class Dbt : private DBT
...@@ -16,6 +19,7 @@ class Dbt : private DBT ...@@ -16,6 +19,7 @@ class Dbt : private DBT
DBT *get_DBT(void) { return (DBT*)this; } DBT *get_DBT(void) { return (DBT*)this; }
Dbt(void */*data*/, u_int32_t /*size*/);
Dbt(void); Dbt(void);
~Dbt(); ~Dbt();
...@@ -29,17 +33,17 @@ class Db { ...@@ -29,17 +33,17 @@ class Db {
Db(DbEnv *dbenv, u_int32_t flags); Db(DbEnv *dbenv, u_int32_t flags);
~Db(); ~Db();
DB *Db::get_DB(void) { DB *get_DB(void) {
return the_db; return the_db;
} }
const DB *Db::get_const_DB() const { const DB *get_const_DB() const {
return the_db; return the_db;
} }
static Db *Db::get_Db(DB *db) { static Db *get_Db(DB *db) {
return (Db*)db->toku_internal; return (Db*)db->api_internal;
} }
static const Db *Db::get_const_Db(const DB *db) { static const Db *get_const_Db(const DB *db) {
return (Db*)db->toku_internal; return (Db*)db->api_internal;
} }
/* C++ analogues of the C functions. */ /* C++ analogues of the C functions. */
...@@ -55,5 +59,36 @@ class Db { ...@@ -55,5 +59,36 @@ class Db {
private: private:
DB *the_db; DB *the_db;
DbEnv *the_Env;
int is_private_env;
};
class DbEnv {
friend class Db;
public:
DbEnv(u_int32_t flags);
DB_ENV *get_DB_ENV(void) {
return the_env;
}
/* C++ analogues of the C functions. */
int close(u_int32_t);
private:
DB_ENV *the_env;
DbEnv(DB_ENV *, u_int32_t flags);
};
class DbTxn {
public:
DB_TXN *get_DB_TXN()
{
return the_txn;
}
private:
DB_TXN *the_txn;
}; };
...@@ -5,6 +5,13 @@ Dbt::Dbt(void) { ...@@ -5,6 +5,13 @@ Dbt::Dbt(void) {
memset(dbt, 0, sizeof(*dbt)); memset(dbt, 0, sizeof(*dbt));
} }
Dbt::Dbt(void *data, u_int32_t size) {
DBT *dbt = this;
memset(dbt, 0, sizeof(*dbt));
set_data(data);
set_size(size);
}
Dbt::~Dbt(void) Dbt::~Dbt(void)
{ {
} }
SRCS = $(wildcard *.cpp)
TARGETS = $(patsubst %.cpp,%,$(SRCS))
DBCXX = ../dbt.o ../db.o ../dbenv.o ../dbt.o
CPPFLAGS = -I../ -I../../include
CXXFLAGS = -Wall -g
LDFLAGS = -L../lib -ldb
$(TARGETS): $(DBCXX)
all: $(TARGETS)
clean:
rm -rf $(TARGETS)
#include <assert.h>
#include <db_cxx.h>
int dbcreate(char *dbfile, char *dbname, int argc, char *argv[]) {
int r;
#if USE_ENV
DbEnv *env = new DbEnv(DB_CXX_NO_EXCEPTIONS);
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->open(0, dbfile, dbname, DB_BTREE, DB_CREATE, 0777);
assert(r == 0);
int i = 0;
while (i < argc) {
char *k = argv[i++];
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->close(0); assert(r == 0);
if (env) {
r = env->close(0); assert(r == 0);
delete env;
}
return 0;
}
int usage() {
printf("db_create [-s DBNAME] DBFILE [KEY VAL]*\n");
return 1;
}
int main(int argc, char *argv[]) {
char *dbname = 0;
int i;
for (i=1; i<argc; i++) {
char *arg = argv[i];
if (0 == strcmp(arg, "-h") || 0 == strcmp(arg, "--help"))
return usage();
if (0 == strcmp(arg, "-s")) {
i++;
if (i >= argc)
return usage();
dbname = argv[i];
continue;
}
break;
}
if (i >= argc)
return usage();
char *dbfile = argv[i++];
return dbcreate(dbfile, dbname, argc-i, &argv[i]);
}
...@@ -43,6 +43,7 @@ typedef enum { ...@@ -43,6 +43,7 @@ typedef enum {
#define DB_ARCH_ABS 1 #define DB_ARCH_ABS 1
#define DB_ARCH_LOG 4 #define DB_ARCH_LOG 4
#define DB_CREATE 1 #define DB_CREATE 1
#define DB_CXX_NO_EXCEPTIONS 1
#define DB_EXCL 8192 #define DB_EXCL 8192
#define DB_PRIVATE 1048576 #define DB_PRIVATE 1048576
#define DB_RDONLY 16 #define DB_RDONLY 16
......
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