1 /* $Id: isamb.c,v 1.45 2004-06-08 15:15:51 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include <yaz/xmalloc.h>
41 #define ISAMB_DATA_OFFSET 3
43 /* maximum size of encoded buffer */
44 #define DST_ITEM_MAX 256
46 #define ISAMB_MAX_LEVEL 10
47 /* approx 2*max page + max size of item */
48 #define DST_BUF_SIZE 16840
50 #define ISAMB_CACHE_ENTRY_SIZE 4096
52 /* CAT_MAX: _must_ be power of 2 */
54 #define CAT_MASK (CAT_MAX-1)
55 /* CAT_NO: <= CAT_MAX */
58 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
59 #define ISAMB_PTR_CODEC 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;
120 void isamb_pp_pos( ISAMB_PP pp, int *current, int *total );
121 /* FIXME - this should be in a header file */
124 static void encode_ptr (char **dst, unsigned pos)
126 unsigned char *bp = (unsigned char*) *dst;
130 *bp++ = 128 | (pos & 127);
137 static void encode_ptr (char **dst, unsigned pos)
139 memcpy(*dst, &pos, sizeof(pos));
140 (*dst) += sizeof(pos);
145 static void decode_ptr (char **src1, int *pos)
147 unsigned char **src = (unsigned char **) src1;
152 while (((c = *(*src)++) & 128))
154 d += ((c & 127) << r);
161 static void decode_ptr (char **src, int *pos)
163 memcpy (pos, *src, sizeof(*pos));
164 (*src) += sizeof(*pos);
168 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
171 ISAMB isamb = xmalloc (sizeof(*isamb));
175 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
176 memcpy (isamb->method, method, sizeof(*method));
177 isamb->no_cat = CAT_NO;
179 isamb->log_freelist = 0;
180 isamb->cache = cache;
181 isamb->skipped_numbers=0;
182 isamb->returned_numbers=0;
183 for (i=0;i<ISAMB_MAX_LEVEL;i++)
184 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
187 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
188 for (i = 0; i<isamb->no_cat; i++)
190 char fname[DST_BUF_SIZE];
191 isamb->file[i].cache_entries = 0;
192 isamb->file[i].head_dirty = 0;
193 sprintf (fname, "%s%c", name, i+'A');
195 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
198 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
201 if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
202 &isamb->file[i].head))
204 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
205 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
206 isamb->file[i].head.block_size = b_size;
207 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
208 isamb->file[i].head.free_list = 0;
210 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
211 isamb->file[i].head_dirty = 0;
212 assert(isamb->file[i].head.block_size == b_size);
216 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
221 static void flush_blocks (ISAMB b, int cat)
223 while (b->file[cat].cache_entries)
225 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
226 b->file[cat].cache_entries = ce_this->next;
230 yaz_log (b->log_io, "bf_write: flush_blocks");
231 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
233 xfree (ce_this->buf);
238 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
240 int cat = pos&CAT_MASK;
241 int off = ((pos/CAT_MAX) &
242 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
243 * b->file[cat].head.block_size;
244 int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
246 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
251 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
252 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
255 if ((*ce)->pos == norm)
258 *ce = (*ce)->next; /* remove from list */
260 ce_this->next = b->file[cat].cache_entries; /* move to front */
261 b->file[cat].cache_entries = ce_this;
265 memcpy (ce_this->buf + off, userbuf,
266 b->file[cat].head.block_size);
270 memcpy (userbuf, ce_this->buf + off,
271 b->file[cat].head.block_size);
278 assert (ce_last && *ce_last);
280 *ce_last = 0; /* remove the last entry from list */
283 yaz_log (b->log_io, "bf_write: get_block");
284 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
286 xfree (ce_this->buf);
289 ce_this = xmalloc (sizeof(*ce_this));
290 ce_this->next = b->file[cat].cache_entries;
291 b->file[cat].cache_entries = ce_this;
292 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
294 yaz_log (b->log_io, "bf_read: get_block");
295 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
296 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
299 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
305 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
311 void isamb_close (ISAMB isamb)
314 for (i=0;isamb->accessed_nodes[i];i++)
315 logf(LOG_DEBUG,"isamb_close level leaf-%d: %d read, %d skipped",
316 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
317 logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
318 isamb->skipped_numbers, isamb->returned_numbers);
319 for (i = 0; i<isamb->no_cat; i++)
321 flush_blocks (isamb, i);
322 if (isamb->file[i].head_dirty)
323 bf_write (isamb->file[i].bf, 0, 0,
324 sizeof(struct ISAMB_head), &isamb->file[i].head);
326 bf_close (isamb->file[i].bf);
329 xfree (isamb->method);
333 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
335 int cat = pos&CAT_MASK;
336 struct ISAMB_block *p;
339 p = xmalloc (sizeof(*p));
341 p->cat = pos & CAT_MASK;
342 p->buf = xmalloc (b->file[cat].head.block_size);
345 if (!get_block (b, pos, p->buf, 0))
347 yaz_log (b->log_io, "bf_read: open_block");
348 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
350 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
351 (long) pos, (long) pos/CAT_MAX);
355 p->bytes = p->buf + ISAMB_DATA_OFFSET;
357 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
360 yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
362 assert (p->size >= 0);
366 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
367 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
371 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
373 struct ISAMB_block *p;
375 p = xmalloc (sizeof(*p));
376 p->buf = xmalloc (b->file[cat].head.block_size);
378 if (!b->file[cat].head.free_list)
381 block_no = b->file[cat].head.last_block++;
382 p->pos = block_no * CAT_MAX + cat;
386 p->pos = b->file[cat].head.free_list;
387 assert((p->pos & CAT_MASK) == cat);
388 if (!get_block (b, p->pos, p->buf, 0))
390 yaz_log (b->log_io, "bf_read: new_block");
391 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
393 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
394 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
398 yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
399 cat, p->pos/CAT_MAX);
400 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
403 b->file[cat].head_dirty = 1;
404 memset (p->buf, 0, b->file[cat].head.block_size);
405 p->bytes = p->buf + ISAMB_DATA_OFFSET;
411 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
415 struct ISAMB_block *new_leaf (ISAMB b, int cat)
417 return new_block (b, 1, cat);
421 struct ISAMB_block *new_int (ISAMB b, int cat)
423 return new_block (b, 0, cat);
426 static void check_block (ISAMB b, struct ISAMB_block *p)
435 char *startp = p->bytes;
437 char *endp = p->bytes + p->size;
440 decode_ptr (&src, &pos);
441 assert ((pos&CAT_MASK) == p->cat);
445 decode_ptr (&src, &item_len);
446 assert (item_len > 0 && item_len < 30);
448 decode_ptr (&src, &pos);
449 assert ((pos&CAT_MASK) == p->cat);
454 void close_block (ISAMB b, struct ISAMB_block *p)
460 yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
461 p->pos, p->cat, p->pos/CAT_MAX);
462 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
463 b->file[p->cat].head.free_list = p->pos;
464 if (!get_block (b, p->pos, p->buf, 1))
466 yaz_log (b->log_io, "bf_write: close_block (deleted)");
467 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
472 int size = p->size + ISAMB_DATA_OFFSET;
473 assert (p->size >= 0);
475 p->buf[1] = size & 255;
476 p->buf[2] = size >> 8;
478 if (!get_block (b, p->pos, p->buf, 1))
480 yaz_log (b->log_io, "bf_write: close_block");
481 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
484 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
489 int insert_sub (ISAMB b, struct ISAMB_block **p,
490 void *new_item, int *mode,
492 struct ISAMB_block **sp,
493 void *sub_item, int *sub_size,
496 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
498 ISAMC_I *stream, struct ISAMB_block **sp,
499 void *split_item, int *split_size, void *last_max_item)
501 char *startp = p->bytes;
503 char *endp = p->bytes + p->size;
505 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
506 char sub_item[DST_ITEM_MAX];
512 assert(p->size >= 0);
513 decode_ptr (&src, &pos);
519 decode_ptr (&src, &item_len);
520 d = (*b->method->compare_item)(src, lookahead_item);
523 sub_p1 = open_block (b, pos);
525 more = insert_sub (b, &sub_p1, lookahead_item, mode,
527 sub_item, &sub_size, src);
532 decode_ptr (&src, &pos);
536 sub_p1 = open_block (b, pos);
538 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
539 sub_item, &sub_size, last_max_item);
543 /* there was a split - must insert pointer in this one */
544 char dst_buf[DST_BUF_SIZE];
547 assert (sub_size < 30 && sub_size > 1);
549 memcpy (dst, startp, src - startp);
553 encode_ptr (&dst, sub_size); /* sub length and item */
554 memcpy (dst, sub_item, sub_size);
557 encode_ptr (&dst, sub_p2->pos); /* pos */
559 if (endp - src) /* remaining data */
561 memcpy (dst, src, endp - src);
564 p->size = dst - dst_buf;
565 assert (p->size >= 0);
566 if (p->size <= b->file[p->cat].head.block_max)
568 memcpy (startp, dst_buf, dst - dst_buf);
577 half = src + b->file[p->cat].head.block_size/2;
578 decode_ptr (&src, &pos);
581 decode_ptr (&src, split_size);
583 decode_ptr (&src, &pos);
585 p_new_size = src - dst_buf;
586 memcpy (p->bytes, dst_buf, p_new_size);
588 decode_ptr (&src, split_size);
589 memcpy (split_item, src, *split_size);
592 *sp = new_int (b, p->cat);
593 (*sp)->size = endp - src;
594 memcpy ((*sp)->bytes, src, (*sp)->size);
596 p->size = p_new_size;
599 close_block (b, sub_p2);
601 close_block (b, sub_p1);
606 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
607 int *lookahead_mode, ISAMC_I *stream,
608 struct ISAMB_block **sp2,
609 void *sub_item, int *sub_size,
612 struct ISAMB_block *p = *sp1;
613 char *src = 0, *endp = 0;
614 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
616 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
617 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
619 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
620 char *cut = dst_buf + quater * 2;
621 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
624 char cut_item_buf[DST_ITEM_MAX];
625 int cut_item_size = 0;
629 char file_item_buf[DST_ITEM_MAX];
630 char *file_item = file_item_buf;
633 endp = p->bytes + p->size;
634 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
639 char *lookahead_next;
643 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
647 dst_item = lookahead_item;
648 if (!*lookahead_mode)
650 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
651 assert (*lookahead_mode);
655 dst_item = file_item_buf;
656 if (!*lookahead_mode && d == 0)
660 else if (!half1 && dst > cut)
662 char *dst_item_0 = dst_item;
663 half1 = dst; /* candidate for splitting */
665 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
667 cut_item_size = dst_item - dst_item_0;
668 memcpy (cut_item_buf, dst_item_0, cut_item_size);
673 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
683 lookahead_next = lookahead_item;
684 if (!(*stream->read_item)(stream->clientData,
691 if (lookahead_item && max_item &&
692 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
703 lookahead_next = lookahead_item;
704 if (!(*stream->read_item)(stream->clientData,
705 &lookahead_next, lookahead_mode))
712 file_item = file_item_buf;
713 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
719 file_item = file_item_buf;
720 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
724 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
725 while (lookahead_item)
727 char *dst_item = lookahead_item;
731 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
736 if (!*lookahead_mode)
738 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
741 else if (!half1 && dst > cut)
743 char *dst_item_0 = dst_item;
744 half1 = dst; /* candidate for splitting */
746 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
748 cut_item_size = dst_item - dst_item_0;
749 memcpy (cut_item_buf, dst_item_0, cut_item_size);
754 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
763 dst_item = lookahead_item;
764 if (!(*stream->read_item)(stream->clientData, &dst_item,
771 new_size = dst - dst_buf;
772 if (p && p->cat != b->no_cat-1 &&
773 new_size > b->file[p->cat].head.block_max)
775 /* non-btree block will be removed */
778 /* delete it too!! */
779 p = 0; /* make a new one anyway */
782 { /* must create a new one */
784 for (i = 0; i < b->no_cat; i++)
785 if (new_size <= b->file[i].head.block_max)
791 if (new_size > b->file[p->cat].head.block_max)
794 char *cut_item = cut_item_buf;
800 p->size = half1 - dst_buf;
801 memcpy (p->bytes, dst_buf, half1 - dst_buf);
804 *sp2 = new_leaf (b, p->cat);
806 (*b->method->code_reset)(c2);
808 first_dst = (*sp2)->bytes;
810 (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
812 memcpy (first_dst, half2, dst - half2);
814 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
817 memcpy (sub_item, cut_item_buf, cut_item_size);
818 *sub_size = cut_item_size;
822 memcpy (p->bytes, dst_buf, dst - dst_buf);
825 (*b->method->code_stop)(ISAMC_DECODE, c1);
826 (*b->method->code_stop)(ISAMC_ENCODE, c2);
831 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
834 struct ISAMB_block **sp,
835 void *sub_item, int *sub_size,
838 if (!*p || (*p)->leaf)
839 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
842 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
846 int isamb_unlink (ISAMB b, ISAMC_P pos)
848 struct ISAMB_block *p1;
852 p1 = open_block(b, pos);
858 char *src = p1->bytes + p1->offset;
860 decode_ptr(&src, &sub_p);
861 isamb_unlink(b, sub_p);
863 while (src != p1->bytes + p1->size)
865 decode_ptr(&src, &item_len);
867 decode_ptr(&src, &sub_p);
868 isamb_unlink(b, sub_p);
875 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
877 char item_buf[DST_ITEM_MAX];
889 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
894 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
897 struct ISAMB_block *p = 0, *sp = 0;
898 char sub_item[DST_ITEM_MAX];
902 p = open_block (b, pos);
903 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
904 sub_item, &sub_size, 0);
906 { /* increase level of tree by one */
907 struct ISAMB_block *p2 = new_int (b, p->cat);
908 char *dst = p2->bytes + p2->size;
910 encode_ptr (&dst, p->pos);
911 assert (sub_size < 20);
912 encode_ptr (&dst, sub_size);
913 memcpy (dst, sub_item, sub_size);
915 encode_ptr (&dst, sp->pos);
917 p2->size = dst - p2->bytes;
918 pos = p2->pos; /* return new super page */
923 pos = p->pos; /* return current one (again) */
929 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
931 ISAMB_PP pp = xmalloc (sizeof(*pp));
935 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
942 pp->skipped_numbers=0;
943 pp->returned_numbers=0;
944 for (i=0;i<ISAMB_MAX_LEVEL;i++)
945 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
948 struct ISAMB_block *p = open_block (isamb, pos);
949 char *src = p->bytes + p->offset;
950 pp->block[pp->level] = p;
952 pp->total_size += p->size;
958 decode_ptr (&src, &pos);
959 p->offset = src - p->bytes;
961 pp->accessed_nodes[pp->level]++;
963 pp->block[pp->level+1] = 0;
964 pp->maxlevel=pp->level;
970 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
972 return isamb_pp_open_x (isamb, pos, 0);
975 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
980 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
981 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
982 for (i=pp->maxlevel;i>=0;i--)
983 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
984 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: %d read, %d skipped", i,
985 pp->accessed_nodes[i], pp->skipped_nodes[i]);
986 pp->isamb->skipped_numbers += pp->skipped_numbers;
987 pp->isamb->returned_numbers += pp->returned_numbers;
988 for (i=pp->maxlevel;i>=0;i--)
990 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
991 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
994 *size = pp->total_size;
996 *blocks = pp->no_blocks;
997 for (i = 0; i <= pp->level; i++)
998 close_block (pp->isamb, pp->block[i]);
1003 int isamb_block_info (ISAMB isamb, int cat)
1005 if (cat >= 0 && cat < isamb->no_cat)
1006 return isamb->file[cat].head.block_size;
1010 void isamb_pp_close (ISAMB_PP pp)
1012 isamb_pp_close_x (pp, 0, 0);
1015 /* simple recursive dumper .. */
1016 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1020 char prefix_str[1024];
1023 struct ISAMB_block *p = open_block (b, pos);
1024 sprintf(prefix_str, "%*s %d cat=%d size=%d max=%d", level*2, "",
1025 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1027 sprintf(prefix_str, "%*s %d", level*2, "", pos);
1030 while (p->offset < p->size)
1032 char *src = p->bytes + p->offset;
1034 (*b->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1036 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1037 p->offset = src - (char*) p->bytes;
1039 assert(p->offset == p->size);
1043 char *src = p->bytes + p->offset;
1047 decode_ptr (&src, &sub);
1048 p->offset = src - (char*) p->bytes;
1050 isamb_dump_r(b, sub, pr, level+1);
1052 while (p->offset < p->size)
1054 decode_ptr (&src, &item_len);
1055 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1057 decode_ptr (&src, &sub);
1059 p->offset = src - (char*) p->bytes;
1061 isamb_dump_r(b, sub, pr, level+1);
1068 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1070 return isamb_dump_r(b, pos, pr, 0);
1074 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1075 debug the more complex pp_read that also forwards. May be deleted near end
1076 of 2004, if it has not shown to be useful */
1079 int isamb_pp_read (ISAMB_PP pp, void *buf)
1083 struct ISAMB_block *p = pp->block[pp->level];
1087 while (p->offset == p->size)
1090 while (p->offset == p->size)
1094 close_block (pp->isamb, pp->block[pp->level]);
1095 pp->block[pp->level] = 0;
1097 p = pp->block[pp->level];
1100 src = p->bytes + p->offset;
1102 decode_ptr (&src, &item_len);
1104 decode_ptr (&src, &pos);
1106 p->offset = src - (char*) p->bytes;
1112 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1114 pp->total_size += p->size;
1121 src = p->bytes + p->offset;
1122 decode_ptr (&src, &pos);
1123 p->offset = src - (char*) p->bytes;
1127 assert (p->offset < p->size);
1129 src = p->bytes + p->offset;
1130 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1132 p->offset = src - (char*) p->bytes;
1133 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1138 int isamb_pp_read (ISAMB_PP pp, void *buf)
1140 return isamb_pp_forward(pp,buf,0);
1144 #define NEW_FORWARD 1
1146 #if NEW_FORWARD == 1
1150 #define ISAMB_DEBUG 1
1152 static int isamb_pp_on_right_node(ISAMB_PP pp, int level, const void *untilbuf)
1153 { /* looks one node higher to see if we should be on this node at all */
1154 /* useful in backing off quickly, and in avoiding tail descends */
1155 /* call with pp->level to begin with */
1156 struct ISAMB_block *p;
1163 logf(LOG_DEBUG,"isamb_pp_on_right returning true for root");
1165 return 1; /* we can never skip the root node */
1169 assert(p->offset <= p->size);
1170 if (p->offset < p->size )
1172 assert(p->offset>0);
1173 src=p->bytes + p->offset;
1174 decode_ptr(&src,&item_len);
1176 (*pp->isamb->method->log_item)(LOG_DEBUG,untilbuf,"on_leaf: until");
1177 (*pp->isamb->method->log_item)(LOG_DEBUG,src,"on_leaf: value");
1179 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1182 logf(LOG_DEBUG,"isamb_pp_on_right returning true "
1183 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1189 logf(LOG_DEBUG,"isamb_pp_on_right returning false "
1190 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1197 logf(LOG_DEBUG,"isamb_pp_on_right at tail, looking higher "
1200 return isamb_pp_on_right_node(pp, level, untilbuf);
1202 } /* isamb_pp_on_right_node */
1204 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1205 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1206 struct ISAMB_block *p = pp->block[pp->level];
1211 if (p->offset == p->size) {
1213 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1215 return 0; /* at end of leaf */
1217 src=p->bytes + p->offset;
1219 (*pp->isamb->method->code_item)
1220 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1221 p->offset = src - (char*) p->bytes;
1224 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1228 } /* read_on_leaf */
1230 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1231 { /* forwards on the current leaf, returns 0 if not found */
1235 if (!isamb_pp_read_on_leaf(pp,buf))
1237 /* FIXME - this is an extra function call, inline the read? */
1238 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1239 if (cmp <2){ /* found a good one */
1242 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1244 pp->returned_numbers++;
1248 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1249 return 0; /* never mind the rest of this leaf */
1250 pp->skipped_numbers++;
1253 } /* forward_on_leaf */
1255 static int isamb_pp_climb_level(ISAMB_PP pp, int *pos)
1256 { /* climbs higher in the tree, until finds a level with data left */
1257 /* returns the node to (consider to) descend to in *pos) */
1258 struct ISAMB_block *p = pp->block[pp->level];
1262 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1263 "at level %d node %d ofs=%d sz=%d",
1264 pp->level, p->pos, p->offset, p->size);
1266 assert(pp->level >= 0);
1267 assert(p->offset <= p->size);
1271 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1275 assert(pp->level>0);
1276 close_block(pp->isamb, pp->block[pp->level]);
1277 pp->block[pp->level]=0;
1279 p=pp->block[pp->level];
1281 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1282 pp->level, p->pos, p->offset);
1285 assert(p->offset <= p->size);
1286 if (p->offset == p->size ) {
1287 /* we came from the last pointer, climb on */
1288 if (!isamb_pp_climb_level(pp,pos))
1290 p=pp->block[pp->level];
1294 /* skip the child we just came from */
1296 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1297 pp->level, p->offset, p->size);
1299 assert (p->offset < p->size );
1300 src=p->bytes + p->offset;
1301 decode_ptr(&src, &item_len);
1303 decode_ptr(&src, pos);
1304 p->offset=src - (char *)p->bytes;
1311 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1312 { /* scans a upper node until it finds a child <= untilbuf */
1313 /* pp points to the key value, as always. pos is the child read from */
1315 /* if all values are too small, returns the last child in the node */
1316 /* FIXME - this can be detected, and avoided by looking at the */
1317 /* parent node, but that gets messy. Presumably the cost is */
1318 /* pretty low anyway */
1319 struct ISAMB_block *p = pp->block[pp->level];
1320 char *src=p->bytes + p->offset;
1326 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1327 "at level %d node %d ofs=%di sz=%d",
1328 pp->level, p->pos, p->offset, p->size);
1331 assert(p->offset <= p->size);
1332 if (p->offset == p->size) {
1334 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1335 "at level %d node %d ofs=%di sz=%d",
1336 pp->level, p->pos, p->offset, p->size);
1338 return pos; /* already at the end of it */
1340 while(p->offset < p->size) {
1341 decode_ptr(&src,&item_len);
1342 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1344 decode_ptr(&src,&nxtpos);
1348 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1349 "at level %d node %d ofs=%d sz=%d",
1350 pp->level, p->pos, p->offset, p->size);
1355 p->offset=src-(char*)p->bytes;
1356 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1362 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1363 "at level %d node %d ofs=%d sz=%d skips=%d",
1364 pp->level, p->pos, p->offset, p->size, skips);
1366 return pos; /* that's the last one in the line */
1368 } /* forward_unode */
1370 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1371 { /* climbs down the tree, from pos, to the leftmost leaf */
1372 struct ISAMB_block *p = pp->block[pp->level];
1376 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1377 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1378 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1381 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1384 p=open_block(pp->isamb, pos);
1385 pp->block[pp->level]=p;
1386 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1389 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1390 "got lev %d node %d lf=%d",
1391 pp->level, p->pos, p->leaf);
1395 assert (p->offset==0 );
1396 src=p->bytes + p->offset;
1397 decode_ptr(&src, &pos);
1398 p->offset=src-(char*)p->bytes;
1399 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1401 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1402 "returning at lev %d node %d ofs=%d lf=%d",
1403 pp->level, p->pos, p->offset, p->leaf);
1405 } /* descend_to_leaf */
1407 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1408 { /* finds the next leaf by climbing up and down */
1410 if (!isamb_pp_climb_level(pp,&pos))
1412 isamb_pp_descend_to_leaf(pp, pos,0);
1416 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1417 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1420 struct ISAMB_block *p = pp->block[pp->level];
1421 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1422 "at level %d node %d ofs=%d sz=%d",
1423 pp->level, p->pos, p->offset, p->size);
1425 if (!isamb_pp_climb_level(pp,&pos))
1427 /* see if it would pay to climb one higher */
1428 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1429 if (!isamb_pp_climb_level(pp,&pos))
1431 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1433 p = pp->block[pp->level];
1434 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1435 "at level %d node %d ofs=%d sz=%d",
1436 pp->level, p->pos, p->offset, p->size);
1441 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1444 struct ISAMB_block *p = pp->block[pp->level];
1446 logf(LOG_DEBUG,"isamb_pp_forward starting "
1447 "at level %d node %d ofs=%d sz=%d u=%p",
1448 pp->level, p->pos, p->offset, p->size,untilbuf);
1451 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1453 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1454 "at level %d node %d ofs=%d sz=%d",
1455 pp->level, p->pos, p->offset, p->size);
1459 if (! isamb_pp_climb_desc( pp, buf, untilbuf)) {
1461 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1462 "at level %d node %d ofs=%d sz=%d",
1463 pp->level, p->pos, p->offset, p->size);
1465 return 0; /* could not find a leaf */
1468 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1470 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1471 "at level %d node %d ofs=%d sz=%d",
1472 pp->level, p->pos, p->offset, p->size);
1476 }while ( isamb_pp_find_next_leaf(pp));
1477 return 0; /* could not find at all */
1479 else { /* no untilbuf, a straight read */
1480 /* FIXME - this should be moved
1481 * directly into the pp_read */
1482 /* keeping here now, to keep same
1483 * interface as the old fwd */
1484 if (isamb_pp_read_on_leaf( pp, buf)) {
1486 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1487 "at level %d node %d ofs=%d sz=%d",
1488 pp->level, p->pos, p->offset, p->size);
1492 if (isamb_pp_find_next_leaf(pp)) {
1494 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1495 "at level %d node %d ofs=%d sz=%d",
1496 pp->level, p->pos, p->offset, p->size);
1498 return isamb_pp_read_on_leaf(pp, buf);
1503 } /* isam_pp_forward (new version) */
1505 #elif NEW_FORWARD == 0
1507 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1511 * while at end of node
1512 * climb higher. If out, return 0
1513 * while not on a leaf (and not at its end)
1522 * The upper nodes consist of a sequence of nodenumbers and keys
1523 * When opening a block, the first node number is read in, and
1524 * offset points to the first key, which is the upper limit of keys
1525 * in the node just read.
1529 struct ISAMB_block *p = pp->block[pp->level];
1534 int descending=0; /* used to prevent a border condition error */
1538 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1540 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1541 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1546 while ( (p->offset == p->size) && !descending )
1547 { /* end of this block - climb higher */
1548 assert (p->offset <= p->size);
1550 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1556 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1558 return 0; /* at end of the root, nothing left */
1560 close_block(pp->isamb, pp->block[pp->level]);
1561 pp->block[pp->level]=0;
1563 p=pp->block[pp->level];
1565 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1569 assert(p->offset <= p->size);
1570 /* skip the child we have handled */
1571 if (p->offset != p->size)
1573 src = p->bytes + p->offset;
1574 decode_ptr(&src, &item_len);
1576 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1577 " isamb_pp_forward "
1578 "climb skipping old key");
1581 decode_ptr(&src,&pos);
1582 p->offset = src - (char*) p->bytes;
1583 break; /* even if this puts us at the end of the block, we
1584 need to descend to the last pos. UGLY coding,
1585 clean up some day */
1590 src = p->bytes + p->offset;
1591 if (p->offset == p->size)
1592 cmp=-2 ; /* descend to the last node, as we have
1596 decode_ptr(&src, &item_len);
1598 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1599 "ofs=%d sz=%d nxtpos=%d ",
1600 p->offset,p->size,pos);
1601 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1604 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1608 decode_ptr(&src,&nxtpos);
1613 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1616 descending=1; /* prevent climbing for a while */
1618 p = open_block(pp->isamb,pos);
1619 pp->block[pp->level] = p ;
1620 pp->total_size += p->size;
1621 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1624 { /* block starts with a pos */
1625 src = p->bytes + p->offset;
1626 decode_ptr(&src,&pos);
1627 p->offset=src-(char*) p->bytes;
1629 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1633 } /* descend to the node */
1635 { /* skip the node */
1636 p->offset = src - (char*) p->bytes;
1638 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1641 "isamb_pp_forward: skipping block on level %d, noting "
1643 pp->level, pp->maxlevel - pp->level-1 ,
1644 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1646 /* 0 is always leafs, 1 is one level above leafs etc, no
1647 * matter how high tree */
1649 } /* not on a leaf */
1652 if (p->offset == p->size) {
1657 assert (p->offset < p->size);
1658 src = p->bytes + p->offset;
1660 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1662 p->offset = src - (char*) p->bytes;
1664 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1668 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1670 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1677 (*pp->isamb->method->log_item)(
1678 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1682 (*pp->isamb->method->log_item)(
1683 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1686 pp->returned_numbers++;
1690 pp->skipped_numbers++;
1696 #elif NEW_FORWARD == 2
1698 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1702 struct ISAMB_block *p = pp->block[pp->level];
1707 while (p->offset == p->size)
1710 while (p->offset == p->size)
1714 close_block (pp->isamb, pp->block[pp->level]);
1715 pp->block[pp->level] = 0;
1717 p = pp->block[pp->level];
1722 src = p->bytes + p->offset;
1724 decode_ptr (&src, &item_len);
1726 decode_ptr (&src, &pos);
1728 p->offset = src - (char*) p->bytes;
1730 src = p->bytes + p->offset;
1734 if (!untilb || p->offset == p->size)
1736 assert(p->offset < p->size);
1737 decode_ptr (&src, &item_len);
1738 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1741 decode_ptr (&src, &pos);
1742 p->offset = src - (char*) p->bytes;
1749 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1751 pp->total_size += p->size;
1759 src = p->bytes + p->offset;
1762 decode_ptr (&src, &pos);
1763 p->offset = src - (char*) p->bytes;
1765 if (!untilb || p->offset == p->size)
1767 assert(p->offset < p->size);
1768 decode_ptr (&src, &item_len);
1769 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1776 assert (p->offset < p->size);
1781 src = p->bytes + p->offset;
1782 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1784 p->offset = src - (char*) p->bytes;
1785 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1788 if (p->offset == p->size) goto again;
1790 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1796 int isamb_pp_num (ISAMB_PP pp)
1801 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1802 int *current, int *total, void *dummybuf )
1804 struct ISAMB_block *p = pp->block[pp->level];
1806 char *end=p->bytes+p->size;
1807 char *cur=p->bytes+p->offset;
1809 assert(p->offset <= p->size);
1817 (*pp->isamb->method->code_item)
1818 (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1819 assert(dst<(char*) dummybuf+100); /*FIXME */
1824 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur=%d tot=%d ofs=%d sz=%d lev=%d",
1825 *current, *total, p->offset, p->size, pp->level);
1829 static void isamb_pp_upper_pos( ISAMB_PP pp, int *current, int *total,
1830 int size, int level )
1831 { /* estimates total/current occurrences from here up, excl leaf */
1832 struct ISAMB_block *p = pp->block[level];
1834 char *end=p->bytes+p->size;
1835 char *cur=p->bytes+p->offset;
1840 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1841 "cur=%d tot=%d ofs=%d sz=%d pos=%d",
1842 level, *current, *total, p->offset, p->size, p->pos);
1843 assert (p->offset <= p->size);
1844 decode_ptr (&src, &child ); /* first child */
1851 decode_ptr (&src, &item_size );
1852 assert(src+item_size<=end);
1854 decode_ptr (&src, &child );
1857 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1860 void isamb_pp_pos( ISAMB_PP pp, int *current, int *total )
1861 { /* return an estimate of the current position and of the total number of */
1862 /* occureences in the isam tree, based on the current leaf */
1863 struct ISAMB_block *p = pp->block[pp->level];
1864 char dummy[100]; /* 100 bytes/entry must be enough */
1868 isamb_pp_leaf_pos(pp,current, total, dummy);
1870 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1872 logf(LOG_DEBUG,"isamb_pp_pos: C=%d T=%d =%6.2f%%",
1873 *current, *total, 100.0*(*current)/(*total));