?? lr.c
字號:
/*************************************************************** * * * Copyright (c) 2001-2006 McObject LLC. All Right Reserved. * * * ***************************************************************/#include <platform.h>#include <mco.h>#include <mcolog.h>#include "lrdb.h" /* logging related defines */#define LOG_MAX_FILESIZE 1024*64#define LOG_MAX_FILES_IN_DIR 2 //256#if defined (_VXWORKS) #define LOG_FILEPATH "/tgtsvr/lr_logs"#elif defined (_WIN32_WCE) #define LOG_FILEPATH "lr_logs"#else #define LOG_FILEPATH "lr_logs"#endif/* common defines */#define MAP_ADDRESS 0x20000000const char * dbname = "lrdb";const int SEGSZ = 1024 * 512;#ifndef MCO_PLATFORM_X64const uint2 PAGESIZE = 96;#elseconst uint2 PAGESIZE = 192;#endifvoid * start_mem = (void*)MAP_ADDRESS;mco_db_h db;static mco_runtime_info_t info;static unsigned char deferred_deletion_mode = 1;static unsigned char slave_flag = 1;static unsigned int thread_num = 1;static unsigned int mode_flags = 0;static int flush_depth;static int flush_time;static int test_Mode = 0;THREAD_ID_T td = (THREAD_ID_T)INVALID_HANDLE_VALUE;#define NUMB_OF_ITEMS 1000#define MAX_STRING_LENGTH 100void help(void){ printf ("Usage: log [OPTIONS]\n\n"); printf ("Description: This sample demonstrates the eXtremeDB Transaction Logging.\n" " While populating the database, the sample application calcu-\n" " lates the data checksum. The application can be terminated\n" " \"normally\" by pressing \"Enter\", or \"abnormally\" by press-\n" " ing \"Ctrl-C\" in the console window.\n" " In any case the log files are kept on disk, and when restarted\n" " the sample application attempts to recover the database.\n" " After the recovery is complete, the checksum of the new database\n" " is compared with the checksum of the old one.\n\n"); printf ("Options:\n\n"); printf ("-m Runs as a \"main database process\" -- creates the database\n" " and performs the recovery from the log files\n\n"); printf ("-s Demonstrates the \"multi-process\" database access mode. Runs as\n" " a \"supplementary\" process: it connects to the existing database,\n" " but does not create the database and does not perform the\n" " recovery\n\n"); printf ("-d Demonstrates the \"deferred deletion\" API\n\n"); printf ("-w[I] Demonstrates multi-threaded access to the database. Creates a \n" " number of working threads each with the separate database\n" " connection.\n" " The number of threads I = 1 - 9. Default is 1 thread\n\n"); printf ("Buffering: File buffering options. The runtime typically writes data to an\n" " internal buffer that the operating system writes to disk on a\n" " regular basis. The following options specify how often the\n" " runtime clears the buffers for the log file and causes all\n" " buffered data to be written to disk.\n\n"); printf ("-f[I] Instructs the runtime to flush log file buffers after a number\n" " of commited transactions (transaction queue depth)\n\n" " \"I\" specifies the number of transactions in the queue.\n" " The default depth is 1 (flush buffers after each transaction)\n\n"); printf ("-t[I] Instructs the runtime to flush buffers every \"I\" milliseconds\n" " The default is 100 milliseconds\n\n"); printf ("-n Runs in \"non-buffered\" mode\n"); printf ("-h help\n\n");}void usage(void){ printf ("Usage: log [-d]\n" " [-m]\n" " [-s]\n" " [-w [number_of_working_threads] ]\n" " [-f [transaction_depth] ]\n" " [-t [milliseconds] ]\n" " [-n]\n" " [-h]\n\n");}void _SH_(void){ printf("\neXtremeDB runtime version %d.%d, build %d\n\n", MCO_COMP_VER_MAJOR, MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM);}/* random related stuff */double rrand(double range) { return (((double)rand()) / ((double)RAND_MAX) ) * (range-1.0);};/* generate random string having random length*/char* rand_string() { int l = (int)rrand(MAX_STRING_LENGTH)+1; int i; char* pc = (char*)malloc(l+1); for (i=0;i<l;i++) { pc[i] = 'a'+(char)rrand(26); }; pc[l] = 0; return pc;};/* eXtremeDB fatal error handler */static void errhandler( int n ){ Printf( "\n*** eXtremeDB runtime fatal error: %d\n", n ); EXIT( -1 );};/* initialize the database */void init_database() { MCO_RET rc; Printf( "Starting up the database"); /* alloc memory for the database */ mco_get_runtime_info( &info); if ( info.mco_shm_supported == 0 ) { start_mem = (void*)malloc( SEGSZ ); if ( start_mem == 0 ) { Printf( "Unable to allocate amount of memory\n"); EXIT(-1); }; }; Printf( "." ); /* start the eXtremeDB engine and setup fatal error handler */ mco_runtime_start(); mco_error_set_handler( &errhandler ); Printf( "." ); /* make the database */ if(slave_flag == 1) { mco_db_kill(dbname); rc = mco_db_open( dbname, lrdb_get_dictionary(), start_mem, SEGSZ, PAGESIZE ); if ( rc ) { Printf ( "Couldn't open database: %d\n",rc); mco_runtime_stop(); EXIT( -1 ); } } Printf( "." ); /* connect to the database */ if ( (rc = mco_db_connect( dbname, &db )) != MCO_S_OK) { Printf("Error connecting to database %d", rc); EXIT(-1); } Printf( "." ); Printf( "Done\n" );};/* clean the database */void free_database() { Printf( "\nCleaning up the database"); /* disconnect and close the database */ mco_db_disconnect( db ); Printf( "." ); mco_db_close( dbname ); Printf( "." ); /* stop eXtremeDB engine */ mco_runtime_stop(); Printf( "." ); /* cleanup */ if ( info.mco_shm_supported == 0 ) { free(start_mem); }; Printf( "Done\n" );};/* create an object */MCO_RET make_an_object ( mco_trans_h t, unsigned int index ) { MCO_RET rc = MCO_S_OK; Obj obj; /* make an object */ if ( MCO_S_OK == ( rc = Obj_new( t, &obj))) { char *ps = rand_string(); /* and fill it up */ Obj_index_put( &obj, index ); Obj_value_put( &obj, ps, (uint2)strlen(ps) ); rc = Obj_checkpoint( &obj ); }; return rc;};/* fill up the database */MCO_RET populate_database () { unsigned int i; MCO_RET rc = MCO_S_OK; mco_trans_h t; Printf( "Populating the database"); /* makes a lot of objects */ for ( i=0 ; rc == MCO_S_OK && i < NUMB_OF_ITEMS; i++ ) { if ( i % 100 == 0 ) { Printf( "." ); Sleep(10); }; /* open a transaction */ if ( MCO_S_OK != (rc = mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t ) )) { /* error */ Printf( "Unable to start a transaction\n"); } else { /* OK */ /* make an object */ if ( MCO_S_OK == (rc = make_an_object( t, i ))) { /* commit transaction */ rc = mco_trans_commit( t ); } else { /* rollback transaction */ mco_trans_rollback( t ); }; }; }; if ( rc != 0 ) { Printf( "Error: %d\n", rc); } else { Printf( "Done\n" ); }; return rc;};/* show the database info */void show_db_info () { mco_puint mn, mx, fp, tp; MCO_RET rc = MCO_S_OK; mco_trans_h t; mco_cursor_t c; Obj obj; char buf[MAX_STRING_LENGTH+1], *p; uint2 len; unsigned int checksum = 0; /* open a transaction */ if ( MCO_S_OK != (rc = mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t ) )) { /* error */ Printf( "Unable to start a transaction\n"); } else { /* OK */ /* open index cursor */ if ( MCO_S_OK != (rc=Obj_index_index_cursor( t, &c ))) { /* error */ Printf( "Unable to open a cursor\n"); } else { /* get memory status */ mco_db_free_pages ( db, &fp ); mco_db_total_pages ( db, &tp ); /* get first index */ mco_cursor_first( t, &c ); Obj_from_cursor( t, &c, &obj ); Obj_index_get( &obj, &mn ); /* calculate the checksum of the database */ do { /* get the string field */ Obj_from_cursor( t, &c, &obj ); Obj_value_get( &obj, buf, sizeof(buf), &len ); /* sum the field's chars */ p = buf; while ( *p ) { checksum += *p; p++; }; } while ( mco_cursor_next( t, &c ) == MCO_S_OK ) ; /* get last index */ mco_cursor_last( t, &c ); Obj_from_cursor( t, &c, &obj ); Obj_index_get( &obj, &mx ); /* show the information */#if (defined (_VXWORKS) && (CPU==SIMNT)) || defined(_WIN32_WCE) Printf( "Memory: %.2f%% free, Min index:%d Max index:%d Checksum:%X\n", (double)(fp)/(double)tp * 100.0, mn, mx, checksum);#else Printf( "\rMemory: %.2f%% free, Min index:%d Max index:%d Checksum:%X", (double)(fp)/(double)tp * 100.0, mn, mx, checksum);#endif }; mco_trans_rollback( t );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -