Debian Codegraph @

lib/dat/dat.hpp

raw symbols SCIP
1 /*
2 Copyright(C) 2011-2016 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 as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #pragma once
20
21 #ifndef _MSC_VER
22 # include <stddef.h>
23 # include <stdint.h>
24 #endif // _MSC_VER
25
26 #include <cstddef>
27 #include <exception>
28
29 #ifdef _DEBUG
30 # include <iostream>
31 #endif // _DEBUG
32
33 #ifdef GRN_STATIC
34 # define GRN_DAT_API
35 #else
36 # ifndef GRN_DAT_API
37 # ifdef WIN32
38 # ifdef GRN_DAT_EXPORT
39 # define GRN_DAT_API __declspec(dllexport)
40 # else // GRN_DAT_EXPORT
41 # define GRN_DAT_API __declspec(dllimport)
42 # endif // GRN_DAT_EXPORT
43 # else // WIN32
44 # define GRN_DAT_API
45 # endif // WIN32
46 # endif // GRN_DAT_API
47 #endif
48
49 namespace grn {
50 namespace dat {
51
52 #ifdef _MSC_VER
53 typedef unsigned __int8 UInt8;
54 typedef unsigned __int16 UInt16;
55 typedef unsigned __int32 UInt32;
56 typedef unsigned __int64 UInt64;
57 #else // _MSC_VER
58 typedef ::uint8_t UInt8;
59 typedef ::uint16_t UInt16;
60 typedef ::uint32_t UInt32;
61 typedef ::uint64_t UInt64;
62 #endif // _MSC_VER
63
64 const UInt8 MAX_UINT8 = static_cast<UInt8>(0xFFU);
65 const UInt16 MAX_UINT16 = static_cast<UInt16>(0xFFFFU);
66 const UInt32 MAX_UINT32 = static_cast<UInt32>(0xFFFFFFFFU);
67 const UInt64 MAX_UINT64 = static_cast<UInt64>(0xFFFFFFFFFFFFFFFFULL);
68
69 // If a key is a prefix of another key, such a key is associated with a special
70 // terminal node which has TERMINAL_LABEL.
71 const UInt16 TERMINAL_LABEL = 0x100;
72 const UInt16 MIN_LABEL = '\0';
73 const UInt16 MAX_LABEL = TERMINAL_LABEL;
74 const UInt32 INVALID_LABEL = 0x1FF;
75 const UInt32 LABEL_MASK = 0x1FF;
76
77 // The MSB of BASE is used to represent whether the node is a linker node or
78 // not and the other 31 bits represent the offset to its child nodes. So, the
79 // number of nodes is limited to 2^31.
80 const UInt32 ROOT_NODE_ID = 0;
81 const UInt32 MAX_NODE_ID = 0x7FFFFFFF;
82 const UInt32 MAX_NUM_NODES = MAX_NODE_ID + 1;
83 const UInt32 INVALID_NODE_ID = MAX_NODE_ID + 1;
84
85 // 0 is reserved for non-linker leaf nodes. For example, the root node of an
86 // initial double-array is a non-linker leaf node.
87 const UInt32 MAX_OFFSET = MAX_NODE_ID;
88 const UInt32 INVALID_OFFSET = 0;
89
90 // Phantom nodes are managed in each block because siblings are always put in
91 // the same block.
92 const UInt32 BLOCK_SIZE = 0x200;
93 const UInt32 BLOCK_MASK = 0x1FF;
94 const UInt32 MAX_BLOCK_ID = MAX_NODE_ID / BLOCK_SIZE;
95 const UInt32 MAX_NUM_BLOCKS = MAX_BLOCK_ID + 1;
96
97 // Blocks are divided by their levels, which indicate how easily update
98 // operations can find a good offset in them. The level of a block rises when
99 // find_offset() fails in that block many times. MAX_FAILURE_COUNT is the
100 // threshold. Also, in order to limit the time cost, find_offset() scans at
101 // most MAX_BLOCK_COUNT blocks.
102 // Larger parameters bring more chances of finding good offsets but it leads to
103 // more node renumberings, which are costly operations, and thus results in
104 // a degradation of space/time efficiencies.
105 const UInt32 MAX_FAILURE_COUNT = 4;
106 const UInt32 MAX_BLOCK_COUNT = 16;
107 const UInt32 MAX_BLOCK_LEVEL = 5;
108
109 // Blocks in the same level compose a doubly linked list. The entry block of
110 // a linked list is called a leader. INVALID_LEADER means that a linked list is
111 // empty and there exists no leader.
112 const UInt32 INVALID_LEADER = 0x7FFFFFFF;
113
114 const UInt32 MIN_KEY_ID = 1;
115 const UInt32 MAX_KEY_ID = MAX_NODE_ID;
116 const UInt32 INVALID_KEY_ID = 0;
117
118 // A key length is represented as a 12-bit unsigned integer in Key.
119 // 4096 bytes key overflows to 0, so 0 is treated as 4096.
120 // A key ID is represented as a 28-bit unsigned integer in Key.
121 const UInt32 MAX_KEY_LENGTH = (1U << 12);
122 const UInt32 MAX_NUM_KEYS = (1U << 28) - 1;
123
124 const UInt64 MIN_FILE_SIZE = 1 << 16;
125 const UInt64 DEFAULT_FILE_SIZE = 1 << 20;
126 const UInt64 MAX_FILE_SIZE = (UInt64)1 << 40;
127 const double DEFAULT_NUM_NODES_PER_KEY = 4.0;
128 const double MAX_NUM_NODES_PER_KEY = 16.0;
129 const double DEFAULT_AVERAGE_KEY_LENGTH = 16.0;
130 const UInt32 MAX_KEY_BUF_SIZE = 0x80000000U;
131 const UInt32 MAX_TOTAL_KEY_LENGTH = 0xFFFFFFFFU;
132
133 const UInt32 ID_RANGE_CURSOR = 0x00001;
134 const UInt32 KEY_RANGE_CURSOR = 0x00002;
135 const UInt32 PREFIX_CURSOR = 0x00004;
136 const UInt32 PREDICTIVE_CURSOR = 0x00008;
137 const UInt32 CURSOR_TYPE_MASK = 0x000FF;
138
139 const UInt32 ASCENDING_CURSOR = 0x00100;
140 const UInt32 DESCENDING_CURSOR = 0x00200;
141 const UInt32 CURSOR_ORDER_MASK = 0x00F00;
142
143 const UInt32 EXCEPT_LOWER_BOUND = 0x01000;
144 const UInt32 EXCEPT_UPPER_BOUND = 0x02000;
145 const UInt32 EXCEPT_EXACT_MATCH = 0x04000;
146 const UInt32 CURSOR_OPTIONS_MASK = 0xFF000;
147
148 const UInt32 REMOVING_FLAG = 1U << 0;
149 const UInt32 INSERTING_FLAG = 1U << 1;
150 const UInt32 UPDATING_FLAG = 1U << 2;
151 const UInt32 CHANGING_MASK = REMOVING_FLAG | INSERTING_FLAG | UPDATING_FLAG;
152
153 const UInt32 MKQ_SORT_THRESHOLD = 10;
154
155 enum ErrorCode {
156 PARAM_ERROR = -1,
157 IO_ERROR = -2,
158 FORMAT_ERROR = -3,
159 MEMORY_ERROR = -4,
160 SIZE_ERROR = -5,
161 UNEXPECTED_ERROR = -6,
162 STATUS_ERROR = -7
163 };
164
165 class Exception : public std::exception {
166 public:
167 Exception() throw()
168 : std::exception(),
169 file_(""),
170 line_(-1),
171 what_("") {}
172 Exception(const char *file, int line, const char *what) throw()
173 : std::exception(),
174 file_(file),
175 line_(line),
176 what_((what != NULL) ? what : "") {}
177 Exception(const Exception &ex) throw()
178 : std::exception(ex),
179 file_(ex.file_),
180 line_(ex.line_),
181 what_(ex.what_) {}
182 virtual ~Exception() throw() {}
183
184 virtual ErrorCode code() const throw() = 0;
185 virtual const char *file() const throw() {
186 return file_;
187 }
188 virtual int line() const throw() {
189 return line_;
190 }
191 virtual const char *what() const throw() override {
192 return what_;
193 }
194
195 private:
196 const char *file_;
197 int line_;
198 const char *what_;
199 };
200
201 template <ErrorCode T>
202 class Error : public Exception {
203 public:
204 Error() throw()
205 : Exception() {}
206 Error(const char *file, int line, const char *what) throw()
207 : Exception(file, line, what) {}
208 Error(const Error &ex) throw()
209 : Exception(ex) {}
210 virtual ~Error() throw() {}
211
212 virtual ErrorCode code() const throw() override {
213 return T;
214 }
215 };
216
217 typedef Error<PARAM_ERROR> ParamError;
218 typedef Error<IO_ERROR> IOError;
219 typedef Error<FORMAT_ERROR> FormatError;
220 typedef Error<MEMORY_ERROR> MemoryError;
221 typedef Error<SIZE_ERROR> SizeError;
222 typedef Error<UNEXPECTED_ERROR> UnexpectedError;
223 typedef Error<STATUS_ERROR> StatusError;
224
225 #define GRN_DAT_INT_TO_STR(value) #value
226 #define GRN_DAT_LINE_TO_STR(line) GRN_DAT_INT_TO_STR(line)
227 #define GRN_DAT_LINE_STR GRN_DAT_LINE_TO_STR(__LINE__)
228
229 #define GRN_DAT_THROW(code, msg)\
230 (throw grn::dat::Error<code>(__FILE__, __LINE__,\
231 __FILE__ ":" GRN_DAT_LINE_STR ": " #code ": " msg))
232 #define GRN_DAT_THROW_IF(code, cond)\
233 (void)((!(cond)) || (GRN_DAT_THROW(code, #cond), 0))
234
235 #ifdef _DEBUG
236 #define GRN_DAT_DEBUG_THROW_IF(cond)\
237 GRN_DAT_THROW_IF(grn::dat::UNEXPECTED_ERROR, cond)
238 #define GRN_DAT_DEBUG_LOG(var)\
239 (std::clog << __FILE__ ":" GRN_DAT_LINE_STR ": " #var ": "\
240 << (var) << std::endl)
241 #else // _DEBUG
242 #define GRN_DAT_DEBUG_THROW_IF(cond)
243 #define GRN_DAT_DEBUG_LOG(var)
244 #endif // _DEBUG
245
246 } // namespace dat
247 } // namespace grn