Coverage Report

Created: 2026-03-15 22:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/path_trie.hpp
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 <map>
21
#include <memory>
22
#include <string>
23
#include <vector>
24
25
namespace doris {
26
27
// This tree is usd for manage restful api path.
28
template <class T>
29
class PathTrie {
30
public:
31
54
    PathTrie() : _root("/", "*"), _root_value(nullptr), _separator('/') {}
32
33
30
    ~PathTrie() {
34
30
        if (_root_value != nullptr) {
35
3
            _allocator.destroy(_root_value);
36
3
            _allocator.deallocate(_root_value, 1);
37
3
        }
38
30
    }
39
40
    class Allocator {
41
    public:
42
        using value_type = T;
43
44
556
        T* allocate(size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
45
46
        template <typename... Args>
47
7.78k
        void construct(T* p, Args&&... args) {
48
7.78k
            new (p) T(std::forward<Args>(args)...);
49
7.78k
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRKS2_EEEvPS2_DpOT_
Line
Count
Source
47
556
        void construct(T* p, Args&&... args) {
48
556
            new (p) T(std::forward<Args>(args)...);
49
556
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRS2_EEEvPS2_DpOT_
Line
Count
Source
47
7.22k
        void construct(T* p, Args&&... args) {
48
7.22k
            new (p) T(std::forward<Args>(args)...);
49
7.22k
        }
50
51
235
        void destroy(T* p) { p->~T(); }
52
53
235
        void deallocate(T* p, size_t n) { ::operator delete(p); }
54
    };
55
56
    class TrieNode {
57
    public:
58
        TrieNode(const std::string& key, const std::string& wildcard)
59
302
                : _value(nullptr), _wildcard(wildcard) {
60
302
            if (is_named_wildcard(key)) {
61
35
                _named_wildcard = extract_template(key);
62
35
            }
63
302
        }
64
65
        TrieNode(const std::string& key, const T& value, const std::string& wildcard)
66
549
                : _value(nullptr), _wildcard(wildcard) {
67
549
            _value = _allocator.allocate(1);
68
549
            _allocator.construct(_value, value);
69
549
            if (is_named_wildcard(key)) {
70
70
                _named_wildcard = extract_template(key);
71
70
            }
72
549
        }
73
74
372
        ~TrieNode() {
75
372
            for (auto& iter : _children) {
76
342
                delete iter.second;
77
342
                iter.second = nullptr;
78
342
            }
79
372
            if (_value != nullptr) {
80
232
                _allocator.destroy(_value);
81
232
                _allocator.deallocate(_value, 1);
82
232
            }
83
372
        }
84
85
        // Return true if insert success.
86
1.31k
        bool insert(const std::vector<std::string> path, int index, const T& value) {
87
1.31k
            if (index >= path.size()) {
88
0
                return false;
89
0
            }
90
1.31k
            const std::string& token = path[index];
91
1.31k
            std::string key = token;
92
93
1.31k
            if (is_named_wildcard(token)) {
94
154
                key = _wildcard;
95
154
            }
96
97
1.31k
            TrieNode* node = get_child(key);
98
99
1.31k
            if (node == nullptr) {
100
                // no exist child for this key
101
797
                if (index == path.size() - 1) {
102
549
                    node = new TrieNode(token, value, _wildcard);
103
549
                    _children.insert(std::make_pair(key, node));
104
549
                    return true;
105
549
                } else {
106
248
                    node = new TrieNode(token, _wildcard);
107
248
                    _children.insert(std::make_pair(key, node));
108
248
                }
109
797
            } else {
110
                // If this is a template, set this to the node
111
515
                if (is_named_wildcard(token)) {
112
49
                    std::string temp = extract_template(token);
113
49
                    if (node->_named_wildcard.empty() || node->_named_wildcard.compare(temp) == 0) {
114
49
                        node->_named_wildcard = temp;
115
49
                    } else {
116
                        // Duplicated
117
0
                        return false;
118
0
                    }
119
49
                }
120
515
                if (index == path.size() - 1) {
121
0
                    if (node->_value == nullptr) {
122
0
                        node->_value = _allocator.allocate(1);
123
0
                        _allocator.construct(node->_value, value);
124
0
                        return true;
125
0
                    }
126
                    // Already register by other path
127
0
                    return false;
128
0
                }
129
515
            }
130
763
            return node->insert(path, index + 1, value);
131
1.31k
        }
132
133
        bool retrieve(const std::vector<std::string> path, int index, T* value,
134
23.6k
                      std::map<std::string, std::string>* params) {
135
            // check max index
136
23.6k
            if (index >= path.size()) {
137
0
                return false;
138
0
            }
139
23.6k
            bool use_wildcard = false;
140
23.6k
            const std::string& token = path[index];
141
23.6k
            TrieNode* node = get_child(token);
142
23.6k
            if (node == nullptr) {
143
4.83k
                node = get_child(_wildcard);
144
4.83k
                if (node == nullptr) {
145
3
                    return false;
146
3
                }
147
4.83k
                use_wildcard = true;
148
18.8k
            } else {
149
                // If we the last one, but we have no value, check wildcard
150
18.8k
                if (index == path.size() - 1 && node->_value == nullptr &&
151
18.8k
                    get_child(_wildcard) != nullptr) {
152
0
                    node = get_child(_wildcard);
153
0
                    use_wildcard = true;
154
18.8k
                } else {
155
18.8k
                    use_wildcard = (token.compare(_wildcard) == 0);
156
18.8k
                }
157
18.8k
            }
158
159
23.6k
            put(params, node, token);
160
161
23.6k
            if (index == path.size() - 1) {
162
7.22k
                if (node->_value == nullptr) {
163
0
                    return false;
164
0
                }
165
7.22k
                _allocator.construct(value, *node->_value);
166
7.22k
                return true;
167
7.22k
            }
168
169
            // find exact
170
16.4k
            if (node->retrieve(path, index + 1, value, params)) {
171
16.4k
                return true;
172
16.4k
            }
173
174
            // backtrace to test if wildcard can match
175
2
            if (!use_wildcard) {
176
2
                node = get_child(_wildcard);
177
2
                if (node != nullptr) {
178
0
                    put(params, node, token);
179
0
                    return node->retrieve(path, index + 1, value, params);
180
0
                }
181
2
            }
182
2
            return false;
183
2
        }
184
185
    private:
186
2.67k
        bool is_named_wildcard(const std::string& key) {
187
2.67k
            if (key.find('{') != std::string::npos && key.find('}') != std::string::npos) {
188
308
                return true;
189
308
            }
190
2.37k
            return false;
191
2.67k
        }
192
193
154
        std::string extract_template(const std::string& key) {
194
154
            std::size_t left = key.find_first_of('{') + 1;
195
154
            std::size_t right = key.find_last_of('}');
196
154
            return key.substr(left, right - left);
197
154
        }
198
199
29.8k
        TrieNode* get_child(const std::string& key) {
200
29.8k
            auto pair = _children.find(key);
201
29.8k
            if (pair == _children.end()) {
202
5.63k
                return nullptr;
203
5.63k
            }
204
24.1k
            return pair->second;
205
29.8k
        }
206
207
        void put(std::map<std::string, std::string>* params, TrieNode* node,
208
23.6k
                 const std::string& token) {
209
23.6k
            if (params != nullptr && !node->_named_wildcard.empty()) {
210
4.83k
                params->insert(std::make_pair(node->_named_wildcard, token));
211
4.83k
            }
212
23.6k
        }
213
214
        T* _value;
215
        std::string _wildcard;
216
        std::string _named_wildcard;
217
        std::map<std::string, TrieNode*> _children;
218
        Allocator _allocator;
219
    };
220
221
556
    bool insert(const std::string& path, const T& value) {
222
556
        std::vector<std::string> path_array;
223
556
        split(path, &path_array);
224
556
        if (path_array.empty()) {
225
7
            if (_root_value == nullptr) {
226
7
                _root_value = _allocator.allocate(1);
227
7
                _allocator.construct(_root_value, value);
228
7
                return true;
229
7
            } else {
230
0
                return false;
231
0
            }
232
7
        }
233
549
        int index = 0;
234
549
        if (path_array[0].empty()) {
235
0
            index = 1;
236
0
        }
237
549
        return _root.insert(path_array, index, value);
238
556
    }
239
240
    bool retrieve(const std::string& path, T* value) { return retrieve(path, value, nullptr); }
241
242
7.23k
    bool retrieve(const std::string& path, T* value, std::map<std::string, std::string>* params) {
243
7.23k
        if (path.empty()) {
244
0
            if (_root_value == nullptr) {
245
0
                return false;
246
0
            } else {
247
0
                _allocator.construct(value, *_root_value);
248
0
                return true;
249
0
            }
250
0
        }
251
7.23k
        std::vector<std::string> path_array;
252
7.23k
        split(path, &path_array);
253
7.23k
        if (path_array.empty()) {
254
0
            if (_root_value == nullptr) {
255
0
                return false;
256
0
            } else {
257
0
                _allocator.construct(value, *_root_value);
258
0
                return true;
259
0
            }
260
0
        }
261
7.23k
        int index = 0;
262
7.23k
        if (path_array[0].empty()) {
263
0
            index = 1;
264
0
        }
265
7.23k
        return _root.retrieve(path_array, index, value, params);
266
7.23k
    }
267
268
private:
269
7.78k
    void split(const std::string& path, std::vector<std::string>* array) {
270
7.78k
        const char* path_str = path.c_str();
271
7.78k
        std::size_t start = 0;
272
7.78k
        std::size_t pos = 0;
273
318k
        for (; pos < path.length(); ++pos) {
274
310k
            if (path_str[pos] == _separator) {
275
24.9k
                if (pos - start > 0) {
276
17.2k
                    array->push_back(path.substr(start, pos - start));
277
17.2k
                }
278
24.9k
                start = pos + 1;
279
24.9k
            }
280
310k
        }
281
7.78k
        if (pos - start > 0) {
282
7.78k
            array->push_back(path.substr(start, pos - start));
283
7.78k
        }
284
7.78k
    }
285
286
    TrieNode _root;
287
    T* _root_value;
288
    char _separator;
289
    Allocator _allocator;
290
};
291
292
} // namespace doris