modbtree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #include <stdio.h>
  27. #include <string.h>
  28. #include <errno.h> // for declaration of global errno variable
  29. #include <fcntl.h>
  30. #include "py/runtime.h"
  31. #include "py/stream.h"
  32. #if MICROPY_PY_BTREE
  33. #include <db.h>
  34. #include <../../btree/btree.h>
  35. typedef struct _mp_obj_btree_t {
  36. mp_obj_base_t base;
  37. DB *db;
  38. mp_obj_t start_key;
  39. mp_obj_t end_key;
  40. #define FLAG_END_KEY_INCL 1
  41. #define FLAG_DESC 2
  42. #define FLAG_ITER_TYPE_MASK 0xc0
  43. #define FLAG_ITER_KEYS 0x40
  44. #define FLAG_ITER_VALUES 0x80
  45. #define FLAG_ITER_ITEMS 0xc0
  46. byte flags;
  47. byte next_flags;
  48. } mp_obj_btree_t;
  49. STATIC const mp_obj_type_t btree_type;
  50. #define CHECK_ERROR(res) \
  51. if (res == RET_ERROR) { \
  52. mp_raise_OSError(errno); \
  53. }
  54. void __dbpanic(DB *db) {
  55. printf("__dbpanic(%p)\n", db);
  56. }
  57. STATIC mp_obj_btree_t *btree_new(DB *db) {
  58. mp_obj_btree_t *o = m_new_obj(mp_obj_btree_t);
  59. o->base.type = &btree_type;
  60. o->db = db;
  61. o->start_key = mp_const_none;
  62. o->end_key = mp_const_none;
  63. o->next_flags = 0;
  64. return o;
  65. }
  66. STATIC void btree_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  67. (void)kind;
  68. mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
  69. mp_printf(print, "<btree %p>", self->db);
  70. }
  71. STATIC mp_obj_t btree_flush(mp_obj_t self_in) {
  72. mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
  73. return MP_OBJ_NEW_SMALL_INT(__bt_sync(self->db, 0));
  74. }
  75. STATIC MP_DEFINE_CONST_FUN_OBJ_1(btree_flush_obj, btree_flush);
  76. STATIC mp_obj_t btree_close(mp_obj_t self_in) {
  77. mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
  78. return MP_OBJ_NEW_SMALL_INT(__bt_close(self->db));
  79. }
  80. STATIC MP_DEFINE_CONST_FUN_OBJ_1(btree_close_obj, btree_close);
  81. STATIC mp_obj_t btree_put(size_t n_args, const mp_obj_t *args) {
  82. (void)n_args;
  83. mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
  84. DBT key, val;
  85. key.data = (void*)mp_obj_str_get_data(args[1], &key.size);
  86. val.data = (void*)mp_obj_str_get_data(args[2], &val.size);
  87. return MP_OBJ_NEW_SMALL_INT(__bt_put(self->db, &key, &val, 0));
  88. }
  89. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_put_obj, 3, 4, btree_put);
  90. STATIC mp_obj_t btree_get(size_t n_args, const mp_obj_t *args) {
  91. mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
  92. DBT key, val;
  93. key.data = (void*)mp_obj_str_get_data(args[1], &key.size);
  94. int res = __bt_get(self->db, &key, &val, 0);
  95. if (res == RET_SPECIAL) {
  96. if (n_args > 2) {
  97. return args[2];
  98. } else {
  99. return mp_const_none;
  100. }
  101. }
  102. CHECK_ERROR(res);
  103. return mp_obj_new_bytes(val.data, val.size);
  104. }
  105. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_get_obj, 2, 3, btree_get);
  106. STATIC mp_obj_t btree_seq(size_t n_args, const mp_obj_t *args) {
  107. mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
  108. int flags = MP_OBJ_SMALL_INT_VALUE(args[1]);
  109. DBT key, val;
  110. if (n_args > 2) {
  111. key.data = (void*)mp_obj_str_get_data(args[2], &key.size);
  112. }
  113. int res = __bt_seq(self->db, &key, &val, flags);
  114. CHECK_ERROR(res);
  115. if (res == RET_SPECIAL) {
  116. return mp_const_none;
  117. }
  118. mp_obj_t pair_o = mp_obj_new_tuple(2, NULL);
  119. mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(pair_o);
  120. pair->items[0] = mp_obj_new_bytes(key.data, key.size);
  121. pair->items[1] = mp_obj_new_bytes(val.data, val.size);
  122. return pair_o;
  123. }
  124. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_seq_obj, 2, 4, btree_seq);
  125. STATIC mp_obj_t btree_init_iter(size_t n_args, const mp_obj_t *args, byte type) {
  126. mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
  127. self->next_flags = type;
  128. self->start_key = mp_const_none;
  129. self->end_key = mp_const_none;
  130. if (n_args > 1) {
  131. self->start_key = args[1];
  132. if (n_args > 2) {
  133. self->end_key = args[2];
  134. if (n_args > 3) {
  135. self->next_flags = type | MP_OBJ_SMALL_INT_VALUE(args[3]);
  136. }
  137. }
  138. }
  139. return args[0];
  140. }
  141. STATIC mp_obj_t btree_keys(size_t n_args, const mp_obj_t *args) {
  142. return btree_init_iter(n_args, args, FLAG_ITER_KEYS);
  143. }
  144. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_keys_obj, 1, 4, btree_keys);
  145. STATIC mp_obj_t btree_values(size_t n_args, const mp_obj_t *args) {
  146. return btree_init_iter(n_args, args, FLAG_ITER_VALUES);
  147. }
  148. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_values_obj, 1, 4, btree_values);
  149. STATIC mp_obj_t btree_items(size_t n_args, const mp_obj_t *args) {
  150. return btree_init_iter(n_args, args, FLAG_ITER_ITEMS);
  151. }
  152. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_items_obj, 1, 4, btree_items);
  153. STATIC mp_obj_t btree_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
  154. (void)iter_buf;
  155. mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
  156. if (self->next_flags != 0) {
  157. // If we're called immediately after keys(), values(), or items(),
  158. // use their setup for iteration.
  159. self->flags = self->next_flags;
  160. self->next_flags = 0;
  161. } else {
  162. // Otherwise, iterate over all keys.
  163. self->flags = FLAG_ITER_KEYS;
  164. self->start_key = mp_const_none;
  165. self->end_key = mp_const_none;
  166. }
  167. return self_in;
  168. }
  169. STATIC mp_obj_t btree_iternext(mp_obj_t self_in) {
  170. mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
  171. DBT key, val;
  172. int res;
  173. bool desc = self->flags & FLAG_DESC;
  174. if (self->start_key != MP_OBJ_NULL) {
  175. int flags = R_FIRST;
  176. if (self->start_key != mp_const_none) {
  177. key.data = (void*)mp_obj_str_get_data(self->start_key, &key.size);
  178. flags = R_CURSOR;
  179. } else if (desc) {
  180. flags = R_LAST;
  181. }
  182. res = __bt_seq(self->db, &key, &val, flags);
  183. self->start_key = MP_OBJ_NULL;
  184. } else {
  185. res = __bt_seq(self->db, &key, &val, desc ? R_PREV : R_NEXT);
  186. }
  187. if (res == RET_SPECIAL) {
  188. return MP_OBJ_STOP_ITERATION;
  189. }
  190. CHECK_ERROR(res);
  191. if (self->end_key != mp_const_none) {
  192. DBT end_key;
  193. end_key.data = (void*)mp_obj_str_get_data(self->end_key, &end_key.size);
  194. BTREE *t = self->db->internal;
  195. int cmp = t->bt_cmp(&key, &end_key);
  196. if (desc) {
  197. cmp = -cmp;
  198. }
  199. if (self->flags & FLAG_END_KEY_INCL) {
  200. cmp--;
  201. }
  202. if (cmp >= 0) {
  203. self->end_key = MP_OBJ_NULL;
  204. return MP_OBJ_STOP_ITERATION;
  205. }
  206. }
  207. switch (self->flags & FLAG_ITER_TYPE_MASK) {
  208. case FLAG_ITER_KEYS:
  209. return mp_obj_new_bytes(key.data, key.size);
  210. case FLAG_ITER_VALUES:
  211. return mp_obj_new_bytes(val.data, val.size);
  212. default: {
  213. mp_obj_t pair_o = mp_obj_new_tuple(2, NULL);
  214. mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(pair_o);
  215. pair->items[0] = mp_obj_new_bytes(key.data, key.size);
  216. pair->items[1] = mp_obj_new_bytes(val.data, val.size);
  217. return pair_o;
  218. }
  219. }
  220. }
  221. STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
  222. mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
  223. if (value == MP_OBJ_NULL) {
  224. // delete
  225. DBT key;
  226. key.data = (void*)mp_obj_str_get_data(index, &key.size);
  227. int res = __bt_delete(self->db, &key, 0);
  228. if (res == RET_SPECIAL) {
  229. nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
  230. }
  231. CHECK_ERROR(res);
  232. return mp_const_none;
  233. } else if (value == MP_OBJ_SENTINEL) {
  234. // load
  235. DBT key, val;
  236. key.data = (void*)mp_obj_str_get_data(index, &key.size);
  237. int res = __bt_get(self->db, &key, &val, 0);
  238. if (res == RET_SPECIAL) {
  239. nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
  240. }
  241. CHECK_ERROR(res);
  242. return mp_obj_new_bytes(val.data, val.size);
  243. } else {
  244. // store
  245. DBT key, val;
  246. key.data = (void*)mp_obj_str_get_data(index, &key.size);
  247. val.data = (void*)mp_obj_str_get_data(value, &val.size);
  248. int res = __bt_put(self->db, &key, &val, 0);
  249. CHECK_ERROR(res);
  250. return mp_const_none;
  251. }
  252. }
  253. STATIC mp_obj_t btree_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  254. mp_obj_btree_t *self = MP_OBJ_TO_PTR(lhs_in);
  255. switch (op) {
  256. case MP_BINARY_OP_CONTAINS: {
  257. DBT key, val;
  258. key.data = (void*)mp_obj_str_get_data(rhs_in, &key.size);
  259. int res = __bt_get(self->db, &key, &val, 0);
  260. CHECK_ERROR(res);
  261. return mp_obj_new_bool(res != RET_SPECIAL);
  262. }
  263. default:
  264. // op not supported
  265. return MP_OBJ_NULL;
  266. }
  267. }
  268. STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = {
  269. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&btree_close_obj) },
  270. { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&btree_flush_obj) },
  271. { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&btree_get_obj) },
  272. { MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&btree_put_obj) },
  273. { MP_ROM_QSTR(MP_QSTR_seq), MP_ROM_PTR(&btree_seq_obj) },
  274. { MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&btree_keys_obj) },
  275. { MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&btree_values_obj) },
  276. { MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&btree_items_obj) },
  277. };
  278. STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table);
  279. STATIC const mp_obj_type_t btree_type = {
  280. { &mp_type_type },
  281. // Save on qstr's, reuse same as for module
  282. .name = MP_QSTR_btree,
  283. .print = btree_print,
  284. .getiter = btree_getiter,
  285. .iternext = btree_iternext,
  286. .binary_op = btree_binary_op,
  287. .subscr = btree_subscr,
  288. .locals_dict = (void*)&btree_locals_dict,
  289. };
  290. STATIC FILEVTABLE btree_stream_fvtable = {
  291. mp_stream_posix_read,
  292. mp_stream_posix_write,
  293. mp_stream_posix_lseek,
  294. mp_stream_posix_fsync
  295. };
  296. STATIC mp_obj_t mod_btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  297. static const mp_arg_t allowed_args[] = {
  298. { MP_QSTR_flags, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  299. { MP_QSTR_cachesize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  300. { MP_QSTR_pagesize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  301. { MP_QSTR_minkeypage, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  302. };
  303. // Make sure we got a stream object
  304. mp_get_stream_raise(pos_args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
  305. struct {
  306. mp_arg_val_t flags;
  307. mp_arg_val_t cachesize;
  308. mp_arg_val_t pagesize;
  309. mp_arg_val_t minkeypage;
  310. } args;
  311. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
  312. MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
  313. BTREEINFO openinfo = {0};
  314. openinfo.flags = args.flags.u_int;
  315. openinfo.cachesize = args.cachesize.u_int;
  316. openinfo.psize = args.pagesize.u_int;
  317. openinfo.minkeypage = args.minkeypage.u_int;
  318. DB *db = __bt_open(pos_args[0], &btree_stream_fvtable, &openinfo, /*dflags*/0);
  319. if (db == NULL) {
  320. mp_raise_OSError(errno);
  321. }
  322. return MP_OBJ_FROM_PTR(btree_new(db));
  323. }
  324. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_btree_open_obj, 1, mod_btree_open);
  325. STATIC const mp_rom_map_elem_t mp_module_btree_globals_table[] = {
  326. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_btree) },
  327. { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mod_btree_open_obj) },
  328. { MP_ROM_QSTR(MP_QSTR_INCL), MP_ROM_INT(FLAG_END_KEY_INCL) },
  329. { MP_ROM_QSTR(MP_QSTR_DESC), MP_ROM_INT(FLAG_DESC) },
  330. };
  331. STATIC MP_DEFINE_CONST_DICT(mp_module_btree_globals, mp_module_btree_globals_table);
  332. const mp_obj_module_t mp_module_btree = {
  333. .base = { &mp_type_module },
  334. .globals = (mp_obj_dict_t*)&mp_module_btree_globals,
  335. };
  336. #endif // MICROPY_PY_BTREE