1 /* $Id: isamb.c,v 1.53 2004-08-10 08:54:40 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include <yaz/xmalloc.h>
41 #define ISAMB_DATA_OFFSET 3
43 /* maximum size of encoded buffer */
44 #define DST_ITEM_MAX 256
46 #define ISAMB_MAX_LEVEL 10
47 /* approx 2*max page + max size of item */
48 #define DST_BUF_SIZE 16840
50 #define ISAMB_CACHE_ENTRY_SIZE 4096
52 /* CAT_MAX: _must_ be power of 2 */
54 #define CAT_MASK (CAT_MAX-1)
55 /* CAT_NO: <= CAT_MAX */
58 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
59 #define ISAMB_PTR_CODEC 0
61 struct ISAMB_cache_entry {
66 struct ISAMB_cache_entry *next;
72 struct ISAMB_head head;
73 struct ISAMB_cache_entry *cache_entries;
80 struct ISAMB_file *file;
82 int cache; /* 0=no cache, 1=use cache, -1=dummy isam (for testing only) */
83 int log_io; /* log level for bf_read/bf_write calls */
84 int log_freelist; /* log level for freelist handling */
85 zint skipped_numbers; /* on a leaf node */
86 zint returned_numbers;
87 zint skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
88 zint accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
102 void *decodeClientData;
110 int maxlevel; /* total depth */
113 zint skipped_numbers; /* on a leaf node */
114 zint returned_numbers;
115 zint skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
116 zint 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 = (int) (pos&CAT_MASK);
239 int off = (int) (((pos/CAT_MAX) &
240 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
241 * b->file[cat].head.block_size);
242 zint 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: "ZINT_FORMAT" read, "
314 ZINT_FORMAT" skipped",
315 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
316 logf(LOG_DEBUG,"isamb_close returned "ZINT_FORMAT" values, "
317 "skipped "ZINT_FORMAT,
318 isamb->skipped_numbers, isamb->returned_numbers);
319 for (i = 0; i<isamb->no_cat; i++)
321 flush_blocks (isamb, i);
322 if (isamb->file[i].head_dirty)
323 bf_write (isamb->file[i].bf, 0, 0,
324 sizeof(struct ISAMB_head), &isamb->file[i].head);
326 bf_close (isamb->file[i].bf);
329 xfree (isamb->method);
333 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
335 int cat = (int) (pos&CAT_MASK);
336 struct ISAMB_block *p;
339 p = xmalloc (sizeof(*p));
341 p->cat = (int) (pos & CAT_MASK);
342 p->buf = xmalloc (b->file[cat].head.block_size);
345 if (!get_block (b, pos, p->buf, 0))
347 yaz_log (b->log_io, "bf_read: open_block");
348 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
350 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
351 (long) pos, (long) pos/CAT_MAX);
355 p->bytes = p->buf + ISAMB_DATA_OFFSET;
357 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
360 yaz_log (LOG_FATAL, "Bad block size %d in pos=" ZINT_FORMAT "\n",
363 assert (p->size >= 0);
367 p->decodeClientData = (*b->method->codec.start)();
368 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block " ZINT_FORMAT " ofs=%d",pos, p->offset);
372 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
374 struct ISAMB_block *p;
376 p = xmalloc (sizeof(*p));
377 p->buf = xmalloc (b->file[cat].head.block_size);
379 if (!b->file[cat].head.free_list)
382 block_no = b->file[cat].head.last_block++;
383 p->pos = block_no * CAT_MAX + cat;
387 p->pos = b->file[cat].head.free_list;
388 assert((p->pos & CAT_MASK) == cat);
389 if (!get_block (b, p->pos, p->buf, 0))
391 yaz_log (b->log_io, "bf_read: new_block");
392 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
394 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
395 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
399 yaz_log (b->log_freelist, "got block " ZINT_FORMAT " from freelist %d:" ZINT_FORMAT, p->pos,
400 cat, p->pos/CAT_MAX);
401 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
404 b->file[cat].head_dirty = 1;
405 memset (p->buf, 0, b->file[cat].head.block_size);
406 p->bytes = p->buf + ISAMB_DATA_OFFSET;
412 p->decodeClientData = (*b->method->codec.start)();
416 struct ISAMB_block *new_leaf (ISAMB b, int cat)
418 return new_block (b, 1, cat);
422 struct ISAMB_block *new_int (ISAMB b, int cat)
424 return new_block (b, 0, cat);
427 static void check_block (ISAMB b, struct ISAMB_block *p)
429 assert(b); /* mostly to make the compiler shut up about unused b */
437 char *startp = p->bytes;
438 const char *src = startp;
439 char *endp = p->bytes + p->size;
442 decode_ptr (&src, &pos);
443 assert ((pos&CAT_MASK) == p->cat);
447 decode_ptr (&src, &item_len);
448 assert (item_len > 0 && item_len < 80);
450 decode_ptr (&src, &pos);
451 assert ((pos&CAT_MASK) == p->cat);
456 void close_block (ISAMB b, struct ISAMB_block *p)
462 yaz_log (b->log_freelist, "release block " ZINT_FORMAT " from freelist %d:" ZINT_FORMAT,
463 p->pos, p->cat, p->pos/CAT_MAX);
464 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
465 b->file[p->cat].head.free_list = p->pos;
466 if (!get_block (b, p->pos, p->buf, 1))
468 yaz_log (b->log_io, "bf_write: close_block (deleted)");
469 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
474 int size = p->size + ISAMB_DATA_OFFSET;
475 assert (p->size >= 0);
477 p->buf[1] = size & 255;
478 p->buf[2] = size >> 8;
480 if (!get_block (b, p->pos, p->buf, 1))
482 yaz_log (b->log_io, "bf_write: close_block");
483 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
486 (*b->method->codec.stop)(p->decodeClientData);
491 int insert_sub (ISAMB b, struct ISAMB_block **p,
492 void *new_item, int *mode,
494 struct ISAMB_block **sp,
495 void *sub_item, int *sub_size,
496 const void *max_item);
498 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
500 ISAMC_I *stream, struct ISAMB_block **sp,
501 void *split_item, int *split_size, const void *last_max_item)
503 char *startp = p->bytes;
504 const char *src = startp;
505 char *endp = p->bytes + p->size;
507 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
508 char sub_item[DST_ITEM_MAX];
514 assert(p->size >= 0);
515 decode_ptr (&src, &pos);
520 const char *src0 = src;
521 decode_ptr (&src, &item_len);
522 d = (*b->method->compare_item)(src, lookahead_item);
525 sub_p1 = open_block (b, pos);
527 more = insert_sub (b, &sub_p1, lookahead_item, mode,
529 sub_item, &sub_size, src);
534 decode_ptr (&src, &pos);
538 sub_p1 = open_block (b, pos);
540 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
541 sub_item, &sub_size, last_max_item);
545 /* there was a split - must insert pointer in this one */
546 char dst_buf[DST_BUF_SIZE];
549 assert (sub_size < 80 && sub_size > 1);
551 memcpy (dst, startp, src - startp);
555 encode_ptr (&dst, sub_size); /* sub length and item */
556 memcpy (dst, sub_item, sub_size);
559 encode_ptr (&dst, sub_p2->pos); /* pos */
561 if (endp - src) /* remaining data */
563 memcpy (dst, src, endp - src);
566 p->size = dst - dst_buf;
567 assert (p->size >= 0);
568 if (p->size <= b->file[p->cat].head.block_max)
570 memcpy (startp, dst_buf, dst - dst_buf);
580 half = src + b->file[p->cat].head.block_size/2;
581 decode_ptr (&src, &pos);
584 decode_ptr (&src, &split_size_tmp);
585 *split_size = (int) split_size_tmp;
588 decode_ptr (&src, &pos);
590 p_new_size = src - dst_buf;
591 memcpy (p->bytes, dst_buf, p_new_size);
593 decode_ptr (&src, &split_size_tmp);
594 *split_size = (int) split_size_tmp;
595 memcpy (split_item, src, *split_size);
598 *sp = new_int (b, p->cat);
599 (*sp)->size = endp - src;
600 memcpy ((*sp)->bytes, src, (*sp)->size);
602 p->size = p_new_size;
605 close_block (b, sub_p2);
607 close_block (b, sub_p1);
611 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
612 int *lookahead_mode, ISAMC_I *stream,
613 struct ISAMB_block **sp2,
614 void *sub_item, int *sub_size,
615 const void *max_item)
617 struct ISAMB_block *p = *sp1;
620 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
622 void *c1 = (*b->method->codec.start)();
623 void *c2 = (*b->method->codec.start)();
625 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
626 char *cut = dst_buf + quater * 2;
627 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
630 char cut_item_buf[DST_ITEM_MAX];
631 int cut_item_size = 0;
635 char file_item_buf[DST_ITEM_MAX];
636 char *file_item = file_item_buf;
639 endp = p->bytes + p->size;
640 (*b->method->codec.decode)(c1, &file_item, &src);
643 const char *dst_item = 0;
645 char *lookahead_next;
649 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
653 dst_item = lookahead_item;
654 if (!*lookahead_mode)
656 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
657 assert (*lookahead_mode);
661 dst_item = file_item_buf;
662 if (!*lookahead_mode && d == 0)
666 else if (!half1 && dst > cut)
668 const char *dst_item_0 = dst_item;
669 half1 = dst; /* candidate for splitting */
671 (*b->method->codec.encode)(c2, &dst, &dst_item);
673 cut_item_size = dst_item - dst_item_0;
674 assert(cut_item_size > 0);
675 memcpy (cut_item_buf, dst_item_0, cut_item_size);
680 (*b->method->codec.encode)(c2, &dst, &dst_item);
690 lookahead_next = lookahead_item;
691 if (!(*stream->read_item)(stream->clientData,
698 if (lookahead_item && max_item &&
699 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
710 lookahead_next = lookahead_item;
711 if (!(*stream->read_item)(stream->clientData,
712 &lookahead_next, lookahead_mode))
719 file_item = file_item_buf;
720 (*b->method->codec.decode)(c1, &file_item, &src);
726 file_item = file_item_buf;
727 (*b->method->codec.decode)(c1, &file_item, &src);
731 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
732 while (lookahead_item)
735 const char *src = lookahead_item;
739 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
744 if (!*lookahead_mode)
746 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
749 else if (!half1 && dst > cut)
751 const char *src_0 = src;
752 half1 = dst; /* candidate for splitting */
754 (*b->method->codec.encode)(c2, &dst, &src);
756 cut_item_size = src - src_0;
757 assert(cut_item_size > 0);
758 memcpy (cut_item_buf, src_0, cut_item_size);
763 (*b->method->codec.encode)(c2, &dst, &src);
772 dst_item = lookahead_item;
773 if (!(*stream->read_item)(stream->clientData, &dst_item,
780 new_size = dst - dst_buf;
781 if (p && p->cat != b->no_cat-1 &&
782 new_size > b->file[p->cat].head.block_max)
784 /* non-btree block will be removed */
787 /* delete it too!! */
788 p = 0; /* make a new one anyway */
791 { /* must create a new one */
793 for (i = 0; i < b->no_cat; i++)
794 if (new_size <= b->file[i].head.block_max)
800 if (new_size > b->file[p->cat].head.block_max)
803 const char *cut_item = cut_item_buf;
808 assert(cut_item_size > 0);
811 p->size = half1 - dst_buf;
812 memcpy (p->bytes, dst_buf, half1 - dst_buf);
815 *sp2 = new_leaf (b, p->cat);
817 (*b->method->codec.reset)(c2);
819 first_dst = (*sp2)->bytes;
821 (*b->method->codec.encode)(c2, &first_dst, &cut_item);
823 memcpy (first_dst, half2, dst - half2);
825 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
828 memcpy (sub_item, cut_item_buf, cut_item_size);
829 *sub_size = cut_item_size;
833 memcpy (p->bytes, dst_buf, dst - dst_buf);
836 (*b->method->codec.stop)(c1);
837 (*b->method->codec.stop)(c2);
842 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
845 struct ISAMB_block **sp,
846 void *sub_item, int *sub_size,
847 const void *max_item)
849 if (!*p || (*p)->leaf)
850 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
853 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
857 int isamb_unlink (ISAMB b, ISAMC_P pos)
859 struct ISAMB_block *p1;
863 p1 = open_block(b, pos);
869 const char *src = p1->bytes + p1->offset;
871 decode_ptr(&src, &sub_p);
872 isamb_unlink(b, sub_p);
874 while (src != p1->bytes + p1->size)
876 decode_ptr(&src, &item_len);
878 decode_ptr(&src, &sub_p);
879 isamb_unlink(b, sub_p);
886 ISAMB_P isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
888 char item_buf[DST_ITEM_MAX];
900 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
905 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
908 struct ISAMB_block *p = 0, *sp = 0;
909 char sub_item[DST_ITEM_MAX];
913 p = open_block (b, pos);
914 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
915 sub_item, &sub_size, 0);
917 { /* increase level of tree by one */
918 struct ISAMB_block *p2 = new_int (b, p->cat);
919 char *dst = p2->bytes + p2->size;
921 encode_ptr (&dst, p->pos);
922 assert (sub_size < 40);
923 encode_ptr (&dst, sub_size);
924 memcpy (dst, sub_item, sub_size);
926 encode_ptr (&dst, sp->pos);
928 p2->size = dst - p2->bytes;
929 pos = p2->pos; /* return new super page */
934 pos = p->pos; /* return current one (again) */
940 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
942 ISAMB_PP pp = xmalloc (sizeof(*pp));
946 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
953 pp->skipped_numbers=0;
954 pp->returned_numbers=0;
955 for (i=0;i<ISAMB_MAX_LEVEL;i++)
956 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
959 struct ISAMB_block *p = open_block (isamb, pos);
960 const char *src = p->bytes + p->offset;
961 pp->block[pp->level] = p;
963 pp->total_size += p->size;
969 decode_ptr (&src, &pos);
970 p->offset = src - p->bytes;
972 pp->accessed_nodes[pp->level]++;
974 pp->block[pp->level+1] = 0;
975 pp->maxlevel=pp->level;
981 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
983 return isamb_pp_open_x (isamb, pos, 0);
986 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
991 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned "ZINT_FORMAT" values,"
992 "skipped "ZINT_FORMAT,
993 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
994 for (i=pp->maxlevel;i>=0;i--)
995 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
996 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: "
997 ZINT_FORMAT" read, "ZINT_FORMAT" skipped", i,
998 pp->accessed_nodes[i], pp->skipped_nodes[i]);
999 pp->isamb->skipped_numbers += pp->skipped_numbers;
1000 pp->isamb->returned_numbers += pp->returned_numbers;
1001 for (i=pp->maxlevel;i>=0;i--)
1003 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
1004 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
1007 *size = pp->total_size;
1009 *blocks = pp->no_blocks;
1010 for (i = 0; i <= pp->level; i++)
1011 close_block (pp->isamb, pp->block[i]);
1016 int isamb_block_info (ISAMB isamb, int cat)
1018 if (cat >= 0 && cat < isamb->no_cat)
1019 return isamb->file[cat].head.block_size;
1023 void isamb_pp_close (ISAMB_PP pp)
1025 isamb_pp_close_x (pp, 0, 0);
1028 /* simple recursive dumper .. */
1029 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1033 char prefix_str[1024];
1036 struct ISAMB_block *p = open_block (b, pos);
1037 sprintf(prefix_str, "%*s " ZINT_FORMAT " cat=%d size=%d max=%d", level*2, "",
1038 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1040 sprintf(prefix_str, "%*s " ZINT_FORMAT, level*2, "", pos);
1043 while (p->offset < p->size)
1045 const char *src = p->bytes + p->offset;
1047 (*b->method->codec.decode)(p->decodeClientData, &dst, &src);
1048 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1049 p->offset = src - (char*) p->bytes;
1051 assert(p->offset == p->size);
1055 const char *src = p->bytes + p->offset;
1059 decode_ptr (&src, &sub);
1060 p->offset = src - (char*) p->bytes;
1062 isamb_dump_r(b, sub, pr, level+1);
1064 while (p->offset < p->size)
1066 decode_ptr (&src, &item_len);
1067 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1069 decode_ptr (&src, &sub);
1071 p->offset = src - (char*) p->bytes;
1073 isamb_dump_r(b, sub, pr, level+1);
1080 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1082 isamb_dump_r(b, pos, pr, 0);
1086 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1087 debug the more complex pp_read that also forwards. May be deleted near end
1088 of 2004, if it has not shown to be useful */
1091 int isamb_pp_read (ISAMB_PP pp, void *buf)
1095 struct ISAMB_block *p = pp->block[pp->level];
1099 while (p->offset == p->size)
1102 while (p->offset == p->size)
1106 close_block (pp->isamb, pp->block[pp->level]);
1107 pp->block[pp->level] = 0;
1109 p = pp->block[pp->level];
1112 src = p->bytes + p->offset;
1114 decode_ptr (&src, &item_len);
1116 decode_ptr (&src, &pos);
1118 p->offset = src - (char*) p->bytes;
1124 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1126 pp->total_size += p->size;
1133 src = p->bytes + p->offset;
1134 decode_ptr (&src, &pos);
1135 p->offset = src - (char*) p->bytes;
1139 assert (p->offset < p->size);
1141 src = p->bytes + p->offset;
1142 (*pp->isamb->method->codec.code_item)(ISAMC_DECODE, p->decodeClientData,
1144 p->offset = src - (char*) p->bytes;
1145 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1150 int isamb_pp_read (ISAMB_PP pp, void *buf)
1152 return isamb_pp_forward(pp, buf, 0);
1156 #define NEW_FORWARD 1
1158 #if NEW_FORWARD == 1
1160 static int isamb_pp_on_right_node(ISAMB_PP pp, int level, const void *untilbuf)
1161 { /* looks one node higher to see if we should be on this node at all */
1162 /* useful in backing off quickly, and in avoiding tail descends */
1163 /* call with pp->level to begin with */
1164 struct ISAMB_block *p;
1171 logf(LOG_DEBUG,"isamb_pp_on_right returning true for root");
1173 return 1; /* we can never skip the root node */
1177 assert(p->offset <= p->size);
1178 if (p->offset < p->size )
1180 assert(p->offset>0);
1181 src=p->bytes + p->offset;
1182 decode_ptr(&src, &item_len);
1184 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,untilbuf,"on_leaf: until");
1185 (*pp->isamb->method->codec.log_item)(LOG_DEBUG,src,"on_leaf: value");
1187 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1190 logf(LOG_DEBUG,"isamb_pp_on_right returning true "
1191 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1197 logf(LOG_DEBUG,"isamb_pp_on_right returning false "
1198 "cmp=%d lev=%d ofs=%d",cmp,level,p->offset);
1205 logf(LOG_DEBUG,"isamb_pp_on_right at tail, looking higher "
1208 return isamb_pp_on_right_node(pp, level, untilbuf);
1210 } /* isamb_pp_on_right_node */
1212 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1213 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1214 struct ISAMB_block *p = pp->block[pp->level];
1219 if (p->offset == p->size) {
1221 logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1223 return 0; /* at end of leaf */
1225 src=p->bytes + p->offset;
1227 (*pp->isamb->method->codec.decode)(p->decodeClientData,&dst, &src);
1228 p->offset = src - (char*) p->bytes;
1231 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1234 pp->returned_numbers++;
1236 } /* read_on_leaf */
1238 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1239 { /* forwards on the current leaf, returns 0 if not found */
1243 if (!isamb_pp_read_on_leaf(pp,buf))
1245 /* FIXME - this is an extra function call, inline the read? */
1246 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1247 if (cmp <2){ /* found a good one */
1250 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1252 pp->returned_numbers++;
1256 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1257 return 0; /* never mind the rest of this leaf */
1258 pp->skipped_numbers++;
1261 } /* forward_on_leaf */
1263 static int isamb_pp_climb_level(ISAMB_PP pp, ISAMB_P *pos)
1264 { /* climbs higher in the tree, until finds a level with data left */
1265 /* returns the node to (consider to) descend to in *pos) */
1266 struct ISAMB_block *p = pp->block[pp->level];
1270 logf(LOG_DEBUG,"isamb_pp_climb_level starting "
1271 "at level %d node %d ofs=%d sz=%d",
1272 pp->level, p->pos, p->offset, p->size);
1274 assert(pp->level >= 0);
1275 assert(p->offset <= p->size);
1279 logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1283 assert(pp->level>0);
1284 close_block(pp->isamb, pp->block[pp->level]);
1285 pp->block[pp->level]=0;
1287 p=pp->block[pp->level];
1289 logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1290 pp->level, p->pos, p->offset);
1293 assert(p->offset <= p->size);
1294 if (p->offset == p->size ) {
1295 /* we came from the last pointer, climb on */
1296 if (!isamb_pp_climb_level(pp,pos))
1298 p=pp->block[pp->level];
1302 /* skip the child we just came from */
1304 logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d",
1305 pp->level, p->offset, p->size);
1307 assert (p->offset < p->size );
1308 src=p->bytes + p->offset;
1309 decode_ptr(&src, &item_len);
1311 decode_ptr(&src, pos);
1312 p->offset=src - (char *)p->bytes;
1319 static zint isamb_pp_forward_unode(ISAMB_PP pp, zint pos, const void *untilbuf)
1320 { /* scans a upper node until it finds a child <= untilbuf */
1321 /* pp points to the key value, as always. pos is the child read from */
1323 /* if all values are too small, returns the last child in the node */
1324 /* FIXME - this can be detected, and avoided by looking at the */
1325 /* parent node, but that gets messy. Presumably the cost is */
1326 /* pretty low anyway */
1327 struct ISAMB_block *p = pp->block[pp->level];
1328 const char *src=p->bytes + p->offset;
1334 logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1335 "at level %d node %d ofs=%di sz=%d",
1336 pp->level, p->pos, p->offset, p->size);
1339 assert(p->offset <= p->size);
1340 if (p->offset == p->size) {
1342 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1343 "at level %d node %d ofs=%di sz=%d",
1344 pp->level, p->pos, p->offset, p->size);
1346 return pos; /* already at the end of it */
1348 while(p->offset < p->size) {
1349 decode_ptr(&src,&item_len);
1350 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1352 decode_ptr(&src,&nxtpos);
1356 logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1357 "at level %d node %d ofs=%d sz=%d",
1358 pp->level, p->pos, p->offset, p->size);
1363 p->offset=src-(char*)p->bytes;
1364 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1370 logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1371 "at level %d node %d ofs=%d sz=%d skips=%d",
1372 pp->level, p->pos, p->offset, p->size, skips);
1374 return pos; /* that's the last one in the line */
1376 } /* forward_unode */
1378 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, ISAMB_P pos, const void *untilbuf)
1379 { /* climbs down the tree, from pos, to the leftmost leaf */
1380 struct ISAMB_block *p = pp->block[pp->level];
1384 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1385 "starting at lev %d node %d ofs=%d lf=%d u=%p",
1386 pp->level, p->pos, p->offset, p->leaf, untilbuf);
1389 pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1392 p=open_block(pp->isamb, pos);
1393 pp->block[pp->level]=p;
1394 ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1397 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1398 "got lev %d node %d lf=%d",
1399 pp->level, p->pos, p->leaf);
1403 assert (p->offset==0 );
1404 src=p->bytes + p->offset;
1405 decode_ptr(&src, &pos);
1406 p->offset=src-(char*)p->bytes;
1407 isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1409 logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1410 "returning at lev %d node %d ofs=%d lf=%d",
1411 pp->level, p->pos, p->offset, p->leaf);
1413 } /* descend_to_leaf */
1415 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1416 { /* finds the next leaf by climbing up and down */
1418 if (!isamb_pp_climb_level(pp,&pos))
1420 isamb_pp_descend_to_leaf(pp, pos,0);
1424 static int isamb_pp_climb_desc(ISAMB_PP pp, const void *untilbuf)
1425 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1428 struct ISAMB_block *p = pp->block[pp->level];
1429 logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1430 "at level %d node %d ofs=%d sz=%d",
1431 pp->level, p->pos, p->offset, p->size);
1433 if (!isamb_pp_climb_level(pp,&pos))
1435 /* see if it would pay to climb one higher */
1436 if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1437 if (!isamb_pp_climb_level(pp,&pos))
1439 isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1441 p = pp->block[pp->level];
1442 logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1443 "at level %d node %d ofs=%d sz=%d",
1444 pp->level, p->pos, p->offset, p->size);
1449 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1452 struct ISAMB_block *p = pp->block[pp->level];
1454 logf(LOG_DEBUG,"isamb_pp_forward starting "
1455 "at level %d node %d ofs=%d sz=%d u=%p",
1456 pp->level, p->pos, p->offset, p->size,untilbuf);
1459 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1461 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1462 "at level %d node %d ofs=%d sz=%d",
1463 pp->level, p->pos, p->offset, p->size);
1467 if (! isamb_pp_climb_desc( pp, untilbuf)) {
1469 logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1470 "at level %d node %d ofs=%d sz=%d",
1471 pp->level, p->pos, p->offset, p->size);
1473 return 0; /* could not find a leaf */
1476 if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1478 logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1479 "at level %d node %d ofs=%d sz=%d",
1480 pp->level, p->pos, p->offset, p->size);
1484 }while ( isamb_pp_find_next_leaf(pp));
1485 return 0; /* could not find at all */
1487 else { /* no untilbuf, a straight read */
1488 /* FIXME - this should be moved
1489 * directly into the pp_read */
1490 /* keeping here now, to keep same
1491 * interface as the old fwd */
1492 if (isamb_pp_read_on_leaf( pp, buf)) {
1494 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1495 "at level %d node %d ofs=%d sz=%d",
1496 pp->level, p->pos, p->offset, p->size);
1500 if (isamb_pp_find_next_leaf(pp)) {
1502 logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1503 "at level %d node %d ofs=%d sz=%d",
1504 pp->level, p->pos, p->offset, p->size);
1506 return isamb_pp_read_on_leaf(pp, buf);
1511 } /* isam_pp_forward (new version) */
1513 #elif NEW_FORWARD == 0
1515 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1519 * while at end of node
1520 * climb higher. If out, return 0
1521 * while not on a leaf (and not at its end)
1530 * The upper nodes consist of a sequence of nodenumbers and keys
1531 * When opening a block, the first node number is read in, and
1532 * offset points to the first key, which is the upper limit of keys
1533 * in the node just read.
1537 struct ISAMB_block *p = pp->block[pp->level];
1542 int descending=0; /* used to prevent a border condition error */
1546 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1548 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, untilbuf, "until");
1549 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "buf");
1554 while ( (p->offset == p->size) && !descending )
1555 { /* end of this block - climb higher */
1556 assert (p->offset <= p->size);
1558 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1564 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1566 return 0; /* at end of the root, nothing left */
1568 close_block(pp->isamb, pp->block[pp->level]);
1569 pp->block[pp->level]=0;
1571 p=pp->block[pp->level];
1573 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1577 assert(p->offset <= p->size);
1578 /* skip the child we have handled */
1579 if (p->offset != p->size)
1581 src = p->bytes + p->offset;
1582 decode_ptr(&src, &item_len);
1584 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src,
1585 " isamb_pp_forward "
1586 "climb skipping old key");
1589 decode_ptr(&src,&pos);
1590 p->offset = src - (char*) p->bytes;
1591 break; /* even if this puts us at the end of the block, we
1592 need to descend to the last pos. UGLY coding,
1593 clean up some day */
1598 src = p->bytes + p->offset;
1599 if (p->offset == p->size)
1600 cmp=-2 ; /* descend to the last node, as we have
1604 decode_ptr(&src, &item_len);
1606 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1607 "ofs=%d sz=%d nxtpos=%d ",
1608 p->offset,p->size,pos);
1609 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, src, "");
1612 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1616 decode_ptr(&src,&nxtpos);
1621 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1624 descending=1; /* prevent climbing for a while */
1626 p = open_block(pp->isamb,pos);
1627 pp->block[pp->level] = p ;
1628 pp->total_size += p->size;
1629 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1632 { /* block starts with a pos */
1633 src = p->bytes + p->offset;
1634 decode_ptr(&src,&pos);
1635 p->offset=src-(char*) p->bytes;
1637 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1641 } /* descend to the node */
1643 { /* skip the node */
1644 p->offset = src - (char*) p->bytes;
1646 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1649 "isamb_pp_forward: skipping block on level %d, noting "
1651 pp->level, pp->maxlevel - pp->level-1 ,
1652 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1654 /* 0 is always leafs, 1 is one level above leafs etc, no
1655 * matter how high tree */
1657 } /* not on a leaf */
1660 if (p->offset == p->size) {
1665 assert (p->offset < p->size);
1666 src = p->bytes + p->offset;
1668 (*pp->isamb->method->codec.decode)(p->decodeClientData,
1670 p->offset = src - (char*) p->bytes;
1672 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1676 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1678 (*pp->isamb->method->codec.log_item)(LOG_DEBUG, buf, "");
1685 (*pp->isamb->method->codec.log_item)(
1686 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1690 (*pp->isamb->method->codec.log_item)(
1691 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1694 pp->returned_numbers++;
1698 pp->skipped_numbers++;
1704 #elif NEW_FORWARD == 2
1706 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1710 struct ISAMB_block *p = pp->block[pp->level];
1715 while (p->offset == p->size)
1718 while (p->offset == p->size)
1722 close_block (pp->isamb, pp->block[pp->level]);
1723 pp->block[pp->level] = 0;
1725 p = pp->block[pp->level];
1730 src = p->bytes + p->offset;
1732 decode_ptr (&src, &item_len);
1734 decode_ptr (&src, &pos);
1736 p->offset = src - (char*) p->bytes;
1738 src = p->bytes + p->offset;
1742 if (!untilb || p->offset == p->size)
1744 assert(p->offset < p->size);
1745 decode_ptr (&src, &item_len);
1746 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1749 decode_ptr (&src, &pos);
1750 p->offset = src - (char*) p->bytes;
1757 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1759 pp->total_size += p->size;
1767 src = p->bytes + p->offset;
1770 decode_ptr (&src, &pos);
1771 p->offset = src - (char*) p->bytes;
1773 if (!untilb || p->offset == p->size)
1775 assert(p->offset < p->size);
1776 decode_ptr (&src, &item_len);
1777 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1784 assert (p->offset < p->size);
1789 src = p->bytes + p->offset;
1790 (*pp->isamb->method->codec.decode)(p->decodeClientData, &dst, &src);
1791 p->offset = src - (char*) p->bytes;
1792 if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1795 if (p->offset == p->size) goto again;
1797 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1803 int isamb_pp_num (ISAMB_PP pp)
1805 assert(pp); /* shut up about unused arguments */
1809 static void isamb_pp_leaf_pos( ISAMB_PP pp,
1810 double *current, double *total,
1813 struct ISAMB_block *p = pp->block[pp->level];
1814 const char *src=p->bytes;
1815 char *end=p->bytes+p->size;
1816 char *cur=p->bytes+p->offset;
1818 void *decodeClientData;
1819 assert(p->offset <= p->size);
1825 decodeClientData = (pp->isamb->method->codec.start)();
1829 (*pp->isamb->method->codec.decode)(decodeClientData,&dst, &src);
1830 assert(dst<(char*) dummybuf+100); /*FIXME */
1836 logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur= %0.1f tot=%0.1f "
1837 " ofs=%d sz=%d lev=%d",
1838 *current, *total, p->offset, p->size, pp->level);
1841 (pp->isamb->method->codec.stop)(decodeClientData);
1844 static void isamb_pp_upper_pos( ISAMB_PP pp, double *current, double *total,
1845 double size, int level )
1846 { /* estimates total/current occurrences from here up, excl leaf */
1847 struct ISAMB_block *p = pp->block[level];
1848 const char *src=p->bytes;
1849 char *end=p->bytes+p->size;
1850 char *cur=p->bytes+p->offset;
1857 #if 1 // ISAMB_DEBUG
1858 logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning l=%d "
1859 "cur=%0.1f tot=%0.1f "
1860 " ofs=%d sz=%d pos=" ZINT_FORMAT,
1861 level, *current, *total, p->offset, p->size, p->pos);
1863 assert (p->offset <= p->size);
1864 decode_ptr (&src, &child ); /* first child */
1871 decode_ptr (&src, &item_size );
1872 assert(src+item_size<=end);
1874 decode_ptr (&src, &child );
1882 logf(LOG_DEBUG,"isamb_pp_upper_pos before recursion l=%d "
1883 "cur=%0.1f tot=%0.1f "
1884 " ofs=%d sz=%d pos=" ZINT_FORMAT,
1885 level, *current, *total, p->offset, p->size, p->pos);
1888 isamb_pp_upper_pos(pp, current, total, *total, level-1);
1891 void isamb_pp_pos( ISAMB_PP pp, double *current, double *total )
1892 { /* return an estimate of the current position and of the total number of */
1893 /* occureences in the isam tree, based on the current leaf */
1894 /* FIXME - Isam-B ought to know how many we have, so we could return */
1896 struct ISAMB_block *p = pp->block[pp->level];
1897 char dummy[100]; /* 100 bytes/entry must be enough */
1901 isamb_pp_leaf_pos(pp,current, total, dummy);
1903 isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1904 *current = (double) pp->returned_numbers;
1905 /* use the precise number, since we have it! */
1907 logf(LOG_LOG, "isamb_pp_pos returning: cur= %0.1f tot=%0.1f rn="ZINT_FORMAT,
1908 *current, *total, pp->returned_numbers);