cpp70Example_main.cpp
TBD: Update GenDefines.txt with a $DESCRIPTION for this example. This example needs a compiled schema, cpp70.sdl.
#include <cstdio>
#include <iostream>
#include <math.h>
#include "rdmtfsapi.h"
#include "rdmdbapi.h"
#include "rdmcursorapi.h"
#include "rdmstartupapi.h"
#include "rdm_db_transaction.h"
#include "rdm_db_insert_row.h"
#include "rdm_time_series_split.h"
#include "rdm_time_series_stats.h"
/* Generated \c struct and \c typedef definitions to be used with the RDM APIs
*/
#include "cpp70_structs.h"
/* Generated catalog definition to be used with the RDM rdm_dbSetCatalog() API
*/
#include "cpp70_cat.h"
using namespace RDM_CPP::CPP70;
#define print_error(rc) print_errorEx (rc, __FILE__, __LINE__)
RDM_RETCODE rc,
const char *file,
int line)
{
{
fprintf (
stderr, "%s:%d:0: error: %d (%s): %s\n", file, line, rc,
}
}
RDM_RETCODE insertTimeSeriesData (
RDM_DB db
)
{
using namespace RDM::TIME_SERIES;
using namespace RDM::DB;
stats <60, STATS_MINUTES,
stats <60, STATS_HOURS,
stats <24, STATS_DAYS,
insert_row <TABLE_STATS_DAYS>>>>>>>> ts;
{
rc = ts.begin ();
print_error (rc);
if (rc == sOKAY)
{
for (uint64_t time_stamp = hour * 60 * 60; rc == sOKAY && time_stamp < (hour + 1) * 60 * 60; time_stamp++)
{
const double tau = 6.283185307179586;
const double signal = sin (tau * time_stamp / 23.45);
MEASUREMENT_SECONDS measurement = {time_stamp, signal};
rc = ts.put_value (&measurement);
print_error (rc);
}
}
if (rc == sOKAY)
{
rc = ts.end ();
print_error (rc);
}
else
{
ts.rollback ();
}
}
rc = sOKAY;
return rc;
}
RDM_RETCODE readTimeSeriesData (
RDM_DB db
)
{
RDM_CURSOR cursor = NULL;
RDM_TABLE_ID readTable[] = {TABLE_STATS_DAYS};
print_error (rc);
if (rc == sOKAY)
{
RDM_TRANS trans;
print_error (rc);
if (rc == sOKAY)
{
rc = rdm_cursorMoveToFirst (cursor);
while (rc == sOKAY)
{
STATS_DAYS stats;
print_error (rc);
std::cout << "(" << stats.time_stamp_first << ":" << stats.time_stamp_last << "):" << "N: " << stats.n <<
sqrt ((stats.value_sum2 - stats.value_sum * stats.value_sum / stats.n) / (stats.n - 1)) << std::endl;
if (rc == sOKAY)
{
rc = rdm_cursorMoveToNext (cursor);
}
}
{
rc = sOKAY;
}
print_error (rc);
rdm_transEnd (trans);
}
rdm_cursorFree (cursor);
}
return rc;
}
RDM_RETCODE openAndDeleteRows (
RDM_DB db
)
{
print_error (rc);
if (rc == sOKAY)
{
print_error (rc);
}
RDM_TRANS trans;
if (rc == sOKAY)
{
print_error (rc);
}
if (rc == sOKAY)
{
rc = rdm_dbDeleteAllRowsFromDatabase (db);
print_error (rc);
if (rc == sOKAY)
{
rc = rdm_transEnd (trans);
print_error (rc);
}
else
{
rdm_transEndRollback (trans);
}
}
return rc;
}
RDM_RETCODE doDBOperations (
RDM_TFS tfs
)
{
RDM_DB db;
print_error (rc);
if (rc == sOKAY)
{
rc = openAndDeleteRows (db);
if (rc == sOKAY)
{
rc = insertTimeSeriesData (db);
}
if (rc == sOKAY)
{
rc = readTimeSeriesData (db);
}
if (rc == sOKAY)
{
rc = rdm_dbFree (db);
print_error (rc);
}
else
{
rdm_dbFree (db);
}
}
return rc;
}
RDM_RETCODE setupTFSAndDoDBOperations (void)
{
RDM_TFS tfs;
print_error (rc);
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (tfs);
print_error (rc);
if (rc == sOKAY)
{
rc = doDBOperations (tfs);
}
rdm_tfsFree (tfs);
}
return rc;
}
int main_cpp70 (int argc, const char *const *argv)
{
RDM_CMDLINE cmd;
RDM_RETCODE rc = rdm_cmdlineInit (&cmd, argc, argv, "An example that demonstrates some time series functionality.", opts);
print_error (rc);
rc = setupTFSAndDoDBOperations ();
return rc == sOKAY ? 0 : 1;
}
RDM_STARTUP_EXAMPLE (cpp70)
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
Header for the RDM Cursor APIs.
RDM_RETCODE rdm_transEndRollback(RDM_TRANS trans)
End a transactional operation with a rollback.
RDM_RETCODE end(void)
End a transaction.
Definition: rdm_db_transaction.h:145
Time series for split template classes.
RDM_RETCODE rdm_dbDeleteAllRowsFromDatabase(RDM_DB db)
Remove all rows from a database.
RDM_RETCODE rdm_dbStartRead(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
Get read locks.
RDM_RETCODE rdm_cursorReadRow(RDM_CURSOR cursor, void *colValues, size_t bytesIn, size_t *bytesOut)
Read all columns from a row.
The buffer used by the command line parser to hold state information.
Definition: rdmcmdlinetypes.h:85
RDM_RETCODE rollback(void)
Roll back a transaction.
Definition: rdm_db_transaction.h:174
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
Time series for stats template class.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RDM database using the specified database handle.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RDM db.
Header for the Transactional File Server (TFS) API.
RDM_RETCODE rdm_dbGetRows(RDM_DB db, RDM_TABLE_ID tableId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with rows based on a table id.
const char * rdm_retcodeGetDescription(RDM_RETCODE retcode)
Invoke RDM error handler.
RDM_RETCODE put_value(SOURCE_VALUE_T *source_value, TRANSACTIONAL_T transactional=NOT_TRANSACTIONAL)
Template method for receiving a data value.
Definition: rdm_db_transaction.h:349
Time series for transaction template class.
RDM_RETCODE begin(void)
Start a transaction.
Definition: rdm_db_transaction.h:108
RDM_RETCODE rdm_cmdlineInit(RDM_CMDLINE *cmd, int32_t argc, const char *const argv[], const char *description, const RDM_CMDLINE_OPT *opts)
Initialize an RDM_CMDLINE buffer and validate the command line.
Header for the RDM Database APIs.
void print_errorEx(int rc, const char *file, int line)
RDM_RETCODE rdm_dbStartUpdate(RDM_DB db, const RDM_TABLE_ID *writeTableIds, uint32_t numWriteTableIds, const RDM_TABLE_ID *readTableIds, uint32_t numReadTableIds, RDM_TRANS *pTrans)
Get write locks.
Time series for insert_row template class.
Internal RDM Startup API used by startup macros.
RDM_RETCODE init(RDM_DB db)
Initialize this object.
Definition: rdm_db_transaction.h:77