OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RefreshTimeCalculator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "RefreshTimeCalculator.h"
18 
19 #include <chrono>
20 
21 #include "ForeignTable.h"
22 #include "Shared/DateTimeParser.h"
23 
24 namespace foreign_storage {
25 namespace {
33 int64_t get_interval_duration(const std::string& interval) {
34  int interval_count = std::stoi(interval.substr(0, interval.length() - 1));
35  auto interval_type = std::tolower(interval[interval.length() - 1]);
36  int64_t duration{0};
37  if (interval_type == 's') {
38  duration = interval_count;
39  } else if (interval_type == 'h') {
40  duration = interval_count * 60 * 60;
41  } else if (interval_type == 'd') {
42  duration = interval_count * 60 * 60 * 24;
43  } else {
44  UNREACHABLE();
45  }
46  return duration;
47 }
48 } // namespace
49 
50 // TODO: Support complete list of interval types
52  const std::map<std::string, std::string, std::less<>>& foreign_table_options) {
53  int64_t current_time = getCurrentTime();
54  auto start_date_entry = foreign_table_options.find(
56  CHECK(start_date_entry != foreign_table_options.end());
57  auto start_date_time = dateTimeParse<kTIMESTAMP>(start_date_entry->second, 0);
58 
59  // If start date time is current or in the future, then that is the next refresh time
60  if (start_date_time >= current_time) {
61  return start_date_time;
62  }
63  auto interval_entry =
64  foreign_table_options.find(foreign_storage::ForeignTable::REFRESH_INTERVAL_KEY);
65  if (interval_entry != foreign_table_options.end()) {
66  auto interval_duration = get_interval_duration(interval_entry->second);
67  auto num_intervals =
68  (current_time - start_date_time + interval_duration - 1) / interval_duration;
69  return start_date_time + (num_intervals * interval_duration);
70  } else {
71  // If this was a one time refresh, then there is no next refresh time
73  }
74 }
75 
79  return mock_current_time_;
80  } else {
81  return std::chrono::duration_cast<std::chrono::seconds>(
82  std::chrono::system_clock::now().time_since_epoch())
83  .count();
84  }
85 }
86 
87 void RefreshTimeCalculator::setMockCurrentTime(int64_t mock_current_time) {
88  mock_current_time_ = mock_current_time;
90 }
91 
95 }
96 } // namespace foreign_storage
#define UNREACHABLE()
Definition: Logger.h:338
static std::atomic< int64_t > mock_current_time_
#define CHECK_GT(x, y)
Definition: Logger.h:305
static constexpr const char * REFRESH_START_DATE_TIME_KEY
Definition: ForeignTable.h:44
static int64_t getNextRefreshTime(const std::map< std::string, std::string, std::less<>> &foreign_table_options)
static constexpr const char * REFRESH_INTERVAL_KEY
Definition: ForeignTable.h:45
#define CHECK(condition)
Definition: Logger.h:291
static constexpr int NULL_REFRESH_TIME
Definition: ForeignTable.h:55
static void setMockCurrentTime(int64_t mock_current_time)
static std::atomic< bool > should_use_mock_current_time_