1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* Copyright(C) 2011 Brazil |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License version 2.1 as published by the Free Software Foundation. |
7 | |
8 | This library is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | Lesser General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU Lesser General Public |
14 | License along with this library; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
16 | */ |
17 | |
18 | #include "cursor-factory.hpp" |
19 | #include "id-cursor.hpp" |
20 | #include "key-cursor.hpp" |
21 | #include "prefix-cursor.hpp" |
22 | #include "predictive-cursor.hpp" |
23 | |
24 | #include <new> |
25 | |
26 | namespace grn { |
27 | namespace dat { |
28 | |
29 | Cursor *CursorFactory::open(const Trie &trie, |
30 | const void *min_ptr, UInt32 min_length, |
31 | const void *max_ptr, UInt32 max_length, |
32 | UInt32 offset, |
33 | UInt32 limit, |
34 | UInt32 flags) { |
35 | const UInt32 cursor_type = flags & CURSOR_TYPE_MASK; |
36 | switch (cursor_type) { |
37 | case ID_RANGE_CURSOR: { |
38 | IdCursor *cursor = new (std::nothrow) IdCursor; |
39 | GRN_DAT_THROW_IF(MEMORY_ERROR, cursor == NULL); |
40 | try { |
41 | cursor->open(trie, String(min_ptr, min_length), |
42 | String(max_ptr, max_length), offset, limit, flags); |
43 | } catch (...) { |
44 | delete cursor; |
45 | throw; |
46 | } |
47 | return cursor; |
48 | } |
49 | case KEY_RANGE_CURSOR: { |
50 | KeyCursor *cursor = new (std::nothrow) KeyCursor; |
51 | GRN_DAT_THROW_IF(MEMORY_ERROR, cursor == NULL); |
52 | try { |
53 | cursor->open(trie, String(min_ptr, min_length), |
54 | String(max_ptr, max_length), offset, limit, flags); |
55 | } catch (...) { |
56 | delete cursor; |
57 | throw; |
58 | } |
59 | return cursor; |
60 | } |
61 | case PREFIX_CURSOR: { |
62 | PrefixCursor *cursor = new (std::nothrow) PrefixCursor; |
63 | GRN_DAT_THROW_IF(MEMORY_ERROR, cursor == NULL); |
64 | try { |
65 | cursor->open(trie, String(max_ptr, max_length), min_length, |
66 | offset, limit, flags); |
67 | } catch (...) { |
68 | delete cursor; |
69 | throw; |
70 | } |
71 | return cursor; |
72 | } |
73 | case PREDICTIVE_CURSOR: { |
74 | PredictiveCursor *cursor = new (std::nothrow) PredictiveCursor; |
75 | GRN_DAT_THROW_IF(MEMORY_ERROR, cursor == NULL); |
76 | try { |
77 | cursor->open(trie, String(min_ptr, min_length), |
78 | offset, limit, flags); |
79 | } catch (...) { |
80 | delete cursor; |
81 | throw; |
82 | } |
83 | return cursor; |
84 | } |
85 | default: { |
86 | GRN_DAT_THROW(PARAM_ERROR, "unknown cursor type" ); |
87 | } |
88 | } |
89 | } |
90 | |
91 | } // namespace dat |
92 | } // namespace grn |
93 | |