be/src/storage/index/inverted/setting.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <unicode/utf8.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <boost/algorithm/string.hpp> |
24 | | #include <boost/algorithm/string/split.hpp> |
25 | | #include <boost/algorithm/string/trim.hpp> |
26 | | #include <boost/regex.hpp> |
27 | | #include <unordered_map> |
28 | | #include <unordered_set> |
29 | | #include <utility> |
30 | | #include <vector> |
31 | | |
32 | | #include "common/exception.h" |
33 | | |
34 | | namespace doris::segment_v2::inverted_index { |
35 | | |
36 | | class Settings { |
37 | | public: |
38 | 2.01k | Settings() = default; |
39 | | Settings(std::unordered_map<std::string, std::string> args) : _args(std::move(args)) {} |
40 | 4.37k | Settings(const Settings&) = default; |
41 | 1.37k | Settings(Settings&&) = default; |
42 | 7.94k | ~Settings() = default; |
43 | | |
44 | 1.32k | void set(const std::string& key, const std::string& value) { |
45 | 1.32k | _args.insert_or_assign(key, value); |
46 | 1.32k | } |
47 | | |
48 | 219 | bool empty() const { return _args.empty(); } |
49 | | |
50 | 5.54k | bool get_bool(const std::string& key, bool default_value) const { |
51 | 5.54k | auto it = _args.find(key); |
52 | 5.54k | if (it != _args.end()) { |
53 | 2.35k | std::string value = it->second; |
54 | 2.35k | std::transform(value.begin(), value.end(), value.begin(), |
55 | 10.5k | [](unsigned char c) { return std::tolower(c); }); |
56 | 2.35k | if (value == "true" || value == "1") { |
57 | 1.19k | return true; |
58 | 1.19k | } else if (value == "false" || value == "0") { |
59 | 1.16k | return false; |
60 | 1.16k | } |
61 | 2.35k | } |
62 | 3.18k | return default_value; |
63 | 5.54k | } |
64 | | |
65 | 2.16k | int32_t get_int(const std::string& key, int32_t default_value) const { |
66 | 2.16k | auto it = _args.find(key); |
67 | 2.16k | if (it != _args.end()) { |
68 | 216 | try { |
69 | 216 | size_t pos; |
70 | 216 | int32_t num = std::stoi(it->second, &pos); |
71 | 216 | if (pos == it->second.size()) { |
72 | 214 | return num; |
73 | 214 | } |
74 | 216 | } catch (...) { |
75 | 1 | throw Exception(ErrorCode::INVALID_ARGUMENT, |
76 | 1 | "stoi failed (invalid argument or out of range): " + it->second); |
77 | 1 | } |
78 | 216 | } |
79 | 1.94k | return default_value; |
80 | 2.16k | } |
81 | | |
82 | 564 | std::string get_string(const std::string& key, const std::string& default_value = "") const { |
83 | 564 | auto it = _args.find(key); |
84 | 564 | if (it != _args.end()) { |
85 | 87 | return it->second; |
86 | 87 | } |
87 | 477 | return default_value; |
88 | 564 | } |
89 | | |
90 | 301 | std::vector<std::string> get_entry_list(const std::string& key) const { |
91 | 301 | static const boost::regex sep(R"((?<=\])\s*,\s*(?=\[))"); |
92 | 301 | std::vector<std::string> lists; |
93 | 301 | auto it = _args.find(key); |
94 | 301 | if (it != _args.end()) { |
95 | 244 | std::string trimmed_input = boost::algorithm::trim_copy(it->second); |
96 | 244 | if (trimmed_input.empty()) { |
97 | 1 | return lists; |
98 | 1 | } |
99 | | |
100 | 243 | auto validate_single = [&](const std::string& item, const std::string& prefix) { |
101 | 57 | if (item.size() < 2 || item.front() != '[' || item.back() != ']') { |
102 | 1 | throw Exception(ErrorCode::INVALID_ARGUMENT, |
103 | 1 | prefix + key + " must be enclosed in []"); |
104 | 1 | } |
105 | 56 | int depth = 0; |
106 | 443 | for (size_t i = 0; i + 1 < item.size(); ++i) { |
107 | 387 | char c = item[i]; |
108 | 387 | if (c == '[') { |
109 | 56 | ++depth; |
110 | 331 | } else if (c == ']') { |
111 | 0 | --depth; |
112 | 0 | if (depth == 0) { |
113 | 0 | throw Exception(ErrorCode::INVALID_ARGUMENT, |
114 | 0 | prefix + key + " must be enclosed in []"); |
115 | 0 | } |
116 | 0 | } |
117 | 387 | } |
118 | 56 | }; |
119 | | |
120 | 243 | if (boost::regex_search(trimmed_input, sep)) { |
121 | 22 | boost::sregex_token_iterator regex_it(trimmed_input.begin(), trimmed_input.end(), |
122 | 22 | sep, -1); |
123 | 22 | boost::sregex_token_iterator end; |
124 | 79 | for (; regex_it != end; ++regex_it) { |
125 | 57 | std::string item = boost::algorithm::trim_copy(regex_it->str()); |
126 | 57 | validate_single(item, "Each item in "); |
127 | 57 | std::string content = item.substr(1, item.size() - 2); |
128 | 57 | if (!content.empty()) { |
129 | 55 | lists.emplace_back(content); |
130 | 55 | } |
131 | 57 | } |
132 | 221 | } else { |
133 | 221 | if (trimmed_input.size() < 2 || trimmed_input.front() != '[' || |
134 | 221 | trimmed_input.back() != ']') { |
135 | 2 | throw Exception(ErrorCode::INVALID_ARGUMENT, |
136 | 2 | "Item in " + key + " must be enclosed in []"); |
137 | 2 | } |
138 | 219 | std::string content = trimmed_input.substr(1, trimmed_input.size() - 2); |
139 | 219 | if (!content.empty()) { |
140 | 218 | lists.emplace_back(content); |
141 | 218 | } |
142 | 219 | } |
143 | 243 | } |
144 | 298 | return lists; |
145 | 301 | } |
146 | | |
147 | 88 | std::unordered_set<std::string> get_word_set(const std::string& key) const { |
148 | 88 | std::unordered_set<std::string> sets; |
149 | 88 | auto it = _args.find(key); |
150 | 88 | if (it != _args.end()) { |
151 | 27 | std::vector<std::string> lists; |
152 | 27 | boost::split(lists, it->second, boost::is_any_of(",")); |
153 | 37 | for (auto& str : lists) { |
154 | 37 | boost::trim(str); |
155 | 37 | if (!str.empty()) { |
156 | 34 | sets.insert(str); |
157 | 34 | } |
158 | 37 | } |
159 | 27 | } |
160 | 88 | return sets; |
161 | 88 | } |
162 | | |
163 | | std::string to_string() { |
164 | | std::string result; |
165 | | for (const auto& [key, value] : _args) { |
166 | | if (!result.empty()) { |
167 | | result += ", "; |
168 | | } |
169 | | result += key + "=" + value; |
170 | | } |
171 | | return result; |
172 | | } |
173 | | |
174 | 1.13k | std::vector<std::pair<std::string, std::string>> sorted_entries() const { |
175 | 1.13k | std::vector<std::pair<std::string, std::string>> entries(_args.begin(), _args.end()); |
176 | 1.13k | std::sort(entries.begin(), entries.end()); |
177 | 1.13k | return entries; |
178 | 1.13k | } |
179 | | |
180 | | private: |
181 | | std::unordered_map<std::string, std::string> _args; |
182 | | }; |
183 | | |
184 | | } // namespace doris::segment_v2::inverted_index |