eXtremeDB 基本操作
流程
挂载文件系统
csample_os_initialize(DEFAULT); // 内部调用 mount()启动 eXtremeDB 运行环境
cmco_runtime_start();打开数据库
cMCO_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 );连接数据库
cmco_db_connect( /*IN*/ const char * dbname, /*OUT*/ mco_db_h *db);开启事务
cMCO_RET mco_trans_start( /*IN*/ mco_db_h db, // 连接句柄 /*IN*/ MCO_TRANS_TYPE trans_type, // 事务类型 1字节 枚举值 /*IN*/ MCO_TRANS_PRIORITY priority, // 事务优先级 2字节 枚举值 /*OUT*/ mco_trans_h * t ); // 事务句柄CRUD
c/* Allocate an object 实例化对象*/ MCO_RET classname_new( /*IN*/ mco_trans_h t, // 事务句柄 /*OUT*/ classname * handle ); // 对象句柄 /* Put values to scalar fields 给某一标量字段指定值 */ classname_fieldname_put(classname * handle, type vale); /* Put value to string field using db_name as a value 给字符或者字符串字段赋值 classname_fieldname_put() */ classname_fieldname_put(classname * handle, type vale, uint2 length); /* Put int8 value to numeric field 将字符串转换为8字节的整数值 */ classname_fieldname_from_chars( /*OUT*/ mco_int8 scaled_num, /*IN*/ char const * buf ); /* Put decimal value to char field */ classname_fieldname_to_chars( /*IN*/ mco_int8 scaled_num, /*OUT*/ char * buf, /*IN*/ int buf_size); classname_fieldname_put_chars( /*IN*/ classname *handle, /*IN*/ const char * buf ); /* Get struct handle to write it 获取 class 里的结构体句柄 */ structname_structname_write_handle( /*IN*/ structname1 *structhandle1, /*OUT*/ structname2 *structhandle2 ); /* Put values to the struct fields 设置结构体字段 */ structname_fieldname_put( /*IN*/ structname *handle, /*IN*/ <type> value ); // ...提交事务
cmco_trans_commit(mco_trans_h * t);关闭连接
cmco_db_disconnect(mco_db_h db);关闭数据库
cmco_db_close( /*IN*/ const char * dbname);停止 eXtremeDB 运行环境
cmco_runtime_stop();卸载文件系统
csample_os_shutdown(); // 内部调用 umount()
代码示例
c
#include <stdio.h>
#include <common.h>
#include <simpledb.h>
const char *db_name = "operations";
int main(int argc, char *argv[])
{
MCO_RET rc;
sample_memory_t dbmem;
/* 挂载文件系统 */
sample_os_initialize(DEFAULT);
/* Start eXtremeDB runtime 启动 eXtremeDB 运行环境 */
mco_runtime_start();
/* 打开 sample 数据库,对数据库打开操作的封装 */
// MCO_RET sample_open_database( /*IN*/ const char * db_name, 数据库名
// /*IN*/ mco_dictionary_h dict, 数据库字典( schema的 二进制文件)句柄
// /*IN*/ mco_size_t db_sz, 数据库的内存段大小
// /*IN*/ mco_size_t cache_sz, 数据库的 cache 段大小
// /*IN*/ uint2 mem_pg_sz, 内存页大小
// /*IN*/ uint2 pstorage_pg_sz, 持久化存储页大小
// /*IN*/ uint2 max_conn_no, 最大连接数
// /*IN*/ sample_memory_t * pdev) 设备占位
/* 在特定设备上的打开数据库 */
// 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 );
sample_header("Sample 'operations' performs basic insert, read, and update operations.\n");
rc = sample_open_database(db_name, simpledb_get_dictionary(), DATABASE_SIZE, CACHE_SIZE,
MEMORY_PAGE_SIZE, PSTORAGE_PAGE_SIZE, 1, &dbmem);
/* 检查是否打开成功 */
sample_rc_check("\tOpen", rc);
if (MCO_S_OK == rc)
{
mco_db_h connection; /* Connection handle 连接句柄 */
/* The database was opened successfully 打开成功 */
/* Connect it by name 通过数据库名连接 */
rc = mco_db_connect(db_name, &connection);
sample_rc_check("\tConnect", rc);
if (MCO_S_OK == rc)
{
/* The database is connected successfully 连接成功 */
mco_trans_h t; /* 事务句柄 */
/* 开启事务 */
// MCO_RET mco_trans_start( /*IN*/ mco_db_h db, 连接句柄
// /*IN*/ MCO_TRANS_TYPE trans_type, 事务类型 1字节 枚举值
// /*IN*/ MCO_TRANS_PRIORITY priority, 事务优先级 2字节 枚举值
// /*OUT*/ mco_trans_h * t ); 事务句柄
rc = mco_trans_start(connection, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
sample_rc_check("\tOpen Transaction", rc);
if (MCO_S_OK == rc)
{
A a; /* class A 的实例 */
A_fixed _a; /* Fixed size part of class A */
B b; /* Struct handle 结构体句柄*/
B_fixed _b; /* Fixed size part of struct B */
uint1 ui1; /* Value place holders 字段值占位 */
uint2 ui2;
uint4 ui4;
int1 i1;
int2 i2;
int4 i4;
mco_int8 i8;
float f = (float)1.1;
double d = 2.2;
char buf[16]; /* String buffer */
mco_cursor_t csr; /* Cursor to navigate database contents 浏览数据库内容的游标 */
/* Allocate an object 实例化对象*/
// MCO_RET classname_new( /*IN*/ mco_trans_h t, 事务句柄
// /*OUT*/ classname * handle ); 对象句柄
A_new(t, &a);
/* Put values to scalar fields 给某一标量字段指定值 classname_fieldname_put() */
A_ui1_put(&a, 1);
A_ui2_put(&a, 2);
A_ui4_put(&a, 3);
A_d_put(&a, d);
/* Put value to string field using db_name as a value 给字符或者字符串字段赋值 classname_fieldname_put() */
A_s_put(&a, db_name, (uint2)strlen(db_name));
/* Put int8 value to numeric field 将字符串转换为8字节的整数值 */
A_num_from_chars(&i8, "1234567");
A_num_put(&a, i8);
/* Put decimal value to char field */
A_dec_to_chars(987654321, buf, sizeof(buf));
A_dec_put_chars(&a, buf);
/* Get struct handle to write it 获取 class 里的结构体句柄 */
A_b_write_handle(&a, &b);
/* Put values to the struct fields 设置结构体字段 */
B_i1_put(&b, 4);
B_i2_put(&b, 5);
B_i4_put(&b, 6);
B_f_put(&b, f);
B_c10_put(&b, db_name, (uint2)strlen(db_name));
/* 提交事务 */
rc = mco_trans_commit(t);
sample_rc_check("\tCommit Transaction", rc); /* 检查提交状态 */
/* Open a READ_ONLY transaction, read object A and display its contents 开启一个只读的事务 */
rc = mco_trans_start(connection, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
if (MCO_S_OK == rc)
{
rc = A_list_cursor(t, &csr); /* 为(无序的)光标导航创建一个列表光标。 */
if (MCO_S_OK == rc)
{
mco_cursor_first(t, &csr); /* 将光标置于索引的第一个对象 */
A_from_cursor(t, &csr, &a); /* 返回当前光标指向的对象句柄 */
printf("\n\tContents of first record A: \n");
/* Get values from the object fields 获取当前对象的字段 */
A_ui1_get(&a, &ui1);
A_ui2_get(&a, &ui2);
A_ui4_get(&a, &ui4);
A_d_get(&a, &d);
printf("\t\tui1=%d, ui2=%d, ui4=%ld, d=%f\n", ui1, ui2, ui4, d);
/* Get values from numeric/decimal fields */
A_num_get(&a, &i8);
A_num_get_chars(&a, buf, sizeof(buf));
printf("\t\tnum=%lld, chars(%s)\n", i8, buf);
A_dec_get(&a, &i8);
A_dec_get_chars(&a, buf, sizeof(buf));
printf("\t\tdec=%lld, chars(%s)\n", i8, buf);
/* Get string value into the buf and the value length into ui2 */
/* Note: the _get() will null-terminate the string only if the buffer is long enough */
A_s_get(&a, buf, sizeof(buf), &ui2);
printf("\tstring s=(%s), (length = %d)\n", buf, ui2);
/* Get struct read-handle 获取结构体读取句柄 */
A_b_read_handle(&a, &b);
/* Get values of the structs fields */
B_i1_get(&b, &i1);
B_i2_get(&b, &i2);
B_i4_get(&b, &i4);
B_f_get(&b, &f);
B_c10_get(&b, buf, sizeof(buf));
printf("\tStruct b: b.i1=%d, b.i2=%d, b.i4=%ld, b.f=%f, b.c10=(%s)\n", i1, i2, i4, f, buf);
/* Get field values from A_fixed */
printf("\n\tUsing A_fixed :\n");
A_fixed_get(&a, &_a);
printf("\t\t_a.ui1=%d, _a.ui2=%d, _a.ui4=%ld, _a.d=%f\n", _a.ui1, _a.ui2, _a.ui4, _a.d);
/* Get field values from B_fixed */
printf("\n\tUsing B_fixed :\n");
B_fixed_get(&b, &_b);
/* Copy and null terminate the character string in _b.c10 */
strncpy(buf, _b.c10, 10);
printf("\t\t_b.i1=%d, _b.i2=%d, _b.i4=%ld, _b.f=%f, _b.c10=(%s)\n", _b.i1, _b.i2, _b.i4, _b.f, buf);
}
}
rc = mco_trans_commit(t); /* 提交事务 */
}
/* Don't forget to disconnect when done 关闭连接 */
rc = mco_db_disconnect(connection);
sample_rc_check("\tDisconnect", rc); /* 检查是否关闭成功 */
}
/* Close the database 关闭数据库 */
sample_close_database(db_name, &dbmem);
sample_rc_check("\tClose", rc);
}
/* Stop eXtremeDB runtime 停止extremeDB运行环境 */
mco_runtime_stop();
sample_pause_end("\n\nPress any key to continue . . . ");
/* 卸载文件系统 */
sample_os_shutdown();
return (MCO_S_OK == rc ? 0 : 1);
}