Coverage Report

Created: 2026-03-13 21:13

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 <boost/algorithm/string.hpp>
23
#include <boost/algorithm/string/split.hpp>
24
#include <boost/algorithm/string/trim.hpp>
25
#include <boost/regex.hpp>
26
#include <unordered_map>
27
#include <utility>
28
29
#include "common/exception.h"
30
31
namespace doris::segment_v2::inverted_index {
32
33
class Settings {
34
public:
35
619
    Settings() = default;
36
176
    Settings(std::unordered_map<std::string, std::string> args) : _args(std::move(args)) {}
37
372
    Settings(const Settings&) = default;
38
130
    Settings(Settings&&) = default;
39
1.29k
    ~Settings() = default;
40
41
305
    void set(const std::string& key, const std::string& value) {
42
305
        _args.insert_or_assign(key, value);
43
305
    }
44
45
32
    bool empty() const { return _args.empty(); }
46
47
3.14k
    bool get_bool(const std::string& key, bool default_value) const {
48
3.14k
        auto it = _args.find(key);
49
3.14k
        if (it != _args.end()) {
50
1.79k
            std::string value = it->second;
51
1.79k
            std::transform(value.begin(), value.end(), value.begin(),
52
8.13k
                           [](unsigned char c) { return std::tolower(c); });
53
1.79k
            if (value == "true" || value == "1") {
54
817
                return true;
55
974
            } else if (value == "false" || value == "0") {
56
972
                return false;
57
972
            }
58
1.79k
        }
59
1.35k
        return default_value;
60
3.14k
    }
61
62
1.37k
    int32_t get_int(const std::string& key, int32_t default_value) const {
63
1.37k
        auto it = _args.find(key);
64
1.37k
        if (it != _args.end()) {
65
142
            try {
66
142
                size_t pos;
67
142
                int32_t num = std::stoi(it->second, &pos);
68
142
                if (pos == it->second.size()) {
69
140
                    return num;
70
140
                }
71
142
            } catch (...) {
72
1
                throw Exception(ErrorCode::INVALID_ARGUMENT,
73
1
                                "stoi failed (invalid argument or out of range): " + it->second);
74
1
            }
75
142
        }
76
1.23k
        return default_value;
77
1.37k
    }
78
79
527
    std::string get_string(const std::string& key, const std::string& default_value = "") const {
80
527
        auto it = _args.find(key);
81
527
        if (it != _args.end()) {
82
73
            return it->second;
83
73
        }
84
454
        return default_value;
85
527
    }
86
87
50
    std::vector<std::string> get_entry_list(const std::string& key) const {
88
50
        static const boost::regex sep(R"((?<=\])\s*,\s*(?=\[))");
89
50
        std::vector<std::string> lists;
90
50
        auto it = _args.find(key);
91
50
        if (it != _args.end()) {
92
37
            std::string trimmed_input = boost::algorithm::trim_copy(it->second);
93
37
            if (trimmed_input.empty()) {
94
1
                return lists;
95
1
            }
96
97
36
            auto validate_single = [&](const std::string& item, const std::string& prefix) {
98
29
                if (item.size() < 2 || item.front() != '[' || item.back() != ']') {
99
1
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
100
1
                                    prefix + key + " must be enclosed in []");
101
1
                }
102
28
                int depth = 0;
103
188
                for (size_t i = 0; i + 1 < item.size(); ++i) {
104
160
                    char c = item[i];
105
160
                    if (c == '[') {
106
28
                        ++depth;
107
132
                    } else if (c == ']') {
108
0
                        --depth;
109
0
                        if (depth == 0) {
110
0
                            throw Exception(ErrorCode::INVALID_ARGUMENT,
111
0
                                            prefix + key + " must be enclosed in []");
112
0
                        }
113
0
                    }
114
160
                }
115
28
            };
116
117
36
            if (boost::regex_search(trimmed_input, sep)) {
118
11
                boost::sregex_token_iterator regex_it(trimmed_input.begin(), trimmed_input.end(),
119
11
                                                      sep, -1);
120
11
                boost::sregex_token_iterator end;
121
40
                for (; regex_it != end; ++regex_it) {
122
29
                    std::string item = boost::algorithm::trim_copy(regex_it->str());
123
29
                    validate_single(item, "Each item in ");
124
29
                    std::string content = item.substr(1, item.size() - 2);
125
29
                    if (!content.empty()) {
126
27
                        lists.emplace_back(content);
127
27
                    }
128
29
                }
129
25
            } else {
130
25
                if (trimmed_input.size() < 2 || trimmed_input.front() != '[' ||
131
25
                    trimmed_input.back() != ']') {
132
2
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
133
2
                                    "Item in " + key + " must be enclosed in []");
134
2
                }
135
23
                std::string content = trimmed_input.substr(1, trimmed_input.size() - 2);
136
23
                if (!content.empty()) {
137
22
                    lists.emplace_back(content);
138
22
                }
139
23
            }
140
36
        }
141
47
        return lists;
142
50
    }
143
144
38
    std::unordered_set<std::string> get_word_set(const std::string& key) const {
145
38
        std::unordered_set<std::string> sets;
146
38
        auto it = _args.find(key);
147
38
        if (it != _args.end()) {
148
21
            std::vector<std::string> lists;
149
21
            boost::split(lists, it->second, boost::is_any_of(","));
150
31
            for (auto& str : lists) {
151
31
                boost::trim(str);
152
31
                if (!str.empty()) {
153
28
                    sets.insert(str);
154
28
                }
155
31
            }
156
21
        }
157
38
        return sets;
158
38
    }
159
160
1
    std::string to_string() {
161
1
        std::string result;
162
2
        for (const auto& [key, value] : _args) {
163
2
            if (!result.empty()) {
164
1
                result += ", ";
165
1
            }
166
2
            result += key + "=" + value;
167
2
        }
168
1
        return result;
169
1
    }
170
171
private:
172
    std::unordered_map<std::string, std::string> _args;
173
};
174
175
} // namespace doris::segment_v2::inverted_index