eXtremeDB初步调试

2022-07-28 00:00:00 数据库 数据管理 记录 查找 大小


// Demo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <ctype.h> /*ctype.h*/
#include <stdlib.h> /*stdlib.h*/
#include <stdio.h> /*stdio.h*/
#include <string.h> /*string.h*/
//#pragma comment(lib,"mcolib_shm.lib")
#include "shmdb.h" /*用mcocomp生成的shmdb.h-文件*/
#include <iostream.h>
#include <windows.h>

const char * dbname = "demoShmDb"; //DB名称
const int SEGSZ = 1024 * 1024 * 10; //10M大小的数据库
const uint2 PAGESIZE = 90; //页面大小

const int MAP_ADDRESS = 0x20000000; //内存地址

void SH(void)
{
char text[] = {"This sample demonstrates eXtremeDB shared memory interfaces"};
char text1[] = {"Copyright (c) 2001-2005 McObject LLC. All Right Reserved."};

printf("%s eXtremeDB runtime version %d.%d, build %d %s Press Enter to start",text, MCO_COMP_VER_MAJOR, MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM,text1);

getchar();
}


static int askNumber(const char *prompt)
{
char buf[300];
printf("%s", prompt);
fgets(buf, sizeof (buf), stdin);
if(isdigit(buf[0]))
return atoi(buf);
return -1;
}

/*16进制转换成10进制*/
static int hex2num(const char *s)
{
unsigned int a = 0;

while(*s)
{
int n;
if( *s >= '0' && *s <= '9' )
n = *s - '0';
else if( *s >='a' && *s <= 'f')
n = *s - 'a' + 10;
else if( *s >='A' && *s <= 'F')
n = *s - 'A' + 10;
else return a;

a = (a << 4) | n;

s++;
}
return a;
}


static int askHex(const char *prompt)
{
char buf[300];
printf("%s", prompt);
fgets(buf, sizeof (buf), stdin);
return hex2num(buf);
}

/*插入记录*/
static void insertRec(mco_db_h db)
{
MyClass c;
MCO_RET rc;
mco_trans_h t;
int id;
char str1[100], str2[100];

DWORD start_time=GetTickCount();
// id = askNumber("Enter new record's id:");
for (int i=0;i<10000;i++)
{
id=i;

mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t ); //事务start
MyClass_new(t, &c); //新建一条记录
/*char chName[20],chSex[10];
int iAge=0;
cout<<"input name:"<<endl;
cin>>chName;
cout<<"input sex:"<<endl;
cin>>chSex;
cout<<"input age:"<<endl;
cin>>iAge;
*/
sprintf(str1,"%s", "lyt");
sprintf(str2,"%s", "man");
MyClass_id_put(&c,(uint4)id); //插入
MyClass_name_put(&c, str1, (uint2)strlen(str1) );
MyClass_sex_put(&c, str2, (uint2)strlen(str2) );
MyClass_age_put(&c,18);

rc = mco_trans_commit(t); //事务commit
if(rc==MCO_S_OK)
printf("insert done for %d (%s, %s, %d)\n",(int)id,str1,str2,18);
else
printf("insert failed (%d) for %d (%s, %s, %d)\n",rc,(int)id,str1,str2,18);
}

DWORD end_time=GetTickCount();
cout<<"insert 10000 records time:"<<end_time-start_time<<"ms"<<endl;
}

/*删除记录*/
static void deleteRec(mco_db_h db)
{
MyClass c;
MCO_RET rc;
mco_trans_h t;
int id;
char str1[100]= {0}, str2[100]= {0};
unsigned int iage=0;

id = askNumber("deleting record, Enter record's id:");

DWORD start_time=GetTickCount();

mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t );
rc = MyClass_pkey_find(t, (uint4)id, &c); //通过索引查找id
if(rc != MCO_S_OK)
{
printf("Record with id = %d not found", (int)id);
mco_trans_rollback(t); //如果没有找到回滚
return;
}
MyClass_name_get(&c,str1,(uint2)sizeof(str1),0);
MyClass_sex_get(&c,str2,(uint2)sizeof(str2) );
MyClass_age_get(&c,&iage);
rc = MyClass_delete(&c);

if(rc == MCO_S_OK)
rc = mco_trans_commit(t);
else
mco_trans_rollback(t);

if(rc==MCO_S_OK)
printf("delete done for %d (%s, %s, %d)\n",id,str1,str2,iage);
else
printf("delete failed (%d) for %d (%s, %s, %d)\n",rc,id,str1,str2,iage);

DWORD end_time=GetTickCount();
cout<<"delete one record time:"<<end_time-start_time<<"ms"<<endl;
}

/*Update*/
static void updateRec(mco_db_h db)
{
MyClass c;
MCO_RET rc;
mco_trans_h t;
int id;
char str1[100], str2[100];

id = askNumber("updating record, Enter record's id:");
mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t );
rc = MyClass_pkey_find(t, (uint4)id, &c); //通过索引查找id
if(rc != MCO_S_OK)
{
printf("Record with id = %d not found", (int)id);
mco_trans_rollback(t); //如果没有找到回滚
return;
}
MyClass_name_get(&c,str1,(uint2)sizeof(str1),0);
MyClass_sex_get(&c,str2,(uint2)sizeof(str2));
//int iAgeTemp=atoi(str2);
//sprintf(str2,"Age: %d",iAgeTemp+2);
//MyClass_str2_put(&c, str2, (uint2)strlen(str2) );
int iage=0;
cout<<"input updated age:"<<endl;
cin>>iage;
MyClass_age_put(&c,iage);

DWORD start_time=GetTickCount();

mco_trans_commit(t); //提交事务

printf("update done for %d (%s, %s, %d)\n",id,str1,str2,iage);

DWORD end_time=GetTickCount();
cout<<"update 1 record time:"<<end_time-start_time<<"ms"<<endl;
}

/*查找记录*/
static void searchRec(mco_db_h db)
{
MyClass c;
MCO_RET rc;
mco_trans_h t;
int id;
char str1[100]= {0}, str2[100]= {0};
unsigned int iage=0;

id = askNumber("searching record, Enter record's id:");

DWORD start_time=GetTickCount();

mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t );
rc = MyClass_pkey_find(t, (uint4)id, &c); //通过索引查找id
if(rc != MCO_S_OK)
{
printf("Record with id = %d not found", id);
mco_trans_rollback(t);
return;
}
MyClass_name_get(&c,str1,(uint2)sizeof(str1),0);
MyClass_sex_get(&c,str2,(uint2)sizeof(str2) );
MyClass_age_get(&c,&iage);
mco_trans_rollback(t);
printf("record found: %d (%s, %s, %d)\n",id,str1,str2,iage);

DWORD end_time=GetTickCount();
cout<<"find 1 record time:"<<end_time-start_time<<"ms"<<endl;
}

/*显示数据库中的记录信息*/
static void listRecs(mco_db_h db)
{
DWORD start_time=GetTickCount();

MyClass c;
MCO_RET rc;
mco_trans_h t;
uint4 id;
char str1[100]= {0}, str2[100]= {0};
unsigned int iage=0;
mco_cursor_t csr;

mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t );

/*用游标的方法遍历数据*/

MyClass_pkey_index_cursor( t, & csr );
rc = mco_cursor_first(t, &csr); //置游标为条记录
printf("----------- All records of type MyClass: -------- \n" );

/*遍历数据库*/
for( ; rc == MCO_S_OK; rc = mco_cursor_next(t, &csr) )
{
rc = MyClass_from_cursor(t, &csr, &c);
if(rc)
{
printf("Unexpected error %d",rc); break;
}
MyClass_id_get(&c, &id);
MyClass_name_get(&c,str1,(uint2)sizeof(str1),0);
MyClass_sex_get(&c,str2,(uint2)sizeof(str2) );
MyClass_age_get(&c,&iage);
printf("record: %7d (%s, %s, %d)\n",(int)id,str1,str2,iage);
}
printf("------------------------ end of list ------------ \n");
mco_trans_rollback(t);

DWORD end_time=GetTickCount();
cout<<"from first to last record time:"<<end_time-start_time<<"ms"<<endl;
}

/*数据库操作选择*/
static void menu( mco_db_h db )
{
char ibuf[300];
int ch; // menu choice

for(;;)
{

printf("\n Menu:\n 0:exit\n 1:insert a record\n 2:delete a record\n 3::update a record\n 4:search a record\n 5:list records\n 6:print db statistics\n");

fgets(ibuf, sizeof (ibuf), stdin);
if( ! isdigit( ibuf[0] ) )
continue;
ch = atoi(ibuf);

switch(ch)
{
default:break;
case 0:return;
case 1:
insertRec(db); break;
case 2:
deleteRec(db); break;
case 3:
updateRec(db); break;
case 4:
searchRec(db); break;
case 5:
listRecs(db); break;
case 6:
{
uint4 freepg, totalpg, nused;
mco_db_free_pages (db, &freepg); //求数据库剩余空间大小
mco_db_total_pages (db, &totalpg); //求数据库总大小

nused = totalpg - freepg;

printf("\nMemory:%dKb available, %dKb used\n",(int)(freepg * PAGESIZE / 1024), (int)(nused * PAGESIZE / 1024) ); //求后大小需要乘以页面大小
}
break;
}
} // end for
}

/*出错处理*/
static void errhandler( int n )
{
printf("eXtremeDB fatal error: %d", n );
getchar();
exit( -1 );
}

int main(int na,char **aa)
{
//printf("hello world\n");

MCO_RET rc;
mco_db_h db; //数据库句柄
void * start_mem = 0;
char prompt[300], buf[100];
mco_runtime_info_t info; //数据库信息

SH();
mco_get_runtime_info( &info); //获取可运行数据库信息
if ( !info.mco_shm_supported ) //是否有多线程支持
{
printf("This program requires shared memory database runtime");
exit(1);
}

mco_runtime_start(); //初始化运行环境

/* set fatal error handler */
mco_error_set_handler( &errhandler );

printf("Would you like to create a new database [Y] or attach [N] -- [Y/N] ?" );
fgets(buf, sizeof (buf), stdin);

if(buf[0] == 'y' || buf[0] == 'Y')
{

sprintf(prompt,"Enter hint address (default is %x):",(int) MAP_ADDRESS );

start_mem = (void*) askHex(prompt);
if( start_mem == 0 )
start_mem = (void*) MAP_ADDRESS;

printf("SHM Map address is %x", (int) start_mem );

rc = mco_db_open( dbname, shmdb_get_dictionary(), start_mem, SEGSZ, PAGESIZE );
if ( rc )
{
printf("Could not create instance: %d", rc);
exit( 1 );
}
}


rc = mco_db_connect( dbname, &db );
if ( rc )
{
printf("Could not attach to instance: %d", rc);
exit( 1 );
}

menu( db );

rc = mco_db_disconnect( db ); //断开连接
// rc = mco_db_close( dbname ); //关闭数据库

// mco_runtime_stop(); //释放

return 0;
}

————————————————
版权声明:本文为CSDN博主「读万卷书不如行万里路呀」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/nanfeiyannan/article/details/9003047

相关文章