OmniSciDB  0264ff685a
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 extern bool g_enable_s3_fsi;
30 
31 namespace foreign_storage {
37  static constexpr char const* CSV = "OMNISCI_CSV";
38  static constexpr char const* PARQUET = "OMNISCI_PARQUET";
39 
40  static constexpr std::array<std::string_view, 2> supported_data_wrapper_types{PARQUET,
41  CSV};
42 };
43 
45  static constexpr std::string_view STORAGE_TYPE_KEY = "STORAGE_TYPE";
46  static constexpr std::string_view BASE_PATH_KEY = "BASE_PATH";
47  static constexpr std::string_view LOCAL_FILE_STORAGE_TYPE = "LOCAL_FILE";
48 
49  static constexpr std::string_view S3_STORAGE_TYPE = "AWS_S3";
50 
51  static constexpr std::array<std::string_view, 1> supported_storage_types{
52  LOCAL_FILE_STORAGE_TYPE};
53 
54  static constexpr std::array<std::string_view, 2> all_option_keys{STORAGE_TYPE_KEY,
55  BASE_PATH_KEY};
56 
57  int32_t id;
58  std::string name;
59  std::string data_wrapper_type;
60  int32_t user_id;
61  time_t creation_time;
62 
64 
65  ForeignServer(const int32_t server_id,
66  const std::string& server_name,
67  const std::string& data_wrapper_type,
68  const std::string& options_str,
69  const int32_t user_id,
70  const time_t creation_time)
71  : OptionsContainer(options_str)
72  , id(server_id)
73  , name(server_name)
74  , data_wrapper_type(data_wrapper_type)
75  , user_id(user_id)
76  , creation_time(creation_time) {}
77 
78  ForeignServer(const std::string& server_name,
79  const std::string& data_wrapper_type,
80  const std::map<std::string, std::string, std::less<>>& options,
81  const int32_t user_id)
82  : OptionsContainer(options)
83  , name(server_name)
84  , data_wrapper_type(data_wrapper_type)
85  , user_id(user_id) {}
86 
87  void validate() {
88  validateDataWrapperType();
89  validateStorageParameters();
90  validateRecognizedOption();
91  }
92 
93  private:
95  const auto& supported_wrapper_types = DataWrapperType::supported_data_wrapper_types;
96  if (std::find(supported_wrapper_types.begin(),
97  supported_wrapper_types.end(),
98  data_wrapper_type) == supported_wrapper_types.end()) {
99  throw std::runtime_error{"Invalid data wrapper type \"" + data_wrapper_type +
100  "\". Data wrapper type must be one of the following: " +
101  join(supported_wrapper_types, ", ") + "."};
102  }
103  }
104 
106  if (options.find(STORAGE_TYPE_KEY) == options.end()) {
107  throw std::runtime_error{"Foreign server options must contain \"STORAGE_TYPE\"."};
108  }
109 
110  const auto& storage_type = options.find(STORAGE_TYPE_KEY)->second;
111  if (!g_enable_s3_fsi && storage_type == S3_STORAGE_TYPE) {
112  throw std::runtime_error{
113  "Foreign server storage type value of \"" + std::string{S3_STORAGE_TYPE} +
114  "\" is not allowed because FSI S3 support is currently disabled."};
115  } else if (std::find(supported_storage_types.begin(),
116  supported_storage_types.end(),
117  storage_type) == supported_storage_types.end()) {
118  std::string error_message =
119  "Invalid storage type value. Value must be one of the following: " +
120  join(supported_storage_types, ", ") + ".";
121  throw std::runtime_error{error_message};
122  }
123  }
124 
126  for (const auto& entry : options) {
127  if (std::find(all_option_keys.begin(), all_option_keys.end(), entry.first) ==
128  all_option_keys.end()) {
129  std::string error_message =
130  "Invalid option \"" + entry.first + "\"." +
131  " Option must be one of the following: " + join(all_option_keys, ", ") + ".";
132  throw std::runtime_error{error_message};
133  }
134  }
135  }
136 };
137 } // namespace foreign_storage
bool g_enable_s3_fsi
std::string join(T const &container, std::string const &delim)
static constexpr std::array< std::string_view, 2 > supported_data_wrapper_types
Definition: ForeignServer.h:40
Encapsulates an enumeration of foreign data wrapper type strings.
Definition: ForeignServer.h:36
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:65
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:78
static constexpr char const * CSV
Definition: ForeignServer.h:37
static constexpr char const * PARQUET
Definition: ForeignServer.h:38