1 /* $Id: isamb.c,v 1.42 2004-06-03 10:29:49 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 */
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
1147 #define ISAMB_DEBUG 1
1150 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1151 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1152 struct ISAMB_block *p = pp->block[pp->level];
1155 if (p->offset == p->size) {
1157 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1159 return 0; /* at end of leaf */
1161 src=p->bytes + p->offset;
1163 (*pp->isamb->method->code_item)
1164 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1165 p->offset = src - (char*) p->bytes;
1167 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1170 } /* read_on_leaf */
1173 static int isamb_pp_climb_level(ISAMB_PP pp, int *pos)
1174 { /* climbs higher in the tree, until finds a level with data left */
1175 /* returns the node to (consider to) descend to in *pos) */
1176 struct ISAMB_block *p = pp->block[pp->level];
1180 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1181 "at level %d node %d ofs=%d sz=%d",
1182 pp->level, p->pos, p->offset, p->size);
1184 assert(p->offset <= p->size);
1188 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1192 close_block(pp->isamb, pp->block[pp->level]);
1193 pp->block[pp->level]=0;
1195 p=pp->block[pp->level];
1197 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1198 pp->level, p->pos, p->offset);
1201 assert (p->offset <= p->size);
1202 if (p->offset == p->size ) {
1203 /* we came from the last pointer, climb on */
1204 if (!isamb_pp_climb_level(pp,pos))
1206 p=pp->block[pp->level];
1210 /* skip the child we just came from */
1212 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1213 pp->level, p->offset, p->size);
1215 assert (p->offset < p->size );
1216 src=p->bytes + p->offset;
1217 decode_ptr(&src, &item_len);
1219 decode_ptr(&src, pos);
1220 p->offset=src - (char *)p->bytes;
1226 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1227 { /* scans a upper node until it finds a child <= untilbuf */
1228 /* pp points to the key value, as always. pos is the child read from */
1230 /* if all values are too small, returns the last child in the node */
1231 /* FIXME - this can be detected, and avoided by looking at the */
1232 /* parent node, but that gets messy. Presumably the cost is */
1233 /* pretty low anyway */
1234 struct ISAMB_block *p = pp->block[pp->level];
1235 char *src=p->bytes + p->offset;
1240 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1241 "at level %d node %d ofs=%di sz=%d",
1242 pp->level, p->pos, p->offset, p->size);
1245 assert(p->offset <= p->size);
1246 if (p->offset == p->size) {
1248 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1249 "at level %d node %d ofs=%di sz=%d",
1250 pp->level, p->pos, p->offset, p->size);
1252 return pos; /* already at the end of it */
1254 while(p->offset < p->size) {
1255 decode_ptr(&src,&item_len);
1256 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1258 decode_ptr(&src,&nxtpos);
1262 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1263 "at level %d node %d ofs=%di sz=%d",
1264 pp->level, p->pos, p->offset, p->size);
1269 p->offset=src-(char*)p->bytes;
1272 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1273 "at level %d node %d ofs=%di sz=%d",
1274 pp->level, p->pos, p->offset, p->size);
1276 return pos; /* that's the last one in the line */
1278 } /* forward_unode */
1280 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1281 { /* climbs down the tree, from pos, to the leftmost leaf */
1282 struct ISAMB_block *p = pp->block[pp->level];
1286 p=open_block(pp->isamb, pos);
1287 pp->block[pp->level]=p;
1288 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1291 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1292 "got lev %d node %d lf=%d",
1293 pp->level, p->pos, p->leaf);
1297 assert (p->offset==0 );
1298 src=p->bytes + p->offset;
1299 decode_ptr(&src, &pos);
1300 p->offset=src-(char*)p->bytes;
1302 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1303 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1304 } /* descend_to_leaf */
1306 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1307 { /* finds the next leaf by climbing up and down */
1309 if (!isamb_pp_climb_level(pp,&pos))
1311 isamb_pp_descend_to_leaf(pp, pos,0);
1314 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1315 { /* forwards on the current leaf, returns 0 if not found */
1319 if (!isamb_pp_read_on_leaf(pp,buf))
1321 /* FIXME - this is an extra function call, inline the read? */
1322 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1323 if (cmp <2){ /* found a good one */
1326 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1328 pp->returned_numbers++;
1331 pp->skipped_numbers++;
1334 } /* forward_on_leaf */
1336 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1337 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1339 if (!isamb_pp_climb_level(pp,&pos))
1341 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1345 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1348 struct ISAMB_block *p = pp->block[pp->level];
1352 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1354 if (! isamb_pp_climb_desc( pp, buf, untilbuf))
1355 return 0; /* could not find a leaf */
1357 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1359 }while ( isamb_pp_find_next_leaf(pp));
1360 return 0; /* could not find at all */
1362 else { /* no untilbuf, a straight read */
1363 /* FIXME - this should be moved
1364 * directly into the pp_read */
1365 /* keeping here now, to keep same
1366 * interface as the old fwd */
1367 if (isamb_pp_read_on_leaf( pp, buf))
1369 if (isamb_pp_find_next_leaf(pp))
1370 return isamb_pp_read_on_leaf(pp, buf);
1374 } /* isam_pp_forward (new version) */
1376 #elif NEW_FORWARD == 0
1378 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1382 * while at end of node
1383 * climb higher. If out, return 0
1384 * while not on a leaf (and not at its end)
1393 * The upper nodes consist of a sequence of nodenumbers and keys
1394 * When opening a block, the first node number is read in, and
1395 * offset points to the first key, which is the upper limit of keys
1396 * in the node just read.
1400 struct ISAMB_block *p = pp->block[pp->level];
1405 int descending=0; /* used to prevent a border condition error */
1409 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1411 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1412 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1417 while ( (p->offset == p->size) && !descending )
1418 { /* end of this block - climb higher */
1419 assert (p->offset <= p->size);
1421 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1427 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1429 return 0; /* at end of the root, nothing left */
1431 close_block(pp->isamb, pp->block[pp->level]);
1432 pp->block[pp->level]=0;
1434 p=pp->block[pp->level];
1436 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1440 assert(p->offset <= p->size);
1441 /* skip the child we have handled */
1442 if (p->offset != p->size)
1444 src = p->bytes + p->offset;
1445 decode_ptr(&src, &item_len);
1447 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1448 " isamb_pp_forward "
1449 "climb skipping old key");
1452 decode_ptr(&src,&pos);
1453 p->offset = src - (char*) p->bytes;
1454 break; /* even if this puts us at the end of the block, we
1455 need to descend to the last pos. UGLY coding,
1456 clean up some day */
1461 src = p->bytes + p->offset;
1462 if (p->offset == p->size)
1463 cmp=-2 ; /* descend to the last node, as we have
1467 decode_ptr(&src, &item_len);
1469 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1470 "ofs=%d sz=%d nxtpos=%d ",
1471 p->offset,p->size,pos);
1472 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1475 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1479 decode_ptr(&src,&nxtpos);
1484 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1487 descending=1; /* prevent climbing for a while */
1489 p = open_block(pp->isamb,pos);
1490 pp->block[pp->level] = p ;
1491 pp->total_size += p->size;
1492 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1495 { /* block starts with a pos */
1496 src = p->bytes + p->offset;
1497 decode_ptr(&src,&pos);
1498 p->offset=src-(char*) p->bytes;
1500 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1504 } /* descend to the node */
1506 { /* skip the node */
1507 p->offset = src - (char*) p->bytes;
1509 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1512 "isamb_pp_forward: skipping block on level %d, noting "
1514 pp->level, pp->maxlevel - pp->level-1 ,
1515 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1517 /* 0 is always leafs, 1 is one level above leafs etc, no
1518 * matter how high tree */
1520 } /* not on a leaf */
1523 if (p->offset == p->size) {
1528 assert (p->offset < p->size);
1529 src = p->bytes + p->offset;
1531 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1533 p->offset = src - (char*) p->bytes;
1535 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1539 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1541 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1548 (*pp->isamb->method->log_item)(
1549 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1553 (*pp->isamb->method->log_item)(
1554 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1557 pp->returned_numbers++;
1561 pp->skipped_numbers++;
1567 #elif NEW_FORWARD == 2
1569 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1573 struct ISAMB_block *p = pp->block[pp->level];
1578 while (p->offset == p->size)
1581 while (p->offset == p->size)
1585 close_block (pp->isamb, pp->block[pp->level]);
1586 pp->block[pp->level] = 0;
1588 p = pp->block[pp->level];
1593 src = p->bytes + p->offset;
1595 decode_ptr (&src, &item_len);
1597 decode_ptr (&src, &pos);
1599 p->offset = src - (char*) p->bytes;
1601 src = p->bytes + p->offset;
1605 if (!untilb || p->offset == p->size)
1607 assert(p->offset < p->size);
1608 decode_ptr (&src, &item_len);
1609 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1612 decode_ptr (&src, &pos);
1613 p->offset = src - (char*) p->bytes;
1620 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1622 pp->total_size += p->size;
1630 src = p->bytes + p->offset;
1633 decode_ptr (&src, &pos);
1634 p->offset = src - (char*) p->bytes;
1636 if (!untilb || p->offset == p->size)
1638 assert(p->offset < p->size);
1639 decode_ptr (&src, &item_len);
1640 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1647 assert (p->offset < p->size);
1652 src = p->bytes + p->offset;
1653 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1655 p->offset = src - (char*) p->bytes;
1656 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1659 if (p->offset == p->size) goto again;
1661 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1667 int isamb_pp_num (ISAMB_PP pp)