📄 lr.c
字号:
}; if ( rc != 0 ) { Printf( "Error: %d\n", rc); };};MUTEX_T Lock;unsigned char mode = 0;THREAD_PROC_DEFINE( work_with_database, p) { unsigned int passes = 1, i; MCO_RET rc = MCO_S_OK; mco_trans_h t; mco_cursor_t c; Obj obj; for (; rc == MCO_S_OK ;) { /* check stop-condition */ MUTEX_LOCK( &Lock ); if ( mode != 0 ) { break; } MUTEX_UNLOCK( &Lock ); /* delete first 10 objects every 10 passes */ if ( passes % 10 == 0 ) { /* delete first 10 items */ for ( i=0; i<10; i++) { /* 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"); break; } else { /* OK */ /* get index cursor */ Obj_index_index_cursor( t, &c ); /* get the first object */ mco_cursor_first( t, &c ); Obj_from_cursor ( t, &c, &obj ); /* delete the object */ if ( MCO_S_OK == (rc = Obj_delete( &obj ))) { /* commit transaction */ rc = mco_trans_commit( t ); } else { /* rollback transaction */ mco_trans_rollback( t ); break; }; }; }; } /* making the snapshot every 15 passes */ if(slave_flag) { if((int)p == 0) { if ( passes % 15 == 0 ) { if ( MCO_S_OK != (rc=mco_log_save( db, deferred_deletion_mode ))) { Printf ("\nUnable to make snapshot of the database. Error %d\n", rc ); } } } } /* make an object */ /* 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 */ /* get index cursor */ Obj_index_index_cursor( t, &c ); /* get the last object */ mco_cursor_last( t, &c ); Obj_from_cursor ( t, &c, &obj ); /* get the last index value */ Obj_index_get( &obj, &i ); i++; /* 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 ); }; }; /* show the database state */ show_db_info (); /* wait and go to the next cycle */ Sleep( 100 ); passes ++; }; mode--; MUTEX_UNLOCK( &Lock );};/* deferred deletion thread */THREAD_PROC_DEFINE( deferred_deletion, p ) { THREAD_PROC_MODE() mco_log_deferred_deletion( db );}mco_bool FlushTimer (struct flush_timer_* handle){ if(handle->flush_time) { if ( handle->time_elapsed == 0 ) handle->time_elapsed = mco_system_get_current_time(); if( (mco_system_get_current_time()-handle->time_elapsed) > handle->flush_time) { handle->time_elapsed = 0; return TRUE; } } return FALSE;}/* parse command line */int parse_cmd_line(int argc, char* argv[]){ int i, flag = 0; for(i=1; i < argc; i++) { if (strstr(argv[i],"--test")) test_Mode = 1; else if(argv[i][0] == '-') { if( (argv[i][1] == 'd') || (argv[i][1] == 'D') ) { flag ++; deferred_deletion_mode = 0; /* clear "deferred deletion" flag if "-d" specified */ } if((argv[i][1] == 's') || (argv[i][1] == 'S')) { flag ++; slave_flag = 0; /* set "slave mode" flag if "-s" specified */ } if((argv[i][1] == 'm') || (argv[i][1] == 'M')) { flag ++; slave_flag = 1; /* set "master mode" flag if "-m" specified */ } if((argv[i][1] == 'w') || (argv[i][1] == 'W')) { flag ++; if( !(thread_num = atoi( &argv[i][2])) ) /* set the desired number of working threads */ thread_num = 1; if( thread_num > 9) thread_num = 9; } if((argv[i][1] == 'f') || (argv[i][1] == 'F')) { flag ++; if( !(flush_depth = atoi( &argv[i][2])) ) /* set the desired counter of transactions */ flush_depth = 1; } if((argv[i][1] == 't') || (argv[i][1] == 'T')) { flag ++; if( !(flush_time = atoi( &argv[i][2])) ) /* set the desired flush time */ flush_time = 100; } if((argv[i][1] == 'h') || (argv[i][1] == 'H')) { help(); EXIT(0); } if((argv[i][1] == 'n') || (argv[i][1] == 'N')) { flag ++; mode_flags |= MCO_LOG_NONBUFFERED_IO; /* nonbuffered mode */ } } } return flag;}int main ( int argc, char* argv[]) { MCO_RET rc,rc1; THREAD_ID_T tw; int i; flush_timer_t flush_timer; rc=rc1=0; _SH_(); /* common initialization */ srand( time(0) ); MUTEX_INIT( &Lock ); /* parse the command line */ if( !parse_cmd_line( argc, argv ) ) {#ifndef _WIN32_WCE usage();#else char str[128]; usage(); Printf("\nPress ENTER to exit\n"); gets(str);#endif PROG_EXIT(0); } /* initialize the database */ init_database(); /* initialize transaction logging */ { log_args_t args; args.Filepath = LOG_FILEPATH; args.maxlogsize = LOG_MAX_FILESIZE; args.maxfiles = LOG_MAX_FILES_IN_DIR; args.flags = mode_flags; // MCO_LOG_CLOSE | MCO_LOG_NONBUFFERED_IO flags args.flush_depth = flush_depth; args.flush_timer = &flush_timer; flush_timer.flush_time = flush_time; // flush time in milliseconds flush_timer.TimerProc = FlushTimer; rc = mco_log_start(db, &args ); } if ( MCO_S_OK != rc) { Printf( "Unable to initialize transaction log. Error: %d\n", rc); } else { if(slave_flag) { /* Try to restore the database from logged data */ Printf ("Try to restore database: "); if ( MCO_S_OK != (rc = rc1 = mco_log_recover( db )) ) { Printf( " Failed. Error %d",rc); switch ( rc ) { case MCO_E_LOG_IMG_NOT_FOUND: {rc=0;Printf( " \"Logs were not found\"." ); break;} case MCO_E_LOG_BROKEN_RECORD: Printf( " \"Broken record\"." ); if (test_Mode) goto Exit; break; default: Printf( " \"Database image or logs are broken\"." ); goto Exit; }; Printf( "\n" ); /* Make initial snapshot of the database */ Printf ("Making database's snapshot: "); if ( MCO_S_OK != (rc=mco_log_save( db, 1 ))) { Printf ( "Failed. Error %d\n", rc ); goto Exit; } else { Printf ( "OK\n"); }; if ( rc1 == MCO_E_LOG_IMG_NOT_FOUND) { /* fill the database */ Printf ( "Old database doesn't exist.\n"); if (MCO_S_OK != (rc=populate_database())) goto Exit; } } else { Printf( " Done\nThe database's state is:\n" ); show_db_info (); Printf( "\n" ); }; } else { Printf ( "Slave mode, database is assumed to be recovered\n"); } /* start "deferred deletion" if the flag specified */ if ( deferred_deletion_mode == 0 ) { Printf( "\"Deferred deletion\" is enabled. Starting thread:" ); THREAD_PROC_START( deferred_deletion, 0, &td ); Printf( "Done\n" ); }; Printf( "Press any key to normal exit or Ctrl+C to terminate the program\n"); /* show current state of the database */ show_db_info (); /* starts "working hard" */ for(i=0;i<(int)thread_num;i++) { THREAD_PROC_START( work_with_database, (void*)i, &tw ); } /* waits for user's choose */ if (test_Mode) Sleep(15000); getchar(); /* stop the thread */ MUTEX_LOCK( &Lock ); mode = (int)thread_num; MUTEX_UNLOCK( &Lock ); for (;;) { MUTEX_LOCK( &Lock ); if ( mode ==0 ) break; MUTEX_UNLOCK( &Lock ); Sleep( 100 ); }; MUTEX_UNLOCK( &Lock );Exit: mco_log_stop( db ); }; /* cleanup */ if(td != (THREAD_ID_T)INVALID_HANDLE_VALUE) THREAD_CANCEL(td); free_database(); Sleep(200); MUTEX_DESTROY( &Lock ); PROG_EXIT(rc);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -