Commit 5fc045ca authored by Claes Sjofors's avatar Claes Sjofors

Sev mysql, policy when selecting values with deadband changed

parent 6618e0c6
......@@ -1482,6 +1482,7 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo
char orderby_part[80];
char jumpstr[40];
char where_part[200];
int rows;
if ( starttime && starttime->tv_sec == 0 && starttime->tv_nsec == 0)
starttime = 0;
......@@ -1620,7 +1621,7 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo
strcpy( column_part, "time,value");
if ( options & pwr_mSevOptionsMask_UseDeadBand)
strcpy( jumpstr, "or jump = 1");
strcpy( jumpstr, ""); // "or jump = 1" removed
else
strcpy( jumpstr, "");
......@@ -1634,105 +1635,120 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo
strcpy( orderby_part, "time");
}
// 'where' part
if ( options & pwr_mSevOptionsMask_ReadOptimized) {
if ( starttime && endtime) {
if ( div == 1) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr);
for ( int k = 0; k < 2; k++) {
// 'where' part
if ( options & pwr_mSevOptionsMask_ReadOptimized) {
if ( starttime && endtime) {
if ( div == 1) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr);
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld and time <= %ld",
div, jumpstr, (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s' and time <= '%s'",
div, jumpstr, starttimstr, endtimstr);
}
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld and time <= %ld",
div, jumpstr, (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s' and time <= '%s'",
div, jumpstr, starttimstr, endtimstr);
else if ( starttime) {
if ( div == 1) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec);
else
sprintf( where_part, "where time >= '%s'", starttimstr);
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld", div, jumpstr, (long int)starttime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s'", div, jumpstr, starttimstr);
}
}
}
else if ( starttime) {
if ( div == 1) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec);
else
sprintf( where_part, "where time >= '%s'", starttimstr);
else if ( endtime) {
if ( div == 1) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec);
else
sprintf( where_part, "where time <= '%s'", endtimstr);
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time <= %ld", div, jumpstr, (long int)endtime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time <= '%s'", div, jumpstr, endtimstr);
}
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld", div, jumpstr, (long int)starttime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s'", div, jumpstr, starttimstr);
if ( div == 1)
strcpy( where_part, "");
else
sprintf( where_part, "where id %% %d = 0 %s", div, jumpstr);
}
}
else if ( endtime) {
if ( div == 1) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec);
else
sprintf( where_part, "where time <= '%s'", endtimstr);
else {
// Not read optimized
if ( starttime && endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr);
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time <= %ld", div, jumpstr, (long int)endtime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time <= '%s'", div, jumpstr, endtimstr);
else if ( starttime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec);
else
sprintf( where_part, "where time >= '%s'", starttimstr);
}
else if ( endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec);
else
sprintf( where_part, "where time <= '%s'", endtimstr);
}
}
else {
if ( div == 1)
strcpy( where_part, "");
else
sprintf( where_part, "where id %% %d = 0 %s", div, jumpstr);
}
}
else {
// Not read optimized
if ( starttime && endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr);
strcpy( where_part, "");
}
sprintf( query, "select %s from %s %s order by %s limit %d",
column_part, item.tablename, where_part, orderby_part, maxsize*2);
rc = mysql_query( m_env->con(), query);
if (rc) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "Get Values: %s\n", mysql_error(m_env->con()));
*sts = SEV__DBERROR;
return 0;
}
else if ( starttime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec);
else
sprintf( where_part, "where time >= '%s'", starttimstr);
result = mysql_store_result( m_env->con());
if ( !result) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "GetValues Result Error\n");
*sts = SEV__DBERROR;
return 0;
}
else if ( endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec);
else
sprintf( where_part, "where time <= '%s'", endtimstr);
rows = mysql_num_rows( result);
if ( k == 0 &&
options & pwr_mSevOptionsMask_UseDeadBand &&
rows < maxsize / 3 &&
rows != 0 &&
div != 1) {
// Try another read with smaller div
printf( "Retry: %d old dev: %d, new div %d\n", rows, div, div * rows / maxsize + 1);
if ( div == div * rows / maxsize + 1)
break;
div = div * rows / maxsize + 1;
mysql_free_result( result);
}
else
strcpy( where_part, "");
}
sprintf( query, "select %s from %s %s order by %s",
column_part, item.tablename, where_part, orderby_part);
rc = mysql_query( m_env->con(), query);
if (rc) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "Get Values: %s\n", mysql_error(m_env->con()));
*sts = SEV__DBERROR;
return 0;
}
result = mysql_store_result( m_env->con());
if ( !result) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "GetValues Result Error\n");
*sts = SEV__DBERROR;
return 0;
else break;
}
int rows = mysql_num_rows( result);
int bufrows = rows;
if ( options & pwr_mSevOptionsMask_ReadOptimized) {
*tbuf = (pwr_tTime *) calloc( bufrows, sizeof(pwr_tTime));
......@@ -4062,10 +4078,10 @@ void sev_dbms::add_cache( int item_idx)
case pwr_eType_Int32:
if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandMeanValue)
m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Mean, m_items[item_idx].deadband,
m_items[item_idx].scantime);
m_items[item_idx].scantime/2);
else
m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Point, m_items[item_idx].deadband,
m_items[item_idx].scantime);
m_items[item_idx].scantime/2);
m_items[item_idx].cache->set_write_cb( write_db_cb, this, item_idx);
break;
case pwr_eType_Boolean:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment