Coverage Report

Created: 2026-10-09 17:38

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
278
    PathTrie() : _root("/", "*"), _root_value(nullptr), _separator('/') {}
_ZN5doris8PathTrieIiEC2Ev
Line
Count
Source
31
8
    PathTrie() : _root("/", "*"), _root_value(nullptr), _separator('/') {}
_ZN5doris8PathTrieIPNS_11HttpHandlerEEC2Ev
Line
Count
Source
31
270
    PathTrie() : _root("/", "*"), _root_value(nullptr), _separator('/') {}
32
33
278
    ~PathTrie() {
34
278
        if (_root_value != nullptr) {
35
2
            _allocator.destroy(_root_value);
36
2
            _allocator.deallocate(_root_value, 1);
37
2
        }
38
278
    }
_ZN5doris8PathTrieIiED2Ev
Line
Count
Source
33
8
    ~PathTrie() {
34
8
        if (_root_value != nullptr) {
35
1
            _allocator.destroy(_root_value);
36
1
            _allocator.deallocate(_root_value, 1);
37
1
        }
38
8
    }
_ZN5doris8PathTrieIPNS_11HttpHandlerEED2Ev
Line
Count
Source
33
270
    ~PathTrie() {
34
270
        if (_root_value != nullptr) {
35
1
            _allocator.destroy(_root_value);
36
1
            _allocator.deallocate(_root_value, 1);
37
1
        }
38
270
    }
39
40
    class Allocator {
41
    public:
42
        using value_type = T;
43
44
151
        T* allocate(size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
_ZN5doris8PathTrieIiE9Allocator8allocateEm
Line
Count
Source
44
12
        T* allocate(size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator8allocateEm
Line
Count
Source
44
139
        T* allocate(size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
45
46
        template <typename... Args>
47
384
        void construct(T* p, Args&&... args) {
48
384
            new (p) T(std::forward<Args>(args)...);
49
384
        }
_ZN5doris8PathTrieIiE9Allocator9constructIJRKiEEEvPiDpOT_
Line
Count
Source
47
12
        void construct(T* p, Args&&... args) {
48
12
            new (p) T(std::forward<Args>(args)...);
49
12
        }
_ZN5doris8PathTrieIiE9Allocator9constructIJRiEEEvPiDpOT_
Line
Count
Source
47
13
        void construct(T* p, Args&&... args) {
48
13
            new (p) T(std::forward<Args>(args)...);
49
13
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRKS2_EEEvPS2_DpOT_
Line
Count
Source
47
139
        void construct(T* p, Args&&... args) {
48
139
            new (p) T(std::forward<Args>(args)...);
49
139
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator9constructIJRS2_EEEvPS2_DpOT_
Line
Count
Source
47
220
        void construct(T* p, Args&&... args) {
48
220
            new (p) T(std::forward<Args>(args)...);
49
220
        }
50
51
151
        void destroy(T* p) { p->~T(); }
_ZN5doris8PathTrieIiE9Allocator7destroyEPi
Line
Count
Source
51
12
        void destroy(T* p) { p->~T(); }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator7destroyEPS2_
Line
Count
Source
51
139
        void destroy(T* p) { p->~T(); }
52
53
151
        void deallocate(T* p, size_t n) { ::operator delete(p); }
_ZN5doris8PathTrieIiE9Allocator10deallocateEPim
Line
Count
Source
53
12
        void deallocate(T* p, size_t n) { ::operator delete(p); }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE9Allocator10deallocateEPS2_m
Line
Count
Source
53
139
        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
327
                : _value(nullptr), _wildcard(wildcard) {
60
327
            if (is_named_wildcard(key)) {
61
9
                _named_wildcard = extract_template(key);
62
9
            }
63
327
        }
_ZN5doris8PathTrieIiE8TrieNodeC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_
Line
Count
Source
59
19
                : _value(nullptr), _wildcard(wildcard) {
60
19
            if (is_named_wildcard(key)) {
61
4
                _named_wildcard = extract_template(key);
62
4
            }
63
19
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNodeC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_
Line
Count
Source
59
308
                : _value(nullptr), _wildcard(wildcard) {
60
308
            if (is_named_wildcard(key)) {
61
5
                _named_wildcard = extract_template(key);
62
5
            }
63
308
        }
64
65
        TrieNode(const std::string& key, const T& value, const std::string& wildcard)
66
148
                : _value(nullptr), _wildcard(wildcard) {
67
148
            _value = _allocator.allocate(1);
68
148
            _allocator.construct(_value, value);
69
148
            if (is_named_wildcard(key)) {
70
12
                _named_wildcard = extract_template(key);
71
12
            }
72
148
        }
_ZN5doris8PathTrieIiE8TrieNodeC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKiSA_
Line
Count
Source
66
10
                : _value(nullptr), _wildcard(wildcard) {
67
10
            _value = _allocator.allocate(1);
68
10
            _allocator.construct(_value, value);
69
10
            if (is_named_wildcard(key)) {
70
2
                _named_wildcard = extract_template(key);
71
2
            }
72
10
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNodeC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKS2_SC_
Line
Count
Source
66
138
                : _value(nullptr), _wildcard(wildcard) {
67
138
            _value = _allocator.allocate(1);
68
138
            _allocator.construct(_value, value);
69
138
            if (is_named_wildcard(key)) {
70
10
                _named_wildcard = extract_template(key);
71
10
            }
72
138
        }
73
74
475
        ~TrieNode() {
75
475
            for (auto& iter : _children) {
76
197
                delete iter.second;
77
197
                iter.second = nullptr;
78
197
            }
79
475
            if (_value != nullptr) {
80
149
                _allocator.destroy(_value);
81
149
                _allocator.deallocate(_value, 1);
82
149
            }
83
475
        }
_ZN5doris8PathTrieIiE8TrieNodeD2Ev
Line
Count
Source
74
29
        ~TrieNode() {
75
29
            for (auto& iter : _children) {
76
21
                delete iter.second;
77
21
                iter.second = nullptr;
78
21
            }
79
29
            if (_value != nullptr) {
80
11
                _allocator.destroy(_value);
81
11
                _allocator.deallocate(_value, 1);
82
11
            }
83
29
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNodeD2Ev
Line
Count
Source
74
446
        ~TrieNode() {
75
446
            for (auto& iter : _children) {
76
176
                delete iter.second;
77
176
                iter.second = nullptr;
78
176
            }
79
446
            if (_value != nullptr) {
80
138
                _allocator.destroy(_value);
81
138
                _allocator.deallocate(_value, 1);
82
138
            }
83
446
        }
84
85
        // Return true if insert success.
86
256
        bool insert(const std::vector<std::string> path, int index, const T& value) {
87
256
            if (index >= path.size()) {
88
0
                return false;
89
0
            }
90
256
            const std::string& token = path[index];
91
256
            std::string key = token;
92
93
256
            if (is_named_wildcard(token)) {
94
30
                key = _wildcard;
95
30
            }
96
97
256
            TrieNode* node = get_child(key);
98
99
256
            if (node == nullptr) {
100
                // no exist child for this key
101
197
                if (index == path.size() - 1) {
102
148
                    node = new TrieNode(token, value, _wildcard);
103
148
                    _children.insert(std::make_pair(key, node));
104
148
                    return true;
105
148
                } else {
106
49
                    node = new TrieNode(token, _wildcard);
107
49
                    _children.insert(std::make_pair(key, node));
108
49
                }
109
197
            } else {
110
                // If this is a template, set this to the node
111
59
                if (is_named_wildcard(token)) {
112
9
                    std::string temp = extract_template(token);
113
9
                    if (node->_named_wildcard.empty() || node->_named_wildcard.compare(temp) == 0) {
114
8
                        node->_named_wildcard = temp;
115
8
                    } else {
116
                        // Duplicated
117
1
                        return false;
118
1
                    }
119
9
                }
120
58
                if (index == path.size() - 1) {
121
2
                    if (node->_value == nullptr) {
122
1
                        node->_value = _allocator.allocate(1);
123
1
                        _allocator.construct(node->_value, value);
124
1
                        return true;
125
1
                    }
126
                    // Already register by other path
127
1
                    return false;
128
2
                }
129
58
            }
130
105
            return node->insert(path, index + 1, value);
131
256
        }
_ZN5doris8PathTrieIiE8TrieNode6insertESt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EEiRKi
Line
Count
Source
86
34
        bool insert(const std::vector<std::string> path, int index, const T& value) {
87
34
            if (index >= path.size()) {
88
0
                return false;
89
0
            }
90
34
            const std::string& token = path[index];
91
34
            std::string key = token;
92
93
34
            if (is_named_wildcard(token)) {
94
8
                key = _wildcard;
95
8
            }
96
97
34
            TrieNode* node = get_child(key);
98
99
34
            if (node == nullptr) {
100
                // no exist child for this key
101
21
                if (index == path.size() - 1) {
102
10
                    node = new TrieNode(token, value, _wildcard);
103
10
                    _children.insert(std::make_pair(key, node));
104
10
                    return true;
105
11
                } else {
106
11
                    node = new TrieNode(token, _wildcard);
107
11
                    _children.insert(std::make_pair(key, node));
108
11
                }
109
21
            } else {
110
                // If this is a template, set this to the node
111
13
                if (is_named_wildcard(token)) {
112
2
                    std::string temp = extract_template(token);
113
2
                    if (node->_named_wildcard.empty() || node->_named_wildcard.compare(temp) == 0) {
114
1
                        node->_named_wildcard = temp;
115
1
                    } else {
116
                        // Duplicated
117
1
                        return false;
118
1
                    }
119
2
                }
120
12
                if (index == path.size() - 1) {
121
2
                    if (node->_value == nullptr) {
122
1
                        node->_value = _allocator.allocate(1);
123
1
                        _allocator.construct(node->_value, value);
124
1
                        return true;
125
1
                    }
126
                    // Already register by other path
127
1
                    return false;
128
2
                }
129
12
            }
130
21
            return node->insert(path, index + 1, value);
131
34
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNode6insertESt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEiRKS2_
Line
Count
Source
86
222
        bool insert(const std::vector<std::string> path, int index, const T& value) {
87
222
            if (index >= path.size()) {
88
0
                return false;
89
0
            }
90
222
            const std::string& token = path[index];
91
222
            std::string key = token;
92
93
222
            if (is_named_wildcard(token)) {
94
22
                key = _wildcard;
95
22
            }
96
97
222
            TrieNode* node = get_child(key);
98
99
222
            if (node == nullptr) {
100
                // no exist child for this key
101
176
                if (index == path.size() - 1) {
102
138
                    node = new TrieNode(token, value, _wildcard);
103
138
                    _children.insert(std::make_pair(key, node));
104
138
                    return true;
105
138
                } else {
106
38
                    node = new TrieNode(token, _wildcard);
107
38
                    _children.insert(std::make_pair(key, node));
108
38
                }
109
176
            } else {
110
                // If this is a template, set this to the node
111
46
                if (is_named_wildcard(token)) {
112
7
                    std::string temp = extract_template(token);
113
7
                    if (node->_named_wildcard.empty() || node->_named_wildcard.compare(temp) == 0) {
114
7
                        node->_named_wildcard = temp;
115
7
                    } else {
116
                        // Duplicated
117
0
                        return false;
118
0
                    }
119
7
                }
120
46
                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
46
            }
130
84
            return node->insert(path, index + 1, value);
131
222
        }
132
133
        bool retrieve(const std::vector<std::string> path, int index, T* value,
134
337
                      std::map<std::string, std::string>* params) {
135
            // check max index
136
337
            if (index >= path.size()) {
137
0
                return false;
138
0
            }
139
337
            bool use_wildcard = false;
140
337
            const std::string& token = path[index];
141
337
            TrieNode* node = get_child(token);
142
337
            if (node == nullptr) {
143
25
                node = get_child(_wildcard);
144
25
                if (node == nullptr) {
145
3
                    return false;
146
3
                }
147
22
                use_wildcard = true;
148
312
            } else {
149
                // If we the last one, but we have no value, check wildcard
150
312
                if (index == path.size() - 1 && node->_value == nullptr &&
151
312
                    get_child(_wildcard) != nullptr) {
152
0
                    node = get_child(_wildcard);
153
0
                    use_wildcard = true;
154
312
                } else {
155
312
                    use_wildcard = (token.compare(_wildcard) == 0);
156
312
                }
157
312
            }
158
159
334
            put(params, node, token);
160
161
334
            if (index == path.size() - 1) {
162
231
                if (node->_value == nullptr) {
163
0
                    return false;
164
0
                }
165
231
                _allocator.construct(value, *node->_value);
166
231
                return true;
167
231
            }
168
169
            // find exact
170
103
            if (node->retrieve(path, index + 1, value, params)) {
171
101
                return true;
172
101
            }
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
        }
_ZN5doris8PathTrieIiE8TrieNode8retrieveESt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EEiPiPSt3mapIS9_S9_St4lessIS9_ESaISt4pairIKS9_S9_EEE
Line
Count
Source
134
33
                      std::map<std::string, std::string>* params) {
135
            // check max index
136
33
            if (index >= path.size()) {
137
0
                return false;
138
0
            }
139
33
            bool use_wildcard = false;
140
33
            const std::string& token = path[index];
141
33
            TrieNode* node = get_child(token);
142
33
            if (node == nullptr) {
143
11
                node = get_child(_wildcard);
144
11
                if (node == nullptr) {
145
2
                    return false;
146
2
                }
147
9
                use_wildcard = true;
148
22
            } else {
149
                // If we the last one, but we have no value, check wildcard
150
22
                if (index == path.size() - 1 && node->_value == nullptr &&
151
22
                    get_child(_wildcard) != nullptr) {
152
0
                    node = get_child(_wildcard);
153
0
                    use_wildcard = true;
154
22
                } else {
155
22
                    use_wildcard = (token.compare(_wildcard) == 0);
156
22
                }
157
22
            }
158
159
31
            put(params, node, token);
160
161
31
            if (index == path.size() - 1) {
162
11
                if (node->_value == nullptr) {
163
0
                    return false;
164
0
                }
165
11
                _allocator.construct(value, *node->_value);
166
11
                return true;
167
11
            }
168
169
            // find exact
170
20
            if (node->retrieve(path, index + 1, value, params)) {
171
18
                return true;
172
18
            }
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
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNode8retrieveESt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEiPS2_PSt3mapISB_SB_St4lessISB_ESaISt4pairIKSB_SB_EEE
Line
Count
Source
134
304
                      std::map<std::string, std::string>* params) {
135
            // check max index
136
304
            if (index >= path.size()) {
137
0
                return false;
138
0
            }
139
304
            bool use_wildcard = false;
140
304
            const std::string& token = path[index];
141
304
            TrieNode* node = get_child(token);
142
304
            if (node == nullptr) {
143
14
                node = get_child(_wildcard);
144
14
                if (node == nullptr) {
145
1
                    return false;
146
1
                }
147
13
                use_wildcard = true;
148
290
            } else {
149
                // If we the last one, but we have no value, check wildcard
150
290
                if (index == path.size() - 1 && node->_value == nullptr &&
151
290
                    get_child(_wildcard) != nullptr) {
152
0
                    node = get_child(_wildcard);
153
0
                    use_wildcard = true;
154
290
                } else {
155
290
                    use_wildcard = (token.compare(_wildcard) == 0);
156
290
                }
157
290
            }
158
159
303
            put(params, node, token);
160
161
303
            if (index == path.size() - 1) {
162
220
                if (node->_value == nullptr) {
163
0
                    return false;
164
0
                }
165
220
                _allocator.construct(value, *node->_value);
166
220
                return true;
167
220
            }
168
169
            // find exact
170
83
            if (node->retrieve(path, index + 1, value, params)) {
171
83
                return true;
172
83
            }
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
790
        bool is_named_wildcard(const std::string& key) {
187
790
            if (key.find('{') != std::string::npos && key.find('}') != std::string::npos) {
188
60
                return true;
189
60
            }
190
730
            return false;
191
790
        }
_ZN5doris8PathTrieIiE8TrieNode17is_named_wildcardERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
186
76
        bool is_named_wildcard(const std::string& key) {
187
76
            if (key.find('{') != std::string::npos && key.find('}') != std::string::npos) {
188
16
                return true;
189
16
            }
190
60
            return false;
191
76
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNode17is_named_wildcardERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
186
714
        bool is_named_wildcard(const std::string& key) {
187
714
            if (key.find('{') != std::string::npos && key.find('}') != std::string::npos) {
188
44
                return true;
189
44
            }
190
670
            return false;
191
714
        }
192
193
30
        std::string extract_template(const std::string& key) {
194
30
            std::size_t left = key.find_first_of('{') + 1;
195
30
            std::size_t right = key.find_last_of('}');
196
30
            return key.substr(left, right - left);
197
30
        }
_ZN5doris8PathTrieIiE8TrieNode16extract_templateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
193
8
        std::string extract_template(const std::string& key) {
194
8
            std::size_t left = key.find_first_of('{') + 1;
195
8
            std::size_t right = key.find_last_of('}');
196
8
            return key.substr(left, right - left);
197
8
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNode16extract_templateERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
193
22
        std::string extract_template(const std::string& key) {
194
22
            std::size_t left = key.find_first_of('{') + 1;
195
22
            std::size_t right = key.find_last_of('}');
196
22
            return key.substr(left, right - left);
197
22
        }
198
199
620
        TrieNode* get_child(const std::string& key) {
200
620
            auto pair = _children.find(key);
201
620
            if (pair == _children.end()) {
202
227
                return nullptr;
203
227
            }
204
393
            return pair->second;
205
620
        }
_ZN5doris8PathTrieIiE8TrieNode9get_childERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
199
80
        TrieNode* get_child(const std::string& key) {
200
80
            auto pair = _children.find(key);
201
80
            if (pair == _children.end()) {
202
36
                return nullptr;
203
36
            }
204
44
            return pair->second;
205
80
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNode9get_childERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
199
540
        TrieNode* get_child(const std::string& key) {
200
540
            auto pair = _children.find(key);
201
540
            if (pair == _children.end()) {
202
191
                return nullptr;
203
191
            }
204
349
            return pair->second;
205
540
        }
206
207
        void put(std::map<std::string, std::string>* params, TrieNode* node,
208
334
                 const std::string& token) {
209
334
            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
19
                (*params)[node->_named_wildcard] = token;
213
19
            }
214
334
        }
_ZN5doris8PathTrieIiE8TrieNode3putEPSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9_St4lessIS9_ESaISt4pairIKS9_S9_EEEPS2_RSD_
Line
Count
Source
208
31
                 const std::string& token) {
209
31
            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
6
                (*params)[node->_named_wildcard] = token;
213
6
            }
214
31
        }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8TrieNode3putEPSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_St4lessISB_ESaISt4pairIKSB_SB_EEEPS4_RSF_
Line
Count
Source
208
303
                 const std::string& token) {
209
303
            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
13
                (*params)[node->_named_wildcard] = token;
213
13
            }
214
303
        }
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
154
    bool insert(const std::string& path, const T& value) {
224
154
        std::vector<std::string> path_array;
225
154
        split(path, &path_array);
226
154
        if (path_array.empty()) {
227
3
            if (_root_value == nullptr) {
228
2
                _root_value = _allocator.allocate(1);
229
2
                _allocator.construct(_root_value, value);
230
2
                return true;
231
2
            } else {
232
1
                return false;
233
1
            }
234
3
        }
235
151
        int index = 0;
236
151
        if (path_array[0].empty()) {
237
0
            index = 1;
238
0
        }
239
151
        return _root.insert(path_array, index, value);
240
154
    }
_ZN5doris8PathTrieIiE6insertERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKi
Line
Count
Source
223
15
    bool insert(const std::string& path, const T& value) {
224
15
        std::vector<std::string> path_array;
225
15
        split(path, &path_array);
226
15
        if (path_array.empty()) {
227
2
            if (_root_value == nullptr) {
228
1
                _root_value = _allocator.allocate(1);
229
1
                _allocator.construct(_root_value, value);
230
1
                return true;
231
1
            } else {
232
1
                return false;
233
1
            }
234
2
        }
235
13
        int index = 0;
236
13
        if (path_array[0].empty()) {
237
0
            index = 1;
238
0
        }
239
13
        return _root.insert(path_array, index, value);
240
15
    }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE6insertERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKS2_
Line
Count
Source
223
139
    bool insert(const std::string& path, const T& value) {
224
139
        std::vector<std::string> path_array;
225
139
        split(path, &path_array);
226
139
        if (path_array.empty()) {
227
1
            if (_root_value == nullptr) {
228
1
                _root_value = _allocator.allocate(1);
229
1
                _allocator.construct(_root_value, value);
230
1
                return true;
231
1
            } else {
232
0
                return false;
233
0
            }
234
1
        }
235
138
        int index = 0;
236
138
        if (path_array[0].empty()) {
237
0
            index = 1;
238
0
        }
239
138
        return _root.insert(path_array, index, value);
240
139
    }
241
242
9
    bool retrieve(const std::string& path, T* value) { return retrieve(path, value, nullptr); }
243
244
236
    bool retrieve(const std::string& path, T* value, std::map<std::string, std::string>* params) {
245
236
        if (path.empty()) {
246
1
            if (_root_value == nullptr) {
247
0
                return false;
248
1
            } else {
249
1
                _allocator.construct(value, *_root_value);
250
1
                return true;
251
1
            }
252
1
        }
253
235
        std::vector<std::string> path_array;
254
235
        split(path, &path_array);
255
235
        if (path_array.empty()) {
256
1
            if (_root_value == nullptr) {
257
0
                return false;
258
1
            } else {
259
1
                _allocator.construct(value, *_root_value);
260
1
                return true;
261
1
            }
262
1
        }
263
234
        int index = 0;
264
234
        if (path_array[0].empty()) {
265
0
            index = 1;
266
0
        }
267
234
        return _root.retrieve(path_array, index, value, params);
268
235
    }
_ZN5doris8PathTrieIiE8retrieveERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPiPSt3mapIS7_S7_St4lessIS7_ESaISt4pairIS8_S7_EEE
Line
Count
Source
244
15
    bool retrieve(const std::string& path, T* value, std::map<std::string, std::string>* params) {
245
15
        if (path.empty()) {
246
1
            if (_root_value == nullptr) {
247
0
                return false;
248
1
            } else {
249
1
                _allocator.construct(value, *_root_value);
250
1
                return true;
251
1
            }
252
1
        }
253
14
        std::vector<std::string> path_array;
254
14
        split(path, &path_array);
255
14
        if (path_array.empty()) {
256
1
            if (_root_value == nullptr) {
257
0
                return false;
258
1
            } else {
259
1
                _allocator.construct(value, *_root_value);
260
1
                return true;
261
1
            }
262
1
        }
263
13
        int index = 0;
264
13
        if (path_array[0].empty()) {
265
0
            index = 1;
266
0
        }
267
13
        return _root.retrieve(path_array, index, value, params);
268
14
    }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE8retrieveERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPS2_PSt3mapIS9_S9_St4lessIS9_ESaISt4pairISA_S9_EEE
Line
Count
Source
244
221
    bool retrieve(const std::string& path, T* value, std::map<std::string, std::string>* params) {
245
221
        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
221
        std::vector<std::string> path_array;
254
221
        split(path, &path_array);
255
221
        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
221
        int index = 0;
264
221
        if (path_array[0].empty()) {
265
0
            index = 1;
266
0
        }
267
221
        return _root.retrieve(path_array, index, value, params);
268
221
    }
269
270
private:
271
393
    void split(const std::string& path, std::vector<std::string>* array) {
272
393
        const char* path_str = path.c_str();
273
393
        std::size_t start = 0;
274
393
        std::size_t pos = 0;
275
4.15k
        for (; pos < path.length(); ++pos) {
276
3.76k
            if (path_str[pos] == _separator) {
277
608
                if (pos - start > 0) {
278
215
                    array->push_back(path.substr(start, pos - start));
279
215
                }
280
608
                start = pos + 1;
281
608
            }
282
3.76k
        }
283
393
        if (pos - start > 0) {
284
387
            array->push_back(path.substr(start, pos - start));
285
387
        }
286
393
    }
_ZN5doris8PathTrieIiE5splitERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPSt6vectorIS7_SaIS7_EE
Line
Count
Source
271
33
    void split(const std::string& path, std::vector<std::string>* array) {
272
33
        const char* path_str = path.c_str();
273
33
        std::size_t start = 0;
274
33
        std::size_t pos = 0;
275
403
        for (; pos < path.length(); ++pos) {
276
370
            if (path_str[pos] == _separator) {
277
82
                if (pos - start > 0) {
278
48
                    array->push_back(path.substr(start, pos - start));
279
48
                }
280
82
                start = pos + 1;
281
82
            }
282
370
        }
283
33
        if (pos - start > 0) {
284
28
            array->push_back(path.substr(start, pos - start));
285
28
        }
286
33
    }
_ZN5doris8PathTrieIPNS_11HttpHandlerEE5splitERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPSt6vectorIS9_SaIS9_EE
Line
Count
Source
271
360
    void split(const std::string& path, std::vector<std::string>* array) {
272
360
        const char* path_str = path.c_str();
273
360
        std::size_t start = 0;
274
360
        std::size_t pos = 0;
275
3.75k
        for (; pos < path.length(); ++pos) {
276
3.39k
            if (path_str[pos] == _separator) {
277
526
                if (pos - start > 0) {
278
167
                    array->push_back(path.substr(start, pos - start));
279
167
                }
280
526
                start = pos + 1;
281
526
            }
282
3.39k
        }
283
360
        if (pos - start > 0) {
284
359
            array->push_back(path.substr(start, pos - start));
285
359
        }
286
360
    }
287
288
    TrieNode _root;
289
    T* _root_value;
290
    char _separator;
291
    Allocator _allocator;
292
};
293
294
} // namespace doris