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 5476 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ MysqlHistory()

MysqlHistory::MysqlHistory ( )
inline

Definition at line 5479 of file history_schema.cxx.

5479 { // ctor
5480#ifdef HAVE_MYSQL
5481 fSql = new Mysql();
5482#endif
5483 }
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 5778 of file history_schema.cxx.

5779{
5780 if (fDebug)
5781 printf("MysqlHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5782
5783 int status;
5784 std::string table_name = MidasNameToSqlName(event_name);
5785
5786 // MySQL table name length limit is 64 bytes
5787 if (table_name.length() > 40) {
5788 table_name.resize(40);
5789 table_name += "_T";
5790 }
5791
5792 time_t now = time(NULL);
5793
5794 int max_attempts = 10;
5795 for (int i=0; i<max_attempts; i++) {
5796 status = fSql->OpenTransaction(table_name.c_str());
5797 if (status != DB_SUCCESS) {
5798 return HS_FILE_ERROR;
5799 }
5800
5801 bool have_transaction = true;
5802
5803 std::string xtable_name = table_name;
5804
5805 if (i>0) {
5806 xtable_name += "_";
5808 if (i>1) {
5809 xtable_name += "_";
5810 char buf[256];
5811 sprintf(buf, "%d", i);
5812 xtable_name += buf;
5813 }
5814 }
5815
5817
5818 //printf("event [%s] create table [%s] status %d\n", event_name, xtable_name.c_str(), status);
5819
5820 if (status == DB_KEY_EXIST) {
5821 // already exists, try with different name!
5822 fSql->RollbackTransaction(table_name.c_str());
5823 continue;
5824 }
5825
5826 if (status != HS_SUCCESS) {
5827 // MYSQL cannot roll back "create table", if we cannot create SQL tables, nothing will work. Give up now.
5828 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());
5829 abort();
5830
5831 // fatal error, give up!
5832 fSql->RollbackTransaction(table_name.c_str());
5833 break;
5834 }
5835
5836 for (int j=0; j<2; j++) {
5837 std::string cmd;
5838 cmd += "INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
5839 cmd += fSql->QuoteString(event_name);
5840 cmd += ", ";
5841 cmd += fSql->QuoteString(xtable_name.c_str());
5842 cmd += ", ";
5843 char buf[256];
5844 sprintf(buf, "%.0f", (double)timestamp);
5845 cmd += fSql->QuoteString(buf);
5846 cmd += ", ";
5847 cmd += fSql->QuoteString("1");
5848 cmd += ");";
5849
5850 int status = fSql->Exec(table_name.c_str(), cmd.c_str());
5851 if (status == DB_SUCCESS)
5852 break;
5853
5854 status = CreateSqlTable(fSql, "_history_index", &have_transaction);
5855 status = CreateSqlColumn(fSql, "_history_index", "event_name", "varchar(256) character set binary not null", &have_transaction, fDebug);
5856 status = CreateSqlColumn(fSql, "_history_index", "table_name", "varchar(256)", &have_transaction, fDebug);
5857 status = CreateSqlColumn(fSql, "_history_index", "tag_name", "varchar(256) character set binary", &have_transaction, fDebug);
5858 status = CreateSqlColumn(fSql, "_history_index", "tag_type", "varchar(256)", &have_transaction, fDebug);
5859 status = CreateSqlColumn(fSql, "_history_index", "column_name", "varchar(256)", &have_transaction, fDebug);
5860 status = CreateSqlColumn(fSql, "_history_index", "column_type", "varchar(256)", &have_transaction, fDebug);
5861 status = CreateSqlColumn(fSql, "_history_index", "itimestamp", "integer not null", &have_transaction, fDebug);
5862 status = CreateSqlColumn(fSql, "_history_index", "active", "boolean", &have_transaction, fDebug);
5863 }
5864
5865 status = fSql->CommitTransaction(table_name.c_str());
5866
5867 if (status != DB_SUCCESS) {
5868 return HS_FILE_ERROR;
5869 }
5870
5871 return ReadMysqlTableNames(fSql, sv, xtable_name.c_str(), fDebug, event_name, xtable_name.c_str());
5872 }
5873
5874 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);
5875
5876 return HS_FILE_ERROR;
5877}
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 5573 of file history_schema.cxx.

5574{
5575 if (fDebug)
5576 printf("MysqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5577
5578 // for all schema for table_name, prepopulate is with column names
5579
5580 std::vector<std::string> columns;
5581 fSql->ListColumns(table_name, &columns);
5582
5583 // first, populate column names
5584
5585 for (unsigned i=0; i<sv->size(); i++) {
5586 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5587
5588 if (s->fTableName != table_name)
5589 continue;
5590
5591 // schema should be empty at this point
5592 //assert(s->fVariables.size() == 0);
5593
5594 for (unsigned j=0; j<columns.size(); j+=2) {
5595 const char* cn = columns[j+0].c_str();
5596 const char* ct = columns[j+1].c_str();
5597
5598 if (strcmp(cn, "_t_time") == 0)
5599 continue;
5600 if (strcmp(cn, "_i_time") == 0)
5601 continue;
5602
5603 bool found = false;
5604
5605 for (unsigned k=0; k<s->fColumnNames.size(); k++) {
5606 if (s->fColumnNames[k] == cn) {
5607 found = true;
5608 break;
5609 }
5610 }
5611
5612 //printf("column [%s] sql type [%s]\n", cn.c_str(), ct);
5613
5614 if (!found) {
5616 se.tag_name = cn;
5617 se.tag_type = "";
5618 se.name = cn;
5619 se.type = 0;
5620 se.n_data = 1;
5621 se.n_bytes = 0;
5622 s->fVariables.push_back(se);
5623 s->fColumnNames.push_back(cn);
5624 s->fColumnTypes.push_back(ct);
5625 s->fColumnInactive.push_back(false);
5626 s->fOffsets.push_back(-1);
5627 }
5628 }
5629 }
5630
5631 // then read column name information
5632
5633 std::string cmd;
5634 cmd = "SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
5635 cmd += fSql->QuoteString(event_name);
5636 cmd += ";";
5637
5638 int status = fSql->Prepare(table_name, cmd.c_str());
5639
5640 if (status != DB_SUCCESS) {
5641 return status;
5642 }
5643
5644 while (1) {
5645 status = fSql->Step();
5646
5647 if (status != DB_SUCCESS)
5648 break;
5649
5650 const char* col_name = fSql->GetText(0);
5651 const char* col_type = fSql->GetText(1);
5652 const char* tag_name = fSql->GetText(2);
5653 const char* tag_type = fSql->GetText(3);
5655 const char* active = fSql->GetText(5);
5656 int iactive = atoi(active);
5657
5658 //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);
5659
5660 if (!col_name)
5661 continue;
5662 if (!tag_name)
5663 continue;
5664 if (strlen(col_name) < 1)
5665 continue;
5666
5667 // make sure a schema exists at this time point
5668 NewSqlSchema(sv, table_name, schema_time);
5669
5670 // add this information to all schema
5671
5672 for (unsigned i=0; i<sv->size(); i++) {
5673 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5674 if (s->fTableName != table_name)
5675 continue;
5676 if (s->fTimeFrom < schema_time)
5677 continue;
5678
5679 int tid = rpc_name_tid(tag_type);
5680 int tid_size = rpc_tid_size(tid);
5681
5682 for (unsigned j=0; j<s->fColumnNames.size(); j++) {
5683 if (col_name != s->fColumnNames[j])
5684 continue;
5685
5686 s->fVariables[j].tag_name = tag_name;
5687 s->fVariables[j].tag_type = tag_type;
5688 if (!iactive) {
5689 s->fVariables[j].name = "";
5690 s->fColumnInactive[j] = true;
5691 } else {
5692 s->fVariables[j].name = tag_name;
5693 s->fColumnInactive[j] = false;
5694 }
5695 s->fVariables[j].type = tid;
5696 s->fVariables[j].n_data = 1;
5697 s->fVariables[j].n_bytes = tid_size;
5698
5699 // doctor column names in case MySQL returns different type
5700 // from the type used to create the column, but the types
5701 // are actually the same. K.O.
5703 }
5704 }
5705 }
5706
5707 status = fSql->Finalize();
5708
5709 return HS_SUCCESS;
5710}
std::vector< int > fOffsets
std::vector< HsSchemaEntry > fVariables
std::vector< std::string > fColumnNames
std::vector< std::string > fColumnTypes
std::vector< bool > fColumnInactive
std::string fTableName
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
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 5733 of file history_schema.cxx.

5734{
5735 int status;
5736
5737 if (fDebug)
5738 printf("MysqlHistory::read_table_and_event_names!\n");
5739
5740 // loop over all tables
5741
5742 std::vector<std::string> tables;
5744 if (status != DB_SUCCESS)
5745 return status;
5746
5747 for (unsigned i=0; i<tables.size(); i++) {
5748 const char* table_name = tables[i].c_str();
5749
5750 const char* s;
5751 s = strstr(table_name, "_history_index");
5752 if (s == table_name)
5753 continue;
5754
5755 if (1) {
5756 // seed schema with table names
5757 HsSqlSchema* s = new HsSqlSchema;
5758 s->fSql = fSql;
5759 s->fEventName = table_name;
5760 s->fTimeFrom = 0;
5761 s->fTimeTo = 0;
5762 s->fTableName = table_name;
5763 sv->add(s);
5764 }
5765 }
5766
5767 if (0) {
5768 // print accumulated schema
5769 printf("read_table_and_event_names:\n");
5770 sv->print(false);
5771 }
5772
5774
5775 return HS_SUCCESS;
5776}
std::string fEventName
virtual int ListTables(std::vector< std::string > *plist)=0
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 5879 of file history_schema.cxx.

5880{
5881 if (fDebug)
5882 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());
5883
5884 std::string cmd;
5885 cmd += "INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
5886 cmd += fSql->QuoteString(event_name);
5887 cmd += ", ";
5888 cmd += fSql->QuoteString(table_name);
5889 cmd += ", ";
5890 cmd += fSql->QuoteString(tag_name);
5891 cmd += ", ";
5892 cmd += fSql->QuoteString(tag_type);
5893 cmd += ", ";
5894 cmd += fSql->QuoteString(column_name);
5895 cmd += ", ";
5896 cmd += fSql->QuoteString(column_type);
5897 cmd += ", ";
5898 char buf[256];
5899 sprintf(buf, "%.0f", (double)timestamp);
5900 cmd += fSql->QuoteString(buf);
5901 cmd += ", ";
5902 if (active)
5903 cmd += fSql->QuoteString("1");
5904 else
5905 cmd += fSql->QuoteString("0");
5906 cmd += ");";
5907
5908 int status = fSql->Exec(table_name, cmd.c_str());
5909 if (status != DB_SUCCESS)
5910 return HS_FILE_ERROR;
5911
5912 return HS_SUCCESS;
5913}
Here is the call graph for this function:

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