Coverage Report

Created: 2026-10-14 10:23

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
108
    PathTrie() : _root("/", "*"), _root_value(nullptr), _separator('/') {}
32
33
90
    ~PathTrie() {
34
90
        if (_root_value != nullptr) {
35
4
            _allocator.destroy(_root_value);
36
4
            _allocator.deallocate(_root_value, 1);
37
4
        }
38
90
    }
39
40
    class Allocator {
41
    public:
42
        using value_type = T;
43
44
586
        T* allocate(size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
45
46
        template <typename... Args>
47
675
        void construct(T* p, Args&&... args) {
48
675
            new (p) T(std::forward<Args>(args)...);
49
675
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRKS2_EEEvPS2_DpOT_
Line
Count
Source
47
586
        void construct(T* p, Args&&... args) {
48
586
            new (p) T(std::forward<Args>(args)...);
49
586
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRS2_EEEvPS2_DpOT_
Line
Count
Source
47
89
        void construct(T* p, Args&&... args) {
48
89
            new (p) T(std::forward<Args>(args)...);
49
89
        }
50
51
331
        void destroy(T* p) { p->~T(); }
52
53
331
        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
344
                : _value(nullptr), _wildcard(wildcard) {
60
344
            if (is_named_wildcard(key)) {
61
35
                _named_wildcard = extract_template(key);
62
35
            }
63
344
        }
64
65
        TrieNode(const std::string& key, const T& value, const std::string& wildcard)
66
579
                : _value(nullptr), _wildcard(wildcard) {
67
579
            _value = _allocator.allocate(1);
68
579
            _allocator.construct(_value, value);
69
579
            if (is_named_wildcard(key)) {
70
70
                _named_wildcard = extract_template(key);
71
70
            }
72
579
        }
73
74
554
        ~TrieNode() {
75
554
            for (auto& iter : _children) {
76
464
                delete iter.second;
77
464
                iter.second = nullptr;
78
464
            }
79
554
            if (_value != nullptr) {
80
327
                _allocator.destroy(_value);
81
327
                _allocator.deallocate(_value, 1);
82
327
            }
83
554
        }
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
815
                if (index == path.size() - 1) {
102
579
                    node = new TrieNode(token, value, _wildcard);
103
579
                    _children.insert(std::make_pair(key, node));
104
579
                    return true;
105
579
                } else {
106
236
                    node = new TrieNode(token, _wildcard);
107
236
                    _children.insert(std::make_pair(key, node));
108
236
                }
109
815
            } else {
110
                // If this is a template, set this to the node
111
544
                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
544
                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
544
            }
130
780
            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
191
                      std::map<std::string, std::string>* params) {
135
            // check max index
136
191
            if (index >= path.size()) {
137
0
                return false;
138
0
            }
139
191
            bool use_wildcard = false;
140
191
            const std::string& token = path[index];
141
191
            TrieNode* node = get_child(token);
142
191
            if (node == nullptr) {
143
26
                node = get_child(_wildcard);
144
26
                if (node == nullptr) {
145
1
                    return false;
146
1
                }
147
25
                use_wildcard = true;
148
165
            } else {
149
                // If we the last one, but we have no value, check wildcard
150
165
                if (index == path.size() - 1 && node->_value == nullptr &&
151
165
                    get_child(_wildcard) != nullptr) {
152
0
                    node = get_child(_wildcard);
153
0
                    use_wildcard = true;
154
165
                } else {
155
165
                    use_wildcard = (token.compare(_wildcard) == 0);
156
165
                }
157
165
            }
158
159
190
            put(params, node, token);
160
161
190
            if (index == path.size() - 1) {
162
89
                if (node->_value == nullptr) {
163
0
                    return false;
164
0
                }
165
89
                _allocator.construct(value, *node->_value);
166
89
                return true;
167
89
            }
168
169
            // find exact
170
101
            if (node->retrieve(path, index + 1, value, params)) {
171
101
                return true;
172
101
            }
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.82k
        bool is_named_wildcard(const std::string& key) {
187
2.82k
            if (key.find('{') != std::string::npos && key.find('}') != std::string::npos) {
188
308
                return true;
189
308
            }
190
2.51k
            return false;
191
2.82k
        }
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
1.57k
        TrieNode* get_child(const std::string& key) {
200
1.57k
            auto pair = _children.find(key);
201
1.57k
            if (pair == _children.end()) {
202
842
                return nullptr;
203
842
            }
204
734
            return pair->second;
205
1.57k
        }
206
207
        void put(std::map<std::string, std::string>* params, TrieNode* node,
208
190
                 const std::string& token) {
209
190
            if (params != nullptr && !node->_named_wildcard.empty()) {
210
                // The query string is parsed into the same map before routing runs, so
211
                // insert() would silently keep a "?db=" over the {db} the path matched.
212
25
                (*params)[node->_named_wildcard] = token;
213
25
            }
214
190
        }
215
216
        T* _value;
217
        std::string _wildcard;
218
        std::string _named_wildcard;
219
        std::map<std::string, TrieNode*> _children;
220
        Allocator _allocator;
221
    };
222
223
586
    bool insert(const std::string& path, const T& value) {
224
586
        std::vector<std::string> path_array;
225
586
        split(path, &path_array);
226
586
        if (path_array.empty()) {
227
7
            if (_root_value == nullptr) {
228
7
                _root_value = _allocator.allocate(1);
229
7
                _allocator.construct(_root_value, value);
230
7
                return true;
231
7
            } else {
232
0
                return false;
233
0
            }
234
7
        }
235
579
        int index = 0;
236
579
        if (path_array[0].empty()) {
237
0
            index = 1;
238
0
        }
239
579
        return _root.insert(path_array, index, value);
240
586
    }
241
242
    bool retrieve(const std::string& path, T* value) { return retrieve(path, value, nullptr); }
243
244
90
    bool retrieve(const std::string& path, T* value, std::map<std::string, std::string>* params) {
245
90
        if (path.empty()) {
246
0
            if (_root_value == nullptr) {
247
0
                return false;
248
0
            } else {
249
0
                _allocator.construct(value, *_root_value);
250
0
                return true;
251
0
            }
252
0
        }
253
90
        std::vector<std::string> path_array;
254
90
        split(path, &path_array);
255
90
        if (path_array.empty()) {
256
0
            if (_root_value == nullptr) {
257
0
                return false;
258
0
            } else {
259
0
                _allocator.construct(value, *_root_value);
260
0
                return true;
261
0
            }
262
0
        }
263
90
        int index = 0;
264
90
        if (path_array[0].empty()) {
265
0
            index = 1;
266
0
        }
267
90
        return _root.retrieve(path_array, index, value, params);
268
90
    }
269
270
private:
271
676
    void split(const std::string& path, std::vector<std::string>* array) {
272
676
        const char* path_str = path.c_str();
273
676
        std::size_t start = 0;
274
676
        std::size_t pos = 0;
275
13.9k
        for (; pos < path.length(); ++pos) {
276
13.2k
            if (path_str[pos] == _separator) {
277
1.55k
                if (pos - start > 0) {
278
881
                    array->push_back(path.substr(start, pos - start));
279
881
                }
280
1.55k
                start = pos + 1;
281
1.55k
            }
282
13.2k
        }
283
676
        if (pos - start > 0) {
284
669
            array->push_back(path.substr(start, pos - start));
285
669
        }
286
676
    }
287
288
    TrieNode _root;
289
    T* _root_value;
290
    char _separator;
291
    Allocator _allocator;
292
};
293
294
} // namespace doris