1 /* $Id: isamb.c,v 1.55 2004-08-18 20:00:35 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;
713 char *lookahead_next;
717 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
721 dst_item = lookahead_item;
722 if (!*lookahead_mode)
724 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
725 assert (*lookahead_mode);
729 dst_item = file_item_buf;
730 if (!*lookahead_mode && d == 0)
734 else if (!half1 && dst > cut)
736 const char *dst_item_0 = dst_item;
737 half1 = dst; /* candidate for splitting */
739 (*b->method->codec.encode)(c2, &dst, &dst_item);
741 cut_item_size = dst_item - dst_item_0;
742 assert(cut_item_size > 0);
743 memcpy (cut_item_buf, dst_item_0, cut_item_size);
748 (*b->method->codec.encode)(c2, &dst, &dst_item);
755 lookahead_next = lookahead_item;
756 if (!(*stream->read_item)(stream->clientData,
763 if (lookahead_item && max_item &&
764 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
775 lookahead_next = lookahead_item;
776 if (!(*stream->read_item)(stream->clientData,
777 &lookahead_next, lookahead_mode))
784 file_item = file_item_buf;
785 (*b->method->codec.decode)(c1, &file_item, &src);
791 file_item = file_item_buf;
792 (*b->method->codec.decode)(c1, &file_item, &src);
796 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
797 while (lookahead_item)
800 const char *src = lookahead_item;
804 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
809 if (!*lookahead_mode)
811 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
814 else if (!half1 && dst > cut)
816 const char *src_0 = src;
817 half1 = dst; /* candidate for splitting */
819 (*b->method->codec.encode)(c2, &dst, &src);
821 cut_item_size = src - src_0;
822 assert(cut_item_size > 0);
823 memcpy (cut_item_buf, src_0, cut_item_size);
828 (*b->method->codec.encode)(c2, &dst, &src);
837 dst_item = lookahead_item;
838 if (!(*stream->read_item)(stream->clientData, &dst_item,
845 new_size = dst - dst_buf;
846 if (p && p->cat != b->no_cat-1 &&
847 new_size > b->file[p->cat].head.block_max)
849 /* non-btree block will be removed */
852 /* delete it too!! */
853 p = 0; /* make a new one anyway */
856 { /* must create a new one */
858 for (i = 0; i < b->no_cat; i++)
859 if (new_size <= b->file[i].head.block_max)
865 if (new_size > b->file[p->cat].head.block_max)
868 const char *cut_item = cut_item_buf;
873 assert(cut_item_size > 0);
876 p->size = half1 - dst_buf;
877 memcpy (p->bytes, dst_buf, half1 - dst_buf);
880 *sp2 = new_leaf (b, p->cat);
882 (*b->method->codec.reset)(c2);
884 first_dst = (*sp2)->bytes;
886 (*b->method->codec.encode)(c2, &first_dst, &cut_item);
888 memcpy (first_dst, half2, dst - half2);
890 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
893 memcpy (sub_item, cut_item_buf, cut_item_size);
894 *sub_size = cut_item_size;
898 memcpy (p->bytes, dst_buf, dst - dst_buf);
901 (*b->method->codec.stop)(c1);
902 (*b->method->codec.stop)(c2);
907 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
910 struct ISAMB_block **sp,
911 void *sub_item, int *sub_size,
912 const void *max_item)
914 if (!*p || (*p)->leaf)
915 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
918 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
922 int isamb_unlink (ISAMB b, ISAMC_P pos)
924 struct ISAMB_block *p1;
928 p1 = open_block(b, pos);
934 const char *src = p1->bytes + p1->offset;
936 decode_ptr(&src, &sub_p);
937 isamb_unlink(b, sub_p);
939 while (src != p1->bytes + p1->size)
941 decode_ptr(&src, &item_len);
943 decode_ptr(&src, &sub_p);
944 isamb_unlink(b, sub_p);
951 ISAMB_P isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
953 char item_buf[DST_ITEM_MAX];
965 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
970 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
973 struct ISAMB_block *p = 0, *sp = 0;
974 char sub_item[DST_ITEM_MAX];
978 p = open_block (b, pos);
979 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
980 sub_item, &sub_size, 0);
982 { /* increase level of tree by one */
983 struct ISAMB_block *p2 = new_int (b, p->cat);
984 char *dst = p2->bytes + p2->size;
986 encode_ptr (&dst, p->pos);
987 assert (sub_size < 40);
988 encode_ptr (&dst, sub_size);
989 memcpy (dst, sub_item, sub_size);
991 encode_ptr (&dst, sp->pos);
993 p2->size = dst - p2->bytes;
994 pos = p2->pos; /* return new super page */
999 pos = p->pos; /* return current one (again) */
1005 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
1007 ISAMB_PP pp = xmalloc (sizeof(*pp));
1011 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
1018 pp->skipped_numbers=0;
1019 pp->returned_numbers=0;
1020 for (i=0;i<ISAMB_MAX_LEVEL;i++)
1021 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
1024 struct ISAMB_block *p = open_block (isamb, pos);
1025 const char *src = p->bytes + p->offset;
1026 pp->block[pp->level] = p;
1028 pp->total_size += p->size;
1034 decode_ptr (&src, &pos);
1035 p->offset = src - p->bytes;
1037 pp->accessed_nodes[pp->level]++;
1039 pp->block[pp->level+1] = 0;
1040 pp->maxlevel=pp->level;
1046 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
1048 return isamb_pp_open_x (isamb, pos, 0);
1051 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
1056 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned "ZINT_FORMAT" values,"
1057 "skipped "ZINT_FORMAT,
1058 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
1059 for (i=pp->maxlevel;i>=0;i--)
1060 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
1061 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: "
1062 ZINT_FORMAT" read, "ZINT_FORMAT" skipped", i,
1063 pp->accessed_nodes[i], pp->skipped_nodes[i]);
1064 pp->isamb->skipped_numbers += pp->skipped_numbers;
1065 pp->isamb->returned_numbers += pp->returned_numbers;
1066 for (i=pp->maxlevel;i>=0;i--)
1068 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
1069 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
1072 *size = pp->total_size;
1074 *blocks = pp->no_blocks;
1075 for (i = 0; i <= pp->level; i++)
1076 close_block (pp->isamb, pp->block[i]);
1081 int isamb_block_info (ISAMB isamb, int cat)
1083 if (cat >= 0 && cat < isamb->no_cat)
1084 return isamb->file[cat].head.block_size;
1088 void isamb_pp_close (ISAMB_PP pp)
1090 isamb_pp_close_x (pp, 0, 0);
1093 /* simple recursive dumper .. */
1094 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1098 char prefix_str[1024];
1101 struct ISAMB_block *p = open_block (b, pos);
1102 sprintf(prefix_str, "%*s " ZINT_FORMAT " cat=%d size=%d max=%d", level*2, "",
1103 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1105 sprintf(prefix_str, "%*s " ZINT_FORMAT, level*2, "", pos);
1108 while (p->offset < p->size)
1110 const char *src = p->bytes + p->offset;
1112 (*b->method->codec.decode)(p->decodeClientData, &dst, &src);
1113 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1114 p->offset = src - (char*) p->bytes;
1116 assert(p->offset == p->size);
1120 const char *src = p->bytes + p->offset;
1124 decode_ptr (&src, &sub);
1125 p->offset = src - (char*) p->bytes;
1127 isamb_dump_r(b, sub, pr, level+1);
1129 while (p->offset < p->size)
1131 decode_ptr (&src, &item_len);
1132 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1134 decode_ptr (&src, &sub);
1136 p->offset = src - (char*) p->bytes;
1138 isamb_dump_r(b, sub, pr, level+1);
1145 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1147 isamb_dump_r(b, pos, pr, 0);
1151 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1152 debug the more complex pp_read that also forwards. May be deleted near end
1153 of 2004, if it has not shown to be useful */
1156 int isamb_pp_read (ISAMB_PP pp, void *buf)
1160 struct ISAMB_block *p = pp->block[pp->level];
1164 while (p->offset == p->size)
1167 while (p->offset == p->size)
1171 close_block (pp->isamb, pp->block[pp->level]);
1172 pp->block[pp->level] = 0;
1174 p = pp->block[pp->level];
1177 src = p->bytes + p->offset;
1179 decode_ptr (&src, &item_len);
1181 decode_ptr (&src, &pos);
1183 p->offset = src - (char*) p->bytes;
1189 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1191 pp->total_size += p->size;
1198 src = p->bytes + p->offset;
1199 decode_ptr (&src, &pos);
1200 p->offset = src - (char*) p->bytes;
1204 assert (p->offset < p->size);
1206 src = p->bytes + p->offset;
1207 (*pp->isamb->method->codec.code_item)(ISAMC_DECODE, p->decodeClientData,
1209 p->offset = src - (char*) p->bytes;
1210 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1215 int isamb_pp_read (ISAMB_PP pp, void *buf)
1217 return isamb_pp_forward(pp, buf, 0);
1221 #define NEW_FORWARD 1
1223 #if NEW_FORWARD == 1
1225 static int isamb_pp_on_right_node(ISAMB_PP pp, int level, const void *untilbuf)
1226 { /* looks one node higher to see if we should be on this node at all */
1227 /* useful in backing off quickly, and in avoiding tail descends */
1228 /* call with pp->level to begin with */
1229 struct ISAMB_block *p;
1236 logf(LOG_DEBUG,"isamb_pp_on_right returning true for root");
1238 return 1; /* we can never skip the root node */
1242 assert(p->offset <= p->size);
1243 if (p->offset < p->size )
1245 assert(p->offset>0);
1246 src=p->bytes + p->offset;
1247 decode_ptr(&src, &item_len);
1249 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,untilbuf,"on_leaf: until");
1250 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,src,"on_leaf: value");
1252 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1255 logf(LOG_DEBUG,"isamb_pp_on_right returning true "
1256 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1262 logf(LOG_DEBUG,"isamb_pp_on_right returning false "
1263 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1270 logf(LOG_DEBUG,"isamb_pp_on_right at tail, looking higher "
1273 return isamb_pp_on_right_node(pp, level, untilbuf);
1275 } /* isamb_pp_on_right_node */
1277 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1278 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1279 struct ISAMB_block *p = pp->block[pp->level];
1284 if (p->offset == p->size) {
1286 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1288 return 0; /* at end of leaf */
1290 src=p->bytes + p->offset;
1292 (*pp->isamb->method->codec.decode)(p->decodeClientData,&dst, &src);
1293 p->offset = src - (char*) p->bytes;
1296 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1299 pp->returned_numbers++;
1301 } /* read_on_leaf */
1303 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1304 { /* forwards on the current leaf, returns 0 if not found */
1308 if (!isamb_pp_read_on_leaf(pp,buf))
1310 /* FIXME - this is an extra function call, inline the read? */
1311 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1312 if (cmp <2){ /* found a good one */
1315 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1317 pp->returned_numbers++;
1321 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1322 return 0; /* never mind the rest of this leaf */
1323 pp->skipped_numbers++;
1326 } /* forward_on_leaf */
1328 static int isamb_pp_climb_level(ISAMB_PP pp, ISAMB_P *pos)
1329 { /* climbs higher in the tree, until finds a level with data left */
1330 /* returns the node to (consider to) descend to in *pos) */
1331 struct ISAMB_block *p = pp->block[pp->level];
1335 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1336 "at level %d node %d ofs=%d sz=%d",
1337 pp->level, p->pos, p->offset, p->size);
1339 assert(pp->level >= 0);
1340 assert(p->offset <= p->size);
1344 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1348 assert(pp->level>0);
1349 close_block(pp->isamb, pp->block[pp->level]);
1350 pp->block[pp->level]=0;
1352 p=pp->block[pp->level];
1354 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1355 pp->level, p->pos, p->offset);
1358 assert(p->offset <= p->size);
1359 if (p->offset == p->size ) {
1360 /* we came from the last pointer, climb on */
1361 if (!isamb_pp_climb_level(pp,pos))
1363 p=pp->block[pp->level];
1367 /* skip the child we just came from */
1369 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1370 pp->level, p->offset, p->size);
1372 assert (p->offset < p->size );
1373 src=p->bytes + p->offset;
1374 decode_ptr(&src, &item_len);
1376 decode_ptr(&src, pos);
1377 p->offset=src - (char *)p->bytes;
1384 static zint isamb_pp_forward_unode(ISAMB_PP pp, zint pos, const void *untilbuf)
1385 { /* scans a upper node until it finds a child <= untilbuf */
1386 /* pp points to the key value, as always. pos is the child read from */
1388 /* if all values are too small, returns the last child in the node */
1389 /* FIXME - this can be detected, and avoided by looking at the */
1390 /* parent node, but that gets messy. Presumably the cost is */
1391 /* pretty low anyway */
1392 struct ISAMB_block *p = pp->block[pp->level];
1393 const char *src=p->bytes + p->offset;
1399 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1400 "at level %d node %d ofs=%di sz=%d",
1401 pp->level, p->pos, p->offset, p->size);
1404 assert(p->offset <= p->size);
1405 if (p->offset == p->size) {
1407 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1408 "at level %d node %d ofs=%di sz=%d",
1409 pp->level, p->pos, p->offset, p->size);
1411 return pos; /* already at the end of it */
1413 while(p->offset < p->size) {
1414 decode_ptr(&src,&item_len);
1415 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1417 decode_ptr(&src,&nxtpos);
1421 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1422 "at level %d node %d ofs=%d sz=%d",
1423 pp->level, p->pos, p->offset, p->size);
1428 p->offset=src-(char*)p->bytes;
1429 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1435 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1436 "at level %d node %d ofs=%d sz=%d skips=%d",
1437 pp->level, p->pos, p->offset, p->size, skips);
1439 return pos; /* that's the last one in the line */
1441 } /* forward_unode */
1443 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, ISAMB_P pos, const void *untilbuf)
1444 { /* climbs down the tree, from pos, to the leftmost leaf */
1445 struct ISAMB_block *p = pp->block[pp->level];
1449 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1450 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1451 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1454 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1457 p=open_block(pp->isamb, pos);
1458 pp->block[pp->level]=p;
1459 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1462 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1463 "got lev %d node %d lf=%d",
1464 pp->level, p->pos, p->leaf);
1468 assert (p->offset==0 );
1469 src=p->bytes + p->offset;
1470 decode_ptr(&src, &pos);
1471 p->offset=src-(char*)p->bytes;
1472 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1474 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1475 "returning at lev %d node %d ofs=%d lf=%d",
1476 pp->level, p->pos, p->offset, p->leaf);
1478 } /* descend_to_leaf */
1480 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1481 { /* finds the next leaf by climbing up and down */
1483 if (!isamb_pp_climb_level(pp,&pos))
1485 isamb_pp_descend_to_leaf(pp, pos,0);
1489 static int isamb_pp_climb_desc(ISAMB_PP pp, const void *untilbuf)
1490 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1493 struct ISAMB_block *p = pp->block[pp->level];
1494 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1495 "at level %d node %d ofs=%d sz=%d",
1496 pp->level, p->pos, p->offset, p->size);
1498 if (!isamb_pp_climb_level(pp,&pos))
1500 /* see if it would pay to climb one higher */
1501 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1502 if (!isamb_pp_climb_level(pp,&pos))
1504 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1506 p = pp->block[pp->level];
1507 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1508 "at level %d node %d ofs=%d sz=%d",
1509 pp->level, p->pos, p->offset, p->size);
1514 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1517 struct ISAMB_block *p = pp->block[pp->level];
1519 logf(LOG_DEBUG,"isamb_pp_forward starting "
1520 "at level %d node %d ofs=%d sz=%d u=%p",
1521 pp->level, p->pos, p->offset, p->size,untilbuf);
1524 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1526 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1527 "at level %d node %d ofs=%d sz=%d",
1528 pp->level, p->pos, p->offset, p->size);
1532 if (! isamb_pp_climb_desc( pp, untilbuf)) {
1534 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1535 "at level %d node %d ofs=%d sz=%d",
1536 pp->level, p->pos, p->offset, p->size);
1538 return 0; /* could not find a leaf */
1541 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1543 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1544 "at level %d node %d ofs=%d sz=%d",
1545 pp->level, p->pos, p->offset, p->size);
1549 }while ( isamb_pp_find_next_leaf(pp));
1550 return 0; /* could not find at all */
1552 else { /* no untilbuf, a straight read */
1553 /* FIXME - this should be moved
1554 * directly into the pp_read */
1555 /* keeping here now, to keep same
1556 * interface as the old fwd */
1557 if (isamb_pp_read_on_leaf( pp, buf)) {
1559 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1560 "at level %d node %d ofs=%d sz=%d",
1561 pp->level, p->pos, p->offset, p->size);
1565 if (isamb_pp_find_next_leaf(pp)) {
1567 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1568 "at level %d node %d ofs=%d sz=%d",
1569 pp->level, p->pos, p->offset, p->size);
1571 return isamb_pp_read_on_leaf(pp, buf);
1576 } /* isam_pp_forward (new version) */
1578 #elif NEW_FORWARD == 0
1580 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1584 * while at end of node
1585 * climb higher. If out, return 0
1586 * while not on a leaf (and not at its end)
1595 * The upper nodes consist of a sequence of nodenumbers and keys
1596 * When opening a block, the first node number is read in, and
1597 * offset points to the first key, which is the upper limit of keys
1598 * in the node just read.
1602 struct ISAMB_block *p = pp->block[pp->level];
1607 int descending=0; /* used to prevent a border condition error */
1611 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1613 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, untilbuf, "until");
1614 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "buf");
1619 while ( (p->offset == p->size) && !descending )
1620 { /* end of this block - climb higher */
1621 assert (p->offset <= p->size);
1623 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1629 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1631 return 0; /* at end of the root, nothing left */
1633 close_block(pp->isamb, pp->block[pp->level]);
1634 pp->block[pp->level]=0;
1636 p=pp->block[pp->level];
1638 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1642 assert(p->offset <= p->size);
1643 /* skip the child we have handled */
1644 if (p->offset != p->size)
1646 src = p->bytes + p->offset;
1647 decode_ptr(&src, &item_len);
1649 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src,
1650 " isamb_pp_forward "
1651 "climb skipping old key");
1654 decode_ptr(&src,&pos);
1655 p->offset = src - (char*) p->bytes;
1656 break; /* even if this puts us at the end of the block, we
1657 need to descend to the last pos. UGLY coding,
1658 clean up some day */
1663 src = p->bytes + p->offset;
1664 if (p->offset == p->size)
1665 cmp=-2 ; /* descend to the last node, as we have
1669 decode_ptr(&src, &item_len);
1671 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1672 "ofs=%d sz=%d nxtpos=%d ",
1673 p->offset,p->size,pos);
1674 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src, "");
1677 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1681 decode_ptr(&src,&nxtpos);
1686 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1689 descending=1; /* prevent climbing for a while */
1691 p = open_block(pp->isamb,pos);
1692 pp->block[pp->level] = p ;
1693 pp->total_size += p->size;
1694 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1697 { /* block starts with a pos */
1698 src = p->bytes + p->offset;
1699 decode_ptr(&src,&pos);
1700 p->offset=src-(char*) p->bytes;
1702 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1706 } /* descend to the node */
1708 { /* skip the node */
1709 p->offset = src - (char*) p->bytes;
1711 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1714 "isamb_pp_forward: skipping block on level %d, noting "
1716 pp->level, pp->maxlevel - pp->level-1 ,
1717 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1719 /* 0 is always leafs, 1 is one level above leafs etc, no
1720 * matter how high tree */
1722 } /* not on a leaf */
1725 if (p->offset == p->size) {
1730 assert (p->offset < p->size);
1731 src = p->bytes + p->offset;
1733 (*pp->isamb->method->codec.decode)(p->decodeClientData,
1735 p->offset = src - (char*) p->bytes;
1737 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1741 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1743 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "");
1750 (*pp->isamb->method->codec.log_item)(
1751 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1755 (*pp->isamb->method->codec.log_item)(
1756 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1759 pp->returned_numbers++;
1763 pp->skipped_numbers++;
1769 #elif NEW_FORWARD == 2
1771 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1775 struct ISAMB_block *p = pp->block[pp->level];
1780 while (p->offset == p->size)
1783 while (p->offset == p->size)
1787 close_block (pp->isamb, pp->block[pp->level]);
1788 pp->block[pp->level] = 0;
1790 p = pp->block[pp->level];
1795 src = p->bytes + p->offset;
1797 decode_ptr (&src, &item_len);
1799 decode_ptr (&src, &pos);
1801 p->offset = src - (char*) p->bytes;
1803 src = p->bytes + p->offset;
1807 if (!untilb || p->offset == p->size)
1809 assert(p->offset < p->size);
1810 decode_ptr (&src, &item_len);
1811 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1814 decode_ptr (&src, &pos);
1815 p->offset = src - (char*) p->bytes;
1822 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1824 pp->total_size += p->size;
1832 src = p->bytes + p->offset;
1835 decode_ptr (&src, &pos);
1836 p->offset = src - (char*) p->bytes;
1838 if (!untilb || p->offset == p->size)
1840 assert(p->offset < p->size);
1841 decode_ptr (&src, &item_len);
1842 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1849 assert (p->offset < p->size);
1854 src = p->bytes + p->offset;
1855 (*pp->isamb->method->codec.decode)(p->decodeClientData, &dst, &src);
1856 p->offset = src - (char*) p->bytes;
1857 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1860 if (p->offset == p->size) goto again;
1862 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1868 int isamb_pp_num (ISAMB_PP pp)
1870 assert(pp); /* shut up about unused arguments */
1874 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1875 double *current, double *total,
1878 struct ISAMB_block *p = pp->block[pp->level];
1879 const char *src=p->bytes;
1880 char *end=p->bytes+p->size;
1881 char *cur=p->bytes+p->offset;
1883 void *decodeClientData;
1884 assert(p->offset <= p->size);
1890 decodeClientData = (pp->isamb->method->codec.start)();
1894 (*pp->isamb->method->codec.decode)(decodeClientData,&dst, &src);
1895 assert(dst<(char*) dummybuf+100); /*FIXME */
1901 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur= %0.1f tot=%0.1f "
1902 " ofs=%d sz=%d lev=%d",
1903 *current, *total, p->offset, p->size, pp->level);
1906 (pp->isamb->method->codec.stop)(decodeClientData);
1909 static void isamb_pp_upper_pos( ISAMB_PP pp, double *current, double *total,
1910 double size, int level )
1911 { /* estimates total/current occurrences from here up, excl leaf */
1912 struct ISAMB_block *p = pp->block[level];
1913 const char *src=p->bytes;
1914 char *end=p->bytes+p->size;
1915 char *cur=p->bytes+p->offset;
1922 #if 1 // ISAMB_DEBUG
1923 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1924 "cur=%0.1f tot=%0.1f "
1925 " ofs=%d sz=%d pos=" ZINT_FORMAT,
1926 level, *current, *total, p->offset, p->size, p->pos);
1928 assert (p->offset <= p->size);
1929 decode_ptr (&src, &child ); /* first child */
1936 decode_ptr (&src, &item_size );
1937 assert(src+item_size<=end);
1939 decode_ptr (&src, &child );
1947 logf(LOG_DEBUG,"isamb_pp_upper_pos before recursion l=%d "
1948 "cur=%0.1f tot=%0.1f "
1949 " ofs=%d sz=%d pos=" ZINT_FORMAT,
1950 level, *current, *total, p->offset, p->size, p->pos);
1953 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1956 void isamb_pp_pos( ISAMB_PP pp, double *current, double *total )
1957 { /* return an estimate of the current position and of the total number of */
1958 /* occureences in the isam tree, based on the current leaf */
1959 /* FIXME - Isam-B ought to know how many we have, so we could return */
1961 struct ISAMB_block *p = pp->block[pp->level];
1962 char dummy[100]; /* 100 bytes/entry must be enough */
1966 isamb_pp_leaf_pos(pp,current, total, dummy);
1968 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1969 *current = (double) pp->returned_numbers;
1970 /* use the precise number, since we have it! */
1972 logf(LOG_LOG, "isamb_pp_pos returning: cur= %0.1f tot=%0.1f rn="ZINT_FORMAT,
1973 *current, *total, pp->returned_numbers);