📄 db.c
字号:
/***************************************************************
* *
* Copyright(c) 2001-2005 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"
#include "mcoxml.h"
const int MAP_ADDRESS = 0x20000000;
static void _SH_(void)
{
char text[] =
{
"\nThis sample shows how to use mco_xml_export() and mco_xml_import() APIs.\n"
"These APIs are used to export and import in-memory database in form of XML document 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-2003 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;
#define SETUP_POLICY
int main(void)
{
MCO_RET rc;
mco_db_h db = 0;
FILE* f;
mco_xml_policy_t op, np;
mco_trans_h t;
void* start_mem;
/* setup policy */
np.blob_coding = MCO_TEXT_BASE64;
np.encode_lf = MCO_YES;
np.encode_nat = MCO_YES;
np.encode_spec = MCO_YES;
np.float_format = MCO_FLOAT_FIXED;
np.ignore_field = MCO_YES;
np.indent = MCO_YES; /*or MCO_NO*/
np.int_base = MCO_NUM_DEC;
np.quad_base = MCO_NUM_HEX; /* other are invalid */
np.text_coding = MCO_TEXT_ASCII;
np.truncate_sp = MCO_YES;
np.use_xml_attrs = MCO_YES;
np.ignore_autoid = MCO_NO;
np.ignore_autooid = MCO_NO;
_SH_();
mco_get_runtime_info(&info);
if (!info.mco_save_load_supported)
{
printf("This sample requires xml import/export 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("Exporting database to %s...", fname);
f = fopen(fname, "wb");
/* export content of the database to a file */
{
rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_HIGH, &t);
if (rc == MCO_S_OK)
{
/* setup XML subsystem*/
#ifdef SETUP_POLICY
mco_xml_get_policy(t, &op);
rc = mco_xml_set_policy(t, &np);
if (MCO_S_OK != rc)
{
printf("unable to set xml policy %d ", rc);
}
#endif
rc = mco_db_xml_export(t, f, file_writer);
/* revert xml policy */
#ifdef SETUP_POLICY
mco_xml_set_policy(t, &op);
#endif
mco_trans_rollback(t);
if (rc != MCO_S_OK)
{
printf("En error %d occured during exporting.\n", rc);
}
}
else
{
printf("Unable to open a transaction. Error code %d\n", rc);
}
}
/**/
fclose(f);
if (rc)
{
printf("\tfailed, %d\n", rc);
goto end;
}
printf("\tsuccess\n\n");
/* disconnect from the database, db is no longer valid */
mco_db_disconnect(db);
mco_db_close("dbtest");
{
rc = mco_db_open("dbtest", dbtest_get_dictionary(), start_mem, DBSIZE, (uint2)PAGESIZE);
if (rc == MCO_S_OK)
{
mco_db_connect("dbtest", &db);
/* import content of the database from a file */
rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_HIGH, &t);
if (rc == MCO_S_OK)
{
#ifdef SETUP_POLICY
mco_xml_get_policy(t, &op);
rc = mco_xml_set_policy(t, &np);
if (MCO_S_OK != rc)
{
printf("unable to set xml policy %d ", rc);
}
#endif
printf("Importing data from %s...", fname);
f = fopen(fname, "rb");
rc = mco_db_xml_import(t, f, file_reader);
fclose(f);
if (rc)
{
printf("\tfailed. Error code %d\n\n", rc);
}
else
{
printf("\tsuccess\n\n");
}
#ifdef SETUP_POLICY
mco_xml_set_policy(t, &op);
#endif
mco_trans_commit(t);
}
/**/
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);
mco_db_close("dbtest");
}
else
{
printf("Unable to open database. Error code %d\n", rc);
}
}
end: mco_runtime_stop();
if (!info.mco_shm_supported)
{
free(start_mem);
}
printf("Press Enter to finish\n");
getchar();
PROG_EXIT(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -