📄 sync.c
字号:
/************************************************************ * * * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.* * * ************************************************************//* * This sample demonstrates synchronous events. * */#include "platform.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include "evdb.h"#include "handlers.h"#define DBSIZE (1024*4000)#ifndef MCO_PLATFORM_X64#define PAGESIZE 96#else#define PAGESIZE 192#endif#define NOBJECTS 20const char dbName[] = "eventsDB";const int MAP_ADDRESS = 0x20000000;void _SH_(void) { char text[] = { "\nThis sample demonstrates eXtremeDB synchronous events.\n" "The schema declares <new>, <update> and <delete> events\n" "that are registred with the eXtremeDB at runtime. The\n" "The appropriate event handlers - call back functions, are\n" "called when a new object is created, deleted or updated.\n" "Event handlers print out some data obtained from the valid\n" "object handle passed to the call back function by database\n" "runtime. Note that the handlers are executed on the current\n" "stack, and the database transaction is active at the time.\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();}/* Creates a new object. That triggers the <new_object> event */int new_object(mco_db_h db, int8 *id) { mco_trans_h t; MyClass obj; MCO_RET rc; mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t); rc = MyClass_new ( t, &obj ); MyClass_autoid_get(&obj, id); if (rc) mco_trans_rollback(t); rc = mco_trans_commit(t); return rc;}/* Updates an existing object. That triggetrs <update> events */int update_object(mco_db_h db, int8 autoid) { mco_trans_h t; MyClass obj; MCO_RET rc; uint4 u4; rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t); if (rc) return rc; rc = MyClass_autoid_find (t, autoid, &obj); if (rc) { mco_trans_rollback(t); return rc; } MyClass_u4_get(&obj, &u4); u4++; MyClass_u4_put(&obj, u4); rc = mco_trans_commit(t); return rc;}/* This function deletes starts a transaction to delete and object * identified by its autoid. Before the transaction is committed, * a delete_handler() handler is called, which in turn returns * a non-zero for every other object. That cases the current "delete" * transaction to be aborted at the commit time */int del_object(mco_db_h db, int8 id) { MCO_RET rc; mco_trans_h t; MyClass obj; mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t); rc = MyClass_autoid_find(t, id, &obj); if (rc) { mco_trans_rollback(t); return rc; } rc = MyClass_delete (&obj); rc = mco_trans_commit(t); return rc;}/* fatal error handler */static void errhandler( int n ) { printf( "\neXtremeDB runtime fatal error: %d\n", n ); getchar(); exit( -1 );}int main(void) { int i; int8 u8 = {0}; MCO_RET rc; mco_db_h db = 0; void * start_mem; mco_runtime_info_t info; _SH_(); mco_get_runtime_info( &info); if ( info.mco_shm_supported ) { start_mem = (void*)MAP_ADDRESS; } else { start_mem = (void*)malloc(DBSIZE); if (!start_mem) { printf("Couldn't allocated memory\n"); exit (1); } }; /* set fatal error handler */ mco_error_set_handler( &errhandler ); rc = mco_runtime_start(); rc = mco_db_open ( dbName, evdb_get_dictionary(),start_mem,DBSIZE,(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); if (MCO_S_OK!= register_events(db)) { printf("Error registring evenets, %d\n", rc); if ( !info.mco_shm_supported ) free( start_mem ); exit(1); } for (i=0; i<NOBJECTS;i++) { rc = new_object(db, &u8); if (rc) { printf("Error creating object\n"); break; } rc = update_object(db, u8); if (rc) { printf("Error updating object\n"); break; } rc = del_object(db, u8); if (rc == MCO_S_OK) printf("committed\n"); else printf("rolled back\n"); printf("\n"); } unregister_events(db); /* disconnect from the database, db is no longer valid */ mco_db_disconnect (db); /* destroys the db instance */ mco_db_close (dbName); mco_runtime_stop(); if ( !info.mco_shm_supported ) free( start_mem ); printf("\n\nPress any key to exit"); getchar(); PROG_EXIT(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -