1 /* $Id: isamb.c,v 1.48 2004-08-04 08:35:24 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;
122 static void encode_ptr (char **dst, zint pos)
124 unsigned char *bp = (unsigned char*) *dst;
128 *bp++ = 128 | (pos & 127);
135 static void encode_ptr (char **dst, zint pos)
137 memcpy(*dst, &pos, sizeof(pos));
138 (*dst) += sizeof(pos);
143 static void decode_ptr (const char **src1, zint *pos)
145 const unsigned char **src = (const unsigned char **) src1;
150 while (((c = *(*src)++) & 128))
152 d += ((zint) (c & 127) << r);
155 d += ((zint) c << r);
159 static void decode_ptr (const char **src, zint *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=" ZINT_FORMAT "\n",
361 assert (p->size >= 0);
365 p->decodeClientData = (*b->method->codec.start)();
366 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block " ZINT_FORMAT " ofs=%d",pos, p->offset);
370 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
372 struct ISAMB_block *p;
374 p = xmalloc (sizeof(*p));
375 p->buf = xmalloc (b->file[cat].head.block_size);
377 if (!b->file[cat].head.free_list)
380 block_no = b->file[cat].head.last_block++;
381 p->pos = block_no * CAT_MAX + cat;
385 p->pos = b->file[cat].head.free_list;
386 assert((p->pos & CAT_MASK) == cat);
387 if (!get_block (b, p->pos, p->buf, 0))
389 yaz_log (b->log_io, "bf_read: new_block");
390 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
392 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
393 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
397 yaz_log (b->log_freelist, "got block " ZINT_FORMAT " from freelist %d:" ZINT_FORMAT, p->pos,
398 cat, p->pos/CAT_MAX);
399 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
402 b->file[cat].head_dirty = 1;
403 memset (p->buf, 0, b->file[cat].head.block_size);
404 p->bytes = p->buf + ISAMB_DATA_OFFSET;
410 p->decodeClientData = (*b->method->codec.start)();
414 struct ISAMB_block *new_leaf (ISAMB b, int cat)
416 return new_block (b, 1, cat);
420 struct ISAMB_block *new_int (ISAMB b, int cat)
422 return new_block (b, 0, cat);
425 static void check_block (ISAMB b, struct ISAMB_block *p)
434 char *startp = p->bytes;
435 const char *src = startp;
436 char *endp = p->bytes + p->size;
439 decode_ptr (&src, &pos);
440 assert ((pos&CAT_MASK) == p->cat);
444 decode_ptr (&src, &item_len);
445 assert (item_len > 0 && item_len < 80);
447 decode_ptr (&src, &pos);
448 assert ((pos&CAT_MASK) == p->cat);
453 void close_block (ISAMB b, struct ISAMB_block *p)
459 yaz_log (b->log_freelist, "release block " ZINT_FORMAT " from freelist %d:" ZINT_FORMAT,
460 p->pos, p->cat, p->pos/CAT_MAX);
461 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
462 b->file[p->cat].head.free_list = p->pos;
463 if (!get_block (b, p->pos, p->buf, 1))
465 yaz_log (b->log_io, "bf_write: close_block (deleted)");
466 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
471 int size = p->size + ISAMB_DATA_OFFSET;
472 assert (p->size >= 0);
474 p->buf[1] = size & 255;
475 p->buf[2] = size >> 8;
477 if (!get_block (b, p->pos, p->buf, 1))
479 yaz_log (b->log_io, "bf_write: close_block");
480 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
483 (*b->method->codec.stop)(p->decodeClientData);
488 int insert_sub (ISAMB b, struct ISAMB_block **p,
489 void *new_item, int *mode,
491 struct ISAMB_block **sp,
492 void *sub_item, int *sub_size,
493 const void *max_item);
495 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
497 ISAMC_I *stream, struct ISAMB_block **sp,
498 void *split_item, int *split_size, const void *last_max_item)
500 char *startp = p->bytes;
501 const char *src = startp;
502 char *endp = p->bytes + p->size;
504 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
505 char sub_item[DST_ITEM_MAX];
511 assert(p->size >= 0);
512 decode_ptr (&src, &pos);
517 const char *src0 = src;
518 decode_ptr (&src, &item_len);
519 d = (*b->method->compare_item)(src, lookahead_item);
522 sub_p1 = open_block (b, pos);
524 more = insert_sub (b, &sub_p1, lookahead_item, mode,
526 sub_item, &sub_size, src);
531 decode_ptr (&src, &pos);
535 sub_p1 = open_block (b, pos);
537 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
538 sub_item, &sub_size, last_max_item);
542 /* there was a split - must insert pointer in this one */
543 char dst_buf[DST_BUF_SIZE];
546 assert (sub_size < 80 && sub_size > 1);
548 memcpy (dst, startp, src - startp);
552 encode_ptr (&dst, sub_size); /* sub length and item */
553 memcpy (dst, sub_item, sub_size);
556 encode_ptr (&dst, sub_p2->pos); /* pos */
558 if (endp - src) /* remaining data */
560 memcpy (dst, src, endp - src);
563 p->size = dst - dst_buf;
564 assert (p->size >= 0);
565 if (p->size <= b->file[p->cat].head.block_max)
567 memcpy (startp, dst_buf, dst - dst_buf);
577 half = src + b->file[p->cat].head.block_size/2;
578 decode_ptr (&src, &pos);
581 decode_ptr (&src, &split_size_tmp);
582 *split_size = split_size_tmp;
585 decode_ptr (&src, &pos);
587 p_new_size = src - dst_buf;
588 memcpy (p->bytes, dst_buf, p_new_size);
590 decode_ptr (&src, &split_size_tmp);
591 *split_size = split_size_tmp;
592 memcpy (split_item, src, *split_size);
595 *sp = new_int (b, p->cat);
596 (*sp)->size = endp - src;
597 memcpy ((*sp)->bytes, src, (*sp)->size);
599 p->size = p_new_size;
602 close_block (b, sub_p2);
604 close_block (b, sub_p1);
608 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
609 int *lookahead_mode, ISAMC_I *stream,
610 struct ISAMB_block **sp2,
611 void *sub_item, int *sub_size,
612 const void *max_item)
614 struct ISAMB_block *p = *sp1;
617 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
619 void *c1 = (*b->method->codec.start)();
620 void *c2 = (*b->method->codec.start)();
622 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
623 char *cut = dst_buf + quater * 2;
624 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
627 char cut_item_buf[DST_ITEM_MAX];
628 int cut_item_size = 0;
632 char file_item_buf[DST_ITEM_MAX];
633 char *file_item = file_item_buf;
636 endp = p->bytes + p->size;
637 (*b->method->codec.decode)(c1, &file_item, &src);
640 const char *dst_item = 0;
642 char *lookahead_next;
646 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
650 dst_item = lookahead_item;
651 if (!*lookahead_mode)
653 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
654 assert (*lookahead_mode);
658 dst_item = file_item_buf;
659 if (!*lookahead_mode && d == 0)
663 else if (!half1 && dst > cut)
665 const char *dst_item_0 = dst_item;
666 half1 = dst; /* candidate for splitting */
668 (*b->method->codec.encode)(c2, &dst, &dst_item);
670 cut_item_size = dst_item - dst_item_0;
671 assert(cut_item_size > 0);
672 memcpy (cut_item_buf, dst_item_0, cut_item_size);
677 (*b->method->codec.encode)(c2, &dst, &dst_item);
687 lookahead_next = lookahead_item;
688 if (!(*stream->read_item)(stream->clientData,
695 if (lookahead_item && max_item &&
696 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
707 lookahead_next = lookahead_item;
708 if (!(*stream->read_item)(stream->clientData,
709 &lookahead_next, lookahead_mode))
716 file_item = file_item_buf;
717 (*b->method->codec.decode)(c1, &file_item, &src);
723 file_item = file_item_buf;
724 (*b->method->codec.decode)(c1, &file_item, &src);
728 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
729 while (lookahead_item)
732 const char *src = lookahead_item;
736 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
741 if (!*lookahead_mode)
743 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
746 else if (!half1 && dst > cut)
748 const char *src_0 = src;
749 half1 = dst; /* candidate for splitting */
751 (*b->method->codec.encode)(c2, &dst, &src);
753 cut_item_size = src - src_0;
754 assert(cut_item_size > 0);
755 memcpy (cut_item_buf, src_0, cut_item_size);
760 (*b->method->codec.encode)(c2, &dst, &src);
769 dst_item = lookahead_item;
770 if (!(*stream->read_item)(stream->clientData, &dst_item,
777 new_size = dst - dst_buf;
778 if (p && p->cat != b->no_cat-1 &&
779 new_size > b->file[p->cat].head.block_max)
781 /* non-btree block will be removed */
784 /* delete it too!! */
785 p = 0; /* make a new one anyway */
788 { /* must create a new one */
790 for (i = 0; i < b->no_cat; i++)
791 if (new_size <= b->file[i].head.block_max)
797 if (new_size > b->file[p->cat].head.block_max)
800 const char *cut_item = cut_item_buf;
805 assert(cut_item_size > 0);
808 p->size = half1 - dst_buf;
809 memcpy (p->bytes, dst_buf, half1 - dst_buf);
812 *sp2 = new_leaf (b, p->cat);
814 (*b->method->codec.reset)(c2);
816 first_dst = (*sp2)->bytes;
818 (*b->method->codec.encode)(c2, &first_dst, &cut_item);
820 memcpy (first_dst, half2, dst - half2);
822 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
825 memcpy (sub_item, cut_item_buf, cut_item_size);
826 *sub_size = cut_item_size;
830 memcpy (p->bytes, dst_buf, dst - dst_buf);
833 (*b->method->codec.stop)(c1);
834 (*b->method->codec.stop)(c2);
839 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
842 struct ISAMB_block **sp,
843 void *sub_item, int *sub_size,
844 const void *max_item)
846 if (!*p || (*p)->leaf)
847 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
850 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
854 int isamb_unlink (ISAMB b, ISAMC_P pos)
856 struct ISAMB_block *p1;
860 p1 = open_block(b, pos);
866 const char *src = p1->bytes + p1->offset;
868 decode_ptr(&src, &sub_p);
869 isamb_unlink(b, sub_p);
871 while (src != p1->bytes + p1->size)
873 decode_ptr(&src, &item_len);
875 decode_ptr(&src, &sub_p);
876 isamb_unlink(b, sub_p);
883 ISAMB_P isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
885 char item_buf[DST_ITEM_MAX];
897 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
902 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
905 struct ISAMB_block *p = 0, *sp = 0;
906 char sub_item[DST_ITEM_MAX];
910 p = open_block (b, pos);
911 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
912 sub_item, &sub_size, 0);
914 { /* increase level of tree by one */
915 struct ISAMB_block *p2 = new_int (b, p->cat);
916 char *dst = p2->bytes + p2->size;
918 encode_ptr (&dst, p->pos);
919 assert (sub_size < 40);
920 encode_ptr (&dst, sub_size);
921 memcpy (dst, sub_item, sub_size);
923 encode_ptr (&dst, sp->pos);
925 p2->size = dst - p2->bytes;
926 pos = p2->pos; /* return new super page */
931 pos = p->pos; /* return current one (again) */
937 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
939 ISAMB_PP pp = xmalloc (sizeof(*pp));
943 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
950 pp->skipped_numbers=0;
951 pp->returned_numbers=0;
952 for (i=0;i<ISAMB_MAX_LEVEL;i++)
953 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
956 struct ISAMB_block *p = open_block (isamb, pos);
957 const char *src = p->bytes + p->offset;
958 pp->block[pp->level] = p;
960 pp->total_size += p->size;
966 decode_ptr (&src, &pos);
967 p->offset = src - p->bytes;
969 pp->accessed_nodes[pp->level]++;
971 pp->block[pp->level+1] = 0;
972 pp->maxlevel=pp->level;
978 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
980 return isamb_pp_open_x (isamb, pos, 0);
983 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
988 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
989 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
990 for (i=pp->maxlevel;i>=0;i--)
991 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
992 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: %d read, %d skipped", i,
993 pp->accessed_nodes[i], pp->skipped_nodes[i]);
994 pp->isamb->skipped_numbers += pp->skipped_numbers;
995 pp->isamb->returned_numbers += pp->returned_numbers;
996 for (i=pp->maxlevel;i>=0;i--)
998 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
999 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
1002 *size = pp->total_size;
1004 *blocks = pp->no_blocks;
1005 for (i = 0; i <= pp->level; i++)
1006 close_block (pp->isamb, pp->block[i]);
1011 int isamb_block_info (ISAMB isamb, int cat)
1013 if (cat >= 0 && cat < isamb->no_cat)
1014 return isamb->file[cat].head.block_size;
1018 void isamb_pp_close (ISAMB_PP pp)
1020 isamb_pp_close_x (pp, 0, 0);
1023 /* simple recursive dumper .. */
1024 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1028 char prefix_str[1024];
1031 struct ISAMB_block *p = open_block (b, pos);
1032 sprintf(prefix_str, "%*s " ZINT_FORMAT " cat=%d size=%d max=%d", level*2, "",
1033 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1035 sprintf(prefix_str, "%*s " ZINT_FORMAT, level*2, "", pos);
1038 while (p->offset < p->size)
1040 const char *src = p->bytes + p->offset;
1042 (*b->method->codec.decode)(p->decodeClientData, &dst, &src);
1043 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1044 p->offset = src - (char*) p->bytes;
1046 assert(p->offset == p->size);
1050 const char *src = p->bytes + p->offset;
1054 decode_ptr (&src, &sub);
1055 p->offset = src - (char*) p->bytes;
1057 isamb_dump_r(b, sub, pr, level+1);
1059 while (p->offset < p->size)
1061 decode_ptr (&src, &item_len);
1062 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1064 decode_ptr (&src, &sub);
1066 p->offset = src - (char*) p->bytes;
1068 isamb_dump_r(b, sub, pr, level+1);
1075 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1077 return isamb_dump_r(b, pos, pr, 0);
1081 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1082 debug the more complex pp_read that also forwards. May be deleted near end
1083 of 2004, if it has not shown to be useful */
1086 int isamb_pp_read (ISAMB_PP pp, void *buf)
1090 struct ISAMB_block *p = pp->block[pp->level];
1094 while (p->offset == p->size)
1097 while (p->offset == p->size)
1101 close_block (pp->isamb, pp->block[pp->level]);
1102 pp->block[pp->level] = 0;
1104 p = pp->block[pp->level];
1107 src = p->bytes + p->offset;
1109 decode_ptr (&src, &item_len);
1111 decode_ptr (&src, &pos);
1113 p->offset = src - (char*) p->bytes;
1119 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1121 pp->total_size += p->size;
1128 src = p->bytes + p->offset;
1129 decode_ptr (&src, &pos);
1130 p->offset = src - (char*) p->bytes;
1134 assert (p->offset < p->size);
1136 src = p->bytes + p->offset;
1137 (*pp->isamb->method->codec.code_item)(ISAMC_DECODE, p->decodeClientData,
1139 p->offset = src - (char*) p->bytes;
1140 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1145 int isamb_pp_read (ISAMB_PP pp, void *buf)
1147 return isamb_pp_forward(pp, buf, 0);
1151 #define NEW_FORWARD 1
1153 #if NEW_FORWARD == 1
1157 #define ISAMB_DEBUG 1
1159 static int isamb_pp_on_right_node(ISAMB_PP pp, int level, const void *untilbuf)
1160 { /* looks one node higher to see if we should be on this node at all */
1161 /* useful in backing off quickly, and in avoiding tail descends */
1162 /* call with pp->level to begin with */
1163 struct ISAMB_block *p;
1170 logf(LOG_DEBUG,"isamb_pp_on_right returning true for root");
1172 return 1; /* we can never skip the root node */
1176 assert(p->offset <= p->size);
1177 if (p->offset < p->size )
1179 assert(p->offset>0);
1180 src=p->bytes + p->offset;
1181 decode_ptr(&src, &item_len);
1183 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,untilbuf,"on_leaf: until");
1184 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,src,"on_leaf: value");
1186 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1189 logf(LOG_DEBUG,"isamb_pp_on_right returning true "
1190 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1196 logf(LOG_DEBUG,"isamb_pp_on_right returning false "
1197 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1204 logf(LOG_DEBUG,"isamb_pp_on_right at tail, looking higher "
1207 return isamb_pp_on_right_node(pp, level, untilbuf);
1209 } /* isamb_pp_on_right_node */
1211 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1212 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1213 struct ISAMB_block *p = pp->block[pp->level];
1218 if (p->offset == p->size) {
1220 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1222 return 0; /* at end of leaf */
1224 src=p->bytes + p->offset;
1226 (*pp->isamb->method->codec.decode)(p->decodeClientData,&dst, &src);
1227 p->offset = src - (char*) p->bytes;
1230 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1234 } /* read_on_leaf */
1236 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1237 { /* forwards on the current leaf, returns 0 if not found */
1241 if (!isamb_pp_read_on_leaf(pp,buf))
1243 /* FIXME - this is an extra function call, inline the read? */
1244 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1245 if (cmp <2){ /* found a good one */
1248 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1250 pp->returned_numbers++;
1254 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1255 return 0; /* never mind the rest of this leaf */
1256 pp->skipped_numbers++;
1259 } /* forward_on_leaf */
1261 static int isamb_pp_climb_level(ISAMB_PP pp, ISAMB_P *pos)
1262 { /* climbs higher in the tree, until finds a level with data left */
1263 /* returns the node to (consider to) descend to in *pos) */
1264 struct ISAMB_block *p = pp->block[pp->level];
1268 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1269 "at level %d node %d ofs=%d sz=%d",
1270 pp->level, p->pos, p->offset, p->size);
1272 assert(pp->level >= 0);
1273 assert(p->offset <= p->size);
1277 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1281 assert(pp->level>0);
1282 close_block(pp->isamb, pp->block[pp->level]);
1283 pp->block[pp->level]=0;
1285 p=pp->block[pp->level];
1287 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1288 pp->level, p->pos, p->offset);
1291 assert(p->offset <= p->size);
1292 if (p->offset == p->size ) {
1293 /* we came from the last pointer, climb on */
1294 if (!isamb_pp_climb_level(pp,pos))
1296 p=pp->block[pp->level];
1300 /* skip the child we just came from */
1302 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1303 pp->level, p->offset, p->size);
1305 assert (p->offset < p->size );
1306 src=p->bytes + p->offset;
1307 decode_ptr(&src, &item_len);
1309 decode_ptr(&src, pos);
1310 p->offset=src - (char *)p->bytes;
1317 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1318 { /* scans a upper node until it finds a child <= untilbuf */
1319 /* pp points to the key value, as always. pos is the child read from */
1321 /* if all values are too small, returns the last child in the node */
1322 /* FIXME - this can be detected, and avoided by looking at the */
1323 /* parent node, but that gets messy. Presumably the cost is */
1324 /* pretty low anyway */
1325 struct ISAMB_block *p = pp->block[pp->level];
1326 const char *src=p->bytes + p->offset;
1332 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1333 "at level %d node %d ofs=%di sz=%d",
1334 pp->level, p->pos, p->offset, p->size);
1337 assert(p->offset <= p->size);
1338 if (p->offset == p->size) {
1340 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1341 "at level %d node %d ofs=%di sz=%d",
1342 pp->level, p->pos, p->offset, p->size);
1344 return pos; /* already at the end of it */
1346 while(p->offset < p->size) {
1347 decode_ptr(&src,&item_len);
1348 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1350 decode_ptr(&src,&nxtpos);
1354 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1355 "at level %d node %d ofs=%d sz=%d",
1356 pp->level, p->pos, p->offset, p->size);
1361 p->offset=src-(char*)p->bytes;
1362 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1368 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1369 "at level %d node %d ofs=%d sz=%d skips=%d",
1370 pp->level, p->pos, p->offset, p->size, skips);
1372 return pos; /* that's the last one in the line */
1374 } /* forward_unode */
1376 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, ISAMB_P pos, const void *untilbuf)
1377 { /* climbs down the tree, from pos, to the leftmost leaf */
1378 struct ISAMB_block *p = pp->block[pp->level];
1382 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1383 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1384 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1387 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1390 p=open_block(pp->isamb, pos);
1391 pp->block[pp->level]=p;
1392 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1395 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1396 "got lev %d node %d lf=%d",
1397 pp->level, p->pos, p->leaf);
1401 assert (p->offset==0 );
1402 src=p->bytes + p->offset;
1403 decode_ptr(&src, &pos);
1404 p->offset=src-(char*)p->bytes;
1405 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1407 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1408 "returning at lev %d node %d ofs=%d lf=%d",
1409 pp->level, p->pos, p->offset, p->leaf);
1411 } /* descend_to_leaf */
1413 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1414 { /* finds the next leaf by climbing up and down */
1416 if (!isamb_pp_climb_level(pp,&pos))
1418 isamb_pp_descend_to_leaf(pp, pos,0);
1422 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1423 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1426 struct ISAMB_block *p = pp->block[pp->level];
1427 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1428 "at level %d node %d ofs=%d sz=%d",
1429 pp->level, p->pos, p->offset, p->size);
1431 if (!isamb_pp_climb_level(pp,&pos))
1433 /* see if it would pay to climb one higher */
1434 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1435 if (!isamb_pp_climb_level(pp,&pos))
1437 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1439 p = pp->block[pp->level];
1440 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1441 "at level %d node %d ofs=%d sz=%d",
1442 pp->level, p->pos, p->offset, p->size);
1447 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1450 struct ISAMB_block *p = pp->block[pp->level];
1452 logf(LOG_DEBUG,"isamb_pp_forward starting "
1453 "at level %d node %d ofs=%d sz=%d u=%p",
1454 pp->level, p->pos, p->offset, p->size,untilbuf);
1457 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1459 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1460 "at level %d node %d ofs=%d sz=%d",
1461 pp->level, p->pos, p->offset, p->size);
1465 if (! isamb_pp_climb_desc( pp, buf, untilbuf)) {
1467 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1468 "at level %d node %d ofs=%d sz=%d",
1469 pp->level, p->pos, p->offset, p->size);
1471 return 0; /* could not find a leaf */
1474 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1476 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1477 "at level %d node %d ofs=%d sz=%d",
1478 pp->level, p->pos, p->offset, p->size);
1482 }while ( isamb_pp_find_next_leaf(pp));
1483 return 0; /* could not find at all */
1485 else { /* no untilbuf, a straight read */
1486 /* FIXME - this should be moved
1487 * directly into the pp_read */
1488 /* keeping here now, to keep same
1489 * interface as the old fwd */
1490 if (isamb_pp_read_on_leaf( pp, buf)) {
1492 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1493 "at level %d node %d ofs=%d sz=%d",
1494 pp->level, p->pos, p->offset, p->size);
1498 if (isamb_pp_find_next_leaf(pp)) {
1500 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1501 "at level %d node %d ofs=%d sz=%d",
1502 pp->level, p->pos, p->offset, p->size);
1504 return isamb_pp_read_on_leaf(pp, buf);
1509 } /* isam_pp_forward (new version) */
1511 #elif NEW_FORWARD == 0
1513 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1517 * while at end of node
1518 * climb higher. If out, return 0
1519 * while not on a leaf (and not at its end)
1528 * The upper nodes consist of a sequence of nodenumbers and keys
1529 * When opening a block, the first node number is read in, and
1530 * offset points to the first key, which is the upper limit of keys
1531 * in the node just read.
1535 struct ISAMB_block *p = pp->block[pp->level];
1540 int descending=0; /* used to prevent a border condition error */
1544 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1546 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, untilbuf, "until");
1547 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "buf");
1552 while ( (p->offset == p->size) && !descending )
1553 { /* end of this block - climb higher */
1554 assert (p->offset <= p->size);
1556 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1562 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1564 return 0; /* at end of the root, nothing left */
1566 close_block(pp->isamb, pp->block[pp->level]);
1567 pp->block[pp->level]=0;
1569 p=pp->block[pp->level];
1571 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1575 assert(p->offset <= p->size);
1576 /* skip the child we have handled */
1577 if (p->offset != p->size)
1579 src = p->bytes + p->offset;
1580 decode_ptr(&src, &item_len);
1582 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src,
1583 " isamb_pp_forward "
1584 "climb skipping old key");
1587 decode_ptr(&src,&pos);
1588 p->offset = src - (char*) p->bytes;
1589 break; /* even if this puts us at the end of the block, we
1590 need to descend to the last pos. UGLY coding,
1591 clean up some day */
1596 src = p->bytes + p->offset;
1597 if (p->offset == p->size)
1598 cmp=-2 ; /* descend to the last node, as we have
1602 decode_ptr(&src, &item_len);
1604 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1605 "ofs=%d sz=%d nxtpos=%d ",
1606 p->offset,p->size,pos);
1607 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src, "");
1610 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1614 decode_ptr(&src,&nxtpos);
1619 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1622 descending=1; /* prevent climbing for a while */
1624 p = open_block(pp->isamb,pos);
1625 pp->block[pp->level] = p ;
1626 pp->total_size += p->size;
1627 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1630 { /* block starts with a pos */
1631 src = p->bytes + p->offset;
1632 decode_ptr(&src,&pos);
1633 p->offset=src-(char*) p->bytes;
1635 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1639 } /* descend to the node */
1641 { /* skip the node */
1642 p->offset = src - (char*) p->bytes;
1644 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1647 "isamb_pp_forward: skipping block on level %d, noting "
1649 pp->level, pp->maxlevel - pp->level-1 ,
1650 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1652 /* 0 is always leafs, 1 is one level above leafs etc, no
1653 * matter how high tree */
1655 } /* not on a leaf */
1658 if (p->offset == p->size) {
1663 assert (p->offset < p->size);
1664 src = p->bytes + p->offset;
1666 (*pp->isamb->method->codec.decode)(p->decodeClientData,
1668 p->offset = src - (char*) p->bytes;
1670 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1674 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1676 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "");
1683 (*pp->isamb->method->codec.log_item)(
1684 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1688 (*pp->isamb->method->codec.log_item)(
1689 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1692 pp->returned_numbers++;
1696 pp->skipped_numbers++;
1702 #elif NEW_FORWARD == 2
1704 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1708 struct ISAMB_block *p = pp->block[pp->level];
1713 while (p->offset == p->size)
1716 while (p->offset == p->size)
1720 close_block (pp->isamb, pp->block[pp->level]);
1721 pp->block[pp->level] = 0;
1723 p = pp->block[pp->level];
1728 src = p->bytes + p->offset;
1730 decode_ptr (&src, &item_len);
1732 decode_ptr (&src, &pos);
1734 p->offset = src - (char*) p->bytes;
1736 src = p->bytes + p->offset;
1740 if (!untilb || p->offset == p->size)
1742 assert(p->offset < p->size);
1743 decode_ptr (&src, &item_len);
1744 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1747 decode_ptr (&src, &pos);
1748 p->offset = src - (char*) p->bytes;
1755 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1757 pp->total_size += p->size;
1765 src = p->bytes + p->offset;
1768 decode_ptr (&src, &pos);
1769 p->offset = src - (char*) p->bytes;
1771 if (!untilb || p->offset == p->size)
1773 assert(p->offset < p->size);
1774 decode_ptr (&src, &item_len);
1775 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1782 assert (p->offset < p->size);
1787 src = p->bytes + p->offset;
1788 (*pp->isamb->method->codec.decode)(p->decodeClientData, &dst, &src);
1789 p->offset = src - (char*) p->bytes;
1790 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1793 if (p->offset == p->size) goto again;
1795 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1801 int isamb_pp_num (ISAMB_PP pp)
1806 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1807 int *current, int *total, void *dummybuf )
1809 struct ISAMB_block *p = pp->block[pp->level];
1810 const char *src=p->bytes;
1811 char *end=p->bytes+p->size;
1812 char *cur=p->bytes+p->offset;
1814 assert(p->offset <= p->size);
1822 (*pp->isamb->method->codec.decode)(p->decodeClientData,&dst, &src);
1823 assert(dst<(char*) dummybuf+100); /*FIXME */
1828 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur=%d tot=%d ofs=%d sz=%d lev=%d",
1829 *current, *total, p->offset, p->size, pp->level);
1833 static void isamb_pp_upper_pos( ISAMB_PP pp, int *current, int *total,
1834 int size, int level )
1835 { /* estimates total/current occurrences from here up, excl leaf */
1836 struct ISAMB_block *p = pp->block[level];
1837 const char *src=p->bytes;
1838 char *end=p->bytes+p->size;
1839 char *cur=p->bytes+p->offset;
1844 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1845 "cur=%d tot=%d ofs=%d sz=%d pos=" ZINT_FORMAT,
1846 level, *current, *total, p->offset, p->size, p->pos);
1847 assert (p->offset <= p->size);
1848 decode_ptr (&src, &child ); /* first child */
1855 decode_ptr (&src, &item_size );
1856 assert(src+item_size<=end);
1858 decode_ptr (&src, &child );
1861 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1864 void isamb_pp_pos( ISAMB_PP pp, int *current, int *total )
1865 { /* return an estimate of the current position and of the total number of */
1866 /* occureences in the isam tree, based on the current leaf */
1867 struct ISAMB_block *p = pp->block[pp->level];
1868 char dummy[100]; /* 100 bytes/entry must be enough */
1872 isamb_pp_leaf_pos(pp,current, total, dummy);
1874 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1876 logf(LOG_DEBUG,"isamb_pp_pos: C=%d T=%d =%6.2f%%",
1877 *current, *total, 100.0*(*current)/(*total));