2007-12-18 16:22:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <db_cxx.h>
|
|
|
|
|
2008-02-07 19:06:12 +00:00
|
|
|
#define USE_ENV 1
|
|
|
|
|
2008-02-05 19:07:46 +00:00
|
|
|
int dbcreate(char *dbfile, char *dbname, int dbflags, int argc, char *argv[]) {
|
2007-12-18 16:22:21 +00:00
|
|
|
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);
|
2008-02-05 19:07:46 +00:00
|
|
|
r = db->set_flags(dbflags);
|
|
|
|
assert(r == 0);
|
2007-12-18 16:22:21 +00:00
|
|
|
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);
|
2008-02-07 19:06:12 +00:00
|
|
|
delete db;
|
|
|
|
#if USE_ENV
|
2007-12-18 16:22:21 +00:00
|
|
|
if (env) {
|
|
|
|
r = env->close(0); assert(r == 0);
|
|
|
|
delete env;
|
|
|
|
}
|
2008-02-07 19:06:12 +00:00
|
|
|
#endif
|
2007-12-18 16:22:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usage() {
|
2008-02-05 19:07:46 +00:00
|
|
|
printf("db_create [-s DBNAME] [-D] [-S] DBFILE [KEY VAL]*\n");
|
2007-12-18 16:22:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
char *dbname = 0;
|
2008-02-05 19:07:46 +00:00
|
|
|
int dbflags = 0;
|
2007-12-18 16:22:21 +00:00
|
|
|
|
|
|
|
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")) {
|
2008-02-05 19:07:46 +00:00
|
|
|
if (i+1 >= argc)
|
2007-12-18 16:22:21 +00:00
|
|
|
return usage();
|
2008-02-05 19:07:46 +00:00
|
|
|
dbname = argv[++i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (0 == strcmp(arg, "-D")) {
|
|
|
|
dbflags += DB_DUP;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (0 == strcmp(arg, "-S")) {
|
|
|
|
dbflags += DB_DUPSORT;
|
2007-12-18 16:22:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= argc)
|
|
|
|
return usage();
|
|
|
|
char *dbfile = argv[i++];
|
2008-02-05 19:07:46 +00:00
|
|
|
return dbcreate(dbfile, dbname, dbflags, argc-i, &argv[i]);
|
2007-12-18 16:22:21 +00:00
|
|
|
}
|
|
|
|
|