1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2016 Paul Sokolovsky |
7 | * |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal |
10 | * in the Software without restriction, including without limitation the rights |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | * copies of the Software, and to permit persons to whom the Software is |
13 | * furnished to do so, subject to the following conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | * THE SOFTWARE. |
25 | */ |
26 | |
27 | #include <stdio.h> |
28 | #include <string.h> |
29 | #include <errno.h> // for declaration of global errno variable |
30 | #include <fcntl.h> |
31 | |
32 | #include "py/runtime.h" |
33 | #include "py/stream.h" |
34 | |
35 | #if MICROPY_PY_BTREE |
36 | |
37 | #include <db.h> |
38 | #include <../../btree/btree.h> |
39 | |
40 | typedef struct _mp_obj_btree_t { |
41 | mp_obj_base_t base; |
42 | mp_obj_t stream; // retain a reference to prevent GC from reclaiming it |
43 | DB *db; |
44 | mp_obj_t start_key; |
45 | mp_obj_t end_key; |
46 | #define FLAG_END_KEY_INCL 1 |
47 | #define FLAG_DESC 2 |
48 | #define FLAG_ITER_TYPE_MASK 0xc0 |
49 | #define FLAG_ITER_KEYS 0x40 |
50 | #define FLAG_ITER_VALUES 0x80 |
51 | #define FLAG_ITER_ITEMS 0xc0 |
52 | byte flags; |
53 | byte next_flags; |
54 | } mp_obj_btree_t; |
55 | |
56 | #if !MICROPY_ENABLE_DYNRUNTIME |
57 | STATIC const mp_obj_type_t btree_type; |
58 | #endif |
59 | |
60 | #define CHECK_ERROR(res) \ |
61 | if (res == RET_ERROR) { \ |
62 | mp_raise_OSError(errno); \ |
63 | } |
64 | |
65 | void __dbpanic(DB *db) { |
66 | mp_printf(&mp_plat_print, "__dbpanic(%p)\n" , db); |
67 | } |
68 | |
69 | STATIC mp_obj_btree_t *btree_new(DB *db, mp_obj_t stream) { |
70 | mp_obj_btree_t *o = m_new_obj(mp_obj_btree_t); |
71 | o->base.type = &btree_type; |
72 | o->stream = stream; |
73 | o->db = db; |
74 | o->start_key = mp_const_none; |
75 | o->end_key = mp_const_none; |
76 | o->next_flags = 0; |
77 | return o; |
78 | } |
79 | |
80 | STATIC void btree_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
81 | (void)kind; |
82 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); |
83 | mp_printf(print, "<btree %p>" , self->db); |
84 | } |
85 | |
86 | STATIC mp_obj_t btree_flush(mp_obj_t self_in) { |
87 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); |
88 | return MP_OBJ_NEW_SMALL_INT(__bt_sync(self->db, 0)); |
89 | } |
90 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(btree_flush_obj, btree_flush); |
91 | |
92 | STATIC mp_obj_t btree_close(mp_obj_t self_in) { |
93 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); |
94 | return MP_OBJ_NEW_SMALL_INT(__bt_close(self->db)); |
95 | } |
96 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(btree_close_obj, btree_close); |
97 | |
98 | STATIC mp_obj_t btree_put(size_t n_args, const mp_obj_t *args) { |
99 | (void)n_args; |
100 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]); |
101 | DBT key, val; |
102 | key.data = (void *)mp_obj_str_get_data(args[1], &key.size); |
103 | val.data = (void *)mp_obj_str_get_data(args[2], &val.size); |
104 | return MP_OBJ_NEW_SMALL_INT(__bt_put(self->db, &key, &val, 0)); |
105 | } |
106 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_put_obj, 3, 4, btree_put); |
107 | |
108 | STATIC mp_obj_t btree_get(size_t n_args, const mp_obj_t *args) { |
109 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]); |
110 | DBT key, val; |
111 | key.data = (void *)mp_obj_str_get_data(args[1], &key.size); |
112 | int res = __bt_get(self->db, &key, &val, 0); |
113 | if (res == RET_SPECIAL) { |
114 | if (n_args > 2) { |
115 | return args[2]; |
116 | } else { |
117 | return mp_const_none; |
118 | } |
119 | } |
120 | CHECK_ERROR(res); |
121 | return mp_obj_new_bytes(val.data, val.size); |
122 | } |
123 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_get_obj, 2, 3, btree_get); |
124 | |
125 | STATIC mp_obj_t btree_seq(size_t n_args, const mp_obj_t *args) { |
126 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]); |
127 | int flags = MP_OBJ_SMALL_INT_VALUE(args[1]); |
128 | DBT key, val; |
129 | if (n_args > 2) { |
130 | key.data = (void *)mp_obj_str_get_data(args[2], &key.size); |
131 | } |
132 | |
133 | int res = __bt_seq(self->db, &key, &val, flags); |
134 | CHECK_ERROR(res); |
135 | if (res == RET_SPECIAL) { |
136 | return mp_const_none; |
137 | } |
138 | |
139 | mp_obj_t pair_o = mp_obj_new_tuple(2, NULL); |
140 | mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(pair_o); |
141 | pair->items[0] = mp_obj_new_bytes(key.data, key.size); |
142 | pair->items[1] = mp_obj_new_bytes(val.data, val.size); |
143 | return pair_o; |
144 | } |
145 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_seq_obj, 2, 4, btree_seq); |
146 | |
147 | STATIC mp_obj_t btree_init_iter(size_t n_args, const mp_obj_t *args, byte type) { |
148 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]); |
149 | self->next_flags = type; |
150 | self->start_key = mp_const_none; |
151 | self->end_key = mp_const_none; |
152 | if (n_args > 1) { |
153 | self->start_key = args[1]; |
154 | if (n_args > 2) { |
155 | self->end_key = args[2]; |
156 | if (n_args > 3) { |
157 | self->next_flags = type | MP_OBJ_SMALL_INT_VALUE(args[3]); |
158 | } |
159 | } |
160 | } |
161 | return args[0]; |
162 | } |
163 | |
164 | STATIC mp_obj_t btree_keys(size_t n_args, const mp_obj_t *args) { |
165 | return btree_init_iter(n_args, args, FLAG_ITER_KEYS); |
166 | } |
167 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_keys_obj, 1, 4, btree_keys); |
168 | |
169 | STATIC mp_obj_t btree_values(size_t n_args, const mp_obj_t *args) { |
170 | return btree_init_iter(n_args, args, FLAG_ITER_VALUES); |
171 | } |
172 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_values_obj, 1, 4, btree_values); |
173 | |
174 | STATIC mp_obj_t btree_items(size_t n_args, const mp_obj_t *args) { |
175 | return btree_init_iter(n_args, args, FLAG_ITER_ITEMS); |
176 | } |
177 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_items_obj, 1, 4, btree_items); |
178 | |
179 | STATIC mp_obj_t btree_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { |
180 | (void)iter_buf; |
181 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); |
182 | if (self->next_flags != 0) { |
183 | // If we're called immediately after keys(), values(), or items(), |
184 | // use their setup for iteration. |
185 | self->flags = self->next_flags; |
186 | self->next_flags = 0; |
187 | } else { |
188 | // Otherwise, iterate over all keys. |
189 | self->flags = FLAG_ITER_KEYS; |
190 | self->start_key = mp_const_none; |
191 | self->end_key = mp_const_none; |
192 | } |
193 | |
194 | return self_in; |
195 | } |
196 | |
197 | STATIC mp_obj_t btree_iternext(mp_obj_t self_in) { |
198 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); |
199 | DBT key, val; |
200 | int res; |
201 | bool desc = self->flags & FLAG_DESC; |
202 | if (self->start_key != MP_OBJ_NULL) { |
203 | int flags = R_FIRST; |
204 | if (self->start_key != mp_const_none) { |
205 | key.data = (void *)mp_obj_str_get_data(self->start_key, &key.size); |
206 | flags = R_CURSOR; |
207 | } else if (desc) { |
208 | flags = R_LAST; |
209 | } |
210 | res = __bt_seq(self->db, &key, &val, flags); |
211 | self->start_key = MP_OBJ_NULL; |
212 | } else { |
213 | res = __bt_seq(self->db, &key, &val, desc ? R_PREV : R_NEXT); |
214 | } |
215 | |
216 | if (res == RET_SPECIAL) { |
217 | return MP_OBJ_STOP_ITERATION; |
218 | } |
219 | CHECK_ERROR(res); |
220 | |
221 | if (self->end_key != mp_const_none) { |
222 | DBT end_key; |
223 | end_key.data = (void *)mp_obj_str_get_data(self->end_key, &end_key.size); |
224 | BTREE *t = self->db->internal; |
225 | int cmp = t->bt_cmp(&key, &end_key); |
226 | if (desc) { |
227 | cmp = -cmp; |
228 | } |
229 | if (self->flags & FLAG_END_KEY_INCL) { |
230 | cmp--; |
231 | } |
232 | if (cmp >= 0) { |
233 | self->end_key = MP_OBJ_NULL; |
234 | return MP_OBJ_STOP_ITERATION; |
235 | } |
236 | } |
237 | |
238 | switch (self->flags & FLAG_ITER_TYPE_MASK) { |
239 | case FLAG_ITER_KEYS: |
240 | return mp_obj_new_bytes(key.data, key.size); |
241 | case FLAG_ITER_VALUES: |
242 | return mp_obj_new_bytes(val.data, val.size); |
243 | default: { |
244 | mp_obj_t pair_o = mp_obj_new_tuple(2, NULL); |
245 | mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(pair_o); |
246 | pair->items[0] = mp_obj_new_bytes(key.data, key.size); |
247 | pair->items[1] = mp_obj_new_bytes(val.data, val.size); |
248 | return pair_o; |
249 | } |
250 | } |
251 | } |
252 | |
253 | STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { |
254 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); |
255 | if (value == MP_OBJ_NULL) { |
256 | // delete |
257 | DBT key; |
258 | key.data = (void *)mp_obj_str_get_data(index, &key.size); |
259 | int res = __bt_delete(self->db, &key, 0); |
260 | if (res == RET_SPECIAL) { |
261 | mp_raise_type(&mp_type_KeyError); |
262 | } |
263 | CHECK_ERROR(res); |
264 | return mp_const_none; |
265 | } else if (value == MP_OBJ_SENTINEL) { |
266 | // load |
267 | DBT key, val; |
268 | key.data = (void *)mp_obj_str_get_data(index, &key.size); |
269 | int res = __bt_get(self->db, &key, &val, 0); |
270 | if (res == RET_SPECIAL) { |
271 | mp_raise_type(&mp_type_KeyError); |
272 | } |
273 | CHECK_ERROR(res); |
274 | return mp_obj_new_bytes(val.data, val.size); |
275 | } else { |
276 | // store |
277 | DBT key, val; |
278 | key.data = (void *)mp_obj_str_get_data(index, &key.size); |
279 | val.data = (void *)mp_obj_str_get_data(value, &val.size); |
280 | int res = __bt_put(self->db, &key, &val, 0); |
281 | CHECK_ERROR(res); |
282 | return mp_const_none; |
283 | } |
284 | } |
285 | |
286 | STATIC mp_obj_t btree_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
287 | mp_obj_btree_t *self = MP_OBJ_TO_PTR(lhs_in); |
288 | switch (op) { |
289 | case MP_BINARY_OP_CONTAINS: { |
290 | DBT key, val; |
291 | key.data = (void *)mp_obj_str_get_data(rhs_in, &key.size); |
292 | int res = __bt_get(self->db, &key, &val, 0); |
293 | CHECK_ERROR(res); |
294 | return mp_obj_new_bool(res != RET_SPECIAL); |
295 | } |
296 | default: |
297 | // op not supported |
298 | return MP_OBJ_NULL; |
299 | } |
300 | } |
301 | |
302 | #if !MICROPY_ENABLE_DYNRUNTIME |
303 | STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = { |
304 | { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&btree_close_obj) }, |
305 | { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&btree_flush_obj) }, |
306 | { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&btree_get_obj) }, |
307 | { MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&btree_put_obj) }, |
308 | { MP_ROM_QSTR(MP_QSTR_seq), MP_ROM_PTR(&btree_seq_obj) }, |
309 | { MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&btree_keys_obj) }, |
310 | { MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&btree_values_obj) }, |
311 | { MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&btree_items_obj) }, |
312 | }; |
313 | |
314 | STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table); |
315 | |
316 | STATIC const mp_obj_type_t btree_type = { |
317 | { &mp_type_type }, |
318 | // Save on qstr's, reuse same as for module |
319 | .name = MP_QSTR_btree, |
320 | .print = btree_print, |
321 | .getiter = btree_getiter, |
322 | .iternext = btree_iternext, |
323 | .binary_op = btree_binary_op, |
324 | .subscr = btree_subscr, |
325 | .locals_dict = (void *)&btree_locals_dict, |
326 | }; |
327 | #endif |
328 | |
329 | STATIC const FILEVTABLE btree_stream_fvtable = { |
330 | mp_stream_posix_read, |
331 | mp_stream_posix_write, |
332 | mp_stream_posix_lseek, |
333 | mp_stream_posix_fsync |
334 | }; |
335 | |
336 | #if !MICROPY_ENABLE_DYNRUNTIME |
337 | STATIC mp_obj_t mod_btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
338 | static const mp_arg_t allowed_args[] = { |
339 | { MP_QSTR_flags, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
340 | { MP_QSTR_cachesize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
341 | { MP_QSTR_pagesize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
342 | { MP_QSTR_minkeypage, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
343 | }; |
344 | |
345 | // Make sure we got a stream object |
346 | mp_get_stream_raise(pos_args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL); |
347 | |
348 | struct { |
349 | mp_arg_val_t flags; |
350 | mp_arg_val_t cachesize; |
351 | mp_arg_val_t pagesize; |
352 | mp_arg_val_t minkeypage; |
353 | } args; |
354 | mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, |
355 | MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args); |
356 | BTREEINFO openinfo = {0}; |
357 | openinfo.flags = args.flags.u_int; |
358 | openinfo.cachesize = args.cachesize.u_int; |
359 | openinfo.psize = args.pagesize.u_int; |
360 | openinfo.minkeypage = args.minkeypage.u_int; |
361 | |
362 | DB *db = __bt_open(MP_OBJ_TO_PTR(pos_args[0]), &btree_stream_fvtable, &openinfo, /*dflags*/ 0); |
363 | if (db == NULL) { |
364 | mp_raise_OSError(errno); |
365 | } |
366 | return MP_OBJ_FROM_PTR(btree_new(db, pos_args[0])); |
367 | } |
368 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_btree_open_obj, 1, mod_btree_open); |
369 | |
370 | STATIC const mp_rom_map_elem_t mp_module_btree_globals_table[] = { |
371 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_btree) }, |
372 | { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mod_btree_open_obj) }, |
373 | { MP_ROM_QSTR(MP_QSTR_INCL), MP_ROM_INT(FLAG_END_KEY_INCL) }, |
374 | { MP_ROM_QSTR(MP_QSTR_DESC), MP_ROM_INT(FLAG_DESC) }, |
375 | }; |
376 | |
377 | STATIC MP_DEFINE_CONST_DICT(mp_module_btree_globals, mp_module_btree_globals_table); |
378 | |
379 | const mp_obj_module_t mp_module_btree = { |
380 | .base = { &mp_type_module }, |
381 | .globals = (mp_obj_dict_t *)&mp_module_btree_globals, |
382 | }; |
383 | #endif |
384 | |
385 | #endif // MICROPY_PY_BTREE |
386 | |