Create a database on the specified device(s).
MCO_RET mco_db_open_dev(/*IN*/ const char *dbname,
/*IN*/ mco_dictionary_h dict,
/*IN*/ mco_device_t *devs,
/*IN*/ mco_size_t n_devs,
/*IN*/ mco_db_params_t *params);
dbname
declare database dbname DDL statement.
Note that the maximum database name length is 16 characters.dict
mcocomp. Normally,
this handle is passed in by dbname_get_dictionary().devs
mco_device_t).n_devs
devs.params
This function creates the database
dbnameon the devices defined bydevsaccording to the parameters specified inparams. (See mco_device_t for detailed description of the memory device structuredevsandmco_db_params_tfor the database parametersparams.
MCO_S_OK
MCO_E_ILLEGAL_PARAM
MCO_E_NOMEM
MCO_E_INSTANCE_DUPLICATE
MCO_E_SHM_ERROR
MCO_ERR_DB_NAMELONG
MCO_E_DISK_INVALID_PARAM
MCO_E_DISK_ALREADY_OPENED
The following example code demonstrates how to define the memory devices and database parameters and call
mco_db_open_dev() for an all-in-memory database:
/* All-in-memory example */
#define DATABASE_SEGMENT_SIZE 16 * 1024 * 1024
#define MEMORY_PAGE_SIZE 128
const char *dbname = "convdb";
int main(int argc, char* argv[])
{
MCO_RET rc;
mco_device_t dev;
mco_db_params_t db_params;
mco_runtime_start();
/* setup memory device as a plain conventional memory region */
dev.type = MCO_MEMORY_CONV; /* conventional mem. */
dev.assignment = MCO_MEMORY_ASSIGN_DATABASE; /* main database mem. */
dev.size = DATABASE_SEGMENT_SIZE; /* device size */
dev.dev.conv.ptr = (void*)malloc(DATABASE_SEGMENT_SIZE);
if (!dev.dev.conv.ptr)
exit(-1);
/* initialize and customize the database parameters */
mco_db_params_init(&db_params); /* initialize with default values */
db_params.mem_page_size = MEMORY_PAGE_SIZE; /* in-mem. part */
db_params.disk_page_size = 0; /* disable disk operations */
db_params.db_max_connections = 1; /* total connections to db */
/* open a database on the device with given params */
rc = mco_db_open_dev(dbname, convdb_get_dictionary(), &dev, 1, &db_params);
...
}
The next example code demonstrates how to define the memory devices and database parameters and call
mco_db_open_dev() for a persistent database:
/* Persistent database example */
#define MEMORY_PAGE_SIZE 128
#define DISK_PAGE_SIZE 128
#define DB_LOG_TYPE REDO_LOG
#define REDO_LOG_SIZE (2 * 1024 * 1024)
const char *dbname = "diskdb";
int main(int argc, char* argv[])
{
MCO_RET rc;
mco_device_t dev[4];
mco_db_params_t db_params;
dev[0].type = MCO_MEMORY_CONV;
dev[0].assignment = MCO_MEMORY_ASSIGN_DATABASE;
dev[0].size = DATABASE_SEGMENT_SIZE;
dev[1].type = MCO_MEMORY_CONV;
dev[1].assignment = MCO_MEMORY_ASSIGN_CACHE;
dev[1].size = CACHE_SEGMENT_SIZE;
dev[2].type = MCO_MEMORY_FILE;
dev[2].assignment = MCO_MEMORY_ASSIGN_PERSISTENT;
sprintf(dev[2].dev.file.name, FILE_PREFIX "%s.dbs", dbname);
dev[2].dev.file.flags = MCO_FILE_OPEN_DEFAULT;
dev[3].type = MCO_MEMORY_FILE;
dev[3].assignment = MCO_MEMORY_ASSIGN_LOG;
sprintf(dev[3].dev.file.name, FILE_PREFIX "%s.log", dbname);
dev[3].dev.file.flags = MCO_FILE_OPEN_DEFAULT;
/* allocate memory for the database and cache */
dev[0].dev.conv.ptr = (void*)malloc(DATABASE_SEGMENT_SIZE);
if (!dev[0].dev.conv.ptr)
exit(-1);
dev[1].dev.conv.ptr = (void*)malloc(CACHE_SEGMENT_SIZE);
if (!dev[1].dev.conv.ptr)
exit(-1);
/* set default database parameters */
mco_db_params_init(&db_params);
/* customize the parameters */
db_params.mem_page_size = MEMORY_PAGE_SIZE;
db_params.disk_page_size = DISK_PAGE_SIZE;
db_params.db_max_connections = 1;
db_params.disk_max_database_size = MAXIMUM_DATABASE_SIZE;
db_params.db_log_type = DB_LOG_TYPE;
db_params.log_params.redo_log_limit = REDO_LOG_SIZE;
rc = mco_db_open_dev(dbname, diskdb_get_dictionary(), dev, sizeof(dev) / sizeof(dev[0]), &db_params);
...
}