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

📄 psearch.c

📁 extremeDB s sample code,useful for you
💻 C
字号:
/*************************************************************** *                                                             * * Copyright (c) 2001-2006 McObject LLC. All Right Reserved.   * *                                                             * ***************************************************************//* This samples demonstartes pattern search methods */#include "test.h"#include <platform.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <mcowrap.h>static const char   *   dbname = "test";static const int        FIRST_SEGSZ = 1024 * 8000UL;#ifndef MCO_PLATFORM_X64static const int        PAGESIZE = 96;#elsestatic const int        PAGESIZE = 192;#endifconst int MAP_ADDRESS =  0x20000000;#define PSEARCH_K1 "abc_1*"#define PSEARCH_K2 "??c_?2?"#define PSEARCH_K3 5#define NUMOBJECTS    20void _SH_(void) {	char text[] = {		"\nThis samples demonstartes basic eXtremeDB pattern search methods.\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();}void psearch( mco_db_h db, char *pattern1, char *pattern2, int p3 ) {	MCO_RET                 rc = 0;	mco_trans_h             trn;	TestClass               test;	uint2                   size1;		uint4                   v1;	char                    key[64];	char                    k2[64];	int4                    k3;	uint4 bsize;	void *buf;	mco_pattern_policy_t p;	/*	 * Get amount of memory needed for set of patterns	 */	TestClass_i1_pattern_size(pattern1,strlen(pattern1),pattern2,strlen(pattern2),0,&bsize);		/*	 * Allocate memory	 */	buf=malloc(bsize);		/* start read-write transaction and initialize	a cursor.	 */	mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &trn );	printf("\n");	/*	 *  You can change '?' and '*' symbols to any ascii char	 *  or you can ignore other field than char<n> or string	 *	 *  NOTE: change policy operation require RW transaction!	 *  	 */ 		mco_get_pattern_policy(trn,&p);	p.ignore_other_fields=0;	mco_set_pattern_policy(trn,&p);			/*	 * Pattern search	 */		printf("\n\tStart search using all patterns and other index components\n");		for(rc = TestClass_i1_pattern_search (trn, buf, bsize, &test, 					  pattern1,strlen(pattern1),pattern2,strlen(pattern2),p3);	    rc == MCO_S_OK ;rc = TestClass_i1_pattern_next(trn,buf,&test)){	  TestClass_v1_get( &test, &v1 );	  TestClass_k3_get( &test, &k3 );	  TestClass_key_get( &test, key, (uint2) sizeof(key), &size1 );	  TestClass_k2_get( &test, k2, (uint2) sizeof(k2));	  printf( "\t Found: v1=%d, key=%s(%d), k2=%s(%d), k3=%d\n", v1, key, size1 , 		  k2,strlen(k2),k3);	}	p.ignore_other_fields=1;	mco_set_pattern_policy(trn,&p);	printf("\n\tStart search using all patterns and ignore other index components\n");	for(rc = TestClass_i1_pattern_search (trn, buf, bsize, &test, 					  pattern1,strlen(pattern1),pattern2,strlen(pattern2),p3);	    rc == MCO_S_OK ;rc = TestClass_i1_pattern_next(trn,buf,&test)){	  TestClass_v1_get( &test, &v1 );	  TestClass_k3_get( &test, &k3 );	  TestClass_key_get( &test, key, (uint2) sizeof(key), &size1 );	  TestClass_k2_get( &test, k2, (uint2) sizeof(k2));	  printf( "\t Found: v1=%d, key=%s(%d), k2=%s(%d), k3=%d\n", v1, key, size1 , 		  k2,strlen(k2),k3);	}	free(buf);	mco_trans_commit( trn );	return;}int newobj( mco_db_h db, int id0, uint4 vh ) {	MCO_RET             rc;	mco_trans_h         t;	TestClass hClass;	test_oid  id;	char                src1[64];	char                src2[64];	int                 donetrn = 0;	uint2               v1 = ( uint4 ) ( rand() & 0x3FF );	int4                k3=( uint4 ) ( rand() % 10);	id.seq = id0;	/*	 * open a transaction and obtain a transaction handle. This transaction is a	 * read_write transaction, ran at the FOREGROUND priority level.	 */	mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t );	/*	 * Allocate a new class and return a class handle. Two input parameters must be	 * passed to the new interface: the transaction handle t and class id. If	 * successful the output parameter hClass is a pointer to the newly allocated class.	 */	rc = TestClass_new( t, &id, &hClass );	if ( rc ) goto Err;	/* assign values, two strings and one integer. By this time, we must	 * know the string size already. Note that return value is not checked	 * with every call. In case of an error, runtime sets the transaction into	 * set "error state" and will return the error indication at commit	 */	TestClass_v1_put( &hClass, v1 );	TestClass_k3_put( &hClass, k3 );	sprintf( src1, "abc_%03d", rand() & 0xff );	TestClass_key_put( &hClass, src1, (uint2) strlen(src1) );	sprintf( src2, "abc_%03d", rand() & 0xff );	TestClass_k2_put( &hClass, src2, (uint2) strlen(src2) );	/* 	 * commit the transaction unless there is a problem and return 1, otherwise	 * rollback and return 0	 */	rc = mco_trans_commit( t );	donetrn = 1;	/*	 * Important! After the transaction was committed, the class handle is no longer	 * valid. Any attempt to reference the created object would result in error	 * condition.	 */	if ( rc ) goto Err;	printf( "\n\t v1=%d, k1=%s, k2=%s k3=%d\tinserted okay", v1, src1, src2, k3 );	return 1;	Err:	printf( "\n\t error (%d) inserting object with OID %d", rc, id.seq );	if ( !donetrn ) mco_trans_rollback( t );	return 0;}/* fatal error handler */static void errhandler( int n ) {	printf( "\neXtremeDB runtime fatal error: %d", n );	getchar();	exit( -1 );}int main( int argc, char **argv ) {	MCO_RET     rc;	mco_db_h    db = 0;	int         num = NUMOBJECTS, one_id[NUMOBJECTS], vhash[NUMOBJECTS];	char    *  start_mem;	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(FIRST_SEGSZ);		  if (!start_mem) {	    printf("Couldn't allocated memory\n");	    exit (1);	  }	}		/* set fatal error handler */	mco_error_set_handler( &errhandler );	mco_runtime_start();	/* Create a database - allocate 2M starting from mem. */	rc = mco_db_open( dbname, test_get_dictionary(), start_mem, FIRST_SEGSZ, (uint2) PAGESIZE );	if ( rc ) {		printf( "\nerror creating database" );		free( start_mem );		exit( 1 );	}	/* connect to the database, obtain a database handle */	mco_db_connect( dbname, &db );	printf( "\ndb handle = %d\n\tFilling database with random values:", db );	srand( (unsigned) time(NULL) );	while ( 0 < num-- ) {		/* get some random values */		one_id[num] = rand();		vhash[num] = ( uint4 ) rand();		/* create a new object */		newobj( db, one_id[num], vhash[num] );	}	if (argc<4) {		printf( "\n\n\tPattern search keys: \"%s\", \"%s\", %d\n", PSEARCH_K1, PSEARCH_K2, PSEARCH_K3 );		psearch( db, PSEARCH_K1, PSEARCH_K2, PSEARCH_K3 );	} else {		printf( "\n\n\tPattern search keys: \"%s\", \"%s\", %d\n", argv[1], argv[2], atoi(argv[3]) );		psearch( db, argv[1], argv[2], atoi(argv[3]) );	};	/* disconnect from the database, db is no longer valid */	mco_db_disconnect( db );	/* destroy the db */	mco_db_close( dbname );	mco_runtime_stop();	if ( !info.mco_shm_supported )	  free( start_mem );		printf( "Press Enter key to exit" );        getchar();  PROG_EXIT(0);}

⌨️ 快捷键说明

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