1 /* $Id: isamb.c,v 1.41 2004-06-03 00:23:48 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include <yaz/xmalloc.h>
41 #define ISAMB_DATA_OFFSET 3
43 /* maximum size of encoded buffer */
44 #define DST_ITEM_MAX 256
46 #define ISAMB_MAX_LEVEL 10
47 /* approx 2*max page + max size of item */
48 #define DST_BUF_SIZE 16840
50 #define ISAMB_CACHE_ENTRY_SIZE 4096
52 /* CAT_MAX: _must_ be power of 2 */
54 #define CAT_MASK (CAT_MAX-1)
55 /* CAT_NO: <= CAT_MAX */
58 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
59 #define ISAMB_PTR_CODEC 1
61 struct ISAMB_cache_entry {
66 struct ISAMB_cache_entry *next;
72 struct ISAMB_head head;
73 struct ISAMB_cache_entry *cache_entries;
80 struct ISAMB_file *file;
82 int cache; /* 0=no cache, 1=use cache, -1=dummy isam (for testing only) */
83 int log_io; /* log level for bf_read/bf_write calls */
84 int log_freelist; /* log level for freelist handling */
85 int skipped_numbers; /* on a leaf node */
87 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
88 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
102 void *decodeClientData;
110 int maxlevel; /* total depth */
113 int skipped_numbers; /* on a leaf node */
114 int returned_numbers;
115 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
116 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
117 struct ISAMB_block **block;
121 static void encode_ptr (char **dst, unsigned pos)
123 unsigned char *bp = (unsigned char*) *dst;
127 *bp++ = 128 | (pos & 127);
134 static void encode_ptr (char **dst, unsigned pos)
136 memcpy(*dst, &pos, sizeof(pos));
137 (*dst) += sizeof(pos);
142 static void decode_ptr (char **src1, int *pos)
144 unsigned char **src = (unsigned char **) src1;
149 while (((c = *(*src)++) & 128))
151 d += ((c & 127) << r);
158 static void decode_ptr (char **src, int *pos)
160 memcpy (pos, *src, sizeof(*pos));
161 (*src) += sizeof(*pos);
165 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
168 ISAMB isamb = xmalloc (sizeof(*isamb));
172 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
173 memcpy (isamb->method, method, sizeof(*method));
174 isamb->no_cat = CAT_NO;
176 isamb->log_freelist = 0;
177 isamb->cache = cache;
178 isamb->skipped_numbers=0;
179 isamb->returned_numbers=0;
180 for (i=0;i<ISAMB_MAX_LEVEL;i++)
181 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
184 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
185 for (i = 0; i<isamb->no_cat; i++)
187 char fname[DST_BUF_SIZE];
188 isamb->file[i].cache_entries = 0;
189 isamb->file[i].head_dirty = 0;
190 sprintf (fname, "%s%c", name, i+'A');
192 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
195 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
198 if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
199 &isamb->file[i].head))
201 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
202 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
203 isamb->file[i].head.block_size = b_size;
204 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
205 isamb->file[i].head.free_list = 0;
207 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
208 isamb->file[i].head_dirty = 0;
209 assert(isamb->file[i].head.block_size == b_size);
213 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
218 static void flush_blocks (ISAMB b, int cat)
220 while (b->file[cat].cache_entries)
222 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
223 b->file[cat].cache_entries = ce_this->next;
227 yaz_log (b->log_io, "bf_write: flush_blocks");
228 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
230 xfree (ce_this->buf);
235 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
237 int cat = pos&CAT_MASK;
238 int off = ((pos/CAT_MAX) &
239 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
240 * b->file[cat].head.block_size;
241 int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
243 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
248 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
249 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
252 if ((*ce)->pos == norm)
255 *ce = (*ce)->next; /* remove from list */
257 ce_this->next = b->file[cat].cache_entries; /* move to front */
258 b->file[cat].cache_entries = ce_this;
262 memcpy (ce_this->buf + off, userbuf,
263 b->file[cat].head.block_size);
267 memcpy (userbuf, ce_this->buf + off,
268 b->file[cat].head.block_size);
275 assert (ce_last && *ce_last);
277 *ce_last = 0; /* remove the last entry from list */
280 yaz_log (b->log_io, "bf_write: get_block");
281 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
283 xfree (ce_this->buf);
286 ce_this = xmalloc (sizeof(*ce_this));
287 ce_this->next = b->file[cat].cache_entries;
288 b->file[cat].cache_entries = ce_this;
289 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
291 yaz_log (b->log_io, "bf_read: get_block");
292 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
293 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
296 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
302 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
308 void isamb_close (ISAMB isamb)
311 for (i=0;isamb->accessed_nodes[i];i++)
312 logf(LOG_DEBUG,"isamb_close level leaf-%d: %d read, %d skipped",
313 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
314 logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
315 isamb->skipped_numbers, isamb->returned_numbers);
316 for (i = 0; i<isamb->no_cat; i++)
318 flush_blocks (isamb, i);
319 if (isamb->file[i].head_dirty)
320 bf_write (isamb->file[i].bf, 0, 0,
321 sizeof(struct ISAMB_head), &isamb->file[i].head);
323 bf_close (isamb->file[i].bf);
326 xfree (isamb->method);
330 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
332 int cat = pos&CAT_MASK;
333 struct ISAMB_block *p;
336 p = xmalloc (sizeof(*p));
338 p->cat = pos & CAT_MASK;
339 p->buf = xmalloc (b->file[cat].head.block_size);
342 if (!get_block (b, pos, p->buf, 0))
344 yaz_log (b->log_io, "bf_read: open_block");
345 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
347 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
348 (long) pos, (long) pos/CAT_MAX);
352 p->bytes = p->buf + ISAMB_DATA_OFFSET;
354 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
357 yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
359 assert (p->size >= 0);
363 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
364 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
368 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
370 struct ISAMB_block *p;
372 p = xmalloc (sizeof(*p));
373 p->buf = xmalloc (b->file[cat].head.block_size);
375 if (!b->file[cat].head.free_list)
378 block_no = b->file[cat].head.last_block++;
379 p->pos = block_no * CAT_MAX + cat;
383 p->pos = b->file[cat].head.free_list;
384 assert((p->pos & CAT_MASK) == cat);
385 if (!get_block (b, p->pos, p->buf, 0))
387 yaz_log (b->log_io, "bf_read: new_block");
388 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
390 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
391 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
395 yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
396 cat, p->pos/CAT_MAX);
397 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
400 b->file[cat].head_dirty = 1;
401 memset (p->buf, 0, b->file[cat].head.block_size);
402 p->bytes = p->buf + ISAMB_DATA_OFFSET;
408 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
412 struct ISAMB_block *new_leaf (ISAMB b, int cat)
414 return new_block (b, 1, cat);
418 struct ISAMB_block *new_int (ISAMB b, int cat)
420 return new_block (b, 0, cat);
423 static void check_block (ISAMB b, struct ISAMB_block *p)
432 char *startp = p->bytes;
434 char *endp = p->bytes + p->size;
437 decode_ptr (&src, &pos);
438 assert ((pos&CAT_MASK) == p->cat);
442 decode_ptr (&src, &item_len);
443 assert (item_len > 0 && item_len < 30);
445 decode_ptr (&src, &pos);
446 assert ((pos&CAT_MASK) == p->cat);
451 void close_block (ISAMB b, struct ISAMB_block *p)
457 yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
458 p->pos, p->cat, p->pos/CAT_MAX);
459 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
460 b->file[p->cat].head.free_list = p->pos;
461 if (!get_block (b, p->pos, p->buf, 1))
463 yaz_log (b->log_io, "bf_write: close_block (deleted)");
464 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
469 int size = p->size + ISAMB_DATA_OFFSET;
470 assert (p->size >= 0);
472 p->buf[1] = size & 255;
473 p->buf[2] = size >> 8;
475 if (!get_block (b, p->pos, p->buf, 1))
477 yaz_log (b->log_io, "bf_write: close_block");
478 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
481 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
486 int insert_sub (ISAMB b, struct ISAMB_block **p,
487 void *new_item, int *mode,
489 struct ISAMB_block **sp,
490 void *sub_item, int *sub_size,
493 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
495 ISAMC_I *stream, struct ISAMB_block **sp,
496 void *split_item, int *split_size, void *last_max_item)
498 char *startp = p->bytes;
500 char *endp = p->bytes + p->size;
502 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
503 char sub_item[DST_ITEM_MAX];
509 assert(p->size >= 0);
510 decode_ptr (&src, &pos);
516 decode_ptr (&src, &item_len);
517 d = (*b->method->compare_item)(src, lookahead_item);
520 sub_p1 = open_block (b, pos);
522 more = insert_sub (b, &sub_p1, lookahead_item, mode,
524 sub_item, &sub_size, src);
529 decode_ptr (&src, &pos);
533 sub_p1 = open_block (b, pos);
535 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
536 sub_item, &sub_size, last_max_item);
540 /* there was a split - must insert pointer in this one */
541 char dst_buf[DST_BUF_SIZE];
544 assert (sub_size < 30 && sub_size > 1);
546 memcpy (dst, startp, src - startp);
550 encode_ptr (&dst, sub_size); /* sub length and item */
551 memcpy (dst, sub_item, sub_size);
554 encode_ptr (&dst, sub_p2->pos); /* pos */
556 if (endp - src) /* remaining data */
558 memcpy (dst, src, endp - src);
561 p->size = dst - dst_buf;
562 assert (p->size >= 0);
563 if (p->size <= b->file[p->cat].head.block_max)
565 memcpy (startp, dst_buf, dst - dst_buf);
574 half = src + b->file[p->cat].head.block_size/2;
575 decode_ptr (&src, &pos);
578 decode_ptr (&src, split_size);
580 decode_ptr (&src, &pos);
582 p_new_size = src - dst_buf;
583 memcpy (p->bytes, dst_buf, p_new_size);
585 decode_ptr (&src, split_size);
586 memcpy (split_item, src, *split_size);
589 *sp = new_int (b, p->cat);
590 (*sp)->size = endp - src;
591 memcpy ((*sp)->bytes, src, (*sp)->size);
593 p->size = p_new_size;
596 close_block (b, sub_p2);
598 close_block (b, sub_p1);
603 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
604 int *lookahead_mode, ISAMC_I *stream,
605 struct ISAMB_block **sp2,
606 void *sub_item, int *sub_size,
609 struct ISAMB_block *p = *sp1;
610 char *src = 0, *endp = 0;
611 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
613 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
614 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
616 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
617 char *cut = dst_buf + quater * 2;
618 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
621 char cut_item_buf[DST_ITEM_MAX];
622 int cut_item_size = 0;
626 char file_item_buf[DST_ITEM_MAX];
627 char *file_item = file_item_buf;
630 endp = p->bytes + p->size;
631 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
636 char *lookahead_next;
640 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
644 dst_item = lookahead_item;
645 if (!*lookahead_mode)
647 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
648 assert (*lookahead_mode);
652 dst_item = file_item_buf;
653 if (!*lookahead_mode && d == 0)
657 else if (!half1 && dst > cut)
659 char *dst_item_0 = dst_item;
660 half1 = dst; /* candidate for splitting */
662 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
664 cut_item_size = dst_item - dst_item_0;
665 memcpy (cut_item_buf, dst_item_0, cut_item_size);
670 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
680 lookahead_next = lookahead_item;
681 if (!(*stream->read_item)(stream->clientData,
688 if (lookahead_item && max_item &&
689 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
700 lookahead_next = lookahead_item;
701 if (!(*stream->read_item)(stream->clientData,
702 &lookahead_next, lookahead_mode))
709 file_item = file_item_buf;
710 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
716 file_item = file_item_buf;
717 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
721 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
722 while (lookahead_item)
724 char *dst_item = lookahead_item;
728 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
733 if (!*lookahead_mode)
735 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
738 else if (!half1 && dst > cut)
740 char *dst_item_0 = dst_item;
741 half1 = dst; /* candidate for splitting */
743 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
745 cut_item_size = dst_item - dst_item_0;
746 memcpy (cut_item_buf, dst_item_0, cut_item_size);
751 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
760 dst_item = lookahead_item;
761 if (!(*stream->read_item)(stream->clientData, &dst_item,
768 new_size = dst - dst_buf;
769 if (p && p->cat != b->no_cat-1 &&
770 new_size > b->file[p->cat].head.block_max)
772 /* non-btree block will be removed */
775 /* delete it too!! */
776 p = 0; /* make a new one anyway */
779 { /* must create a new one */
781 for (i = 0; i < b->no_cat; i++)
782 if (new_size <= b->file[i].head.block_max)
788 if (new_size > b->file[p->cat].head.block_max)
791 char *cut_item = cut_item_buf;
797 p->size = half1 - dst_buf;
798 memcpy (p->bytes, dst_buf, half1 - dst_buf);
801 *sp2 = new_leaf (b, p->cat);
803 (*b->method->code_reset)(c2);
805 first_dst = (*sp2)->bytes;
807 (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
809 memcpy (first_dst, half2, dst - half2);
811 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
814 memcpy (sub_item, cut_item_buf, cut_item_size);
815 *sub_size = cut_item_size;
819 memcpy (p->bytes, dst_buf, dst - dst_buf);
822 (*b->method->code_stop)(ISAMC_DECODE, c1);
823 (*b->method->code_stop)(ISAMC_ENCODE, c2);
828 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
831 struct ISAMB_block **sp,
832 void *sub_item, int *sub_size,
835 if (!*p || (*p)->leaf)
836 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
839 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
843 int isamb_unlink (ISAMB b, ISAMC_P pos)
845 struct ISAMB_block *p1;
849 p1 = open_block(b, pos);
855 char *src = p1->bytes + p1->offset;
857 decode_ptr(&src, &sub_p);
858 isamb_unlink(b, sub_p);
860 while (src != p1->bytes + p1->size)
862 decode_ptr(&src, &item_len);
864 decode_ptr(&src, &sub_p);
865 isamb_unlink(b, sub_p);
872 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
874 char item_buf[DST_ITEM_MAX];
886 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
891 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
894 struct ISAMB_block *p = 0, *sp = 0;
895 char sub_item[DST_ITEM_MAX];
899 p = open_block (b, pos);
900 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
901 sub_item, &sub_size, 0);
903 { /* increase level of tree by one */
904 struct ISAMB_block *p2 = new_int (b, p->cat);
905 char *dst = p2->bytes + p2->size;
907 encode_ptr (&dst, p->pos);
908 assert (sub_size < 20);
909 encode_ptr (&dst, sub_size);
910 memcpy (dst, sub_item, sub_size);
912 encode_ptr (&dst, sp->pos);
914 p2->size = dst - p2->bytes;
915 pos = p2->pos; /* return new super page */
920 pos = p->pos; /* return current one (again) */
926 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
928 ISAMB_PP pp = xmalloc (sizeof(*pp));
932 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
939 pp->skipped_numbers=0;
940 pp->returned_numbers=0;
941 for (i=0;i<ISAMB_MAX_LEVEL;i++)
942 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
945 struct ISAMB_block *p = open_block (isamb, pos);
946 char *src = p->bytes + p->offset;
947 pp->block[pp->level] = p;
949 pp->total_size += p->size;
955 decode_ptr (&src, &pos);
956 p->offset = src - p->bytes;
958 pp->accessed_nodes[pp->level]++;
960 pp->block[pp->level+1] = 0;
961 pp->maxlevel=pp->level;
967 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
969 return isamb_pp_open_x (isamb, pos, 0);
972 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
977 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
978 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
979 for (i=pp->maxlevel;i>=0;i--)
980 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
981 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: %d read, %d skipped", i,
982 pp->accessed_nodes[i], pp->skipped_nodes[i]);
983 pp->isamb->skipped_numbers += pp->skipped_numbers;
984 pp->isamb->returned_numbers += pp->returned_numbers;
985 for (i=pp->maxlevel;i>=0;i--)
987 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
988 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
991 *size = pp->total_size;
993 *blocks = pp->no_blocks;
994 for (i = 0; i <= pp->level; i++)
995 close_block (pp->isamb, pp->block[i]);
1000 int isamb_block_info (ISAMB isamb, int cat)
1002 if (cat >= 0 && cat < isamb->no_cat)
1003 return isamb->file[cat].head.block_size;
1007 void isamb_pp_close (ISAMB_PP pp)
1009 isamb_pp_close_x (pp, 0, 0);
1012 /* simple recursive dumper .. */
1013 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1017 char prefix_str[1024];
1020 struct ISAMB_block *p = open_block (b, pos);
1021 sprintf(prefix_str, "%*s %d cat=%d size=%d max=%d", level*2, "",
1022 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1024 sprintf(prefix_str, "%*s %d", level*2, "", pos);
1027 while (p->offset < p->size)
1029 char *src = p->bytes + p->offset;
1031 (*b->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1033 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1034 p->offset = src - (char*) p->bytes;
1036 assert(p->offset == p->size);
1040 char *src = p->bytes + p->offset;
1044 decode_ptr (&src, &sub);
1045 p->offset = src - (char*) p->bytes;
1047 isamb_dump_r(b, sub, pr, level+1);
1049 while (p->offset < p->size)
1051 decode_ptr (&src, &item_len);
1052 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1054 decode_ptr (&src, &sub);
1056 p->offset = src - (char*) p->bytes;
1058 isamb_dump_r(b, sub, pr, level+1);
1065 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1067 return isamb_dump_r(b, pos, pr, 0);
1071 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1072 debug the more complex pp_read that also forwards. May be deleted near end
1073 of 2004, if it has not shown to be useful */
1076 int isamb_pp_read (ISAMB_PP pp, void *buf)
1080 struct ISAMB_block *p = pp->block[pp->level];
1084 while (p->offset == p->size)
1087 while (p->offset == p->size)
1091 close_block (pp->isamb, pp->block[pp->level]);
1092 pp->block[pp->level] = 0;
1094 p = pp->block[pp->level];
1097 src = p->bytes + p->offset;
1099 decode_ptr (&src, &item_len);
1101 decode_ptr (&src, &pos);
1103 p->offset = src - (char*) p->bytes;
1109 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1111 pp->total_size += p->size;
1118 src = p->bytes + p->offset;
1119 decode_ptr (&src, &pos);
1120 p->offset = src - (char*) p->bytes;
1124 assert (p->offset < p->size);
1126 src = p->bytes + p->offset;
1127 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1129 p->offset = src - (char*) p->bytes;
1130 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1135 int isamb_pp_read (ISAMB_PP pp, void *buf)
1137 return isamb_pp_forward(pp,buf,0);
1141 #define NEW_FORWARD 0
1143 #if NEW_FORWARD == 1
1145 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1146 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1147 struct ISAMB_block *p = pp->block[pp->level];
1150 if (p->offset == p->size) {
1152 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1154 return 0; /* at end of leaf */
1156 src=p->bytes + p->offset;
1158 (*pp->isamb->method->code_item)
1159 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1160 p->offset = src - (char*) p->bytes;
1162 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1165 } /* read_on_leaf */
1168 static int isamb_pp_climb_level(ISAMB_PP pp, int *pos)
1169 { /* climbs higher in the tree, until finds a level with data left */
1170 /* returns the node to (consider to) descend to in *pos) */
1171 struct ISAMB_block *p = pp->block[pp->level];
1174 assert(p->offset <= p->size);
1178 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1182 close_block(pp->isamb, pp->block[pp->level]);
1183 pp->block[pp->level]=0;
1185 p=pp->block[pp->level];
1186 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to node %d ofs=%d",
1189 assert (p->offset <= p->size);
1190 if (p->offset == p->size ) {
1191 /* we came from the last pointer, climb on */
1192 if (!isamb_pp_climb_level(pp,pos))
1194 p=pp->block[pp->level];
1196 /* skip the child we just came from */
1197 assert (p->offset < p->size );
1198 src=p->bytes + p->offset;
1199 decode_ptr(&src, &item_len);
1201 decode_ptr(&src, pos);
1202 p->offset=src - (char *)p->bytes;
1207 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1208 { /* scans a upper node until it finds a child <= untilbuf */
1209 /* pp points to the key value, as always. pos is the child read from */
1211 /* if all values are too small, returns the last child in the node */
1212 /* FIXME - this can be detected, and avoided by looking at the */
1213 /* parent node, but that gets messy. Presumably the cost is */
1214 /* pretty low anyway */
1215 struct ISAMB_block *p = pp->block[pp->level];
1216 char *src=p->bytes + p->offset;
1221 assert(p->offset <= p->size);
1222 if (p->offset == p->size)
1223 return pos; /* already at the end of it */
1224 while(p->offset < p->size) {
1225 decode_ptr(&src,&item_len);
1226 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1228 decode_ptr(&src,&nxtpos);
1234 p->offset=src-(char*)p->bytes;
1236 return pos; /* that's the last one in the line */
1238 } /* forward_unode */
1240 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1241 { /* climbs down the tree, from pos, to the leftmost leaf */
1242 struct ISAMB_block *p = pp->block[pp->level];
1246 p=open_block(pp->isamb, pos);
1247 pp->block[pp->level]=p;
1248 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1251 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1252 "got lev %d node %d lf=%d",
1253 pp->level, p->pos, p->leaf);
1257 assert (p->offset==0 );
1258 src=p->bytes + p->offset;
1259 decode_ptr(&src, &pos);
1260 p->offset=src-(char*)p->bytes;
1262 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1263 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1264 } /* descend_to_leaf */
1266 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1267 { /* finds the next leaf by climbing up and down */
1269 if (!isamb_pp_climb_level(pp,&pos))
1271 isamb_pp_descend_to_leaf(pp, pos,0);
1274 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1275 { /* forwards on the current leaf, returns 0 if not found */
1279 if (!isamb_pp_read_on_leaf(pp,buf))
1281 /* FIXME - this is an extra function call, inline the read? */
1282 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1283 if (cmp <2){ /* found a good one */
1286 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1288 pp->returned_numbers++;
1291 pp->skipped_numbers++;
1294 } /* forward_on_leaf */
1296 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1297 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1299 if (!isamb_pp_climb_level(pp,&pos))
1301 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1305 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1308 struct ISAMB_block *p = pp->block[pp->level];
1312 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1314 if (! isamb_pp_climb_desc( pp, buf, untilbuf))
1315 return 0; /* could not find a leaf */
1317 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1319 }while ( isamb_pp_find_next_leaf(pp));
1320 return 0; /* could not find at all */
1322 else { /* no untilbuf, a straight read */
1323 /* FIXME - this should be moved
1324 * directly into the pp_read */
1325 /* keeping here now, to keep same
1326 * interface as the old fwd */
1327 if (isamb_pp_read_on_leaf( pp, buf))
1329 if (isamb_pp_find_next_leaf(pp))
1330 return isamb_pp_read_on_leaf(pp, buf);
1334 } /* isam_pp_forward (new version) */
1336 #elif NEW_FORWARD == 0
1338 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1342 * while at end of node
1343 * climb higher. If out, return 0
1344 * while not on a leaf (and not at its end)
1353 * The upper nodes consist of a sequence of nodenumbers and keys
1354 * When opening a block, the first node number is read in, and
1355 * offset points to the first key, which is the upper limit of keys
1356 * in the node just read.
1360 struct ISAMB_block *p = pp->block[pp->level];
1365 int descending=0; /* used to prevent a border condition error */
1369 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1371 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1372 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1377 while ( (p->offset == p->size) && !descending )
1378 { /* end of this block - climb higher */
1379 assert (p->offset <= p->size);
1381 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1387 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1389 return 0; /* at end of the root, nothing left */
1391 close_block(pp->isamb, pp->block[pp->level]);
1392 pp->block[pp->level]=0;
1394 p=pp->block[pp->level];
1396 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1400 assert(p->offset <= p->size);
1401 /* skip the child we have handled */
1402 if (p->offset != p->size)
1404 src = p->bytes + p->offset;
1405 decode_ptr(&src, &item_len);
1407 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1408 " isamb_pp_forward "
1409 "climb skipping old key");
1412 decode_ptr(&src,&pos);
1413 p->offset = src - (char*) p->bytes;
1414 break; /* even if this puts us at the end of the block, we
1415 need to descend to the last pos. UGLY coding,
1416 clean up some day */
1421 src = p->bytes + p->offset;
1422 if (p->offset == p->size)
1423 cmp=-2 ; /* descend to the last node, as we have
1427 decode_ptr(&src, &item_len);
1429 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1430 "ofs=%d sz=%d nxtpos=%d ",
1431 p->offset,p->size,pos);
1432 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1435 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1439 decode_ptr(&src,&nxtpos);
1444 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1447 descending=1; /* prevent climbing for a while */
1449 p = open_block(pp->isamb,pos);
1450 pp->block[pp->level] = p ;
1451 pp->total_size += p->size;
1452 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1455 { /* block starts with a pos */
1456 src = p->bytes + p->offset;
1457 decode_ptr(&src,&pos);
1458 p->offset=src-(char*) p->bytes;
1460 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1464 } /* descend to the node */
1466 { /* skip the node */
1467 p->offset = src - (char*) p->bytes;
1469 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1472 "isamb_pp_forward: skipping block on level %d, noting "
1474 pp->level, pp->maxlevel - pp->level-1 ,
1475 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1477 /* 0 is always leafs, 1 is one level above leafs etc, no
1478 * matter how high tree */
1480 } /* not on a leaf */
1483 if (p->offset == p->size) {
1488 assert (p->offset < p->size);
1489 src = p->bytes + p->offset;
1491 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1493 p->offset = src - (char*) p->bytes;
1495 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1499 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1501 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1508 (*pp->isamb->method->log_item)(
1509 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1513 (*pp->isamb->method->log_item)(
1514 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1517 pp->returned_numbers++;
1521 pp->skipped_numbers++;
1527 #elif NEW_FORWARD == 2
1529 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1533 struct ISAMB_block *p = pp->block[pp->level];
1538 while (p->offset == p->size)
1541 while (p->offset == p->size)
1545 close_block (pp->isamb, pp->block[pp->level]);
1546 pp->block[pp->level] = 0;
1548 p = pp->block[pp->level];
1553 src = p->bytes + p->offset;
1555 decode_ptr (&src, &item_len);
1557 decode_ptr (&src, &pos);
1559 p->offset = src - (char*) p->bytes;
1561 src = p->bytes + p->offset;
1565 if (!untilb || p->offset == p->size)
1567 assert(p->offset < p->size);
1568 decode_ptr (&src, &item_len);
1569 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1572 decode_ptr (&src, &pos);
1573 p->offset = src - (char*) p->bytes;
1580 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1582 pp->total_size += p->size;
1590 src = p->bytes + p->offset;
1593 decode_ptr (&src, &pos);
1594 p->offset = src - (char*) p->bytes;
1596 if (!untilb || p->offset == p->size)
1598 assert(p->offset < p->size);
1599 decode_ptr (&src, &item_len);
1600 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1607 assert (p->offset < p->size);
1612 src = p->bytes + p->offset;
1613 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1615 p->offset = src - (char*) p->bytes;
1616 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1619 if (p->offset == p->size) goto again;
1621 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1627 int isamb_pp_num (ISAMB_PP pp)