示例#1
0
int mail_cache_db_put(struct mail_cache_db * cache_db,
    const void * key, size_t key_len, const void * value, size_t value_len)
{
#if DBVERS >= 1
  int r;
  DBT db_key;
  DBT db_data;
  DB * dbp;
  
  dbp = cache_db->internal_database;

  memset(&db_key, 0, sizeof(db_key));
  memset(&db_data, 0, sizeof(db_data));
  db_key.data = (void *) key;
  db_key.size = key_len;
  db_data.data = (void *) value;
  db_data.size = value_len;
  
#if DBVERS > 1  
  r = dbp->put(dbp, NULL, &db_key, &db_data, 0);
#elif DBVERS == 1
  r = dbp->put(dbp, &db_key, &db_data, 0);
#else
  r = -1;
#endif
  if (r != 0)
    return -1;
  
  return 0;
#else
  return -1;
#endif
}
示例#2
0
int vdadisk_write (const char *db_name, LogicalSector *sector) {
	DB *base;
	DBT key, data;
	int ret;

	if ((ret = db_create(&base, NULL, 0)) != 0)
		return ret;
	/* Associate DB with a file (create a btree)*/
	//if ((ret = base->open(base, NULL,concat( getGlobalHeap(), 2, db_name, ".db"), NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
	if ((ret = base->open(base, NULL, db_name, NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
		if(isDebugEnabled()) error( "[BDB] Fallo al abrir la base" );
		//base->err(base, ret, "DB open failed\n");
		base->close(base,0);
		return ret;
	}
	loadDBT (&key, &data, sector);
	ret = base->put(base, NULL, &key, &data, DB_NOOVERWRITE);
	if (ret == DB_KEYEXIST) {
		//base->err(base, ret, "La clave %d ya existe!\n");*
		//_itoa_s(sector->sectorId, aux, 10, 10);
		if(isDebugEnabled()) debug(concat( getGlobalHeap() , 2 , "[BDB] Ya existe el sector ", itoa_buf(sector->sectorId)));
		if ((ret = base->del(base, NULL, &key, 0)) == 0 ){  /* Si existe lo borro y lo vuelvo a escribir */
			if(isDebugEnabled()) debug(concat( getGlobalHeap() , 2 , "[BDB] Reescribiendo el sector ", itoa_buf(sector->sectorId)));
			ret = base->put(base, NULL, &key, &data, DB_NOOVERWRITE);
		}
	}
	base->close(base,0);
	return ret;
}
示例#3
0
文件: b_inmem.c 项目: AdeebNqo/poedit
static void
b_inmem_op_ds(u_int ops, int update)
{
	DB_ENV *dbenv;
	char *letters = "abcdefghijklmnopqrstuvwxuz";
	DB *dbp;
	DBT key, data;
	char *keybuf, *databuf;
	DB_MPOOL_STAT  *gsp;

	DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
	DB_BENCH_ASSERT((databuf = malloc(datasize)) != NULL);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	key.data = keybuf;
	key.size = keysize;
	memset(keybuf, 'a', 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->open(
	    dbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0666) == 0);

	(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);

	if (update) {
		TIMER_START;
		for (; ops > 0; --ops) {
			keybuf[(ops % keysize)] = letters[(ops % 26)];
			DB_BENCH_ASSERT(
			    dbp->put(dbp, NULL, &key, &data, 0) == 0);
		}
		TIMER_STOP;
	} else {
		DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
		TIMER_START;
		for (; ops > 0; --ops)
			DB_BENCH_ASSERT(
			    dbp->get(dbp, NULL, &key, &data, 0) == 0);
		TIMER_STOP;
	}

	if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
		DB_BENCH_ASSERT(gsp->st_cache_miss == 0);

	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
}
示例#4
0
/* perform write command */
int dowrite(char *name, int rnum){
  DB *dbp;
  DBT key, data;
  int i, err, len;
  char buf[RECBUFSIZ];
  if(showprgr) printf("<Writing Test of Hash>\n  name=%s  rnum=%d\n\n", name, rnum);
  /* open a database */
  if(db_create(&dbp, NULL, 0) != 0){
    fprintf(stderr, "db_create failed\n");
    return 1;
  }
  if(dbp->set_pagesize(dbp, SMALL_PAGESIZE) != 0){
    fprintf(stderr, "DB->set_pagesize failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  if(dbp->set_cachesize(dbp, 0, BIG_CACHESIZE, 0) != 0){
    fprintf(stderr, "DB->set_cachesize failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  if(dbp->open(dbp, NULL, name, NULL, DB_HASH, DB_CREATE | DB_TRUNCATE, 00644) != 0){
    fprintf(stderr, "DB->open failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  err = FALSE;
  memset(&key, 0, sizeof(key));
  memset(&data, 0, sizeof(data));
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    /* store a record */
    len = sprintf(buf, "%08d", i);
    key.data = buf;
    key.size = len;
    data.data = buf;
    data.size = len;
    if(dbp->put(dbp, NULL, &key, &data, 0) != 0){
      fprintf(stderr, "DB->put failed\n");
      err = TRUE;
      break;
    }
    /* print progression */
    if(showprgr && rnum > 250 && i % (rnum / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == rnum || i % (rnum / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  /* close the database */
  if(dbp->close(dbp, 0) != 0){
    fprintf(stderr, "DB->close failed\n");
    err = TRUE;
  }
  if(showprgr && !err) printf("ok\n\n");
  return err ? 1 : 0;
}
示例#5
0
int
svn_fs_bdb__open_nodes_table(DB **nodes_p,
                             DB_ENV *env,
                             svn_boolean_t create)
{
  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
  DB *nodes;

  BDB_ERR(svn_fs_bdb__check_version());
  BDB_ERR(db_create(&nodes, env, 0));
  BDB_ERR((nodes->open)(SVN_BDB_OPEN_PARAMS(nodes, NULL),
                        "nodes", 0, DB_BTREE,
                        open_flags, 0666));

  /* Create the `next-key' table entry (use '1' because '0' is
     reserved for the root directory to use). */
  if (create)
  {
    DBT key, value;

    BDB_ERR(nodes->put(nodes, 0,
                       svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
                       svn_fs_base__str_to_dbt(&value, "1"), 0));
  }

  *nodes_p = nodes;
  return 0;
}
int
main()
{
	const char data[] = "abcd";
	DB *db;
	DBT dbt;

	/*
	 * Set _malloc_options to "J" so that all memory obtained from
	 * malloc(3) is iniatialized to 0x5a. See malloc(3) manual page
	 * for additional information.
	 */
	_malloc_options = "J";

	db = dbopen("test.db", O_RDWR | O_CREAT | O_TRUNC, 0644, DB_HASH, NULL);
	if (db == NULL)
		err(1, "dbopen()");

	dbt.data = &data;
	dbt.size = sizeof(data);

	if (db->put(db, &dbt, &dbt, 0) != 0)
		err(1, "db->put()");

	db->close(db);

	return (0);
}
示例#7
0
    void CollectionMap::update_ns(const StringData& ns, const BSONObj &serialized, bool overwrite) {
        if (!Lock::isWriteLocked(ns)) {
            throw RetryWithWriteLock();
        }

        init();
        dassert(allocated()); // cannot update a non-existent metadb

        // Note this ns in the rollback, even though we aren't modifying
        // _collections directly. But we know this operation is part of
        // a scheme to create this namespace or change something about it.
        CollectionMapRollback &rollback = cc().txn().collectionMapRollback();
        rollback.noteNs(ns);

        BSONObj nsobj = BSON("ns" << ns);
        storage::Key sKey(nsobj, NULL);
        DBT ndbt = sKey.dbt();
        DBT ddbt = storage::dbt_make(serialized.objdata(), serialized.objsize());
        DB *db = _metadb->db();
        const int flags = overwrite ? 0 : DB_NOOVERWRITE;
        const int r = db->put(db, cc().txn().db_txn(), &ndbt, &ddbt, flags);
        if (r != 0) {
            storage::handle_ydb_error(r);
        }
    }
示例#8
0
int
svn_fs_bdb__open_strings_table(DB **strings_p,
                               DB_ENV *env,
                               svn_boolean_t create)
{
    const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
    DB *strings;

    BDB_ERR(svn_fs_bdb__check_version());
    BDB_ERR(db_create(&strings, env, 0));

    /* Enable duplicate keys. This allows the data to be spread out across
       multiple records. Note: this must occur before ->open().  */
    BDB_ERR(strings->set_flags(strings, DB_DUP));

    BDB_ERR((strings->open)(SVN_BDB_OPEN_PARAMS(strings, NULL),
                            "strings", 0, DB_BTREE,
                            open_flags, 0666));

    if (create)
    {
        DBT key, value;

        /* Create the `next-key' table entry.  */
        BDB_ERR(strings->put
                (strings, 0,
                 svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
                 svn_fs_base__str_to_dbt(&value, "0"), 0));
    }

    *strings_p = strings;
    return 0;
}
示例#9
0
文件: pctldb.c 项目: noaaport/nbsp
int pctldb_snd(struct pctldb_st *pctldb, void *p, uint32_t size, int *dberror){

  int status;
  DBT key, data;
  uint32_t recno;
  DB* dbp = pctldb->dbp;

  memset(&key, 0 , sizeof(DBT));
  memset(&data, 0 , sizeof(DBT));

  key.data = &recno;
  key.ulen = sizeof(recno);
  key.flags = DB_DBT_USERMEM;
  data.data = p;
  data.size = size;

  if((status = dbp->put(dbp, NULL, &key, &data, DB_APPEND)) == 0)
    ++pctldb->n;
  else{
    *dberror = status;
    status = -1;
  }

  return(status);
}
示例#10
0
文件: db.c 项目: rforge/rberkeley
/* {{{ rberkeley_db_put */
SEXP rberkeley_db_put(SEXP _dbp, SEXP _txnid, SEXP _key, SEXP _data, SEXP _flags)
{
  DB *dbp;
  DBT key, data;
  DB_TXN *txnid;
  u_int32_t flags = INTEGER(_flags)[0];
  int ret;

  memset(&key, 0, sizeof(key));
  memset(&data, 0, sizeof(data));

  key.data = (unsigned char *)RAW(_key);
  key.size = length(_key);
  data.data = (unsigned char *)RAW(_data);
  data.size = length(_data);

  dbp = R_ExternalPtrAddr(_dbp);
  if(R_ExternalPtrTag(_dbp) != RBerkeley_DB || dbp == NULL)
    error("invalid 'db' handle");

  if(!isNull(_txnid)) {
    txnid = R_ExternalPtrAddr(_txnid);
  } else txnid = NULL;

/* Store a key/data pair. */
  if ((ret = dbp->put(dbp, txnid, &key, &data, flags)) == 0) {
    return ScalarInteger(ret);
  } else {
    dbp->err(dbp, ret, "DB->put");
  }
  return R_NilValue;

}
示例#11
0
文件: bucket.c 项目: jgarzik/tabled
static int add_access_user(DB_TXN *txn, const char *bucket, const char *key,
			   const char *user, const char *perms)
{
	DB *acls = tdbrep.tdb.acls;
	int key_len;
	int acl_len;
	struct db_acl_ent *acl;
	struct db_acl_key *acl_key;
	DBT pkey, pval;

	key_len = strlen(key);
	acl_len = sizeof(struct db_acl_ent) + key_len + 1;

	acl = alloca(acl_len);
	memset(acl, 0, acl_len);

	acl_key = (struct db_acl_key *) &acl->bucket;	/* trick */

	strncpy(acl->bucket, bucket, sizeof(acl->bucket));
	strncpy(acl->grantee, user, sizeof(acl->grantee));
	strncpy(acl->perm, perms, sizeof(acl->perm));
	strcpy(acl->key, key);

	memset(&pkey, 0, sizeof(pkey));
	memset(&pval, 0, sizeof(pval));

	pkey.data = acl_key;
	pkey.size = sizeof(struct db_acl_key) + key_len + 1;

	pval.data = acl;
	pval.size = acl_len;

	return acls->put(acls, txn, &pkey, &pval, 0);
}
int
svn_fs_bdb__open_reps_table(DB **reps_p,
                            DB_ENV *env,
                            svn_boolean_t create)
{
  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
  DB *reps;

  BDB_ERR(svn_fs_bdb__check_version());
  BDB_ERR(db_create(&reps, env, 0));
  BDB_ERR((reps->open)(SVN_BDB_OPEN_PARAMS(reps, NULL),
                       "representations", 0, DB_BTREE,
                       open_flags, 0666));

  /* Create the `next-key' table entry.  */
  if (create)
  {
    DBT key, value;

    BDB_ERR(reps->put
            (reps, 0,
             svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
             svn_fs_base__str_to_dbt(&value, "0"), 0));
  }

  *reps_p = reps;
  return 0;
}
示例#13
0
文件: sync_src.c 项目: javelinjs/VDD
void createDB()
{
	DB *dbp = NULL;
	u_int32_t open_flags = DB_CREATE;
	int ret, i, j;

	ret = db_create(&dbp, NULL, 0);
	if (ret) {
		printf("db_create error - %d\n", ret);
		return;
	}

	ret = dbp->open(dbp, NULL, "vdd_sync_db.db", NULL, DB_HASH, open_flags, 0);
	if (ret) {
		printf("dbp->open error - %d.\n", ret);
		return;
	}

	DBT key, data;
	unsigned char a[KEY_SIZE];
	unsigned char b[BLOCK_INDEX_SIZE];

	/*****************  put  ********************/
	for (i = 0; i < 10; i++) {
		printf("inbuild ");
		for (j = 0; j < KEY_SIZE; ++j) {
			a[j] = i;
			printf("%u ", a[j]);
		}
		for (j = 0; j < BLOCK_INDEX_SIZE; ++j) {
			b[j] = 0;
			if (j == 0)
				b[j] = i;
			if (j == 4)
				b[j] = 1;
			printf("%u ", b[j]);
		}
		printf("\n");

		memset(&key, 0, sizeof(DBT));
		key.data = a;
		key.size = KEY_SIZE;

		memset(&data, 0, sizeof(DBT));
		data.data = b;
		data.size = BLOCK_INDEX_SIZE;

		ret = dbp->put(dbp, NULL, &(key), &(data), DB_NOOVERWRITE);
		//ret = dbp->put(dbp, NULL, &key, &data, 0);
		if (ret == DB_KEYEXIST){
			printf("key exsit\n");
		} else if (ret) {
			printf("dbp->put error\n");
		}
		dbp->sync(dbp, 0);//toooooo slow
	}
}
示例#14
0
int main() {
	DB *dbp;
	DB_ENV *dbenv;
	DB_TXN *xid;
	DBT key, data;
	const unsigned int INSERT_NUM = 100;
	char value[22]; /* should be log INSERT_NUM */
	int ret, i, t_ret;

        env_dir_create();
        env_open(&dbenv);

	if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
		exit (1);
	}

	dbenv->txn_begin(dbenv, NULL, &xid, 0);
	if ((ret = dbp->open(dbp,
					xid, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
		dbp->err(dbp, ret, "%s", DATABASE);
		goto err;
	}

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));

	key.size = sizeof(int);
	key.data = malloc(sizeof(int));
	data.data = value;

	for( i = 0; i < INSERT_NUM; i++ ) {
		*((int*)key.data) = i;
		data.size = sizeof(char)*strlen(data.data);
		sprintf(value, "value: %u\n", i);
		dbp->put(dbp, xid, &key, &data, 0);
	}

	xid->commit(xid, 0);
	dbenv->txn_begin(dbenv, NULL, &xid, 0);
	
	for( i = 0; i < INSERT_NUM; i++ ) {
		*((int*)key.data) = i;
		dbp->get(dbp, xid, &key, &data, 0);
		printf("db: %u: key retrieved: data was %s.\n", *((int*)key.data), (char *)data.data);
	}

	xid->abort(xid);

err:    if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
			ret = t_ret; 


	return 0;

}
示例#15
0
extern TDBStatus
tdbVectorPutInNewRecord(TDBVector* vector, TDBUint64 owner)
{
    DBT key;
    DBT data;
    DB* db;
    TDBInt32 length;
    TDBInt32 i;
    char* tmpbuf;
    char* ptr;
    char* endptr;
    int dbstatus;
    if (vector == NULL || vector->recordnum != 0) {
        tdb_ASSERT(0);
        return TDB_FAILURE;
    }
    db = tdbGetRecordDB(vector->base);
    if (db == NULL) {
        tdb_ASSERT(0);
        return TDB_FAILURE;
    }
    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    length = sizeof(TDBUint64) + sizeof(TDBUint8) + sizeof(TDBUint8);
    for (i=0 ; i<vector->numfields ; i++) {
        length += tdbNodeSize(tdbVectorGetInternedNode(vector, i));
    }
    length += 20;               /* Just for general slop. */
    tmpbuf = tdbGrowTmpBuf(vector->base, length);
    if (tmpbuf == NULL) return TDB_FAILURE;
    data.data = tmpbuf;
    ptr = data.data;
    endptr = ptr + length;
    for (i=0 ; i<vector->numfields ; i++) {
        if (tdbPutNode(&ptr, tdbVectorGetInternedNode(vector, i),
                       endptr) != TDB_SUCCESS) return TDB_FAILURE;
    }
    tdbPutUInt8(&ptr, vector->layer, endptr);
    tdbPutUInt8(&ptr, vector->flags, endptr);
    tdbPutUInt64(&ptr, vector->owner, endptr);
    data.size = ptr - tmpbuf;
    if (data.size != length - 20) {
        tdb_ASSERT(0);
        return TDB_FAILURE;
    }
    key.data = &(vector->recordnum);
    key.ulen = sizeof(vector->recordnum);
    key.flags = DB_DBT_USERMEM;
    dbstatus = db->put(db, tdbGetTransaction(vector->base),
                       &key, &data, DB_APPEND);
    if (dbstatus != DB_OK) return TDB_FAILURE;
    tdb_ASSERT(vector->recordnum > 0);
    return TDB_SUCCESS;
}
示例#16
0
文件: server.c 项目: Pakrik/libdb
/* 
 * This function is called by the client.  Here, on Server 1 data is
 * inserted into table 1 and it returns successfully.  On Server 2 data
 * is inserted into table 2, but then it sleeps for 30 seconds then
 * returns, in order to force a timeout and to make sure that the data
 * is rolled back in both databases. 
 */
void
TXN_FUNC(TPSVCINFO *msg)
{
	DBT data;
	DBT key;
	int ret, val, i;
	DB *db;
	const char *name;
#ifdef SERVER1
	i = 0;
#else
	i = 1;
#endif

	db = dbs[i];
	name = db_names[i];

        val = 1;

	memset(&key, 0, sizeof(key));
	key.data = &val;
	key.size = sizeof(val);
	memset(&data, 0, sizeof(data));
	data.data = &val;
	data.size = sizeof(val);

	if (verbose) {
		printf("put: key in %s: %i\n", val, db_names[0]);
		printf("put: data in %s: %i\n", val, db_names[0]);
	}
	if ((ret = db->put(db, NULL, &key, &data, 0)) != 0) {
		if (ret == DB_LOCK_DEADLOCK)
			goto abort;
		fprintf(stderr, "%s: %s: %s->put: %s\n",
		    progname, TXN_STRING, name, 
		    db_strerror(ret));
		goto err;
	}

        /* Sleep for 30 seconds to force a timeout error. */
#ifdef SERVER2
        sleep(30);
#endif

	tpreturn(TPSUCCESS, 0L, 0, 0L, 0);
        if (0) {
abort:		if (verbose)
			printf("%s: %s: abort\n", progname, TXN_STRING);
		tpreturn(TPSUCCESS, 1L, 0, 0L, 0);
	}
	return;
err:
	tpreturn(TPFAIL, 0L, 0, 0L, 0);
}
示例#17
0
int storeAccoutingInfo(int db, AccountingInfo *info)
{
	DB *dbp = radacct_dbp[db];
	DBT key, data;
	char buf[REC_SIZE];
	int ret;

	marshalAccountingInfo(info, buf, sizeof(buf));

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.flags = DB_DBT_MALLOC;

	data.data = buf;
	data.size = data.ulen = sizeof(buf);
	data.flags = DB_DBT_USERMEM;

	#if USE_TXN
	switch((ret = dbp->put(dbp, NULL, &key, &data, DB_AUTO_COMMIT | DB_APPEND)))
	#else
	switch((ret = dbp->put(dbp, NULL, &key, &data, DB_APPEND)))
	#endif
	{
		case DB_LOCK_DEADLOCK:
			NETERROR(MRADC, ("storeAccoutingInfo: deadlock: %s\n", db_strerror(ret)));
			break;

		case 0:
			break;

		default:
			NETERROR(MRADC, ("storeAccoutingInfo: oops: %d\n", ret));
			break;
	}	

	if(key.data) free(key.data);

	return ret;
}
示例#18
0
文件: koef.c 项目: arunanj/mrts-ng
int init_koef_db(DB **kdbp)
{
	FILE *fp;
	DB *dbp;
	DBT key, data;
	struct kdb_key	kdbkey;
	struct kdb_data	kdbdata;

	unsigned int asdu;
	unsigned int ioaf;
	float koefu, koefi;
	int ret;
	char line[LMAX];

	if ( (fp = fopen(KFILE,"r")) == NULL)
		return -1;

	if ( db_create(&dbp, NULL, 0) != 0)
		return -2;

	if ( dbp->open(dbp,NULL,NULL,NULL,DB_BTREE,DB_CREATE,0) != 0 )
		return -3;

	/* Zero out the DBTs before using them. */
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	
	while(fgets(line, LMAX, fp)!=NULL){
		if (sscanf(line,"%u;%x;%f;%f\n",&asdu,&ioaf,&koefu,&koefi) < 1)
			continue;
		kdbkey.asdu = (unsigned short) asdu;
		kdbkey.ioa2 = (unsigned char)  ioaf;
		kdbdata.koef_u = koefu;
		kdbdata.koef_i = koefi;

		key.data = &kdbkey;
		key.size = sizeof(struct kdb_key);

		data.data = &kdbdata;
		data.size = sizeof(struct kdb_data);

		ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
		if (ret != 0) {
			dbp->err(dbp, ret, "Put failed: ");
		}

	}

	fclose(fp);
	*kdbp = dbp;
	return 0;
}
示例#19
0
int main(int argc,char * argv[])
{
	int rc;
	DB_ENV *env;
	DB *dbi;
	DBT key, data;
	DB_TXN *txn;
	DBC *cursor;
	char sval[32], kval[32];

#define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD)
	rc = db_env_create(&env, 0);
	rc = env->open(env, "./testdb", FLAGS, 0664);
	rc = db_create(&dbi, env, 0);
	rc = env->txn_begin(env, NULL, &txn, 0);
	rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664);

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	key.size = sizeof(int);
	key.data = sval;
	data.size = sizeof(sval);
	data.data = sval;

	sprintf(sval, "%03x %d foo bar", 32, 3141592);
	rc = dbi->put(dbi, txn, &key, &data, 0);
	rc = txn->commit(txn, 0);
	if (rc) {
		fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
		goto leave;
	}
	rc = env->txn_begin(env, NULL, &txn, 0);
	rc = dbi->cursor(dbi, txn, &cursor, 0);
	key.flags = DB_DBT_USERMEM;
	key.data = kval;
	key.ulen = sizeof(kval);
	data.flags = DB_DBT_USERMEM;
	data.data = sval;
	data.ulen = sizeof(sval);
	while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
		printf("key: %p %.*s, data: %p %.*s\n",
			key.data,  (int) key.size,  (char *) key.data,
			data.data, (int) data.size, (char *) data.data);
	}
	rc = cursor->c_close(cursor);
	rc = txn->abort(txn);
leave:
	rc = dbi->close(dbi, 0);
	rc = env->close(env, 0);
	return rc;
}
示例#20
0
void config_db(DB_ENV *env)
{
   DB *db;
   DBT key, data;
   u_int8_t i;
   char buf[64];
   
   if (db_create(&db, env, 0) != 0) {
      fprintf(stderr, "database create %s\n", CONFIG_DB_NAME);
      goto exit_1;
   }
   if (db->open(db, NULL, CONFIG_DB_NAME, NULL, DB_BTREE,
            DB_CREATE | DB_EXCL, 0644) != 0) {
      fprintf(stderr, "Failed DB->open: %s\n", CONFIG_DB_NAME);
      goto exit_1;
   }
   
   u_int32_t addr;
   
   for (i = 0; i < 4; i++) {
      memset(&key, 0, sizeof(DBT));
      memset(&data, 0, sizeof(DBT));
      memset(buf, '\0', sizeof(buf));
      
      key.data = &i;
      key.size = sizeof(u_int8_t);
      
      switch (i) {
         case 0: inet_pton(AF_INET, "192.168.10.254", &addr);
                 memcpy(buf, &addr, sizeof(u_int32_t));
            break;
         case 1: inet_pton(AF_INET, "8.8.8.8", &addr);
                 memcpy(buf, &addr, sizeof(u_int32_t));
            break;
         case 2: memcpy(buf, "yahoo.com", sizeof(buf));
            break;
         case 3: memcpy(buf, "eth1", 4);
                 memset(buf + IFNAMSIZ, '0', sizeof(buf) - (IFNAMSIZ + 3));
            break;
      }
      
      data.data = buf;
      data.size = strlen(buf) +1;
      
      db->put(db, NULL, &key, &data, DB_NOOVERWRITE);
   }
    
   exit_1:
   if (db)
      db->close(db, 0);
}
示例#21
0
int uriindex_add (char uri[], unsigned int DocID, unsigned int lastmodified, char subname[]) {
        DB dbpArray;

        DB *dbp = NULL;

        DBT key, data;
	int ret;

	struct uriindexFormat uriindex;


	#ifdef DEBUG
	printf("uriindex_add: subname %s\n",subname);
	#endif

	uriindex_open(&dbp,subname, DB_CREATE);


	//resetter minne
        memset(&key, 0, sizeof(DBT));
        memset(&data, 0, sizeof(DBT));

        //legger inn datane i bdb strukturen
        key.data = uri;
        key.size = strlen(uri);

	uriindex.DocID = DocID;
	uriindex.lastmodified = lastmodified;

        //data.data = &DocID;
        //data.size = sizeof(DocID);

	data.data = &uriindex;
	data.size = sizeof(uriindex);

        //legger til i databasen
        if  ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0) {
                dbp->err(dbp, ret, "DB->put");
		//kan ikke returnere her for da blir den aldr lukket.
        	//return (EXIT_FAILURE);
        }
        



	uriindex_close(&dbp);



}
示例#22
0
int smf_lookup_db4_update(const char *database, const char *key, const char *value) {
    DB *dbp;
    DBT db_key, db_data;
    int ret;

    /* Initialize the structure. This database is not opened in an environment,
     * so the environment pointer is NULL. */
    if ((ret = db_create(&dbp, NULL, 0)) != 0) {
        TRACE(TRACE_ERR, "db_create: %s\n", db_strerror(ret));
        return -1;
    }

    /* set page- and cachesize */
    if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
        printf("db->open: %s\n", db_strerror(ret));
        return(-1);
    }
    if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
        printf("db->open: %s\n", db_strerror(ret));
        return(-1);
    }

    /* open the database */
    ret = dbp->open(dbp, NULL, database, NULL, DB_HASH, DB_CREATE, 0);
    if (ret != 0) {
        TRACE(TRACE_ERR,"DB: %s",db_strerror(ret));
        return -1;
    }

    memset(&db_key, 0, sizeof(DBT));
    db_key.data = (char*)key;
    db_key.size = strlen(key) + 1;
    memset(&db_data, 0, sizeof(DBT));
    db_data.data = (char*)value;
    db_data.size = strlen(value) + 1;

    ret = dbp->put(dbp, NULL, &db_key, &db_data, 0);
    if (ret == 0) {
        TRACE(TRACE_DEBUG, "[%p] update was successful [%s: %s]", dbp, key, value);
    } else {
        TRACE(TRACE_ERR, "[%p] failed to update database [%s: %s]: %s",
              dbp, key, value, db_strerror(ret));
    }

    if (dbp != NULL)
        dbp->close(dbp, 0);

    return (ret == 0) ? ret : -1;
}
示例#23
0
文件: bdb.c 项目: rmdir/sendmailornot
void
bdb_update_mail_count(const char *dbpath, uid_t uid, int mailcount)
{
	DBT key, data;
	DB *map = _bdb_open_database(dbpath);
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	key.data = &uid;
	key.size = sizeof(uid_t);
	data.data = &mailcount;
	data.size = sizeof(int);
	if(map->put(map, NULL, &key, &data, 0) != 0)
		err(1,"PUT error");
	map->close(map, 0); 
}
示例#24
0
void
write_db1(TPSVCINFO *msg)
{
	int ret, commit, key_value, data_value;
	DBT key, data;
	DB *db;

	db = dbs[0];
	memset(&key, 0, sizeof key);
	memset(&data, 0, sizeof data);
	commit = 1;

	key_value = data_value = KEY_VALUE;
	data.data = &data_value;
	data.size = sizeof(data_value);
	key.data = &key_value;
	key.size = sizeof(key_value);

	/* Insert data into the tables. */
	if (verbose) {
		printf("put: key: %i ", key_value);
		printf("put: data: %i\n", data_value);
	}
	/* Insert data into the tables. */
	if ((ret = db->put(db, NULL, &key, 
	    &data, 0)) != 0) {
		if (ret == DB_LOCK_DEADLOCK || 
		    ret == DB_LOCK_NOTGRANTED)
			goto abort;
		fprintf(stderr, "%s: %s: %s->put: %s\n",
		    progname, WRITE_STRING, db_names[0], 
		    db_strerror(ret));
		goto err;
	}

	/* Returns a commit or abort command to the client. */
	if (verbose)
		printf("%s: %s: commit\n", progname, WRITE_STRING);
	tpreturn(TPSUCCESS, 0L, 0, 0L, 0);
	if (0) {
abort:		if (verbose)
			printf("%s: %s: abort\n", progname, WRITE_STRING);
		tpreturn(TPSUCCESS, 1L, 0, 0L, 0);
	}
	return;

err:	tpreturn(TPFAIL, 1L, 0, 0L, 0);
}
示例#25
0
acceptor_record*
storage_save_prepare(struct storage* s, prepare_req* pr)
{
	int flags, result;
	DBT dbkey, dbdata;
	DB* dbp = s->db;
	DB_TXN* txn = s->txn;

	// Query the database, check if a previous record exists
	acceptor_record* record_buffer = storage_get_record(s,pr->iid);
	if (record_buffer == NULL) {
		// No record exists, make a new one
		record_buffer = malloc(ACCEPT_RECORD_BUFF_SIZE(0));
		assert(record_buffer != NULL);

		record_buffer->acceptor_id = s->acceptor_id;
		record_buffer->iid = pr->iid;
		record_buffer->value_ballot = 0;
		record_buffer->is_final = 0;
		record_buffer->value_size = 0;
	}
	// Always update the ballot
	record_buffer->ballot = pr->ballot;
    
	memset(&dbkey, 0, sizeof(DBT));
	memset(&dbdata, 0, sizeof(DBT));

	//Key is iid
	dbkey.data = &pr->iid;
	dbkey.size = sizeof(iid_t);
        
	//Data is our buffer
	dbdata.data = record_buffer;
	dbdata.size = ACCEPT_RECORD_BUFF_SIZE(record_buffer->value_size);
    
	//Store permanently
	flags = 0;
	result = dbp->put(dbp, 
		txn, 
		&dbkey, 
		&dbdata, 
		0);
        
	assert(result == 0);
	return record_buffer;	
}
示例#26
0
TEST(DB, read_and_write) {
    Options opts;
    opts.dir = create_ram_directory();
    opts.comparator = new NumericComparator<uint64_t>();
    opts.inner_node_page_size = 4 * 1024;
    opts.inner_node_children_number = 64;
    opts.leaf_node_page_size = 4 * 1024;
    opts.cache_limit = 32 * 1024;

    DB *db = DB::open("test_db", opts);
    EXPECT_TRUE(db != NULL);

    for (uint64_t i = 0; i < 100000; i++ ) {
        char buf[16] = {0};
        sprintf(buf, "%ld", i);
        Slice key = Slice((char*)&i, sizeof(uint64_t));
        Slice value = Slice(buf, strlen(buf));
        ASSERT_TRUE(db->put(key, value)) << "put key " << i << " error";
        if (i % 10000 == 0) {
            cout << "write " << i << " records" << endl;
        }
    }

    db->flush();
    db->debug_print(cout);

    for (uint64_t i = 0; i < 100000; i++ ) {
        Slice key = Slice((char*)&i, sizeof(uint64_t));
        Slice value;
        ASSERT_TRUE(db->get(key, value)) << "get key " << i << " error";

        char buf[16] = {0};
        sprintf(buf, "%ld", i);
        ASSERT_EQ(value.size(), strlen(buf)) << "get key " << i << " value size unequal" ;
        ASSERT_TRUE(strncmp(buf, value.data(), value.size()) == 0) << "get key " << i << " value data unequal";
        value.destroy();

        if (i % 10000 == 0) {
            cout << "read " << i << " records" << endl;
        }
    }

    delete db;
    delete opts.dir;
    delete opts.comparator;
}
示例#27
0
文件: db3.c 项目: xrg/RPM
static int db3cput(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
		unsigned int flags)
{
    DB * db = dbi->dbi_db;
    int rc;

    assert(db != NULL);
    if (dbcursor == NULL) {
	rc = db->put(db, dbi->dbi_txnid, key, data, 0);
	rc = cvtdberr(dbi, "db->put", rc, _debug);
    } else {
	rc = dbcursor->c_put(dbcursor, key, data, DB_KEYLAST);
	rc = cvtdberr(dbi, "dbcursor->c_put", rc, _debug);
    }

    return rc;
}
示例#28
0
vsdb_ret_t vsdb_set(vsdb_t vsdb, const char *key, size_t key_length,
                                 const void *value, size_t value_size)
{
  DB *db;
  DBT kt, dt;
  int ret;

  if ((db = getdb(vsdb)) == NULL)
    goto failed;
  if (key == NULL)
    goto failed;
  if (key_length == SIZE_T_MAX)
    key_length = strlen(key);
  if (key_length == 0)
    goto failed;

  kt.data = (void *)key;
  kt.size = key_length;

  if (value != NULL) {
    dt.data = (void *)value;
    dt.size = value_size;

    lockdb(vsdb);
    ret = db->put(db, &kt, &dt, 0);
    unlockdb(vsdb);

    if (ret != 0) {
      goto failed;
    }
  }
  else {
    lockdb(vsdb);
    ret = db->del(db, &kt, 0);
    unlockdb(vsdb);

    if (ret != 0) {
      goto failed;
    }
  }

  return vsdb_okay;

failed:
  return vsdb_failed;
}
示例#29
0
int Db::put(DbTxn *txnid, Dbt *key, Dbt *value, u_int32_t flags)
{
	int err;
	DB *db = unwrap(this);

	if ((err = db->put(db, unwrap(txnid), key, value, flags)) != 0) {

		// DB_KEYEXIST is a "normal" return, so should not be
		// thrown as an error
		//
		if (err != DB_KEYEXIST) {
			DB_ERROR("Db::put", err, error_policy());
			return (err);
		}
	}
	return (err);
}
示例#30
0
int main() {
  DB *db;
  DB *uiddb;
  int ret;

  db_create(&db, NULL, 0);
  db->open(db, NULL, NULL, "data", DB_BTREE, DB_CREATE, 0);

  db_create(&uiddb, NULL, 0);
  uiddb->open(uiddb, NULL, NULL, "byuid", DB_BTREE, DB_CREATE, 0);

  ret = db->associate(db, NULL, uiddb, byuid_callback, 0);
  printf("associate: %d\n", ret);

  user_t u;
  strcpy(u.user, "jls");
  u.uid = 1000;
  strcpy(u.path, "/home/jls");

  DBT key, value;
  memset(&key, 0, sizeof(DBT));
  memset(&value, 0, sizeof(DBT));
  key.data = u.user;
  key.size = strlen(u.user);

  value.data = &u;
  value.size = sizeof(u);

  ret = db->put(db, NULL, &key, &value, 0);
  printf("put: %d\n", ret);

  memset(&value, 0, sizeof(value));
  ret = db->get(db, NULL, &key, &value, 0);
  printf("put: %d\n", ret);
  printf("Uid: %d\n", ((user_t*)value.data)->uid);

  memset(&value, 0, sizeof(value));
  int uid = 1000;
  key.data = &uid;
  key.size = sizeof(int);

  uiddb->get(uiddb, NULL, &key, &value, 0);
  printf("Secondary lookup: user=%s\n", ((user_t*)value.data)->user);

  return 0;
}