70 #define bAdr(p) *(ion_bpp_address_t *) (p) 71 #define eAdr(p) *(ion_bpp_external_address_t *) (p) 74 #define childLT(k) bAdr((char *) k - sizeof(ion_bpp_address_t)) 76 #define rec(k) eAdr((char *) (k) + h->keySize) 77 #define childGE(k) bAdr((char *) (k) + h->keySize + sizeof(ion_bpp_external_address_t)) 80 #define leaf(b) b->p->leaf 81 #define ct(b) b->p->ct 82 #define next(b) b->p->next 83 #define prev(b) b->p->prev 84 #define fkey(b) & b->p->fkey 85 #define lkey(b) (fkey(b) + ks((ct(b) - 1))) 86 #define p(b) (char *) (b->p) 89 #define ks(ct) ((ct) * h->ks) 113 unsigned int leaf : 1;
114 unsigned int ct : 15;
151 #define error(rc) lineError(__LINE__, rc) 209 if (0 != fseek(h->
fp, buf->
adr, SEEK_SET)) {
213 for (i = 0; i < len; i++) {
214 if (1 != fwrite(&buf[i].
p->
leaf,
sizeof(buf->
p->
leaf), 1, h->
fp)) {
218 if (1 != fwrite(&buf[i].
p->
ct,
sizeof(buf->
p->
ct), 1, h->
fp)) {
222 if (1 != fwrite(&buf[i].
p->
prev,
sizeof(buf->
p->
prev), 1, h->
fp)) {
226 if (1 != fwrite(&buf[i].
p->
next,
sizeof(buf->
p->
next), 1, h->
fp)) {
234 if (1 != fwrite(&buf[i].
p->
fkey,
sizeof(buf->
p->
fkey), 1, h->
fp)) {
255 if ((rc =
flush(handle, &h->
root)) != 0) {
264 if ((rc =
flush(handle, buf)) != 0) {
295 if (buf->
valid && (buf->
adr == adr)) {
304 if (buf->
adr != adr) {
306 if ((rc =
flush(handle, buf)) != 0) {
352 if ((rc =
assignBuf(handle, adr, &buf)) != 0) {
380 if (0 != fseek(h->
fp, buf->
adr, SEEK_SET)) {
384 for (i = 0; i < len; i++) {
385 if (1 != fread(&buf[i].
p->
leaf,
sizeof(buf->
p->
leaf), 1, h->
fp)) {
389 if (1 != fread(&buf[i].
p->
ct,
sizeof(buf->
p->
ct), 1, h->
fp)) {
393 if (1 != fread(&buf[i].
p->
prev,
sizeof(buf->
p->
prev), 1, h->
fp)) {
397 if (1 != fread(&buf[i].
p->
next,
sizeof(buf->
p->
next), 1, h->
fp)) {
405 if (1 != fread(&buf[i].
p->
fkey,
sizeof(buf->
p->
fkey), 1, h->
fp)) {
458 *mkey =
fkey(buf) +
ks(m);
461 if ((cc < 0) || ((cc == 0) && (
MODE_FGEQ == mode))) {
465 else if ((cc > 0) || ((cc == 0) && (
MODE_LLEQ == mode))) {
487 if (rec <
rec(*mkey)) {
491 else if (rec >
rec(*mkey)) {
525 *mkey =
fkey(buf) +
ks(ub + 1);
528 if ((ub ==
ct(buf) - 1) || ((ub != -1) && (cc <= 0))) {
529 *mkey =
fkey(buf) +
ks(ub);
537 *mkey =
fkey(buf) +
ks(lb);
540 if ((lb <
ct(buf) - 1) && (cc < 0)) {
541 *mkey =
fkey(buf) +
ks(lb + 1);
619 k0Max = h->
maxCt - 1;
620 knMax = h->
maxCt - 1;
622 k0Min = (h->
maxCt / 2) + 1;
623 knMin = (h->
maxCt / 2) + 1;
627 k0Max = h->
maxCt - 1;
629 k0Min = (h->
maxCt / 2) + 1;
630 knMin = ((h->
maxCt + 1) / 2) + 1;
635 if ((iu == 0) || (ct > (k0Max + (iu - 1) * knMax))) {
650 prev(tmp[iu]) = tmp[iu - 1]->
adr;
652 next(tmp[iu - 1]) = tmp[iu]->
adr;
659 else if ((iu > 1) && (ct < (k0Min + (iu - 1) * knMin))) {
664 if (
leaf(gbuf) && tmp[iu - 1]->
adr) {
680 for (i = 0; i < iu; i++) {
700 if (
leaf(gbuf) &&
next(tmp[iu - 1])) {
703 if ((rc =
readDisk(handle,
next(tmp[iu - 1]), &buf)) != 0) {
718 len =
ks(
ct(pbuf)) - (pkey -
fkey(pbuf)) + sw;
719 memmove(pkey, pkey - sw, len);
722 len =
ks(
ct(pbuf)) - (pkey -
fkey(pbuf));
723 memmove(pkey + sw, pkey, len);
731 ct(pbuf) += iu - is - 1;
738 for (i = 0; i < iu; i++) {
749 memcpy(pkey, gkey,
ks(1));
765 memcpy(pkey, gkey,
ks(1));
774 memcpy(
fkey(tmp[i]), gkey,
ks(
ct(tmp[i])));
777 gkey +=
ks(
ct(tmp[i]));
789 for (i = 0; i < iu; i++) {
844 if (*pkey ==
lkey(pbuf)) {
866 memcpy(gkey,
fkey(tmp[0]),
ks(
ct(tmp[0])));
867 gkey +=
ks(
ct(tmp[0]));
868 ct(gbuf) =
ct(tmp[0]);
872 memcpy(gkey, *pkey,
ks(1));
878 memcpy(gkey,
fkey(tmp[1]),
ks(
ct(tmp[1])));
879 gkey +=
ks(
ct(tmp[1]));
880 ct(gbuf) +=
ct(tmp[1]);
884 memcpy(gkey, *pkey +
ks(1),
ks(1));
890 memcpy(gkey,
fkey(tmp[2]),
ks(
ct(tmp[2])));
891 ct(gbuf) +=
ct(tmp[2]);
967 for (i = 0; i < (bufCt + 6) * h->
sectorSize + 2 * h->
ks; i++) {
977 for (i = 0; i < bufCt; i++) {
1004 if ((rc =
readDisk(h, 0, &root)) != 0) {
1017 #if defined(ARDUINO) 1050 #if defined(ARDUINO) 1135 if ((lgeqkey -
fkey(buf)) / (h->
ks) == (
ct(buf))) {
1145 *rec =
rec(lgeqkey);
1178 unsigned int keyOff;
1182 unsigned int lastGEkey;
1192 if (
ct(root) == 3 * h->
maxCt) {
1199 if ((rc =
scatter(handle, root,
fkey(root), 0, tmp)) != 0) {
1240 keyOff = mkey -
fkey(buf);
1241 len =
ks(
ct(buf)) - keyOff;
1244 memmove(mkey +
ks(1), mkey, len);
1258 if (!keyOff && lastLTvalid) {
1262 if ((rc =
readDisk(handle, lastGE, &tbuf)) != 0) {
1300 if ((rc =
gather(handle, buf, &mkey, tmp)) != 0) {
1304 if ((rc =
scatter(handle, buf, mkey, 3, tmp)) != 0) {
1321 if ((cc >= 0) || (mkey !=
fkey(buf))) {
1325 lastGEkey = mkey -
fkey(buf);
1362 if (
ct(root) == 3 * h->
maxCt) {
1369 if ((rc =
scatter(handle, root,
fkey(root), 0, tmp)) != 0) {
1424 if ((rc =
gather(handle, buf, &mkey, tmp)) != 0) {
1428 if ((rc =
scatter(handle, buf, mkey, 3, tmp)) != 0) {
1464 unsigned int keyOff;
1468 unsigned int lastGEkey;
1492 keyOff = mkey -
fkey(buf);
1493 len =
ks(
ct(buf) - 1) - keyOff;
1496 memmove(mkey, mkey +
ks(1), len);
1506 if (!keyOff && lastLTvalid) {
1510 if ((rc =
readDisk(handle, lastGE, &tbuf)) != 0) {
1514 tkey =
fkey(tbuf) + lastGEkey;
1543 if (
ct(cbuf) == h->
maxCt / 2) {
1545 if ((rc =
gather(handle, buf, &mkey, tmp)) != 0) {
1550 if ((buf == root) && (
ct(root) == 2) && (
ct(gbuf) < (3 * (3 * h->
maxCt)) / 4)) {
1557 if ((rc =
scatter(handle, buf, mkey, 3, tmp)) != 0) {
1574 if ((cc >= 0) || (mkey !=
fkey(buf))) {
1578 lastGEkey = mkey -
fkey(buf);
1610 while (!
leaf(buf)) {
1651 while (!
leaf(buf)) {
1680 if ((buf = h->
curBuf) == NULL) {
1734 if ((buf = h->
curBuf) == NULL) {
1748 pkey =
fkey(buf) +
ks((
ct(buf) - 1));
ion_bpp_err_t b_close(ion_bpp_handle_t handle)
unsigned char ion_byte_t
A byte type.
enum ION_BPP_ERR ion_bpp_err_t
ion_boolean_e ion_bpp_bool_t
ion_bpp_err_t b_insert(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t rec)
static ion_bpp_err_t lineError(int lineno, ion_bpp_err_t rc)
static ion_bpp_err_t gather(ion_bpp_handle_t handle, ion_bpp_buffer_t *pbuf, ion_bpp_key_t **pkey, ion_bpp_buffer_t **tmp)
ion_boolean_t ion_fexists(char *name)
ion_bpp_address_t childLT
ion_bpp_err_t b_find_last_key(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t *rec)
static ion_bpp_err_t flush(ion_bpp_handle_t handle, ion_bpp_buffer_t *buf)
ion_bpp_err_t b_get(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t *rec)
static ion_bpp_err_t assignBuf(ion_bpp_handle_t handle, ion_bpp_address_t adr, ion_bpp_buffer_t **b)
char ion_err_t
The error type used to store error codes.
static ion_bpp_err_t writeDisk(ion_bpp_buffer_t *buf)
ion_err_t ion_fseek(ion_file_handle_t file, ion_file_offset_t seek_to, int origin)
char(* ion_bpp_comparison_t)(ion_key_t key1, ion_key_t key2, ion_key_size_t size)
ion_bpp_comparison_t comp
ion_file_handle_t ion_fopen(char *name)
ion_bpp_err_t b_open(ion_bpp_open_t info, ion_bpp_handle_t *handle)
static ion_bpp_address_t allocAdr(ion_bpp_handle_t handle)
ion_bpp_address_t nextFreeAdr
long ion_bpp_external_address_t
static ion_bpp_err_t gatherRoot(ion_bpp_handle_t handle)
static int search(ion_bpp_handle_t handle, ion_bpp_buffer_t *buf, void *key, ion_bpp_external_address_t rec, ion_bpp_key_t **mkey, ion_bpp_mode_e mode)
ion_err_t ion_fclose(ion_file_handle_t file)
ion_bpp_err_t b_find_prev_key(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t *rec)
ion_bpp_err_t b_update(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t rec)
struct ion_bpp_buffer_tag * next
static ion_bpp_err_t readDisk(ion_bpp_handle_t handle, ion_bpp_address_t adr, ion_bpp_buffer_t **b)
ion_bpp_buffer_t * curBuf
struct ion_bpp_buffer_tag * prev
ion_err_t ion_fwrite_at(ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *to_write)
int ion_key_size_t
The size (length) of a dictionary key in bytes.
ion_file_offset_t ion_ftell(ion_file_handle_t file)
struct ion_bpp_buffer_tag ion_bpp_buffer_t
ion_bpp_err_t b_find_first_key(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t *rec)
ion_err_t ion_fread_at(ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *write_to)
ion_bpp_err_t b_delete(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t *rec)
ion_bpp_err_t b_find_first_greater_or_equal(ion_bpp_handle_t handle, void *key, void *mkey, ion_bpp_external_address_t *rec)
static ion_bpp_err_t flushAll(ion_bpp_handle_t handle)
enum ION_BPP_MODE ion_bpp_mode_e
struct ion_bpp_h_node_tag ion_bpp_h_node_t
static ion_bpp_err_t scatterRoot(ion_bpp_handle_t handle)
ion_bpp_comparison_t comp
ion_bpp_err_t b_find_next_key(ion_bpp_handle_t handle, void *key, ion_bpp_external_address_t *rec)
static ion_bpp_err_t scatter(ion_bpp_handle_t handle, ion_bpp_buffer_t *pbuf, ion_bpp_key_t *pkey, int is, ion_bpp_buffer_t **tmp)