]> pilppa.org Git - libplp.git/blobdiff - src/StoreCache.cc
Data read optimizations
[libplp.git] / src / StoreCache.cc
index 0d62d777f5ba586b89bb1e0c0466d33800c9aabe..d95c9fb0fdf72cc74610df68cc1793825f7b7427 100644 (file)
@@ -154,14 +154,24 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                                        res_data        = NULL;
                                        cnt             = 0;
                                        val_cnt         = 0;
+                                       dr              = NULL;
                                        while(cur_date->before(max_date)) {
                                                if (period_type_param == PERIOD_YEARLY) {
                                                        store   = new StoreCache(device_id, cur_date);
                                                        dr      = store->get_mean(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_mean(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_mean(PERIOD_DAILY);
+                                                       }
+                                                       if (dr == NULL) {
+                                                               // data not found for that day, try to find next date containing data
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                       cur_date,
+                                                                                       max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -204,12 +214,14 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                        case PERIOD_DAILY:
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
-                       case PERIOD_SECONDLY: {
+                       case PERIOD_SECONDLY:
+                               if (StoreDay::exist(device_id, date, false)) {
                                        StoreDay        *store;
 
                                        store   = new StoreDay(device_id, date);
                                        ret_val = store->get_mean(period_type_param);
-                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                       if ((ret_val != NULL) &&
+                                           (period_type_param != PERIOD_MINUTELY) &&
                                            (period_type_param != PERIOD_SECONDLY)) {
                                                // no need cache second or minute data
                                                save(fname, ret_val, 4);
@@ -272,8 +284,17 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                                                        dr      = store->get_sum(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_sum(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_sum(PERIOD_DAILY);
+                                                       }
+                                                       if (dr == NULL) {
+                                                               // data not found for that day, try to find next date containing data
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                               cur_date,
+                                                                                               max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -314,14 +335,17 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_sum(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_sum(period_type_param);
+                                       if ((ret_val != NULL) &&
+                                           (period_type_param != PERIOD_MINUTELY) &&
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -388,37 +412,47 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                                        Data    *last_data;
                                        Data    *cur_data;
                                        Date    *cur_date;
-                                       Date    *limit_date;
+                                       Date    *max_date;
                                        int     ii;
                                        int     cnt;
 
                                        cur_date        = date->clone();
-                                       limit_date      = date->clone();
-                                       limit_date->next_month();
+                                       max_date        = date->clone();
+                                       max_date->next_month();
                                        first_data      = NULL;
                                        last_data       = NULL;
-                                       while(cur_date->before(limit_date)) {
-                                               store   = new StoreDay(device_id, cur_date);
-                                               if (first_data == NULL) {
-                                                       cur_data        = store->get_oldest_data();
+                                       while(cur_date->before(max_date)) {
+                                               if (StoreDay::exist(device_id,
+                                                               cur_date,
+                                                               false)) {
+                                                       store   = new StoreDay(device_id, cur_date);
+                                                       if (first_data == NULL) {
+                                                               cur_data        = store->get_oldest_data();
+                                                               if (cur_data != NULL) {
+                                                                       first_data      = cur_data->clone();
+                                                                       last_data       = cur_data->clone();
+                                                                       delete(cur_data);
+                                                               }
+                                                       }
+                                                       cur_data        = store->get_latest_data();
                                                        if (cur_data != NULL) {
-                                                               first_data      = cur_data->clone();
-                                                               last_data       = cur_data->clone();
-                                                               delete(cur_data);
+                                                               if (last_data != NULL) {
+                                                                       delete(last_data);
+                                                               }
+                                                               last_data       = cur_data;
                                                        }
+                                                       delete(store);
+                                                       cur_date->next_day();
                                                }
-                                               cur_data        = store->get_latest_data();
-                                               if (cur_data != NULL) {
-                                                       if (last_data != NULL) {
-                                                               delete(last_data);
-                                                       }
-                                                       last_data       = cur_data;
+                                               else {
+                                                       // data not found for that day, try to find next date containing data
+                                                       StoreDay::get_next_date_with_data(device_id,
+                                                                               cur_date,
+                                                                               max_date);
                                                }
-                                               delete(store);
-                                               cur_date->next_day();
                                        }
                                        delete(cur_date);
-                                       delete(limit_date);
+                                       delete(max_date);
                                        if (first_data != NULL) {
                                                if (last_data == NULL) {
                                                        last_data       = first_data->clone();
@@ -441,14 +475,16 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_delta(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_delta(period_type_param);
+                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -505,8 +541,16 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                                                        dr      = store->get_max(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_max(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_max(PERIOD_DAILY);
+                                                       }
+                                                       else {
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                               cur_date,
+                                                                                               max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -557,14 +601,16 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_max(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_max(period_type_param);
+                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -621,8 +667,16 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                                                        dr      = store->get_min(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_min(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_min(PERIOD_DAILY);
+                                                       }
+                                                       else {
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                               cur_date,
+                                                                                               max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -673,14 +727,16 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_min(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_min(period_type_param);
+                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -721,7 +777,9 @@ void StoreCache::save(std::string fname_param, plp::DataRange *datarange_param,
        }
 }
 
-Data *StoreCache::get_oldest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+Data *StoreCache::get_oldest_data(Date *date_param,
+                       string device_id_param,
+                       EnumSummaryPeriod period_type_param) {
        int             size;
        unsigned int    ii;
        string          year_dr;
@@ -789,7 +847,9 @@ Data *StoreCache::get_oldest_data(Date *date_param, string device_id_param, Enum
        return ret_val;
 }
 
-Data *StoreCache::get_latest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+Data *StoreCache::get_latest_data(Date *date_param,
+                       string device_id_param,
+                       EnumSummaryPeriod period_type_param) {
        int             ii;
        string          mon_dr;
        vector<string>  mon_vcr;