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)
 
HsSchemamaybe_reopen (const char *event_name, time_t timestamp, HsSchema *s)
 
- 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 = 0
 
std::string fConnectString
 
HsSchemaVector fWriterSchema
 
std::vector< HsSchema * > fWriterEvents
 
HsSchemaVector fReaderSchema
 

Detailed Description

Definition at line 5571 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ MysqlHistory()

MysqlHistory::MysqlHistory ( )
inline

Definition at line 5574 of file history_schema.cxx.

5574 { // ctor
5575#ifdef HAVE_MYSQL
5576 fSql = new Mysql();
5577#endif
5578 }
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 5873 of file history_schema.cxx.

5874{
5875 if (fDebug)
5876 printf("MysqlHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5877
5878 int status;
5879 std::string table_name = MidasNameToSqlName(event_name);
5880
5881 // MySQL table name length limit is 64 bytes
5882 if (table_name.length() > 40) {
5883 table_name.resize(40);
5884 table_name += "_T";
5885 }
5886
5887 time_t now = time(NULL);
5888
5889 int max_attempts = 10;
5890 for (int i=0; i<max_attempts; i++) {
5891 status = fSql->OpenTransaction(table_name.c_str());
5892 if (status != DB_SUCCESS) {
5893 return HS_FILE_ERROR;
5894 }
5895
5896 bool have_transaction = true;
5897
5898 std::string xtable_name = table_name;
5899
5900 if (i>0) {
5901 xtable_name += "_";
5903 if (i>1) {
5904 xtable_name += "_";
5905 char buf[256];
5906 sprintf(buf, "%d", i);
5907 xtable_name += buf;
5908 }
5909 }
5910
5912
5913 //printf("event [%s] create table [%s] status %d\n", event_name, xtable_name.c_str(), status);
5914
5915 if (status == DB_KEY_EXIST) {
5916 // already exists, try with different name!
5917 fSql->RollbackTransaction(table_name.c_str());
5918 continue;
5919 }
5920
5921 if (status != HS_SUCCESS) {
5922 // MYSQL cannot roll back "create table", if we cannot create SQL tables, nothing will work. Give up now.
5923 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());
5924 abort();
5925
5926 // fatal error, give up!
5927 fSql->RollbackTransaction(table_name.c_str());
5928 break;
5929 }
5930
5931 for (int j=0; j<2; j++) {
5932 std::string cmd;
5933 cmd += "INSERT INTO _history_index (event_name, table_name, itimestamp, active) VALUES (";
5934 cmd += fSql->QuoteString(event_name);
5935 cmd += ", ";
5936 cmd += fSql->QuoteString(xtable_name.c_str());
5937 cmd += ", ";
5938 char buf[256];
5939 sprintf(buf, "%.0f", (double)timestamp);
5940 cmd += fSql->QuoteString(buf);
5941 cmd += ", ";
5942 cmd += fSql->QuoteString("1");
5943 cmd += ");";
5944
5945 int status = fSql->Exec(table_name.c_str(), cmd.c_str());
5946 if (status == DB_SUCCESS)
5947 break;
5948
5949 status = CreateSqlTable(fSql, "_history_index", &have_transaction);
5950 status = CreateSqlColumn(fSql, "_history_index", "event_name", "varchar(256) character set binary not null", &have_transaction, fDebug);
5951 status = CreateSqlColumn(fSql, "_history_index", "table_name", "varchar(256)", &have_transaction, fDebug);
5952 status = CreateSqlColumn(fSql, "_history_index", "tag_name", "varchar(256) character set binary", &have_transaction, fDebug);
5953 status = CreateSqlColumn(fSql, "_history_index", "tag_type", "varchar(256)", &have_transaction, fDebug);
5954 status = CreateSqlColumn(fSql, "_history_index", "column_name", "varchar(256)", &have_transaction, fDebug);
5955 status = CreateSqlColumn(fSql, "_history_index", "column_type", "varchar(256)", &have_transaction, fDebug);
5956 status = CreateSqlColumn(fSql, "_history_index", "itimestamp", "integer not null", &have_transaction, fDebug);
5957 status = CreateSqlColumn(fSql, "_history_index", "active", "boolean", &have_transaction, fDebug);
5958 }
5959
5960 status = fSql->CommitTransaction(table_name.c_str());
5961
5962 if (status != DB_SUCCESS) {
5963 return HS_FILE_ERROR;
5964 }
5965
5966 return ReadMysqlTableNames(fSql, sv, xtable_name.c_str(), fDebug, event_name, xtable_name.c_str());
5967 }
5968
5969 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);
5970
5971 return HS_FILE_ERROR;
5972}
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 5668 of file history_schema.cxx.

5669{
5670 if (fDebug)
5671 printf("MysqlHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5672
5673 // for all schema for table_name, prepopulate is with column names
5674
5675 std::vector<std::string> columns;
5676 fSql->ListColumns(table_name, &columns);
5677
5678 // first, populate column names
5679
5680 for (size_t i=0; i<sv->size(); i++) {
5681 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5682
5683 if (s->fTableName != table_name)
5684 continue;
5685
5686 // schema should be empty at this point
5687 //assert(s->fVariables.size() == 0);
5688
5689 for (size_t j=0; j<columns.size(); j+=2) {
5690 const char* cn = columns[j+0].c_str();
5691 const char* ct = columns[j+1].c_str();
5692
5693 if (strcmp(cn, "_t_time") == 0)
5694 continue;
5695 if (strcmp(cn, "_i_time") == 0)
5696 continue;
5697
5698 bool found = false;
5699
5700 for (size_t k=0; k<s->fColumnNames.size(); k++) {
5701 if (s->fColumnNames[k] == cn) {
5702 found = true;
5703 break;
5704 }
5705 }
5706
5707 //printf("column [%s] sql type [%s]\n", cn.c_str(), ct);
5708
5709 if (!found) {
5711 se.tag_name = cn;
5712 se.tag_type = "";
5713 se.name = cn;
5714 se.type = 0;
5715 se.n_data = 1;
5716 se.n_bytes = 0;
5717 s->fVariables.push_back(se);
5718 s->fColumnNames.push_back(cn);
5719 s->fColumnTypes.push_back(ct);
5720 s->fColumnInactive.push_back(false);
5721 s->fOffsets.push_back(-1);
5722 }
5723 }
5724 }
5725
5726 // then read column name information
5727
5728 std::string cmd;
5729 cmd = "SELECT column_name, column_type, tag_name, tag_type, itimestamp, active FROM _history_index WHERE event_name=";
5730 cmd += fSql->QuoteString(event_name);
5731 cmd += ";";
5732
5733 int status = fSql->Prepare(table_name, cmd.c_str());
5734
5735 if (status != DB_SUCCESS) {
5736 return status;
5737 }
5738
5739 while (1) {
5740 status = fSql->Step();
5741
5742 if (status != DB_SUCCESS)
5743 break;
5744
5745 const char* col_name = fSql->GetText(0);
5746 const char* col_type = fSql->GetText(1);
5747 const char* tag_name = fSql->GetText(2);
5748 const char* tag_type = fSql->GetText(3);
5750 const char* active = fSql->GetText(5);
5751 int iactive = atoi(active);
5752
5753 //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);
5754
5755 if (!col_name)
5756 continue;
5757 if (!tag_name)
5758 continue;
5759 if (strlen(col_name) < 1)
5760 continue;
5761
5762 // make sure a schema exists at this time point
5763 NewSqlSchema(sv, table_name, schema_time);
5764
5765 // add this information to all schema
5766
5767 for (size_t i=0; i<sv->size(); i++) {
5768 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5769 if (s->fTableName != table_name)
5770 continue;
5771 if (s->fTimeFrom < schema_time)
5772 continue;
5773
5774 int tid = rpc_name_tid(tag_type);
5775 int tid_size = rpc_tid_size(tid);
5776
5777 for (size_t j=0; j<s->fColumnNames.size(); j++) {
5778 if (col_name != s->fColumnNames[j])
5779 continue;
5780
5781 s->fVariables[j].tag_name = tag_name;
5782 s->fVariables[j].tag_type = tag_type;
5783 if (!iactive) {
5784 s->fVariables[j].name = "";
5785 s->fColumnInactive[j] = true;
5786 } else {
5787 s->fVariables[j].name = tag_name;
5788 s->fColumnInactive[j] = false;
5789 }
5790 s->fVariables[j].type = tid;
5791 s->fVariables[j].n_data = 1;
5792 s->fVariables[j].n_bytes = tid_size;
5793
5794 // doctor column names in case MySQL returns different type
5795 // from the type used to create the column, but the types
5796 // are actually the same. K.O.
5798 }
5799 }
5800 }
5801
5802 status = fSql->Finalize();
5803
5804 return HS_SUCCESS;
5805}
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 5828 of file history_schema.cxx.

5829{
5830 int status;
5831
5832 if (fDebug)
5833 printf("MysqlHistory::read_table_and_event_names!\n");
5834
5835 // loop over all tables
5836
5837 std::vector<std::string> tables;
5839 if (status != DB_SUCCESS)
5840 return status;
5841
5842 for (size_t i=0; i<tables.size(); i++) {
5843 const char* table_name = tables[i].c_str();
5844
5845 const char* s;
5846 s = strstr(table_name, "_history_index");
5847 if (s == table_name)
5848 continue;
5849
5850 if (1) {
5851 // seed schema with table names
5852 HsSqlSchema* s = new HsSqlSchema;
5853 s->fSql = fSql;
5854 s->fEventName = table_name;
5855 s->fTimeFrom = 0;
5856 s->fTimeTo = 0;
5857 s->fTableName = table_name;
5858 sv->add(s);
5859 }
5860 }
5861
5862 if (0) {
5863 // print accumulated schema
5864 printf("read_table_and_event_names:\n");
5865 sv->print(false);
5866 }
5867
5869
5870 return HS_SUCCESS;
5871}
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 5974 of file history_schema.cxx.

5975{
5976 if (fDebug)
5977 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());
5978
5979 std::string cmd;
5980 cmd += "INSERT INTO _history_index (event_name, table_name, tag_name, tag_type, column_name, column_type, itimestamp, active) VALUES (";
5981 cmd += fSql->QuoteString(event_name);
5982 cmd += ", ";
5983 cmd += fSql->QuoteString(table_name);
5984 cmd += ", ";
5985 cmd += fSql->QuoteString(tag_name);
5986 cmd += ", ";
5987 cmd += fSql->QuoteString(tag_type);
5988 cmd += ", ";
5989 cmd += fSql->QuoteString(column_name);
5990 cmd += ", ";
5991 cmd += fSql->QuoteString(column_type);
5992 cmd += ", ";
5993 char buf[256];
5994 sprintf(buf, "%.0f", (double)timestamp);
5995 cmd += fSql->QuoteString(buf);
5996 cmd += ", ";
5997 if (active)
5998 cmd += fSql->QuoteString("1");
5999 else
6000 cmd += fSql->QuoteString("0");
6001 cmd += ");";
6002
6003 int status = fSql->Exec(table_name, cmd.c_str());
6004 if (status != DB_SUCCESS)
6005 return HS_FILE_ERROR;
6006
6007 return HS_SUCCESS;
6008}
Here is the call graph for this function:

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