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

Constructor & Destructor Documentation

◆ MysqlHistory()

MysqlHistory::MysqlHistory ( )
inline

Definition at line 5576 of file history_schema.cxx.

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

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

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

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

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

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