Debian Codegraph @

lib/dat/key-cursor.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2011 Brazil
3 Copyright (C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "key-cursor.hpp"
21
22 #include <algorithm>
23 #include <cstring>
24
25 #include "trie.hpp"
26
27 #include <groonga/portability.h>
28
29 namespace grn {
30 namespace dat {
31
32 KeyCursor::KeyCursor()
33 : trie_(NULL),
34 offset_(0),
35 limit_(MAX_UINT32),
36 flags_(KEY_RANGE_CURSOR),
37 buf_(),
38 count_(0),
39 max_count_(0),
40 finished_(false),
41 end_buf_(NULL),
42 end_str_() {}
43
44 KeyCursor::~KeyCursor() {
45 if (end_buf_ != NULL) {
46 delete [] end_buf_;
47 }
48 }
49
50 void KeyCursor::open(const Trie &trie,
51 const String &min_str,
52 const String &max_str,
53 UInt32 offset,
54 UInt32 limit,
55 UInt32 flags) {
56 GRN_DAT_THROW_IF(PARAM_ERROR,
57 (min_str.ptr() == NULL) && (min_str.length() != 0));
58 GRN_DAT_THROW_IF(PARAM_ERROR,
59 (max_str.ptr() == NULL) && (max_str.length() != 0));
60
61 flags = fix_flags(flags);
62 KeyCursor new_cursor(trie, offset, limit, flags);
63 new_cursor.init(min_str, max_str);
64 new_cursor.swap(this);
65 }
66
67 void KeyCursor::close() {
68 KeyCursor new_cursor;
69 new_cursor.swap(this);
70 }
71
72 const Key &KeyCursor::next() {
73 if (finished_ || (count_ >= max_count_)) {
74 return Key::invalid_key();
75 }
76
77 if ((flags_ & ASCENDING_CURSOR) == ASCENDING_CURSOR) {
78 return ascending_next();
79 } else {
80 return descending_next();
81 }
82 }
83
84 KeyCursor::KeyCursor(const Trie &trie,
85 UInt32 offset, UInt32 limit, UInt32 flags)
86 : trie_(&trie),
87 offset_(offset),
88 limit_(limit),
89 flags_(flags),
90 buf_(),
91 count_(0),
92 max_count_(0),
93 finished_(false),
94 end_buf_(NULL),
95 end_str_() {}
96
97 UInt32 KeyCursor::fix_flags(UInt32 flags) const {
98 const UInt32 cursor_type = flags & CURSOR_TYPE_MASK;
99 GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_type != 0) &&
100 (cursor_type != KEY_RANGE_CURSOR));
101 flags |= KEY_RANGE_CURSOR;
102
103 const UInt32 cursor_order = flags & CURSOR_ORDER_MASK;
104 GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_order != 0) &&
105 (cursor_order != ASCENDING_CURSOR) &&
106 (cursor_order != DESCENDING_CURSOR));
107 if (cursor_order == 0) {
108 flags |= ASCENDING_CURSOR;
109 }
110
111 const UInt32 cursor_options = flags & CURSOR_OPTIONS_MASK;
112 GRN_DAT_THROW_IF(PARAM_ERROR,
113 cursor_options & ~(EXCEPT_LOWER_BOUND | EXCEPT_UPPER_BOUND));
114
115 return flags;
116 }
117
118 void KeyCursor::init(const String &min_str, const String &max_str) {
119 if (offset_ > (MAX_UINT32 - limit_)) {
120 max_count_ = MAX_UINT32;
121 } else {
122 max_count_ = offset_ + limit_;
123 }
124
125 if (limit_ == 0) {
126 return;
127 }
128
129 if ((flags_ & ASCENDING_CURSOR) == ASCENDING_CURSOR) {
130 ascending_init(min_str, max_str);
131 } else {
132 descending_init(min_str, max_str);
133 }
134 }
135
136 void KeyCursor::ascending_init(const String &min_str, const String &max_str) {
137 if (max_str.ptr() != NULL) {
138 if (max_str.length() != 0) {
139 end_buf_ = new UInt8[max_str.length()];
140 grn_memcpy(end_buf_, max_str.ptr(), max_str.length());
141 end_str_.assign(end_buf_, max_str.length());
142 }
143 }
144
145 if ((min_str.ptr() == NULL) || (min_str.length() == 0)) {
146 buf_.push_back(ROOT_NODE_ID);
147 return;
148 }
149
150 UInt32 node_id = ROOT_NODE_ID;
151 Node node;
152 for (UInt32 i = 0; i < min_str.length(); ++i) {
153 node = trie_->ith_node(node_id);
154 if (node.is_linker()) {
155 const Key &key = trie_->get_key(node.key_pos());
156 const int result = key.str().compare(min_str, i);
157 if ((result > 0) || ((result == 0) &&
158 ((flags_ & EXCEPT_LOWER_BOUND) != EXCEPT_LOWER_BOUND))) {
159 buf_.push_back(node_id);
160 } else if (node.sibling() != INVALID_LABEL) {
161 buf_.push_back(node_id ^ node.label() ^ node.sibling());
162 }
163 return;
164 } else if (node.sibling() != INVALID_LABEL) {
165 buf_.push_back(node_id ^ node.label() ^ node.sibling());
166 }
167
168 node_id = node.offset() ^ min_str[i];
169 if (trie_->ith_node(node_id).label() != min_str[i]) {
170 UInt32 label = node.child();
171 if (label == TERMINAL_LABEL) {
172 label = trie_->ith_node(node.offset() ^ label).sibling();
173 }
174 while (label != INVALID_LABEL) {
175 if (label > min_str[i]) {
176 buf_.push_back(node.offset() ^ label);
177 break;
178 }
179 label = trie_->ith_node(node.offset() ^ label).sibling();
180 }
181 return;
182 }
183 }
184
185 node = trie_->ith_node(node_id);
186 if (node.is_linker()) {
187 const Key &key = trie_->get_key(node.key_pos());
188 if ((key.length() != min_str.length()) ||
189 ((flags_ & EXCEPT_LOWER_BOUND) != EXCEPT_LOWER_BOUND)) {
190 buf_.push_back(node_id);
191 } else if (node.sibling() != INVALID_LABEL) {
192 buf_.push_back(node_id ^ node.label() ^ node.sibling());
193 }
194 return;
195 } else if (node.sibling() != INVALID_LABEL) {
196 buf_.push_back(node_id ^ node.label() ^ node.sibling());
197 }
198
199 UInt32 label = node.child();
200 if ((label == TERMINAL_LABEL) &&
201 ((flags_ & EXCEPT_LOWER_BOUND) == EXCEPT_LOWER_BOUND)) {
202 label = trie_->ith_node(node.offset() ^ label).sibling();
203 }
204 if (label != INVALID_LABEL) {
205 buf_.push_back(node.offset() ^ label);
206 }
207 }
208
209 void KeyCursor::descending_init(const String &min_str, const String &max_str) {
210 if (min_str.ptr() != NULL) {
211 if (min_str.length() != 0) {
212 end_buf_ = new UInt8[min_str.length()];
213 grn_memcpy(end_buf_, min_str.ptr(), min_str.length());
214 end_str_.assign(end_buf_, min_str.length());
215 }
216 }
217
218 if ((max_str.ptr() == NULL) || (max_str.length() == 0)) {
219 buf_.push_back(ROOT_NODE_ID);
220 return;
221 }
222
223 UInt32 node_id = ROOT_NODE_ID;
224 for (UInt32 i = 0; i < max_str.length(); ++i) {
225 const Base base = trie_->ith_node(node_id).base();
226 if (base.is_linker()) {
227 const Key &key = trie_->get_key(base.key_pos());
228 const int result = key.str().compare(max_str, i);
229 if ((result < 0) || ((result == 0) &&
230 ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND))) {
231 buf_.push_back(node_id | POST_ORDER_FLAG);
232 }
233 return;
234 }
235
236 UInt32 label = trie_->ith_node(node_id).child();
237 if (label == TERMINAL_LABEL) {
238 node_id = base.offset() ^ label;
239 buf_.push_back(node_id | POST_ORDER_FLAG);
240 label = trie_->ith_node(node_id).sibling();
241 }
242 while (label != INVALID_LABEL) {
243 node_id = base.offset() ^ label;
244 if (label < max_str[i]) {
245 buf_.push_back(node_id);
246 } else if (label > max_str[i]) {
247 return;
248 } else {
249 break;
250 }
251 label = trie_->ith_node(node_id).sibling();
252 }
253 if (label == INVALID_LABEL) {
254 return;
255 }
256 }
257
258 const Base base = trie_->ith_node(node_id).base();
259 if (base.is_linker()) {
260 const Key &key = trie_->get_key(base.key_pos());
261 if ((key.length() == max_str.length()) &&
262 ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND)) {
263 buf_.push_back(node_id | POST_ORDER_FLAG);
264 }
265 return;
266 }
267
268 UInt32 label = trie_->ith_node(node_id).child();
269 if ((label == TERMINAL_LABEL) &&
270 ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND)) {
271 buf_.push_back((base.offset() ^ label) | POST_ORDER_FLAG);
272 }
273 }
274
275 void KeyCursor::swap(KeyCursor *cursor) {
276 std::swap(trie_, cursor->trie_);
277 std::swap(offset_, cursor->offset_);
278 std::swap(limit_, cursor->limit_);
279 std::swap(flags_, cursor->flags_);
280 buf_.swap(&cursor->buf_);
281 std::swap(count_, cursor->count_);
282 std::swap(max_count_, cursor->max_count_);
283 std::swap(finished_, cursor->finished_);
284 std::swap(end_buf_, cursor->end_buf_);
285 end_str_.swap(&cursor->end_str_);
286 }
287
288 const Key &KeyCursor::ascending_next() {
289 while (!buf_.empty()) {
290 const UInt32 node_id = buf_.back();
291 buf_.pop_back();
292
293 const Node node = trie_->ith_node(node_id);
294 if (node.sibling() != INVALID_LABEL) {
295 buf_.push_back(node_id ^ node.label() ^ node.sibling());
296 }
297
298 if (node.is_linker()) {
299 const Key &key = trie_->get_key(node.key_pos());
300 if (end_buf_ != NULL) {
301 const int result = key.str().compare(end_str_);
302 if ((result > 0) || ((result == 0) &&
303 ((flags_ & EXCEPT_UPPER_BOUND) == EXCEPT_UPPER_BOUND))) {
304 finished_ = true;
305 return Key::invalid_key();
306 }
307 }
308 if (count_++ >= offset_) {
309 return key;
310 }
311 } else if (node.child() != INVALID_LABEL) {
312 buf_.push_back(node.offset() ^ node.child());
313 }
314 }
315 return Key::invalid_key();
316 }
317
318 const Key &KeyCursor::descending_next() {
319 while (!buf_.empty()) {
320 const bool post_order = (buf_.back() & POST_ORDER_FLAG) == POST_ORDER_FLAG;
321 const UInt32 node_id = buf_.back() & ~POST_ORDER_FLAG;
322
323 const Base base = trie_->ith_node(node_id).base();
324 if (post_order) {
325 buf_.pop_back();
326 if (base.is_linker()) {
327 const Key &key = trie_->get_key(base.key_pos());
328 if (end_buf_ != NULL) {
329 const int result = key.str().compare(end_str_);
330 if ((result < 0) || ((result == 0) &&
331 ((flags_ & EXCEPT_LOWER_BOUND) == EXCEPT_LOWER_BOUND))) {
332 finished_ = true;
333 return Key::invalid_key();
334 }
335 }
336 if (count_++ >= offset_) {
337 return key;
338 }
339 }
340 } else {
341 buf_.back() |= POST_ORDER_FLAG;
342 UInt32 label = trie_->ith_node(node_id).child();
343 while (label != INVALID_LABEL) {
344 buf_.push_back(base.offset() ^ label);
345 label = trie_->ith_node(base.offset() ^ label).sibling();
346 }
347 }
348 }
349 return Key::invalid_key();
350 }
351
352 } // namespace dat
353 } // namespace grn