STATIC mp_obj_t btree_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { mp_obj_btree_t *self = MP_OBJ_TO_PTR(lhs_in); switch (op) { case MP_BINARY_OP_IN: { mp_uint_t v; DBT key, val; key.data = (void*)mp_obj_str_get_data(rhs_in, &v); key.size = v; 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; } }
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; // Different ports may have different type sizes mp_uint_t v; key.data = (void*)mp_obj_str_get_data(args[1], &v); key.size = v; int res = __bt_get(self->db, &key, &val, 0); if (res == RET_SPECIAL) { if (n_args > 2) { return args[2]; } else { return mp_const_none; } } CHECK_ERROR(res); return mp_obj_new_bytes(val.data, val.size); }
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); // Different ports may have different type sizes mp_uint_t v; if (value == MP_OBJ_NULL) { // delete DBT key; key.data = (void*)mp_obj_str_get_data(index, &v); key.size = v; int res = __bt_delete(self->db, &key, 0); if (res == RET_SPECIAL) { nlr_raise(mp_obj_new_exception(&mp_type_KeyError)); } CHECK_ERROR(res); return mp_const_none; } else if (value == MP_OBJ_SENTINEL) { // load DBT key, val; key.data = (void*)mp_obj_str_get_data(index, &v); key.size = v; int res = __bt_get(self->db, &key, &val, 0); if (res == RET_SPECIAL) { nlr_raise(mp_obj_new_exception(&mp_type_KeyError)); } CHECK_ERROR(res); return mp_obj_new_bytes(val.data, val.size); } else { // store DBT key, val; key.data = (void*)mp_obj_str_get_data(index, &v); key.size = v; val.data = (void*)mp_obj_str_get_data(value, &v); val.size = v; int res = __bt_put(self->db, &key, &val, 0); CHECK_ERROR(res); return mp_const_none; } }