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

📄 treeindex.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 <math.h>#include "randdb.h"#define DB_MEMORY_SIZE  4 * 1024 * 1024#ifndef MCO_PLATFORM_X64#define PAGESIZE        96#else#define PAGESIZE        192#endifconst int MAP_ADDRESS =  0x20000000;#define SERIES_SIZE 5#define DELTA       5#define RANGE_DIFF  (float)0.9#define TARGET_SERIES  5#define RGB(r,g,b)     ((uint4)(((uint1)(r)|((uint2)((uint1)(g))<<8))|(((uint4)(uint1)(b))<<16)))/* Load a data to database */int LoadData(mco_db_h db) {	MCO_RET     rc;	mco_trans_h t;	int         i;	uint2       j;	Measurement Obj;	/* let's make a 1000 measurements */	for (i=0;i<1000;i++) {		/* reset random-mechanism for new series of measurements */		if ( i % SERIES_SIZE == 0 )			srand(time(0));		/* 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 = Measurement_new(t, &Obj);			if (rc == MCO_S_OK) {				/* some trivial math */				float ft1,ft2;				int idx, series;				idx = (i-(i/SERIES_SIZE)*SERIES_SIZE)+1;				series = (i / SERIES_SIZE)+1;				ft1 = ((float)rand() / (float)RAND_MAX);				ft2 = ((float)rand() / (float)RAND_MAX);				/* fill attributes */				Measurement_iSeries_put(&Obj, series );				Measurement_iIdx_put(&Obj, idx );				Measurement_tTime_put(&Obj, time(0) );				Measurement_fValue1_put(&Obj,  ft1);				Measurement_fValue2_put(&Obj,  ft2);				Measurement_fDiff_put(&Obj,  (float)fabs(ft2-ft1));				Measurement_vVis_alloc(&Obj, DELTA);				for (j=0;j<DELTA;j++) {					Vis         vis;					float       f = (float)j * ( (float)fabs(ft2-ft1) / (float)DELTA);					uint4       c = (uint4)(255 - f*255);					Measurement_vVis_put(&Obj, j, &vis);					Vis_fValue_put(&vis, f );					Vis_iRGB_put  (&vis, RGB(c,c,c) );				};				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");		};	};	return 1;}/* Show attrs of one measurement */void ShowObj( Measurement* hObj) {  char         stub[]="0:0\n"; 	int          i,j;	float        ft1,ft2,ft3;	time_t       tm;	mco_time     _tm;	char        *p1 = stub, *p2;	/* get attr's values */	Measurement_iSeries_get(hObj, &i );	Measurement_iIdx_get(hObj, &j );	Measurement_tTime_get(hObj, (mco_time*)&_tm);	tm=_tm;	Measurement_fValue1_get(hObj, &ft1);	Measurement_fValue2_get(hObj, &ft2);	Measurement_fDiff_get(hObj, &ft3);	/* format & print it */	p1 = (char*)ctime((time_t*)&tm);	p2 = strchr(p1,'\n');	*p2 = (char)0;	printf("%3dx%d. Measurement from %s: %f %f %f\n", i, j, p1, ft1, ft2, ft3);};/* Show data using tree-indexes routine */void ShowData(mco_db_h db) {	MCO_RET      rc;	mco_trans_h  t;	mco_cursor_t csr, csr2;	Measurement   Obj;	int          i,j;	/* navigate downward thru the items from first to last */	printf("List of the first 10 measurements:\n");	/* open a ransaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Measurement_I_Time_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* jump to first measurement */			rc = mco_cursor_first(t, &csr);			/* take 10 first items */			for (i=0; i<10 && rc==MCO_S_OK; i++) {				/* get measurement from cursor */				rc = Measurement_from_cursor(t, &csr, &Obj);				if (rc == MCO_S_OK) {					/* show it */					ShowObj(&Obj);					/* move to 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 last to first */	printf("List of the last 10 measurements:\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 = Measurement_I_Time_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 measurement */				rc = Measurement_from_cursor(t, &csr, &Obj);				if (rc == MCO_S_OK) {					/* show	it */					ShowObj(&Obj);					/* move to 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 measurement, where the difference is equal to 0.5\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 = Measurement_I_Diff_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* search an item by key-value */			rc = Measurement_I_Diff_search(t, &csr, MCO_EQ, 0.5);			/* analyze the results */			switch (rc) {				case MCO_S_OK:				{					/* Item's found. Display it. */					rc = Measurement_from_cursor(t, &csr, &Obj);					if (rc == MCO_S_OK ) {						ShowObj(&Obj);						/* Locate it in the I_Index cursor */						rc = Measurement_I_Index_locate(t, &csr2, &Obj);						if (rc == MCO_S_OK) {							/* And show next one */							if ( mco_cursor_next(t, &csr2) == MCO_S_OK &&								Measurement_from_cursor(t, &csr2, &Obj) == MCO_S_OK )								ShowObj(&Obj);						};					};				};break;				case MCO_S_NOTFOUND:					printf("Difference 0.5 is 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");	/* ranged search */	printf("List of measurements with differencial greater or equal to %f\n", RANGE_DIFF );	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Measurement_I_Diff_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* Search for the first occurence */			rc = Measurement_I_Diff_search(t, &csr, MCO_GE, RANGE_DIFF);			for (i=0; i<10 && rc==MCO_S_OK; i++) {				/* get item from cursor */				rc = Measurement_from_cursor(t, &csr, &Obj);				if (rc == MCO_S_OK) {					/* Show the measurement */					ShowObj(&Obj);					/* looking for next occurence */					while (rc == MCO_S_OK) {						int cmp=0;						/* if theresn't next item */						rc = mco_cursor_next(t, &csr);						if ( rc != MCO_S_OK )							break;				  /* then break */						/* if it's diff. is great or equal 0.5 then break; */						rc = Measurement_I_Diff_compare(t, &csr, RANGE_DIFF , &cmp);						if ( rc == MCO_S_OK && cmp >= 0 )							break;						/* else move to next item */					};				};			};		}		else {			printf ("Unable to open a cursor");		};		/* close the tramsaction */		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open a transaction");	};	printf("\n");	/* using composite index I_DiffSum */	printf( "List of all measurements from the series #%d\n",TARGET_SERIES );	/* open a transaction */	rc = mco_trans_start(db,MCO_READ_ONLY,MCO_TRANS_FOREGROUND,&t);	if ( rc == MCO_S_OK ) {		/* open the cursor */		rc = Measurement_I_Index_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* Search for the first occurence */			i = 1;			rc = Measurement_I_Index_search(t, &csr, MCO_EQ, TARGET_SERIES, i);			do {				int cmp=0;				/* compare the item */				rc = Measurement_I_Index_compare(t, &csr, TARGET_SERIES, (uint4)i, &cmp);				if ( rc == MCO_S_OK && cmp == 0 ) {					/* get measurement */					rc = Measurement_from_cursor(t, &csr, &Obj);					if ( rc == MCO_S_OK ) {						/* Show the measurement */						ShowObj(&Obj);						/* move to next measurement */						rc = mco_cursor_next(t, &csr);						i++;					};				}				else {					/* current item is from another serie of measurments */					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");	/* using index on element of structure I_RGB */	printf( "List of the first 10 dark gray or darker (ink <= RGB(128,128,128) ) elements\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 = Measurement_I_RGB_index_cursor(t, &csr);		if (rc == MCO_S_OK ) {			/* Search for the first occurence */			rc = Measurement_I_RGB_search(t, &csr, MCO_LE, RGB(128,128,128) );			for (i=0; i<10 && rc == MCO_S_OK; i++) {				int cmp=0;				/* get measurement */				rc = Measurement_from_cursor(t, &csr, &Obj);				if ( rc == MCO_S_OK ) {					/* Show the measurement */					ShowObj(&Obj);					j++;					/* move to next measurement */					rc = mco_cursor_next(t, &csr);					if (rc == MCO_S_OK) {						/* compare the item */						rc = Measurement_I_RGB_compare(t, &csr, RGB(128,128,128), &cmp);						if (cmp > 0)							break;					};				}			};		}		else {			printf ("Unable to open cursor");		};		/* close the tramsaction */		rc = mco_trans_commit(t);	}	else {		printf ("Unable to open a transaction");	};	if ( j > 0)		printf("\n");	else		printf("None\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 = "randdb";	mco_runtime_info_t info;	_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, randdb_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);			}			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 ) {			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(0);}

⌨️ 快捷键说明

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