?? db.c
字號(hào):
/***************************************************************
* *
* Copyright (c) 2001-2007 McObject LLC. All Right Reserved. *
* *
***************************************************************/
/*
* This sample shows how to use mco_db_save() and mco_db_load() APIs. These APIs are
* used to save and load in-memory database to and from any stream device, such as
* a file or socket.
* The application presented here was used as actual test to verify the "correctness"
* of the save and load operations. The schema file dbtest.mco describes declares classes
* with various data types and various layouts.
*
* The project consists of several C files:
*
* dbinit.c - functions to initialize the database with the test data.
* db.c - main driver.
* dbtest.c - generated interface implemetation file
* strm.c - FILE stream implemetaion
* verify.c - functions that read the database back after it has been loaded with mco_db_load
* and validates the results.
*/
#include "platform.h"
#include "load.h"
const mco_puint MAP_ADDRESS = 0x20000000;
static void _SH_(void)
{
char text[] =
{
"\nThis sample shows how to use mco_db_save() and mco_db_load() APIs.\n"
"These APIs are used to save and load in-memory database to and from\n"
"any stream device, such as a file or a socket.\n"
"The application presented here was used as an actual test to verify the\n"
"\"correctness\" of the save and load operations. It populates a database\n"
"that contains classes with the \"fixed\", and \"dynamaic\" fields, a class\n"
"with blobs, a class with declared with \"autoid\" and \"histoty\", etc.\n"
"The applicataion then saves the database to the file, closes the connection,\n"
"re-opens the database and loads the saved image into the memory. It then\n"
"performs sanity check on the loaded data and prints out the results\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();
}
static void errhandler(int n)
{
printf("\neXtremeDB runtime fatal error: %d", n);
getchar();
exit( - 1);
}
mco_runtime_info_t info;
int main(void)
{
MCO_RET rc;
mco_db_h db = 0;
FILE* f;
void* start_mem;
_SH_();
mco_get_runtime_info(&info);
if (!info.mco_save_load_supported)
{
printf("This sample requires SAVE/LOAD runtime support\n");
exit(0);
}
if (info.mco_shm_supported)
{
start_mem = (char*)MAP_ADDRESS;
}
else
{
start_mem = (char*)malloc(DBSIZE);
if (!start_mem)
{
printf("Couldn't allocated memory\n");
exit(1);
}
};
mco_error_set_handler(&errhandler);
mco_runtime_start();
/* Create a database - allocate 2M starting from mem. */
rc = mco_db_open("dbtest", dbtest_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("dbtest", &db);
printf("Initializing database...");
rc = init_database(db);
if (rc)
{
printf("\tfailed, rc=%d\n\n", rc);
mco_db_disconnect(db);
goto end;
}
printf("\tsuccess\n");
printf("Saving database to %s...", fname);
f = fopen(fname, "wb");
rc = mco_db_save(f, file_writer, db);
if (rc)
{
fclose(f);
printf("\tfailed, %d\n", rc);
goto end;
}
printf("\tsuccess\n\n");
fclose(f);
/* disconnect from the database, db is no longer valid */
mco_db_disconnect(db);
mco_db_close("dbtest");
printf("Loading data from %s...", fname);
f = fopen(fname, "rb");
rc = mco_db_load(f, file_reader, "dbtest", dbtest_get_dictionary(), start_mem, DBSIZE);
fclose(f);
if (rc)
{
printf("\tfailed, rc=%d\n\n", rc);
goto end;
}
else
{
printf("\tsuccess\n");
}
mco_db_connect("dbtest", &db);
printf("Verifying data pointers ...");
rc = verify_db(db);
if (rc)
{
printf("\tfailed\n\n");
}
else
{
printf("\tsuccess\n\n");
}
printf("Verifying object counters...\n\n");
printf("\tWritten\t%d objects to class \"Dynamic\"\n", nd);
printf("\tWritten\t%d objects to class \"Fixed\"\n", nf);
printf("\tWritten\t%d objects to class \"Blobs\"\n", nb);
printf("\tWritten\t%d versions for class \"Blobs\"\n", nh);
printf("\tWritten\t%d vector elements to class \"Idxs\"\n\n", ni);
printf("\tRead\t%d objects from class \"Dynamic\"\n", dv);
printf("\tRead\t%d objects from class \"Fixed\"\n", fv);
printf("\tRead\t%d objects from class \"Blobs\"\n", bv);
printf("\tRead\t%d version from class \"Blobs\"\n", bh);
printf("\tRead\t%d vector elements from class \"Idxs\"\n\n", iv);
mco_db_disconnect(db);
end: mco_db_close("dbtest");
mco_runtime_stop();
if (!info.mco_shm_supported)
{
free(start_mem);
}
printf("Press Enter to finish\n");
getchar();
PROG_EXIT(0);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -