53#define FREE(x) { if (x) free(x); (x) = NULL; }
67 const char*
sign =
"";
131 const char* s = event_name;
132 for (
int j=0; s[
j];
j++) {
188 for (
int i=0; s[
i]!=0;
i++) {
205 for (
int i=0; s[
i]!=0;
i++) {
249 "xxxINVALIDxxxARRAY",
250 "xxxINVALIDxxxSTRUCT",
271 "xxxINVALIDxxxARRAY",
272 "xxxINVALIDxxxSTRUCT",
293 "xxxINVALIDxxxARRAY",
294 "xxxINVALIDxxxSTRUCT",
320 cm_msg(
MERROR,
"SqlHistory",
"Cannot use this SQL database, incompatible column names: created column type [%s] is reported with column type [%s]",
index_type,
col_type->c_str());
357 cm_msg(
MERROR,
"SqlHistory",
"Cannot use this SQL database, incompatible column names: created column type [%s] is reported with column type [%s]",
index_type,
col_type->c_str());
365 for (
int tid=0; tid<
TID_LAST; tid++)
369 printf(
"sql2midasType: Cannot convert SQL data type \'%s\' to a MIDAS data type!\n",
name);
384 printf(
"sql2midasType: Cannot convert SQL data type \'%s\' to a MIDAS data type!\n",
name);
439 time_t* last_written) = 0;
469 for (
unsigned i=0;
i<
fData.size();
i++)
478 for (
unsigned i=0;
i<
fData.size();
i++)
551 printf(
"find_event: All schema for event %s: (total %d)\n", event_name, (
int)
fData.size());
553 for (
unsigned i=0;
i<
fData.size();
i++) {
562 printf(
"find_event: Found %d schemas for event %s\n",
found, event_name);
568 for (
unsigned i=0;
i<
fData.size();
i++) {
589 for (
unsigned i=0;
i<
fData.size();
i++) {
651 virtual int Exec(
const char* table_name,
const char*
sql) = 0;
655 virtual int Prepare(
const char* table_name,
const char*
sql) = 0;
673 virtual std::string
QuoteId(
const char* s) = 0;
791 for (
unsigned j=0;
j<
nv;
j++)
792 printf(
" %d: name [%s], type [%s] tid %d, n_data %d, n_bytes %d, offset %d\n",
j, this->
fVariables[
j].name.c_str(),
rpc_tid_name(this->
fVariables[
j].
type), this->
fVariables[
j].type, this->
fVariables[
j].n_data, this->
fVariables[
j].n_bytes, this->
fOffsets[
j]);
798 printf(
"event [%s], sql_table [%s], time %s..%s, %d variables, %d bytes\n", this->
fEventName.c_str(),
this->fTableName.c_str(),
TimeToString(this->
fTimeFrom).
c_str(),
TimeToString(this->
fTimeTo).
c_str(), nv,
fNumBytes);
800 for (
unsigned j=0;
j<
nv;
j++) {
812 printf(
"event [%s], file_name [%s], time %s..%s, %d variables, %d bytes, dat_offset %d, record_size %d\n", this->
fEventName.c_str(),
this->fFileName.c_str(),
TimeToString(this->
fTimeFrom).
c_str(),
TimeToString(this->
fTimeTo).
c_str(), nv,
fNumBytes,
fDataOffset,
fRecordSize);
814 for (
unsigned j=0;
j<
nv;
j++)
815 printf(
" %d: name [%s], type [%s] tid %d, n_data %d, n_bytes %d, offset %d\n",
j, this->
fVariables[
j].name.c_str(),
rpc_tid_name(this->
fVariables[
j].
type), this->
fVariables[
j].type, this->
fVariables[
j].n_data, this->
fVariables[
j].n_bytes, this->
fOffsets[
j]);
837 std::string fConnectString;
864 int Exec(
const char* table_name,
const char*
sql);
867 int Prepare(
const char* table_name,
const char*
sql);
881 std::string
QuoteId(
const char* s);
895 fTransactionPerTable =
false;
924 std::string user_name;
939 char* s =
fgets(buf,
sizeof(buf),
fp);
976 printf(
"Mysql::Connect: connecting to server [%s] port %d, unix socket [%s], database [%s], user [%s], password [%s], buffer [%d]\n",
host_name.c_str(),
tcp_port,
unix_socket.c_str(),
db_name.c_str(), user_name.c_str(),
user_password.c_str(),
fMaxDisconnected);
988 cm_msg(
MERROR,
"Mysql::Connect",
"mysql_real_connect() to host [%s], port %d, unix socket [%s], database [%s], user [%s], password [%s]: error %d (%s)",
host_name.c_str(),
tcp_port,
unix_socket.c_str(),
db_name.c_str(), user_name.c_str(),
"xxx",
mysql_errno(
fMysql),
mysql_error(
fMysql));
999 status = Exec(
"(notable)",
"SET SESSION sql_mode='ANSI'");
1001 cm_msg(
MERROR,
"Mysql::Connect",
"Cannot set ANSI mode, nothing will work");
1007 cm_msg(
MINFO,
"Mysql::Connect",
"Connected to a MySQL database on host [%s], port %d, unix socket [%s], database [%s], user [%s], password [%s], buffer %d",
host_name.c_str(),
tcp_port,
unix_socket.c_str(),
db_name.c_str(), user_name.c_str(),
"xxx",
fMaxDisconnected);
1011 fIsConnected =
true;
1034int Mysql::Disconnect()
1050 fIsConnected =
false;
1054bool Mysql::IsConnected()
1056 return fIsConnected;
1059int Mysql::OpenTransaction(
const char* table_name)
1061 return Exec(table_name,
"START TRANSACTION");
1065int Mysql::CommitTransaction(
const char* table_name)
1067 Exec(table_name,
"COMMIT");
1071int Mysql::RollbackTransaction(
const char* table_name)
1073 Exec(table_name,
"ROLLBACK");
1077int Mysql::ListTables(std::vector<std::string> *
plist)
1083 printf(
"Mysql::ListTables!\n");
1100 std::string
tn = GetText(0);
1109int Mysql::ListColumns(
const char* table_name, std::vector<std::string> *
plist)
1115 printf(
"Mysql::ListColumns for table \'%s\'\n", table_name);
1120 cmd +=
"SHOW COLUMNS FROM ";
1121 cmd += QuoteId(table_name);
1124 status = Prepare(table_name, cmd.c_str());
1134 std::string
cn = GetText(0);
1135 std::string
ct = GetText(1);
1148int Mysql::Exec(
const char* table_name,
const char*
sql)
1151 printf(
"Mysql::Exec(%s, %s)\n", table_name,
sql);
1178 return ExecDisconnected(table_name,
sql);
1186int Mysql::ExecDisconnected(
const char* table_name,
const char*
sql)
1189 printf(
"Mysql::ExecDisconnected(%s, %s)\n", table_name,
sql);
1194 cm_msg(
MERROR,
"Mysql::ExecDisconnected",
"Error: Disconnected database buffer overflow, size %d, subsequent events are lost", (
int)
fDisconnectedBuffer.size());
1203 int status = Connect(fConnectString.c_str());
1224int Mysql::Prepare(
const char* table_name,
const char*
sql)
1227 printf(
"Mysql::Prepare(%s, %s)\n", table_name,
sql);
1246 status = Connect(fConnectString.c_str());
1251 cm_msg(
MERROR,
"Mysql::Prepare",
"mysql_query(%s) - MySQL server has gone away, and couldn't reconnect - %d",
sql,
status);
1259 cm_msg(
MINFO,
"Mysql::Prepare",
"Reconnected to MySQL after long inactivity.");
1280 printf(
"Mysql::Step()\n");
1298const char* Mysql::GetText(
int column)
1311double Mysql::GetDouble(
int column)
1321int Mysql::Finalize()
1334const char* Mysql::ColumnType(
int midas_tid)
1371 printf(
"type mismatch!\n");
1376std::string Mysql::QuoteId(
const char* s)
1385std::string Mysql::QuoteString(
const char* s)
1391 while (
int c = *s++) {
1419#include <libpq-fe.h>
1424 std::string fConnectString;
1443 int Connect(
const char* path);
1452 int Exec(
const char* table_name,
const char*
sql);
1455 int Prepare(
const char* table_name,
const char*
sql);
1470 std::string
QuoteId(
const char* s);
1485 fTransactionPerTable =
false;
1514 std::string user_name;
1529 char* s =
fgets(buf,
sizeof(buf),
fp);
1566 printf(
"Pgsql::Connect: connecting to server [%s] port %s, unix socket [%s], database [%s], user [%s], password [%s], buffer [%d]\n",
host_name.c_str(),
tcp_port.c_str(),
unix_socket.c_str(),
db_name.c_str(), user_name.c_str(),
user_password.c_str(),
fMaxDisconnected);
1571 msg.erase(std::remove(msg.begin(), msg.end(),
'\n'), msg.end());
1572 cm_msg(
MERROR,
"Pgsql::Connect",
"PQsetdbLogin() to host [%s], port %s, unix socket [%s], database [%s], user [%s], password [%s]: error (%s)",
host_name.c_str(),
tcp_port.c_str(),
unix_socket.c_str(),
db_name.c_str(), user_name.c_str(),
"xxx", msg.c_str());
1580 cm_msg(
MINFO,
"Pgsql::Connect",
"Connected to a PostgreSQL database on host [%s], port %s, unix socket [%s], database [%s], user [%s], password [%s], buffer %d",
host_name.c_str(),
tcp_port.c_str(),
unix_socket.c_str(),
db_name.c_str(), user_name.c_str(),
"xxx",
fMaxDisconnected);
1584 fIsConnected =
true;
1605 status = Prepare(
"pg_extensions",
"select extname from pg_extension where extname = 'timescaledb';");
1608 cm_msg(
MERROR,
"Pgsql::Connect",
"TimescaleDB extension not installed");
1613 status = Prepare(
"pg_extensions",
"select extname from pg_extension where extname = 'timescaledb_toolkit';");
1616 cm_msg(
MERROR,
"Pgsql::Connect",
"TimescaleDB_toolkit extension not installed");
1621 cm_msg(
MINFO,
"Pgsql::Connect",
"TimescaleDB extensions found - downsampling enabled");
1627int Pgsql::Disconnect()
1635 fIsConnected =
false;
1639bool Pgsql::IsConnected()
1641 return fIsConnected;
1644int Pgsql::OpenTransaction(
const char* table_name)
1646 return Exec(table_name,
"BEGIN TRANSACTION;");
1649int Pgsql::CommitTransaction(
const char* table_name)
1651 return Exec(table_name,
"COMMIT;");
1654int Pgsql::RollbackTransaction(
const char* table_name)
1656 return Exec(table_name,
"ROLLBACK;");
1659int Pgsql::ListTables(std::vector<std::string> *
plist)
1665 printf(
"Pgsql::ListTables!\n");
1667 int status = Prepare(
"pg_tables",
"select tablename from pg_tables where schemaname = 'public';");
1677 std::string
tn = GetText(0);
1686int Pgsql::ListColumns(
const char* table_name, std::vector<std::string> *
plist)
1692 printf(
"Pgsql::ListColumns for table \'%s\'\n", table_name);
1695 cmd +=
"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = ";
1696 cmd += QuoteString(table_name);
1699 int status = Prepare(table_name, cmd.c_str());
1708 std::string
cn = GetText(0);
1709 std::string
ct = GetText(1);
1719int Pgsql::Exec(
const char* table_name,
const char*
sql)
1722 printf(
"Pgsql::Exec(%s, %s)\n", table_name,
sql);
1742 return ExecDisconnected(table_name,
sql);
1749int Pgsql::ExecDisconnected(
const char* table_name,
const char*
sql)
1752 printf(
"Pgsql::ExecDisconnected(%s, %s)\n", table_name,
sql);
1757 cm_msg(
MERROR,
"Pgsql::ExecDisconnected",
"Error: Disconnected database buffer overflow, size %d, subsequent events are lost", (
int)
fDisconnectedBuffer.size());
1766 int status = Connect(fConnectString.c_str());
1787int Pgsql::Prepare(
const char* table_name,
const char*
sql)
1790 printf(
"Pgsql::Prepare(%s, %s)\n", table_name,
sql);
1802 int status = Connect(fConnectString.c_str());
1807 cm_msg(
MERROR,
"Pgsql::Prepare",
"PQexec(%s) PostgreSQL server has gone away, and couldn't reconnect - %d",
sql,
status);
1814 cm_msg(
MINFO,
"Pgsql::Prepare",
"Reconnected to PostgreSQL after long inactivity.");
1822std::string Pgsql::BuildDownsampleQuery(
const time_t start_time,
const time_t end_time,
const int npoints,
1823 const char* table_name,
const char* column_name)
1826 cmd +=
"SELECT extract(epoch from time::TIMESTAMPTZ) as _i_time, value ";
1828 cmd +=
" FROM unnest(( SELECT lttb";
1829 cmd +=
"(_t_time, ";
1832 cmd += std::to_string(
npoints);
1835 cmd += QuoteId(table_name);
1836 cmd +=
" WHERE _t_time BETWEEN ";
1837 cmd +=
"to_timestamp(";
1839 cmd +=
") AND to_timestamp(";
1841 cmd +=
") )) ORDER BY time;";
1859const char* Pgsql::GetText(
int column)
1870double Pgsql::GetDouble(
int column)
1880int Pgsql::Finalize()
1891const char* Pgsql::ColumnType(
int midas_tid)
1924 printf(
"type mismatch!\n");
1929std::string Pgsql::QuoteId(
const char* s)
1938std::string Pgsql::QuoteString(
const char* s)
1957typedef std::map<std::string, sqlite3*>
DbMap;
1973 int Connect(
const char* path);
1983 int Exec(
const char* table_name,
const char*
sql);
1986 int Prepare(
const char* table_name,
const char*
sql);
2000 std::string
QuoteId(
const char* s);
2004std::string Sqlite::QuoteId(
const char* s)
2013std::string Sqlite::QuoteString(
const char* s)
2022const char* Sqlite::ColumnType(
int midas_tid)
2047const char* Sqlite::GetText(
int column)
2057double Sqlite::GetDouble(
int column)
2064 fIsConnected =
false;
2081int Sqlite::ConnectTable(
const char* table_name)
2083 std::string
fname = fPath +
"mh_" + table_name +
".sqlite3";
2096#if SQLITE_VERSION_NUMBER >= 3006020
2102#warning Missing sqlite3_extended_result_codes()!
2107 Exec(table_name,
"PRAGMA journal_mode=persist;");
2108 Exec(table_name,
"PRAGMA synchronous=normal;");
2110 Exec(table_name,
"PRAGMA journal_size_limit=-1;");
2113 Exec(table_name,
"PRAGMA legacy_file_format;");
2114 Exec(table_name,
"PRAGMA synchronous;");
2115 Exec(table_name,
"PRAGMA journal_mode;");
2116 Exec(table_name,
"PRAGMA journal_size_limit;");
2119#ifdef SQLITE_LIMIT_COLUMN
2127 cm_msg(
MINFO,
"Sqlite::Connect",
"Table %s: connected to Sqlite file \'%s\'", table_name,
fname.c_str());
2132sqlite3* Sqlite::GetTable(
const char* table_name)
2143 return fMap[table_name];
2146int Sqlite::Connect(
const char* path)
2154 if (fPath.length() > 0) {
2160 cm_msg(
MINFO,
"Sqlite::Connect",
"Connected to Sqlite database in \'%s\'", fPath.c_str());
2162 fIsConnected =
true;
2167int Sqlite::Disconnect()
2173 const char* table_name =
iter->first.c_str();
2183 fIsConnected =
false;
2188bool Sqlite::IsConnected()
2190 return fIsConnected;
2193int Sqlite::OpenTransaction(
const char* table_name)
2195 int status = Exec(table_name,
"BEGIN TRANSACTION");
2199int Sqlite::CommitTransaction(
const char* table_name)
2201 int status = Exec(table_name,
"COMMIT TRANSACTION");
2205int Sqlite::RollbackTransaction(
const char* table_name)
2207 int status = Exec(table_name,
"ROLLBACK TRANSACTION");
2211int Sqlite::Prepare(
const char* table_name,
const char*
sql)
2218 printf(
"Sqlite::Prepare(%s, %s)\n", table_name,
sql);
2223#if SQLITE_VERSION_NUMBER >= 3006020
2226#warning Missing sqlite3_prepare_v2()!
2244 printf(
"Sqlite::Step()\n");
2262int Sqlite::Finalize()
2265 printf(
"Sqlite::Finalize()\n");
2286int Sqlite::ListTables(std::vector<std::string> *
plist)
2292 printf(
"Sqlite::ListTables at path [%s]\n", fPath.c_str());
2296 const char* cmd =
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
2309 const char*
dn =
de->d_name;
2324 char table_name[256];
2325 mstrlcpy(table_name,
dn+3,
sizeof(table_name));
2327 char*
ss =
strstr(table_name,
".sqlite3");
2334 status = Prepare(table_name, cmd);
2343 const char*
tn = GetText(0);
2357int Sqlite::ListColumns(
const char* table, std::vector<std::string> *
plist)
2363 printf(
"Sqlite::ListColumns for table \'%s\'\n", table);
2366 cmd =
"PRAGMA table_info(";
2372 status = Prepare(table, cmd.c_str());
2381 const char*
colname = GetText(1);
2382 const char*
coltype = GetText(2);
2397 printf(
"history_sqlite::callback---->\n");
2405int Sqlite::Exec(
const char* table_name,
const char*
sql)
2420 printf(
"Sqlite::Exec(%s, %s)\n", table_name,
sql);
2442int Sqlite::ExecDisconnected(
const char* table_name,
const char*
sql)
2444 cm_msg(
MERROR,
"Sqlite::Exec",
"sqlite driver does not support disconnected operations");
2480 cm_msg(
MERROR,
"FileHistory::write_event",
"File \'%s\' may be truncated, data offset %d, record size %d, file size: %d, should be %d, truncating the file", s->
fFileName.c_str(), s->
fDataOffset, s->
fRecordSize, file_size,
data_end);
2572 cm_msg(
MERROR,
"FileHistory::ReadRecord",
"Cannot read \'%s\', unexpected end of file on read()",
file_name);
2586static int FindTime(
const char*
file_name,
int fd,
int offset,
int recsize,
int nrec,
time_t timestamp,
int*
i1p,
time_t*
t1p,
int*
i2p,
time_t*
t2p,
time_t* tstart,
time_t* tend,
int debug)
2607 char* buf =
new char[
recsize];
2625 if (timestamp <=
t1) {
2635 assert(
t1 < timestamp);
2658 if (
t2 < timestamp) {
2667 assert(
t1 < timestamp);
2668 assert(timestamp <=
t2);
2689 if (timestamp <= t) {
2705 assert(
t1 < timestamp);
2706 assert(timestamp <=
t2);
2769 if (
lw >= timestamp) {
2777 status =
FindTime(s->
fFileName.c_str(), fd, s->
fDataOffset, s->
fRecordSize,
nrec, timestamp, &
irec, &
trec, &
iunused, &
tunused, &tstart, &tend, 0*
debug);
2783 assert(
trec < timestamp);
2794 assert(
lw < timestamp);
2837 if (file_size == (
off_t)-1) {
2859 int status =
FindTime(s->
fFileName.c_str(), fd, s->
fDataOffset, s->
fRecordSize,
nrec, start_time, &
iunused, &
tunused, &
irec, &
trec, &tstart, &tend, 0*
debug);
2867 printf(
"FindTime %d, nrec %d, (%d, %s) (%d, %s), tstart %s, tend %s, want %s\n",
status,
nrec,
iunused,
TimeToString(
tunused).
c_str(),
irec,
TimeToString(
trec).
c_str(),
TimeToString(tstart).
c_str(),
TimeToString(tend).
c_str(),
TimeToString(start_time).
c_str());
2876 printf(
"FileHistory::read: file %s, schema time %s..%s, read time %s..%s, file time %s..%s, data in this file is too old\n", s->
fFileName.c_str(),
TimeToString(s->
fTimeFrom).c_str(),
TimeToString(s->
fTimeTo).c_str(),
TimeToString(start_time).
c_str(),
TimeToString(end_time).
c_str(),
TimeToString(tstart).
c_str(),
TimeToString(tend).
c_str());
2969 char*
data = buf + 4;
2988 int ii = var_index[
i];
2998 v = ((
unsigned char*)ptr)[
ii];
3001 v = ((
signed char *)ptr)[
ii];
3004 v = ((
char*)ptr)[
ii];
3007 v = ((
unsigned short *)ptr)[
ii];
3010 v = ((
signed short *)ptr)[
ii];
3013 v = ((
unsigned int *)ptr)[
ii];
3016 v = ((
int *)ptr)[
ii];
3019 v = ((
unsigned int *)ptr)[
ii];
3022 v = ((
float*)ptr)[
ii];
3025 v = ((
double*)ptr)[
ii];
3029 buffer[
i]->
Add(t, v);
3048 printf(
"FileHistory::read: file %s, schema time %s..%s, read time %s..%s, %d vars, read %d rows\n", s->
fFileName.c_str(),
TimeToString(s->
fTimeFrom).c_str(),
TimeToString(s->
fTimeTo).c_str(),
TimeToString(start_time).
c_str(),
TimeToString(end_time).
c_str(),
num_var,
count);
3113 int hs_write_event(
const char* event_name,
time_t timestamp,
int buffer_size,
const char* buffer);
3125 int num_var,
const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3202 (*fTimeBuffer)[pos] = t;
3203 (*fDataBuffer)[pos] = v;
3205 (*fNumEntries) = pos + 1;
3221 const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3229 int num_var,
const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3398 printf(
"hs_define_event: event name [%s] with %d tags\n", event_name,
ntags);
3404 for (
unsigned int i=0;
i<
fEvents.size();
i++)
3408 printf(
"deleting exising event %s\n", event_name);
3417 cm_msg(
MERROR,
"hs_define_event",
"Error: History event \'%s\' has empty name at index %d", event_name,
i);
3420 if (tags[
i].
name[0] ==
' ') {
3421 cm_msg(
MERROR,
"hs_define_event",
"Error: History event \'%s\' has name \'%s\' starting with a blank", event_name, tags[
i].
name);
3425 cm_msg(
MERROR,
"hs_define_event",
"Error: History event \'%s\' tag \'%s\' at index %d has invalid type %d",
3431 "Error: History event \'%s\' tag \'%s\' at index %d has forbidden type TID_STRING", event_name,
3435 if (tags[
i].n_data <= 0) {
3436 cm_msg(
MERROR,
"hs_define_event",
"Error: History event \'%s\' tag \'%s\' at index %d has invalid n_data %d",
3437 event_name, tags[
i].
name,
i, tags[
i].n_data);
3443 std::vector<std::string>
names;
3453 "Error: History event \'%s\' has duplicate tag name \'%s\'", event_name,
3468 for (
unsigned int i=0;
i<
fEvents.size();
i++)
3485 printf(
"hs_write_event: write event \'%s\', time %d, size %d\n", event_name, (
int)timestamp, buffer_size);
3517 cm_msg(
MERROR,
"hs_write_event",
"Event \'%s\' data size mismatch: expected %d bytes, got %d bytes", s->
fEventName.c_str(), s->
fNumBytes, buffer_size);
3527 cm_msg(
MERROR,
"hs_write_event",
"Event \'%s\' data size mismatch: expected %d bytes, got %d bytes", s->
fEventName.c_str(), s->
fNumBytes, buffer_size);
3547 cm_msg(
MERROR,
"hs_write_event",
"Event \'%s\' disabled after write error %d", event_name,
status);
3559 printf(
"hs_flush_buffers!\n");
3561 for (
unsigned int i=0;
i<
fEvents.size();
i++)
3578 printf(
"SchemaHistoryBase::hs_clear_cache!\n");
3596 printf(
"hs_get_events: available schema:\n");
3620 printf(
"hs_get_events: returning %d events\n", (
int)
pevents->size());
3657 for (
unsigned k=0;
k<
ptags->size();
k++)
3669 ptags->push_back(t);
3678 printf(
"hs_get_tags: event [%s], returning %d tags\n", event_name, (
int)
ptags->size());
3679 for (
unsigned i=0;
i<
ptags->size();
i++) {
3694 last_written[
j] = 0;
3729 if (
lw > last_written[
j])
3730 last_written[
j] =
lw;
3747 int num_var,
const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3800 cm_msg(
MERROR,
"SchemaHistoryBase::hs_read_buffer",
"History internal error, schema is not ordered by time. Please report this error to the midas forum.");
3807 std::vector<HsSchema*>
slist;
3808 std::vector<std::vector<int>>
smap;
3819 std::vector<int>
sm;
3843 printf(
"Found %d matching schema:\n", (
int)
slist.size());
3845 for (
size_t i=0;
i<
slist.size();
i++) {
3866 printf(
"Check schema %zu/%zu: prev from %s, this from %s to %s, compare %d\n",
ss,
slist.size(),
3875 cm_msg(
MERROR,
"SchemaHistoryBase::hs_read_buffer",
"History internal error, selected schema is not ordered by time. Please report this error to the midas forum.");
3886 for (
int i=
slist.size()-1;
i>=0;
i--) {
3904 const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3952 int num_var,
const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
4072 for (
unsigned i=0;
i<
sv->size();
i++) {
4123 cm_msg(
MERROR,
"NewSqlSchema",
"Error: Unexpected ordering of schema for table \'%s\', good luck!", table_name);
4137 cm_msg(
MERROR,
"NewSqlSchema",
"Error: Cannot clone schema for table \'%s\', good luck!", table_name);
4218 cm_msg(
MERROR,
"HsSqlSchema::write_event",
"Internal error, unexpected inactive column %d",
i);
4229 cm_msg(
MERROR,
"HsSqlSchema::write_event",
"Internal error, unexpected negative offset %d for column %d",
offset,
i);
4234 assert(n_data == 1);
4235 assert(
strlen(column_name) > 0);
4236 assert(
offset < data_size);
4253 sprintf(buf,
"%u",((
unsigned char *)ptr)[
j]);
4256 sprintf(buf,
"%d",((
signed char*)ptr)[
j]);
4260 sprintf(buf,
"\'%c\'",((
char*)ptr)[
j]);
4263 sprintf(buf,
"%u",((
unsigned short *)ptr)[
j]);
4266 sprintf(buf,
"%d",((
short *)ptr)[
j]);
4269 sprintf(buf,
"%u",((
unsigned int *)ptr)[
j]);
4272 sprintf(buf,
"%d",((
int *)ptr)[
j]);
4275 sprintf(buf,
"%u",((
unsigned int *)ptr)[
j]);
4279 sprintf(buf,
"\'%.8g\'",((
float*)ptr)[
j]);
4283 sprintf(buf,
"\'%.16g\'",((
double*)ptr)[
j]);
4294 strftime(buf,
sizeof(buf)-1,
"%Y-%m-%d %H:%M:%S.0", &
tms);
4297 cmd =
"INSERT INTO ";
4299 cmd +=
" (_t_time, _i_time";
4301 cmd +=
") VALUES (";
4347 cmd +=
"SELECT _i_time FROM ";
4349 cmd +=
" WHERE _i_time < ";
4351 cmd +=
" ORDER BY _i_time DESC LIMIT 2;";
4410 cmd +=
"SELECT _i_time, ";
4414 cmd +=
" WHERE _i_time>=";
4416 cmd +=
" and _i_time<=";
4418 cmd +=
" ORDER BY _i_time;";
4455 buffer[
i]->
Add(t, v);
4508 int status =
sql->OpenTransaction(table_name);
4526 cmd =
"CREATE TABLE ";
4527 cmd +=
sql->QuoteId(table_name);
4529 cmd +=
" (_t_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, _i_time INTEGER NOT NULL DEFAULT 0);";
4531 cmd +=
" (_t_time TIMESTAMP NOT NULL, _i_time INTEGER NOT NULL);";
4534 status =
sql->Exec(table_name, cmd.c_str());
4538 cm_msg(
MINFO,
"CreateSqlTable",
"Adding SQL table \"%s\", but it already exists", table_name);
4544 cm_msg(
MINFO,
"CreateSqlTable",
"Adding SQL table \"%s\", error status %d", table_name,
status);
4549 cm_msg(
MINFO,
"CreateSqlTable",
"Adding SQL table \"%s\"", table_name);
4560 cmd =
"CREATE INDEX ";
4563 cmd +=
sql->QuoteId(table_name);
4564 cmd +=
" (_i_time ASC);";
4566 status =
sql->Exec(table_name, cmd.c_str());
4570 cmd =
"CREATE INDEX ";
4573 cmd +=
sql->QuoteId(table_name);
4574 cmd +=
" (_t_time);";
4576 status =
sql->Exec(table_name, cmd.c_str());
4592 cmd =
"CREATE TABLE ";
4593 cmd +=
sql->QuoteId(table_name);
4594 cmd +=
" (_t_time TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, _i_time INTEGER NOT NULL DEFAULT 0);";
4596 status =
sql->Exec(table_name, cmd.c_str());
4599 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Adding SQL table \"%s\", but it already exists", table_name);
4605 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Adding SQL table \"%s\", error status %d", table_name,
status);
4610 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Adding SQL table \"%s\"", table_name);
4613 cmd =
"SELECT create_hypertable(";
4614 cmd +=
sql->QuoteString(table_name);
4615 cmd +=
", '_t_time');";
4618 status =
sql->Exec(table_name, cmd.c_str());
4621 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Converting SQL table to hypertable \"%s\", error status %d", table_name,
status);
4634 cmd =
"CREATE INDEX ";
4637 cmd +=
sql->QuoteId(table_name);
4638 cmd +=
" (_i_time ASC);";
4640 status =
sql->Exec(table_name, cmd.c_str());
4644 cmd =
"CREATE INDEX ";
4647 cmd +=
sql->QuoteId(table_name);
4648 cmd +=
" (_t_time);";
4650 status =
sql->Exec(table_name, cmd.c_str());
4660 printf(
"CreateSqlColumn: table [%s], column [%s], type [%s]\n", table_name, column_name,
column_type);
4667 cmd =
"ALTER TABLE ";
4668 cmd +=
sql->QuoteId(table_name);
4669 cmd +=
" ADD COLUMN ";
4670 cmd +=
sql->QuoteId(column_name);
4675 status =
sql->Exec(table_name, cmd.c_str());
4677 cm_msg(
MINFO,
"CreateSqlColumn",
"Adding column \"%s\" to SQL table \"%s\", status %d", column_name, table_name,
status);
4761 printf(
"hs_disconnect!\n");
4797 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
4812 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot update schema database for event \'%s\', see previous messages", event_name);
4817 printf(
"SqlHistory::new_event: schema for [%s] is %p\n", event_name, s);
4824 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
4835 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot update schema database for event \'%s\', see previous messages", event_name);
4840 printf(
"SqlHistory::new_event: schema for [%s] is %p\n", event_name, s);
4849 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
4866 printf(
"SqlHistory::read_schema: loading schema for event [%s] at time %s\n", event_name,
TimeToString(timestamp).
c_str());
4878 if (event_name ==
NULL)
4881 for (
unsigned i=0;
i<
sv->size();
i++) {
4890 unsigned nn =
sv->size();
4895 if (
sv->size() !=
nn)
4933 printf(
"update_schema1\n");
4941 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
4946 if (tags[
i].n_data > 1) {
4976 printf(
"Incompatible column!\n");
5039 for (
int t=0; t<20; t++) {
5083 cm_msg(
MERROR,
"SqlHistory::update_schema",
"Duplicate tags or SQL columns for history event \"%s\" tag \"%s\"", s->
fEventName.c_str(),
tagname.c_str());
5096 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
5099 if (tags[
i].n_data > 1) {
5134 printf(
"Return error!\n");
5148 printf(
"ReadSqliteTableNames: table [%s]\n", table_name);
5154 cmd =
"SELECT event_name, _i_time FROM \'_event_name_";
5156 cmd +=
"\' WHERE table_name='";
5160 status =
sql->Prepare(table_name, cmd.c_str());
5193 printf(
"ReadSqliteTableSchema: table [%s]\n", table_name);
5233 printf(
"SqliteHistory::read_table_and_event_names!\n");
5237 std::vector<std::string>
tables;
5242 for (
unsigned i=0;
i<
tables.size();
i++) {
5243 const char* table_name =
tables[
i].c_str();
5246 s =
strstr(table_name,
"_event_name_");
5247 if (s == table_name)
5249 s =
strstr(table_name,
"_column_names_");
5250 if (s == table_name)
5262 printf(
"SqliteHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5266 std::vector<std::string>
columns;
5271 for (
unsigned i=0;
i<
sv->size();
i++) {
5318 tn +=
"_column_names_";
5322 cmd =
"SELECT column_name, tag_name, tag_type, _i_time FROM ";
5324 cmd +=
" WHERE table_name=";
5326 cmd +=
" ORDER BY _i_time ASC;";
5356 for (
unsigned i=0;
i<
sv->size();
i++) {
5384 printf(
"SqliteHistory::create_table: event [%s], timestamp %s\n", event_name,
TimeToString(timestamp).
c_str());
5406 en +=
"_event_name_";
5409 cmd =
"CREATE TABLE ";
5411 cmd +=
" (table_name TEXT NOT NULL, event_name TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5415 cmd =
"INSERT INTO ";
5417 cmd +=
" (table_name, event_name, _i_time) VALUES (";
5428 cn +=
"_column_names_";
5431 cmd =
"CREATE TABLE ";
5433 cmd +=
" (table_name TEXT NOT NULL, column_name TEXT NOT NULL, tag_name TEXT NOT NULL, tag_type TEXT NOT NULL, column_type TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5448 printf(
"SqliteHistory::update_column: event [%s], table [%s], column [%s], new name [%s], timestamp %s\n", event_name, table_name, column_name, tag_name,
TimeToString(timestamp).
c_str());
5456 cmd =
"INSERT INTO \'_column_names_";
5458 cmd +=
"\' (table_name, column_name, tag_name, tag_type, column_type, _i_time) VALUES (\'";
5504 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name='";
5508 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name!='';";
5509 table_name =
"_history_index";
5512 status =
sql->Prepare(table_name, cmd.c_str());
5562 cm_msg(
MERROR,
"ReadMysqlTableNames",
"Error: Cannot continue, nothing will work after this error\n");
5580 printf(
"MysqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5584 std::vector<std::string>
columns;
5589 for (
unsigned i=0;
i<
sv->size();
i++) {
5638 cmd =
"SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
5676 for (
unsigned i=0;
i<
sv->size();
i++) {
5720 printf(
"ReadMysqlTableSchema: table [%s]\n", table_name);
5742 printf(
"MysqlHistory::read_table_and_event_names!\n");
5746 std::vector<std::string>
tables;
5751 for (
unsigned i=0;
i<
tables.size();
i++) {
5752 const char* table_name =
tables[
i].c_str();
5755 s =
strstr(table_name,
"_history_index");
5756 if (s == table_name)
5773 printf(
"read_table_and_event_names:\n");
5785 printf(
"MysqlHistory::create_table: event [%s], timestamp %s\n", event_name,
TimeToString(timestamp).
c_str());
5791 if (table_name.length() > 40) {
5792 table_name.resize(40);
5832 cm_msg(
MERROR,
"MysqlHistory::create_table",
"Could not create table [%s] for event [%s], timestamp %s, please fix the SQL database configuration and try again", table_name.c_str(), event_name,
TimeToString(timestamp).
c_str());
5840 for (
int j=0;
j<2;
j++) {
5842 cmd +=
"INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
5848 sprintf(buf,
"%.0f", (
double)timestamp);
5878 cm_msg(
MERROR,
"MysqlHistory::create_table",
"Could not create table [%s] for event [%s], timestamp %s, after %d attempts", table_name.c_str(), event_name,
TimeToString(timestamp).
c_str(),
max_attempts);
5886 printf(
"MysqlHistory::update_column: event [%s], table [%s], column [%s], type [%s] new name [%s], timestamp %s\n", event_name, table_name, column_name,
column_type, tag_name,
TimeToString(timestamp).
c_str());
5889 cmd +=
"INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
5903 sprintf(buf,
"%.0f", (
double)timestamp);
5950 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name='";
5954 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name!='';";
5955 table_name =
"_history_index";
5958 status =
sql->Prepare(table_name, cmd.c_str());
6008 cm_msg(
MERROR,
"ReadPgsqlTableNames",
"Error: Cannot continue, nothing will work after this error\n");
6023int PgsqlHistory::read_column_names(
HsSchemaVector *
sv,
const char* table_name,
const char* event_name)
6026 printf(
"PgsqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
6030 std::vector<std::string>
columns;
6031 fSql->ListColumns(table_name, &
columns);
6035 for (
unsigned i=0;
i<
sv->size();
i++) {
6082 cmd =
"SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
6083 cmd += fSql->QuoteString(event_name);
6086 int status = fSql->Prepare(table_name, cmd.c_str());
6098 const char*
col_name = fSql->GetText(0);
6099 const char*
col_type = fSql->GetText(1);
6100 const char* tag_name = fSql->GetText(2);
6101 const char* tag_type = fSql->GetText(3);
6103 const char* active = fSql->GetText(5);
6119 for (
unsigned i=0;
i<
sv->size();
i++) {
6154 status = fSql->Finalize();
6164 printf(
"PgsqlHistory::read_table_and_event_names!\n");
6168 std::vector<std::string>
tables;
6173 for (
unsigned i=0;
i<
tables.size();
i++) {
6174 const char* table_name =
tables[
i].c_str();
6177 s =
strstr(table_name,
"_history_index");
6178 if (s == table_name)
6195 printf(
"read_table_and_event_names:\n");
6207 printf(
"PgsqlHistory::create_table: event [%s], timestamp %s\n", event_name,
TimeToString(timestamp).
c_str());
6213 if (table_name.length() > 40) {
6214 table_name.resize(40);
6222 status = fSql->OpenTransaction(table_name.c_str());
6251 fSql->RollbackTransaction(table_name.c_str());
6256 fSql->RollbackTransaction(table_name.c_str());
6260 fSql->Exec(table_name.c_str(),
"SAVEPOINT t0");
6262 for (
int j=0;
j<2;
j++) {
6264 cmd +=
"INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
6265 cmd += fSql->QuoteString(event_name);
6270 sprintf(buf,
"%.0f", (
double)timestamp);
6273 cmd += fSql->QuoteString(
"1");
6276 int status = fSql->Exec(table_name.c_str(), cmd.c_str());
6282 fSql->Exec(table_name.c_str(),
"ROLLBACK TO SAVEPOINT t0");
6294 status = fSql->CommitTransaction(table_name.c_str());
6304 cm_msg(
MERROR,
"PgsqlHistory::create_table",
"Could not create table [%s] for event [%s], timestamp %s, after %d attempts", table_name.c_str(), event_name,
TimeToString(timestamp).
c_str(),
max_attempts);
6309int PgsqlHistory::update_column(
const char* event_name,
const char* table_name,
const char* column_name,
const char*
column_type,
const char* tag_name,
const char* tag_type,
const time_t timestamp,
bool active,
bool*
have_transaction)
6312 printf(
"PgsqlHistory::update_column: event [%s], table [%s], column [%s], type [%s] new name [%s], timestamp %s\n", event_name, table_name, column_name,
column_type, tag_name,
TimeToString(timestamp).
c_str());
6315 cmd +=
"INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
6316 cmd += fSql->QuoteString(event_name);
6318 cmd += fSql->QuoteString(table_name);
6320 cmd += fSql->QuoteString(tag_name);
6322 cmd += fSql->QuoteString(tag_type);
6324 cmd += fSql->QuoteString(column_name);
6329 sprintf(buf,
"%.0f", (
double)timestamp);
6333 cmd += fSql->QuoteString(
"1");
6335 cmd += fSql->QuoteString(
"0");
6338 int status = fSql->Exec(table_name, cmd.c_str());
6401 if (
fPath.length() > 0) {
6412 printf(
"FileHistory::hs_clear_cache!\n");
6420 printf(
"FileHistory::hs_disconnect!\n");
6454 printf(
"FileHistory::read_file_list: history directory \"%s\" mtime %d did not change\n",
fPath.c_str(),
int(
stat_buf.st_mtime));
6461 printf(
"FileHistory::read_file_list: reading list of history files in \"%s\"\n",
fPath.c_str());
6463 std::vector<std::string>
flist;
6479 printf(
"file names sorted by time:\n");
6480 for (
unsigned i=0;
i<
flist.size();
i++) {
6486 std::vector<bool>
fread;
6494 for (
size_t i=0;
i<
flist.size();
i++) {
6519 printf(
"FileHistory::read_schema: schema is empty, do a full reload from disk\n");
6538 if ((*sv).find_event(event_name, timestamp)) {
6540 printf(
"FileHistory::read_schema: event [%s] at time %s, no new history files, already have this schema\n", event_name,
TimeToString(timestamp).
c_str());
6587 cm_msg(
MINFO,
"FileHistory::read_schema",
"Loading schema for event \"%s\" timestamp %s, reading %d history files took %.1f sec", event_name,
TimeToString(timestamp).
c_str(), count_read,
read_elapsed);
6638 printf(
"CCC: event [%s] index %d: name [%s] -> [%s]!\n", event_name,
i, s->
fVariables[
i].name.c_str(), tags[
i].
name);
6657 printf(
"*** Schema for event %s has changed!\n", event_name);
6661 printf(
"*** New tags:\n");
6666 printf(
"FileHistory::new_event: event [%s], timestamp %s, ntags %d: schema mismatch, starting a new file.\n", event_name,
TimeToString(timestamp).
c_str(),
ntags);
6678 printf(
"FileHistory::new_event: event [%s], timestamp %s, ntags %d: schema is too old, age %.1f months, starting a new file.\n", event_name,
TimeToString(timestamp).
c_str(),
ntags, (
double)
age/(
double)
kMonth);
6691 printf(
"FileHistory::new_event: event [%s], timestamp %s, ntags %d: file too big, size %.1f MiBytes, starting a new file.\n", event_name,
TimeToString(timestamp).
c_str(),
ntags, size/
MiB);
6699 std::string filename;
6707 cm_msg(
MERROR,
"FileHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
6716 cm_msg(
MERROR,
"FileHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
6730 printf(
"schema for [%s] is %p\n", event_name, s);
6746 printf(
"FileHistory::create_file: event [%s]\n", event_name);
6758 std::string filename;
6797 cm_msg(
MERROR,
"FileHistory::create_file",
"Error: Cannot create file \'%s\' for event \'%s\'", filename.c_str(), event_name);
6803 ss +=
"version: 2.0\n";
6804 ss +=
"event_name: ";
6813 ss +=
"tag: /DWORD 1 4 /timestamp\n";
6870 ss +=
"record_size: ";
6881 ss +=
"data_offset: ";
6903 printf(
"FileHistory::read_file_schema: file %s\n", filename);
6905 FILE*
fp =
fopen(filename,
"r");
6929 char* b =
fgets(buf,
sizeof(buf),
fp);
6995 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Unknown MIDAS data type \'%s\' in history file \'%s\'",
midas_type, filename);
7045 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Malformed history schema in \'%s\', maybe it is not a history file", filename);
7050 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Record size mismatch in history schema from \'%s\', file says %d while total of all tags is %d", filename, s->
fRecordSize,
rd_recsize);
7057 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Could not read history schema from \'%s\', maybe it is not a history file", filename);
7078 cm_msg(
MERROR,
"MakeMidasHistorySqlite",
"Error: Cannot initialize SQLITE history - this MIDAS was built without SQLITE support - HAVE_SQLITE is not defined");
7088 cm_msg(
MERROR,
"MakeMidasHistoryMysql",
"Error: Cannot initialize MySQL history - this MIDAS was built without MySQL support - HAVE_MYSQL is not defined");
7098 cm_msg(
MERROR,
"MakeMidasHistoryPgsql",
"Error: Cannot initialize PgSQL history - this MIDAS was built without PostgreSQL support - HAVE_PGSQL is not defined");
HsFileSchema * read_file_schema(const char *filename)
std::vector< std::string > fSortedFiles
HsSchema * new_event(const char *event_name, time_t timestamp, int ntags, const TAG tags[])
std::vector< bool > fSortedRead
int hs_connect(const char *connect_string)
returns HS_SUCCESS
int hs_clear_cache()
clear internal cache, returns HS_SUCCESS
int create_file(const char *event_name, time_t timestamp, int ntags, const TAG tags[], std::string *filenamep)
int read_schema(HsSchemaVector *sv, const char *event_name, const time_t timestamp)
int read_file_list(bool *pchanged)
int hs_disconnect()
disconnect from history, returns HS_SUCCESS
void remove_inactive_columns()
int write_event(const time_t t, const char *data, const int data_size)
int read_data(const time_t start_time, const time_t end_time, const int num_var, const std::vector< int > &var_schema_index, const int var_index[], const int debug, std::vector< time_t > &last_time, MidasHistoryBufferInterface *buffer[])
void print(bool print_tags=true) const
int read_last_written(const time_t timestamp, const int debug, time_t *last_written)
virtual void print(bool print_tags=true) const
std::vector< int > fOffsets
virtual void remove_inactive_columns()=0
std::vector< HsSchemaEntry > fVariables
virtual int write_event(const time_t t, const char *data, const int data_size)=0
virtual int read_data(const time_t start_time, const time_t end_time, const int num_var, const std::vector< int > &var_schema_index, const int var_index[], const int debug, std::vector< time_t > &last_time, MidasHistoryBufferInterface *buffer[])=0
virtual int read_last_written(const time_t timestamp, const int debug, time_t *last_written)=0
virtual int flush_buffers()=0
virtual int match_event_var(const char *event_name, const char *var_name, const int var_index)
void print(bool print_tags=true) const
std::vector< HsSchema * > fData
HsSchema * find_event(const char *event_name, const time_t timestamp, int debug=0)
HsSchema * operator[](int index) const
int get_transaction_count()
int read_last_written(const time_t timestamp, const int debug, time_t *last_written)
int read_data(const time_t start_time, const time_t end_time, const int num_var, const std::vector< int > &var_schema_index, const int var_index[], const int debug, std::vector< time_t > &last_time, MidasHistoryBufferInterface *buffer[])
std::vector< std::string > fColumnNames
void print(bool print_tags=true) const
void remove_inactive_columns()
std::vector< std::string > fColumnTypes
int fTableTransactionCount
std::vector< bool > fColumnInactive
void increment_transaction_count()
int match_event_var(const char *event_name, const char *var_name, const int var_index)
static std::map< SqlBase *, int > gfTransactionCount
int write_event(const time_t t, const char *data, const int data_size)
void reset_transaction_count()
MidasHistoryBinnedBuffer(time_t first_time, time_t last_time, int num_bins)
~MidasHistoryBinnedBuffer()
void Add(time_t t, double v)
virtual void Add(time_t time, double value)=0
char type[NAME_LENGTH]
history channel name
int update_column(const char *event_name, const char *table_name, const char *column_name, const char *column_type, const char *tag_name, const char *tag_type, const time_t timestamp, bool active, bool *have_transaction)
int read_table_and_event_names(HsSchemaVector *sv)
int create_table(HsSchemaVector *sv, const char *event_name, time_t timestamp)
int read_column_names(HsSchemaVector *sv, const char *table_name, const char *event_name)
void Add(time_t t, double v)
ReadBuffer(time_t first_time, time_t last_time, time_t interval)
void Realloc(int wantalloc)
std::vector< HsSchema * > fEvents
int hs_get_tags(const char *event_name, time_t t, std::vector< TAG > *ptags)
get list of history variables for given event (use event names returned by hs_get_events()) that exis...
virtual int hs_connect(const char *connect_string)=0
returns HS_SUCCESS
HsSchemaVector fWriterCurrentSchema
virtual ~SchemaHistoryBase()
int hs_write_event(const char *event_name, time_t timestamp, int buffer_size, const char *buffer)
see hs_write_event(), returns HS_SUCCESS or HS_FILE_ERROR
int hs_read_buffer(time_t start_time, time_t end_time, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], MidasHistoryBufferInterface *buffer[], int hs_status[])
returns HS_SUCCESS
int hs_define_event(const char *event_name, time_t timestamp, int ntags, const TAG tags[])
see hs_define_event(), returns HS_SUCCESS or HS_FILE_ERROR
int hs_read_binned(time_t start_time, time_t end_time, int num_bins, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], int num_entries[], int *count_bins[], double *mean_bins[], double *rms_bins[], double *min_bins[], double *max_bins[], time_t *bins_first_time[], double *bins_first_value[], time_t *bins_last_time[], double *bins_last_value[], time_t last_time[], double last_value[], int st[])
returns HS_SUCCESS
virtual HsSchema * new_event(const char *event_name, time_t timestamp, int ntags, const TAG tags[])=0
int hs_get_events(time_t t, std::vector< std::string > *pevents)
get list of events that exist(ed) at given time and later (value 0 means "return all events from begi...
int hs_get_last_written(time_t timestamp, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], time_t last_written[])
virtual int hs_set_debug(int debug)
set debug level, returns previous debug level
virtual int hs_disconnect()=0
disconnect from history, returns HS_SUCCESS
int hs_read(time_t start_time, time_t end_time, time_t interval, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], int num_entries[], time_t *time_buffer[], double *data_buffer[], int st[])
see hs_read(), returns HS_SUCCESS
int hs_clear_cache()
clear internal cache, returns HS_SUCCESS
std::string fConnectString
int hs_flush_buffers()
flush buffered data to storage where it is visible to mhttpd
virtual int read_schema(HsSchemaVector *sv, const char *event_name, const time_t timestamp)=0
virtual int ListColumns(const char *table_name, std::vector< std::string > *plist)=0
virtual int Connect(const char *path)=0
virtual int ListColumns(const char *table, std::vector< std::string > *plist)=0
virtual double GetDouble(int column)=0
virtual int RollbackTransaction(const char *table_name)=0
virtual bool IsConnected()=0
virtual int CommitTransaction(const char *table_name)=0
virtual int ListTables(std::vector< std::string > *plist)=0
virtual std::string QuoteId(const char *s)=0
virtual int Disconnect()=0
virtual bool TypesCompatible(int midas_tid, const char *sql_type)=0
virtual int Prepare(const char *table_name, const char *sql)=0
virtual int Exec(const char *sql)=0
virtual int Connect(const char *dsn=0)=0
virtual int Exec(const char *table_name, const char *sql)=0
virtual std::string QuoteString(const char *s)=0
bool fTransactionPerTable
virtual time_t GetTime(int column)=0
virtual const char * GetText(int column)=0
virtual int ExecDisconnected(const char *table_name, const char *sql)=0
virtual int OpenTransaction(const char *table_name)=0
virtual const char * ColumnType(int midas_tid)=0
int update_schema1(HsSqlSchema *s, const time_t timestamp, const int ntags, const TAG tags[], bool write_enable, bool *have_transaction)
int hs_disconnect()
disconnect from history, returns HS_SUCCESS
int hs_set_debug(int debug)
set debug level, returns previous debug level
int read_schema(HsSchemaVector *sv, const char *event_name, const time_t timestamp)
virtual ~SqlHistoryBase()
virtual int create_table(HsSchemaVector *sv, const char *event_name, time_t timestamp)=0
virtual int update_column(const char *event_name, const char *table_name, const char *column_name, const char *column_type, const char *tag_name, const char *tag_type, const time_t timestamp, bool active, bool *have_transaction)=0
virtual int read_column_names(HsSchemaVector *sv, const char *table_name, const char *event_name)=0
virtual int read_table_and_event_names(HsSchemaVector *sv)=0
int hs_connect(const char *connect_string)
returns HS_SUCCESS
HsSchema * new_event(const char *event_name, time_t timestamp, int ntags, const TAG tags[])
int update_schema(HsSqlSchema *s, const time_t timestamp, const int ntags, const TAG tags[], bool write_enable)
int update_column(const char *event_name, const char *table_name, const char *column_name, const char *column_type, const char *tag_name, const char *tag_type, const time_t timestamp, bool active, bool *have_transaction)
int read_column_names(HsSchemaVector *sv, const char *table_name, const char *event_name)
int create_table(HsSchemaVector *sv, const char *event_name, time_t timestamp)
int read_table_and_event_names(HsSchemaVector *sv)
INT cm_get_watchdog_params(BOOL *call_watchdog, DWORD *timeout)
INT cm_set_watchdog_params(BOOL call_watchdog, DWORD timeout)
#define DB_NO_MORE_SUBKEYS
#define HS_UNDEFINED_EVENT
double ss_file_size(const char *path)
INT ss_file_find(const char *path, const char *pattern, char **plist)
INT cm_msg_flush_buffer()
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
const char * rpc_tid_name(INT id)
int rpc_name_tid(const char *name)
static std::string q(const char *s)
static const int tid_size[]
static const char * sql_type_mysql[]
static const char ** sql_type
static std::string MidasNameToSqlName(const char *s)
static int ReadRecord(const char *file_name, int fd, int offset, int recsize, int irec, char *rec)
static int FindTime(const char *file_name, int fd, int offset, int recsize, int nrec, time_t timestamp, int *i1p, time_t *t1p, int *i2p, time_t *t2p, time_t *tstart, time_t *tend, int debug)
static int CreateSqlColumn(SqlBase *sql, const char *table_name, const char *column_name, const char *column_type, bool *have_transaction, int debug)
static int ReadSqliteTableNames(SqlBase *sql, HsSchemaVector *sv, const char *table_name, int debug)
void DoctorPgsqlColumnType(std::string *col_type, const char *index_type)
static int CreateSqlHyperTable(SqlBase *sql, const char *table_name, bool *have_transaction)
void DoctorSqlColumnType(std::string *col_type, const char *index_type)
static bool MatchTagName(const char *tag_name, int n_data, const char *var_tag_name, const int var_tag_index)
static int var_name_cmp(const std::string &v1, const char *v2)
static std::string TimeToString(time_t t)
MidasHistoryInterface * MakeMidasHistorySqlite()
static void PrintTags(int ntags, const TAG tags[])
static char * skip_spaces(char *s)
static bool MatchEventName(const char *event_name, const char *var_event_name)
static HsSqlSchema * NewSqlSchema(HsSchemaVector *sv, const char *table_name, time_t t)
static int StartSqlTransaction(SqlBase *sql, const char *table_name, bool *have_transaction)
MidasHistoryInterface * MakeMidasHistoryMysql()
static int ReadSqliteTableSchema(SqlBase *sql, HsSchemaVector *sv, const char *table_name, int debug)
static int event_name_cmp(const std::string &e1, const char *e2)
MidasHistoryInterface * MakeMidasHistoryPgsql()
MidasHistoryInterface * MakeMidasHistoryFile()
static int ReadMysqlTableNames(SqlBase *sql, HsSchemaVector *sv, const char *table_name, int debug, const char *must_have_event_name, const char *must_have_table_name)
static std::string SmallIntToString(int i)
static int CreateSqlTable(SqlBase *sql, const char *table_name, bool *have_transaction, bool set_default_timestamp=false)
static std::string MidasNameToFileName(const char *s)
BOOL debug
debug printouts
char host_name[HOST_NAME_LENGTH]
#define DIR_SEPARATOR_STR
#define write(n, a, f, d)
struct callback_addr callback
BOOL match(char *pat, char *str)
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)