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

📄 wtest.c

📁 extremeDB s sample code,useful for you
💻 C
字号:
/**************************************************************** *                                                              * * Copyright (c) 2001-2006 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;#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;	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 = wcslen(e_ptr->dep);				Record_wcCode_put(&Obj, e_ptr->dep, len);				len = wcslen(e_ptr->name);				Record_wsName_put(&Obj,  e_ptr->name, len);				len = wcslen(e_ptr->dep);				Record_wcDepartment_put(&Obj,  e_ptr->dep, len);				len = wcslen(e_ptr->job);				Record_wsJob_put(&Obj,  e_ptr->job, len);                if (e_ptr->prev_jobs) {                    int j, n = 0;                    wchar_t** prev_j = e_ptr->prev_jobs;                    while(*prev_j++) n++;                    prev_j = e_ptr->prev_jobs;                    Record_vwsPrevJob_alloc(&Obj, n);                    for(j = 0; j < n; j++)                          Record_vwsPrevJob_put(&Obj, j, prev_j[j], wcslen(prev_j[j]));                }				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) {	wchar_t      buffer[500];	uint4        i;	uint2        len;	Record_iNumber_get(hObj, &i);	printf("# %d: ", i);	Record_wcCode_get(hObj, buffer, 500);    buffer[8] = (wchar_t)0;    printf("Code='%ls' ", buffer);	Record_wsName_get(hObj, buffer, 500, &len);    buffer[len] = (wchar_t)0;	printf("Name='%ls' ", buffer);	Record_wcDepartment_get(hObj, buffer, 500);    buffer[20] = (wchar_t)0;	printf("Department='%ls' ", buffer);	Record_wsJob_get(hObj, buffer, 500, &len);    buffer[len] = (wchar_t)0;	printf("Job='%ls'\n", buffer);};/* 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 ) {			wchar_t * wbuf = L"QA"; int len;			len = 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 ) {			wchar_t * wbuf = L"R&D"; int len = 3;			/* Search for the first occurence */			i = 1;			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");					printf( "List of all records where previouse job was = MontaVista\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_PrevJob_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			wchar_t * wbuf = L"MontaVista";			int len = wcslen(wbuf);			/* Search for the first occurence */			i = 1;			rc = Record_I_PrevJob_search(t, &csr, MCO_EQ, wbuf, len);			do {				int cmp=0;				/* compare the item */				rc = Record_I_PrevJob_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");}void _SH_(void) {	char text[] = {		"\nThis samples demonstartes various tree index operations\n"	};	char text1[] = {		"Copyright (c) 2001-2006 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 ) {			printf( "mco_db_disconnect(...) failed\n" );		};		/* destroys the memory manager */		if ( mco_db_close( dbName ) != MCO_S_OK ) {			rs=2;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 + -