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

Constructor & Destructor Documentation

◆ MysqlHistory()

MysqlHistory::MysqlHistory ( )
inline

Definition at line 5483 of file history_schema.cxx.

5483 { // ctor
5484#ifdef HAVE_MYSQL
5485 fSql = new Mysql();
5486#endif
5487 }
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 5782 of file history_schema.cxx.

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

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

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

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

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