1 /* $Id: isamb.c,v 1.38 2004-06-02 17:26:03 heikki 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 */
101 void *decodeClientData;
109 int maxlevel; /* total depth */
112 int skipped_numbers; /* on a leaf node */
113 int returned_numbers;
114 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
115 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
116 struct ISAMB_block **block;
120 static void encode_ptr (char **dst, unsigned pos)
122 unsigned char *bp = (unsigned char*) *dst;
126 *bp++ = 128 | (pos & 127);
133 static void encode_ptr (char **dst, unsigned pos)
135 memcpy(*dst, &pos, sizeof(pos));
136 (*dst) += sizeof(pos);
141 static void decode_ptr (char **src1, int *pos)
143 unsigned char **src = (unsigned char **) src1;
148 while (((c = *(*src)++) & 128))
150 d += ((c & 127) << r);
157 static void decode_ptr (char **src, int *pos)
159 memcpy (pos, *src, sizeof(*pos));
160 (*src) += sizeof(*pos);
164 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
167 ISAMB isamb = xmalloc (sizeof(*isamb));
171 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
172 memcpy (isamb->method, method, sizeof(*method));
173 isamb->no_cat = CAT_NO;
175 isamb->log_freelist = 0;
176 isamb->cache = cache;
177 isamb->skipped_numbers=0;
178 isamb->returned_numbers=0;
179 for (i=0;i<ISAMB_MAX_LEVEL;i++)
180 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
183 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
184 for (i = 0; i<isamb->no_cat; i++)
186 char fname[DST_BUF_SIZE];
187 isamb->file[i].cache_entries = 0;
188 isamb->file[i].head_dirty = 0;
189 sprintf (fname, "%s%c", name, i+'A');
191 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
194 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
197 if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
198 &isamb->file[i].head))
200 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
201 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
202 isamb->file[i].head.block_size = b_size;
203 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
204 isamb->file[i].head.free_list = 0;
206 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
207 isamb->file[i].head_dirty = 0;
208 assert(isamb->file[i].head.block_size == b_size);
212 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
217 static void flush_blocks (ISAMB b, int cat)
219 while (b->file[cat].cache_entries)
221 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
222 b->file[cat].cache_entries = ce_this->next;
226 yaz_log (b->log_io, "bf_write: flush_blocks");
227 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
229 xfree (ce_this->buf);
234 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
236 int cat = pos&CAT_MASK;
237 int off = ((pos/CAT_MAX) &
238 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
239 * b->file[cat].head.block_size;
240 int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
242 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
247 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
248 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
251 if ((*ce)->pos == norm)
254 *ce = (*ce)->next; /* remove from list */
256 ce_this->next = b->file[cat].cache_entries; /* move to front */
257 b->file[cat].cache_entries = ce_this;
261 memcpy (ce_this->buf + off, userbuf,
262 b->file[cat].head.block_size);
266 memcpy (userbuf, ce_this->buf + off,
267 b->file[cat].head.block_size);
274 assert (ce_last && *ce_last);
276 *ce_last = 0; /* remove the last entry from list */
279 yaz_log (b->log_io, "bf_write: get_block");
280 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
282 xfree (ce_this->buf);
285 ce_this = xmalloc (sizeof(*ce_this));
286 ce_this->next = b->file[cat].cache_entries;
287 b->file[cat].cache_entries = ce_this;
288 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
290 yaz_log (b->log_io, "bf_read: get_block");
291 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
292 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
295 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
301 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
307 void isamb_close (ISAMB isamb)
310 for (i=0;isamb->accessed_nodes[i];i++)
311 logf(LOG_DEBUG,"isamb_close level leaf-%d: %d read, %d skipped",
312 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
313 logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
314 isamb->skipped_numbers, isamb->returned_numbers);
315 for (i = 0; i<isamb->no_cat; i++)
317 flush_blocks (isamb, i);
318 if (isamb->file[i].head_dirty)
319 bf_write (isamb->file[i].bf, 0, 0,
320 sizeof(struct ISAMB_head), &isamb->file[i].head);
322 bf_close (isamb->file[i].bf);
325 xfree (isamb->method);
329 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
331 int cat = pos&CAT_MASK;
332 struct ISAMB_block *p;
335 p = xmalloc (sizeof(*p));
337 p->cat = pos & CAT_MASK;
338 p->buf = xmalloc (b->file[cat].head.block_size);
340 if (!get_block (b, pos, p->buf, 0))
342 yaz_log (b->log_io, "bf_read: open_block");
343 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
345 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
346 (long) pos, (long) pos/CAT_MAX);
350 p->bytes = p->buf + ISAMB_DATA_OFFSET;
352 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
355 yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
357 assert (p->size >= 0);
361 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
362 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
366 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
368 struct ISAMB_block *p;
370 p = xmalloc (sizeof(*p));
371 p->buf = xmalloc (b->file[cat].head.block_size);
373 if (!b->file[cat].head.free_list)
376 block_no = b->file[cat].head.last_block++;
377 p->pos = block_no * CAT_MAX + cat;
381 p->pos = b->file[cat].head.free_list;
382 assert((p->pos & CAT_MASK) == cat);
383 if (!get_block (b, p->pos, p->buf, 0))
385 yaz_log (b->log_io, "bf_read: new_block");
386 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
388 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
389 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
393 yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
394 cat, p->pos/CAT_MAX);
395 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
398 b->file[cat].head_dirty = 1;
399 memset (p->buf, 0, b->file[cat].head.block_size);
400 p->bytes = p->buf + ISAMB_DATA_OFFSET;
406 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
410 struct ISAMB_block *new_leaf (ISAMB b, int cat)
412 return new_block (b, 1, cat);
416 struct ISAMB_block *new_int (ISAMB b, int cat)
418 return new_block (b, 0, cat);
421 static void check_block (ISAMB b, struct ISAMB_block *p)
430 char *startp = p->bytes;
432 char *endp = p->bytes + p->size;
435 decode_ptr (&src, &pos);
436 assert ((pos&CAT_MASK) == p->cat);
440 decode_ptr (&src, &item_len);
441 assert (item_len > 0 && item_len < 30);
443 decode_ptr (&src, &pos);
444 assert ((pos&CAT_MASK) == p->cat);
449 void close_block (ISAMB b, struct ISAMB_block *p)
455 yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
456 p->pos, p->cat, p->pos/CAT_MAX);
457 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
458 b->file[p->cat].head.free_list = p->pos;
459 if (!get_block (b, p->pos, p->buf, 1))
461 yaz_log (b->log_io, "bf_write: close_block (deleted)");
462 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
467 int size = p->size + ISAMB_DATA_OFFSET;
468 assert (p->size >= 0);
470 p->buf[1] = size & 255;
471 p->buf[2] = size >> 8;
473 if (!get_block (b, p->pos, p->buf, 1))
475 yaz_log (b->log_io, "bf_write: close_block");
476 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
479 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
484 int insert_sub (ISAMB b, struct ISAMB_block **p,
485 void *new_item, int *mode,
487 struct ISAMB_block **sp,
488 void *sub_item, int *sub_size,
491 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
493 ISAMC_I *stream, struct ISAMB_block **sp,
494 void *split_item, int *split_size, void *last_max_item)
496 char *startp = p->bytes;
498 char *endp = p->bytes + p->size;
500 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
501 char sub_item[DST_ITEM_MAX];
507 assert(p->size >= 0);
508 decode_ptr (&src, &pos);
514 decode_ptr (&src, &item_len);
515 d = (*b->method->compare_item)(src, lookahead_item);
518 sub_p1 = open_block (b, pos);
520 more = insert_sub (b, &sub_p1, lookahead_item, mode,
522 sub_item, &sub_size, src);
527 decode_ptr (&src, &pos);
531 sub_p1 = open_block (b, pos);
533 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
534 sub_item, &sub_size, last_max_item);
538 /* there was a split - must insert pointer in this one */
539 char dst_buf[DST_BUF_SIZE];
542 assert (sub_size < 30 && sub_size > 1);
544 memcpy (dst, startp, src - startp);
548 encode_ptr (&dst, sub_size); /* sub length and item */
549 memcpy (dst, sub_item, sub_size);
552 encode_ptr (&dst, sub_p2->pos); /* pos */
554 if (endp - src) /* remaining data */
556 memcpy (dst, src, endp - src);
559 p->size = dst - dst_buf;
560 assert (p->size >= 0);
561 if (p->size <= b->file[p->cat].head.block_max)
563 memcpy (startp, dst_buf, dst - dst_buf);
572 half = src + b->file[p->cat].head.block_size/2;
573 decode_ptr (&src, &pos);
576 decode_ptr (&src, split_size);
578 decode_ptr (&src, &pos);
580 p_new_size = src - dst_buf;
581 memcpy (p->bytes, dst_buf, p_new_size);
583 decode_ptr (&src, split_size);
584 memcpy (split_item, src, *split_size);
587 *sp = new_int (b, p->cat);
588 (*sp)->size = endp - src;
589 memcpy ((*sp)->bytes, src, (*sp)->size);
591 p->size = p_new_size;
594 close_block (b, sub_p2);
596 close_block (b, sub_p1);
601 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
602 int *lookahead_mode, ISAMC_I *stream,
603 struct ISAMB_block **sp2,
604 void *sub_item, int *sub_size,
607 struct ISAMB_block *p = *sp1;
608 char *src = 0, *endp = 0;
609 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
611 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
612 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
614 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
615 char *cut = dst_buf + quater * 2;
616 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
619 char cut_item_buf[DST_ITEM_MAX];
620 int cut_item_size = 0;
624 char file_item_buf[DST_ITEM_MAX];
625 char *file_item = file_item_buf;
628 endp = p->bytes + p->size;
629 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
634 char *lookahead_next;
638 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
642 dst_item = lookahead_item;
643 if (!*lookahead_mode)
645 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
646 assert (*lookahead_mode);
650 dst_item = file_item_buf;
651 if (!*lookahead_mode && d == 0)
655 else if (!half1 && dst > cut)
657 char *dst_item_0 = dst_item;
658 half1 = dst; /* candidate for splitting */
660 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
662 cut_item_size = dst_item - dst_item_0;
663 memcpy (cut_item_buf, dst_item_0, cut_item_size);
668 (*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;
943 struct ISAMB_block *p = open_block (isamb, pos);
944 char *src = p->bytes + p->offset;
945 pp->block[pp->level] = p;
947 pp->total_size += p->size;
953 decode_ptr (&src, &pos);
954 p->offset = src - p->bytes;
956 pp->accessed_nodes[pp->level]++;
958 pp->block[pp->level+1] = 0;
959 pp->maxlevel=pp->level;
965 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
967 return isamb_pp_open_x (isamb, pos, 0);
970 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
975 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
976 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
977 for (i=pp->maxlevel;i>=0;i--)
978 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
979 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: %d read, %d skipped", i,
980 pp->accessed_nodes[i], pp->skipped_nodes[i]);
981 pp->isamb->skipped_numbers += pp->skipped_numbers;
982 pp->isamb->returned_numbers += pp->returned_numbers;
983 for (i=pp->maxlevel;i>=0;i--)
985 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
986 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
989 *size = pp->total_size;
991 *blocks = pp->no_blocks;
992 for (i = 0; i <= pp->level; i++)
993 close_block (pp->isamb, pp->block[i]);
998 int isamb_block_info (ISAMB isamb, int cat)
1000 if (cat >= 0 && cat < isamb->no_cat)
1001 return isamb->file[cat].head.block_size;
1005 void isamb_pp_close (ISAMB_PP pp)
1007 isamb_pp_close_x (pp, 0, 0);
1010 /* simple recursive dumper .. */
1011 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1015 char prefix_str[1024];
1018 struct ISAMB_block *p = open_block (b, pos);
1019 sprintf(prefix_str, "%*s %d cat=%d size=%d max=%d", level*2, "",
1020 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1022 sprintf(prefix_str, "%*s %d", level*2, "", pos);
1025 while (p->offset < p->size)
1027 char *src = p->bytes + p->offset;
1029 (*b->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1031 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1032 p->offset = src - (char*) p->bytes;
1034 assert(p->offset == p->size);
1038 char *src = p->bytes + p->offset;
1042 decode_ptr (&src, &sub);
1043 p->offset = src - (char*) p->bytes;
1045 isamb_dump_r(b, sub, pr, level+1);
1047 while (p->offset < p->size)
1049 decode_ptr (&src, &item_len);
1050 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1052 decode_ptr (&src, &sub);
1054 p->offset = src - (char*) p->bytes;
1056 isamb_dump_r(b, sub, pr, level+1);
1063 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1065 return isamb_dump_r(b, pos, pr, 0);
1069 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1070 debug the more complex pp_read that also forwards. May be deleted near end
1071 of 2004, if it has not shown to be useful */
1073 int isamb_pp_read (ISAMB_PP pp, void *buf)
1077 struct ISAMB_block *p = pp->block[pp->level];
1081 while (p->offset == p->size)
1084 while (p->offset == p->size)
1088 close_block (pp->isamb, pp->block[pp->level]);
1089 pp->block[pp->level] = 0;
1091 p = pp->block[pp->level];
1094 src = p->bytes + p->offset;
1096 decode_ptr (&src, &item_len);
1098 decode_ptr (&src, &pos);
1100 p->offset = src - (char*) p->bytes;
1106 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1108 pp->total_size += p->size;
1115 src = p->bytes + p->offset;
1116 decode_ptr (&src, &pos);
1117 p->offset = src - (char*) p->bytes;
1121 assert (p->offset < p->size);
1123 src = p->bytes + p->offset;
1124 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1126 p->offset = src - (char*) p->bytes;
1127 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1132 int isamb_pp_read (ISAMB_PP pp, void *buf)
1134 return isamb_pp_forward(pp,buf,0);
1138 #define NEW_FORWARD 0
1142 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1143 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1144 struct ISAMB_block *p = pp->block[pp->level];
1147 if (p->offset == p->size) {
1149 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1151 return 0; /* at end of leaf */
1153 src=p->bytes + p->offset;
1155 (*pp->isamb->method->code_item)
1156 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1157 p->offset = src - (char*) p->bytes;
1159 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1162 } /* read_on_leaf */
1165 static int isamb_pp_climb_level(ISAMB_PP pp, int *pos)
1166 { /* climbs higher in the tree, until finds a level with data left */
1167 /* returns teh node to (consider to) descend to in *pos) */
1168 struct ISAMB_block *p = pp->block[pp->level];
1171 assert(p->offset <= p->size);
1175 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1179 close_block(pp->isamb, pp->block[pp->level]);
1180 pp->block[pp->level]=0;
1182 p=pp->block[pp->level];
1183 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to node %d ofs=%d",
1186 assert (p->offset <= p->size);
1187 if (p->offset == p->size ) {
1188 /* we came from the last pointer, climb on */
1189 if (!isamb_pp_climb_level(pp,pos))
1191 p=pp->block[pp->level];
1193 /* skip the child we just came from */
1194 assert (p->offset < p->size );
1195 src=p->bytes + p->offset;
1196 decode_ptr(&src, &item_len);
1198 decode_ptr(&src, pos);
1199 p->offset=src - (char *)p->bytes;
1204 static void isamb_pp_descend_to_first_leaf(ISAMB_PP pp, int pos)
1205 { /* climbs down the tree, from pos, to the leftmost leaf */
1206 struct ISAMB_block *p = pp->block[pp->level];
1210 p=open_block(pp->isamb, pos);
1211 pp->block[pp->level]=p;
1212 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1215 logf(LOG_DEBUG,"isamb_pp_descend_to_first_leaf "
1216 "got lev %d node %d lf=%d",
1217 pp->level, p->pos, p->leaf);
1221 assert (p->offset==0 );
1222 src=p->bytes + p->offset;
1223 decode_ptr(&src, &pos);
1224 p->offset=src-(char*)p->bytes;
1225 isamb_pp_descend_to_first_leaf(pp,pos);
1226 } /* descend_to_first_leaf */
1228 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1229 { /* finds the next leaf by climbing up and down */
1231 if (!isamb_pp_climb_level(pp,&pos))
1233 isamb_pp_descend_to_first_leaf(pp, pos);
1236 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1237 { /* forwards on the current leaf, returns 0 if not found */
1238 assert (!"FIXME - not done yet");
1240 } /* forward_on_leaf */
1242 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1243 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1244 assert (!"FIXME - not done yet");
1248 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1251 struct ISAMB_block *p = pp->block[pp->level];
1254 untilbuf=0; /*FIXME - disabling the forward part */
1256 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1258 if (! isamb_pp_climb_desc( pp, buf, untilbuf))
1259 return 0; /* could not find a leaf */
1261 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1263 }while ( isamb_pp_find_next_leaf(pp));
1264 return 0; /* could not find at all */
1266 else { /* no untilbuf, a straight read */
1267 /* FIXME - this should be moved
1268 * directly into the pp_read */
1269 /* keeping here now, to keep same
1270 * interface as the old fwd */
1271 if (isamb_pp_read_on_leaf( pp, buf))
1273 if (isamb_pp_find_next_leaf(pp))
1274 return isamb_pp_read_on_leaf(pp, buf);
1278 } /* isam_pp_forward (new version) */
1282 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1286 * while at end of node
1287 * climb higher. If out, return 0
1288 * while not on a leaf (and not at its end)
1297 * The upper nodes consist of a sequence of nodenumbers and keys
1298 * When opening a block, the first node number is read in, and
1299 * offset points to the first key, which is the upper limit of keys
1300 * in the node just read.
1304 struct ISAMB_block *p = pp->block[pp->level];
1309 int descending=0; /* used to prevent a border condition error */
1313 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1315 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1316 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1321 while ( (p->offset == p->size) && !descending )
1322 { /* end of this block - climb higher */
1323 assert (p->offset <= p->size);
1325 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1331 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1333 return 0; /* at end of the root, nothing left */
1335 close_block(pp->isamb, pp->block[pp->level]);
1336 pp->block[pp->level]=0;
1338 p=pp->block[pp->level];
1340 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1344 assert(p->offset <= p->size);
1345 /* skip the child we have handled */
1346 if (p->offset != p->size)
1348 src = p->bytes + p->offset;
1349 decode_ptr(&src, &item_len);
1351 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1352 " isamb_pp_forward "
1353 "climb skipping old key");
1356 decode_ptr(&src,&pos);
1357 p->offset = src - (char*) p->bytes;
1358 break; /* even if this puts us at the end of the block, we
1359 need to descend to the last pos. UGLY coding,
1360 clean up some day */
1365 src = p->bytes + p->offset;
1366 if (p->offset == p->size)
1367 cmp=-2 ; /* descend to the last node, as we have
1371 decode_ptr(&src, &item_len);
1373 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1374 "ofs=%d sz=%d nxtpos=%d ",
1375 p->offset,p->size,pos);
1376 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1379 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1383 decode_ptr(&src,&nxtpos);
1388 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1391 descending=1; /* prevent climbing for a while */
1393 p = open_block(pp->isamb,pos);
1394 pp->block[pp->level] = p ;
1395 pp->total_size += p->size;
1396 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1399 { /* block starts with a pos */
1400 src = p->bytes + p->offset;
1401 decode_ptr(&src,&pos);
1402 p->offset=src-(char*) p->bytes;
1404 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1408 } /* descend to the node */
1410 { /* skip the node */
1411 p->offset = src - (char*) p->bytes;
1413 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1416 "isamb_pp_forward: skipping block on level %d, noting "
1418 pp->level, pp->maxlevel - pp->level-1 ,
1419 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1421 /* 0 is always leafs, 1 is one level above leafs etc, no
1422 * matter how high tree */
1424 } /* not on a leaf */
1427 if (p->offset == p->size) {
1432 assert (p->offset < p->size);
1433 src = p->bytes + p->offset;
1435 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1437 p->offset = src - (char*) p->bytes;
1439 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1443 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1445 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1452 (*pp->isamb->method->log_item)(
1453 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1457 (*pp->isamb->method->log_item)(
1458 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1461 pp->returned_numbers++;
1465 pp->skipped_numbers++;
1473 int isamb_pp_num (ISAMB_PP pp)