Пример #1
0
int mail_cache_db_get(struct mail_cache_db * cache_db,
    const void * key, size_t key_len, void ** pvalue, size_t * pvalue_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;
  
#if DBVERS > 1  
  r = dbp->get(dbp, NULL, &db_key, &db_data, 0);
#elif DBVERS == 1
  r = dbp->get(dbp, &db_key, &db_data, 0);
#else
  r = -1;
#endif
  
  if (r != 0)
    return -1;
  
  * pvalue = db_data.data;
  * pvalue_len = db_data.size;
  
  return 0;
#else
  return -1;
#endif
}
Пример #2
0
int vdadisk_read (const char *db_name, LogicalSector *sector, tSectorId idToSearch) {
	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->get(base, NULL, &key, &data, 0);
	if((ret = base->get(base, NULL, &key, &data, 0)) != 0) {
		if(isDebugEnabled()) 
			debug(concat( getGlobalHeap() , 2 , "[BDB] No se encuentra el sector " ,itoa_buf(sector->sectorId)));
		//base->err(base, ret, "Not found.");
	}
	base->close(base,0);
	return ret;
}
Пример #3
0
int search(context_t context, char*fname, resSet*set, int*len) {
  int 
    ret,
    ix;

  DB*   dbContext = db_getContext(context);

  DBT 
    key, 
    data;

  analSet   anal;
  analTuple  toComp[MAXRES * MAXINDEX];
  cmpString   *list[MAXRES * MAXINDEX];
  
  char *pContext;
  if(context[0]) {
    pContext = context;
  } else {
    pContext = g_lastcontext;
  }

  if(strlen(fname) < 1) {
    return 0;
  }
  memset (&key, 0, sizeof (key));
  memset (&data, 0, sizeof (data));
  
  //compress(fname, comp);
  key.data = fname;
  key.size = strlen(fname);

  // first we grab the file from the table
  if ((ret = dbContext->get (dbContext, NULL, &key, &data, 0)) != 0) {
    return 0;
  }
  memcpy(&anal, (analSet*)data.data, sizeof(analSet));
  // then we execute an index search on the image
  ixGet(&anal, list, len);

  printf ("(db) [%s] getting %d\n", pContext, *len);

  // retrieve the entries from bdb
  for(ix = 0; ix < *len; ix++) {
    key.data = list[ix];
    key.size = strlen(*list[ix]);

    if ((ret = dbContext->get (dbContext, NULL, &key, &data, 0)) != 0) {
      return 0;
    } else {
      memcpy(&toComp[ix].set, (analSet*)data.data, sizeof(analSet));
      toComp[ix].name = list[ix];
    }
  }

  // then do a full analysis over the list
  // and return it
  return match(&anal, toComp, set, *len);
}
Пример #4
0
AccountingInfo *getAccoutingInfo(int db)
{
	DB_ENV *dbenv = radacct_dbenv[db];
	DB *dbp = radacct_dbp[db];
	DB_TXN *tid = NULL;
	DBT key, data;
	db_recno_t recno;
	AccountingInfo *info = NULL;
	char buf[REC_SIZE];
	int ret;

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

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

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

	#if USE_TXN
	if((ret = dbenv->txn_begin(dbenv, NULL, &tid, 0)) != 0)
	{
		NETERROR(MRADC, ("getAccoutingInfo: transaction failed: %s\n", db_strerror(ret)));
		return 0;
	}

	switch((ret = dbp->get(dbp, tid, &key, &data, DB_CONSUME)))
	#else
	switch((ret = dbp->get(dbp, NULL, &key, &data, DB_CONSUME)))
	#endif
	{
		case DB_LOCK_DEADLOCK:
			NETERROR(MRADC, ("getAccoutingInfo: deadlock: %s\n", db_strerror(ret)));
			break;

		case 0:
			if((info = (AccountingInfo*)malloc(sizeof(AccountingInfo))))
			{
				memset(info, 0, sizeof(AccountingInfo));

				info->tid = tid;

				unMarshalAccountingInfo(buf, info);
			}
			break;

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

	return info;
}
Пример #5
0
int pctlmfdb_rcv(struct pctldb_st *pctldb, struct timespec *ts,
		 struct memfile_st *mf, int *dberror){

  int status = 0;
  int dberror_del;
  DBT key, data;
  DB* mfdbp = pctldb->mfdbp;

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

  key.data = ts;
  key.ulen = sizeof(struct timespec);
  key.flags = DB_DBT_USERMEM;
  data.data = mf->p;
  data.ulen = get_memfile_allocated_size(mf);
  data.flags = DB_DBT_USERMEM;
  *dberror = mfdbp->get(mfdbp, NULL, &key, &data, 0);
  if(*dberror == DB_BUFFER_SMALL){
    status = realloc_memfile(mf, data.size);
    if(status == 0){
      data.data = mf->p;
      data.ulen = get_memfile_allocated_size(mf);
      *dberror = mfdbp->get(mfdbp, NULL, &key, &data, 0);
    }else
      *dberror = errno;
  }

  if(*dberror == 0)
    mf->size = data.size;
  else{
    status = -1;
    mf->size = 0; /* Only for convenience in the substraction below */
  }

  dberror_del = mfdbp->del(mfdbp, NULL, &key, 0);

  if(dberror_del == 0){
    if(pctldb->mf_total_size > mf->size)
      pctldb->mf_total_size -= mf->size;
    else
      pctldb->mf_total_size = 0;
  }else{
    if(*dberror == 0){
      status = -1;
      *dberror = dberror_del;
    }
  }

  return(status);
}
Пример #6
0
static int bucket_find(DB_TXN *txn, const char *bucket, char *owner,
		       int owner_len)
{
	DB *buckets = tdbrep.tdb.buckets;
	DBT key, val;
	struct db_bucket_ent ent;
	int rc;

	memset(&key, 0, sizeof(key));
	key.data = (char *) bucket;
	key.size = strlen(bucket) + 1;

	memset(&val, 0, sizeof(val));
	val.data = &ent;
	val.ulen = sizeof(struct db_bucket_ent);
	val.flags = DB_DBT_USERMEM;

	rc = buckets->get(buckets, txn, &key, &val, 0);

	if (rc == 0 && owner != NULL && owner_len > 0) {
		strncpy(owner, ent.owner, owner_len);
		owner[owner_len-1] = 0;
	}

	return rc;
}
Пример #7
0
 int PerconaFTEngine::Get(Context& ctx, const KeyObject& key, ValueObject& value)
 {
     DB* db = GetFTDB(ctx, key.GetNameSpace(), false);
     if (NULL == db)
     {
         return ERR_ENTRY_NOT_EXIST;
     }
     PerconaFTLocalContext& local_ctx = g_local_ctx.GetValue();
     Slice ks = local_ctx.GetSlice(key);
     DBT key_slice = to_dbt(ks);
     DB_TXN* txn = local_ctx.transc.Peek();
     int r = 0;
     DBT val_slice;
     memset(&val_slice, 0, sizeof(DBT));
     val_slice.flags = DB_DBT_MALLOC;
     //CHECK_EXPR(r = m_env->txn_begin(m_env, NULL, &txn, 0));
     CHECK_EXPR(r = db->get(db, txn, &key_slice, &val_slice, 0));
     if (0 == r)
     {
         Buffer valBuffer((char*) (val_slice.data), 0, val_slice.size);
         value.Decode(valBuffer, true);
     }
     //txn->commit(txn, 0);
     return ENGINE_ERR(r);
 }
Пример #8
0
/* {{{ rberkeley_db_get */
SEXP rberkeley_db_get(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);
  if(!isNull(_data)) {
    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;

  ret = dbp->get(dbp, txnid, &key, &data, flags);
  if(ret == 0) {
    SEXP retdata;
    PROTECT(retdata = allocVector(RAWSXP, data.size));
    memcpy(RAW(retdata), data.data, data.size);
    UNPROTECT(1);
    return retdata;
  } else return ScalarInteger(ret); 
}
Пример #9
0
static Info* berkely_registry_get(void *self, const char* filename)
{
  DB *dbp = NULL;
  int ret;

  dbp = get_db(self);
  g_assert(dbp != NULL);

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

  key.data = (void*) filename;
  key.size = strlen(filename) + 1;
  key.flags = DB_DBT_USERMEM;

  if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) {
    g_debug("db: '%s' key retreived", (char*)key.data);
  } else {
    dbp->err(dbp, ret, "DB->get");
    g_warning("DB get failed");
  }

  return bab_info_unmarshall(data.data);
}
Пример #10
0
int load_metadata_logflags(bdb_table_p _tp)
{
	int i, ret;
	char dbuf[MAX_ROW_SIZE];

	DB *db = NULL;
	DBT key, data;
	i = 0;
	
	if(!_tp || !_tp->db)
		return -1;
	
	db = _tp->db;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	memset(dbuf, 0, MAX_ROW_SIZE);
	key.data = METADATA_LOGFLAGS;
	key.size = strlen(METADATA_LOGFLAGS);
	data.data = dbuf;
	data.ulen = MAX_ROW_SIZE;
	data.flags = DB_DBT_USERMEM;
	
	if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) 
	{	return ret;
	}
	
	if( 1 == sscanf(dbuf,"%i", &i) )
		_tp->logflags=i;
	
	return 0;
}
Пример #11
0
int km_load_metadata_readonly(table_p _tp)
{
	int i, ret;
	char dbuf[MAX_ROW_SIZE];

	DB *db = NULL;
	DBT key, data;
	i = 0;
	
	if(!_tp || !_tp->db)
		return -1;
	
	db = _tp->db;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	memset(dbuf, 0, MAX_ROW_SIZE);
	key.data = METADATA_READONLY;
	key.size = strlen(METADATA_READONLY);
	data.data = dbuf;
	data.ulen = MAX_ROW_SIZE;
	data.flags = DB_DBT_USERMEM;
	
	if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) 
	{	return ret;
	}
	
	if( 1 == sscanf(dbuf,"%i", &i) )
		_tp->ro=(i>0)?1:0;
	
	return 0;
}
Пример #12
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;
}
Пример #13
0
/**
 *  Retrieving a concept:
 *  if it's already cached in memory,
 *  return a reference to it,
 *  otherwise check the MindMap DB
 *  and initialize a new concept.
 */
static struct ooConcept* 
ooMindMap_get(struct ooMindMap *self, mindmap_size_t id)
{
    DB *dbp;
    DBT key, data;
    int ret;

    struct ooConcept *conc = NULL;
    char *key_buffer;
    static mindmap_size_t idsize = sizeof(mindmap_size_t);

    if (id > self->num_concepts) return NULL;

    /* check the memory cache */
    conc = (struct ooConcept*)self->concept_index[id];
    if (conc) return conc;

    dbp = self->_storage;

    /* initialize the DBTs */
    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    key_buffer = malloc(idsize);
    if (!key_buffer) return NULL;

    memcpy(key_buffer, &id, idsize);

    /* set the search key with the Concept id */
    key.data = key_buffer;
    key.size = idsize;

    /* get the record */
    ret = dbp->get(dbp, NULL, &key, &data, 0);
    if (ret != 0) {
        dbp->err(dbp, ret,
                 "Error searching for id: %ld", id);
        return NULL;
    }

    /* create concept */
    ret = ooConcept_new(&conc);
    if (ret != oo_OK) return NULL;

    conc->numid = id;
    conc->bytecode = (char*)data.data;
    conc->bytecode_size = (size_t)data.size;
    conc->unpack(conc);

    /* update the cache */
    self->concept_index[id] = conc;

    free(key_buffer);

    return conc;
}
Пример #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
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);
}
Пример #16
0
char *smf_lookup_db4_query(char *database, char *key) {
    DB *dbp;
    DBT db_key, db_value;
    int ret;
    char *db_res = NULL;

    /* initialize db4 */
    if ((ret = db_create(&dbp, NULL, 0)) != 0) {
        TRACE(TRACE_ERR, "db_create: %s\n", db_strerror(ret));
        return NULL;
    }

    TRACE(TRACE_LOOKUP, "[%p] lookup key [%s]", dbp,key);

    if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
        TRACE(TRACE_WARNING, "DB (%s): %s", database, db_strerror(ret));
    }
    if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
        TRACE(TRACE_WARNING, "DB (%s): %s", database, db_strerror(ret));
    }

    /* open db */
#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR < 1
    if ((ret = dbp->open(dbp, database, NULL, DB_HASH, DB_RDONLY, 0)) != 0) {
        TRACE(TRACE_ERR, "DB (%s): %s", database, db_strerror(ret));
        return NULL;
    }
#else
    if ((ret = dbp->open(dbp, NULL, database, NULL, DB_HASH, DB_RDONLY, 0)) != 0) {
        TRACE(TRACE_ERR,"DB (%s): %s", database, db_strerror(ret));
        return NULL;
    }
#endif
    else {
        TRACE(TRACE_DEBUG, "DB (%s): open", database);
    }

    memset(&db_key, 0, sizeof(DBT));
    memset(&db_value, 0, sizeof(DBT));
    db_key.data = (void *)key;
    db_key.size = strlen(key) + 1;

    ret = dbp->get(dbp, NULL, &db_key, &db_value, 0);
    
    if (ret == 0) {
        if (asprintf(&db_res, "%s", (char *)db_value.data) != -1)
            TRACE(TRACE_LOOKUP, "[%p] found value [%s]", dbp, db_res);
    } else
        TRACE(TRACE_LOOKUP, "[%p] nothing found", dbp);

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

    return db_res;
}
Пример #17
0
/* perform btread command */
int dobtread(char *name, int rnum, int rnd){
  DB *dbp;
  DBT key, data;
  int i, err, len;
  char buf[RECBUFSIZ];
  if(showprgr) printf("<Reading Test of B+ Tree>\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_cachesize(dbp, 0, rnd ? BIG_CACHESIZE : SMALL_CACHESIZE, 0) != 0){
    fprintf(stderr, "DB->set_cachesize failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  if(dbp->open(dbp, NULL, name, NULL, DB_BTREE, DB_RDONLY, 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", rnd ? myrand() % rnum + 1 : i);
    key.data = buf;
    key.size = len;
    if(dbp->get(dbp, NULL, &key, &data, 0) != 0){
      fprintf(stderr, "DB->get 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;
}
Пример #18
0
extern
TDBVector* tdbVectorNewFromRecord(TDBBase* base, TDBUint32 recordnum)
{
    TDBVector* vector;
    TDBInt32 numfields = 3;     /* Sigh... */
    TDBNodePtr nodes[3];
    DBT key;
    DBT data;
    DB* db;
    int dbstatus;
    char* ptr;
    char* endptr;
    TDBInt32 i;
    TDBUint8 layer;
    TDBUint8 flags;
    TDBUint64 owner;
    if (base == NULL || recordnum == 0) {
        tdb_ASSERT(0);
        return NULL;
    }
    db = tdbGetRecordDB(base);
    if (db == NULL) {
        tdb_ASSERT(0);
        return NULL;
    }
    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = &recordnum;
    key.size = sizeof(recordnum);
    dbstatus = db->get(db, tdbGetTransaction(base), &key, &data, 0);
    if (dbstatus != DB_OK) {
        tdbMarkCorrupted(base);
        return NULL;
    }
    ptr = data.data;
    endptr = ptr + data.size;
    for (i=0 ; i<numfields ; i++) {
        nodes[i] = tdbGetNode(&ptr, endptr);
        if (nodes[i] == NULL) {
            tdbMarkCorrupted(base);
            for (i-- ; i>=0 ; i--) TDBFreeNode(nodes[i]);
            return NULL;
        }
    }
    layer = tdbGetUInt8(&ptr, endptr);
    flags = tdbGetUInt8(&ptr, endptr);
    owner = tdbGetUInt64(&ptr, endptr);
    vector = tdbVectorNewFromNodes(base, nodes, layer, flags, recordnum);
    for (i=0 ; i<numfields ; i++) TDBFreeNode(nodes[i]);
    if (vector == NULL) return NULL;
    vector->owner = owner;
    return vector;
}
Пример #19
0
int
bdb_dn2id_children(
	BackendDB	*be,
	DB_TXN *txn,
	struct berval *dn, 
	int flags )
{
	int		rc;
	DBT		key, data;
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;
	DB *db = bdb->bi_dn2id->bdi_db;
	ID		id;

#ifdef NEW_LOGGING
	LDAP_LOG ( INDEX, ARGS, 
		"=> bdb_dn2id_children( %s )\n", dn->bv_val, 0, 0 );
#else
	Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
		dn->bv_val, 0, 0 );
#endif

	DBTzero( &key );
	key.size = dn->bv_len + 2;
	key.data = ch_malloc( key.size );
	((char *)key.data)[0] = DN_ONE_PREFIX;
	AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );

	/* we actually could do a empty get... */
	DBTzero( &data );
	data.data = &id;
	data.ulen = sizeof(id);
	data.flags = DB_DBT_USERMEM;
	data.doff = 0;
	data.dlen = sizeof(id);

	rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags | flags );
	free( key.data );

#ifdef NEW_LOGGING
	LDAP_LOG ( INDEX, DETAIL1, 
		"<= bdb_dn2id_children( %s ): %schildren (%d)\n", 
		dn->bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
		db_strerror(rc)), rc );
#else
	Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
		dn->bv_val,
		rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
			db_strerror(rc) ), rc );
#endif

	return rc;
}
Пример #20
0
int uriindex_get (char uri[], unsigned int *DocID, unsigned int *lastmodified, char subname[]) {
        DB dbpArray;

        DB *dbp = NULL;

        DBT key, data;
        int ret;
	int forreturn = 1;
	struct uriindexFormat uriindex;

        if (!uriindex_open(&dbp,subname, DB_RDONLY)) {
		#ifdef DEBUG
			fprintf(stderr,"can't open uriindex\n");
		#endif
		return 0;
	}


        //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);

        if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) {

		*DocID = (*(struct uriindexFormat *)data.data).DocID;
		*lastmodified = (*(struct uriindexFormat *)data.data).lastmodified;
                forreturn = 1;
        }
        else if (ret == DB_NOTFOUND) {
		#ifdef DEBUG
                	dbp->err(dbp, ret, "DBcursor->get");
			printf("search for \"%s\", len %i\n",key.data,key.size);		
		#endif
                forreturn = 0;
        }
        else {
                dbp->err(dbp, ret, "DBcursor->get");
                forreturn = 0;
        }

	uriindex_close(&dbp);

	return forreturn;

}
Пример #21
0
/* Read from underlying datastore */
int pam_cc_db_get(void *_db, const char *keyname, size_t keylength,
		  char *data, size_t *size)
{
	DB *db = (DB *)_db;
	DBT key;
	DBT val;
	int rc;

	memset(&key, 0, sizeof(key));
	key.data = (char *)keyname;
	key.size = keylength;

	memset(&val, 0, sizeof(val));

#if USE_FLOCK
	rc = _pam_cc_db_lock(db, LOCK_SH);
	if (rc != PAM_SUCCESS) {
		return rc;
	}
#endif

	rc = db->get(db,
#if DB_VERSION_MAJOR >= 2
		NULL,
#endif
		&key, &val, 0);

#if USE_FLOCK
	_pam_cc_db_lock(db, LOCK_UN);
#endif

	if (rc != 0) {
#if DB_VERSION_MAJOR >= 2
		if (rc != DB_NOTFOUND)
			errno = rc;
#endif
		return (rc == DB_NOTFOUND) ? PAM_AUTHINFO_UNAVAIL : PAM_SERVICE_ERR;
	}

	if (val.size < *size) {
		return PAM_BUF_ERR;
	}

	memcpy(data, val.data, val.size);
	*size = val.size;

	return PAM_SUCCESS;
}
Пример #22
0
void
read_db1(TPSVCINFO *msg)
{
	int ret, db_num, commit, key_value;
	DBT key, data;
	DB *db;

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

	db_num = key_value = KEY_VALUE;
	key.data = &key_value;
	key.size = sizeof(key_value);
	data.flags = DB_DBT_MALLOC;

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

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

err:	tpreturn(TPFAIL, 1L, 0, 0L, 0);
}
Пример #23
0
int Db::get(DbTxn *txnid, Dbt *key, Dbt *value, u_int32_t flags)
{
	DB *db = unwrap(this);
	int err;

	if ((err = db->get(db, unwrap(txnid), key, value, flags)) != 0) {
		// DB_NOTFOUND and DB_KEYEMPTY are "normal" returns,
		// so should not be thrown as an error
		//
		if (err != DB_NOTFOUND && err != DB_KEYEMPTY) {
			DB_ERROR("Db::get", err, error_policy());
			return (err);
		}
	}
	return (err);
}
Пример #24
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;
}
Пример #25
0
int
bdb_dn2id_children(
	Operation *op,
	DB_TXN *txn,
	Entry *e )
{
	DBT		key, data;
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
	DB *db = bdb->bi_dn2id->bdi_db;
	ID		id;
	int		rc;

	Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
		e->e_nname.bv_val, 0, 0 );
	DBTzero( &key );
	key.size = e->e_nname.bv_len + 2;
	key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
	((char *)key.data)[0] = DN_ONE_PREFIX;
	AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );

	if ( bdb->bi_idl_cache_size ) {
		rc = bdb_idl_cache_get( bdb, db, &key, NULL );
		if ( rc != LDAP_NO_SUCH_OBJECT ) {
			op->o_tmpfree( key.data, op->o_tmpmemctx );
			return rc;
		}
	}
	/* we actually could do a empty get... */
	DBTzero( &data );
	data.data = &id;
	data.ulen = sizeof(id);
	data.flags = DB_DBT_USERMEM;
	data.doff = 0;
	data.dlen = sizeof(id);

	rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
	op->o_tmpfree( key.data, op->o_tmpmemctx );

	Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
		e->e_nname.bv_val,
		rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
			db_strerror(rc) ), rc );

	return rc;
}
Пример #26
0
int get (void **db, void *key, int key_len, void *value, int value_len) {
    int ret;
    DB *dbp = *db;
    DBT db_key, db_data;

    memset(&db_key, 0, sizeof(db_key));
    memset(&db_data, 0, sizeof(db_data));

    db_key.data = key;
    db_key.size = key_len;

    dbp->get(dbp, NULL, &db_key, &db_data, 0);

    value = db_data.data;
    value_len = db_data.size;

    return ES_OK;
}
Пример #27
0
int
_bdb_get_mail_count(const char *dbpath, uid_t uid)
{
	DBT key, data;
	DB *map = _bdb_open_database(dbpath);
	int mailcount = 0;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	key.data = &uid;
	key.size = sizeof(uid_t); 
	data.data = &mailcount;
	data.ulen = sizeof(int);
	data.flags = DB_DBT_USERMEM;
	if(map->get(map, NULL, &key, &data, 0) != 0)
		return 0;	
	map->close(map, 0); 
	return mailcount;
}
Пример #28
0
int pctldb_rcv(struct pctldb_st *pctldb, void *p, uint32_t *size, int *dberror){
  int status = 0;
  DBT key, data;
  uint32_t recno;
  uint32_t reclen;
  DB* dbp = pctldb->dbp;

  assert(p != NULL);
  if(p == NULL){
    *dberror = EINVAL;
    return(-1);
  }    
  
  memset(&key, 0 , sizeof(DBT));
  memset(&data, 0 , sizeof(DBT));

  if((status = dbp->get_re_len(dbp, &reclen)) != 0){
    *dberror = status;
    return(-1);
  }

  if(*size < reclen){
    *dberror = EINVAL;
    return(-1);
  }

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

  status = dbp->get(dbp, NULL, &key, &data, DB_CONSUME);
  if(status != 0){
    *dberror = status;
    status = -1;
  }else{
    *size = reclen;
    --pctldb->n;
  }

  return(status);
}
Пример #29
0
int load_metadata_keys(bdb_table_p _tp)
{
	int ret,n,ci;
	char dbuf[MAX_ROW_SIZE];
	char *s = NULL;
	DB *db = NULL;
	DBT key, data;
	ret = n = ci = 0;
	
	if(!_tp || !_tp->db)
		return -1;
	
	db = _tp->db;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	memset(dbuf, 0, MAX_ROW_SIZE);
	key.data = METADATA_KEY;
	key.size = strlen(METADATA_KEY);
	data.data = dbuf;
	data.ulen = MAX_ROW_SIZE;
	data.flags = DB_DBT_USERMEM;
	
	if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) 
	{
		db->err(db, ret, "load_metadata_keys DB->get failed");
		ERR("FAILED to find METADATA in table \n");
		return ret;
	}
	
	s = strtok(dbuf, " ");
	while(s!=NULL && n< _tp->ncols) 
	{	ret = sscanf(s,"%i", &ci);
		if(ret != 1) return -1;
		if( _tp->colp[ci] ) 
		{	_tp->colp[ci]->flag = 1;
			_tp->nkeys++;
		}
		n++;
		s=strtok(NULL, " ");
	}

	return 0;
}
Пример #30
0
/******************************************************************************
DOCBOOK START

FUNCTION panda_dbread
PURPOSE read a value from the Panda internal database

SYNOPSIS START
#include&lt;panda/constants.h&gt;
#include&lt;panda/functions.h&gt;
char *panda_dbread(panda_pdf *document, char *key);
SYNOPSIS END

DESCRIPTION <command>PANDA INTERNAL</command>. This function reads a value from the Panda internal database.

RETURNS The value, or NULL if there is no value.

EXAMPLE START
There is no examples for this internal call
EXAMPLE END
SEEALSO panda_dbopen, panda_dbclose, panda_dbread
DOCBOOK END
******************************************************************************/
char *
panda_dbread (panda_pdf * document, char *key)
{
  if(NULL == document)
    panda_error(panda_true, "Cannot write for NULL document\n");
  if(NULL == document->db)
    panda_error(panda_true, "Cannot write to NULL database\n");

#if defined DEBUG
  printf("Reading database key %s\n", key);
#endif

#if defined _WINDOWS
  return panda_windbread (document, key);
#else
#ifdef USE_EDB
 {
   int size;
   
   if (key == NULL)
     panda_error (panda_true, "Cannot read a NULL key\n");
   
   return e_db_data_get((E_DB_File *) document->db, key, &size);
 }
#else
 {
   DB *dbptr = document->db;
   DBT db_key, db_data;
   int ec;
   
   memset(&db_key, 0, sizeof(db_key));
   memset(&db_data, 0, sizeof(db_data));
   db_key.data = key;
   db_key.size = strlen(key) + 1;
   
   if ((ec = dbptr->get(dbptr, NULL, &db_key, &db_data, 0)) == 0)
     return panda_xsnprintf("%s", db_data.data);
   else
     return NULL;
 }
#endif // USE_EDB
#endif // _WINDOWS
}