Save a database image to an external file.
MCO_RET mco_db_save(/*IN*/ void *stream_handle,
/*IN*/ mco_stream_write output_stream_writer,
/*IN*/ mco_db_h db);
stream_handle
output_stream_writer
db
This function is used to stream an eXtremeDB database to permanent storage. Used together with
mco_db_load(), an application can implement robust backup and restore capability.It is the application's responsibility to open the output stream in the proper mode to stream binary data, and to ensure that there is adequate space at the destination to hold the database. Indexes are not streamed; they will be rebuilt when the database is reloaded. See the control structure stream writer for further details.
With release 7.1, eXtremeDB has simplified the
mco_db_save()andmco_db_load()APIs, and the former variantsmco_inmem_save(),mco_inmem_load(),mco_disk_save(),mco_disk_load(), andmco_disk_load_file()have been deprecated.The simplified versions of
mco_db_save()andmco_db_load()allow the application to stream database contents to and from a persistent media file, socket, or pipe, withCRCchecking, "binary schema evolution" (BSE) support, and buffering to allow interruption and restart of the streaming process. Database snapshot files are also encrypted ifmco_db_params_t::cipher_keyis specified.
MCO_S_OK
MCO_E_NOINSTANCE
MCO_E_WRITE_STREAM
MCO_E_INVALID_HANDLE
Application snippet:
const char *dbname = "SimpleDb";
/* Stream writer with prototype mco_stream_write */
mco_size_sig_t file_writer(void *stream_handle /* FILE * */, const void *from, mco_size_t nbytes)
{
FILE *f = (FILE *)stream_handle;
mco_size_sig_t nbs;
nbs = fwrite(from, 1, nbytes, f);
return nbs;
}
int main(int argc, char* argv[])
{
mco_db_h db;
MCO_RET rc;
FILE *fbak;
mco_device_t dev;
...
if ((rc = mco_runtime_start()) != MCO_S_OK)
exit(-1);
rc = mco_db_open_dev(dbname, simpledb_get_dictionary(), &dev, 1, &db_params);
if (MCO_S_OK != rc)
{
rc = mco_db_connect(dbname, &db);
...
/* Backup database */
fbak = fopen("backupdb.bak", "wb");
if (fbak == 0)
{
printf("\n\tCan't open output file for streaming\n");
}
else
{
rc = mco_db_save((void *)fbak, file_writer, db);
fclose(fbak);
sample_rc_check("\tSave database", rc);
}
...
}
}