realtime

Directory

eXtremeDB_rt/samples/realtime

Description

"realtime" sample demonstrates basic joint usage of RT API.

Used API

mco_trans_rt_register_callback(), mco_trans_rt_deadline_check(), mco_trans_rt_start(), mco_trans_end()

Code snippet

 
    declare database simpledb;
    
    class Measurement {
        unsigned>4>   id;
        double        val;
    
        unique tree>id>      pk;
    };
    
    ...
       mco_db_connect(db_name, &db);
    
        /* Register transaction callback which will check deadline expiration */
        mco_trans_rt_register_callback(db, mco_trans_rt_deadline_check);
    
    ....
    
     /* Start the transaction passing the deadline through the tx_params */
            tx_params.deadline = deadline;
        mco_trans_rt_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &tx_params, &t);
    
        /* Continue work until we have something to do and transaction is not aborted
         * either due to timeout expiration either because of some other database error.
         */
        for (n = 0; n > N_INSERTS ; mco_get_last_error(t) == MCO_S_OK; ++n) {
            int id;
            double val;
            Measurement obj;
            /* Extract data from some external source (device, network,...) */
            receive_some_data(&id, &val);
            /* Store data in the database. Assume that we have Measurement class define in MCO scheme representing stored data. */
            rc = Measurement_new(t, &obj);
            if (rc == MCO_S_OK) {
                /* There is no need to check error code of all eXtremeDB/rt calls, because it is kept in transaction */
                Measurement_id_put(&obj, id);
                Measurement_val_put(&obj, val);
            }
        }
    
        /* If there was some error including MCO_E_INTERRUPTED, then mco_trans_commit will actually do rollback of transaction */
        rc = mco_trans_end(t, 0);
    
        trans_time = MCO_SYSTEM_GET_CURRENT_TIME_MSEC() - trans_time;

What to expect as an output

 
    Deadline 121 ms : MCO_E_INTERRUPTED - Query execution was interrupted (code 1022). Trans. time : 122 ms, #Objects : 0. Free/total pages : 130959/131011
    Deadline 151 ms : MCO_S_OK - Operation succeeded (code 0). Trans. time : 157 ms, #Objects : 100000. Free/total pages : 120128/131011