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

📄 main.c

📁 eXtremeDB数据库在Linux平台下的使用源码。内含多个示例程序
💻 C
字号:
/***************************************************************** *                                                               * * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.     * *                                                               * *****************************************************************//* *  This sample demonstrates the recovery when one of the processe * working with the database fails. */#include <platform.h>#include <stdio.h>#include <stdlib.h>#include <string.h>//#include <mcoHA.h>#include "monitorDB.h"#include "sensor.h"//#include "mcocfg.h"//#define   TRANSACTION_BREAK     1#define   LOOP_BEFORE_BREAK       10000#define   DEFAULT_WATCHDOG_TIME   1000/* Make sure you've got this 16M, otherwise you'll be measuring * the performance of your disk. */#define DBSIZE                    (16*1024*1024)#ifndef MCO_PLATFORM_X64#define PAGESIZE                  128#else#define PAGESIZE                  256#endif#define MAX_INSTANCES             9#define MAX_THREADS               9const int MAP_ADDRESS =           0x20000000;#define S1_RECORDS	              100 // 100000   #define S2_RECORDS	              200 // 1000#define S3_RECORDS	              2#define S4_RECORDS	              3 // 1000#define REPORT_COUNTER	          10000DEFINE_PROCESS_MASKS(set);extern void make_strings( void );mco_db_h      db;void *	      start_mem;char          dbName[30] = "Recovery_DB";int count   = 0;int           stop_flag   = 0;THREAD_ID     thWorking;THREAD_ID     thCheckRes;int           CheckTime =   DEFAULT_WATCHDOG_TIME;/*************************** *    program banners      * ***************************/void usage(void){  Printf ("Usage: \trecov3\t[-t][watchdog time] | \n");  Printf ("\t\t[-h]\n");}void help(void){    Printf ("Usage: \trecov3 [OPTIONS]\n\n");  Printf (		"\nThis sample demonstrates the recovery when one of the processes\n"		"working with the database fails. Please run some instances of this program\n"		"and then kill one of them by <CTRL><C>. One of the rest processes will repare\n"		"resources and recover the database.\n"          );  Printf ("\nOptions:\n\n");  Printf ("-t[time]     Sets the resource watchdog time\n"          "              time = timeout value in milliseconds\n"          "              Default is 1000 ms\n"	  ); Printf  ("-h            help\n");}void _SH_(void) {  Printf("\neXtremeDB-HA runtime version %d.%d, build %d\n\n",    MCO_COMP_VER_MAJOR, MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM);    usage();}static void errhandler( int n ) {	printf( "\neXtremeDB runtime fatal error: %d\n", n );	getchar();	exit( -1 );}/* parsing command line */int parse_cmd(int argc, char **argv ){  int   i;  char  *p;  int   flag  = 1;   if (argc < 2)  {    return 1;  }  for (i=1;i<argc;i++)  {    p=argv[i];    if ( (p[0] == '/')||(p[0] == '-') )    {      if(p[1] == 'h') {        help();        return 0;      }      flag++;            if(p[1] == 't') {        flag++;        CheckTime=atoi((const char*) &p[2]);	      if(CheckTime == 0)    	  CheckTime = DEFAULT_WATCHDOG_TIME;      }    }  }  return flag;}/**************************************************** *          "Check Resources" thread  ****************************************************/THREAD_PROC_DEFINE(ResourceWatchdog, arg){  mco_db_h  db = 0;  unsigned  wdt_state = 0;    THREAD_PROC_MODE();    for(;;) {      Sleep(CheckTime);      if( stop_flag )  {	      stop_flag--;	      return;      }      if(!mco_db_check_resources( dbName, &db, -1, &wdt_state)) {        printf("\n\n*** Recovery was done ***\n\n");//        Sleep(5000);      }    }}/**************************************************** *          working thread  ****************************************************/THREAD_PROC_DEFINE(Working, arg){  int		  i;  mco_runtime_info_t info;  MCO_RET         rc;  mco_trans_h     t;  unsigned long   counter;  timer_unit      time = 0, time2 = 1000;    THREAD_PROC_MODE();    mco_get_runtime_info( &info);    make_strings();   /* try to connect to the database, obtain a database handle */    printf("\nconnecting to the database: ... ");    if(       ((rc = mco_db_connect( dbName, &db )) != MCO_S_OK)      )    {      /* database doesn't exist. Creating the new database */	    if ( info.mco_shm_supported ) {	      start_mem = (void*)((ULONG)MAP_ADDRESS + ((DBSIZE+4095)&(-4096)));	    }      else {	      start_mem = (void*)malloc(DBSIZE);	      if (!start_mem) {		      printf("Couldn't allocate memory\n");		      exit (1);	    }    }	  mco_db_kill( dbName );  	  rc = mco_db_open(        dbName,        monitorDB_get_dictionary(),        start_mem,        DBSIZE,        (uint2) PAGESIZE        );	  if ( rc ) {	    printf( "\nerror %d creating database", rc );	    if ( !info.mco_shm_supported )		    free( start_mem );	      exit( 1);      }      if(    	    (rc = mco_db_connect( dbName, &db )) != MCO_S_OK        )	    {	      printf( "\nCouldn't connect to the database %s %d", dbName,  rc );	      exit( 1);	    }      Printf ("\nPopulating initial data...");      s1_init(db, 1);    }    printf(" done\n");    time  = mco_system_get_current_time();    for (i=0, counter=1; !stop_flag; counter++, i++ ) {      if(!i) time  = mco_system_get_current_time();      rc = mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t );      if ( rc )      {         Printf( "\nerror starting write transaction %d\n", rc );        exit( 1 );      }      s1_new_record(t, 1000000+counter);#ifdef  TRANSACTION_BREAK      if(i >=  LOOP_BEFORE_BREAK) {printf("transaction is broken\n");        EXIT(1);      }#endif      mco_trans_commit(t);      if(i == REPORT_COUNTER) {          if( (time = mco_system_get_current_time() - time) ==0) time++;        time2 = (time2+(time))/2;        Printf("%ld ms for %d transactions\n",		            time2,		            i          	  );        Sleep(100+ counter%100);        i = -1;      }    }    /* disconnect from the database, db is no longer valid */    Printf(" Disconnecting from the database\n");   /* destroy the database */    Printf(" Closing database\n");    if ( !info.mco_shm_supported )	    free( start_mem );    stop_flag--;    Sleep(50);}#ifndef WIN32_WCEint main(  int argc, char **argv )#elseint __cdecl main(  int argc, char **argv )#endif{  const char  *dbName = "perf2";  MCO_RET     rc;  mco_db_h    db = 0;  long 	      n = 0;  uint4	      key = 1999;    PROCESS_MASKS();         // for LINUX, QNX, SOLARIS    {      mco_runtime_info_t info;      mco_get_runtime_info( &info );      if( !info.mco_recovery_supported ) {        printf("\n\n Recovery support is not configured! \n");        PROG_EXIT(0);      }    }        _SH_();  /* parse command line */      if (parse_cmd(argc,argv )==0) {  #ifdef _WIN32_WCE      char str[128];      printf("\npress ENTER to exit\n");      gets(str);  #endif      PROG_EXIT(0);    }    mco_error_set_handler( &errhandler );    rc= mco_runtime_start();    /* run working thread */    createThread(Working, 0, &thWorking);    Sleep(100);    /* run "Resource's Watchdog" thread */    if(CheckTime != 0)      createThread(ResourceWatchdog, 0, &thCheckRes);    printf( "\nPress Enter to exit\n" );/************************************************** * wait for a keystroke to stop test **************************************************/    {      int i;      char s[256];      FILE * f = stdin;                 s[0] = 0;	    for(;;){#ifdef _VXWORKS	      i = 0;	      while(i == 0) {	          ioctl (f->_file, FIONREAD, (int)&i);	          Sleep(100);	      }#endif        if((fgets(s,256,stdin)) == 0) {          continue;        }        if (s[0] ==0 ) continue;        i = atoi(s);        if ( i == 343 ) EXIT(1);        break;      }      if(CheckTime) {        stop_flag = 2;        CheckTime = 0;      }      else {        stop_flag = 1;      }      Printf("\n\nApplication interrupted by keystroke\n");      Printf("\n\nwaiting for completion\n");      while( stop_flag ) {        Sleep(100);  /* wait for terminatin of working threads */      }    }    mco_runtime_stop();    EXIT(0);}

⌨️ 快捷键说明

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