# UDA Programming

> As with all eXtremeDB applications, the runtime must be started and initialized, memory devices defined and an error handler mapped. Then the Meta-dictionary is initialized and the database opened with `mco_uda_db_open()`. The following example demonstrates a typical sequence for opening a database for UDA access:

>         int main(void)
>         {
>             MCO_RET rc;
>             mco_runtime_info_t info;
>             mco_device_t dev[4];
>             unsigned int n_dev, metadict_size;
>             mco_db_params_t db_params;

>             mco_runtime_start();
>             mco_error_set_handler(&sample_errhandler);

>             mco_get_runtime_info(&info);
>             /* initialize memory device(s) */
>             dev[0].assignment = MCO_MEMORY_ASSIGN_DATABASE;
>             dev[0].size       = DATABASE_SIZE;

>             if (info.mco_shm_supported)
>              {
>                 dev[0].type       = MCO_MEMORY_NAMED;
>                 sprintf( dev[0].dev.named.name, "%s-db", dbName );
>                 dev[0].dev.named.flags = 0;
>                 dev[0].dev.named.hint  = 0;
>             }
>             else
>             {
>                 dev[0].type       = MCO_MEMORY_CONV;
>                 dev[0].dev.conv.ptr = (void*)malloc( DATABASE_SIZE );
>             }

>             n_dev = 1;
>             if (info.mco_disk_supported)
>             {
>                 dev[1].assignment = MCO_MEMORY_ASSIGN_CACHE
>                 dev[1].size       = CACHE_SIZE;

>                 if (info.mco_shm_supported)
>                 {
>                     dev[1].type       = MCO_MEMORY_NAMED;
>                     sprintf( dev[0].dev.named.name, "%s-cache", dbName );
>                     dev[1].dev.named.flags = 0;
>                     dev[1].dev.named.hint  = 0;
>                 }
>                 else
>                 {
>                     dev[1].type       = MCO_MEMORY_CONV;
>                     dev[1].dev.conv.ptr = (void*)malloc( CACHE_SIZE );
>                 }

>                 /* setup memory device for main database storage */
>                 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;

>                 /* setup memory device for database log */
>                 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;

>                 n_dev            += 3;
>             }

>             /* initialize and customize the database parameters */
>             mco_db_params_init ( &db_params );
>             db_params.mem_page_size      = MEMORY_PAGE_SIZE;
>             db_params.disk_page_size     = PSTORAGE_PAGE_SIZE;
>             db_params.db_max_connections = 1;

>             /* initialize meta-dictionary for 1 entry */
>             mco_metadict_size(1, &metadict_size);
>             metadict = (mco_metadict_header_t *) malloc(metadict_size);
>             mco_metadict_init (metadict, metadict_size, 0);

>             /* register dictionary */
>             rc = mco_metadict_register( metadict, dbName,
>             udaopen_get_dictionary(), 0);
>             printf("Register dictionary : %s\n", mco_ret_string(rc, 0));
>             printf("Runtime disk support : %s\n",
>             (info.mco_disk_supported) ? "yes" : "no");

>             /* change class persistence */
>             patches[0].class_code = get_class_code("InMem");
>             patches[0].persistence = MCO_UDA_CLASS_TRANSIENT;
>             patches[1].class_code = get_class_code("OnDisk");

>             patches[1].persistence = (info.mco_disk_supported) ?
>             MCO_UDA_CLASS_PERSISTENT :
>             MCO_UDA_CLASS_TRANSIENT;

>             /* open database */
>             rc = mco_uda_db_open(metadict,   /* meta-dictionary header */
>                                 0,          /* dictionary number */
>                                 dev,        /* memory devices */
>                                 n_dev,      /* num of memory devices */
>                                 &db_params, /* db parameters */
>                                 patches,    /* class persistence patches */
>                                 2);         /* num of persistence patches */

>             printf("mco_uda_db_open() : %s\n", mco_ret_string(rc, 0));

>             if (rc == MCO_S_OK)
>             {
>                 /* destroy the database */
>                 mco_uda_db_close(metadict, 0);
>             }

>             mco_runtime_stop();
>             if (!info.mco_shm_supported)
>             {
>                 free( dev[0].dev.conv.ptr );
>             }
>             if (info.mco_disk_supported)
>              {
>                 free( dev[1].dev.conv.ptr );
>             }

>             return 0;
>         }

> For a better understanding of the UDA API, build and run in the debugger the following SDK samples: 16-UDA-Udaopen, 16-UDA-Udaops, 16-UDA-Udapsearch, and others in this SDK directory.