OmniSciDB  dfae7c3b14
ForeignServer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 #pragma once
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "rapidjson/document.h"
23 #include "rapidjson/stringbuffer.h"
24 #include "rapidjson/writer.h"
25 
27 #include "Shared/StringTransform.h"
28 
29 namespace foreign_storage {
35  static constexpr char const* CSV = "OMNISCI_CSV";
36  static constexpr char const* PARQUET = "OMNISCI_PARQUET";
37 
38  static constexpr std::array<std::string_view, 2> supported_data_wrapper_types{CSV,
39  PARQUET};
40 };
41 
43  static constexpr std::string_view STORAGE_TYPE_KEY = "STORAGE_TYPE";
44  static constexpr std::string_view BASE_PATH_KEY = "BASE_PATH";
45  static constexpr std::string_view LOCAL_FILE_STORAGE_TYPE = "LOCAL_FILE";
46  static constexpr std::array<std::string_view, 1> supported_storage_types{
47  LOCAL_FILE_STORAGE_TYPE};
48  static constexpr std::array<std::string_view, 2> all_option_keys{STORAGE_TYPE_KEY,
49  BASE_PATH_KEY};
50 
51  int32_t id;
52  std::string name;
53  std::string data_wrapper_type;
54  int32_t user_id;
55  time_t creation_time;
56 
58 
59  ForeignServer(const int32_t server_id,
60  const std::string& server_name,
61  const std::string& data_wrapper_type,
62  const std::string& options_str,
63  const int32_t user_id,
64  const time_t creation_time)
65  : OptionsContainer(options_str)
66  , id(server_id)
67  , name(server_name)
68  , data_wrapper_type(data_wrapper_type)
69  , user_id(user_id)
70  , creation_time(creation_time) {}
71 
72  ForeignServer(const std::string& server_name,
73  const std::string& data_wrapper_type,
74  const std::map<std::string, std::string, std::less<>>& options,
75  const int32_t user_id)
76  : OptionsContainer(options)
77  , name(server_name)
78  , data_wrapper_type(data_wrapper_type)
79  , user_id(user_id) {}
80 
81  void validate() {
82  validateDataWrapperType();
83  validateStorageParameters();
84  validateRecognizedOption();
85  }
86 
87  private:
89  const auto& supported_wrapper_types = DataWrapperType::supported_data_wrapper_types;
90  if (std::find(supported_wrapper_types.begin(),
91  supported_wrapper_types.end(),
92  data_wrapper_type) == supported_wrapper_types.end()) {
93  throw std::runtime_error{"Invalid data wrapper type \"" + data_wrapper_type +
94  "\". Data wrapper type must be one of the following: " +
95  join(supported_wrapper_types, ", ") + "."};
96  }
97  }
98 
100  if (options.find(STORAGE_TYPE_KEY) == options.end()) {
101  throw std::runtime_error{"Foreign server options must contain \"STORAGE_TYPE\"."};
102  }
103 
104  if (std::find(supported_storage_types.begin(),
105  supported_storage_types.end(),
106  options.find(STORAGE_TYPE_KEY)->second) ==
107  supported_storage_types.end()) {
108  std::string error_message =
109  "Invalid storage type value. Value must be one of the following: " +
110  join(supported_storage_types, ", ") + ".";
111  throw std::runtime_error{error_message};
112  }
113 
114  if (options.find(STORAGE_TYPE_KEY)->second == LOCAL_FILE_STORAGE_TYPE) {
115  if (options.find(BASE_PATH_KEY) == options.end()) {
116  throw std::runtime_error{"Foreign server options must contain \"BASE_PATH\"."};
117  }
118  } else {
119  UNREACHABLE();
120  }
121  }
122 
124  for (const auto& entry : options) {
125  if (std::find(all_option_keys.begin(), all_option_keys.end(), entry.first) ==
126  all_option_keys.end()) {
127  std::string error_message =
128  "Invalid option \"" + entry.first + "\"." +
129  " Option must be one of the following: " + join(all_option_keys, ", ") + ".";
130  throw std::runtime_error{error_message};
131  }
132  }
133  }
134 };
135 } // namespace foreign_storage
std::string join(T const &container, std::string const &delim)
#define UNREACHABLE()
Definition: Logger.h:241
static constexpr std::array< std::string_view, 2 > supported_data_wrapper_types
Definition: ForeignServer.h:38
Encapsulates an enumeration of foreign data wrapper type strings.
Definition: ForeignServer.h:34
ForeignServer(const int32_t server_id, const std::string &server_name, const std::string &data_wrapper_type, const std::string &options_str, const int32_t user_id, const time_t creation_time)
Definition: ForeignServer.h:59
ForeignServer(const std::string &server_name, const std::string &data_wrapper_type, const std::map< std::string, std::string, std::less<>> &options, const int32_t user_id)
Definition: ForeignServer.h:72
static constexpr char const * CSV
Definition: ForeignServer.h:35
static constexpr char const * PARQUET
Definition: ForeignServer.h:36