Exemplo n.º 1
0
DB *icalbdbset_bdb_open_secondary(DB *dbp, const char *database, const char *sub_database,
                                  int (*callback) (DB *db, const DBT *dbt1, const DBT *dbt2,
                                                   DBT *dbt3), int type)
{
    int ret;
    u_int32_t flags;
    DB *sdbp = NULL;

    if (!sub_database) {
        return NULL;
    }

    if (!ICAL_DB_ENV) {
        if (icalbdbset_init_dbenv(NULL, NULL) != 0) {
            return NULL;
        }
    }

    /* Open/create secondary */
    if ((ret = db_create(&sdbp, ICAL_DB_ENV, 0)) != 0) {
        ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "secondary index: %s", sub_database);
        return NULL;
    }

    if ((ret = sdbp->set_flags(sdbp, (u_int32_t) (DB_DUP | DB_DUPSORT))) != 0) {
        ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "set_flags error for secondary index: %s", sub_database);
        return NULL;
    }

    flags = (u_int32_t) (DB_CREATE | DB_THREAD);
    ret = sdbp->open(sdbp, NULL, database, sub_database, type, (u_int32_t) flags, 0644);
    if (ret != 0) {
        ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "failed to open secondary index: %s", sub_database);
        if (ret == DB_RUNRECOVERY) {
            abort();
        } else {
            return NULL;
        }
    }

    /* Associate the primary index with a secondary */
    if ((ret = dbp->associate(dbp, NULL, sdbp, callback, 0)) != 0) {
        ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "failed to associate secondary index: %s", sub_database);
        return NULL;
    }

    return sdbp;
}
Exemplo n.º 2
0
/* Open a Berkeley DB database */
int
open_db(DB **dbpp, const char *progname, const char *file_name,
        DB_ENV *envp, u_int32_t extra_flags)
{
    int ret;
    u_int32_t open_flags;
    DB *dbp;

    /* Initialize the DB handle */
    ret = db_create(&dbp, envp, 0);
    if (ret != 0) {
        fprintf(stderr, "%s: %s\n", progname,
                db_strerror(ret));
        return (EXIT_FAILURE);
    }

    /* Point to the memory malloc'd by db_create() */
    *dbpp = dbp;

    if (extra_flags != 0) {
        ret = dbp->set_flags(dbp, extra_flags);
        if (ret != 0) {
            dbp->err(dbp, ret,
                     "open_db: Attempt to set extra flags failed.");
            return (EXIT_FAILURE);
        }
    }

    /* Now open the database */
    open_flags = DB_CREATE              | /* Allow database creation */
                 DB_READ_UNCOMMITTED    | /* Allow dirty reads */
                 DB_AUTO_COMMIT         | /* Allow autocommit */
                 DB_THREAD;   /* Cause the database to be free-threaded */

    ret = dbp->open(dbp,        /* Pointer to the database */
                    NULL,       /* Txn pointer */
                    file_name,  /* File name */
                    NULL,       /* Logical db name */
                    DB_BTREE,   /* Database type (using btree) */
                    open_flags, /* Open flags */
                    0);         /* File mode. Using defaults */
    if (ret != 0) {
        dbp->err(dbp, ret, "Database '%s' open failed",
                 file_name);
        return (EXIT_FAILURE);
    }
    return (EXIT_SUCCESS);
}
Exemplo n.º 3
0
int openDB(DB **dbp, char *num )
{
	int ret = 0;	 
	if ( (ret = db_create(dbp,NULL,0)) != 0 )
        {
                printf("create the database failed!\n");
                exit(1);
        }
        if ( (ret = (*dbp)->open(*dbp,NULL,DATABASE,NULL,DB_BTREE,DB_CREATE,atoi(num))) != 0 )
        {
                (*dbp)->err(*dbp,ret,"$s",DATABASE);
                exit(1);
        }
	return 0;

}
Exemplo n.º 4
0
local int init_db(int write)
{
    int err;
    if ((err = db_env_create(&dbenv, 0)))
    {
        fprintf(stderr, "db_env_create: %s\n", db_strerror(err));
        return -1;
    }
    if ((err = dbenv->open(
                   dbenv,
                   ASSS_DB_HOME,
                   DB_INIT_CDB | DB_INIT_MPOOL,
                   0644)))
    {
        fprintf(stderr, "dbenv->open: %s\n", db_strerror(err));
        goto close_env;
    }
    if ((err = db_create(&db, dbenv, 0)))
    {
        fprintf(stderr, "db_create: %s\n", db_strerror(err));
        goto close_env;
    }
    if ((err = db->open(
                   db,
#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
                   /* they added a transaction parameter to the db->open
                    * call in 4.1.0. */
                   NULL,
#endif
                   ASSS_DB_FILENAME,
                   NULL,
                   DB_BTREE,
                   (args.needwrite ? 0 : DB_RDONLY),
                   0644)))
    {
        fprintf(stderr, "db->open: %s\n", db_strerror(err));
        goto close_db;
    }

    return 0;

close_db:
    db->close(db, 0);
close_env:
    dbenv->close(dbenv, 0);
    return -1;
}
Exemplo n.º 5
0
int dInsert_CONDResult(char* szAlm, char* szMsg, int dLen, unsigned char ucTAMID, unsigned char ucTAFID)
{
	int		   dRet;
	char       szIQuery[1024], szCQuery[1024];
	st_almsts *palm;

	//memset( szIQuery, 0x00, 1024 );
	//memset( szCQuery, 0x00, 1024 );

	palm     = (st_almsts*)szAlm;
	getInsertQuery(szIQuery, szAlm, szMsg, ucTAMID, ucTAFID);

	if( (dRet = db_insert(&stMySQL, szIQuery)) < 0 ){
		switch( db_errno(&stMySQL) ){
			case E_DB_NOT_CONNECT:
				log_print(LOGN_WARN, LH"FAILED IN connect DB",LT);
				break;
			case E_DB_TABLE_NOT_EXIST:
				getCreateQuery(szCQuery);
				if( (dRet = db_create(&stMySQL, szCQuery)) == 0 ){
					if( (dRet = db_insert(&stMySQL, szIQuery)) < 0 ){
						log_print(LOGN_CRI, LH"ERROR IN db_insert(SYS_COND_MSG), dRet=%d\nQuery=%s",
							LT, dRet,szIQuery);
						return dRet;
					}
					log_print(LOGN_CRI, LH"SUCCESS IN db_created(SYS_COND_MSG)",LT);
				}
				log_print(LOGN_CRI, LH"ERROR IN db_create(SYS_COND_MSG), dRet=%d",
					LT, dRet);
				break;
			case E_DB_DUPLICATED_ENTRY:
				log_print(LOGN_WARN, LH"FAILED IN db_insert(DUPLICATED)",LT);
				vErrPrint(szAlm, szMsg, ucTAMID, ucTAFID);
				return 0;
				break;
			default:
				log_print(LOGN_CRI, LH"FAILED IN db_insert(), dRet=%d", LT, dRet);
				vErrPrint(szAlm, szMsg, ucTAMID, ucTAFID);
				break;
			
		}
		return dRet;
	}

	log_print(LOGN_DEBUG,"SUCCESS INSERT COND MSG TO DB\nQuery=%s",szIQuery);
	return 0;
}
Exemplo n.º 6
0
Arquivo: state.c Projeto: kbarber/cfng
void
DeletePersistentClass(char *name)
{
    int errno;
    DBT key,value;
    DB *dbp;
    struct CfState state;
    time_t now = time(NULL),expires;
    char filename[CF_BUFSIZE];

    snprintf(filename,CF_BUFSIZE,"%s/%s",g_vlockdir,CF_STATEDB_FILE);

    if ((errno = db_create(&dbp,NULL,0)) != 0) {
        snprintf(g_output, CF_BUFSIZE,
                "Couldn't open average database %s\n", filename);
        CfLog(cferror,g_output,"db_open");
        return;
    }

#ifdef CF_OLD_DB
    if ((errno = dbp->open(dbp,filename,NULL,DB_BTREE,
                    DB_CREATE,0644)) != 0)
#else
    if ((errno = dbp->open(dbp,NULL,filename,NULL,DB_BTREE,
                    DB_CREATE,0644)) != 0)
#endif
    {
    snprintf(g_output,CF_BUFSIZE,"Couldn't open average database %s\n",filename);
    CfLog(cferror,g_output,"db_open");
    return;
    }

    chmod(filename,0644);

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

    key.data = name;
    key.size = strlen(name)+1;

    if ((errno = dbp->del(dbp,NULL,&key,0)) != 0) {
        CfLog(cferror,"","db_store");
    }

    Debug("Deleted persistent state %s if found\n",name);
    dbp->close(dbp,0);
}
Exemplo n.º 7
0
void bdb_db_open(void) {
    int ret;
    int db_open = 0;
    /* for replicas to get a full master copy, then open db */
    while(!db_open) {
        /* if replica, just scratch the db file from a master */
        if (1 == bdb_settings.is_replicated) {
            if (MDB_CLIENT == bdb_settings.rep_whoami) {
                bdb_settings.db_flags = DB_AUTO_COMMIT;
            } else if (MDB_MASTER == bdb_settings.rep_whoami) {
                bdb_settings.db_flags = DB_CREATE | DB_AUTO_COMMIT;
            } else {
                /* do nothing */
            }
        }

        bdb_db_close();

        if ((ret = db_create(&dbp, env, 0)) != 0) {
            fprintf(stderr, "db_create: %s\n", db_strerror(ret));
            exit(EXIT_FAILURE);
        }
        /* set page size */
        if((ret = dbp->set_pagesize(dbp, bdb_settings.page_size)) != 0) {
            fprintf(stderr, "dbp->set_pagesize: %s\n", db_strerror(ret));
            exit(EXIT_FAILURE);
        }

        /* try to open db*/
        ret = dbp->open(dbp, NULL, bdb_settings.db_file, NULL, bdb_settings.db_type, bdb_settings.db_flags, 0664);
        switch (ret) {
        case 0:
            db_open = 1;
            break;
        case ENOENT:
        case DB_LOCK_DEADLOCK:
        case DB_REP_LOCKOUT:
            fprintf(stderr, "db_open: %s\n", db_strerror(ret));
            sleep(3);
            break;
        default:
            fprintf(stderr, "db_open: %s\n", db_strerror(ret));
            exit(EXIT_FAILURE);
        }
    }

}
Exemplo n.º 8
0
int init_experiment(char *store_name)
{
	unsigned long env_flags, open_flags;;
	int rval;

	env_flags = DB_CREATE|DB_INIT_MPOOL;
	if (log_flag == 1) {
		env_flags |= DB_RECOVER|DB_INIT_TXN|DB_INIT_LOG;
	}
	//env_flags |=  DB_THREAD|DB_INIT_LOCK;

	if (db_env_create(&db_env, 0) != 0) {
		printf("[BDB] db_env_create() fail!\n");
		return 0;
	}
	
	//rval = envp->set_lk_detect(db_env, DB_LOCK_MINWRITE);
	//rval = envp->set_lk_max_locks(db_env, 10000);
	//rval = envp->set_lk_max_objects(db_env, 10000);
	val = db_env->set_cachesize(db_env, 0, 1024*1024*1024, 1);	// 0G + 1024M
	if (db_env->open(db_env, DB_DIRECTORY, env_flags, 0) != 0) {
		printf("[BDB] db_env->open() fail!\n");
		db_env->close(db_env, 0);
		return 0;
	}

	if(db_create(&db, db_env, 0) != 0) {
		printf("[BDB] db_create() fail!\n");
		return 0;
	}
	
	open_flags = DB_CREATE;
	if (log_flag == 1) {
		open_flags |= DB_AUTO_COMMIT;
	}
	if (kv_type == BTREE) {
		rval = db->open(db, NULL, store_name, NULL, DB_BTREE, open_flags, 0);
	} else if (kv_type == HASH) {
		rval = db->open(db, NULL, store_name, NULL, DB_HASH, open_flags, 0);
	}
	if (rval != 0) {
		printf("[BDB] db->open() fail!\n");
		return 0;
	}
	
	return 1;
}
Exemplo n.º 9
0
int main() {
	size_t i;
	obj *op;
	db* db = db_create(16);
	cstr k = cstr_new("1", 1);	
	obj* obj = cstr_obj_create("2");
	db_set(db, 0, k, obj);
	for(i = 0; i < 1; i++) {
		op = db_get(db, 0, k);
		obj_decr(op);
		op = db_get(db, 0, k);
		obj_decr(op);
		op = db_get(db, 0, k);
		obj_decr(op);
	}
	return 0;
}
Exemplo n.º 10
0
static void scanscan_setup (void) {
    int r;
    r = db_env_create(&env, 0);                                                           assert(r==0);
    r = env->set_cachesize(env, 0, cachesize, 1);                                         assert(r==0);
    r = env->open(env, dbdir, do_txns? env_open_flags_yesx : env_open_flags_nox, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);   assert(r==0);
    r = db_create(&db, env, 0);                                                           assert(r==0);
    if (do_txns) {
        r = env->txn_begin(env, 0, &tid, 0);                                              assert(r==0);
    }
    r = db->open(db, tid, dbfilename, NULL, DB_BTREE, 0, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);                           assert(r==0);
#ifdef TOKUDB
    if (prelock) {
        r = db->pre_acquire_table_lock(db, tid);
        assert(r==0);
    }
#endif
}
Exemplo n.º 11
0
static int
prepare_db()
{
    int rc;
    rc = db_create(&db, dbenv, 0);
    if (rc)
        log_db_error("db_create", rc);
    else {
        rc = db_open(db, DB_FILE_NAME, NULL, DB_BTREE, DB_CREATE, 0644);
        if (rc) {
            log_db_error("db->open", rc);
            db->close(db, 0);
            db = 0;
        }
    }
    return rc;
}
Exemplo n.º 12
0
bool open(char const* url)
{
    // create db
    if (db) {
        this->close();
    }    
    
    if (db_create(&db, NULL, 0) != 0) 
    {
        trace_msg("failed to create bdb.");
        return false;
    }
    
    int ret = 0;
    
    if (d_owner->dup)
    {
        ret = db->set_flags(db, DB_DUP);
        
        if (ret != 0)
        {
            this->close();
            return false;
        }
    }

    // open db
    ret = db->open(db,
                   NULL,
                   url,
                   NULL,
                   DB_BTREE,
                   (d_owner->readonly ? DB_RDONLY : DB_CREATE),
                   0);
    
    if (ret != 0) 
    {      
        this->close();
        return false;
    }
    
    dbpath = url;
    
    return true;   
}
Exemplo n.º 13
0
db* initialize_db(const char* db_name, uint32_t flag){
    db* db_ptr = NULL;
    DB* b_db;
    DB_ENV* dbenv;
    int ret;
    char* full_path = NULL;
    if((ret = mkdir(db_dir,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0){
        if(errno!=EEXIST){
            con_err_log("DB : Dir Creation Failed\n");
            goto db_init_return;
        }
    }
    full_path = (char*)malloc(strlen(db_dir) + strlen(db_name) + 2);
    mk_path(full_path, db_dir, db_name);
    if ((ret = db_env_create(&dbenv, 0)) != 0) {
        dbenv->err(dbenv, ret, "Environment Created: %s", db_dir);
        goto db_init_return;
    }
    if ((ret = dbenv->open(dbenv, db_dir, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL|DB_THREAD, 0)) != 0) {
        //dbenv->err(dbenv, ret, "Environment Open: %s", db_dir);
        goto db_init_return;
    }
    /* Initialize the DB handle */
    if((ret = db_create(&b_db,dbenv,flag)) != 0){
        con_err_log("DB : %s.\n", db_strerror(ret));
        goto db_init_return;
    }

    if((ret = b_db->open(b_db, NULL, db_name, NULL, DB_BTREE, DB_THREAD|DB_CREATE,0)) != 0){
        //b_db->err(b_db,ret,"%s","test.db");
        goto db_init_return;
    }
    db_ptr = (db*)(malloc(sizeof(db)));
    db_ptr->bdb_ptr = b_db;

db_init_return:
    if(full_path != NULL){
        free(full_path);
    }
    if(db_ptr != NULL){
        ;
    }
    return db_ptr;
}
Exemplo n.º 14
0
static int db_init(DB_ENV *dbenv, DB **dbp)
{
	int ret;

	/* Initialize the database. */
	if((ret = db_create(dbp, dbenv, 0)) != 0)
	{
		dbenv->err(dbenv, ret, "db_create");
		(void)dbenv->close(dbenv, 0);
		return 1;
	}

	if((ret = (*dbp)->set_re_len(*dbp, REC_SIZE)) != 0)
	{
		(*dbp)->err(*dbp, ret, "set_re_source");
		goto err;
	}

	if((ret = (*dbp)->set_q_extentsize(*dbp, 1000)) != 0)
	{
		(*dbp)->err(*dbp, ret, "set_q_extentsize");
		goto err;
	}

	#if USE_TXN
	if((ret = (*dbp)->open(*dbp, NULL, DATABASE, NULL, DB_QUEUE,
					DB_AUTO_COMMIT | DB_CREATE | DB_THREAD, 0664)) != 0)
	#else
	if((ret = (*dbp)->open(*dbp, NULL, DATABASE, NULL, DB_QUEUE,
								DB_CREATE | DB_THREAD, 0664)) != 0)
	#endif
	{
		(*dbp)->err(*dbp, ret, "%s: open", DATABASE);
		goto err;
	}

	return 0;

err:
	(void)(*dbp)->close(*dbp, 0);
	(void)dbenv->close(dbenv, 0);

	return 1;
}
Exemplo n.º 15
0
static int
sess_db_open (void)
{
    uint32_t flags;
    int      rc;

    rc = db_create(&sess_db, NULL, 0);
    if (rc) {
        ERR("db_create(): %s %d\n", db_strerror(rc), rc);
        return (-1);
    }
    flags = DB_CREATE;
    rc = sess_db->open(sess_db, NULL, "sessions.db", NULL, DB_BTREE, flags, 0);
    if (rc) {
        ERR("db_open(): %s (%d)\n", db_strerror(rc), rc);
        return (-1);
    }
    return (0);
}
Exemplo n.º 16
0
int
main(int argc, char **argv)
{
	int ret;

	if (argc < 2)
		errx(1, "Usage: ./prog command");

	if ((ret = db_create(&maindb, NULL, 0)) != 0) {
		err(1, "db_create: %s\n", db_strerror(ret));
	}

	if ((ret = maindb->set_cachesize(maindb, 0, dbCashe, dbCasheBlokes)) != 0) {
		maindb->err(maindb, ret, "set_cachesize");
	}


	if ((ret = maindb->open(maindb, NULL, THEDB, NULL, DB_BTREE, DB_RDONLY, 0664)) != 0) {
		maindb->err(maindb, ret, "%s", "db/maindb");
		exit(1);
	}

	if (strcmp(argv[1], "distinct") == 0) {
		int cutoff = 10;

		if (argc >= 3)
			cutoff = atoi(argv[2]);
		print_distinct(cutoff);
	} else if (strcmp(argv[1], "all") == 0) {
		print_all();
	} else if (strcmp(argv[1], "list") == 0) {
		printf("Commands:\n\n");
		printf("distinct [cutoff]\n");
	}



	if (maindb->close(maindb, 0) != 0)
		return 1;

	
	return 0;
}
Exemplo n.º 17
0
int open_table (void **db, char *table) {
    int ret;
    DB *dbp = NULL;

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

    if ((ret = dbp->open(
      dbp, NULL, table, NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
        dbp->err(dbp, ret, "%s", table);
        exit (1);
    }

    *db = dbp;

    return ES_OK;
}
Exemplo n.º 18
0
static isc_result_t
bdb_create(const char *zone, int argc, char **argv,
	   void *unused, void **dbdata)
{
	int ret;

	UNUSED(zone);
	UNUSED(unused);

	if (argc < 1)
		return ISC_R_FAILURE;	/* database path must be given */

	if (db_create((DB **)dbdata, NULL, 0) != 0) {
		/*
		 * XXX Should use dns_msgcat et al
		 * but seems to be unavailable.
		 */
		isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			       DNS_LOGMODULE_SDB, ISC_LOG_CRITICAL, isc_msgcat,
			       ISC_MSGSET_GENERAL, ISC_MSG_FATALERROR,
			       "db_create");
		return ISC_R_FAILURE;
	}

	if (isc_file_exists(*argv) != ISC_TRUE) {
		isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			       DNS_LOGMODULE_SDB, ISC_LOG_CRITICAL, isc_msgcat,
			       ISC_MSGSET_GENERAL, ISC_MSG_FATALERROR,
			       "isc_file_exists: %s", *argv);
		return ISC_R_FAILURE;
	}

	if ((ret = (*(DB **)dbdata)->open(*(DB **)dbdata, *argv, NULL, DB_HASH,
	    DB_RDONLY, 0)) != 0) {
			isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
				       DNS_LOGMODULE_SDB, ISC_LOG_CRITICAL,
				       isc_msgcat, ISC_MSGSET_GENERAL,
				       ISC_MSG_FATALERROR, "DB->open: %s",
				       db_strerror(ret));
			return ISC_R_FAILURE;
	}
	return ISC_R_SUCCESS;
}
Exemplo n.º 19
0
DB *icalbdbset_bdb_open(const char *path, const char *subdb, int dbtype, int mode, u_int32_t flag)
{
    DB *dbp = NULL;
    int ret;
    u_int32_t flags;

    /* Initialize the correct set of db subsystems (see capdb.c) */
    flags = (u_int32_t) (DB_CREATE | DB_THREAD);

    /* should just abort here instead of opening an env in the current dir..  */
    if (!ICAL_DB_ENV) {
        if (icalbdbset_init_dbenv(NULL, NULL) != 0) {
            return NULL;
        }
    }

    /* Create and initialize database object, open the database. */
    if ((ret = db_create(&dbp, ICAL_DB_ENV, 0)) != 0) {
        return NULL;
    }

    /* set comparison function, if BTREE */
    if (dbtype == DB_BTREE) {
        dbp->set_bt_compare(dbp, _compare_keys);
    }

    /* set DUP, DUPSORT */
    if (flag != 0) {
        dbp->set_flags(dbp, flag);
    }

    if ((ret = dbp->open(dbp, NULL, path, subdb, dbtype, flags, mode)) != 0) {
        ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "%s (database: %s): open failed.", path, subdb);
        if (ret == DB_RUNRECOVERY) {
            abort();
        } else {
            return NULL;
        }
    }

    return dbp;
}
Exemplo n.º 20
0
Berkley::Berkley(std::string dbName)
{
	//dataFile = new std::string(dbName, 0, sizeof(dbName));
	
	dataFile = dbName;
	
	// creates the database object
	ret = db_create(&db, env, 0);
	if (ret != 0) 
	{
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
	}

	// If database doesn't exist, create it 
	ret = db->open(db, NULL, dbName.c_str(), NULL, DB_HASH, DB_CREATE, 0664); 
	
	// std::cout << "Created DB: " << dataFile << std::endl;
	
	dbCount++;
}
Exemplo n.º 21
0
Arquivo: data.c Projeto: Tayyib/uludag
static int
open_database(DB_ENV *env, DB **dbp, const char *name)
{
	int e;
	DB *db;

	e = db_create(dbp, env, 0);
	if (e) {
		log_error("Cannot create database, %s\n", db_strerror(e));
		*dbp = NULL;
		return -1;
	}
	db = *dbp;
	e = db->open(db, NULL, name, NULL, DB_BTREE, DB_CREATE, 0);
	if (e) {
		log_error("Cannot open database, %s\n", db_strerror(e));
		return -2;
	}
	return 0;
}
Exemplo n.º 22
0
static apr_status_t _bdb_reslist_get_connection(void **conn_, void *params, apr_pool_t *pool) {
   int ret;
   mapcache_cache_bdb *cache = (mapcache_cache_bdb*)params;
   char *dbfile = apr_pstrcat(pool,cache->basedir,"/",cache->cache.name,".db",NULL);
   struct bdb_env *benv = apr_pcalloc(pool,sizeof(struct bdb_env));

   ret = db_env_create(&benv->env, 0);
   if(ret) { 
      cache->ctx->set_error(cache->ctx,500,"bdb cache failure for db_env_create: %s", db_strerror(ret));
      return APR_EGENERAL;
   }
   ret = benv->env->set_cachesize(benv->env,0,CACHESIZE,1); /* set a larger cache size than default */
   if(ret) { 
      cache->ctx->set_error(cache->ctx,500,"bdb cache failure for db->set_cachesize: %s", db_strerror(ret));
      return APR_EGENERAL;
   }
   int env_flags = DB_INIT_CDB|DB_INIT_MPOOL|DB_CREATE;
   ret = benv->env->open(benv->env,cache->basedir,env_flags,0);
   if(ret) { 
      cache->ctx->set_error(cache->ctx,500,"bdb cache failure for env->open: %s", db_strerror(ret));
      return APR_EGENERAL;
   }

   if ((ret = db_create(&benv->db, benv->env, 0)) != 0) {
      cache->ctx->set_error(cache->ctx,500,"bdb cache failure for db_create: %s", db_strerror(ret));
      return APR_EGENERAL;
   }
   int mode = DB_BTREE;
   ret = benv->db->set_pagesize(benv->db,PAGESIZE); /* set pagesize to maximum allowed, as tile data is usually pretty large */
   if(ret) { 
      cache->ctx->set_error(cache->ctx,500,"bdb cache failure for db->set_pagesize: %s", db_strerror(ret));
      return APR_EGENERAL;
   }

   if ((ret = benv->db->open(benv->db, NULL, dbfile, NULL, mode, DB_CREATE, 0664)) != 0) {
      cache->ctx->set_error(cache->ctx,500,"bdb cache failure 1 for db->open: %s", db_strerror(ret));
      return APR_EGENERAL;
   }
   *conn_ = benv;
   return APR_SUCCESS;
}
Exemplo n.º 23
0
void db_service::sql_update(const char* sql, db_query* query)
{
	assert(sql && *sql);

	db_handle* db;

	std::list<db_handle*>::iterator it = dbpool_.begin();
	if (it == dbpool_.end())
		db = db_create();
	else
	{
		db = *it;
		dbpool_.erase(it);
	}

	// 创建子线程的请求对象
	db_ipc_request* ipc_req = NEW db_ipc_request(db, sql, query, false);

	// 调用基类 ipc_service 请求过程
	request(ipc_req);
}
Exemplo n.º 24
0
int
open_db(DB** pDb, const char* pcszDBName, int nClear, int nDuplicate){
	DB*	db;
	int	nRet;
	
	*pDb = NULL;
 opendb:
	/* Initialize BDB handle */
	if ( db_create(&db, NULL, 0) ){
		write_log(LT_BOTH, "Error: Can't init BDB,"
			  "when open database %s!\n", pcszDBName ) ;
		return 0;
	}

	/* if need clear table */
	if ( nClear == 1 ){
		db->remove(db, pcszDBName, NULL, 0);
		nClear = 0;
		goto opendb;
	}

	/* set up error handling for this database */
	db->set_errcall(db, db_err_handler);
	db->set_errpfx(db, "jau_db");
	if ( nDuplicate == 1 )
		db->set_flags(db, DB_DUPSORT);
	
	/* Now open the database */
	nRet = db->open(db, NULL, pcszDBName,
			     NULL, DB_BTREE, DB_CREATE, 0);
	if (nRet != 0) {
		write_log(LT_BOTH, "Error: Can't open database %s!\n",
			  pcszDBName);
		db->close(db, 0);
		return 0;
	}
	*pDb = db;
	
	return 1;
}
Exemplo n.º 25
0
void fvt_kv_utils_SGD_LOOP(ARK     *ark,
                           void*  (*db_create)(uint32_t, uint32_t, uint32_t),
                           uint32_t klen,
                           uint32_t vlen,
                           uint32_t len,
                           uint32_t secs)
{
    uint32_t start = 0;
    uint32_t cur   = 0;
    int32_t  LEN   = (int32_t)len;
    kv_t    *db    = NULL;

    ASSERT_TRUE(NULL != ark);
    ASSERT_TRUE(NULL != db_create);

    start = time(0);

    do
    {
        db = (kv_t*)db_create(LEN, klen, vlen);
        ASSERT_TRUE(db != NULL);

        /* load all key/value pairs from the db into the ark */
        fvt_kv_utils_load(ark, db, LEN);

        /* query all key/value pairs from the db */
        fvt_kv_utils_query(ark, db, vlen, LEN);

        /* delete all key/value pairs from the db */
        fvt_kv_utils_del(ark, db, LEN);

        /* query all key/value pairs from the db */
        fvt_kv_utils_query_empty(ark, db, vlen, LEN);

        kv_db_destroy(db, LEN);

        cur = time(0);
    }
    while (cur-start < secs);
}
Exemplo n.º 26
0
Arquivo: main.c Projeto: zbierak/ipa
int main(int argc, char* argv[])
{
	if (argc != 2)
	{
		LOG_ERROR("Usage: %s <mount location>", argv[0]);
		return 1;
	}

	filesystem_h fs = filesystem_create();

	device_h* devices = NULL;
	size_t devices_count = 0;

	if (get_available_devices(&devices, &devices_count))
	{
		for (size_t i = 0; i < devices_count; i++)
		{
			LOG_INFO("Found device %s (%s)", device_get_uid(devices[i]), device_get_name(devices[i]));

			char* db_location = device_get_photo_db_location(devices[i]);
			char* root_path = device_get_root_path(devices[i]);
			db_h db = db_create(db_location, device_get_name(devices[i]), root_path);

			if (db != NULL)
			{
				filesystem_add_database(fs, db);
			}

			free(db_location);
			free(root_path);
			device_free(devices[i]);
		}

		free(devices);
	}

	filesystem_run(fs, argv[1]);

	filesystem_free(fs);
}
Exemplo n.º 27
0
int neo_bdb_init(u_int32_t flags)
{
    int i = 0, ret;
    char bdbname[128];
    pthread_t id;

    mkdir(DBDIR, 0777);

    for (i = 0; i < MAX_BDB_NUM; i++)
    {
        if ((ret = db_create(&dbp[i], NULL, 0)) != 0)
        {
            fprintf(stderr, "db_create: %s\n", db_strerror(ret));
            exit(1);
        }

        assert(dbp[i]->set_pagesize(dbp[i], 64 * 1024) == 0);

        snprintf(bdbname, 128, "%s/%s.%03d", DBDIR, DATABASE, i);

        if ((ret =
             dbp[i]->open(dbp[i], NULL, bdbname, NULL, DB_BTREE, flags,
                          0664)) != 0)
        {
            dbp[i]->err(dbp[i], ret, "%s", bdbname);
            exit(1);
        }

        pthread_mutex_init(&bdb_mutex[i], NULL);
    }

    ret = pthread_create(&id, NULL, (void *) cmd_thr, NULL);
    if (ret != 0)
    {
        printf("create cmd_thr thr error.\n");
        exit(1);
    }

    return 0;
}
Exemplo n.º 28
0
int
svn_fs_bdb__open_miscellaneous_table(DB **miscellaneous_p,
                                     DB_ENV *env,
                                     svn_boolean_t create)
{
  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
  DB *miscellaneous;
  int error;

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

  /* Create the table if it doesn't yet exist.  This is a form of
     automagical repository upgrading. */
  if (error == ENOENT && (! create))
    {
      BDB_ERR(miscellaneous->close(miscellaneous, 0));
      return svn_fs_bdb__open_miscellaneous_table(miscellaneous_p, env, TRUE);
    }
  BDB_ERR(error);

  /* If we're creating the table from scratch (not upgrading), record the
     upgrade rev as 0. */
  if (create)
    {
      DBT key, value;

      BDB_ERR(miscellaneous->put
              (miscellaneous, 0,
               svn_fs_base__str_to_dbt
               (&key, SVN_FS_BASE__MISC_FORWARD_DELTA_UPGRADE),
               svn_fs_base__str_to_dbt(&value, "0"), 0));
    }

  *miscellaneous_p = miscellaneous;
  return 0;
}
Exemplo n.º 29
0
static int
bdb_init_db(struct storage* s, char* db_path)
{
	int result;
	DB* dbp;
	
	//Create the DB file
	result = db_create(&(s->db), s->env, 0);
	if (result != 0) {
		paxos_log_error("Berkeley DB storage call to db_create failed: %s", 
			db_strerror(result));
		return -1;
	}
	
	dbp = s->db;
    
	// DB flags
	int flags = DB_CREATE;          /*Create if not existing */

	storage_tx_begin(s);

	//Open the DB file
	result = dbp->open(dbp,
		s->txn,          /* Transaction pointer */
		db_path,         /* On-disk file that holds the database. */
		NULL,            /* Optional logical database name */
		DB_BTREE,        /* Database access method */
		flags,           /* Open flags */
		0);              /* Default file permissions */

	storage_tx_commit(s);

	if (result != 0) {
		paxos_log_error("Berkeley DB storage open failed: %s",
			db_strerror(result));
		return -1;
	}
	
	return 0;
}
Exemplo n.º 30
0
int open_db(DB_ENV *pdb_env,DB **pdb,const char *db_name,
		DBTYPE type,unsigned int open_flags,
		unsigned int set_flags,
		int (*bt_compare_fcn)(DB *db,const DBT *dbt1,
			const DBT *dbt2))
{
	DB_TXN *tid = NULL;
	int ret;
	if((ret = db_create(pdb,pdb_env,0))!= 0)
	{
		return -1;
	}

	if(set_flags)
		(*pdb)->set_flags((*pdb),set_flags);
	if(bt_compare_fcn)
		(*pdb)->set_bt_compare((*pdb),bt_compare_fcn);
	ret = (*pdb)->open((*pdb),tid,db_name,NULL,type,open_flags,0);
	if(ret != 0)
		return -1;
	return 0;
}