OmniSciDB  2e3a973ef4
RefreshTimeCalculator.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 
5 #include "ForeignTable.h"
7 
8 namespace foreign_storage {
16 inline int64_t get_interval_duration(const std::string& interval) {
17  int interval_count = std::stoi(interval.substr(0, interval.length() - 1));
18  auto interval_type = std::tolower(interval[interval.length() - 1]);
19  int64_t duration{0};
20  if (interval_type == 's') {
21  duration = interval_count;
22  } else if (interval_type == 'h') {
23  duration = interval_count * 60 * 60;
24  } else if (interval_type == 'd') {
25  duration = interval_count * 60 * 60 * 24;
26  } else {
27  UNREACHABLE();
28  }
29  return duration;
30 }
31 
32 // TODO: Support complete list of interval types
33 inline int64_t get_next_refresh_time(
34  const std::map<std::string, std::string, std::less<>>& foreign_table_options) {
35  int64_t current_time = std::chrono::duration_cast<std::chrono::seconds>(
36  std::chrono::system_clock::now().time_since_epoch())
37  .count();
38  auto start_date_entry = foreign_table_options.find(
40  CHECK(start_date_entry != foreign_table_options.end());
41  auto start_date_time = dateTimeParse<kTIMESTAMP>(start_date_entry->second, 0);
42 
43  // If start date time is current or in the future, then that is the next refresh time
44  if (start_date_time >= current_time) {
45  return start_date_time;
46  }
47  auto interval_entry =
48  foreign_table_options.find(foreign_storage::ForeignTable::REFRESH_INTERVAL_KEY);
49  if (interval_entry != foreign_table_options.end()) {
50  auto interval_duration = get_interval_duration(interval_entry->second);
51  auto num_intervals =
52  (current_time - start_date_time + interval_duration - 1) / interval_duration;
53  return start_date_time + (num_intervals * interval_duration);
54  } else {
55  // If this was a one time refresh, then there is no next refresh time
57  }
58 }
59 } // namespace foreign_storage
int64_t get_interval_duration(const std::string &interval)
int64_t dateTimeParse< kTIMESTAMP >(std::string_view str, unsigned const dim)
#define UNREACHABLE()
Definition: Logger.h:241
static constexpr const char * REFRESH_START_DATE_TIME_KEY
Definition: ForeignTable.h:30
int64_t get_next_refresh_time(const std::map< std::string, std::string, std::less<>> &foreign_table_options)
static constexpr const char * REFRESH_INTERVAL_KEY
Definition: ForeignTable.h:31
#define CHECK(condition)
Definition: Logger.h:197
static constexpr int NULL_REFRESH_TIME
Definition: ForeignTable.h:37