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"
/* 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__)
const char *file,
int line)
{
if (rc != sOKAY)
{
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,
RDM_RETCODE rc = ts.init (db);
for (int hour = 0; hour < 72 && rc == sOKAY; hour++)
{
rc = ts.begin ();
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);
}
}
if (rc == sOKAY)
{
rc = ts.end ();
}
else
{
ts.rollback ();
}
}
rc = sOKAY;
return rc;
}
RDM_RETCODE readTimeSeriesData (
RDM_DB db
)
{
RDM_CURSOR cursor = NULL;
RDM_TABLE_ID readTable[] = {TABLE_STATS_DAYS};
RDM_RETCODE rc = rdm_dbGetRows (db, TABLE_STATS_DAYS, &cursor);
if (rc == sOKAY)
{
RDM_TRANS trans;
rc = rdm_dbStartRead (db, readTable, RDM_LEN (readTable), &trans);
if (rc == sOKAY)
{
rc = rdm_cursorMoveToFirst (cursor);
while (rc == sOKAY)
{
STATS_DAYS stats;
rc = rdm_cursorReadRow (cursor, &stats, sizeof (stats), NULL);
std::cout << "(" << stats.time_stamp_first << ":" << stats.time_stamp_last << "):" << "N: " << stats.n <<
", average: " << stats.value_sum / stats.n << ", standard deviation: " <<
sqrt ((stats.value_sum2 - stats.value_sum * stats.value_sum / stats.n) / (stats.n - 1)) << std::endl;
if (rc == sOKAY)
{
rc = rdm_cursorMoveToNext (cursor);
}
}
if (rc == sENDOFCURSOR)
{
rc = sOKAY;
}
rdm_transEnd (trans);
}
rdm_cursorFree (cursor);
}
return rc;
}
RDM_RETCODE openAndDeleteRows (
RDM_DB db
)
{
RDM_RETCODE rc = rdm_dbSetCatalog (db, cpp70_cat);
if (rc == sOKAY)
{
rc = rdm_dbOpen (db, "cpp70", RDM_OPEN_EXCLUSIVE);
}
RDM_TRANS trans;
if (rc == sOKAY)
{
rdm_dbStartUpdate(db, RDM_LOCK_ALL, 0, NULL, 0, &trans);
}
if (rc == sOKAY)
{
if (rc == sOKAY)
{
rc = rdm_transEnd (trans);
}
else
{
}
}
return rc;
}
RDM_RETCODE doDBOperations (
RDM_TFS tfs
)
{
RDM_DB db;
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);
}
else
{
}
}
return rc;
}
RDM_RETCODE setupTFSAndDoDBOperations (void)
{
RDM_TFS tfs;
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
{
rc = doDBOperations (tfs);
}
}
return rc;
}
int main_cpp70 (int argc, const char *const *argv)
{
const RDM_CMDLINE_OPT opts[] = {{NULL, NULL, NULL, NULL}};
RDM_RETCODE rc = rdm_cmdlineInit (&cmd, argc, argv, "An example that demonstrates some time series functionality.", opts);
rc = setupTFSAndDoDBOperations ();
return rc == sOKAY ? 0 : 1;
}
RDM_RETCODE rdm_cursorMoveToFirst(RDM_CURSOR cursor)
Position a cursor to the first row in the collection.
RDM_RETCODE rdm_transEnd(RDM_TRANS trans)
End a transactional operation.
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
RDM_RETCODE rdm_cursorMoveToNext(RDM_CURSOR cursor)
Position a cursor to the next row in the collection.
Header for the RDM Cursor APIs.
RDM_RETCODE rdm_rdmAllocTFS(RDM_TFS *phTFS)
Allocate a TFS handle.
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:147
Time series for split template classes.
RDM_RETCODE rdm_dbDeleteAllRowsFromDatabase(RDM_DB db)
Remove all rows from a database.
struct RDM_CURSOR_S * RDM_CURSOR
Definition: rdmtypes.h:306
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.
@ RDM_OPEN_EXCLUSIVE
Definition: rdmtypes.h:254
The RDM Time Series Namespace.
Definition: rdm_time_series.h:32
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:176
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
Time series for stats template class.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
RDM_RETCODE rdm_dbFree(RDM_DB db)
Free a database handle.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RDM database using the specified database handle.
#define print_error(rc)
Definition: example_fcns.h:17
@ sOKAY
Definition: rdmretcodetypes.h:97
struct RDM_TRANS_S * RDM_TRANS
Definition: rdmtypes.h:307
Generic usage function option record.
Definition: rdmcmdlinetypes.h:32
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
Free an RDM_CURSOR.
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.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
The RDM Database Namespace.
Definition: rdm_db.h:29
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.
Template Class for inserting a row.
Definition: rdm_db_insert_row.h:39
Template Class for doing transactions.
Definition: rdm_db_transaction.h:57
Template Class for doing statistics.
Definition: rdm_time_series_stats.h:63
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:351
Time series for transaction template class.
Template Class for doing a split.
Definition: rdm_time_series_split.h:42
RDM_RETCODE begin(void)
Start a transaction.
Definition: rdm_db_transaction.h:110
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
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.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:305
RDM_RETCODE rdm_tfsFree(RDM_TFS hTFS)
Terminate a TFS service.
Header for the RDM Database APIs.
void print_errorEx(int rc, const char *file, int line)
#define RDM_LOCK_ALL
Definition: rdmtypes.h:170
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.
#define RDM_LEN(x)
Definition: psptypes.h:78
@ sENDOFCURSOR
Definition: rdmretcodetypes.h:58
Time series for insert_row template class.
Internal RDM Startup API used by startup macros.
RDM_RETCODE
RDM status and error return codes.
Definition: rdmretcodetypes.h:43
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.
RDM_RETCODE init(RDM_DB db)
Initialize this object.
Definition: rdm_db_transaction.h:79