OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionsContainer.h
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 #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 
26 #include "Logger/Logger.h"
27 #include "Shared/StringTransform.h"
28 
29 namespace foreign_storage {
30 using OptionsMap = std::map<std::string, std::string, std::less<>>;
33 
35 
36  OptionsContainer(const OptionsMap& options) : options(options) {}
37 
38  OptionsContainer(const std::string& options_str) { populateOptionsMap(options_str); }
39 
40  void populateOptionsMap(OptionsMap&& options_map, bool clear = false) {
41  if (clear) {
42  options = options_map;
43  } else {
44  // The merge order here is to make sure we overwrite duplicates in options. If we
45  // used options.merge(options_map) we would preserve existing entries.
46  options_map.merge(options);
47  options = options_map;
48  }
49  }
50 
51  void populateOptionsMap(const rapidjson::Value& ddl_options, bool clear = false) {
52  CHECK(ddl_options.IsObject());
53  if (clear) {
54  options.clear();
55  }
56  for (auto itr = ddl_options.MemberBegin(); itr != ddl_options.MemberEnd(); ++itr) {
57  std::string key = to_upper(itr->name.GetString());
58  options[key] = itr->value.GetString();
59  }
60  }
61 
62  void populateOptionsMap(const std::string& options_json, bool clear = false) {
63  CHECK(!options_json.empty());
64  rapidjson::Document options;
65  options.Parse(options_json);
66  populateOptionsMap(options);
67  }
68 
69  std::string getOptionsAsJsonString() const {
70  rapidjson::Document document;
71  document.SetObject();
72 
73  for (const auto& [key, value] : options) {
74  document.AddMember(rapidjson::Value().SetString(
75  key.c_str(), key.length(), document.GetAllocator()),
76  rapidjson::Value().SetString(
77  value.c_str(), value.length(), document.GetAllocator()),
78  document.GetAllocator());
79  }
80 
81  rapidjson::StringBuffer buffer;
82  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
83  document.Accept(writer);
84  return buffer.GetString();
85  }
86 
87  std::optional<std::string> getOption(const std::string_view& key) const {
88  if (auto it = options.find(key); it != options.end()) {
89  return it->second;
90  } else {
91  return {};
92  }
93  }
94 
95  bool getOptionAsBool(const std::string_view& key) const {
96  auto option = getOption(key);
97  return option.has_value() && option.value() == "TRUE";
98  }
99 };
100 } // namespace foreign_storage
void populateOptionsMap(const rapidjson::Value &ddl_options, bool clear=false)
void populateOptionsMap(const std::string &options_json, bool clear=false)
std::string getOptionsAsJsonString() const
OptionsContainer(const std::string &options_str)
void populateOptionsMap(OptionsMap &&options_map, bool clear=false)
std::optional< std::string > getOption(const std::string_view &key) const
OptionsContainer(const OptionsMap &options)
std::string to_upper(const std::string &str)
#define CHECK(condition)
Definition: Logger.h:291
std::map< std::string, std::string, std::less<>> OptionsMap
bool getOptionAsBool(const std::string_view &key) const