1 /* $Id: isamb.c,v 1.54 2004-08-16 10:08:37 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>
33 #define ISAMB_MAJOR_VERSION 1
34 #define ISAMB_MINOR_VERSION 0
44 #define ISAMB_DATA_OFFSET 3
46 /* maximum size of encoded buffer */
47 #define DST_ITEM_MAX 256
49 #define ISAMB_MAX_LEVEL 10
50 /* approx 2*max page + max size of item */
51 #define DST_BUF_SIZE 16840
53 #define ISAMB_CACHE_ENTRY_SIZE 4096
55 /* CAT_MAX: _must_ be power of 2 */
57 #define CAT_MASK (CAT_MAX-1)
58 /* CAT_NO: <= CAT_MAX */
61 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
62 #define ISAMB_PTR_CODEC 1
64 struct ISAMB_cache_entry {
69 struct ISAMB_cache_entry *next;
75 struct ISAMB_head head;
76 struct ISAMB_cache_entry *cache_entries;
83 struct ISAMB_file *file;
85 int cache; /* 0=no cache, 1=use cache, -1=dummy isam (for testing only) */
86 int log_io; /* log level for bf_read/bf_write calls */
87 int log_freelist; /* log level for freelist handling */
88 zint skipped_numbers; /* on a leaf node */
89 zint returned_numbers;
90 zint skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
91 zint accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
105 void *decodeClientData;
113 int maxlevel; /* total depth */
116 zint skipped_numbers; /* on a leaf node */
117 zint returned_numbers;
118 zint skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
119 zint accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
120 struct ISAMB_block **block;
125 static void encode_ptr (char **dst, zint pos)
127 unsigned char *bp = (unsigned char*) *dst;
131 *bp++ = 128 | (pos & 127);
138 static void encode_ptr (char **dst, zint pos)
140 memcpy(*dst, &pos, sizeof(pos));
141 (*dst) += sizeof(pos);
146 static void decode_ptr (const char **src1, zint *pos)
148 const unsigned char **src = (const unsigned char **) src1;
153 while (((c = *(*src)++) & 128))
155 d += ((zint) (c & 127) << r);
158 d += ((zint) c << r);
162 static void decode_ptr (const char **src, zint *pos)
164 memcpy (pos, *src, sizeof(*pos));
165 (*src) += sizeof(*pos);
169 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
172 ISAMB isamb = xmalloc (sizeof(*isamb));
176 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
177 memcpy (isamb->method, method, sizeof(*method));
178 isamb->no_cat = CAT_NO;
180 isamb->log_freelist = 0;
181 isamb->cache = cache;
182 isamb->skipped_numbers=0;
183 isamb->returned_numbers=0;
184 for (i=0;i<ISAMB_MAX_LEVEL;i++)
185 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
188 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
189 for (i = 0; i<isamb->no_cat; i++)
191 char fname[DST_BUF_SIZE];
192 char hbuf[DST_BUF_SIZE];
193 isamb->file[i].cache_entries = 0;
194 isamb->file[i].head_dirty = 0;
195 sprintf (fname, "%s%c", name, i+'A');
197 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
200 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
202 /* fill-in default values (for empty isamb) */
203 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
204 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
205 isamb->file[i].head.block_size = b_size;
206 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
207 isamb->file[i].head.free_list = 0;
208 if (bf_read (isamb->file[i].bf, 0, 0, 0, hbuf))
210 /* got header assume "isamb"major minor len can fit in 16 bytes */
212 int major, minor, len, pos = 0;
215 if (memcmp(hbuf, "isamb", 5))
217 logf(LOG_WARN, "bad isamb header for file %s", fname);
220 if (sscanf(hbuf+5, "%d %d %d", &major, &minor, &len) != 3)
222 logf(LOG_WARN, "bad isamb header for file %s", fname);
225 if (major != ISAMB_MAJOR_VERSION)
227 logf(LOG_WARN, "bad major version for file %s %d, must be %d",
228 fname, major, ISAMB_MAJOR_VERSION);
231 for (left = len - b_size; left > 0; left = left - b_size)
234 if (!bf_read (isamb->file[i].bf, pos, 0, 0, hbuf + pos*b_size))
236 logf(LOG_WARN, "truncated isamb header for "
237 "file=%s len=%d pos=%d",
243 decode_ptr(&src, &isamb->file[i].head.first_block);
244 decode_ptr(&src, &isamb->file[i].head.last_block);
245 decode_ptr(&src, &zint_tmp);
246 isamb->file[i].head.block_size = zint_tmp;
247 decode_ptr(&src, &zint_tmp);
248 isamb->file[i].head.block_max = zint_tmp;
249 decode_ptr(&src, &isamb->file[i].head.free_list);
251 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
252 isamb->file[i].head_dirty = 0;
253 assert(isamb->file[i].head.block_size == b_size);
257 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
262 static void flush_blocks (ISAMB b, int cat)
264 while (b->file[cat].cache_entries)
266 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
267 b->file[cat].cache_entries = ce_this->next;
271 yaz_log (b->log_io, "bf_write: flush_blocks");
272 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
274 xfree (ce_this->buf);
279 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
281 int cat = (int) (pos&CAT_MASK);
282 int off = (int) (((pos/CAT_MAX) &
283 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
284 * b->file[cat].head.block_size);
285 zint norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
287 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
292 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
293 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
296 if ((*ce)->pos == norm)
299 *ce = (*ce)->next; /* remove from list */
301 ce_this->next = b->file[cat].cache_entries; /* move to front */
302 b->file[cat].cache_entries = ce_this;
306 memcpy (ce_this->buf + off, userbuf,
307 b->file[cat].head.block_size);
311 memcpy (userbuf, ce_this->buf + off,
312 b->file[cat].head.block_size);
319 assert (ce_last && *ce_last);
321 *ce_last = 0; /* remove the last entry from list */
324 yaz_log (b->log_io, "bf_write: get_block");
325 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
327 xfree (ce_this->buf);
330 ce_this = xmalloc (sizeof(*ce_this));
331 ce_this->next = b->file[cat].cache_entries;
332 b->file[cat].cache_entries = ce_this;
333 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
335 yaz_log (b->log_io, "bf_read: get_block");
336 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
337 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
340 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
346 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
352 void isamb_close (ISAMB isamb)
355 for (i=0;isamb->accessed_nodes[i];i++)
356 logf(LOG_DEBUG,"isamb_close level leaf-%d: "ZINT_FORMAT" read, "
357 ZINT_FORMAT" skipped",
358 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
359 logf(LOG_DEBUG,"isamb_close returned "ZINT_FORMAT" values, "
360 "skipped "ZINT_FORMAT,
361 isamb->skipped_numbers, isamb->returned_numbers);
362 for (i = 0; i<isamb->no_cat; i++)
364 flush_blocks (isamb, i);
365 if (isamb->file[i].head_dirty)
367 char hbuf[DST_BUF_SIZE];
368 int major = ISAMB_MAJOR_VERSION;
369 int minor = ISAMB_MINOR_VERSION;
371 char *dst = hbuf + 16;
373 int b_size = isamb->file[i].head.block_size;
375 encode_ptr(&dst, isamb->file[i].head.first_block);
376 encode_ptr(&dst, isamb->file[i].head.last_block);
377 encode_ptr(&dst, isamb->file[i].head.block_size);
378 encode_ptr(&dst, isamb->file[i].head.block_max);
379 encode_ptr(&dst, isamb->file[i].head.free_list);
380 memset(dst, '\0', 16); /* ensure no random bytes are written */
384 /* print exactly 16 bytes (including trailing 0) */
385 sprintf(hbuf, "isamb%02d %02d %02d\r\n", major, minor, len);
387 bf_write (isamb->file[i].bf, pos, 0, 0, hbuf);
389 for (left = len - b_size; left > 0; left = left - b_size)
392 bf_write (isamb->file[i].bf, pos, 0, 0, hbuf + pos*b_size);
395 bf_close (isamb->file[i].bf);
398 xfree (isamb->method);
402 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
404 int cat = (int) (pos&CAT_MASK);
405 struct ISAMB_block *p;
408 p = xmalloc (sizeof(*p));
410 p->cat = (int) (pos & CAT_MASK);
411 p->buf = xmalloc (b->file[cat].head.block_size);
414 if (!get_block (b, pos, p->buf, 0))
416 yaz_log (b->log_io, "bf_read: open_block");
417 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
419 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
420 (long) pos, (long) pos/CAT_MAX);
424 p->bytes = p->buf + ISAMB_DATA_OFFSET;
426 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
429 yaz_log (LOG_FATAL, "Bad block size %d in pos=" ZINT_FORMAT "\n",
432 assert (p->size >= 0);
436 p->decodeClientData = (*b->method->codec.start)();
437 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block " ZINT_FORMAT " ofs=%d",pos, p->offset);
441 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
443 struct ISAMB_block *p;
445 p = xmalloc (sizeof(*p));
446 p->buf = xmalloc (b->file[cat].head.block_size);
448 if (!b->file[cat].head.free_list)
451 block_no = b->file[cat].head.last_block++;
452 p->pos = block_no * CAT_MAX + cat;
456 p->pos = b->file[cat].head.free_list;
457 assert((p->pos & CAT_MASK) == cat);
458 if (!get_block (b, p->pos, p->buf, 0))
460 yaz_log (b->log_io, "bf_read: new_block");
461 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
463 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
464 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
468 yaz_log (b->log_freelist, "got block " ZINT_FORMAT " from freelist %d:" ZINT_FORMAT, p->pos,
469 cat, p->pos/CAT_MAX);
470 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
473 b->file[cat].head_dirty = 1;
474 memset (p->buf, 0, b->file[cat].head.block_size);
475 p->bytes = p->buf + ISAMB_DATA_OFFSET;
481 p->decodeClientData = (*b->method->codec.start)();
485 struct ISAMB_block *new_leaf (ISAMB b, int cat)
487 return new_block (b, 1, cat);
491 struct ISAMB_block *new_int (ISAMB b, int cat)
493 return new_block (b, 0, cat);
496 static void check_block (ISAMB b, struct ISAMB_block *p)
498 assert(b); /* mostly to make the compiler shut up about unused b */
506 char *startp = p->bytes;
507 const char *src = startp;
508 char *endp = p->bytes + p->size;
511 decode_ptr (&src, &pos);
512 assert ((pos&CAT_MASK) == p->cat);
516 decode_ptr (&src, &item_len);
517 assert (item_len > 0 && item_len < 80);
519 decode_ptr (&src, &pos);
520 assert ((pos&CAT_MASK) == p->cat);
525 void close_block (ISAMB b, struct ISAMB_block *p)
531 yaz_log (b->log_freelist, "release block " ZINT_FORMAT " from freelist %d:" ZINT_FORMAT,
532 p->pos, p->cat, p->pos/CAT_MAX);
533 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
534 b->file[p->cat].head.free_list = p->pos;
535 if (!get_block (b, p->pos, p->buf, 1))
537 yaz_log (b->log_io, "bf_write: close_block (deleted)");
538 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
543 int size = p->size + ISAMB_DATA_OFFSET;
544 assert (p->size >= 0);
546 p->buf[1] = size & 255;
547 p->buf[2] = size >> 8;
549 if (!get_block (b, p->pos, p->buf, 1))
551 yaz_log (b->log_io, "bf_write: close_block");
552 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
555 (*b->method->codec.stop)(p->decodeClientData);
560 int insert_sub (ISAMB b, struct ISAMB_block **p,
561 void *new_item, int *mode,
563 struct ISAMB_block **sp,
564 void *sub_item, int *sub_size,
565 const void *max_item);
567 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
569 ISAMC_I *stream, struct ISAMB_block **sp,
570 void *split_item, int *split_size, const void *last_max_item)
572 char *startp = p->bytes;
573 const char *src = startp;
574 char *endp = p->bytes + p->size;
576 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
577 char sub_item[DST_ITEM_MAX];
583 assert(p->size >= 0);
584 decode_ptr (&src, &pos);
589 const char *src0 = src;
590 decode_ptr (&src, &item_len);
591 d = (*b->method->compare_item)(src, lookahead_item);
594 sub_p1 = open_block (b, pos);
596 more = insert_sub (b, &sub_p1, lookahead_item, mode,
598 sub_item, &sub_size, src);
603 decode_ptr (&src, &pos);
607 sub_p1 = open_block (b, pos);
609 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
610 sub_item, &sub_size, last_max_item);
614 /* there was a split - must insert pointer in this one */
615 char dst_buf[DST_BUF_SIZE];
618 assert (sub_size < 80 && sub_size > 1);
620 memcpy (dst, startp, src - startp);
624 encode_ptr (&dst, sub_size); /* sub length and item */
625 memcpy (dst, sub_item, sub_size);
628 encode_ptr (&dst, sub_p2->pos); /* pos */
630 if (endp - src) /* remaining data */
632 memcpy (dst, src, endp - src);
635 p->size = dst - dst_buf;
636 assert (p->size >= 0);
637 if (p->size <= b->file[p->cat].head.block_max)
639 memcpy (startp, dst_buf, dst - dst_buf);
649 half = src + b->file[p->cat].head.block_size/2;
650 decode_ptr (&src, &pos);
653 decode_ptr (&src, &split_size_tmp);
654 *split_size = (int) split_size_tmp;
657 decode_ptr (&src, &pos);
659 p_new_size = src - dst_buf;
660 memcpy (p->bytes, dst_buf, p_new_size);
662 decode_ptr (&src, &split_size_tmp);
663 *split_size = (int) split_size_tmp;
664 memcpy (split_item, src, *split_size);
667 *sp = new_int (b, p->cat);
668 (*sp)->size = endp - src;
669 memcpy ((*sp)->bytes, src, (*sp)->size);
671 p->size = p_new_size;
674 close_block (b, sub_p2);
676 close_block (b, sub_p1);
680 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
681 int *lookahead_mode, ISAMC_I *stream,
682 struct ISAMB_block **sp2,
683 void *sub_item, int *sub_size,
684 const void *max_item)
686 struct ISAMB_block *p = *sp1;
689 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
691 void *c1 = (*b->method->codec.start)();
692 void *c2 = (*b->method->codec.start)();
694 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
695 char *cut = dst_buf + quater * 2;
696 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
699 char cut_item_buf[DST_ITEM_MAX];
700 int cut_item_size = 0;
704 char file_item_buf[DST_ITEM_MAX];
705 char *file_item = file_item_buf;
708 endp = p->bytes + p->size;
709 (*b->method->codec.decode)(c1, &file_item, &src);
712 const char *dst_item = 0;
714 char *lookahead_next;
718 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
722 dst_item = lookahead_item;
723 if (!*lookahead_mode)
725 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
726 assert (*lookahead_mode);
730 dst_item = file_item_buf;
731 if (!*lookahead_mode && d == 0)
735 else if (!half1 && dst > cut)
737 const char *dst_item_0 = dst_item;
738 half1 = dst; /* candidate for splitting */
740 (*b->method->codec.encode)(c2, &dst, &dst_item);
742 cut_item_size = dst_item - dst_item_0;
743 assert(cut_item_size > 0);
744 memcpy (cut_item_buf, dst_item_0, cut_item_size);
749 (*b->method->codec.encode)(c2, &dst, &dst_item);
759 lookahead_next = lookahead_item;
760 if (!(*stream->read_item)(stream->clientData,
767 if (lookahead_item && max_item &&
768 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
779 lookahead_next = lookahead_item;
780 if (!(*stream->read_item)(stream->clientData,
781 &lookahead_next, lookahead_mode))
788 file_item = file_item_buf;
789 (*b->method->codec.decode)(c1, &file_item, &src);
795 file_item = file_item_buf;
796 (*b->method->codec.decode)(c1, &file_item, &src);
800 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
801 while (lookahead_item)
804 const char *src = lookahead_item;
808 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
813 if (!*lookahead_mode)
815 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
818 else if (!half1 && dst > cut)
820 const char *src_0 = src;
821 half1 = dst; /* candidate for splitting */
823 (*b->method->codec.encode)(c2, &dst, &src);
825 cut_item_size = src - src_0;
826 assert(cut_item_size > 0);
827 memcpy (cut_item_buf, src_0, cut_item_size);
832 (*b->method->codec.encode)(c2, &dst, &src);
841 dst_item = lookahead_item;
842 if (!(*stream->read_item)(stream->clientData, &dst_item,
849 new_size = dst - dst_buf;
850 if (p && p->cat != b->no_cat-1 &&
851 new_size > b->file[p->cat].head.block_max)
853 /* non-btree block will be removed */
856 /* delete it too!! */
857 p = 0; /* make a new one anyway */
860 { /* must create a new one */
862 for (i = 0; i < b->no_cat; i++)
863 if (new_size <= b->file[i].head.block_max)
869 if (new_size > b->file[p->cat].head.block_max)
872 const char *cut_item = cut_item_buf;
877 assert(cut_item_size > 0);
880 p->size = half1 - dst_buf;
881 memcpy (p->bytes, dst_buf, half1 - dst_buf);
884 *sp2 = new_leaf (b, p->cat);
886 (*b->method->codec.reset)(c2);
888 first_dst = (*sp2)->bytes;
890 (*b->method->codec.encode)(c2, &first_dst, &cut_item);
892 memcpy (first_dst, half2, dst - half2);
894 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
897 memcpy (sub_item, cut_item_buf, cut_item_size);
898 *sub_size = cut_item_size;
902 memcpy (p->bytes, dst_buf, dst - dst_buf);
905 (*b->method->codec.stop)(c1);
906 (*b->method->codec.stop)(c2);
911 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
914 struct ISAMB_block **sp,
915 void *sub_item, int *sub_size,
916 const void *max_item)
918 if (!*p || (*p)->leaf)
919 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
922 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
926 int isamb_unlink (ISAMB b, ISAMC_P pos)
928 struct ISAMB_block *p1;
932 p1 = open_block(b, pos);
938 const char *src = p1->bytes + p1->offset;
940 decode_ptr(&src, &sub_p);
941 isamb_unlink(b, sub_p);
943 while (src != p1->bytes + p1->size)
945 decode_ptr(&src, &item_len);
947 decode_ptr(&src, &sub_p);
948 isamb_unlink(b, sub_p);
955 ISAMB_P isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
957 char item_buf[DST_ITEM_MAX];
969 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
974 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
977 struct ISAMB_block *p = 0, *sp = 0;
978 char sub_item[DST_ITEM_MAX];
982 p = open_block (b, pos);
983 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
984 sub_item, &sub_size, 0);
986 { /* increase level of tree by one */
987 struct ISAMB_block *p2 = new_int (b, p->cat);
988 char *dst = p2->bytes + p2->size;
990 encode_ptr (&dst, p->pos);
991 assert (sub_size < 40);
992 encode_ptr (&dst, sub_size);
993 memcpy (dst, sub_item, sub_size);
995 encode_ptr (&dst, sp->pos);
997 p2->size = dst - p2->bytes;
998 pos = p2->pos; /* return new super page */
1000 close_block (b, p2);
1003 pos = p->pos; /* return current one (again) */
1009 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
1011 ISAMB_PP pp = xmalloc (sizeof(*pp));
1015 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
1022 pp->skipped_numbers=0;
1023 pp->returned_numbers=0;
1024 for (i=0;i<ISAMB_MAX_LEVEL;i++)
1025 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
1028 struct ISAMB_block *p = open_block (isamb, pos);
1029 const char *src = p->bytes + p->offset;
1030 pp->block[pp->level] = p;
1032 pp->total_size += p->size;
1038 decode_ptr (&src, &pos);
1039 p->offset = src - p->bytes;
1041 pp->accessed_nodes[pp->level]++;
1043 pp->block[pp->level+1] = 0;
1044 pp->maxlevel=pp->level;
1050 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
1052 return isamb_pp_open_x (isamb, pos, 0);
1055 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
1060 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned "ZINT_FORMAT" values,"
1061 "skipped "ZINT_FORMAT,
1062 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
1063 for (i=pp->maxlevel;i>=0;i--)
1064 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
1065 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: "
1066 ZINT_FORMAT" read, "ZINT_FORMAT" skipped", i,
1067 pp->accessed_nodes[i], pp->skipped_nodes[i]);
1068 pp->isamb->skipped_numbers += pp->skipped_numbers;
1069 pp->isamb->returned_numbers += pp->returned_numbers;
1070 for (i=pp->maxlevel;i>=0;i--)
1072 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
1073 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
1076 *size = pp->total_size;
1078 *blocks = pp->no_blocks;
1079 for (i = 0; i <= pp->level; i++)
1080 close_block (pp->isamb, pp->block[i]);
1085 int isamb_block_info (ISAMB isamb, int cat)
1087 if (cat >= 0 && cat < isamb->no_cat)
1088 return isamb->file[cat].head.block_size;
1092 void isamb_pp_close (ISAMB_PP pp)
1094 isamb_pp_close_x (pp, 0, 0);
1097 /* simple recursive dumper .. */
1098 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1102 char prefix_str[1024];
1105 struct ISAMB_block *p = open_block (b, pos);
1106 sprintf(prefix_str, "%*s " ZINT_FORMAT " cat=%d size=%d max=%d", level*2, "",
1107 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1109 sprintf(prefix_str, "%*s " ZINT_FORMAT, level*2, "", pos);
1112 while (p->offset < p->size)
1114 const char *src = p->bytes + p->offset;
1116 (*b->method->codec.decode)(p->decodeClientData, &dst, &src);
1117 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1118 p->offset = src - (char*) p->bytes;
1120 assert(p->offset == p->size);
1124 const char *src = p->bytes + p->offset;
1128 decode_ptr (&src, &sub);
1129 p->offset = src - (char*) p->bytes;
1131 isamb_dump_r(b, sub, pr, level+1);
1133 while (p->offset < p->size)
1135 decode_ptr (&src, &item_len);
1136 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1138 decode_ptr (&src, &sub);
1140 p->offset = src - (char*) p->bytes;
1142 isamb_dump_r(b, sub, pr, level+1);
1149 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1151 isamb_dump_r(b, pos, pr, 0);
1155 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1156 debug the more complex pp_read that also forwards. May be deleted near end
1157 of 2004, if it has not shown to be useful */
1160 int isamb_pp_read (ISAMB_PP pp, void *buf)
1164 struct ISAMB_block *p = pp->block[pp->level];
1168 while (p->offset == p->size)
1171 while (p->offset == p->size)
1175 close_block (pp->isamb, pp->block[pp->level]);
1176 pp->block[pp->level] = 0;
1178 p = pp->block[pp->level];
1181 src = p->bytes + p->offset;
1183 decode_ptr (&src, &item_len);
1185 decode_ptr (&src, &pos);
1187 p->offset = src - (char*) p->bytes;
1193 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1195 pp->total_size += p->size;
1202 src = p->bytes + p->offset;
1203 decode_ptr (&src, &pos);
1204 p->offset = src - (char*) p->bytes;
1208 assert (p->offset < p->size);
1210 src = p->bytes + p->offset;
1211 (*pp->isamb->method->codec.code_item)(ISAMC_DECODE, p->decodeClientData,
1213 p->offset = src - (char*) p->bytes;
1214 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1219 int isamb_pp_read (ISAMB_PP pp, void *buf)
1221 return isamb_pp_forward(pp, buf, 0);
1225 #define NEW_FORWARD 1
1227 #if NEW_FORWARD == 1
1229 static int isamb_pp_on_right_node(ISAMB_PP pp, int level, const void *untilbuf)
1230 { /* looks one node higher to see if we should be on this node at all */
1231 /* useful in backing off quickly, and in avoiding tail descends */
1232 /* call with pp->level to begin with */
1233 struct ISAMB_block *p;
1240 logf(LOG_DEBUG,"isamb_pp_on_right returning true for root");
1242 return 1; /* we can never skip the root node */
1246 assert(p->offset <= p->size);
1247 if (p->offset < p->size )
1249 assert(p->offset>0);
1250 src=p->bytes + p->offset;
1251 decode_ptr(&src, &item_len);
1253 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,untilbuf,"on_leaf: until");
1254 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,src,"on_leaf: value");
1256 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1259 logf(LOG_DEBUG,"isamb_pp_on_right returning true "
1260 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1266 logf(LOG_DEBUG,"isamb_pp_on_right returning false "
1267 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1274 logf(LOG_DEBUG,"isamb_pp_on_right at tail, looking higher "
1277 return isamb_pp_on_right_node(pp, level, untilbuf);
1279 } /* isamb_pp_on_right_node */
1281 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1282 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1283 struct ISAMB_block *p = pp->block[pp->level];
1288 if (p->offset == p->size) {
1290 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1292 return 0; /* at end of leaf */
1294 src=p->bytes + p->offset;
1296 (*pp->isamb->method->codec.decode)(p->decodeClientData,&dst, &src);
1297 p->offset = src - (char*) p->bytes;
1300 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1303 pp->returned_numbers++;
1305 } /* read_on_leaf */
1307 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1308 { /* forwards on the current leaf, returns 0 if not found */
1312 if (!isamb_pp_read_on_leaf(pp,buf))
1314 /* FIXME - this is an extra function call, inline the read? */
1315 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1316 if (cmp <2){ /* found a good one */
1319 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1321 pp->returned_numbers++;
1325 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1326 return 0; /* never mind the rest of this leaf */
1327 pp->skipped_numbers++;
1330 } /* forward_on_leaf */
1332 static int isamb_pp_climb_level(ISAMB_PP pp, ISAMB_P *pos)
1333 { /* climbs higher in the tree, until finds a level with data left */
1334 /* returns the node to (consider to) descend to in *pos) */
1335 struct ISAMB_block *p = pp->block[pp->level];
1339 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1340 "at level %d node %d ofs=%d sz=%d",
1341 pp->level, p->pos, p->offset, p->size);
1343 assert(pp->level >= 0);
1344 assert(p->offset <= p->size);
1348 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1352 assert(pp->level>0);
1353 close_block(pp->isamb, pp->block[pp->level]);
1354 pp->block[pp->level]=0;
1356 p=pp->block[pp->level];
1358 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1359 pp->level, p->pos, p->offset);
1362 assert(p->offset <= p->size);
1363 if (p->offset == p->size ) {
1364 /* we came from the last pointer, climb on */
1365 if (!isamb_pp_climb_level(pp,pos))
1367 p=pp->block[pp->level];
1371 /* skip the child we just came from */
1373 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1374 pp->level, p->offset, p->size);
1376 assert (p->offset < p->size );
1377 src=p->bytes + p->offset;
1378 decode_ptr(&src, &item_len);
1380 decode_ptr(&src, pos);
1381 p->offset=src - (char *)p->bytes;
1388 static zint isamb_pp_forward_unode(ISAMB_PP pp, zint pos, const void *untilbuf)
1389 { /* scans a upper node until it finds a child <= untilbuf */
1390 /* pp points to the key value, as always. pos is the child read from */
1392 /* if all values are too small, returns the last child in the node */
1393 /* FIXME - this can be detected, and avoided by looking at the */
1394 /* parent node, but that gets messy. Presumably the cost is */
1395 /* pretty low anyway */
1396 struct ISAMB_block *p = pp->block[pp->level];
1397 const char *src=p->bytes + p->offset;
1403 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1404 "at level %d node %d ofs=%di sz=%d",
1405 pp->level, p->pos, p->offset, p->size);
1408 assert(p->offset <= p->size);
1409 if (p->offset == p->size) {
1411 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1412 "at level %d node %d ofs=%di sz=%d",
1413 pp->level, p->pos, p->offset, p->size);
1415 return pos; /* already at the end of it */
1417 while(p->offset < p->size) {
1418 decode_ptr(&src,&item_len);
1419 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1421 decode_ptr(&src,&nxtpos);
1425 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1426 "at level %d node %d ofs=%d sz=%d",
1427 pp->level, p->pos, p->offset, p->size);
1432 p->offset=src-(char*)p->bytes;
1433 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1439 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1440 "at level %d node %d ofs=%d sz=%d skips=%d",
1441 pp->level, p->pos, p->offset, p->size, skips);
1443 return pos; /* that's the last one in the line */
1445 } /* forward_unode */
1447 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, ISAMB_P pos, const void *untilbuf)
1448 { /* climbs down the tree, from pos, to the leftmost leaf */
1449 struct ISAMB_block *p = pp->block[pp->level];
1453 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1454 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1455 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1458 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1461 p=open_block(pp->isamb, pos);
1462 pp->block[pp->level]=p;
1463 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1466 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1467 "got lev %d node %d lf=%d",
1468 pp->level, p->pos, p->leaf);
1472 assert (p->offset==0 );
1473 src=p->bytes + p->offset;
1474 decode_ptr(&src, &pos);
1475 p->offset=src-(char*)p->bytes;
1476 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1478 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1479 "returning at lev %d node %d ofs=%d lf=%d",
1480 pp->level, p->pos, p->offset, p->leaf);
1482 } /* descend_to_leaf */
1484 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1485 { /* finds the next leaf by climbing up and down */
1487 if (!isamb_pp_climb_level(pp,&pos))
1489 isamb_pp_descend_to_leaf(pp, pos,0);
1493 static int isamb_pp_climb_desc(ISAMB_PP pp, const void *untilbuf)
1494 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1497 struct ISAMB_block *p = pp->block[pp->level];
1498 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1499 "at level %d node %d ofs=%d sz=%d",
1500 pp->level, p->pos, p->offset, p->size);
1502 if (!isamb_pp_climb_level(pp,&pos))
1504 /* see if it would pay to climb one higher */
1505 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1506 if (!isamb_pp_climb_level(pp,&pos))
1508 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1510 p = pp->block[pp->level];
1511 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1512 "at level %d node %d ofs=%d sz=%d",
1513 pp->level, p->pos, p->offset, p->size);
1518 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1521 struct ISAMB_block *p = pp->block[pp->level];
1523 logf(LOG_DEBUG,"isamb_pp_forward starting "
1524 "at level %d node %d ofs=%d sz=%d u=%p",
1525 pp->level, p->pos, p->offset, p->size,untilbuf);
1528 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1530 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1531 "at level %d node %d ofs=%d sz=%d",
1532 pp->level, p->pos, p->offset, p->size);
1536 if (! isamb_pp_climb_desc( pp, untilbuf)) {
1538 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1539 "at level %d node %d ofs=%d sz=%d",
1540 pp->level, p->pos, p->offset, p->size);
1542 return 0; /* could not find a leaf */
1545 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1547 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1548 "at level %d node %d ofs=%d sz=%d",
1549 pp->level, p->pos, p->offset, p->size);
1553 }while ( isamb_pp_find_next_leaf(pp));
1554 return 0; /* could not find at all */
1556 else { /* no untilbuf, a straight read */
1557 /* FIXME - this should be moved
1558 * directly into the pp_read */
1559 /* keeping here now, to keep same
1560 * interface as the old fwd */
1561 if (isamb_pp_read_on_leaf( pp, buf)) {
1563 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1564 "at level %d node %d ofs=%d sz=%d",
1565 pp->level, p->pos, p->offset, p->size);
1569 if (isamb_pp_find_next_leaf(pp)) {
1571 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1572 "at level %d node %d ofs=%d sz=%d",
1573 pp->level, p->pos, p->offset, p->size);
1575 return isamb_pp_read_on_leaf(pp, buf);
1580 } /* isam_pp_forward (new version) */
1582 #elif NEW_FORWARD == 0
1584 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1588 * while at end of node
1589 * climb higher. If out, return 0
1590 * while not on a leaf (and not at its end)
1599 * The upper nodes consist of a sequence of nodenumbers and keys
1600 * When opening a block, the first node number is read in, and
1601 * offset points to the first key, which is the upper limit of keys
1602 * in the node just read.
1606 struct ISAMB_block *p = pp->block[pp->level];
1611 int descending=0; /* used to prevent a border condition error */
1615 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1617 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, untilbuf, "until");
1618 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "buf");
1623 while ( (p->offset == p->size) && !descending )
1624 { /* end of this block - climb higher */
1625 assert (p->offset <= p->size);
1627 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1633 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1635 return 0; /* at end of the root, nothing left */
1637 close_block(pp->isamb, pp->block[pp->level]);
1638 pp->block[pp->level]=0;
1640 p=pp->block[pp->level];
1642 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1646 assert(p->offset <= p->size);
1647 /* skip the child we have handled */
1648 if (p->offset != p->size)
1650 src = p->bytes + p->offset;
1651 decode_ptr(&src, &item_len);
1653 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src,
1654 " isamb_pp_forward "
1655 "climb skipping old key");
1658 decode_ptr(&src,&pos);
1659 p->offset = src - (char*) p->bytes;
1660 break; /* even if this puts us at the end of the block, we
1661 need to descend to the last pos. UGLY coding,
1662 clean up some day */
1667 src = p->bytes + p->offset;
1668 if (p->offset == p->size)
1669 cmp=-2 ; /* descend to the last node, as we have
1673 decode_ptr(&src, &item_len);
1675 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1676 "ofs=%d sz=%d nxtpos=%d ",
1677 p->offset,p->size,pos);
1678 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src, "");
1681 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1685 decode_ptr(&src,&nxtpos);
1690 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1693 descending=1; /* prevent climbing for a while */
1695 p = open_block(pp->isamb,pos);
1696 pp->block[pp->level] = p ;
1697 pp->total_size += p->size;
1698 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1701 { /* block starts with a pos */
1702 src = p->bytes + p->offset;
1703 decode_ptr(&src,&pos);
1704 p->offset=src-(char*) p->bytes;
1706 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1710 } /* descend to the node */
1712 { /* skip the node */
1713 p->offset = src - (char*) p->bytes;
1715 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1718 "isamb_pp_forward: skipping block on level %d, noting "
1720 pp->level, pp->maxlevel - pp->level-1 ,
1721 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1723 /* 0 is always leafs, 1 is one level above leafs etc, no
1724 * matter how high tree */
1726 } /* not on a leaf */
1729 if (p->offset == p->size) {
1734 assert (p->offset < p->size);
1735 src = p->bytes + p->offset;
1737 (*pp->isamb->method->codec.decode)(p->decodeClientData,
1739 p->offset = src - (char*) p->bytes;
1741 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1745 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1747 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "");
1754 (*pp->isamb->method->codec.log_item)(
1755 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1759 (*pp->isamb->method->codec.log_item)(
1760 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1763 pp->returned_numbers++;
1767 pp->skipped_numbers++;
1773 #elif NEW_FORWARD == 2
1775 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1779 struct ISAMB_block *p = pp->block[pp->level];
1784 while (p->offset == p->size)
1787 while (p->offset == p->size)
1791 close_block (pp->isamb, pp->block[pp->level]);
1792 pp->block[pp->level] = 0;
1794 p = pp->block[pp->level];
1799 src = p->bytes + p->offset;
1801 decode_ptr (&src, &item_len);
1803 decode_ptr (&src, &pos);
1805 p->offset = src - (char*) p->bytes;
1807 src = p->bytes + p->offset;
1811 if (!untilb || p->offset == p->size)
1813 assert(p->offset < p->size);
1814 decode_ptr (&src, &item_len);
1815 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1818 decode_ptr (&src, &pos);
1819 p->offset = src - (char*) p->bytes;
1826 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1828 pp->total_size += p->size;
1836 src = p->bytes + p->offset;
1839 decode_ptr (&src, &pos);
1840 p->offset = src - (char*) p->bytes;
1842 if (!untilb || p->offset == p->size)
1844 assert(p->offset < p->size);
1845 decode_ptr (&src, &item_len);
1846 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1853 assert (p->offset < p->size);
1858 src = p->bytes + p->offset;
1859 (*pp->isamb->method->codec.decode)(p->decodeClientData, &dst, &src);
1860 p->offset = src - (char*) p->bytes;
1861 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1864 if (p->offset == p->size) goto again;
1866 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1872 int isamb_pp_num (ISAMB_PP pp)
1874 assert(pp); /* shut up about unused arguments */
1878 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1879 double *current, double *total,
1882 struct ISAMB_block *p = pp->block[pp->level];
1883 const char *src=p->bytes;
1884 char *end=p->bytes+p->size;
1885 char *cur=p->bytes+p->offset;
1887 void *decodeClientData;
1888 assert(p->offset <= p->size);
1894 decodeClientData = (pp->isamb->method->codec.start)();
1898 (*pp->isamb->method->codec.decode)(decodeClientData,&dst, &src);
1899 assert(dst<(char*) dummybuf+100); /*FIXME */
1905 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur= %0.1f tot=%0.1f "
1906 " ofs=%d sz=%d lev=%d",
1907 *current, *total, p->offset, p->size, pp->level);
1910 (pp->isamb->method->codec.stop)(decodeClientData);
1913 static void isamb_pp_upper_pos( ISAMB_PP pp, double *current, double *total,
1914 double size, int level )
1915 { /* estimates total/current occurrences from here up, excl leaf */
1916 struct ISAMB_block *p = pp->block[level];
1917 const char *src=p->bytes;
1918 char *end=p->bytes+p->size;
1919 char *cur=p->bytes+p->offset;
1926 #if 1 // ISAMB_DEBUG
1927 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1928 "cur=%0.1f tot=%0.1f "
1929 " ofs=%d sz=%d pos=" ZINT_FORMAT,
1930 level, *current, *total, p->offset, p->size, p->pos);
1932 assert (p->offset <= p->size);
1933 decode_ptr (&src, &child ); /* first child */
1940 decode_ptr (&src, &item_size );
1941 assert(src+item_size<=end);
1943 decode_ptr (&src, &child );
1951 logf(LOG_DEBUG,"isamb_pp_upper_pos before recursion l=%d "
1952 "cur=%0.1f tot=%0.1f "
1953 " ofs=%d sz=%d pos=" ZINT_FORMAT,
1954 level, *current, *total, p->offset, p->size, p->pos);
1957 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1960 void isamb_pp_pos( ISAMB_PP pp, double *current, double *total )
1961 { /* return an estimate of the current position and of the total number of */
1962 /* occureences in the isam tree, based on the current leaf */
1963 /* FIXME - Isam-B ought to know how many we have, so we could return */
1965 struct ISAMB_block *p = pp->block[pp->level];
1966 char dummy[100]; /* 100 bytes/entry must be enough */
1970 isamb_pp_leaf_pos(pp,current, total, dummy);
1972 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1973 *current = (double) pp->returned_numbers;
1974 /* use the precise number, since we have it! */
1976 logf(LOG_LOG, "isamb_pp_pos returning: cur= %0.1f tot=%0.1f rn="ZINT_FORMAT,
1977 *current, *total, pp->returned_numbers);