MIDAS
Loading...
Searching...
No Matches
MysqlHistory Class Reference
Inheritance diagram for MysqlHistory:
Collaboration diagram for MysqlHistory:

Public Member Functions

 MysqlHistory ()
 
int read_table_and_event_names (HsSchemaVector *sv)
 
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 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)
 
- Public Member Functions inherited from SqlHistoryBase
 SqlHistoryBase ()
 
virtual ~SqlHistoryBase ()
 
int hs_set_debug (int debug)
 set debug level, returns previous debug level
 
int hs_connect (const char *connect_string)
 returns HS_SUCCESS
 
int hs_disconnect ()
 disconnect from history, returns HS_SUCCESS
 
HsSchemanew_event (const char *event_name, time_t timestamp, int ntags, const TAG tags[])
 
int read_schema (HsSchemaVector *sv, const char *event_name, const time_t timestamp)
 
- Public Member Functions inherited from SchemaHistoryBase
 SchemaHistoryBase ()
 
virtual ~SchemaHistoryBase ()
 
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_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_flush_buffers ()
 flush buffered data to storage where it is visible to mhttpd
 
int hs_clear_cache ()
 clear internal cache, returns HS_SUCCESS
 
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 beginning of time"), returns HS_SUCCESS
 
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 exist(ed) at given time and later (value 0 means "all variables for this event that ever existed"), also see hs_get_tags(), returns HS_SUCCESS
 
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[])
 
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_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_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
 
- Public Member Functions inherited from MidasHistoryInterface
 MidasHistoryInterface ()
 history type: MIDAS, ODBC, SQLITE, etc
 
virtual ~MidasHistoryInterface ()
 

Additional Inherited Members

- Public Attributes inherited from SqlHistoryBase
SqlBasefSql
 
- Public Attributes inherited from MidasHistoryInterface
char name [NAME_LENGTH]
 
char type [NAME_LENGTH]
 history channel name
 
- Protected Member Functions inherited from SqlHistoryBase
int update_schema (HsSqlSchema *s, const time_t timestamp, const int ntags, const TAG tags[], bool write_enable)
 
int update_schema1 (HsSqlSchema *s, const time_t timestamp, const int ntags, const TAG tags[], bool write_enable, bool *have_transaction)
 
- Protected Attributes inherited from SchemaHistoryBase
int fDebug
 
std::string fConnectString
 
HsSchemaVector fWriterCurrentSchema
 
std::vector< HsSchema * > fEvents
 
HsSchemaVector fSchema
 

Detailed Description

Definition at line 5409 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ MysqlHistory()

MysqlHistory::MysqlHistory ( )
inline

Definition at line 5412 of file history_schema.cxx.

5412 { // ctor
5413#ifdef HAVE_MYSQL
5414 fSql = new Mysql();
5415#endif
5416 }
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Here is the call graph for this function:

Member Function Documentation

◆ create_table()

int MysqlHistory::create_table ( HsSchemaVector sv,
const char event_name,
time_t  timestamp 
)
virtual

Implements SqlHistoryBase.

Definition at line 5710 of file history_schema.cxx.

5711{
5712 if (fDebug)
5713 printf("MysqlHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5714
5715 int status;
5716 std::string table_name = MidasNameToSqlName(event_name);
5717
5718 // MySQL table name length limit is 64 bytes
5719 if (table_name.length() > 40) {
5720 table_name.resize(40);
5721 table_name += "_T";
5722 }
5723
5724 time_t now = time(NULL);
5725
5726 int max_attempts = 10;
5727 for (int i=0; i<max_attempts; i++) {
5728 status = fSql->OpenTransaction(table_name.c_str());
5729 if (status != DB_SUCCESS) {
5730 return HS_FILE_ERROR;
5731 }
5732
5733 bool have_transaction = true;
5734
5735 std::string xtable_name = table_name;
5736
5737 if (i>0) {
5738 xtable_name += "_";
5740 if (i>1) {
5741 xtable_name += "_";
5742 char buf[256];
5743 sprintf(buf, "%d", i);
5744 xtable_name += buf;
5745 }
5746 }
5747
5749
5750 //printf("event [%s] create table [%s] status %d\n", event_name, xtable_name.c_str(), status);
5751
5752 if (status == DB_KEY_EXIST) {
5753 // already exists, try with different name!
5754 fSql->RollbackTransaction(table_name.c_str());
5755 continue;
5756 }
5757
5758 if (status != HS_SUCCESS) {
5759 // MYSQL cannot roll back "create table", if we cannot create SQL tables, nothing will work. Give up now.
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());
5761 abort();
5762
5763 // fatal error, give up!
5764 fSql->RollbackTransaction(table_name.c_str());
5765 break;
5766 }
5767
5768 for (int j=0; j<2; j++) {
5769 std::string cmd;
5770 cmd += "INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
5771 cmd += fSql->QuoteString(event_name);
5772 cmd += ", ";
5773 cmd += fSql->QuoteString(xtable_name.c_str());
5774 cmd += ", ";
5775 char buf[256];
5776 sprintf(buf, "%.0f", (double)timestamp);
5777 cmd += fSql->QuoteString(buf);
5778 cmd += ", ";
5779 cmd += fSql->QuoteString("1");
5780 cmd += ");";
5781
5782 int status = fSql->Exec(table_name.c_str(), cmd.c_str());
5783 if (status == DB_SUCCESS)
5784 break;
5785
5786 status = CreateSqlTable(fSql, "_history_index", &have_transaction);
5787 status = CreateSqlColumn(fSql, "_history_index", "event_name", "varchar(256) character set binary not null", &have_transaction, fDebug);
5788 status = CreateSqlColumn(fSql, "_history_index", "table_name", "varchar(256)", &have_transaction, fDebug);
5789 status = CreateSqlColumn(fSql, "_history_index", "tag_name", "varchar(256) character set binary", &have_transaction, fDebug);
5790 status = CreateSqlColumn(fSql, "_history_index", "tag_type", "varchar(256)", &have_transaction, fDebug);
5791 status = CreateSqlColumn(fSql, "_history_index", "column_name", "varchar(256)", &have_transaction, fDebug);
5792 status = CreateSqlColumn(fSql, "_history_index", "column_type", "varchar(256)", &have_transaction, fDebug);
5793 status = CreateSqlColumn(fSql, "_history_index", "itimestamp", "integer not null", &have_transaction, fDebug);
5794 status = CreateSqlColumn(fSql, "_history_index", "active", "boolean", &have_transaction, fDebug);
5795 }
5796
5797 status = fSql->CommitTransaction(table_name.c_str());
5798
5799 if (status != DB_SUCCESS) {
5800 return HS_FILE_ERROR;
5801 }
5802
5803 return ReadMysqlTableNames(fSql, sv, xtable_name.c_str(), fDebug, event_name, xtable_name.c_str());
5804 }
5805
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);
5807
5808 return HS_FILE_ERROR;
5809}
virtual int RollbackTransaction(const char *table_name)=0
virtual int CommitTransaction(const char *table_name)=0
virtual int Exec(const char *sql)=0
virtual std::string QuoteString(const char *s)=0
virtual int OpenTransaction(const char *table_name)=0
#define DB_KEY_EXIST
Definition midas.h:641
#define DB_SUCCESS
Definition midas.h:631
#define HS_SUCCESS
Definition midas.h:727
#define HS_FILE_ERROR
Definition midas.h:728
#define MERROR
Definition midas.h:559
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
Definition midas.cxx:915
static std::string MidasNameToSqlName(const char *s)
static int CreateSqlColumn(SqlBase *sql, const char *table_name, const char *column_name, const char *column_type, bool *have_transaction, int debug)
static std::string TimeToString(time_t t)
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 int CreateSqlTable(SqlBase *sql, const char *table_name, bool *have_transaction, bool set_default_timestamp=false)
INT i
Definition mdump.cxx:32
INT j
Definition odbhist.cxx:40
DWORD status
Definition odbhist.cxx:39
Here is the call graph for this function:

◆ read_column_names()

int MysqlHistory::read_column_names ( HsSchemaVector sv,
const char table_name,
const char event_name 
)
virtual

Implements SqlHistoryBase.

Definition at line 5506 of file history_schema.cxx.

5507{
5508 if (fDebug)
5509 printf("MysqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5510
5511 // for all schema for table_name, prepopulate is with column names
5512
5513 std::vector<std::string> columns;
5514 fSql->ListColumns(table_name, &columns);
5515
5516 // first, populate column names
5517
5518 for (unsigned i=0; i<sv->size(); i++) {
5519 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5520
5521 if (s->table_name != table_name)
5522 continue;
5523
5524 // schema should be empty at this point
5525 //assert(s->variables.size() == 0);
5526
5527 for (unsigned j=0; j<columns.size(); j+=2) {
5528 const char* cn = columns[j+0].c_str();
5529 const char* ct = columns[j+1].c_str();
5530
5531 if (strcmp(cn, "_t_time") == 0)
5532 continue;
5533 if (strcmp(cn, "_i_time") == 0)
5534 continue;
5535
5536 bool found = false;
5537
5538 for (unsigned k=0; k<s->column_names.size(); k++) {
5539 if (s->column_names[k] == cn) {
5540 found = true;
5541 break;
5542 }
5543 }
5544
5545 //printf("column [%s] sql type [%s]\n", cn.c_str(), ct);
5546
5547 if (!found) {
5549 se.tag_name = cn;
5550 se.tag_type = "";
5551 se.name = cn;
5552 se.type = 0;
5553 se.n_data = 1;
5554 se.n_bytes = 0;
5555 s->variables.push_back(se);
5556 s->column_names.push_back(cn);
5557 s->column_types.push_back(ct);
5558 s->offsets.push_back(-1);
5559 }
5560 }
5561 }
5562
5563 // then read column name information
5564
5565 std::string cmd;
5566 cmd = "SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
5567 cmd += fSql->QuoteString(event_name);
5568 cmd += ";";
5569
5570 int status = fSql->Prepare(table_name, cmd.c_str());
5571
5572 if (status != DB_SUCCESS) {
5573 return status;
5574 }
5575
5576 while (1) {
5577 status = fSql->Step();
5578
5579 if (status != DB_SUCCESS)
5580 break;
5581
5582 const char* col_name = fSql->GetText(0);
5583 const char* col_type = fSql->GetText(1);
5584 const char* tag_name = fSql->GetText(2);
5585 const char* tag_type = fSql->GetText(3);
5587 const char* active = fSql->GetText(5);
5588 int iactive = atoi(active);
5589
5590 //printf("read table [%s] column [%s] type [%s] tag name [%s] type [%s] time %s active [%s] %d\n", table_name, col_name, col_type, tag_name, tag_type, TimeToString(schema_time).c_str(), active, iactive);
5591
5592 if (!col_name)
5593 continue;
5594 if (!tag_name)
5595 continue;
5596 if (strlen(col_name) < 1)
5597 continue;
5598
5599 // make sure a schema exists at this time point
5600 NewSqlSchema(sv, table_name, schema_time);
5601
5602 // add this information to all schema
5603
5604 for (unsigned i=0; i<sv->size(); i++) {
5605 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5606 if (s->table_name != table_name)
5607 continue;
5608 if (s->time_from < schema_time)
5609 continue;
5610
5611 int tid = rpc_name_tid(tag_type);
5612 int tid_size = rpc_tid_size(tid);
5613
5614 for (unsigned j=0; j<s->column_names.size(); j++) {
5615 if (col_name != s->column_names[j])
5616 continue;
5617
5618 s->variables[j].tag_name = tag_name;
5619 s->variables[j].tag_type = tag_type;
5620 if (!iactive) {
5621 s->variables[j].name = "";
5622 s->variables[j].inactive = true;
5623 } else {
5624 s->variables[j].name = tag_name;
5625 s->variables[j].inactive = false;
5626 }
5627 s->variables[j].type = tid;
5628 s->variables[j].n_data = 1;
5629 s->variables[j].n_bytes = tid_size;
5630
5631 // doctor column names in case MySQL returns different type
5632 // from the type used to create the column, but the types
5633 // are actually the same. K.O.
5635 }
5636 }
5637 }
5638
5639 status = fSql->Finalize();
5640
5641 return HS_SUCCESS;
5642}
virtual int Finalize()=0
virtual int ListColumns(const char *table, std::vector< std::string > *plist)=0
virtual int Prepare(const char *table_name, const char *sql)=0
virtual time_t GetTime(int column)=0
virtual int Step()=0
virtual const char * GetText(int column)=0
int rpc_name_tid(const char *name)
Definition midas.cxx:11778
INT rpc_tid_size(INT id)
Definition midas.cxx:11757
static const int tid_size[]
void DoctorSqlColumnType(std::string *col_type, const char *index_type)
static HsSqlSchema * NewSqlSchema(HsSchemaVector *sv, const char *table_name, time_t t)
INT k
Definition odbhist.cxx:40
std::string tag_name
std::vector< int > offsets
std::vector< HsSchemaEntry > variables
std::string table_name
std::vector< std::string > column_types
std::vector< std::string > column_names
Here is the call graph for this function:

◆ read_table_and_event_names()

int MysqlHistory::read_table_and_event_names ( HsSchemaVector sv)
virtual

Implements SqlHistoryBase.

Definition at line 5665 of file history_schema.cxx.

5666{
5667 int status;
5668
5669 if (fDebug)
5670 printf("MysqlHistory::read_table_and_event_names!\n");
5671
5672 // loop over all tables
5673
5674 std::vector<std::string> tables;
5676 if (status != DB_SUCCESS)
5677 return status;
5678
5679 for (unsigned i=0; i<tables.size(); i++) {
5680 const char* table_name = tables[i].c_str();
5681
5682 const char* s;
5683 s = strstr(table_name, "_history_index");
5684 if (s == table_name)
5685 continue;
5686
5687 if (1) {
5688 // seed schema with table names
5689 HsSqlSchema* s = new HsSqlSchema;
5690 s->sql = fSql;
5691 s->event_name = table_name;
5692 s->time_from = 0;
5693 s->time_to = 0;
5694 s->table_name = table_name;
5695 sv->add(s);
5696 }
5697 }
5698
5699 if (0) {
5700 // print accumulated schema
5701 printf("read_table_and_event_names:\n");
5702 sv->print(false);
5703 }
5704
5706
5707 return HS_SUCCESS;
5708}
virtual int ListTables(std::vector< std::string > *plist)=0
std::string event_name
Here is the call graph for this function:

◆ update_column()

int MysqlHistory::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 
)
virtual

Implements SqlHistoryBase.

Definition at line 5811 of file history_schema.cxx.

5812{
5813 if (fDebug)
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());
5815
5816 std::string cmd;
5817 cmd += "INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
5818 cmd += fSql->QuoteString(event_name);
5819 cmd += ", ";
5820 cmd += fSql->QuoteString(table_name);
5821 cmd += ", ";
5822 cmd += fSql->QuoteString(tag_name);
5823 cmd += ", ";
5824 cmd += fSql->QuoteString(tag_type);
5825 cmd += ", ";
5826 cmd += fSql->QuoteString(column_name);
5827 cmd += ", ";
5828 cmd += fSql->QuoteString(column_type);
5829 cmd += ", ";
5830 char buf[256];
5831 sprintf(buf, "%.0f", (double)timestamp);
5832 cmd += fSql->QuoteString(buf);
5833 cmd += ", ";
5834 if (active)
5835 cmd += fSql->QuoteString("1");
5836 else
5837 cmd += fSql->QuoteString("0");
5838 cmd += ");";
5839
5840 int status = fSql->Exec(table_name, cmd.c_str());
5841 if (status != DB_SUCCESS)
5842 return HS_FILE_ERROR;
5843
5844 return HS_SUCCESS;
5845}
Here is the call graph for this function:

The documentation for this class was generated from the following file: