2016-06-14 14:51:59 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Paul Sokolovsky
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-07-24 04:43:14 -04:00
|
|
|
#include <errno.h> // for declaration of global errno variable
|
2016-06-14 14:51:59 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2016-07-30 17:40:35 -04:00
|
|
|
#include "py/stream.h"
|
2016-06-14 14:51:59 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_BTREE
|
|
|
|
|
|
|
|
#include <db.h>
|
|
|
|
#include <../../btree/btree.h>
|
|
|
|
|
|
|
|
typedef struct _mp_obj_btree_t {
|
|
|
|
mp_obj_base_t base;
|
2020-04-20 09:04:40 -04:00
|
|
|
mp_obj_t stream; // retain a reference to prevent GC from reclaiming it
|
2016-06-14 14:51:59 -04:00
|
|
|
DB *db;
|
|
|
|
mp_obj_t start_key;
|
|
|
|
mp_obj_t end_key;
|
2016-06-16 17:08:55 -04:00
|
|
|
#define FLAG_END_KEY_INCL 1
|
2016-06-17 17:47:26 -04:00
|
|
|
#define FLAG_DESC 2
|
2016-06-20 08:50:31 -04:00
|
|
|
#define FLAG_ITER_TYPE_MASK 0xc0
|
|
|
|
#define FLAG_ITER_KEYS 0x40
|
|
|
|
#define FLAG_ITER_VALUES 0x80
|
|
|
|
#define FLAG_ITER_ITEMS 0xc0
|
2016-06-17 17:47:26 -04:00
|
|
|
byte flags;
|
2016-06-23 13:08:37 -04:00
|
|
|
byte next_flags;
|
2016-06-14 14:51:59 -04:00
|
|
|
} mp_obj_btree_t;
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2016-06-14 14:51:59 -04:00
|
|
|
STATIC const mp_obj_type_t btree_type;
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2016-06-14 14:51:59 -04:00
|
|
|
|
|
|
|
#define CHECK_ERROR(res) \
|
|
|
|
if (res == RET_ERROR) { \
|
2016-10-06 22:52:14 -04:00
|
|
|
mp_raise_OSError(errno); \
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void __dbpanic(DB *db) {
|
2019-11-04 19:37:36 -05:00
|
|
|
mp_printf(&mp_plat_print, "__dbpanic(%p)\n", db);
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
|
2020-04-20 09:04:40 -04:00
|
|
|
STATIC mp_obj_btree_t *btree_new(DB *db, mp_obj_t stream) {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_btree_t *o = mp_obj_malloc(mp_obj_btree_t, &btree_type);
|
2020-04-20 09:04:40 -04:00
|
|
|
o->stream = stream;
|
2016-06-14 14:51:59 -04:00
|
|
|
o->db = db;
|
2016-06-23 13:08:37 -04:00
|
|
|
o->start_key = mp_const_none;
|
|
|
|
o->end_key = mp_const_none;
|
|
|
|
o->next_flags = 0;
|
2016-06-14 14:51:59 -04:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:28:56 -04:00
|
|
|
STATIC void buf_to_dbt(mp_obj_t obj, DBT *dbt) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(obj, &bufinfo, MP_BUFFER_READ);
|
|
|
|
dbt->data = bufinfo.buf;
|
|
|
|
dbt->size = bufinfo.len;
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:51:59 -04:00
|
|
|
STATIC void btree_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
(void)kind;
|
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_printf(print, "<btree %p>", self->db);
|
|
|
|
}
|
|
|
|
|
2016-12-04 17:50:55 -05:00
|
|
|
STATIC mp_obj_t btree_flush(mp_obj_t self_in) {
|
2016-11-22 21:45:58 -05:00
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(__bt_sync(self->db, 0));
|
|
|
|
}
|
2016-12-04 17:50:55 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(btree_flush_obj, btree_flush);
|
2016-11-22 21:45:58 -05:00
|
|
|
|
2016-06-14 14:51:59 -04:00
|
|
|
STATIC mp_obj_t btree_close(mp_obj_t self_in) {
|
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(__bt_close(self->db));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(btree_close_obj, btree_close);
|
|
|
|
|
|
|
|
STATIC mp_obj_t btree_put(size_t n_args, const mp_obj_t *args) {
|
2016-07-02 08:21:54 -04:00
|
|
|
(void)n_args;
|
2016-06-14 14:51:59 -04:00
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
DBT key, val;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(args[1], &key);
|
|
|
|
buf_to_dbt(args[2], &val);
|
2016-06-14 14:51:59 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(__bt_put(self->db, &key, &val, 0));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_put_obj, 3, 4, btree_put);
|
|
|
|
|
|
|
|
STATIC mp_obj_t btree_get(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
DBT key, val;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(args[1], &key);
|
2016-06-14 14:51:59 -04:00
|
|
|
int res = __bt_get(self->db, &key, &val, 0);
|
|
|
|
if (res == RET_SPECIAL) {
|
2016-06-14 21:17:45 -04:00
|
|
|
if (n_args > 2) {
|
|
|
|
return args[2];
|
|
|
|
} else {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
2016-06-14 21:17:45 -04:00
|
|
|
CHECK_ERROR(res);
|
2016-06-14 14:51:59 -04:00
|
|
|
return mp_obj_new_bytes(val.data, val.size);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_get_obj, 2, 3, btree_get);
|
|
|
|
|
|
|
|
STATIC mp_obj_t btree_seq(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
int flags = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
DBT key, val;
|
|
|
|
if (n_args > 2) {
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(args[2], &key);
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int res = __bt_seq(self->db, &key, &val, flags);
|
2016-08-20 10:59:53 -04:00
|
|
|
CHECK_ERROR(res);
|
2016-06-14 14:51:59 -04:00
|
|
|
if (res == RET_SPECIAL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2016-07-02 08:58:13 -04:00
|
|
|
mp_obj_t pair_o = mp_obj_new_tuple(2, NULL);
|
|
|
|
mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(pair_o);
|
2016-06-14 14:51:59 -04:00
|
|
|
pair->items[0] = mp_obj_new_bytes(key.data, key.size);
|
|
|
|
pair->items[1] = mp_obj_new_bytes(val.data, val.size);
|
2016-07-02 08:58:13 -04:00
|
|
|
return pair_o;
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_seq_obj, 2, 4, btree_seq);
|
|
|
|
|
2016-06-20 08:50:31 -04:00
|
|
|
STATIC mp_obj_t btree_init_iter(size_t n_args, const mp_obj_t *args, byte type) {
|
2016-06-14 14:51:59 -04:00
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]);
|
2016-06-23 13:08:37 -04:00
|
|
|
self->next_flags = type;
|
2016-06-20 08:50:31 -04:00
|
|
|
self->start_key = mp_const_none;
|
|
|
|
self->end_key = mp_const_none;
|
|
|
|
if (n_args > 1) {
|
|
|
|
self->start_key = args[1];
|
|
|
|
if (n_args > 2) {
|
|
|
|
self->end_key = args[2];
|
|
|
|
if (n_args > 3) {
|
2016-06-23 13:08:37 -04:00
|
|
|
self->next_flags = type | MP_OBJ_SMALL_INT_VALUE(args[3]);
|
2016-06-20 08:50:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 17:08:55 -04:00
|
|
|
}
|
2016-06-14 14:51:59 -04:00
|
|
|
return args[0];
|
|
|
|
}
|
2016-06-20 08:50:31 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t btree_keys(size_t n_args, const mp_obj_t *args) {
|
|
|
|
return btree_init_iter(n_args, args, FLAG_ITER_KEYS);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_keys_obj, 1, 4, btree_keys);
|
|
|
|
|
|
|
|
STATIC mp_obj_t btree_values(size_t n_args, const mp_obj_t *args) {
|
|
|
|
return btree_init_iter(n_args, args, FLAG_ITER_VALUES);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_values_obj, 1, 4, btree_values);
|
|
|
|
|
|
|
|
STATIC mp_obj_t btree_items(size_t n_args, const mp_obj_t *args) {
|
|
|
|
return btree_init_iter(n_args, args, FLAG_ITER_ITEMS);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_items_obj, 1, 4, btree_items);
|
2016-06-14 14:51:59 -04:00
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
STATIC mp_obj_t btree_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
(void)iter_buf;
|
2016-06-23 13:08:37 -04:00
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (self->next_flags != 0) {
|
|
|
|
// If we're called immediately after keys(), values(), or items(),
|
|
|
|
// use their setup for iteration.
|
|
|
|
self->flags = self->next_flags;
|
|
|
|
self->next_flags = 0;
|
|
|
|
} else {
|
|
|
|
// Otherwise, iterate over all keys.
|
|
|
|
self->flags = FLAG_ITER_KEYS;
|
|
|
|
self->start_key = mp_const_none;
|
|
|
|
self->end_key = mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self_in;
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:31:24 -04:00
|
|
|
STATIC mp_obj_t btree_iternext(mp_obj_t self_in) {
|
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
DBT key, val;
|
|
|
|
int res;
|
2016-06-17 17:47:26 -04:00
|
|
|
bool desc = self->flags & FLAG_DESC;
|
2016-06-16 10:31:24 -04:00
|
|
|
if (self->start_key != MP_OBJ_NULL) {
|
|
|
|
int flags = R_FIRST;
|
|
|
|
if (self->start_key != mp_const_none) {
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(self->start_key, &key);
|
2016-06-16 10:31:24 -04:00
|
|
|
flags = R_CURSOR;
|
2016-06-17 17:47:26 -04:00
|
|
|
} else if (desc) {
|
|
|
|
flags = R_LAST;
|
2016-06-16 10:31:24 -04:00
|
|
|
}
|
|
|
|
res = __bt_seq(self->db, &key, &val, flags);
|
|
|
|
self->start_key = MP_OBJ_NULL;
|
|
|
|
} else {
|
2016-06-17 17:47:26 -04:00
|
|
|
res = __bt_seq(self->db, &key, &val, desc ? R_PREV : R_NEXT);
|
2016-06-16 10:31:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res == RET_SPECIAL) {
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
|
|
|
}
|
|
|
|
CHECK_ERROR(res);
|
|
|
|
|
2016-06-16 11:16:33 -04:00
|
|
|
if (self->end_key != mp_const_none) {
|
|
|
|
DBT end_key;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(self->end_key, &end_key);
|
2016-06-16 11:16:33 -04:00
|
|
|
BTREE *t = self->db->internal;
|
2016-06-16 17:08:55 -04:00
|
|
|
int cmp = t->bt_cmp(&key, &end_key);
|
2016-06-17 17:47:26 -04:00
|
|
|
if (desc) {
|
|
|
|
cmp = -cmp;
|
|
|
|
}
|
2016-06-16 17:08:55 -04:00
|
|
|
if (self->flags & FLAG_END_KEY_INCL) {
|
|
|
|
cmp--;
|
|
|
|
}
|
|
|
|
if (cmp >= 0) {
|
2016-06-16 11:16:33 -04:00
|
|
|
self->end_key = MP_OBJ_NULL;
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 08:50:31 -04:00
|
|
|
switch (self->flags & FLAG_ITER_TYPE_MASK) {
|
|
|
|
case FLAG_ITER_KEYS:
|
|
|
|
return mp_obj_new_bytes(key.data, key.size);
|
|
|
|
case FLAG_ITER_VALUES:
|
|
|
|
return mp_obj_new_bytes(val.data, val.size);
|
|
|
|
default: {
|
2016-07-02 08:58:13 -04:00
|
|
|
mp_obj_t pair_o = mp_obj_new_tuple(2, NULL);
|
|
|
|
mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(pair_o);
|
2016-06-20 08:50:31 -04:00
|
|
|
pair->items[0] = mp_obj_new_bytes(key.data, key.size);
|
|
|
|
pair->items[1] = mp_obj_new_bytes(val.data, val.size);
|
2016-07-02 08:58:13 -04:00
|
|
|
return pair_o;
|
2016-06-20 08:50:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 10:31:24 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:51:59 -04:00
|
|
|
STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (value == MP_OBJ_NULL) {
|
|
|
|
// delete
|
|
|
|
DBT key;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(index, &key);
|
2016-06-14 14:51:59 -04:00
|
|
|
int res = __bt_delete(self->db, &key, 0);
|
|
|
|
if (res == RET_SPECIAL) {
|
2020-02-10 21:17:41 -05:00
|
|
|
mp_raise_type(&mp_type_KeyError);
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
CHECK_ERROR(res);
|
|
|
|
return mp_const_none;
|
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
|
|
|
// load
|
|
|
|
DBT key, val;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(index, &key);
|
2016-06-14 14:51:59 -04:00
|
|
|
int res = __bt_get(self->db, &key, &val, 0);
|
|
|
|
if (res == RET_SPECIAL) {
|
2020-02-10 21:17:41 -05:00
|
|
|
mp_raise_type(&mp_type_KeyError);
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
CHECK_ERROR(res);
|
|
|
|
return mp_obj_new_bytes(val.data, val.size);
|
|
|
|
} else {
|
|
|
|
// store
|
|
|
|
DBT key, val;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(index, &key);
|
|
|
|
buf_to_dbt(value, &val);
|
2016-06-14 14:51:59 -04:00
|
|
|
int res = __bt_put(self->db, &key, &val, 0);
|
|
|
|
CHECK_ERROR(res);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t btree_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2016-08-01 17:21:05 -04:00
|
|
|
mp_obj_btree_t *self = MP_OBJ_TO_PTR(lhs_in);
|
|
|
|
switch (op) {
|
2017-11-23 21:04:24 -05:00
|
|
|
case MP_BINARY_OP_CONTAINS: {
|
2016-08-01 17:21:05 -04:00
|
|
|
DBT key, val;
|
2022-06-10 13:28:56 -04:00
|
|
|
buf_to_dbt(rhs_in, &key);
|
2016-08-01 17:21:05 -04:00
|
|
|
int res = __bt_get(self->db, &key, &val, 0);
|
|
|
|
CHECK_ERROR(res);
|
|
|
|
return mp_obj_new_bool(res != RET_SPECIAL);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// op not supported
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2016-06-14 14:51:59 -04:00
|
|
|
STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&btree_close_obj) },
|
2016-12-04 17:50:55 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&btree_flush_obj) },
|
2016-06-14 14:51:59 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&btree_get_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&btree_put_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_seq), MP_ROM_PTR(&btree_seq_obj) },
|
2016-06-20 08:50:31 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&btree_keys_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&btree_values_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&btree_items_obj) },
|
2016-06-14 14:51:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t btree_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
// Save on qstr's, reuse same as for module
|
|
|
|
.name = MP_QSTR_btree,
|
|
|
|
.print = btree_print,
|
2016-06-23 13:08:37 -04:00
|
|
|
.getiter = btree_getiter,
|
2016-06-16 10:31:24 -04:00
|
|
|
.iternext = btree_iternext,
|
2016-08-01 17:21:05 -04:00
|
|
|
.binary_op = btree_binary_op,
|
2016-06-14 14:51:59 -04:00
|
|
|
.subscr = btree_subscr,
|
|
|
|
.locals_dict = (void *)&btree_locals_dict,
|
|
|
|
};
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2016-06-14 14:51:59 -04:00
|
|
|
|
2019-11-01 02:25:40 -04:00
|
|
|
STATIC const FILEVTABLE btree_stream_fvtable = {
|
2016-07-30 17:40:35 -04:00
|
|
|
mp_stream_posix_read,
|
|
|
|
mp_stream_posix_write,
|
|
|
|
mp_stream_posix_lseek,
|
|
|
|
mp_stream_posix_fsync
|
|
|
|
};
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2016-06-14 14:51:59 -04:00
|
|
|
STATIC mp_obj_t mod_btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2016-08-05 17:10:22 -04:00
|
|
|
{ MP_QSTR_flags, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_cachesize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_pagesize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_minkeypage, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
2016-06-14 14:51:59 -04:00
|
|
|
};
|
|
|
|
|
2016-07-30 17:40:35 -04:00
|
|
|
// Make sure we got a stream object
|
|
|
|
mp_get_stream_raise(pos_args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
|
2016-06-14 14:51:59 -04:00
|
|
|
|
|
|
|
struct {
|
2016-08-05 17:10:22 -04:00
|
|
|
mp_arg_val_t flags;
|
|
|
|
mp_arg_val_t cachesize;
|
|
|
|
mp_arg_val_t pagesize;
|
|
|
|
mp_arg_val_t minkeypage;
|
2016-06-14 14:51:59 -04:00
|
|
|
} args;
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
|
|
|
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
|
2016-08-05 17:10:22 -04:00
|
|
|
BTREEINFO openinfo = {0};
|
|
|
|
openinfo.flags = args.flags.u_int;
|
|
|
|
openinfo.cachesize = args.cachesize.u_int;
|
|
|
|
openinfo.psize = args.pagesize.u_int;
|
|
|
|
openinfo.minkeypage = args.minkeypage.u_int;
|
2016-06-14 14:51:59 -04:00
|
|
|
|
2018-08-14 03:41:23 -04:00
|
|
|
DB *db = __bt_open(MP_OBJ_TO_PTR(pos_args[0]), &btree_stream_fvtable, &openinfo, /*dflags*/ 0);
|
2016-07-23 17:29:32 -04:00
|
|
|
if (db == NULL) {
|
2016-10-06 22:52:14 -04:00
|
|
|
mp_raise_OSError(errno);
|
2016-07-23 17:29:32 -04:00
|
|
|
}
|
2020-04-20 09:04:40 -04:00
|
|
|
return MP_OBJ_FROM_PTR(btree_new(db, pos_args[0]));
|
2016-06-14 14:51:59 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_btree_open_obj, 1, mod_btree_open);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t mp_module_btree_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_btree) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mod_btree_open_obj) },
|
2016-07-02 08:58:13 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_INCL), MP_ROM_INT(FLAG_END_KEY_INCL) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_DESC), MP_ROM_INT(FLAG_DESC) },
|
2016-06-14 14:51:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_btree_globals, mp_module_btree_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_btree = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_btree_globals,
|
|
|
|
};
|
2022-04-20 02:06:22 -04:00
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_btree, mp_module_btree);
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2016-06-14 14:51:59 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_PY_BTREE
|