Coverage Report

Created: 2026-03-18 17:20

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
36
    ~PathTrie() {
34
36
        if (_root_value != nullptr) {
35
4
            _allocator.destroy(_root_value);
36
4
            _allocator.deallocate(_root_value, 1);
37
4
        }
38
36
    }
39
40
    class Allocator {
41
    public:
42
        using value_type = T;
43
44
575
        T* allocate(size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
45
46
        template <typename... Args>
47
972
        void construct(T* p, Args&&... args) {
48
972
            new (p) T(std::forward<Args>(args)...);
49
972
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRKS2_EEEvPS2_DpOT_
Line
Count
Source
47
575
        void construct(T* p, Args&&... args) {
48
575
            new (p) T(std::forward<Args>(args)...);
49
575
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRS2_EEEvPS2_DpOT_
Line
Count
Source
47
397
        void construct(T* p, Args&&... args) {
48
397
            new (p) T(std::forward<Args>(args)...);
49
397
        }
50
51
320
        void destroy(T* p) { p->~T(); }
52
53
320
        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
308
                : _value(nullptr), _wildcard(wildcard) {
60
308
            if (is_named_wildcard(key)) {
61
35
                _named_wildcard = extract_template(key);
62
35
            }
63
308
        }
64
65
        TrieNode(const std::string& key, const T& value, const std::string& wildcard)
66
568
                : _value(nullptr), _wildcard(wildcard) {
67
568
            _value = _allocator.allocate(1);
68
568
            _allocator.construct(_value, value);
69
568
            if (is_named_wildcard(key)) {
70
70
                _named_wildcard = extract_template(key);
71
70
            }
72
568
        }
73
74
498
        ~TrieNode() {
75
498
            for (auto& iter : _children) {
76
462
                delete iter.second;
77
462
                iter.second = nullptr;
78
462
            }
79
498
            if (_value != nullptr) {
80
316
                _allocator.destroy(_value);
81
316
                _allocator.deallocate(_value, 1);
82
316
            }
83
498
        }
84
85
        // Return true if insert success.
86
1.35k
        bool insert(const std::vector<std::string> path, int index, const T& value) {
87
1.35k
            if (index >= path.size()) {
88
0
                return false;
89
0
            }
90
1.35k
            const std::string& token = path[index];
91
1.35k
            std::string key = token;
92
93
1.35k
            if (is_named_wildcard(token)) {
94
154
                key = _wildcard;
95
154
            }
96
97
1.35k
            TrieNode* node = get_child(key);
98
99
1.35k
            if (node == nullptr) {
100
                // no exist child for this key
101
822
                if (index == path.size() - 1) {
102
568
                    node = new TrieNode(token, value, _wildcard);
103
568
                    _children.insert(std::make_pair(key, node));
104
568
                    return true;
105
568
                } else {
106
254
                    node = new TrieNode(token, _wildcard);
107
254
                    _children.insert(std::make_pair(key, node));
108
254
                }
109
822
            } else {
110
                // If this is a template, set this to the node
111
537
                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
537
                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
537
            }
130
791
            return node->insert(path, index + 1, value);
131
1.35k
        }
132
133
        bool retrieve(const std::vector<std::string> path, int index, T* value,
134
1.30k
                      std::map<std::string, std::string>* params) {
135
            // check max index
136
1.30k
            if (index >= path.size()) {
137
0
                return false;
138
0
            }
139
1.30k
            bool use_wildcard = false;
140
1.30k
            const std::string& token = path[index];
141
1.30k
            TrieNode* node = get_child(token);
142
1.30k
            if (node == nullptr) {
143
514
                node = get_child(_wildcard);
144
514
                if (node == nullptr) {
145
1
                    return false;
146
1
                }
147
513
                use_wildcard = true;
148
793
            } else {
149
                // If we the last one, but we have no value, check wildcard
150
793
                if (index == path.size() - 1 && node->_value == nullptr &&
151
793
                    get_child(_wildcard) != nullptr) {
152
0
                    node = get_child(_wildcard);
153
0
                    use_wildcard = true;
154
793
                } else {
155
793
                    use_wildcard = (token.compare(_wildcard) == 0);
156
793
                }
157
793
            }
158
159
1.30k
            put(params, node, token);
160
161
1.30k
            if (index == path.size() - 1) {
162
397
                if (node->_value == nullptr) {
163
0
                    return false;
164
0
                }
165
397
                _allocator.construct(value, *node->_value);
166
397
                return true;
167
397
            }
168
169
            // find exact
170
909
            if (node->retrieve(path, index + 1, value, params)) {
171
909
                return true;
172
909
            }
173
174
            // backtrace to test if wildcard can match
175
0
            if (!use_wildcard) {
176
0
                node = get_child(_wildcard);
177
0
                if (node != nullptr) {
178
0
                    put(params, node, token);
179
0
                    return node->retrieve(path, index + 1, value, params);
180
0
                }
181
0
            }
182
0
            return false;
183
0
        }
184
185
    private:
186
2.77k
        bool is_named_wildcard(const std::string& key) {
187
2.77k
            if (key.find('{') != std::string::npos && key.find('}') != std::string::npos) {
188
308
                return true;
189
308
            }
190
2.46k
            return false;
191
2.77k
        }
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
3.18k
        TrieNode* get_child(const std::string& key) {
200
3.18k
            auto pair = _children.find(key);
201
3.18k
            if (pair == _children.end()) {
202
1.33k
                return nullptr;
203
1.33k
            }
204
1.84k
            return pair->second;
205
3.18k
        }
206
207
        void put(std::map<std::string, std::string>* params, TrieNode* node,
208
1.30k
                 const std::string& token) {
209
1.30k
            if (params != nullptr && !node->_named_wildcard.empty()) {
210
513
                params->insert(std::make_pair(node->_named_wildcard, token));
211
513
            }
212
1.30k
        }
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
575
    bool insert(const std::string& path, const T& value) {
222
575
        std::vector<std::string> path_array;
223
575
        split(path, &path_array);
224
575
        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
568
        int index = 0;
234
568
        if (path_array[0].empty()) {
235
0
            index = 1;
236
0
        }
237
568
        return _root.insert(path_array, index, value);
238
575
    }
239
240
    bool retrieve(const std::string& path, T* value) { return retrieve(path, value, nullptr); }
241
242
398
    bool retrieve(const std::string& path, T* value, std::map<std::string, std::string>* params) {
243
398
        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
398
        std::vector<std::string> path_array;
252
398
        split(path, &path_array);
253
398
        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
398
        int index = 0;
262
398
        if (path_array[0].empty()) {
263
0
            index = 1;
264
0
        }
265
398
        return _root.retrieve(path_array, index, value, params);
266
398
    }
267
268
private:
269
973
    void split(const std::string& path, std::vector<std::string>* array) {
270
973
        const char* path_str = path.c_str();
271
973
        std::size_t start = 0;
272
973
        std::size_t pos = 0;
273
30.3k
        for (; pos < path.length(); ++pos) {
274
29.3k
            if (path_str[pos] == _separator) {
275
2.65k
                if (pos - start > 0) {
276
1.70k
                    array->push_back(path.substr(start, pos - start));
277
1.70k
                }
278
2.65k
                start = pos + 1;
279
2.65k
            }
280
29.3k
        }
281
973
        if (pos - start > 0) {
282
966
            array->push_back(path.substr(start, pos - start));
283
966
        }
284
973
    }
285
286
    TrieNode _root;
287
    T* _root_value;
288
    char _separator;
289
    Allocator _allocator;
290
};
291
292
} // namespace doris