本文整理汇总了C++中DB_ENV类的典型用法代码示例。如果您正苦于以下问题:C++ DB_ENV类的具体用法?C++ DB_ENV怎么用?C++ DB_ENV使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DB_ENV类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: writer_thread
/*
* A function that performs a series of writes to a
* Berkeley DB database. The information written
* to the database is largely nonsensical, but the
* mechanism of transactional commit/abort and
* deadlock detection is illustrated here.
*/
void *
writer_thread(void *args)
{
DB *dbp;
DB_ENV *envp;
DBT key, value;
DB_TXN *txn;
int i, j, payload, ret, thread_num;
int retry_count, max_retries = 20; /* Max retry on a deadlock */
char *key_strings[] = {"key 1", "key 2", "key 3", "key 4",
"key 5", "key 6", "key 7", "key 8",
"key 9", "key 10"};
dbp = (DB *)args;
envp = dbp->get_env(dbp);
/* Get the thread number */
(void)mutex_lock(&thread_num_lock);
global_thread_num++;
thread_num = global_thread_num;
(void)mutex_unlock(&thread_num_lock);
/* Initialize the random number generator */
srand(thread_num);
/* Write 50 times and then quit */
for (i = 0; i < 50; i++) {
retry_count = 0; /* Used for deadlock retries */
/*
* Some think it is bad form to loop with a goto statement, but
* we do it anyway because it is the simplest and clearest way
* to achieve our abort/retry operation.
*/
retry:
/* Begin our transaction. We group multiple writes in
* this thread under a single transaction so as to
* (1) show that you can atomically perform multiple writes
* at a time, and (2) to increase the chances of a
* deadlock occurring so that we can observe our
* deadlock detection at work.
*
* Normally we would want to avoid the potential for deadlocks,
* so for this workload the correct thing would be to perform our
* puts with autocommit. But that would excessively simplify our
* example, so we do the "wrong" thing here instead.
*/
ret = envp->txn_begin(envp, NULL, &txn, 0);
if (ret != 0) {
envp->err(envp, ret, "txn_begin failed");
return ((void *)EXIT_FAILURE);
}
for (j = 0; j < 10; j++) {
/* Set up our key and values DBTs */
memset(&key, 0, sizeof(DBT));
key.data = key_strings[j];
key.size = (u_int32_t)strlen(key_strings[j]) + 1;
memset(&value, 0, sizeof(DBT));
payload = rand() + i;
value.data = &payload;
value.size = sizeof(int);
/* Perform the database put. */
switch (ret = dbp->put(dbp, txn, &key, &value, 0)) {
case 0:
break;
/*
* Our database is configured for sorted duplicates,
* so there is a potential for a KEYEXIST error return.
* If we get one, simply ignore it and continue on.
*
* Note that you will see KEYEXIST errors only after you
* have run this program at least once.
*/
case DB_KEYEXIST:
printf("Got keyexists.\n");
break;
/*
* Here's where we perform deadlock detection. If
* DB_LOCK_DEADLOCK is returned by the put operation,
* then this thread has been chosen to break a deadlock.
* It must abort its operation, and optionally retry the
* put.
*/
case DB_LOCK_DEADLOCK:
/*
* First thing that we MUST do is abort the
* transaction.
*/
(void)txn->abort(txn);
/*
//.........这里部分代码省略.........
示例2: op_ds_bulk
static void
op_ds_bulk(u_int ops, u_int *totalp)
{
DB_ENV *dbenv;
DB *dbp;
DBC *dbc;
DBT key, data;
u_int32_t len, klen;
u_int i, total;
char *keybuf, *databuf;
void *pointer, *dp, *kp;
DB_MPOOL_STAT *gsp;
DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
DB_BENCH_ASSERT((databuf = malloc(bulkbufsize)) != NULL);
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.data = keybuf;
key.size = keysize;
data.data = databuf;
data.size = datasize;
memset(databuf, 'b', datasize);
DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
dbenv = dbp->dbenv;
dbp->set_errfile(dbp, stderr);
DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 1) == 0);
DB_BENCH_ASSERT(
dbp->open(dbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
for (i = 1; i <= numitems; ++i) {
(void)snprintf(keybuf, keysize, "%7d", i);
DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
}
#if 0
fp = fopen("before", "w");
dbp->set_msgfile(dbp, fp);
DB_BENCH_ASSERT (dbp->stat_print(dbp, DB_STAT_ALL) == 0);
#endif
DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0);
data.ulen = bulkbufsize;
data.flags = DB_DBT_USERMEM;
(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);
TIMER_START;
for (total = 0; ops > 0; --ops) {
DB_BENCH_ASSERT(dbc->c_get(
dbc, &key, &data, DB_FIRST | DB_MULTIPLE_KEY) == 0);
DB_MULTIPLE_INIT(pointer, &data);
while (pointer != NULL) {
DB_MULTIPLE_KEY_NEXT(pointer, &data, kp, klen, dp, len);
if (kp != NULL)
++total;
}
}
TIMER_STOP;
*totalp = total;
if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
DB_BENCH_ASSERT(gsp->st_cache_miss == 0);
#if 0
fp = fopen("before", "w");
dbp->set_msgfile(dbp, fp);
DB_BENCH_ASSERT (dbp->stat_print(dbp, DB_STAT_ALL) == 0);
#endif
DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
COMPQUIET(dp, NULL);
COMPQUIET(klen, 0);
COMPQUIET(len, 0);
}
示例3: bdb_init_tx_handle
static int
bdb_init_tx_handle(struct storage* s, char* db_env_path)
{
int result;
DB_ENV* dbenv;
//Create environment handle
result = db_env_create(&dbenv, 0);
if (result != 0) {
paxos_log_error("DB_ENV creation failed: %s", db_strerror(result));
return -1;
}
//Durability mode
if (!paxos_config.bdb_sync)
result = dbenv->set_flags(dbenv, DB_TXN_WRITE_NOSYNC, 1);
if (result != 0) {
paxos_log_error("DB_ENV set_flags failed: %s", db_strerror(result));
return -1;
}
//Redirect errors to sdout
dbenv->set_errfile(dbenv, stdout);
//Set the size of the memory cache
result = dbenv->set_cachesize(dbenv, 0, paxos_config.bdb_cachesize, 1);
if (result != 0) {
paxos_log_error("DB_ENV set_cachesize failed: %s",
db_strerror(result));
return -1;
}
//TODO see page size impact
//Set page size for this db
// result = dbp->set_pagesize(dbp, pagesize);
// assert(result == 0);
//FIXME set log size
// Environment open flags
int flags;
flags =
DB_CREATE | /* Create if not existing */
DB_RECOVER | /* Run normal recovery. */
DB_INIT_LOCK | /* Initialize the locking subsystem */
DB_INIT_LOG | /* Initialize the logging subsystem */
DB_INIT_TXN | /* Initialize the transactional subsystem. */
DB_THREAD | /* Cause the environment to be free-threaded */
DB_REGISTER |
DB_INIT_MPOOL; /* Initialize the memory pool (in-memory cache) */
//Open the DB environment
result = dbenv->open(dbenv,
db_env_path, /* Environment directory */
flags, /* Open flags */
0); /* Default file permissions */
if (result != 0) {
paxos_log_error("DB_ENV open failed: %s", db_strerror(result));
return -1;
}
paxos_log_info("Berkeley DB storage opened successfully");
s->env = dbenv;
return 0;
}
示例4: db_init
static int db_init(dbiIndex dbi, const char * dbhome,
const char * dbfile,
const char * dbsubfile,
DB_ENV ** dbenvp)
{
rpmdb rpmdb = dbi->dbi_rpmdb;
DB_ENV *dbenv = NULL;
int eflags;
int rc;
if (dbenvp == NULL)
return 1;
/* XXX HACK */
if (rpmdb->db_errfile == NULL)
rpmdb->db_errfile = stderr;
eflags = (dbi->dbi_oeflags | dbi->dbi_eflags);
if (eflags & DB_JOINENV) eflags &= DB_JOINENV;
if (dbfile) {
char *dbiflags = prDbiOpenFlags(eflags, 1);
rpmlog(RPMLOG_DEBUG, "opening db environment %s/%s %s\n",
dbhome, dbfile, dbiflags);
free(dbiflags);
}
/* XXX Can't do RPC w/o host. */
if (dbi->dbi_host == NULL)
dbi->dbi_ecflags &= ~DB_CLIENT;
/* XXX Set a default shm_key. */
if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) {
#if defined(HAVE_FTOK)
dbi->dbi_shmkey = ftok(dbhome, 0);
#else
dbi->dbi_shmkey = 0x44631380;
#endif
}
rc = db_env_create(&dbenv, dbi->dbi_ecflags);
rc = cvtdberr(dbi, "db_env_create", rc, _debug);
if (dbenv == NULL || rc)
goto errxit;
{ int xx;
/* 4.1: dbenv->set_app_dispatch(???) */
/* 4.1: dbenv->set_alloc(???) */
/* 4.1: dbenv->set_data_dir(???) */
/* 4.1: dbenv->set_encrypt(???) */
dbenv->set_errcall(dbenv, (void *) rpmdb->db_errcall);
dbenv->set_errfile(dbenv, rpmdb->db_errfile);
dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
/* 4.1: dbenv->set_feedback(???) */
/* 4.1: dbenv->set_flags(???) */
/* dbenv->set_paniccall(???) */
#if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5)
/*
* These enable automatic stale lock removal.
* thread_count 8 is some kind of "magic minimum" value...
*/
dbenv->set_thread_count(dbenv, 8);
dbenv->set_isalive(dbenv, db3isalive);
#endif
if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) {
const char * home;
int retry = 0;
if ((home = strrchr(dbhome, '/')) != NULL)
dbhome = ++home;
while (retry++ < 5) {
xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host,
dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
if (!xx)
break;
(void) sleep(15);
}
} else {
#if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
(dbi->dbi_verbose & DB_VERB_CHKPOINT));
#endif
xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
(dbi->dbi_verbose & DB_VERB_DEADLOCK));
xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
(dbi->dbi_verbose & DB_VERB_RECOVERY));
xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
(dbi->dbi_verbose & DB_VERB_WAITSFOR));
if (dbi->dbi_mmapsize) {
xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mmapsize);
xx = cvtdberr(dbi, "dbenv->set_mp_mmapsize", xx, _debug);
//.........这里部分代码省略.........
示例5: b_txn
int
b_txn(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
DB_ENV *dbenv;
DB_TXN *txn;
int tabort, ch, i, count;
count = 1000;
tabort = 0;
while ((ch = getopt(argc, argv, "ac:")) != EOF)
switch (ch) {
case 'a':
tabort = 1;
break;
case 'c':
count = atoi(optarg);
break;
case '?':
default:
return (usage());
}
argc -= optind;
argv += optind;
if (argc != 0)
return (usage());
/* Create the environment. */
DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
dbenv->set_errfile(dbenv, stderr);
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
NULL, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE, 0666) == 0);
#else
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE, 0666) == 0);
#endif
/* Start and commit/abort a transaction count times. */
TIMER_START;
if (tabort)
for (i = 0; i < count; ++i) {
#if DB_VERSION_MAJOR < 4
DB_BENCH_ASSERT(txn_begin(dbenv, NULL, &txn, 0) == 0);
DB_BENCH_ASSERT(txn_abort(txn) == 0);
#else
DB_BENCH_ASSERT(
dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
DB_BENCH_ASSERT(txn->abort(txn) == 0);
#endif
}
else
for (i = 0; i < count; ++i) {
#if DB_VERSION_MAJOR < 4
DB_BENCH_ASSERT(txn_begin(dbenv, NULL, &txn, 0) == 0);
DB_BENCH_ASSERT(txn_commit(txn, 0) == 0);
#else
DB_BENCH_ASSERT(
dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
#endif
}
TIMER_STOP;
printf("# %d empty transaction start/%s pairs\n",
count, tabort ? "abort" : "commit");
TIMER_DISPLAY(count);
DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
return (0);
}
示例6: insert_db
static void insert_db(const char *file, const uint8_t type)
{
DB *db = NULL;
DBT key, data;
DB_ENV *env;
union {
IP_LIST ip_list;
DOMAIN_LIST dom_list;
} u;
u_int32_t i, j;
char buf[350];
FILE *fd = NULL;
memset(buf, '\0', sizeof(buf));
fd = fopen(file, "r");
if ((i = db_env_create(&env, 0)) != 0) {
fprintf(stderr, "db_input: %s\n", db_strerror(i));
return;
}
if ((i = env->open(env, DB_ENVIRONMENT,
DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_THREAD, 0)) != 0) {
fprintf(stderr, "environment open: %s\n", DB_ENVIRONMENT);
goto err;
}
if ((i = db_create(&db, env, 0)) != 0) {
fprintf(stderr, "database create\n");
goto err;
}
if (!strcmp(file, "ip_list"))
strncpy(buf, IP_DB_NAME, 32);
else
strncpy(buf, DOMAIN_DB_NAME, 32);
if ((i = db->open(db, NULL, buf, NULL, DB_BTREE,
DB_CREATE | DB_THREAD, 0644)) != 0) {
fprintf(stderr, "DB->open: %s\n", buf);
goto err;
}
uint32_t idnum = 0;
//get idnum
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
if (!strcmp(file, "dom_list")) {
key.data = ID_NUM;
key.size = 2;
data.data = &idnum;
data.ulen = sizeof(uint32_t);
data.flags = DB_DBT_USERMEM;
i = db->get(db, NULL, &key, &data, 0);
if (i != 0)
idnum = 0;
else
idnum++;
}
while (fgets(buf, 350, fd)) {
j = strlen(buf);
memset(buf +(j-1), '\0', 1);
if (!strcmp(file, "ip_list")) {
memset(&u.ip_list, 0, sizeof(IP_LIST));
u.ip_list.id = 0;
u.ip_list.type = type;
u.ip_list.flag = DB_ENABLE | DB_WHOLE_MATCH;
} else {
memset(&u.dom_list, 0, sizeof(DOMAIN_LIST));
idnum++;
u.dom_list.id = idnum;
u.dom_list.ttl = time(NULL);
u.dom_list.type = type;
u.dom_list.flag = DB_ENABLE | DB_WHOLE_MATCH;
}
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
if (!strcmp(file, "ip_list")) {
u_int32_t addr;
if (inet_pton(AF_INET, buf, &addr)) {
key.data = &addr;
key.size = sizeof(uint32_t);
data.data = &u.ip_list;
data.size = sizeof(IP_LIST);
}
} else {
mirror(buf);
key.data = buf;
key.size = j;
//.........这里部分代码省略.........
示例7: TestDbPreOpenSetterAndGetter
int TestDbPreOpenSetterAndGetter(CuTest *ct) {
DB_ENV *dbenv;
DB *db, *env_db, *hash_db, *heap_db, *queue_db, *recno_db;
const char **part_dirs;
u_int32_t encrypt_flags, heap_set_bytes, heap_set_gbytes,
heap_get_bytes, heap_get_gbytes;
int nowait, onoff;
/* ================ General and Btree Configuration =============== */
CuAssert(ct, "db_create", create_db_handle(&db, NULL) == 0);
/*
* Test DB->set_cachesize(), DB->get_cachesize().
* We use specific values to avoid adjustment.
*/
CHECK_3_DIGIT_VALUES(db, set_cachesize, get_cachesize,
u_int32_t, 3, u_int32_t, 1048576, int, 5);
/* Test DB->set_encrypt(), DB->get_encrypt_flags(). */
CuAssert(ct, "db->set_encrypt",
db->set_encrypt(db, passwd, DB_ENCRYPT_AES) == 0);
CuAssert(ct, "db->get_encrypt_flags",
db->get_encrypt_flags(db, &encrypt_flags) == 0);
CuAssert(ct, "check encrypt flags", encrypt_flags == DB_ENCRYPT_AES);
/* Test DB->set_errfile(), DB->get_errfile(). */
CHECK_1_PTR_VALUE_VOID(db, set_errfile, get_errfile, FILE, errfile);
/* Test DB->set_errpfx(), DB->get_errpfx().*/
CHECK_1_STR_VALUE_VOID(db, set_errpfx, get_errpfx, "dbp1");
/* Test DB->set_flags(), DB->get_flags(). */
CHECK_FLAG_VALUE(db, set_flags, get_flags,
u_int32_t, DB_CHKSUM | DB_RECNUM | DB_REVSPLITOFF);
/* Test DB->set_lk_exclusive(), DB->get_lk_exclusive(). */
CuAssert(ct, "db->set_lk_exclusive", db->set_lk_exclusive(db, 1) == 0);
CuAssert(ct, "db->get_lk_exclusive",
db->get_lk_exclusive(db, &onoff, &nowait) == 0);
CuAssert(ct, "check lk_exclusive onoff", onoff == 1);
CuAssert(ct, "check lk_exclusive nowait", nowait == 1);
/*
* Test DB->set_lorder(), DB->get_lorder().
* The only acceptable values are 1234 and 4321.
*/
CHECK_1_DIGIT_VALUE(db, set_lorder, get_lorder, int, 1234);
CHECK_1_DIGIT_VALUE(db, set_lorder, get_lorder, int, 4321);
/* Test DB->set_msgfile(), DB->get_msgfile(). */
CHECK_1_PTR_VALUE_VOID(db, set_msgfile, get_msgfile, FILE, msgfile);
/*
* Test DB->set_pagesize(), DB->get_pagesize().
* The pagesize should be 512-55536, and be power of two.
*/
CHECK_1_DIGIT_VALUE(db, set_pagesize, get_pagesize, u_int32_t, 512);
CHECK_1_DIGIT_VALUE(db, set_pagesize, get_pagesize, u_int32_t, 65536);
/*
* Test DB->set_bt_minkey(), DB->get_bt_minkey().
* The minkey value should be 2 at least.
*/
CHECK_1_DIGIT_VALUE(db, set_bt_minkey, get_bt_minkey, u_int32_t, 17);
CuAssert(ct, "db->close", close_db_handle(db) == 0);
/* =================== Recno-only Configuration ===================== */
CuAssert(ct, "db_create", create_db_handle(&recno_db, NULL) == 0);
/* Test DB->set_flags(), DB->get_flags(). */
CHECK_FLAG_VALUE(recno_db, set_flags, get_flags,
u_int32_t, DB_RENUMBER | DB_SNAPSHOT);
/* Test DB->set_re_delim(), DB->get_re_delim(). */
CHECK_1_DIGIT_VALUE(recno_db, set_re_delim, get_re_delim,
int, rand());
/* Test DB->set_re_len(), DB->get_re_len(). */
CHECK_1_DIGIT_VALUE(recno_db, set_re_len, get_re_len,
u_int32_t, rand());
/* Test DB->set_re_pad(), DB->get_re_pad(). */
CHECK_1_DIGIT_VALUE(recno_db, set_re_pad, get_re_pad, int, rand());
/* Test DB->set_re_source(), DB->get_re_source(). */
CHECK_1_STR_VALUE(recno_db, set_re_source, get_re_source, "re_source1");
CuAssert(ct, "recno_db->close", close_db_handle(recno_db) == 0);
/* ==================== Hash-only Configuration ===================== */
CuAssert(ct, "db_create", create_db_handle(&hash_db, NULL) == 0);
/* Test DB->set_flags(), DB->get_flags(). */
CHECK_FLAG_VALUE(hash_db, set_flags, get_flags,
u_int32_t, DB_DUP | DB_DUPSORT | DB_REVSPLITOFF);
//.........这里部分代码省略.........
示例8: main
int main(int argc, char **argv)
{
std::string path2DbEnv;
std::string theContainer = "namespaceExampleData.dbxml";
for ( int i=1; i<argc; i++ )
{
if ( argv[i][0] == '-' )
{
switch(argv[i][1])
{
case 'h':
path2DbEnv = argv[++i];
break;
default:
usage();
}
}
}
if (! path2DbEnv.length() )
usage();
// Berkeley DB environment flags
u_int32_t envFlags = DB_RECOVER|DB_CREATE|DB_INIT_MPOOL|
DB_INIT_LOCK|DB_INIT_TXN|DB_INIT_LOG;
// Berkeley DB cache size (64 MB). The default is quite small
u_int32_t envCacheSize = 64*1024*1024;
// Create and open a Berkeley DB Transactional Environment.
int dberr;
DB_ENV *dbEnv = 0;
dberr = db_env_create(&dbEnv, 0);
if (dberr == 0) {
dbEnv->set_cachesize(dbEnv, 0, envCacheSize, 1);
dberr = dbEnv->open(dbEnv, path2DbEnv.c_str(), envFlags, 0);
}
if (dberr) {
std::cout << "Unable to create environment handle due to the following error: " <<
db_strerror(dberr) << std::endl;
if (dbEnv) dbEnv->close(dbEnv, 0);
return -1;
}
//Have the XmlManager adopt the db environment
XmlManager mgr(dbEnv, DBXML_ADOPT_DBENV);
//Configure the container to use transactions
XmlContainerConfig config;
config.setTransactional(true);
//Open a container in the db environment
XmlContainer container = mgr.openContainer(theContainer, config);
//Create a context and declare the namespaces
XmlQueryContext context = mgr.createQueryContext();
context.setNamespace( "fruits", "http://groceryItem.dbxml/fruits");
context.setNamespace( "vegetables", "http://groceryItem.dbxml/vegetables");
context.setNamespace( "desserts", "http://groceryItem.dbxml/desserts");
//create a transaction
XmlTransaction txn = mgr.createTransaction();
//get details on Zulu Nuts
getDetails( txn, mgr, container, "/fruits:item[fn:string(product) = 'Zulu Nut']", context);
//get details on all fruits that start with 'A'
getDetails( txn, mgr, container, "/vegetables:item[starts-with(fn:string(product),'A')]", context);
//commit transaction
txn.commit();
return 0;
}
示例9: load
int DbWrapper::load(std::istream *in, unsigned long *lineno)
{
int version, ret, t_ret;
DBTYPE dbtype;
char *subdb = 0;
u_int32_t read_flags, tflags;
DBT key, data;
db_recno_t recno, datarecno;
DB_ENV *dbenv = environment_;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
if ((ret = __db_rheader(dbenv, db_, &dbtype,
&subdb, &version, &read_flags, read_callback, in, lineno)) != 0)
goto err;
/* We always print with keys */
if (!(read_flags & DB_READ_HASKEYS)) {
db_->errx(db_, "Invalid DbXml dump: keys missing");
ret = EINVAL;
goto err;
}
if ((ret = open(NULL, dbtype, DB_CREATE|DB_EXCL)) != 0)
goto err;
/* Initialize the key/data pair. */
if (dbtype == DB_RECNO || dbtype == DB_QUEUE) {
key.size = sizeof(recno);
key.data = &datarecno;
} else {
key.ulen = 1024;
key.data = (void *)malloc(key.ulen);
}
data.ulen = 1024;
data.data = (void *)malloc(data.ulen);
if (key.data == NULL || data.data == NULL) {
db_->err(db_, ENOMEM, NULL);
goto err;
}
// work around gcc optimizer issue that seems to modify
// read_flags (4.1.1 on 64-bit linux)
tflags = read_flags;
/* Get each key/data pair and add them to the database. */
for (recno = 1;; ++recno) {
if ((ret = __db_rdbt(dbenv, &key, &data,
tflags, read_callback, in, lineno)) != 0) {
if (ret == EOF)
ret = 0;
break;
}
switch (ret = db_->put(db_, NULL, &key, &data, 0)) {
case 0:
break;
case DB_KEYEXIST:
db_->errx(db_, "line %d: key already exists, not loaded:", *lineno);
dbenv->prdbt(&key, tflags & DB_READ_PRINTABLE,
0, &std::cerr, pr_callback, 0);
break;
default:
db_->err(db_, ret, NULL);
goto err;
}
}
err: /* Close the database. */
if ((t_ret = close(0)) != 0 && ret == 0)
ret = t_ret;
/* Free allocated memory. */
if (subdb != NULL)
free(subdb);
if (key.data != NULL && dbtype != DB_RECNO && dbtype != DB_QUEUE)
free(key.data);
if (data.data != NULL)
free(data.data);
return (ret);
}
示例10: open_db
static void open_db(bdb_drv_t* pdrv, ErlIOVec *ev) {
drv_cfg* pcfg;
DB* pdb;
DB_ENV* penv;
u_int32_t open_flags, page_size_bytes, cache_size_bytes, bulk_get_buffer_size_bytes;
int ret;
ErlDrvBinary* data = ev->binv[1];
char *bytes = data->orig_bytes;
int txn_enabled = bytes[1];
int db_type = bytes[2];
cache_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4) ));
page_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4 + 4) ));
bulk_get_buffer_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4 + 4 + 4) ));
char* lv_start = bytes + 4 + 4 + 4 + 4;
char *db_name_len_bytes = lv_start;
char *db_name_bytes = lv_start + 4;
uint32_t db_name_length = (uint32_t) ntohl(* ((uint32_t*) db_name_len_bytes) );
char *data_dir_len_bytes = db_name_bytes + db_name_length;
char *data_dir_bytes = data_dir_len_bytes + 4;
uint32_t data_dir_length = (uint32_t) ntohl(* ((uint32_t*) data_dir_len_bytes) );
pcfg = (drv_cfg*) malloc(sizeof(drv_cfg));
pcfg->buffer = (char*) malloc(bulk_get_buffer_size_bytes);
if (pcfg->buffer == NULL) {
return_error_tuple(pdrv, "Could not allocate memory for operation!");
return;
}
pcfg->penv = NULL;
pcfg->pdb = NULL;
pcfg->txn_enabled = txn_enabled;
pcfg->data_dir = malloc(data_dir_length + 1);
pcfg->db_name = malloc(db_name_length + 1);
pcfg->page_size_bytes = page_size_bytes;
pcfg->data_dir[data_dir_length] = 0;
pcfg->db_name[db_name_length] = 0;
if (db_type == 'B') {
pcfg->db_type = DB_BTREE;
} else {
pcfg->db_type = DB_HASH;
}
memcpy(pcfg->data_dir, data_dir_bytes, data_dir_length);
memcpy(pcfg->db_name, db_name_bytes, db_name_length);
pcfg->bulk_get_buffer_size_bytes = bulk_get_buffer_size_bytes;
ret = db_env_create(&penv, 0);
if (ret != 0) {
return_error_tuple(pdrv, db_strerror(ret));
return;
}
penv->app_private = pcfg;
open_flags = DB_CREATE | DB_INIT_MPOOL;
if (pcfg->txn_enabled) {
open_flags |= DB_INIT_LOCK | DB_THREAD | DB_INIT_TXN | DB_INIT_LOG | DB_REGISTER | DB_RECOVER;
}
//penv->set_msgcall(penv, (FILE*)stderr);
//penv->set_verbose(penv, DB_VERB_DEADLOCK | DB_VERB_RECOVERY, 1);
ret = penv->set_cachesize(penv, 0, cache_size_bytes, 1);
if (ret != 0) {
return_error_tuple(pdrv, db_strerror(ret));
return;
}
ret = penv->open(penv, pcfg->data_dir, open_flags, 0);
if (ret != 0) {
return_error_tuple(pdrv, db_strerror(ret));
return;
}
//Only open the table if not in replication mode.... for rep mode the table will be opened in event handler ...
ret = db_create(&pdb, penv, 0);
if (ret != 0) {
return_error_tuple(pdrv, db_strerror(ret));
return;
}
if (pcfg->db_type == DB_BTREE) {
//.........这里部分代码省略.........
示例11: main
int main(int argc, char **argv)
{
// Deal with command line arguments
const char *path2DbEnv = 0;
u_int32_t envFlags = (DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL);
u_int32_t txnEnvFlags = (DB_INIT_TXN|DB_INIT_LOCK|DB_INIT_LOG);
u_int32_t dbxmlFlags = DBXML_ALLOW_EXTERNAL_ACCESS;
vector<string> scripts;
int verbose = 0;
bool transactionMode = false;
bool dbPrivate = false;
bool envCreate = false;
const char *progName = argv[0];
const char *password = 0;
int cacheSize = 64;
int ch;
int ret = 0;
while ((ch = getopt(argc, argv, "?h:hs:tvxVP:cz:")) != -1) {
switch (ch) {
case 'h': {
path2DbEnv = optarg;
break;
}
case 'z': {
cacheSize = atoi(optarg);
break;
}
case 'c': {
envFlags &= ~DB_PRIVATE;
envCreate = true;
break;
}
case 'x': {
dbxmlFlags &= ~DBXML_ALLOW_EXTERNAL_ACCESS;
break;
}
case 't': {
transactionMode = true;
envFlags |= txnEnvFlags;
break;
}
case 's': {
scripts.push_back(optarg);
break;
}
case 'v': {
++verbose;
break;
}
case 'V': {
printf("%s\n", DbXml::dbxml_version(NULL, NULL, NULL));
printf("%s\n", db_version(NULL, NULL, NULL));
exit(0);
}
case 'P': {
password = optarg;
break;
}
case '?':
default: {
usage(progName, 0);
break;
}
}
}
// Turn on logging if extra verbose is specified
if(verbose > 1) {
setLogLevel(LEVEL_ALL, true);
setLogCategory(CATEGORY_ALL, true);
setLogCategory(CATEGORY_NODESTORE, verbose > 2);
verboseErrors = true;
}
SigBlock sb; // block signals, resend at end of scope
try {
// Create a DB environment, and XmlManager
DB_ENV *dbenv;
int dberr = 0;
dberr = db_env_create(&dbenv, 0);
if (dberr) {
cout << "Error creating environment: " << dberr << endl;
exit(-1);
}
if (password)
dbenv->set_encrypt(dbenv, password, DB_ENCRYPT_AES);
dbenv->set_errcall(dbenv, errcall);
dbenv->set_cachesize(dbenv, 0, cacheSize * 1024 * 1024, 1);
dbenv->set_lk_max_lockers(dbenv, 10000);
dbenv->set_lk_max_locks(dbenv, 10000);
dbenv->set_lk_max_objects(dbenv, 10000);
if (!dbPrivate) {
dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
if (verbose && !envCreate) {
cout <<
"Attempting to join environment: "
<< (path2DbEnv ? path2DbEnv : ".")
<< endl;
}
//.........这里部分代码省略.........
示例12: sqlite3_backup_step
/*
** Copy nPage pages from the source b-tree to the destination.
*/
int sqlite3_backup_step(sqlite3_backup *p, int nPage) {
int returnCode, pages;
Parse parse;
DB_ENV *dbenv;
BtShared *pBtDest, *pBtSrc;
pBtDest = pBtSrc = NULL;
if (p->rc != SQLITE_OK || nPage == 0)
return p->rc;
sqlite3_mutex_enter(p->pSrcDb->mutex);
sqlite3_mutex_enter(p->pDestDb->mutex);
/*
* Make sure the schema has been read in, so the keyInfo
* can be retrieved for the indexes. No-op if already read.
* If the schema has not been read then an update must have
* changed it, so backup will restart.
*/
memset(&parse, 0, sizeof(parse));
parse.db = p->pSrcDb;
p->rc = sqlite3ReadSchema(&parse);
if (p->rc != SQLITE_OK)
goto err;
/*
* This process updated the source database, so
* the backup process has to restart.
*/
if (p->pSrc->updateDuringBackup > p->lastUpdate) {
p->rc = SQLITE_LOCKED;
if ((p->rc = backupCleanup(p)) != SQLITE_OK)
goto err;
else
backupReset(p);
}
pages = nPage;
if (!p->cleaned) {
const char *home;
const char inmem[9] = ":memory:";
int storage;
pBtDest = p->pDest->pBt;
storage = p->pDest->pBt->dbStorage;
if (storage == DB_STORE_NAMED)
p->openDest = 1;
if (strcmp(p->destName, "temp") == 0)
p->pDest->schema = NULL;
else
p->pDestDb->aDb[p->iDb].pSchema = NULL;
p->rc = btreeDeleteEnvironment(p->pDest, p->fullName, 1);
if (storage == DB_STORE_INMEM && strcmp(p->destName, "temp")
!= 0)
home = inmem;
else
home = p->fullName;
if (p->rc != SQLITE_BUSY)
p->pDest = p->pDestDb->aDb[p->iDb].pBt = NULL;
if (p->rc != SQLITE_OK)
goto err;
/*
* Call sqlite3OpenTempDatabase instead of
* sqlite3BtreeOpen, because sqlite3OpenTempDatabase
* automatically chooses the right flags before calling
* sqlite3BtreeOpen.
*/
if (strcmp(p->destName, "temp") == 0) {
memset(&parse, 0, sizeof(parse));
parse.db = p->pDestDb;
p->rc = sqlite3OpenTempDatabase(&parse);
p->pDest = p->pDestDb->aDb[p->iDb].pBt;
if (p->pDest && p->iDb != 1)
p->pDest->schema =
p->pDestDb->aDb[p->iDb].pSchema;
} else {
p->rc = sqlite3BtreeOpen(NULL, home, p->pDestDb,
&p->pDest, SQLITE_DEFAULT_CACHE_SIZE |
SQLITE_OPEN_MAIN_DB, p->pDestDb->openFlags);
p->pDestDb->aDb[p->iDb].pBt = p->pDest;
if (p->pDest) {
p->pDestDb->aDb[p->iDb].pSchema =
sqlite3SchemaGet(p->pDestDb, p->pDest);
if (p->pDestDb->aDb[p->iDb].pSchema == NULL)
p->rc = SQLITE_NOMEM;
}
}
if (p->pDest)
p->pDest->nBackup++;
#ifdef SQLITE_HAS_CODEC
/*
* In the case of a temporary source database, use the
* encryption of the main database.
//.........这里部分代码省略.........
示例13: main
int main(int argc, char **argv)
{
std::string path2DbEnv;
std::string theContainer = "namespaceExampleData.dbxml";
for ( int i=1; i<argc; i++ )
{
if ( argv[i][0] == '-' )
{
switch(argv[i][1])
{
case 'h':
path2DbEnv = argv[++i];
break;
default:
usage();
}
}
}
if (! path2DbEnv.length() )
usage();
// Berkeley DB environment flags
u_int32_t envFlags = DB_RECOVER|DB_CREATE|DB_INIT_MPOOL|
DB_INIT_LOCK|DB_INIT_TXN|DB_INIT_LOG;
// Berkeley DB cache size (64 MB). The default is quite small
u_int32_t envCacheSize = 64*1024*1024;
// Create and open a Berkeley DB Transactional Environment.
int dberr;
DB_ENV *dbEnv = 0;
dberr = db_env_create(&dbEnv, 0);
if (dberr == 0) {
dbEnv->set_cachesize(dbEnv, 0, envCacheSize, 1);
dberr = dbEnv->open(dbEnv, path2DbEnv.c_str(), envFlags, 0);
}
if (dberr) {
std::cout << "Unable to create environment handle due to the following error: " <<
db_strerror(dberr) << std::endl;
if (dbEnv) dbEnv->close(dbEnv, 0);
return -1;
}
//Have the XmlManager adopt the db environment
XmlManager db(dbEnv, DBXML_ADOPT_DBENV);
//Configure the container to use transactions
XmlContainerConfig config;
config.setTransactional(true);
//Open a container in the db environment
XmlContainer container = db.openContainer(theContainer, config);
//Get a transaction
XmlTransaction txn = db.createTransaction();
XmlUpdateContext uc = db.createUpdateContext();
//add an string equality index for the "product" element node.
deleteIndex( container, "", "product", "node-element-equality-string", txn, uc );
//Do these deletes in two different transactions
// for no particular reason.
txn.commit();
txn = db.createTransaction();
//add an edge presence index for the product node
deleteIndex( container, "", "product", "edge-element-presence-none", txn, uc );
txn.commit();
return 0;
}
示例14: main
int
main(int argc, char *argv[])
{
/* Initialize our handles */
DB *dbp = NULL;
DB_ENV *envp = NULL;
thread_t writer_threads[NUMWRITERS];
int ch, i, ret, ret_t;
u_int32_t env_flags;
char *db_home_dir;
/* Application name */
const char *prog_name = "txn_guide";
/* Database file name */
const char *file_name = "mydb.db";
/* Parse the command line arguments */
#ifdef _WIN32
db_home_dir = ".\\";
#else
db_home_dir = "./";
#endif
while ((ch = getopt(argc, argv, "h:")) != EOF)
switch (ch) {
case 'h':
db_home_dir = optarg;
break;
case '?':
default:
return (usage());
}
/* Create the environment */
ret = db_env_create(&envp, 0);
if (ret != 0) {
fprintf(stderr, "Error creating environment handle: %s\n",
db_strerror(ret));
goto err;
}
/*
* Indicate that we want db to perform lock detection internally.
* Also indicate that the transaction with the fewest number of
* write locks will receive the deadlock notification in
* the event of a deadlock.
*/
ret = envp->set_lk_detect(envp, DB_LOCK_MINWRITE);
if (ret != 0) {
fprintf(stderr, "Error setting lock detect: %s\n",
db_strerror(ret));
goto err;
}
env_flags =
DB_CREATE | /* Create the environment if it does not exist */
DB_RECOVER | /* Run normal recovery. */
DB_INIT_LOCK | /* Initialize the locking subsystem */
DB_INIT_LOG | /* Initialize the logging subsystem */
DB_INIT_TXN | /* Initialize the transactional subsystem. This
* also turns on logging. */
DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */
DB_THREAD; /* Cause the environment to be free-threaded */
/* Now actually open the environment */
ret = envp->open(envp, db_home_dir, env_flags, 0);
if (ret != 0) {
fprintf(stderr, "Error opening environment: %s\n",
db_strerror(ret));
goto err;
}
/*
* If we had utility threads (for running checkpoints or
* deadlock detection, for example) we would spawn those
* here. However, for a simple example such as this,
* that is not required.
*/
/* Open the database */
ret = open_db(&dbp, prog_name, file_name,
envp, DB_DUPSORT);
if (ret != 0)
goto err;
/* Initialize a mutex. Used to help provide thread ids. */
(void)mutex_init(&thread_num_lock, NULL);
/* Start the writer threads. */
for (i = 0; i < NUMWRITERS; i++)
(void)thread_create(
&writer_threads[i], NULL, writer_thread, (void *)dbp);
/* Join the writers */
for (i = 0; i < NUMWRITERS; i++)
(void)thread_join(writer_threads[i], NULL);
err:
/* Close our database handle, if it was opened. */
if (dbp != NULL) {
ret_t = dbp->close(dbp, 0);
//.........这里部分代码省略.........
示例15: b_recover
int
b_recover(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
DB *dbp;
DBT key, data;
DB_ENV *dbenv;
DB_TXN *txn;
u_int32_t cachesize;
int ch, i, count;
/*
* Recover was too slow before release 4.0 that it's not worth
* running the test.
*/
#if DB_VERSION_MAJOR < 4
return (0);
#endif
cachesize = MEGABYTE;
count = 1000;
while ((ch = getopt(argc, argv, "C:c:")) != EOF)
switch (ch) {
case 'C':
cachesize = (u_int32_t)atoi(optarg);
break;
case 'c':
count = atoi(optarg);
break;
case '?':
default:
return (usage());
}
argc -= optind;
argv += optind;
if (argc != 0)
return (usage());
/* Create the environment. */
DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
dbenv->set_errfile(dbenv, stderr);
DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0);
#define OFLAGS \
(DB_CREATE | DB_INIT_LOCK | \
DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE)
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, NULL, OFLAGS, 0666) == 0);
#endif
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0);
#endif
#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0);
#endif
/* Create the database. */
DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
DB_BENCH_ASSERT(dbp->open(dbp, NULL,
TESTFILE, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0666) == 0);
#else
DB_BENCH_ASSERT(
dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
#endif
/* Initialize the data. */
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.size = data.size = 20;
key.data = data.data = "01234567890123456789";
/* Start/commit a transaction count times. */
for (i = 0; i < count; ++i) {
#if DB_VERSION_MAJOR < 4
DB_BENCH_ASSERT(
txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0);
DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
DB_BENCH_ASSERT(txn_commit(txn, 0) == 0);
#else
DB_BENCH_ASSERT(
dbenv->txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0);
DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
#endif
}
DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
/* Create a new DB_ENV handle. */
DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
dbenv->set_errfile(dbenv, stderr);
DB_BENCH_ASSERT(
dbenv->set_cachesize(dbenv, 0, 1048576 /* 1MB */, 0) == 0);
/* Now run recovery. */
TIMER_START;
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
DB_BENCH_ASSERT(dbenv->open(
//.........这里部分代码省略.........