OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AbstractFileStorageDataWrapper.cpp
Go to the documentation of this file.
2 
4 #include "Catalog/ForeignTable.h"
7 #include "Shared/misc.h"
8 #include "Utils/DdlUtils.h"
9 
10 namespace foreign_storage {
12 
14  const ForeignServer* foreign_server) const {
15  const auto& options = foreign_server->options;
16  for (const auto& entry : options) {
17  if (!shared::contains(supported_server_options_, entry.first)) {
18  throw std::runtime_error{"Invalid foreign server option \"" + entry.first +
19  "\". Option must be one of the following: " +
20  join(supported_server_options_, ", ") + "."};
21  }
22  }
23 
24  if (options.find(STORAGE_TYPE_KEY) == options.end()) {
25  throw std::runtime_error{"Foreign server options must contain \"" + STORAGE_TYPE_KEY +
26  "\"."};
27  }
28  const auto& storage_type = options.find(STORAGE_TYPE_KEY)->second;
29  if (!shared::contains(supported_storage_types, storage_type)) {
30  throw std::runtime_error{"Invalid \"" + STORAGE_TYPE_KEY +
31  "\" option value. Value must be one of the following: " +
32  join(supported_storage_types, ", ") + "."};
33  }
34 
35  if (!g_enable_s3_fsi && storage_type == S3_STORAGE_TYPE) {
36  throw std::runtime_error{
37  "Foreign server storage type value of \"" + std::string{S3_STORAGE_TYPE} +
38  "\" is not allowed because FSI S3 support is currently disabled."};
39  }
40 }
41 
43  const ForeignTable* foreign_table) const {
44  validateFilePathOptionKey(foreign_table);
45  validateFilePath(foreign_table);
46 }
47 
48 const std::set<std::string_view>&
51 }
52 
54  const UserMapping* user_mapping,
55  const ForeignServer* foreign_server) const {
56  throw std::runtime_error{"User mapping for the \"" + foreign_server->data_wrapper_type +
57  "\" data wrapper can only be created for AWS S3 backed "
58  "foreign servers. AWS S3 support is currently disabled."};
59 }
60 
61 const std::set<std::string_view>&
64 }
65 
67  auto& server_options = foreign_table->foreign_server->options;
68  if (server_options.find(STORAGE_TYPE_KEY)->second == LOCAL_FILE_STORAGE_TYPE) {
71  }
72 }
73 
74 namespace {
75 std::string append_file_path(const std::optional<std::string>& base,
76  const std::optional<std::string>& subdirectory) {
77  const std::string separator{boost::filesystem::path::preferred_separator};
78  return std::regex_replace(
79  (base ? *base + separator : "") + (subdirectory ? *subdirectory : ""),
80  std::regex{separator + "{2,}"},
81  separator);
82 }
83 } // namespace
84 
90  const ForeignTable* foreign_table) {
91  auto file_path = foreign_table->getOption(FILE_PATH_KEY);
92  std::optional<std::string> base_path{};
93  auto foreign_server = foreign_table->foreign_server;
94  auto storage_type = foreign_server->getOption(STORAGE_TYPE_KEY);
95  CHECK(storage_type);
96 
97  const std::string separator{boost::filesystem::path::preferred_separator};
98  if (*storage_type == LOCAL_FILE_STORAGE_TYPE) {
99  base_path = foreign_server->getOption(BASE_PATH_KEY);
100  }
101 
102  // If both base_path and file_path are present, then concatenate. Otherwise we are just
103  // taking the one as the path. One of the two must exist, or we have failed validation.
104  CHECK(file_path || base_path);
105  return append_file_path(base_path, file_path);
106 }
107 
108 namespace {
109 void throw_file_path_error(const std::string_view& missing_path,
110  const std::string& table_name,
111  const std::string_view& file_path_key) {
112  std::stringstream ss;
113  ss << "No file_path found for Foreign Table \"" << table_name
114  << "\". Table must have either set a \"" << file_path_key << "\" option, or its "
115  << "parent server must have set a \"" << missing_path << "\" option.";
116  throw std::runtime_error(ss.str());
117 }
118 } // namespace
119 
120 // A valid path is a concatenation of the file_path and the base_path (for local storage).
121 // One of the two must be present.
123  const ForeignTable* foreign_table) {
124  auto file_path = foreign_table->getOption(FILE_PATH_KEY);
125  auto foreign_server = foreign_table->foreign_server;
126  auto storage_type = foreign_server->getOption(STORAGE_TYPE_KEY);
127  CHECK(storage_type) << "No storage type found in parent server. Server \""
128  << foreign_server->name << "\" is not valid.";
129  if (!file_path) {
130  if (*storage_type == LOCAL_FILE_STORAGE_TYPE) {
131  if (!foreign_server->getOption(BASE_PATH_KEY)) {
133  }
134  } else {
135  UNREACHABLE() << "Unknown foreign storage type.";
136  }
137  }
138 }
139 
140 const std::set<std::string_view> AbstractFileStorageDataWrapper::supported_table_options_{
141  FILE_PATH_KEY};
142 
143 const std::set<std::string_view>
145  BASE_PATH_KEY};
146 
147 const std::set<std::string_view>
149 } // namespace foreign_storage
void throw_file_path_error(const std::string_view &missing_path, const std::string &table_name, const std::string_view &file_path_key)
const std::set< std::string_view > & getSupportedUserMappingOptions() const override
bool contains(const T &container, const U &element)
Definition: misc.h:147
static void validateFilePathOptionKey(const ForeignTable *foreign_table)
static const std::array< std::string, 1 > supported_storage_types
std::string tableName
std::string join(T const &container, std::string const &delim)
#define UNREACHABLE()
Definition: Logger.h:241
void validateUserMappingOptions(const UserMapping *user_mapping, const ForeignServer *foreign_server) const override
static const std::set< std::string_view > supported_server_options_
void validateTableOptions(const ForeignTable *foreign_table) const override
static const std::set< std::string_view > supported_user_mapping_options_
bool g_enable_s3_fsi
Definition: Catalog.cpp:93
std::string append_file_path(const std::optional< std::string > &base, const std::optional< std::string > &subdirectory)
std::optional< std::string > getOption(const std::string_view &key) const
void validate_allowed_file_path(const std::string &file_path, const DataTransferType data_transfer_type, const bool allow_wildcards)
Definition: DdlUtils.cpp:613
const std::set< std::string_view > & getSupportedTableOptions() const override
void validateServerOptions(const ForeignServer *foreign_server) const override
const ForeignServer * foreign_server
Definition: ForeignTable.h:53
#define CHECK(condition)
Definition: Logger.h:197
static const std::set< std::string_view > supported_table_options_
static std::string getFullFilePath(const ForeignTable *foreign_table)
Returns the path to the source file/dir of the table. Depending on options this may result from a con...
static void validateFilePath(const ForeignTable *foreign_table)