Saving eXtremeSQL Database Metadata in C

It is often desirable to open a persistent database that has been created by another application (xSQL or an embedded eXtremeSQL application using the C, C++, Java, C# or Python APIs). In order to be able to do so, the two applications must use the exact same metadata (runtime options, page sizes, devices, database parameters and dictionary). The C API function mco_db_save_metadata() facilitates this purpose by saving all relevant information.

     
    MCO_RET mco_db_save_metadata(void* stream_handle,
                    mco_stream_write output_stream_writer,
                    mco_db_h db,
                    mco_bool save_defaults);
     

The first two parameters are the stream handle and the stream writer function (similar to that used with the mco_db_save() API), the db argument is the database connection and save_defaults indicates whether to write the parameter's value if that value is the default. (MCO_NO indicates that only the metadata that is different from the defaults are written into the configuration file. In this context the defaults refer to the default values of the database parameters that are normally created through function mco_db_params_init().If save_defaults is MCO_YES, than all of the mco_db_params field values - several dozen values - are written into the metadata file).

The usage is similar to mco_db_save(). For example:

     
    mco_size_sig_t file_writer(void *stream_handle, const void *from, mco_size_t nbytes)
    {
        return (mco_size_sig_t) fwrite(from, 1, nbytes, (FILE*) stream_handle);
    }
     
    main() 
    {
        ...
        mco_db_connect(dbName, &db);
        FILE *f = fopen("metadata.cfg", "w");
        if (f)
         {
            mco_db_save_metadata(f, file_writer, db, MCO_YES);
            fclose(f);
        }
        ...
    }
     

The metadata is saved in JSON format. (This metadata can be loaded by xSQL from the configuration file as follows:)

 
    ./xsql -c metadata.cfg