📄 cursor.c
字号:
/*************************************************************** * * * Copyright (c) 2001-2007 McObject LLC. All Right Reserved. * * * ***************************************************************//* This samples demonstartes basic cursor methods */#include <platform.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "samples.h"static const char * dbname = "SimpleDb";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;void _SH_(void) { char text[] = { "\nThis samples demonstartes basic eXtremeDB cursor methods.\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();}void cursor( mco_db_h db ) { MCO_RET rc = 0; mco_cursor_t csr; mco_trans_h trn; SimpleClass simple; uint2 a, b, size; uint4 h; char src[64]; simple_oid oid; MCO_CURSOR_TYPE ctype; printf( "\n\nReading using \"UniqueIndex\"\n" ); /* start read-only transaction and initialize a cursor. */ mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &trn ); /* initialize cursor */ rc = SimpleClass_Iabc_index_cursor( trn, &csr ); if ( rc != MCO_S_OK ) { /* if not found for whatever reason - close the transaction and * bail out */ mco_trans_commit( trn ); return; } /* commit the transaction. This is only done to illuatrate the fact that * cursors could be used across the transaction boundaries. In * real-life application the "commit" is not needed and shouldn't be * issued. */ rc = mco_trans_commit( trn ); /* re-open a transaction */ mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &trn ); /* check whether the cursor is still valid */ if ( MCO_S_OK != mco_cursor_type(&csr, &ctype) ) { /* cursor invalid... */ printf( "\n\tcursor invalid" ); /* ..reinitialize it. */ rc = SimpleClass_Iabc_index_cursor( trn, &csr ); } for ( rc = mco_cursor_first(trn, &csr); /* position the cursor at the first object */ rc == MCO_S_OK; rc = mco_cursor_next(trn, &csr) /* advance the cursor (fetch)*/ ) { /* obtain object handle through the current cursor */ rc = SimpleClass_from_cursor( trn, &csr, &simple ); if ( rc != MCO_S_OK ) { printf( "\n\terror:%d", rc ); break; } /* read data out from handle */ SimpleClass_a_get( &simple, &a ); SimpleClass_b_get( &simple, &b ); SimpleClass_h_get( &simple, &h ); SimpleClass_c_get( &simple, src, (uint2) sizeof(src), &size ); /* obtain OID for the object */ rc = SimpleClass_oid_get( &simple, &oid ); if ( rc == MCO_S_OK ) printf( "\n\tobject with OID=%d\n\ta=%d,b=%d,c=%s(%d)\n\n", oid.seq, a, b, src, size ); else printf( "\n\terror:%d", rc ); } /* commit */ mco_trans_commit( trn ); return;}/* fatal error handler */static void errhandler( int n ) { printf( "\neXtremeDB runtime fatal error: %d", n ); getchar(); exit( -1 );}int main( void ) { 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, simple_get_dictionary(), start_mem, FIRST_SEGSZ, (uint2) PAGESIZE ); if ( rc ) { printf( "\nerror creating database" ); if ( !info.mco_shm_supported ) free( start_mem ); exit( 1 ); } /* connect to the database, obtain a database handle */ mco_db_connect( dbname, &db ); printf( "\ndb handle = %d\n", 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] ); } cursor( db ); /* 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 + -