1 /* $Id: isamb.c,v 1.47.2.4 2005-01-16 23:13:30 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
25 #include <yaz/xmalloc.h>
42 #define ISAMB_DATA_OFFSET 3
44 /* maximum size of encoded buffer */
45 #define DST_ITEM_MAX 256
47 #define ISAMB_MAX_LEVEL 10
48 /* approx 2*max page + max size of item */
49 #define DST_BUF_SIZE 16840
51 #define ISAMB_CACHE_ENTRY_SIZE 4096
53 /* CAT_MAX: _must_ be power of 2 */
55 #define CAT_MASK (CAT_MAX-1)
56 /* CAT_NO: <= CAT_MAX */
59 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
60 #define ISAMB_PTR_CODEC 0
62 struct ISAMB_cache_entry {
67 struct ISAMB_cache_entry *next;
73 struct ISAMB_head head;
74 struct ISAMB_cache_entry *cache_entries;
81 struct ISAMB_file *file;
83 int cache; /* 0=no cache, 1=use cache, -1=dummy isam (for testing only) */
84 int log_io; /* log level for bf_read/bf_write calls */
85 int log_freelist; /* log level for freelist handling */
86 int skipped_numbers; /* on a leaf node */
88 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
89 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
103 void *decodeClientData;
111 int maxlevel; /* total depth */
114 int skipped_numbers; /* on a leaf node */
115 int returned_numbers;
116 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
117 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
118 struct ISAMB_block **block;
123 static void encode_ptr (char **dst, unsigned pos)
125 unsigned char *bp = (unsigned char*) *dst;
129 *bp++ = 128 | (pos & 127);
136 static void encode_ptr (char **dst, unsigned pos)
138 memcpy(*dst, &pos, sizeof(pos));
139 (*dst) += sizeof(pos);
144 static void decode_ptr (char **src1, int *pos)
146 unsigned char **src = (unsigned char **) src1;
151 while (((c = *(*src)++) & 128))
153 d += ((c & 127) << r);
160 static void decode_ptr (char **src, int *pos)
162 memcpy (pos, *src, sizeof(*pos));
163 (*src) += sizeof(*pos);
167 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
170 ISAMB isamb = xmalloc (sizeof(*isamb));
174 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
175 memcpy (isamb->method, method, sizeof(*method));
176 isamb->no_cat = CAT_NO;
178 isamb->log_freelist = 0;
179 isamb->cache = cache;
180 isamb->skipped_numbers=0;
181 isamb->returned_numbers=0;
182 for (i=0;i<ISAMB_MAX_LEVEL;i++)
183 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
186 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
187 for (i = 0; i<isamb->no_cat; i++)
189 char fname[DST_BUF_SIZE];
190 isamb->file[i].cache_entries = 0;
191 isamb->file[i].head_dirty = 0;
192 sprintf (fname, "%s%c", name, i+'A');
194 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
197 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
200 if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
201 &isamb->file[i].head))
203 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
204 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
205 isamb->file[i].head.block_size = b_size;
206 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
207 isamb->file[i].head.free_list = 0;
209 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
210 isamb->file[i].head_dirty = 0;
211 assert(isamb->file[i].head.block_size == b_size);
215 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
220 static void flush_blocks (ISAMB b, int cat)
222 while (b->file[cat].cache_entries)
224 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
225 b->file[cat].cache_entries = ce_this->next;
229 yaz_log (b->log_io, "bf_write: flush_blocks");
230 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
232 xfree (ce_this->buf);
237 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
239 int cat = pos&CAT_MASK;
240 int off = ((pos/CAT_MAX) &
241 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
242 * b->file[cat].head.block_size;
243 int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
245 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
250 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
251 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
254 if ((*ce)->pos == norm)
257 *ce = (*ce)->next; /* remove from list */
259 ce_this->next = b->file[cat].cache_entries; /* move to front */
260 b->file[cat].cache_entries = ce_this;
264 memcpy (ce_this->buf + off, userbuf,
265 b->file[cat].head.block_size);
269 memcpy (userbuf, ce_this->buf + off,
270 b->file[cat].head.block_size);
277 assert (ce_last && *ce_last);
279 *ce_last = 0; /* remove the last entry from list */
282 yaz_log (b->log_io, "bf_write: get_block");
283 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
285 xfree (ce_this->buf);
288 ce_this = xmalloc (sizeof(*ce_this));
289 ce_this->next = b->file[cat].cache_entries;
290 b->file[cat].cache_entries = ce_this;
291 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
293 yaz_log (b->log_io, "bf_read: get_block");
294 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
295 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
298 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
304 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
310 void isamb_close (ISAMB isamb)
313 for (i=0;isamb->accessed_nodes[i];i++)
314 logf(LOG_DEBUG,"isamb_close level leaf-%d: %d read, %d skipped",
315 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
316 logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
317 isamb->skipped_numbers, isamb->returned_numbers);
318 for (i = 0; i<isamb->no_cat; i++)
320 flush_blocks (isamb, i);
321 if (isamb->file[i].head_dirty)
322 bf_write (isamb->file[i].bf, 0, 0,
323 sizeof(struct ISAMB_head), &isamb->file[i].head);
325 bf_close (isamb->file[i].bf);
328 xfree (isamb->method);
332 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
334 int cat = pos&CAT_MASK;
335 struct ISAMB_block *p;
338 p = xmalloc (sizeof(*p));
340 p->cat = pos & CAT_MASK;
341 p->buf = xmalloc (b->file[cat].head.block_size);
344 if (!get_block (b, pos, p->buf, 0))
346 yaz_log (b->log_io, "bf_read: open_block");
347 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
349 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
350 (long) pos, (long) pos/CAT_MAX);
354 p->bytes = p->buf + ISAMB_DATA_OFFSET;
356 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
359 yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
361 assert (p->size >= 0);
365 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
366 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
370 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
372 struct ISAMB_block *p;
374 p = xmalloc (sizeof(*p));
375 p->buf = xmalloc (b->file[cat].head.block_size);
377 if (!b->file[cat].head.free_list)
380 block_no = b->file[cat].head.last_block++;
381 p->pos = block_no * CAT_MAX + cat;
385 p->pos = b->file[cat].head.free_list;
386 assert((p->pos & CAT_MASK) == cat);
387 if (!get_block (b, p->pos, p->buf, 0))
389 yaz_log (b->log_io, "bf_read: new_block");
390 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
392 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
393 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
397 yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
398 cat, p->pos/CAT_MAX);
399 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
402 b->file[cat].head_dirty = 1;
403 memset (p->buf, 0, b->file[cat].head.block_size);
404 p->bytes = p->buf + ISAMB_DATA_OFFSET;
410 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
414 struct ISAMB_block *new_leaf (ISAMB b, int cat)
416 return new_block (b, 1, cat);
420 struct ISAMB_block *new_int (ISAMB b, int cat)
422 return new_block (b, 0, cat);
425 static void check_block (ISAMB b, struct ISAMB_block *p)
434 char *startp = p->bytes;
436 char *endp = p->bytes + p->size;
439 decode_ptr (&src, &pos);
440 assert ((pos&CAT_MASK) == p->cat);
444 decode_ptr (&src, &item_len);
445 assert (item_len > 0 && item_len < 30);
447 decode_ptr (&src, &pos);
448 assert ((pos&CAT_MASK) == p->cat);
453 void close_block (ISAMB b, struct ISAMB_block *p)
459 yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
460 p->pos, p->cat, p->pos/CAT_MAX);
461 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
462 b->file[p->cat].head.free_list = p->pos;
463 if (!get_block (b, p->pos, p->buf, 1))
465 yaz_log (b->log_io, "bf_write: close_block (deleted)");
466 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
471 int size = p->size + ISAMB_DATA_OFFSET;
472 assert (p->size >= 0);
474 p->buf[1] = size & 255;
475 p->buf[2] = size >> 8;
477 if (!get_block (b, p->pos, p->buf, 1))
479 yaz_log (b->log_io, "bf_write: close_block");
480 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
483 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
488 int insert_sub (ISAMB b, struct ISAMB_block **p,
489 void *new_item, int *mode,
491 struct ISAMB_block **sp,
492 void *sub_item, int *sub_size,
495 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
497 ISAMC_I *stream, struct ISAMB_block **sp,
498 void *split_item, int *split_size, void *last_max_item)
500 char *startp = p->bytes;
502 char *endp = p->bytes + p->size;
504 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
505 char sub_item[DST_ITEM_MAX];
511 assert(p->size >= 0);
512 decode_ptr (&src, &pos);
518 decode_ptr (&src, &item_len);
519 d = (*b->method->compare_item)(src, lookahead_item);
522 sub_p1 = open_block (b, pos);
524 more = insert_sub (b, &sub_p1, lookahead_item, mode,
526 sub_item, &sub_size, src);
531 decode_ptr (&src, &pos);
535 sub_p1 = open_block (b, pos);
537 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
538 sub_item, &sub_size, last_max_item);
542 /* there was a split - must insert pointer in this one */
543 char dst_buf[DST_BUF_SIZE];
546 assert (sub_size < 30 && sub_size > 1);
548 memcpy (dst, startp, src - startp);
552 encode_ptr (&dst, sub_size); /* sub length and item */
553 memcpy (dst, sub_item, sub_size);
556 encode_ptr (&dst, sub_p2->pos); /* pos */
558 if (endp - src) /* remaining data */
560 memcpy (dst, src, endp - src);
563 p->size = dst - dst_buf;
564 assert (p->size >= 0);
565 if (p->size <= b->file[p->cat].head.block_max)
567 memcpy (startp, dst_buf, dst - dst_buf);
576 half = src + b->file[p->cat].head.block_size/2;
577 decode_ptr (&src, &pos);
580 decode_ptr (&src, split_size);
582 decode_ptr (&src, &pos);
584 p_new_size = src - dst_buf;
585 memcpy (p->bytes, dst_buf, p_new_size);
587 decode_ptr (&src, split_size);
588 memcpy (split_item, src, *split_size);
591 *sp = new_int (b, p->cat);
592 (*sp)->size = endp - src;
593 memcpy ((*sp)->bytes, src, (*sp)->size);
595 p->size = p_new_size;
598 close_block (b, sub_p2);
600 close_block (b, sub_p1);
605 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
606 int *lookahead_mode, ISAMC_I *stream,
607 struct ISAMB_block **sp2,
608 void *sub_item, int *sub_size,
611 struct ISAMB_block *p = *sp1;
612 char *src = 0, *endp = 0;
613 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
615 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
616 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
618 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
619 char *cut = dst_buf + quater * 2;
620 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
623 char cut_item_buf[DST_ITEM_MAX];
624 int cut_item_size = 0;
628 char file_item_buf[DST_ITEM_MAX];
629 char *file_item = file_item_buf;
632 endp = p->bytes + p->size;
633 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
637 char *lookahead_next;
641 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
645 dst_item = lookahead_item;
646 if (!*lookahead_mode)
648 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
649 assert (*lookahead_mode);
653 dst_item = file_item_buf;
654 if (!*lookahead_mode && d == 0)
658 else if (!half1 && dst > cut)
660 char *dst_item_0 = dst_item;
661 half1 = dst; /* candidate for splitting */
663 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
665 cut_item_size = dst_item - dst_item_0;
666 memcpy (cut_item_buf, dst_item_0, cut_item_size);
671 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
678 lookahead_next = lookahead_item;
679 if (!(*stream->read_item)(stream->clientData,
686 if (lookahead_item && max_item &&
687 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
698 lookahead_next = lookahead_item;
699 if (!(*stream->read_item)(stream->clientData,
700 &lookahead_next, lookahead_mode))
707 file_item = file_item_buf;
708 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
714 file_item = file_item_buf;
715 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
719 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
720 while (lookahead_item)
722 char *dst_item = lookahead_item;
726 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
731 if (!*lookahead_mode)
733 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
736 else if (!half1 && dst > cut)
738 char *dst_item_0 = dst_item;
739 half1 = dst; /* candidate for splitting */
741 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
743 cut_item_size = dst_item - dst_item_0;
744 memcpy (cut_item_buf, dst_item_0, cut_item_size);
749 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
758 dst_item = lookahead_item;
759 if (!(*stream->read_item)(stream->clientData, &dst_item,
766 new_size = dst - dst_buf;
767 if (p && p->cat != b->no_cat-1 &&
768 new_size > b->file[p->cat].head.block_max)
770 /* non-btree block will be removed */
773 /* delete it too!! */
774 p = 0; /* make a new one anyway */
777 { /* must create a new one */
779 for (i = 0; i < b->no_cat; i++)
780 if (new_size <= b->file[i].head.block_max)
786 if (new_size > b->file[p->cat].head.block_max)
789 char *cut_item = cut_item_buf;
795 p->size = half1 - dst_buf;
796 memcpy (p->bytes, dst_buf, half1 - dst_buf);
799 *sp2 = new_leaf (b, p->cat);
801 (*b->method->code_reset)(c2);
803 first_dst = (*sp2)->bytes;
805 (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
807 memcpy (first_dst, half2, dst - half2);
809 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
812 memcpy (sub_item, cut_item_buf, cut_item_size);
813 *sub_size = cut_item_size;
817 memcpy (p->bytes, dst_buf, dst - dst_buf);
820 (*b->method->code_stop)(ISAMC_DECODE, c1);
821 (*b->method->code_stop)(ISAMC_ENCODE, c2);
826 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
829 struct ISAMB_block **sp,
830 void *sub_item, int *sub_size,
833 if (!*p || (*p)->leaf)
834 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
837 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
841 int isamb_unlink (ISAMB b, ISAMC_P pos)
843 struct ISAMB_block *p1;
847 p1 = open_block(b, pos);
853 char *src = p1->bytes + p1->offset;
855 decode_ptr(&src, &sub_p);
856 isamb_unlink(b, sub_p);
858 while (src != p1->bytes + p1->size)
860 decode_ptr(&src, &item_len);
862 decode_ptr(&src, &sub_p);
863 isamb_unlink(b, sub_p);
870 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
872 char item_buf[DST_ITEM_MAX];
884 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
889 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
892 struct ISAMB_block *p = 0, *sp = 0;
893 char sub_item[DST_ITEM_MAX];
897 p = open_block (b, pos);
898 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
899 sub_item, &sub_size, 0);
901 { /* increase level of tree by one */
902 struct ISAMB_block *p2 = new_int (b, p->cat);
903 char *dst = p2->bytes + p2->size;
905 encode_ptr (&dst, p->pos);
906 assert (sub_size < 20);
907 encode_ptr (&dst, sub_size);
908 memcpy (dst, sub_item, sub_size);
910 encode_ptr (&dst, sp->pos);
912 p2->size = dst - p2->bytes;
913 pos = p2->pos; /* return new super page */
918 pos = p->pos; /* return current one (again) */
924 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
926 ISAMB_PP pp = xmalloc (sizeof(*pp));
930 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
937 pp->skipped_numbers=0;
938 pp->returned_numbers=0;
939 for (i = 0; i<ISAMB_MAX_LEVEL; i++)
940 pp->skipped_nodes[i] = pp->accessed_nodes[i] = 0;
946 struct ISAMB_block *p = open_block (isamb, pos);
947 char *src = p->bytes + p->offset;
948 pp->block[pp->level] = p;
950 pp->total_size += p->size;
955 decode_ptr (&src, &pos);
956 p->offset = src - p->bytes;
958 pp->accessed_nodes[pp->level]++;
961 pp->block[pp->level+1] = 0;
962 pp->maxlevel = pp->level;
968 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
970 return isamb_pp_open_x (isamb, pos, 0);
973 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
978 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
979 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
980 for (i=pp->maxlevel;i>=0;i--)
981 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
982 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: %d read, %d skipped", i,
983 pp->accessed_nodes[i], pp->skipped_nodes[i]);
984 pp->isamb->skipped_numbers += pp->skipped_numbers;
985 pp->isamb->returned_numbers += pp->returned_numbers;
986 for (i=pp->maxlevel;i>=0;i--)
988 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
989 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
992 *size = pp->total_size;
994 *blocks = pp->no_blocks;
995 for (i = 0; i <= pp->level; i++)
996 close_block (pp->isamb, pp->block[i]);
1001 int isamb_block_info (ISAMB isamb, int cat)
1003 if (cat >= 0 && cat < isamb->no_cat)
1004 return isamb->file[cat].head.block_size;
1008 void isamb_pp_close (ISAMB_PP pp)
1010 isamb_pp_close_x (pp, 0, 0);
1013 /* simple recursive dumper .. */
1014 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1018 char prefix_str[1024];
1021 struct ISAMB_block *p = open_block (b, pos);
1022 sprintf(prefix_str, "%*s %d cat=%d size=%d max=%d", level*2, "",
1023 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1025 sprintf(prefix_str, "%*s %d", level*2, "", pos);
1028 while (p->offset < p->size)
1030 char *src = p->bytes + p->offset;
1032 (*b->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1034 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1035 p->offset = src - (char*) p->bytes;
1037 assert(p->offset == p->size);
1041 char *src = p->bytes + p->offset;
1045 decode_ptr (&src, &sub);
1046 p->offset = src - (char*) p->bytes;
1048 isamb_dump_r(b, sub, pr, level+1);
1050 while (p->offset < p->size)
1052 decode_ptr (&src, &item_len);
1053 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1055 decode_ptr (&src, &sub);
1057 p->offset = src - (char*) p->bytes;
1059 isamb_dump_r(b, sub, pr, level+1);
1066 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1068 isamb_dump_r(b, pos, pr, 0);
1072 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1073 debug the more complex pp_read that also forwards. May be deleted near end
1074 of 2004, if it has not shown to be useful */
1077 int isamb_pp_read (ISAMB_PP pp, void *buf)
1081 struct ISAMB_block *p = pp->block[pp->level];
1085 while (p->offset == p->size)
1088 while (p->offset == p->size)
1092 close_block (pp->isamb, pp->block[pp->level]);
1093 pp->block[pp->level] = 0;
1095 p = pp->block[pp->level];
1098 src = p->bytes + p->offset;
1100 decode_ptr (&src, &item_len);
1102 decode_ptr (&src, &pos);
1104 p->offset = src - (char*) p->bytes;
1110 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1112 pp->total_size += p->size;
1119 src = p->bytes + p->offset;
1120 decode_ptr (&src, &pos);
1121 p->offset = src - (char*) p->bytes;
1125 assert (p->offset < p->size);
1127 src = p->bytes + p->offset;
1128 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1130 p->offset = src - (char*) p->bytes;
1131 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1136 int isamb_pp_read (ISAMB_PP pp, void *buf)
1138 return isamb_pp_forward(pp,buf,0);
1142 #define NEW_FORWARD 1
1144 #if NEW_FORWARD == 1
1148 #define ISAMB_DEBUG 1
1150 static int isamb_pp_on_right_node(ISAMB_PP pp, int level, const void *untilbuf)
1151 { /* looks one node higher to see if we should be on this node at all */
1152 /* useful in backing off quickly, and in avoiding tail descends */
1153 /* call with pp->level to begin with */
1154 struct ISAMB_block *p;
1161 logf(LOG_DEBUG,"isamb_pp_on_right returning true for root");
1163 return 1; /* we can never skip the root node */
1167 assert(p->offset <= p->size);
1168 if (p->offset < p->size )
1170 assert(p->offset>0);
1171 src=p->bytes + p->offset;
1172 decode_ptr(&src,&item_len);
1174 (*pp->isamb->method->log_item)(LOG_DEBUG,untilbuf,"on_leaf: until");
1175 (*pp->isamb->method->log_item)(LOG_DEBUG,src,"on_leaf: value");
1177 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1180 logf(LOG_DEBUG,"isamb_pp_on_right returning true "
1181 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1187 logf(LOG_DEBUG,"isamb_pp_on_right returning false "
1188 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1195 logf(LOG_DEBUG,"isamb_pp_on_right at tail, looking higher "
1198 return isamb_pp_on_right_node(pp, level, untilbuf);
1200 } /* isamb_pp_on_right_node */
1202 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1203 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1204 struct ISAMB_block *p = pp->block[pp->level];
1209 if (!p || p->offset == p->size) {
1211 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1213 return 0; /* at end of leaf */
1215 src=p->bytes + p->offset;
1217 (*pp->isamb->method->code_item)
1218 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1219 p->offset = src - (char*) p->bytes;
1222 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1226 } /* read_on_leaf */
1228 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1229 { /* forwards on the current leaf, returns 0 if not found */
1233 if (!isamb_pp_read_on_leaf(pp,buf))
1235 /* FIXME - this is an extra function call, inline the read? */
1236 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1237 if (cmp <2){ /* found a good one */
1240 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1242 pp->returned_numbers++;
1246 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1247 return 0; /* never mind the rest of this leaf */
1248 pp->skipped_numbers++;
1251 } /* forward_on_leaf */
1253 static int isamb_pp_climb_level(ISAMB_PP pp, int *pos)
1254 { /* climbs higher in the tree, until finds a level with data left */
1255 /* returns the node to (consider to) descend to in *pos) */
1256 struct ISAMB_block *p = pp->block[pp->level];
1260 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1261 "at level %d node %d ofs=%d sz=%d",
1262 pp->level, p->pos, p->offset, p->size);
1264 assert(pp->level >= 0);
1267 assert(p->offset <= p->size);
1271 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1275 assert(pp->level>0);
1276 close_block(pp->isamb, pp->block[pp->level]);
1277 pp->block[pp->level]=0;
1279 p=pp->block[pp->level];
1281 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1282 pp->level, p->pos, p->offset);
1285 assert(p->offset <= p->size);
1286 if (p->offset == p->size ) {
1287 /* we came from the last pointer, climb on */
1288 if (!isamb_pp_climb_level(pp,pos))
1290 p=pp->block[pp->level];
1294 /* skip the child we just came from */
1296 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1297 pp->level, p->offset, p->size);
1299 assert (p->offset < p->size );
1300 src=p->bytes + p->offset;
1301 decode_ptr(&src, &item_len);
1303 decode_ptr(&src, pos);
1304 p->offset=src - (char *)p->bytes;
1311 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1312 { /* scans a upper node until it finds a child <= untilbuf */
1313 /* pp points to the key value, as always. pos is the child read from */
1315 /* if all values are too small, returns the last child in the node */
1316 /* FIXME - this can be detected, and avoided by looking at the */
1317 /* parent node, but that gets messy. Presumably the cost is */
1318 /* pretty low anyway */
1319 struct ISAMB_block *p = pp->block[pp->level];
1320 char *src = p->bytes + p->offset;
1327 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1328 "at level %d node %d ofs=%di sz=%d",
1329 pp->level, p->pos, p->offset, p->size);
1332 assert(p->offset <= p->size);
1333 if (p->offset == p->size) {
1335 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1336 "at level %d node %d ofs=%di sz=%d",
1337 pp->level, p->pos, p->offset, p->size);
1339 return pos; /* already at the end of it */
1341 while(p->offset < p->size)
1343 decode_ptr(&src,&item_len);
1344 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1346 decode_ptr(&src,&nxtpos);
1350 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1351 "at level %d node %d ofs=%d sz=%d",
1352 pp->level, p->pos, p->offset, p->size);
1357 p->offset=src-(char*)p->bytes;
1358 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1364 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1365 "at level %d node %d ofs=%d sz=%d skips=%d",
1366 pp->level, p->pos, p->offset, p->size, skips);
1368 return pos; /* that's the last one in the line */
1370 } /* forward_unode */
1372 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1373 { /* climbs down the tree, from pos, to the leftmost leaf */
1374 struct ISAMB_block *p = pp->block[pp->level];
1378 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1379 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1380 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1383 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1386 p = open_block(pp->isamb, pos);
1387 pp->block[pp->level] = p;
1388 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1391 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1392 "got lev %d node %d lf=%d",
1393 pp->level, p->pos, p->leaf);
1397 assert (p->offset==0);
1398 src = p->bytes + p->offset;
1399 decode_ptr(&src, &pos);
1400 p->offset = src-(char*)p->bytes;
1401 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1403 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1404 "returning at lev %d node %d ofs=%d lf=%d",
1405 pp->level, p->pos, p->offset, p->leaf);
1407 } /* descend_to_leaf */
1409 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1410 { /* finds the next leaf by climbing up and down */
1412 if (!isamb_pp_climb_level(pp,&pos))
1414 isamb_pp_descend_to_leaf(pp, pos,0);
1418 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1419 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1422 struct ISAMB_block *p = pp->block[pp->level];
1423 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1424 "at level %d node %d ofs=%d sz=%d",
1425 pp->level, p->pos, p->offset, p->size);
1427 if (!isamb_pp_climb_level(pp,&pos))
1429 /* see if it would pay to climb one higher */
1430 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1431 if (!isamb_pp_climb_level(pp,&pos))
1433 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1435 p = pp->block[pp->level];
1436 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1437 "at level %d node %d ofs=%d sz=%d",
1438 pp->level, p->pos, p->offset, p->size);
1443 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1446 struct ISAMB_block *p = pp->block[pp->level];
1450 logf(LOG_DEBUG,"isamb_pp_forward starting "
1451 "at level %d node %d ofs=%d sz=%d u=%p",
1452 pp->level, p->pos, p->offset, p->size,untilbuf);
1456 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1459 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1460 "at level %d node %d ofs=%d sz=%d",
1461 pp->level, p->pos, p->offset, p->size);
1465 if (! isamb_pp_climb_desc( pp, buf, untilbuf)) {
1468 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1469 "at level %d node %d ofs=%d sz=%d",
1470 pp->level, p->pos, p->offset, p->size);
1472 return 0; /* could not find a leaf */
1475 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1478 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1479 "at level %d node %d ofs=%d sz=%d",
1480 pp->level, p->pos, p->offset, p->size);
1484 } while (isamb_pp_find_next_leaf(pp));
1485 return 0; /* could not find at all */
1487 else { /* no untilbuf, a straight read */
1488 /* FIXME - this should be moved
1489 * directly into the pp_read */
1490 /* keeping here now, to keep same
1491 * interface as the old fwd */
1492 if (isamb_pp_read_on_leaf( pp, buf)) {
1495 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1496 "at level %d node %d ofs=%d sz=%d",
1497 pp->level, p->pos, p->offset, p->size);
1501 if (isamb_pp_find_next_leaf(pp)) {
1504 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1505 "at level %d node %d ofs=%d sz=%d",
1506 pp->level, p->pos, p->offset, p->size);
1508 return isamb_pp_read_on_leaf(pp, buf);
1513 } /* isam_pp_forward (new version) */
1515 #elif NEW_FORWARD == 0
1517 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1521 * while at end of node
1522 * climb higher. If out, return 0
1523 * while not on a leaf (and not at its end)
1532 * The upper nodes consist of a sequence of nodenumbers and keys
1533 * When opening a block, the first node number is read in, and
1534 * offset points to the first key, which is the upper limit of keys
1535 * in the node just read.
1539 struct ISAMB_block *p = pp->block[pp->level];
1544 int descending=0; /* used to prevent a border condition error */
1548 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1550 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1551 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1556 while ( (p->offset == p->size) && !descending )
1557 { /* end of this block - climb higher */
1558 assert (p->offset <= p->size);
1560 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1566 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1568 return 0; /* at end of the root, nothing left */
1570 close_block(pp->isamb, pp->block[pp->level]);
1571 pp->block[pp->level]=0;
1573 p=pp->block[pp->level];
1575 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1579 assert(p->offset <= p->size);
1580 /* skip the child we have handled */
1581 if (p->offset != p->size)
1583 src = p->bytes + p->offset;
1584 decode_ptr(&src, &item_len);
1586 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1587 " isamb_pp_forward "
1588 "climb skipping old key");
1591 decode_ptr(&src,&pos);
1592 p->offset = src - (char*) p->bytes;
1593 break; /* even if this puts us at the end of the block, we
1594 need to descend to the last pos. UGLY coding,
1595 clean up some day */
1600 src = p->bytes + p->offset;
1601 if (p->offset == p->size)
1602 cmp=-2 ; /* descend to the last node, as we have
1606 decode_ptr(&src, &item_len);
1608 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1609 "ofs=%d sz=%d nxtpos=%d ",
1610 p->offset,p->size,pos);
1611 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1614 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1618 decode_ptr(&src,&nxtpos);
1623 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1626 descending=1; /* prevent climbing for a while */
1628 p = open_block(pp->isamb,pos);
1629 pp->block[pp->level] = p ;
1630 pp->total_size += p->size;
1631 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1634 { /* block starts with a pos */
1635 src = p->bytes + p->offset;
1636 decode_ptr(&src,&pos);
1637 p->offset=src-(char*) p->bytes;
1639 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1643 } /* descend to the node */
1645 { /* skip the node */
1646 p->offset = src - (char*) p->bytes;
1648 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1651 "isamb_pp_forward: skipping block on level %d, noting "
1653 pp->level, pp->maxlevel - pp->level-1 ,
1654 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1656 /* 0 is always leafs, 1 is one level above leafs etc, no
1657 * matter how high tree */
1659 } /* not on a leaf */
1662 if (p->offset == p->size) {
1667 assert (p->offset < p->size);
1668 src = p->bytes + p->offset;
1670 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1672 p->offset = src - (char*) p->bytes;
1674 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1678 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1680 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1687 (*pp->isamb->method->log_item)(
1688 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1692 (*pp->isamb->method->log_item)(
1693 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1696 pp->returned_numbers++;
1700 pp->skipped_numbers++;
1706 #elif NEW_FORWARD == 2
1708 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1712 struct ISAMB_block *p = pp->block[pp->level];
1717 while (p->offset == p->size)
1720 while (p->offset == p->size)
1724 close_block (pp->isamb, pp->block[pp->level]);
1725 pp->block[pp->level] = 0;
1727 p = pp->block[pp->level];
1732 src = p->bytes + p->offset;
1734 decode_ptr (&src, &item_len);
1736 decode_ptr (&src, &pos);
1738 p->offset = src - (char*) p->bytes;
1740 src = p->bytes + p->offset;
1744 if (!untilb || p->offset == p->size)
1746 assert(p->offset < p->size);
1747 decode_ptr (&src, &item_len);
1748 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1751 decode_ptr (&src, &pos);
1752 p->offset = src - (char*) p->bytes;
1759 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1761 pp->total_size += p->size;
1769 src = p->bytes + p->offset;
1772 decode_ptr (&src, &pos);
1773 p->offset = src - (char*) p->bytes;
1775 if (!untilb || p->offset == p->size)
1777 assert(p->offset < p->size);
1778 decode_ptr (&src, &item_len);
1779 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1786 assert (p->offset < p->size);
1791 src = p->bytes + p->offset;
1792 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1794 p->offset = src - (char*) p->bytes;
1795 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1798 if (p->offset == p->size) goto again;
1800 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1806 int isamb_pp_num (ISAMB_PP pp)
1811 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1812 int *current, int *total, void *dummybuf )
1814 struct ISAMB_block *p = pp->block[pp->level];
1816 char *end=p->bytes+p->size;
1817 char *cur=p->bytes+p->offset;
1819 assert(p->offset <= p->size);
1827 (*pp->isamb->method->code_item)
1828 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1829 assert(dst<(char*) dummybuf+100); /*FIXME */
1834 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur=%d tot=%d ofs=%d sz=%d lev=%d",
1835 *current, *total, p->offset, p->size, pp->level);
1839 static void isamb_pp_upper_pos( ISAMB_PP pp, int *current, int *total,
1840 int size, int level )
1841 { /* estimates total/current occurrences from here up, excl leaf */
1842 struct ISAMB_block *p = pp->block[level];
1844 char *end=p->bytes+p->size;
1845 char *cur=p->bytes+p->offset;
1850 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1851 "cur=%d tot=%d ofs=%d sz=%d pos=%d",
1852 level, *current, *total, p->offset, p->size, p->pos);
1853 assert (p->offset <= p->size);
1854 decode_ptr (&src, &child ); /* first child */
1861 decode_ptr (&src, &item_size );
1862 assert(src+item_size<=end);
1864 decode_ptr (&src, &child );
1867 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1870 void isamb_pp_pos( ISAMB_PP pp, int *current, int *total )
1871 { /* return an estimate of the current position and of the total number of */
1872 /* occureences in the isam tree, based on the current leaf */
1873 struct ISAMB_block *p = pp->block[pp->level];
1874 char dummy[100]; /* 100 bytes/entry must be enough */
1878 isamb_pp_leaf_pos(pp,current, total, dummy);
1880 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1882 logf(LOG_DEBUG,"isamb_pp_pos: C=%d T=%d =%6.2f%%",
1883 *current, *total, 100.0*(*current)/(*total));