Coverage Report

Created: 2026-09-02 19:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
991
    Settings() = default;
39
176
    Settings(std::unordered_map<std::string, std::string> args) : _args(std::move(args)) {}
40
1.31k
    Settings(const Settings&) = default;
41
351
    Settings(Settings&&) = default;
42
2.83k
    ~Settings() = default;
43
44
712
    void set(const std::string& key, const std::string& value) {
45
712
        _args.insert_or_assign(key, value);
46
712
    }
47
48
213
    bool empty() const { return _args.empty(); }
49
50
3.19k
    bool get_bool(const std::string& key, bool default_value) const {
51
3.19k
        auto it = _args.find(key);
52
3.19k
        if (it != _args.end()) {
53
1.79k
            std::string value = it->second;
54
1.79k
            std::transform(value.begin(), value.end(), value.begin(),
55
8.13k
                           [](unsigned char c) { return std::tolower(c); });
56
1.79k
            if (value == "true" || value == "1") {
57
818
                return true;
58
974
            } else if (value == "false" || value == "0") {
59
972
                return false;
60
972
            }
61
1.79k
        }
62
1.40k
        return default_value;
63
3.19k
    }
64
65
1.60k
    int32_t get_int(const std::string& key, int32_t default_value) const {
66
1.60k
        auto it = _args.find(key);
67
1.60k
        if (it != _args.end()) {
68
204
            try {
69
204
                size_t pos;
70
204
                int32_t num = std::stoi(it->second, &pos);
71
204
                if (pos == it->second.size()) {
72
202
                    return num;
73
202
                }
74
204
            } catch (...) {
75
1
                throw Exception(ErrorCode::INVALID_ARGUMENT,
76
1
                                "stoi failed (invalid argument or out of range): " + it->second);
77
1
            }
78
204
        }
79
1.40k
        return default_value;
80
1.60k
    }
81
82
535
    std::string get_string(const std::string& key, const std::string& default_value = "") const {
83
535
        auto it = _args.find(key);
84
535
        if (it != _args.end()) {
85
73
            return it->second;
86
73
        }
87
462
        return default_value;
88
535
    }
89
90
245
    std::vector<std::string> get_entry_list(const std::string& key) const {
91
245
        static const boost::regex sep(R"((?<=\])\s*,\s*(?=\[))");
92
245
        std::vector<std::string> lists;
93
245
        auto it = _args.find(key);
94
245
        if (it != _args.end()) {
95
231
            std::string trimmed_input = boost::algorithm::trim_copy(it->second);
96
231
            if (trimmed_input.empty()) {
97
1
                return lists;
98
1
            }
99
100
230
            auto validate_single = [&](const std::string& item, const std::string& prefix) {
101
31
                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
30
                int depth = 0;
106
213
                for (size_t i = 0; i + 1 < item.size(); ++i) {
107
183
                    char c = item[i];
108
183
                    if (c == '[') {
109
30
                        ++depth;
110
153
                    } 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
183
                }
118
30
            };
119
120
230
            if (boost::regex_search(trimmed_input, sep)) {
121
12
                boost::sregex_token_iterator regex_it(trimmed_input.begin(), trimmed_input.end(),
122
12
                                                      sep, -1);
123
12
                boost::sregex_token_iterator end;
124
43
                for (; regex_it != end; ++regex_it) {
125
31
                    std::string item = boost::algorithm::trim_copy(regex_it->str());
126
31
                    validate_single(item, "Each item in ");
127
31
                    std::string content = item.substr(1, item.size() - 2);
128
31
                    if (!content.empty()) {
129
29
                        lists.emplace_back(content);
130
29
                    }
131
31
                }
132
218
            } else {
133
218
                if (trimmed_input.size() < 2 || trimmed_input.front() != '[' ||
134
218
                    trimmed_input.back() != ']') {
135
2
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
136
2
                                    "Item in " + key + " must be enclosed in []");
137
2
                }
138
216
                std::string content = trimmed_input.substr(1, trimmed_input.size() - 2);
139
216
                if (!content.empty()) {
140
215
                    lists.emplace_back(content);
141
215
                }
142
216
            }
143
230
        }
144
242
        return lists;
145
245
    }
146
147
39
    std::unordered_set<std::string> get_word_set(const std::string& key) const {
148
39
        std::unordered_set<std::string> sets;
149
39
        auto it = _args.find(key);
150
39
        if (it != _args.end()) {
151
21
            std::vector<std::string> lists;
152
21
            boost::split(lists, it->second, boost::is_any_of(","));
153
31
            for (auto& str : lists) {
154
31
                boost::trim(str);
155
31
                if (!str.empty()) {
156
28
                    sets.insert(str);
157
28
                }
158
31
            }
159
21
        }
160
39
        return sets;
161
39
    }
162
163
1
    std::string to_string() {
164
1
        std::string result;
165
2
        for (const auto& [key, value] : _args) {
166
2
            if (!result.empty()) {
167
1
                result += ", ";
168
1
            }
169
2
            result += key + "=" + value;
170
2
        }
171
1
        return result;
172
1
    }
173
174
122
    std::vector<std::pair<std::string, std::string>> sorted_entries() const {
175
122
        std::vector<std::pair<std::string, std::string>> entries(_args.begin(), _args.end());
176
122
        std::sort(entries.begin(), entries.end());
177
122
        return entries;
178
122
    }
179
180
private:
181
    std::unordered_map<std::string, std::string> _args;
182
};
183
184
} // namespace doris::segment_v2::inverted_index