⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wtest.c

📁 eXtremeDB数据库在Linux平台下的使用源码。内含多个示例程序
💻 C
字号:
/**************************************************************** *                                                              * * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.    * *                                                              * ****************************************************************/#include <platform.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "wddb.h"#define DB_MEMORY_SIZE  4 * 1024 * 1024#ifndef MCO_PLATFORM_X64#define PAGESIZE        96#else#define PAGESIZE        192 #endifconst int MAP_ADDRESS =  0x20000000;static int char_to_wchar(nchar_t *wbuff, const char* sbuff, int max_len) {	int i;	for(i = 0; i < max_len; i++) {		wbuff[i] = ((uint2)sbuff[i] | 0x1a00);		if (!sbuff[i]) break;	}	return i;}static char* nchar_to_char(nchar_t *wbuff, int len) {	static char buffer[500];	int i;	for(i = 0; i < len && wbuff[i]; i++) {		buffer[i] = (char)(wbuff[i] & 0x00ff);	}	buffer[i] = 0;	return buffer;}static void out_wchar(nchar_t *wbuff, int len) {    int i;    for(i = 0; i < len && wbuff[i]; i++)	putchar((int)(wbuff[i] & 0x00ff));}#include "w_data.h"/* Load data to the database */int LoadData(mco_db_h db) {	MCO_RET     rc;	mco_trans_h t;	int         i = 0, len;	Record      Obj;	nchar_t     wbuffer[100];	struct emp_data* e_ptr = EmpData;		while(e_ptr->name) {	        /* let's store records in DB */		/* begin the transaction  */		rc = mco_trans_start(db,MCO_READ_WRITE,MCO_TRANS_FOREGROUND,&t);		if (rc == MCO_S_OK) {			/* allocate a new object */			rc = Record_new(t, &Obj);			if (rc == MCO_S_OK) {				i++; 			    	/* fill the attributes */				Record_iNumber_put(&Obj, i);				len = char_to_wchar(wbuffer, e_ptr->dep, 100);				Record_ncCode_put(&Obj, wbuffer, len);				len = char_to_wchar(wbuffer, e_ptr->name, 100);				Record_nsName_put(&Obj,  wbuffer, len);				len = char_to_wchar(wbuffer, e_ptr->dep, 100);				Record_ncDepartment_put(&Obj,  wbuffer, len);				len = char_to_wchar(wbuffer, e_ptr->job, 100);				Record_nsJob_put(&Obj,  wbuffer, len);				rc = mco_trans_commit(t);			}			else {				printf("Couldn't allocate a new object\n");				mco_trans_rollback(t);			};		}		else {			printf("Unable to start a transaction\n");		};	    e_ptr++;	};	return 1;}/* Show attrs of a record */void ShowObj( Record* hObj) {	nchar_t      buffer[500];	uint4        i;	uint2        len;	Record_iNumber_get(hObj, &i);	printf("# %d: Code='", i);	Record_ncCode_get(hObj, buffer, 500);	out_wchar(buffer, 8); printf("' Name=");	Record_nsName_get(hObj, buffer, 500, &len);	out_wchar(buffer, len); printf(" Department=");	Record_ncDepartment_get(hObj, buffer, 500);	out_wchar(buffer, 20); printf(" Job=");	Record_nsJob_get(hObj, buffer, 500, &len);	out_wchar(buffer, len); printf("\n");};/* Show data using tree-indexes routine */void ShowData(mco_db_h db) {	MCO_RET      rc;	mco_trans_h  t;	mco_cursor_t csr, csr2;	Record   Obj;	int          i,j;	/* navigate downward thru the items from the first to the last */	printf("List of the first 10 records:\n");	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Record_I_Order_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* jump to the first record */			rc = mco_cursor_first(t, &csr);			/* take 10 items */			for (i=0; i<10 && rc==MCO_S_OK; i++) {				/* get record from cursor */				rc = Record_from_cursor(t, &csr, &Obj);				if (rc == MCO_S_OK) {					/* show it */					ShowObj(&Obj);					/* move to the next item */					rc = mco_cursor_next(t, &csr);				};			};		}		else {			printf ("Unable to open a cursor");		};		/*  close the transaction */		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open a transaction");	};	printf("\n");	/* navigate upward thru the items from the last to the first */	printf("List of the last 10 records:\n");	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Record_I_Order_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* move to last item */			rc = mco_cursor_last(t, &csr);			/* show last 10 items */			for (i=0; i<10 && rc==MCO_S_OK; i++) {				/* get the record */				rc = Record_from_cursor(t, &csr, &Obj);				if (rc == MCO_S_OK) {					/* show	it */					ShowObj(&Obj);					/* move to the next item */					rc = mco_cursor_prev(t, &csr);				};			};		}		else {			printf ("Unable to a open cursor");		};		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open a transaction");	};	printf("\n");	/* looking for specified item */	printf("Search for the first record, where department is QA\n");	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Record_I_Department_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			nchar_t wbuf[100]; int len;			len = char_to_wchar(wbuf, "QA", 2);			/* search an item by key value */			rc = Record_I_Department_search(t, &csr, MCO_EQ, wbuf, len);			/* analyze the results */			switch (rc) {				case MCO_S_OK:				{					/* Item's found. Display it. */					rc = Record_from_cursor(t, &csr, &Obj);					if (rc == MCO_S_OK ) {						ShowObj(&Obj);						/* Locate it in the I_Index cursor */						rc = Record_I_Index_locate(t, &csr2, &Obj);						if (rc == MCO_S_OK) {							/* And show next one */							if ( mco_cursor_next(t, &csr2) == MCO_S_OK &&								Record_from_cursor(t, &csr2, &Obj) == MCO_S_OK )								ShowObj(&Obj);						};					};				};break;				case MCO_S_NOTFOUND:					printf("Not found.\n");					break;				default:					printf("Error while searching.\n");					break;			};		}		else {			printf ("Unable to open a cursor");		};		/* close the transaction */		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open a transaction");	};	printf("\n");	/* using composite index I_DiffSum */	printf( "List of all records from the department = R&D\n" );	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Record_I_Department_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			nchar_t wbuf[100]; int len;			/* Search for the first occurence */			i = 1;			len = char_to_wchar(wbuf, "R&D", 3);			rc = Record_I_Department_search(t, &csr, MCO_EQ, wbuf, len);			do {				int cmp=0;				/* compare the item */				rc = Record_I_Department_compare(t, &csr, wbuf,len, &cmp);				if ( rc == MCO_S_OK && cmp == 0 ) {					/* get the record */					rc = Record_from_cursor(t, &csr, &Obj);					if ( rc == MCO_S_OK ) {						/* Show the record */						ShowObj(&Obj);						/* move to the next record */						rc = mco_cursor_next(t, &csr);						i++;					};				}				else {					break;				};			} while (rc == MCO_S_OK);		}		else {			printf ("Unable to open cursor");		};		/* close the tramsaction */		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open transaction");	};	printf("\n");	printf( "Alphabetical list\n");	j=0;	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Record_I_Name_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* jump to first record */			rc = mco_cursor_first(t, &csr);			/* take 10 first items */			while( rc==MCO_S_OK ) {				/* get a record from cursor */				rc = Record_from_cursor(t, &csr, &Obj);				if (rc == MCO_S_OK) {					/* show it */					ShowObj(&Obj);					/* move to the next item */					rc = mco_cursor_next(t, &csr);				};			}		}		else {			printf ("Unable to open cursor");		};		/* close the tramsaction */		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open a transaction");	};		printf("\n");}void _SH_(void) {	char text[] = {		"\nThis samples demonstartes various tree index operations\n"	};	char text1[] = {		"Copyright (c) 2001-2007 McObject LLC. All Right Reserved.\n\n"	};	printf("%s\neXtremeDB runtime version %d.%d, build %d\n%s\n\nPress Enter to start",		text, MCO_COMP_VER_MAJOR, MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM,text1);	getchar();}static void errhandler( int n ) {	printf( "\neXtremeDB fatal error: %d", n );	getchar();	exit( -1 );}int main( void ) {	MCO_RET        rc;	mco_db_h       db = 0;	char * start_mem  = 0;	const char * dbName = "wddb";	mco_runtime_info_t info;	int rs=1;	_SH_();	mco_get_runtime_info( &info);	if ( info.mco_shm_supported ) {		start_mem = (char*)MAP_ADDRESS;	}	else {		start_mem = (char*)malloc(DB_MEMORY_SIZE);		if (!start_mem) {			printf("Couldn't allocated memory\n");			exit (1);		}	};	/* set fatal error handler */	mco_error_set_handler( &errhandler );	/* start db engine */	if ( mco_runtime_start() != MCO_S_OK) {		printf( "\nUnable to start database engine\n" );		if ( !info.mco_shm_supported )			free( start_mem );		exit(-1);	};	/* Create a database, using first memory segment */	rc = mco_db_open( dbName, wddb_get_dictionary(),		start_mem, DB_MEMORY_SIZE, (uint2) PAGESIZE );	if ( rc == MCO_S_OK ) {		/* connect to the database, obtain a database handle */		if ( mco_db_connect( dbName, &db ) == MCO_S_OK ) {			/* if LoadData is OK than ShowData */			if ( LoadData(db) ) {				ShowData(db);rs=0;			}			else {				printf("Unable to load data\n");			};		}		else {			printf( "Unable to connect database\n" );		};		/* disconnect from the database, db is no longer valid */		if ( mco_db_disconnect( db ) != MCO_S_OK ) {			rs=2;printf( "mco_db_disconnect(...) failed\n" );		};		/* destroys the memory manager */		if ( mco_db_close( dbName ) != MCO_S_OK ) {			rs=3;printf( "mco_db_close(...) failed\n" );		};	}	else {		printf( "\nerror creating database" );	};	/* free the memory */	if ( !info.mco_shm_supported )		free( start_mem );	/* shutdown database engine */	mco_runtime_stop();	printf("\nPress Enter key to exit" );	getchar();	PROG_EXIT(rs);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -