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);
449 time_t* last_written) = 0;
479 for (
unsigned i=0;
i<
data.size();
i++)
488 for (
unsigned i=0;
i<
data.size();
i++)
549 time_to = (*it)->time_from;
561 printf(
"find_event: All schema for event %s: (total %d)\n", event_name, (
int)
data.size());
563 for (
unsigned i=0;
i<
data.size();
i++) {
572 printf(
"find_event: Found %d schemas for event %s\n",
found, event_name);
578 for (
unsigned i=0;
i<
data.size();
i++) {
599 for (
unsigned i=0;
i<
data.size();
i++) {
664 virtual int Exec(
const char* table_name,
const char* sql) = 0;
668 virtual int Prepare(
const char* table_name,
const char* sql) = 0;
686 virtual std::string
QuoteId(
const char* s) = 0;
793 for (
unsigned j=0;
j<
nv;
j++)
794 printf(
" %d: name [%s], type [%s] tid %d, n_data %d, n_bytes %d, offset %d\n",
j, this->
variables[
j].name.c_str(),
rpc_tid_name(this->
variables[
j].
type), this->
variables[
j].type, this->
variables[
j].n_data, this->
variables[
j].
n_bytes, this->
offsets[
j]);
800 printf(
"event [%s], sql_table [%s], time %s..%s, %d variables, %d bytes\n", this->
event_name.c_str(),
this->table_name.c_str(),
TimeToString(this->
time_from).
c_str(),
TimeToString(this->
time_to).
c_str(), nv,
n_bytes);
802 for (
unsigned j=0;
j<
nv;
j++) {
814 printf(
"event [%s], file_name [%s], time %s..%s, %d variables, %d bytes, dat_offset %d, record_size %d\n", this->
event_name.c_str(),
this->file_name.c_str(),
TimeToString(this->
time_from).
c_str(),
TimeToString(this->
time_to).
c_str(), nv,
n_bytes,
data_offset,
record_size);
816 for (
unsigned j=0;
j<
nv;
j++)
817 printf(
" %d: name [%s], type [%s] tid %d, n_data %d, n_bytes %d, offset %d\n",
j, this->
variables[
j].name.c_str(),
rpc_tid_name(this->
variables[
j].
type), this->
variables[
j].type, this->
variables[
j].n_data, this->
variables[
j].
n_bytes, this->
offsets[
j]);
839 std::string fConnectString;
866 int Exec(
const char* table_name,
const char* sql);
869 int Prepare(
const char* table_name,
const char* sql);
883 std::string
QuoteId(
const char* s);
897 fTransactionPerTable =
false;
926 std::string user_name;
941 char* s =
fgets(buf,
sizeof(buf),
fp);
978 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);
990 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));
1001 status = Exec(
"(notable)",
"SET SESSION sql_mode='ANSI'");
1003 cm_msg(
MERROR,
"Mysql::Connect",
"Cannot set ANSI mode, nothing will work");
1009 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);
1013 fIsConnected =
true;
1036int Mysql::Disconnect()
1052 fIsConnected =
false;
1056bool Mysql::IsConnected()
1058 return fIsConnected;
1061int Mysql::OpenTransaction(
const char* table_name)
1063 return Exec(table_name,
"START TRANSACTION");
1067int Mysql::CommitTransaction(
const char* table_name)
1069 Exec(table_name,
"COMMIT");
1073int Mysql::RollbackTransaction(
const char* table_name)
1075 Exec(table_name,
"ROLLBACK");
1079int Mysql::ListTables(std::vector<std::string> *
plist)
1085 printf(
"Mysql::ListTables!\n");
1102 std::string
tn = GetText(0);
1111int Mysql::ListColumns(
const char* table_name, std::vector<std::string> *
plist)
1117 printf(
"Mysql::ListColumns for table \'%s\'\n", table_name);
1122 cmd +=
"SHOW COLUMNS FROM ";
1123 cmd += QuoteId(table_name);
1126 status = Prepare(table_name, cmd.c_str());
1136 std::string
cn = GetText(0);
1137 std::string
ct = GetText(1);
1150int Mysql::Exec(
const char* table_name,
const char* sql)
1153 printf(
"Mysql::Exec(%s, %s)\n", table_name, sql);
1180 return ExecDisconnected(table_name, sql);
1188int Mysql::ExecDisconnected(
const char* table_name,
const char* sql)
1191 printf(
"Mysql::ExecDisconnected(%s, %s)\n", table_name, sql);
1196 cm_msg(
MERROR,
"Mysql::ExecDisconnected",
"Error: Disconnected database buffer overflow, size %d, subsequent events are lost", (
int)
fDisconnectedBuffer.size());
1205 int status = Connect(fConnectString.c_str());
1226int Mysql::Prepare(
const char* table_name,
const char* sql)
1229 printf(
"Mysql::Prepare(%s, %s)\n", table_name, sql);
1248 status = Connect(fConnectString.c_str());
1253 cm_msg(
MERROR,
"Mysql::Prepare",
"mysql_query(%s) - MySQL server has gone away, and couldn't reconnect - %d", sql,
status);
1261 cm_msg(
MINFO,
"Mysql::Prepare",
"Reconnected to MySQL after long inactivity.");
1282 printf(
"Mysql::Step()\n");
1300const char* Mysql::GetText(
int column)
1313double Mysql::GetDouble(
int column)
1323int Mysql::Finalize()
1336const char* Mysql::ColumnType(
int midas_tid)
1373 printf(
"type mismatch!\n");
1378std::string Mysql::QuoteId(
const char* s)
1387std::string Mysql::QuoteString(
const char* s)
1393 while (
int c = *s++) {
1421#include <libpq-fe.h>
1426 std::string fConnectString;
1445 int Connect(
const char* path);
1454 int Exec(
const char* table_name,
const char* sql);
1457 int Prepare(
const char* table_name,
const char* sql);
1472 std::string
QuoteId(
const char* s);
1487 fTransactionPerTable =
false;
1516 std::string user_name;
1531 char* s =
fgets(buf,
sizeof(buf),
fp);
1568 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);
1573 msg.erase(std::remove(msg.begin(), msg.end(),
'\n'), msg.end());
1574 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());
1582 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);
1586 fIsConnected =
true;
1607 status = Prepare(
"pg_extensions",
"select extname from pg_extension where extname = 'timescaledb';");
1610 cm_msg(
MERROR,
"Pgsql::Connect",
"TimescaleDB extension not installed");
1615 status = Prepare(
"pg_extensions",
"select extname from pg_extension where extname = 'timescaledb_toolkit';");
1618 cm_msg(
MERROR,
"Pgsql::Connect",
"TimescaleDB_toolkit extension not installed");
1623 cm_msg(
MINFO,
"Pgsql::Connect",
"TimescaleDB extensions found - downsampling enabled");
1629int Pgsql::Disconnect()
1637 fIsConnected =
false;
1641bool Pgsql::IsConnected()
1643 return fIsConnected;
1646int Pgsql::OpenTransaction(
const char* table_name)
1648 return Exec(table_name,
"BEGIN TRANSACTION;");
1651int Pgsql::CommitTransaction(
const char* table_name)
1653 return Exec(table_name,
"COMMIT;");
1656int Pgsql::RollbackTransaction(
const char* table_name)
1658 return Exec(table_name,
"ROLLBACK;");
1661int Pgsql::ListTables(std::vector<std::string> *
plist)
1667 printf(
"Pgsql::ListTables!\n");
1669 int status = Prepare(
"pg_tables",
"select tablename from pg_tables where schemaname = 'public';");
1679 std::string
tn = GetText(0);
1688int Pgsql::ListColumns(
const char* table_name, std::vector<std::string> *
plist)
1694 printf(
"Pgsql::ListColumns for table \'%s\'\n", table_name);
1697 cmd +=
"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = ";
1698 cmd += QuoteString(table_name);
1701 int status = Prepare(table_name, cmd.c_str());
1710 std::string
cn = GetText(0);
1711 std::string
ct = GetText(1);
1721int Pgsql::Exec(
const char* table_name,
const char* sql)
1724 printf(
"Pgsql::Exec(%s, %s)\n", table_name, sql);
1744 return ExecDisconnected(table_name, sql);
1751int Pgsql::ExecDisconnected(
const char* table_name,
const char* sql)
1754 printf(
"Pgsql::ExecDisconnected(%s, %s)\n", table_name, sql);
1759 cm_msg(
MERROR,
"Pgsql::ExecDisconnected",
"Error: Disconnected database buffer overflow, size %d, subsequent events are lost", (
int)
fDisconnectedBuffer.size());
1768 int status = Connect(fConnectString.c_str());
1789int Pgsql::Prepare(
const char* table_name,
const char* sql)
1792 printf(
"Pgsql::Prepare(%s, %s)\n", table_name, sql);
1804 int status = Connect(fConnectString.c_str());
1809 cm_msg(
MERROR,
"Pgsql::Prepare",
"PQexec(%s) PostgreSQL server has gone away, and couldn't reconnect - %d", sql,
status);
1816 cm_msg(
MINFO,
"Pgsql::Prepare",
"Reconnected to PostgreSQL after long inactivity.");
1824std::string Pgsql::BuildDownsampleQuery(
const time_t start_time,
const time_t end_time,
const int npoints,
1825 const char* table_name,
const char* column_name)
1828 cmd +=
"SELECT extract(epoch from time::TIMESTAMPTZ) as _i_time, value ";
1830 cmd +=
" FROM unnest(( SELECT lttb";
1831 cmd +=
"(_t_time, ";
1834 cmd += std::to_string(
npoints);
1837 cmd += QuoteId(table_name);
1838 cmd +=
" WHERE _t_time BETWEEN ";
1839 cmd +=
"to_timestamp(";
1841 cmd +=
") AND to_timestamp(";
1843 cmd +=
") )) ORDER BY time;";
1861const char* Pgsql::GetText(
int column)
1872double Pgsql::GetDouble(
int column)
1882int Pgsql::Finalize()
1893const char* Pgsql::ColumnType(
int midas_tid)
1926 printf(
"type mismatch!\n");
1931std::string Pgsql::QuoteId(
const char* s)
1940std::string Pgsql::QuoteString(
const char* s)
1959typedef std::map<std::string, sqlite3*>
DbMap;
1975 int Connect(
const char* path);
1985 int Exec(
const char* table_name,
const char* sql);
1988 int Prepare(
const char* table_name,
const char* sql);
2002 std::string
QuoteId(
const char* s);
2006std::string Sqlite::QuoteId(
const char* s)
2015std::string Sqlite::QuoteString(
const char* s)
2024const char* Sqlite::ColumnType(
int midas_tid)
2049const char* Sqlite::GetText(
int column)
2059double Sqlite::GetDouble(
int column)
2066 fIsConnected =
false;
2083int Sqlite::ConnectTable(
const char* table_name)
2085 std::string
fname = fPath +
"mh_" + table_name +
".sqlite3";
2098#if SQLITE_VERSION_NUMBER >= 3006020
2104#warning Missing sqlite3_extended_result_codes()!
2109 Exec(table_name,
"PRAGMA journal_mode=persist;");
2110 Exec(table_name,
"PRAGMA synchronous=normal;");
2112 Exec(table_name,
"PRAGMA journal_size_limit=-1;");
2115 Exec(table_name,
"PRAGMA legacy_file_format;");
2116 Exec(table_name,
"PRAGMA synchronous;");
2117 Exec(table_name,
"PRAGMA journal_mode;");
2118 Exec(table_name,
"PRAGMA journal_size_limit;");
2121#ifdef SQLITE_LIMIT_COLUMN
2129 cm_msg(
MINFO,
"Sqlite::Connect",
"Table %s: connected to Sqlite file \'%s\'", table_name,
fname.c_str());
2134sqlite3* Sqlite::GetTable(
const char* table_name)
2145 return fMap[table_name];
2148int Sqlite::Connect(
const char* path)
2156 if (fPath.length() > 0) {
2162 cm_msg(
MINFO,
"Sqlite::Connect",
"Connected to Sqlite database in \'%s\'", fPath.c_str());
2164 fIsConnected =
true;
2169int Sqlite::Disconnect()
2175 const char* table_name =
iter->first.c_str();
2185 fIsConnected =
false;
2190bool Sqlite::IsConnected()
2192 return fIsConnected;
2195int Sqlite::OpenTransaction(
const char* table_name)
2197 int status = Exec(table_name,
"BEGIN TRANSACTION");
2201int Sqlite::CommitTransaction(
const char* table_name)
2203 int status = Exec(table_name,
"COMMIT TRANSACTION");
2207int Sqlite::RollbackTransaction(
const char* table_name)
2209 int status = Exec(table_name,
"ROLLBACK TRANSACTION");
2213int Sqlite::Prepare(
const char* table_name,
const char* sql)
2220 printf(
"Sqlite::Prepare(%s, %s)\n", table_name, sql);
2225#if SQLITE_VERSION_NUMBER >= 3006020
2228#warning Missing sqlite3_prepare_v2()!
2246 printf(
"Sqlite::Step()\n");
2264int Sqlite::Finalize()
2267 printf(
"Sqlite::Finalize()\n");
2288int Sqlite::ListTables(std::vector<std::string> *
plist)
2294 printf(
"Sqlite::ListTables at path [%s]\n", fPath.c_str());
2298 const char* cmd =
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
2311 const char*
dn =
de->d_name;
2326 char table_name[256];
2327 mstrlcpy(table_name,
dn+3,
sizeof(table_name));
2329 char*
ss =
strstr(table_name,
".sqlite3");
2336 status = Prepare(table_name, cmd);
2345 const char*
tn = GetText(0);
2359int Sqlite::ListColumns(
const char* table, std::vector<std::string> *
plist)
2365 printf(
"Sqlite::ListColumns for table \'%s\'\n", table);
2368 cmd =
"PRAGMA table_info(";
2374 status = Prepare(table, cmd.c_str());
2383 const char*
colname = GetText(1);
2384 const char*
coltype = GetText(2);
2399 printf(
"history_sqlite::callback---->\n");
2407int Sqlite::Exec(
const char* table_name,
const char* sql)
2422 printf(
"Sqlite::Exec(%s, %s)\n", table_name, sql);
2444int Sqlite::ExecDisconnected(
const char* table_name,
const char* sql)
2446 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->
file_name.c_str(), s->
data_offset, s->
record_size, 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->
file_name.c_str(), fd, s->
data_offset, s->
record_size,
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->
file_name.c_str(), fd, s->
data_offset, s->
record_size,
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->
file_name.c_str(),
TimeToString(s->
time_from).c_str(),
TimeToString(s->
time_to).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->
file_name.c_str(),
TimeToString(s->
time_from).c_str(),
TimeToString(s->
time_to).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);
3421 cm_msg(
MERROR,
"hs_define_event",
"Error: History event \'%s\' tag \'%s\' at index %d has invalid type %d",
3427 "Error: History event \'%s\' tag \'%s\' at index %d has forbidden type TID_STRING", event_name,
3431 if (tags[
i].n_data <= 0) {
3432 cm_msg(
MERROR,
"hs_define_event",
"Error: History event \'%s\' tag \'%s\' at index %d has invalid n_data %d",
3433 event_name, tags[
i].
name,
i, tags[
i].n_data);
3439 std::vector<std::string>
names;
3449 "Error: History event \'%s\' has duplicate tag name \'%s\'", event_name,
3465 if (!
var.inactive) {
3473 for (
unsigned int i=0;
i<
fEvents.size();
i++)
3490 printf(
"hs_write_event: write event \'%s\', time %d, size %d\n", event_name, (
int)timestamp, buffer_size);
3518 if (buffer_size > s->
n_bytes) {
3522 cm_msg(
MERROR,
"hs_write_event",
"Event \'%s\' data size mismatch: expected %d bytes, got %d bytes", s->
event_name.c_str(), s->
n_bytes, buffer_size);
3532 cm_msg(
MERROR,
"hs_write_event",
"Event \'%s\' data size mismatch: expected %d bytes, got %d bytes", s->
event_name.c_str(), s->
n_bytes, buffer_size);
3545 assert(buffer_size == s->
n_bytes);
3552 cm_msg(
MERROR,
"hs_write_event",
"Event \'%s\' disabled after write error %d", event_name,
status);
3564 printf(
"hs_flush_buffers!\n");
3566 for (
unsigned int i=0;
i<
fEvents.size();
i++)
3583 printf(
"SchemaHistoryBase::hs_clear_cache!\n");
3601 printf(
"hs_get_events: available schema:\n");
3625 printf(
"hs_get_events: returning %d events\n", (
int)
pevents->size());
3662 for (
unsigned k=0;
k<
ptags->size();
k++)
3674 ptags->push_back(t);
3683 printf(
"hs_get_tags: event [%s], returning %d tags\n", event_name, (
int)
ptags->size());
3684 for (
unsigned i=0;
i<
ptags->size();
i++) {
3699 last_written[
j] = 0;
3734 if (
lw > last_written[
j])
3735 last_written[
j] =
lw;
3752 int num_var,
const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3805 cm_msg(
MERROR,
"SchemaHistoryBase::hs_read_buffer",
"History internal error, schema is not ordered by time. Please report this error to the midas forum.");
3812 std::vector<HsSchema*>
slist;
3813 std::vector<std::vector<int>>
smap;
3824 std::vector<int>
sm;
3848 printf(
"Found %d matching schema:\n", (
int)
slist.size());
3850 for (
size_t i=0;
i<
slist.size();
i++) {
3871 printf(
"Check schema %zu/%zu: prev from %s, this from %s to %s, compare %d\n",
ss,
slist.size(),
3880 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.");
3891 for (
int i=
slist.size()-1;
i>=0;
i--) {
3909 const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
3957 int num_var,
const char*
const event_name[],
const char*
const var_name[],
const int var_index[],
4077 for (
unsigned i=0;
i<
sv->size();
i++) {
4127 cm_msg(
MERROR,
"NewSqlSchema",
"Error: Unexpected ordering of schema for table \'%s\', good luck!", table_name);
4141 cm_msg(
MERROR,
"NewSqlSchema",
"Error: Cannot clone schema for table \'%s\', good luck!", table_name);
4164 assert(n_data == 1);
4165 assert(
strlen(column_name) > 0);
4166 assert(
offset < data_size);
4183 sprintf(buf,
"%u",((
unsigned char *)ptr)[
j]);
4186 sprintf(buf,
"%d",((
signed char*)ptr)[
j]);
4190 sprintf(buf,
"\'%c\'",((
char*)ptr)[
j]);
4193 sprintf(buf,
"%u",((
unsigned short *)ptr)[
j]);
4196 sprintf(buf,
"%d",((
short *)ptr)[
j]);
4199 sprintf(buf,
"%u",((
unsigned int *)ptr)[
j]);
4202 sprintf(buf,
"%d",((
int *)ptr)[
j]);
4205 sprintf(buf,
"%u",((
unsigned int *)ptr)[
j]);
4209 sprintf(buf,
"\'%.8g\'",((
float*)ptr)[
j]);
4213 sprintf(buf,
"\'%.16g\'",((
double*)ptr)[
j]);
4224 strftime(buf,
sizeof(buf)-1,
"%Y-%m-%d %H:%M:%S.0", &
tms);
4227 cmd =
"INSERT INTO ";
4229 cmd +=
" (_t_time, _i_time";
4231 cmd +=
") VALUES (";
4277 cmd +=
"SELECT _i_time FROM ";
4279 cmd +=
" WHERE _i_time < ";
4281 cmd +=
" ORDER BY _i_time DESC LIMIT 2;";
4340 cmd +=
"SELECT _i_time, ";
4344 cmd +=
" WHERE _i_time>=";
4346 cmd +=
" and _i_time<=";
4348 cmd +=
" ORDER BY _i_time;";
4385 buffer[
i]->
Add(t, v);
4456 cmd =
"CREATE TABLE ";
4457 cmd += sql->
QuoteId(table_name);
4459 cmd +=
" (_t_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, _i_time INTEGER NOT NULL DEFAULT 0);";
4461 cmd +=
" (_t_time TIMESTAMP NOT NULL, _i_time INTEGER NOT NULL);";
4468 cm_msg(
MINFO,
"CreateSqlTable",
"Adding SQL table \"%s\", but it already exists", table_name);
4474 cm_msg(
MINFO,
"CreateSqlTable",
"Adding SQL table \"%s\", error status %d", table_name,
status);
4479 cm_msg(
MINFO,
"CreateSqlTable",
"Adding SQL table \"%s\"", table_name);
4490 cmd =
"CREATE INDEX ";
4493 cmd += sql->
QuoteId(table_name);
4494 cmd +=
" (_i_time ASC);";
4500 cmd =
"CREATE INDEX ";
4503 cmd += sql->
QuoteId(table_name);
4504 cmd +=
" (_t_time);";
4522 cmd =
"CREATE TABLE ";
4523 cmd += sql->
QuoteId(table_name);
4524 cmd +=
" (_t_time TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, _i_time INTEGER NOT NULL DEFAULT 0);";
4529 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Adding SQL table \"%s\", but it already exists", table_name);
4535 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Adding SQL table \"%s\", error status %d", table_name,
status);
4540 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Adding SQL table \"%s\"", table_name);
4543 cmd =
"SELECT create_hypertable(";
4545 cmd +=
", '_t_time');";
4551 cm_msg(
MINFO,
"CreateSqlHyperTable",
"Converting SQL table to hypertable \"%s\", error status %d", table_name,
status);
4564 cmd =
"CREATE INDEX ";
4567 cmd += sql->
QuoteId(table_name);
4568 cmd +=
" (_i_time ASC);";
4574 cmd =
"CREATE INDEX ";
4577 cmd += sql->
QuoteId(table_name);
4578 cmd +=
" (_t_time);";
4590 printf(
"CreateSqlColumn: table [%s], column [%s], type [%s]\n", table_name, column_name,
column_type);
4597 cmd =
"ALTER TABLE ";
4598 cmd += sql->
QuoteId(table_name);
4599 cmd +=
" ADD COLUMN ";
4600 cmd += sql->
QuoteId(column_name);
4607 cm_msg(
MINFO,
"CreateSqlColumn",
"Adding column \"%s\" to SQL table \"%s\", status %d", column_name, table_name,
status);
4691 printf(
"hs_disconnect!\n");
4727 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
4742 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot update schema database for event \'%s\', see previous messages", event_name);
4747 printf(
"SqlHistory::new_event: schema for [%s] is %p\n", event_name, s);
4754 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
4765 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot update schema database for event \'%s\', see previous messages", event_name);
4770 printf(
"SqlHistory::new_event: schema for [%s] is %p\n", event_name, s);
4779 cm_msg(
MERROR,
"SqlHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
4796 printf(
"SqlHistory::read_schema: loading schema for event [%s] at time %s\n", event_name,
TimeToString(timestamp).
c_str());
4808 if (event_name ==
NULL)
4811 for (
unsigned i=0;
i<
sv->size();
i++) {
4820 unsigned nn =
sv->size();
4825 if (
sv->size() !=
nn)
4863 printf(
"update_schema1\n");
4871 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
4876 if (tags[
i].n_data > 1) {
4906 printf(
"Incompatible column!\n");
4969 for (
int t=0; t<20; t++) {
5013 cm_msg(
MERROR,
"SqlHistory::update_schema",
"Duplicate tags or SQL columns for history event \"%s\" tag \"%s\"", s->
event_name.c_str(),
tagname.c_str());
5026 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
5029 if (tags[
i].n_data > 1) {
5064 printf(
"Return error!\n");
5078 printf(
"ReadSqliteTableNames: table [%s]\n", table_name);
5084 cmd =
"SELECT event_name, _i_time FROM \'_event_name_";
5086 cmd +=
"\' WHERE table_name='";
5123 printf(
"ReadSqliteTableSchema: table [%s]\n", table_name);
5163 printf(
"SqliteHistory::read_table_and_event_names!\n");
5167 std::vector<std::string>
tables;
5172 for (
unsigned i=0;
i<
tables.size();
i++) {
5173 const char* table_name =
tables[
i].c_str();
5176 s =
strstr(table_name,
"_event_name_");
5177 if (s == table_name)
5179 s =
strstr(table_name,
"_column_names_");
5180 if (s == table_name)
5192 printf(
"SqliteHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5196 std::vector<std::string>
columns;
5201 for (
unsigned i=0;
i<
sv->size();
i++) {
5247 tn +=
"_column_names_";
5251 cmd =
"SELECT column_name, tag_name, tag_type, _i_time FROM ";
5253 cmd +=
" WHERE table_name=";
5255 cmd +=
" ORDER BY _i_time ASC;";
5285 for (
unsigned i=0;
i<
sv->size();
i++) {
5313 printf(
"SqliteHistory::create_table: event [%s], timestamp %s\n", event_name,
TimeToString(timestamp).
c_str());
5335 en +=
"_event_name_";
5338 cmd =
"CREATE TABLE ";
5340 cmd +=
" (table_name TEXT NOT NULL, event_name TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5344 cmd =
"INSERT INTO ";
5346 cmd +=
" (table_name, event_name, _i_time) VALUES (";
5357 cn +=
"_column_names_";
5360 cmd =
"CREATE TABLE ";
5362 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);";
5377 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());
5385 cmd =
"INSERT INTO \'_column_names_";
5387 cmd +=
"\' (table_name, column_name, tag_name, tag_type, column_type, _i_time) VALUES (\'";
5433 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name='";
5437 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name!='';";
5438 table_name =
"_history_index";
5491 cm_msg(
MERROR,
"ReadMysqlTableNames",
"Error: Cannot continue, nothing will work after this error\n");
5509 printf(
"MysqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5513 std::vector<std::string>
columns;
5518 for (
unsigned i=0;
i<
sv->size();
i++) {
5566 cmd =
"SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
5604 for (
unsigned i=0;
i<
sv->size();
i++) {
5648 printf(
"ReadMysqlTableSchema: table [%s]\n", table_name);
5670 printf(
"MysqlHistory::read_table_and_event_names!\n");
5674 std::vector<std::string>
tables;
5679 for (
unsigned i=0;
i<
tables.size();
i++) {
5680 const char* table_name =
tables[
i].c_str();
5683 s =
strstr(table_name,
"_history_index");
5684 if (s == table_name)
5701 printf(
"read_table_and_event_names:\n");
5713 printf(
"MysqlHistory::create_table: event [%s], timestamp %s\n", event_name,
TimeToString(timestamp).
c_str());
5719 if (table_name.length() > 40) {
5720 table_name.resize(40);
5760 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());
5768 for (
int j=0;
j<2;
j++) {
5770 cmd +=
"INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
5776 sprintf(buf,
"%.0f", (
double)timestamp);
5806 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);
5814 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());
5817 cmd +=
"INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
5831 sprintf(buf,
"%.0f", (
double)timestamp);
5878 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name='";
5882 cmd =
"SELECT event_name, table_name, itimestamp FROM _history_index WHERE table_name!='';";
5883 table_name =
"_history_index";
5936 cm_msg(
MERROR,
"ReadPgsqlTableNames",
"Error: Cannot continue, nothing will work after this error\n");
5951int PgsqlHistory::read_column_names(
HsSchemaVector *
sv,
const char* table_name,
const char* event_name)
5954 printf(
"PgsqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5958 std::vector<std::string>
columns;
5959 fSql->ListColumns(table_name, &
columns);
5963 for (
unsigned i=0;
i<
sv->size();
i++) {
6009 cmd =
"SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
6010 cmd += fSql->QuoteString(event_name);
6013 int status = fSql->Prepare(table_name, cmd.c_str());
6025 const char*
col_name = fSql->GetText(0);
6026 const char*
col_type = fSql->GetText(1);
6027 const char* tag_name = fSql->GetText(2);
6028 const char* tag_type = fSql->GetText(3);
6030 const char* active = fSql->GetText(5);
6046 for (
unsigned i=0;
i<
sv->size();
i++) {
6081 status = fSql->Finalize();
6091 printf(
"PgsqlHistory::read_table_and_event_names!\n");
6095 std::vector<std::string>
tables;
6100 for (
unsigned i=0;
i<
tables.size();
i++) {
6101 const char* table_name =
tables[
i].c_str();
6104 s =
strstr(table_name,
"_history_index");
6105 if (s == table_name)
6122 printf(
"read_table_and_event_names:\n");
6134 printf(
"PgsqlHistory::create_table: event [%s], timestamp %s\n", event_name,
TimeToString(timestamp).
c_str());
6140 if (table_name.length() > 40) {
6141 table_name.resize(40);
6149 status = fSql->OpenTransaction(table_name.c_str());
6178 fSql->RollbackTransaction(table_name.c_str());
6183 fSql->RollbackTransaction(table_name.c_str());
6187 fSql->Exec(table_name.c_str(),
"SAVEPOINT t0");
6189 for (
int j=0;
j<2;
j++) {
6191 cmd +=
"INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
6192 cmd += fSql->QuoteString(event_name);
6197 sprintf(buf,
"%.0f", (
double)timestamp);
6200 cmd += fSql->QuoteString(
"1");
6203 int status = fSql->Exec(table_name.c_str(), cmd.c_str());
6209 fSql->Exec(table_name.c_str(),
"ROLLBACK TO SAVEPOINT t0");
6221 status = fSql->CommitTransaction(table_name.c_str());
6231 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);
6236int 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)
6239 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());
6242 cmd +=
"INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
6243 cmd += fSql->QuoteString(event_name);
6245 cmd += fSql->QuoteString(table_name);
6247 cmd += fSql->QuoteString(tag_name);
6249 cmd += fSql->QuoteString(tag_type);
6251 cmd += fSql->QuoteString(column_name);
6256 sprintf(buf,
"%.0f", (
double)timestamp);
6260 cmd += fSql->QuoteString(
"1");
6262 cmd += fSql->QuoteString(
"0");
6265 int status = fSql->Exec(table_name, cmd.c_str());
6328 if (
fPath.length() > 0) {
6339 printf(
"FileHistory::hs_clear_cache!\n");
6347 printf(
"FileHistory::hs_disconnect!\n");
6381 printf(
"FileHistory::read_file_list: history directory \"%s\" mtime %d did not change\n",
fPath.c_str(),
int(
stat_buf.st_mtime));
6388 printf(
"FileHistory::read_file_list: reading list of history files in \"%s\"\n",
fPath.c_str());
6390 std::vector<std::string>
flist;
6406 printf(
"file names sorted by time:\n");
6407 for (
unsigned i=0;
i<
flist.size();
i++) {
6413 std::vector<bool>
fread;
6421 for (
size_t i=0;
i<
flist.size();
i++) {
6446 printf(
"FileHistory::read_schema: schema is empty, do a full reload from disk\n");
6465 if ((*sv).find_event(event_name, timestamp)) {
6467 printf(
"FileHistory::read_schema: event [%s] at time %s, no new history files, already have this schema\n", event_name,
TimeToString(timestamp).
c_str());
6514 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);
6565 printf(
"CCC: event [%s] index %d: name [%s] -> [%s]!\n", event_name,
i, s->
variables[
i].name.c_str(), tags[
i].
name);
6584 printf(
"*** Schema for event %s has changed!\n", event_name);
6588 printf(
"*** New tags:\n");
6593 printf(
"FileHistory::new_event: event [%s], timestamp %s, ntags %d: schema mismatch, starting a new file.\n", event_name,
TimeToString(timestamp).
c_str(),
ntags);
6605 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);
6618 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);
6626 std::string filename;
6634 cm_msg(
MERROR,
"FileHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
6643 cm_msg(
MERROR,
"FileHistory::new_event",
"Error: Cannot create schema for event \'%s\', see previous messages", event_name);
6657 printf(
"schema for [%s] is %p\n", event_name, s);
6673 printf(
"FileHistory::create_file: event [%s]\n", event_name);
6685 std::string filename;
6724 cm_msg(
MERROR,
"FileHistory::create_file",
"Error: Cannot create file \'%s\' for event \'%s\'", filename.c_str(), event_name);
6730 ss +=
"version: 2.0\n";
6731 ss +=
"event_name: ";
6740 ss +=
"tag: /DWORD 1 4 /timestamp\n";
6797 ss +=
"record_size: ";
6808 ss +=
"data_offset: ";
6830 printf(
"FileHistory::read_file_schema: file %s\n", filename);
6856 char* b =
fgets(buf,
sizeof(buf),
fp);
6922 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Unknown MIDAS data type \'%s\' in history file \'%s\'",
midas_type, filename);
6972 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Malformed history schema in \'%s\', maybe it is not a history file", filename);
6977 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->
record_size,
rd_recsize);
6984 cm_msg(
MERROR,
"FileHistory::read_file_schema",
"Could not read history schema from \'%s\', maybe it is not a history file", filename);
7005 cm_msg(
MERROR,
"MakeMidasHistorySqlite",
"Error: Cannot initialize SQLITE history - this MIDAS was built without SQLITE support - HAVE_SQLITE is not defined");
7015 cm_msg(
MERROR,
"MakeMidasHistoryMysql",
"Error: Cannot initialize MySQL history - this MIDAS was built without MySQL support - HAVE_MYSQL is not defined");
7025 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 print(bool print_tags=true) const
std::vector< HsSchema * > data
HsSchema * find_event(const char *event_name, const time_t timestamp, int debug=0)
HsSchema * operator[](int index) const
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)
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
int count_write_undersize
std::vector< int > offsets
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
std::vector< HsSchemaEntry > variables
virtual int match_event_var(const char *event_name, const char *var_name, const int var_index)
int get_transaction_count()
int read_last_written(const time_t timestamp, const int debug, time_t *last_written)
int table_transaction_count
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
void increment_transaction_count()
std::vector< std::string > column_types
std::vector< std::string > disconnected_buffer
static std::map< SqlBase *, int > global_transaction_count
int match_event_var(const char *event_name, const char *var_name, const int var_index)
int write_event(const time_t t, const char *data, const int data_size)
void reset_transaction_count()
std::vector< std::string > column_names