示例#1
0
文件: OptimisticDb.c 项目: high/PRiDe
void optimisticDb_open( OptimisticDb *optimisticDb )
{
	DB_MPOOLFILE 	*mpf;
	DB 				*dbp;
	u_int32_t 		openFlags;
	int 			ret;

	/* Creates the database handler */
	ret = db_create( &dbp, optimisticDb->envp, 0 );
	if( ret != 0 ) {
		printf( "Can't create database for optimistic storage\n" );
		exit( 1 );
	}

	mpf = dbp->get_mpf( dbp );
	mpf->set_flags( mpf, DB_MPOOL_NOFILE, 1 );
	
	/* Opens the database file */
	openFlags = DB_CREATE | DB_EXCL;

	ret = dbp->open( dbp, NULL, NULL, NULL, DB_BTREE, openFlags, 0 );
	if( ret != 0 ) {
		printf( "Failed to open database for optimistic storage\n" );
		exit( 1 );
	}

	optimisticDb->dbp = dbp;
	optimisticDb->isOpen = 1;

}
示例#2
0
static int spoolbdb_open_create(DB **db, DB_ENV *dbenv,
				char *dbfname, int dbmode,
				int f_mpool_nofile){
  /*
   * If dbfname == NULL, then it is an in-memory (private) bdb.
   * If dbfname != NULL, then if the flag f_mpool_nofile is 1 the db
   * is configured as shared memory db or a normal db if the flag is 0.
   */
  DB *dbp = NULL;
  DB_MPOOLFILE *mpf = NULL;
  int status = 0;
  uint32_t dbflags = SPOOLBDB_FLAGS_CREATE;

  if(dbenv == NULL)
    return(EINVAL);

  /*
   * Let the user know that this combination is not valid.
   */
  if((dbfname == NULL) && (f_mpool_nofile == 0))
    return(EINVAL);

  status = db_create(&dbp, dbenv, 0);
  if(status != 0)
    return(status);

  if((dbfname == NULL) || (f_mpool_nofile == 1)){
    /*
     * Private or shared memory db.
     */
    mpf = dbp->get_mpf(dbp);
    status = mpf->set_flags(mpf, DB_MPOOL_NOFILE, 1);
  }

  if(status == 0){
    if(dbfname == NULL)
      status = dbp->open(dbp, NULL, NULL, NULL, DB_BTREE, dbflags, dbmode);
    else if(f_mpool_nofile != 0)
      status = dbp->open(dbp, NULL, NULL, dbfname, DB_BTREE, dbflags, dbmode);
    else
      status = dbp->open(dbp, NULL, dbfname, NULL, DB_BTREE, dbflags, dbmode);
  }

#if 0
  dbp->set_errcall(dbp, XXX_db_errcall_fcn); /* XXX */
#endif

  if(status != 0){
    if(dbp != NULL)
    (void)dbp->close(dbp, 0);
  }else
    *db = dbp;

  return(status);
}
示例#3
0
文件: qdb.c 项目: noaaport/nbsp
/*
 * One channel functions.
 */
int qdb_open(struct nbspq_st **nbspq, struct qdb_param_st *qdbparam,
	     int *dberror){

  DB *dbp = NULL;
  DB_MPOOLFILE *mpf = NULL;
  uint32_t dbflags = QDB_FLAGS;
  uint32_t mb = (1024 * 1024) * qdbparam->cache_mb;
  struct nbspq_st *q = NULL;
  int f_memory_based = 0;
  int status;

  status = db_create(&dbp, qdbparam->dbenv, 0);
  if(status == 0)
    status = dbp->set_re_len(dbp, qdbparam->reclen);

  if(status == 0){
    if((qdbparam->dbname != NULL) && (qdbparam->dbname[0] != '\0')){
      /*
       * We can set the pagesize also, but the berkely db chooses one according
       * to the operating system parameters if we dont'.
       */
      status = dbp->set_q_extentsize(dbp, qdbparam->extent_size);
    }else{
      /*
       * An in-memory db. Temporary overflow pages kept in memory and,
       * if dbenv == NULL, set the cache size explicitly. There is meaning
       * of page size and extent size in this case, so we deal with the
       * issue of reclaiming the unused memory from the cache explicitly
       * in qdb_rcv() below.
       */
      f_memory_based = 1;
      mpf = dbp->get_mpf(dbp);
      status = mpf->set_flags(mpf, DB_MPOOL_NOFILE, 1);
      if((qdbparam->dbenv == NULL) && (status == 0))	
	status = dbp->set_cachesize(dbp, 0, mb, 1);
    }
  }

  if(status == 0)
    status = dbp->open(dbp, NULL, qdbparam->dbname, NULL, DB_QUEUE, dbflags,
		       qdbparam->mode);

  if(status == 0){
    q = malloc(sizeof(struct nbspq_st));
    if(q == NULL)
      status = errno;
  }

  if(status == 0){
    q->dbp = dbp;
    q->n = 0;
    q->nmax = 0xffffffff;
    q->f_memory_based = f_memory_based;
    q->f_qdb_status = QDB_FSTATUS_OK;
    status = qdb_stat(q);
  }

  if(status == 0){
    status = pthread_mutex_init(&q->mutex, NULL);
    if(status == 0){
      status = pthread_cond_init(&q->cond, NULL);
      if(status != 0)
	pthread_mutex_destroy(&q->mutex);
    }
  }

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

    if(q != NULL)
      free(q);

    *dberror = status;
    status = -1;
  }

  if(status == 0)
    *nbspq = q;

  return(status);
}