# mco_db_open_dev
RT

> Create a database on the specified device(s).

### Prototype

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

### Arguments

dbname | The name of the database to open. Taken from the `declare database dbname` DDL statement. Note that the maximum database name length is 16 characters
dict | A handle to the dictionary that was created by the eXtremeDB schema compiler `mcocomp`. Normally, this handle is passed in by `dbname_get_dictionary()`
devs | Memory devices (array of `mco_device_t`)
n_devs | Number of device specifications in array `devs`
params | Database specification parameters

### Description

> This function creates the database `dbname` on the devices defined by `devs` according to the parameters specified in `params`. (See mco_device_t for detailed description of the memory device structure `devs` and `mco_db_params_t` for the database parameters `params`.

### Return Codes

MCO_S_OK | The database was created/opened successfully
MCO_E_ILLEGAL_PARAM | Illegal combination of database parameters. No memory segment for the database or no persistent or cache segments for the database
MCO_E_NOMEM | Not enough memory allocated. (Eg. not enough memory to create indexes)
MCO_E_INSTANCE_DUPLICATE | Duplicate db instance, i.e. attempt to open a database with the same name as a currently open database
MCO_E_SHM_ERROR | An error occurred when attempting to create, truncate or close a shared memory segment
MCO_E_INMEM_ONLY_RUNTIME | The database dictionary contains persistent objects but the application is linked with the in-memory runtime libraries
MCO_ERR_DB_NAMELONG | Database name longer than 16 characters
MCO_E_DISK_INVALID_PARAM | Disk database creation parameters are invalid
MCO_E_DISK_ALREADY_OPENED | The disk device specified is already opened

### Example

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:

        Application snippet:

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

            /* 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;
            dev[1].dev.conv.ptr = (void*)malloc( CACHE_SEGMENT_SIZE );
            if( !dev[1].dev.conv.ptr ) exit;

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

            ...
        }

### Files

Header file:
mco.h
Source file:
mcodb.c
Library:
libmcolib.a