mco_db_save

Save a database image to an external file.

Prototype

    MCO_RET mco_db_save(/*IN*/ void *stream_handle,
                        /*IN*/ mco_stream_write output_stream_writer,
                        /*IN*/ mco_db_h db);
       

Arguments

stream_handle
Handle to the output stream.
output_stream_writer
Handler function called by the runtime to format output.
db
The database handle.

Description

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() and mco_db_load() APIs, and the former variants mco_inmem_save(), mco_inmem_load(), mco_disk_save(), mco_disk_load(), and mco_disk_load_file() have been deprecated.

The simplified versions of mco_db_save() and mco_db_load() allow the application to stream database contents to and from a persistent media file, socket, or pipe, with CRC checking, "binary schema evolution" (BSE) support, and buffering to allow interruption and restart of the streaming process. Database snapshot files are also encrypted if mco_db_params_t::cipher_key is specified.

Return Codes

MCO_S_OK
The database was saved successfully.
MCO_E_NOINSTANCE
The specified database is not opened.
MCO_E_WRITE_STREAM
Error writing to output stream.
MCO_E_INVALID_HANDLE
The specified database handle is invalid.
Other codes
Other return codes may be returned due to implementation-specific errors.

Example

    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);
            }
            ...
        }
    }

Files

Header file:
mco.h
Source file:
mcoseri.c
Library:
libmcoseri.a