1 /* $Id: isamb.c,v 1.47 2004-08-03 14:54:41 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 0
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;
122 static void encode_ptr (char **dst, unsigned pos)
124 unsigned char *bp = (unsigned char*) *dst;
128 *bp++ = 128 | (pos & 127);
135 static void encode_ptr (char **dst, unsigned pos)
137 memcpy(*dst, &pos, sizeof(pos));
138 (*dst) += sizeof(pos);
143 static void decode_ptr (char **src1, int *pos)
145 unsigned char **src = (unsigned char **) src1;
150 while (((c = *(*src)++) & 128))
152 d += ((c & 127) << r);
159 static void decode_ptr (char **src, int *pos)
161 memcpy (pos, *src, sizeof(*pos));
162 (*src) += sizeof(*pos);
166 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
169 ISAMB isamb = xmalloc (sizeof(*isamb));
173 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
174 memcpy (isamb->method, method, sizeof(*method));
175 isamb->no_cat = CAT_NO;
177 isamb->log_freelist = 0;
178 isamb->cache = cache;
179 isamb->skipped_numbers=0;
180 isamb->returned_numbers=0;
181 for (i=0;i<ISAMB_MAX_LEVEL;i++)
182 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
185 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
186 for (i = 0; i<isamb->no_cat; i++)
188 char fname[DST_BUF_SIZE];
189 isamb->file[i].cache_entries = 0;
190 isamb->file[i].head_dirty = 0;
191 sprintf (fname, "%s%c", name, i+'A');
193 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
196 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
199 if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
200 &isamb->file[i].head))
202 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
203 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
204 isamb->file[i].head.block_size = b_size;
205 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
206 isamb->file[i].head.free_list = 0;
208 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
209 isamb->file[i].head_dirty = 0;
210 assert(isamb->file[i].head.block_size == b_size);
214 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
219 static void flush_blocks (ISAMB b, int cat)
221 while (b->file[cat].cache_entries)
223 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
224 b->file[cat].cache_entries = ce_this->next;
228 yaz_log (b->log_io, "bf_write: flush_blocks");
229 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
231 xfree (ce_this->buf);
236 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
238 int cat = pos&CAT_MASK;
239 int off = ((pos/CAT_MAX) &
240 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
241 * b->file[cat].head.block_size;
242 int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
244 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
249 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
250 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
253 if ((*ce)->pos == norm)
256 *ce = (*ce)->next; /* remove from list */
258 ce_this->next = b->file[cat].cache_entries; /* move to front */
259 b->file[cat].cache_entries = ce_this;
263 memcpy (ce_this->buf + off, userbuf,
264 b->file[cat].head.block_size);
268 memcpy (userbuf, ce_this->buf + off,
269 b->file[cat].head.block_size);
276 assert (ce_last && *ce_last);
278 *ce_last = 0; /* remove the last entry from list */
281 yaz_log (b->log_io, "bf_write: get_block");
282 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
284 xfree (ce_this->buf);
287 ce_this = xmalloc (sizeof(*ce_this));
288 ce_this->next = b->file[cat].cache_entries;
289 b->file[cat].cache_entries = ce_this;
290 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
292 yaz_log (b->log_io, "bf_read: get_block");
293 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
294 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
297 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
303 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
309 void isamb_close (ISAMB isamb)
312 for (i=0;isamb->accessed_nodes[i];i++)
313 logf(LOG_DEBUG,"isamb_close level leaf-%d: %d read, %d skipped",
314 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
315 logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
316 isamb->skipped_numbers, isamb->returned_numbers);
317 for (i = 0; i<isamb->no_cat; i++)
319 flush_blocks (isamb, i);
320 if (isamb->file[i].head_dirty)
321 bf_write (isamb->file[i].bf, 0, 0,
322 sizeof(struct ISAMB_head), &isamb->file[i].head);
324 bf_close (isamb->file[i].bf);
327 xfree (isamb->method);
331 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
333 int cat = pos&CAT_MASK;
334 struct ISAMB_block *p;
337 p = xmalloc (sizeof(*p));
339 p->cat = pos & CAT_MASK;
340 p->buf = xmalloc (b->file[cat].head.block_size);
343 if (!get_block (b, pos, p->buf, 0))
345 yaz_log (b->log_io, "bf_read: open_block");
346 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
348 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
349 (long) pos, (long) pos/CAT_MAX);
353 p->bytes = p->buf + ISAMB_DATA_OFFSET;
355 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
358 yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
360 assert (p->size >= 0);
364 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
365 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
369 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
371 struct ISAMB_block *p;
373 p = xmalloc (sizeof(*p));
374 p->buf = xmalloc (b->file[cat].head.block_size);
376 if (!b->file[cat].head.free_list)
379 block_no = b->file[cat].head.last_block++;
380 p->pos = block_no * CAT_MAX + cat;
384 p->pos = b->file[cat].head.free_list;
385 assert((p->pos & CAT_MASK) == cat);
386 if (!get_block (b, p->pos, p->buf, 0))
388 yaz_log (b->log_io, "bf_read: new_block");
389 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
391 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
392 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
396 yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
397 cat, p->pos/CAT_MAX);
398 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
401 b->file[cat].head_dirty = 1;
402 memset (p->buf, 0, b->file[cat].head.block_size);
403 p->bytes = p->buf + ISAMB_DATA_OFFSET;
409 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
413 struct ISAMB_block *new_leaf (ISAMB b, int cat)
415 return new_block (b, 1, cat);
419 struct ISAMB_block *new_int (ISAMB b, int cat)
421 return new_block (b, 0, cat);
424 static void check_block (ISAMB b, struct ISAMB_block *p)
433 char *startp = p->bytes;
435 char *endp = p->bytes + p->size;
438 decode_ptr (&src, &pos);
439 assert ((pos&CAT_MASK) == p->cat);
443 decode_ptr (&src, &item_len);
444 assert (item_len > 0 && item_len < 30);
446 decode_ptr (&src, &pos);
447 assert ((pos&CAT_MASK) == p->cat);
452 void close_block (ISAMB b, struct ISAMB_block *p)
458 yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
459 p->pos, p->cat, p->pos/CAT_MAX);
460 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
461 b->file[p->cat].head.free_list = p->pos;
462 if (!get_block (b, p->pos, p->buf, 1))
464 yaz_log (b->log_io, "bf_write: close_block (deleted)");
465 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
470 int size = p->size + ISAMB_DATA_OFFSET;
471 assert (p->size >= 0);
473 p->buf[1] = size & 255;
474 p->buf[2] = size >> 8;
476 if (!get_block (b, p->pos, p->buf, 1))
478 yaz_log (b->log_io, "bf_write: close_block");
479 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
482 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
487 int insert_sub (ISAMB b, struct ISAMB_block **p,
488 void *new_item, int *mode,
490 struct ISAMB_block **sp,
491 void *sub_item, int *sub_size,
494 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
496 ISAMC_I *stream, struct ISAMB_block **sp,
497 void *split_item, int *split_size, void *last_max_item)
499 char *startp = p->bytes;
501 char *endp = p->bytes + p->size;
503 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
504 char sub_item[DST_ITEM_MAX];
510 assert(p->size >= 0);
511 decode_ptr (&src, &pos);
517 decode_ptr (&src, &item_len);
518 d = (*b->method->compare_item)(src, lookahead_item);
521 sub_p1 = open_block (b, pos);
523 more = insert_sub (b, &sub_p1, lookahead_item, mode,
525 sub_item, &sub_size, src);
530 decode_ptr (&src, &pos);
534 sub_p1 = open_block (b, pos);
536 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
537 sub_item, &sub_size, last_max_item);
541 /* there was a split - must insert pointer in this one */
542 char dst_buf[DST_BUF_SIZE];
545 assert (sub_size < 30 && sub_size > 1);
547 memcpy (dst, startp, src - startp);
551 encode_ptr (&dst, sub_size); /* sub length and item */
552 memcpy (dst, sub_item, sub_size);
555 encode_ptr (&dst, sub_p2->pos); /* pos */
557 if (endp - src) /* remaining data */
559 memcpy (dst, src, endp - src);
562 p->size = dst - dst_buf;
563 assert (p->size >= 0);
564 if (p->size <= b->file[p->cat].head.block_max)
566 memcpy (startp, dst_buf, dst - dst_buf);
575 half = src + b->file[p->cat].head.block_size/2;
576 decode_ptr (&src, &pos);
579 decode_ptr (&src, split_size);
581 decode_ptr (&src, &pos);
583 p_new_size = src - dst_buf;
584 memcpy (p->bytes, dst_buf, p_new_size);
586 decode_ptr (&src, split_size);
587 memcpy (split_item, src, *split_size);
590 *sp = new_int (b, p->cat);
591 (*sp)->size = endp - src;
592 memcpy ((*sp)->bytes, src, (*sp)->size);
594 p->size = p_new_size;
597 close_block (b, sub_p2);
599 close_block (b, sub_p1);
604 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
605 int *lookahead_mode, ISAMC_I *stream,
606 struct ISAMB_block **sp2,
607 void *sub_item, int *sub_size,
610 struct ISAMB_block *p = *sp1;
611 char *src = 0, *endp = 0;
612 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
614 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
615 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
617 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
618 char *cut = dst_buf + quater * 2;
619 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
622 char cut_item_buf[DST_ITEM_MAX];
623 int cut_item_size = 0;
627 char file_item_buf[DST_ITEM_MAX];
628 char *file_item = file_item_buf;
631 endp = p->bytes + p->size;
632 (*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);
681 lookahead_next = lookahead_item;
682 if (!(*stream->read_item)(stream->clientData,
689 if (lookahead_item && max_item &&
690 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
701 lookahead_next = lookahead_item;
702 if (!(*stream->read_item)(stream->clientData,
703 &lookahead_next, lookahead_mode))
710 file_item = file_item_buf;
711 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
717 file_item = file_item_buf;
718 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
722 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
723 while (lookahead_item)
725 char *dst_item = lookahead_item;
729 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
734 if (!*lookahead_mode)
736 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
739 else if (!half1 && dst > cut)
741 char *dst_item_0 = dst_item;
742 half1 = dst; /* candidate for splitting */
744 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
746 cut_item_size = dst_item - dst_item_0;
747 memcpy (cut_item_buf, dst_item_0, cut_item_size);
752 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
761 dst_item = lookahead_item;
762 if (!(*stream->read_item)(stream->clientData, &dst_item,
769 new_size = dst - dst_buf;
770 if (p && p->cat != b->no_cat-1 &&
771 new_size > b->file[p->cat].head.block_max)
773 /* non-btree block will be removed */
776 /* delete it too!! */
777 p = 0; /* make a new one anyway */
780 { /* must create a new one */
782 for (i = 0; i < b->no_cat; i++)
783 if (new_size <= b->file[i].head.block_max)
789 if (new_size > b->file[p->cat].head.block_max)
792 char *cut_item = cut_item_buf;
798 p->size = half1 - dst_buf;
799 memcpy (p->bytes, dst_buf, half1 - dst_buf);
802 *sp2 = new_leaf (b, p->cat);
804 (*b->method->code_reset)(c2);
806 first_dst = (*sp2)->bytes;
808 (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
810 memcpy (first_dst, half2, dst - half2);
812 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
815 memcpy (sub_item, cut_item_buf, cut_item_size);
816 *sub_size = cut_item_size;
820 memcpy (p->bytes, dst_buf, dst - dst_buf);
823 (*b->method->code_stop)(ISAMC_DECODE, c1);
824 (*b->method->code_stop)(ISAMC_ENCODE, c2);
829 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
832 struct ISAMB_block **sp,
833 void *sub_item, int *sub_size,
836 if (!*p || (*p)->leaf)
837 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
840 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
844 int isamb_unlink (ISAMB b, ISAMC_P pos)
846 struct ISAMB_block *p1;
850 p1 = open_block(b, pos);
856 char *src = p1->bytes + p1->offset;
858 decode_ptr(&src, &sub_p);
859 isamb_unlink(b, sub_p);
861 while (src != p1->bytes + p1->size)
863 decode_ptr(&src, &item_len);
865 decode_ptr(&src, &sub_p);
866 isamb_unlink(b, sub_p);
873 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
875 char item_buf[DST_ITEM_MAX];
887 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
892 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
895 struct ISAMB_block *p = 0, *sp = 0;
896 char sub_item[DST_ITEM_MAX];
900 p = open_block (b, pos);
901 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
902 sub_item, &sub_size, 0);
904 { /* increase level of tree by one */
905 struct ISAMB_block *p2 = new_int (b, p->cat);
906 char *dst = p2->bytes + p2->size;
908 encode_ptr (&dst, p->pos);
909 assert (sub_size < 20);
910 encode_ptr (&dst, sub_size);
911 memcpy (dst, sub_item, sub_size);
913 encode_ptr (&dst, sp->pos);
915 p2->size = dst - p2->bytes;
916 pos = p2->pos; /* return new super page */
921 pos = p->pos; /* return current one (again) */
927 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
929 ISAMB_PP pp = xmalloc (sizeof(*pp));
933 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
940 pp->skipped_numbers=0;
941 pp->returned_numbers=0;
942 for (i=0;i<ISAMB_MAX_LEVEL;i++)
943 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;
956 decode_ptr (&src, &pos);
957 p->offset = src - p->bytes;
959 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 return 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->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);
1265 assert(p->offset <= p->size);
1269 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1273 assert(pp->level>0);
1274 close_block(pp->isamb, pp->block[pp->level]);
1275 pp->block[pp->level]=0;
1277 p=pp->block[pp->level];
1279 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1280 pp->level, p->pos, p->offset);
1283 assert(p->offset <= p->size);
1284 if (p->offset == p->size ) {
1285 /* we came from the last pointer, climb on */
1286 if (!isamb_pp_climb_level(pp,pos))
1288 p=pp->block[pp->level];
1292 /* skip the child we just came from */
1294 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1295 pp->level, p->offset, p->size);
1297 assert (p->offset < p->size );
1298 src=p->bytes + p->offset;
1299 decode_ptr(&src, &item_len);
1301 decode_ptr(&src, pos);
1302 p->offset=src - (char *)p->bytes;
1309 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1310 { /* scans a upper node until it finds a child <= untilbuf */
1311 /* pp points to the key value, as always. pos is the child read from */
1313 /* if all values are too small, returns the last child in the node */
1314 /* FIXME - this can be detected, and avoided by looking at the */
1315 /* parent node, but that gets messy. Presumably the cost is */
1316 /* pretty low anyway */
1317 struct ISAMB_block *p = pp->block[pp->level];
1318 char *src=p->bytes + p->offset;
1324 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1325 "at level %d node %d ofs=%di sz=%d",
1326 pp->level, p->pos, p->offset, p->size);
1329 assert(p->offset <= p->size);
1330 if (p->offset == p->size) {
1332 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1333 "at level %d node %d ofs=%di sz=%d",
1334 pp->level, p->pos, p->offset, p->size);
1336 return pos; /* already at the end of it */
1338 while(p->offset < p->size) {
1339 decode_ptr(&src,&item_len);
1340 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1342 decode_ptr(&src,&nxtpos);
1346 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1347 "at level %d node %d ofs=%d sz=%d",
1348 pp->level, p->pos, p->offset, p->size);
1353 p->offset=src-(char*)p->bytes;
1354 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1360 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1361 "at level %d node %d ofs=%d sz=%d skips=%d",
1362 pp->level, p->pos, p->offset, p->size, skips);
1364 return pos; /* that's the last one in the line */
1366 } /* forward_unode */
1368 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1369 { /* climbs down the tree, from pos, to the leftmost leaf */
1370 struct ISAMB_block *p = pp->block[pp->level];
1374 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1375 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1376 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1379 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1382 p=open_block(pp->isamb, pos);
1383 pp->block[pp->level]=p;
1384 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1387 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1388 "got lev %d node %d lf=%d",
1389 pp->level, p->pos, p->leaf);
1393 assert (p->offset==0 );
1394 src=p->bytes + p->offset;
1395 decode_ptr(&src, &pos);
1396 p->offset=src-(char*)p->bytes;
1397 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1399 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1400 "returning at lev %d node %d ofs=%d lf=%d",
1401 pp->level, p->pos, p->offset, p->leaf);
1403 } /* descend_to_leaf */
1405 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1406 { /* finds the next leaf by climbing up and down */
1408 if (!isamb_pp_climb_level(pp,&pos))
1410 isamb_pp_descend_to_leaf(pp, pos,0);
1414 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1415 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1418 struct ISAMB_block *p = pp->block[pp->level];
1419 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1420 "at level %d node %d ofs=%d sz=%d",
1421 pp->level, p->pos, p->offset, p->size);
1423 if (!isamb_pp_climb_level(pp,&pos))
1425 /* see if it would pay to climb one higher */
1426 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1427 if (!isamb_pp_climb_level(pp,&pos))
1429 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1431 p = pp->block[pp->level];
1432 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1433 "at level %d node %d ofs=%d sz=%d",
1434 pp->level, p->pos, p->offset, p->size);
1439 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1442 struct ISAMB_block *p = pp->block[pp->level];
1444 logf(LOG_DEBUG,"isamb_pp_forward starting "
1445 "at level %d node %d ofs=%d sz=%d u=%p",
1446 pp->level, p->pos, p->offset, p->size,untilbuf);
1449 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1451 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1452 "at level %d node %d ofs=%d sz=%d",
1453 pp->level, p->pos, p->offset, p->size);
1457 if (! isamb_pp_climb_desc( pp, buf, untilbuf)) {
1459 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1460 "at level %d node %d ofs=%d sz=%d",
1461 pp->level, p->pos, p->offset, p->size);
1463 return 0; /* could not find a leaf */
1466 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1468 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1469 "at level %d node %d ofs=%d sz=%d",
1470 pp->level, p->pos, p->offset, p->size);
1474 }while ( isamb_pp_find_next_leaf(pp));
1475 return 0; /* could not find at all */
1477 else { /* no untilbuf, a straight read */
1478 /* FIXME - this should be moved
1479 * directly into the pp_read */
1480 /* keeping here now, to keep same
1481 * interface as the old fwd */
1482 if (isamb_pp_read_on_leaf( pp, buf)) {
1484 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1485 "at level %d node %d ofs=%d sz=%d",
1486 pp->level, p->pos, p->offset, p->size);
1490 if (isamb_pp_find_next_leaf(pp)) {
1492 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1493 "at level %d node %d ofs=%d sz=%d",
1494 pp->level, p->pos, p->offset, p->size);
1496 return isamb_pp_read_on_leaf(pp, buf);
1501 } /* isam_pp_forward (new version) */
1503 #elif NEW_FORWARD == 0
1505 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1509 * while at end of node
1510 * climb higher. If out, return 0
1511 * while not on a leaf (and not at its end)
1520 * The upper nodes consist of a sequence of nodenumbers and keys
1521 * When opening a block, the first node number is read in, and
1522 * offset points to the first key, which is the upper limit of keys
1523 * in the node just read.
1527 struct ISAMB_block *p = pp->block[pp->level];
1532 int descending=0; /* used to prevent a border condition error */
1536 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1538 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1539 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1544 while ( (p->offset == p->size) && !descending )
1545 { /* end of this block - climb higher */
1546 assert (p->offset <= p->size);
1548 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1554 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1556 return 0; /* at end of the root, nothing left */
1558 close_block(pp->isamb, pp->block[pp->level]);
1559 pp->block[pp->level]=0;
1561 p=pp->block[pp->level];
1563 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1567 assert(p->offset <= p->size);
1568 /* skip the child we have handled */
1569 if (p->offset != p->size)
1571 src = p->bytes + p->offset;
1572 decode_ptr(&src, &item_len);
1574 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1575 " isamb_pp_forward "
1576 "climb skipping old key");
1579 decode_ptr(&src,&pos);
1580 p->offset = src - (char*) p->bytes;
1581 break; /* even if this puts us at the end of the block, we
1582 need to descend to the last pos. UGLY coding,
1583 clean up some day */
1588 src = p->bytes + p->offset;
1589 if (p->offset == p->size)
1590 cmp=-2 ; /* descend to the last node, as we have
1594 decode_ptr(&src, &item_len);
1596 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1597 "ofs=%d sz=%d nxtpos=%d ",
1598 p->offset,p->size,pos);
1599 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1602 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1606 decode_ptr(&src,&nxtpos);
1611 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1614 descending=1; /* prevent climbing for a while */
1616 p = open_block(pp->isamb,pos);
1617 pp->block[pp->level] = p ;
1618 pp->total_size += p->size;
1619 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1622 { /* block starts with a pos */
1623 src = p->bytes + p->offset;
1624 decode_ptr(&src,&pos);
1625 p->offset=src-(char*) p->bytes;
1627 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1631 } /* descend to the node */
1633 { /* skip the node */
1634 p->offset = src - (char*) p->bytes;
1636 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1639 "isamb_pp_forward: skipping block on level %d, noting "
1641 pp->level, pp->maxlevel - pp->level-1 ,
1642 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1644 /* 0 is always leafs, 1 is one level above leafs etc, no
1645 * matter how high tree */
1647 } /* not on a leaf */
1650 if (p->offset == p->size) {
1655 assert (p->offset < p->size);
1656 src = p->bytes + p->offset;
1658 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1660 p->offset = src - (char*) p->bytes;
1662 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1666 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1668 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1675 (*pp->isamb->method->log_item)(
1676 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1680 (*pp->isamb->method->log_item)(
1681 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1684 pp->returned_numbers++;
1688 pp->skipped_numbers++;
1694 #elif NEW_FORWARD == 2
1696 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1700 struct ISAMB_block *p = pp->block[pp->level];
1705 while (p->offset == p->size)
1708 while (p->offset == p->size)
1712 close_block (pp->isamb, pp->block[pp->level]);
1713 pp->block[pp->level] = 0;
1715 p = pp->block[pp->level];
1720 src = p->bytes + p->offset;
1722 decode_ptr (&src, &item_len);
1724 decode_ptr (&src, &pos);
1726 p->offset = src - (char*) p->bytes;
1728 src = p->bytes + p->offset;
1732 if (!untilb || p->offset == p->size)
1734 assert(p->offset < p->size);
1735 decode_ptr (&src, &item_len);
1736 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1739 decode_ptr (&src, &pos);
1740 p->offset = src - (char*) p->bytes;
1747 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1749 pp->total_size += p->size;
1757 src = p->bytes + p->offset;
1760 decode_ptr (&src, &pos);
1761 p->offset = src - (char*) p->bytes;
1763 if (!untilb || p->offset == p->size)
1765 assert(p->offset < p->size);
1766 decode_ptr (&src, &item_len);
1767 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1774 assert (p->offset < p->size);
1779 src = p->bytes + p->offset;
1780 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1782 p->offset = src - (char*) p->bytes;
1783 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1786 if (p->offset == p->size) goto again;
1788 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1794 int isamb_pp_num (ISAMB_PP pp)
1799 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1800 int *current, int *total, void *dummybuf )
1802 struct ISAMB_block *p = pp->block[pp->level];
1804 char *end=p->bytes+p->size;
1805 char *cur=p->bytes+p->offset;
1807 assert(p->offset <= p->size);
1815 (*pp->isamb->method->code_item)
1816 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1817 assert(dst<(char*) dummybuf+100); /*FIXME */
1822 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur=%d tot=%d ofs=%d sz=%d lev=%d",
1823 *current, *total, p->offset, p->size, pp->level);
1827 static void isamb_pp_upper_pos( ISAMB_PP pp, int *current, int *total,
1828 int size, int level )
1829 { /* estimates total/current occurrences from here up, excl leaf */
1830 struct ISAMB_block *p = pp->block[level];
1832 char *end=p->bytes+p->size;
1833 char *cur=p->bytes+p->offset;
1838 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1839 "cur=%d tot=%d ofs=%d sz=%d pos=%d",
1840 level, *current, *total, p->offset, p->size, p->pos);
1841 assert (p->offset <= p->size);
1842 decode_ptr (&src, &child ); /* first child */
1849 decode_ptr (&src, &item_size );
1850 assert(src+item_size<=end);
1852 decode_ptr (&src, &child );
1855 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1858 void isamb_pp_pos( ISAMB_PP pp, int *current, int *total )
1859 { /* return an estimate of the current position and of the total number of */
1860 /* occureences in the isam tree, based on the current leaf */
1861 struct ISAMB_block *p = pp->block[pp->level];
1862 char dummy[100]; /* 100 bytes/entry must be enough */
1866 isamb_pp_leaf_pos(pp,current, total, dummy);
1868 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1870 logf(LOG_DEBUG,"isamb_pp_pos: C=%d T=%d =%6.2f%%",
1871 *current, *total, 100.0*(*current)/(*total));