1 /* $Id: isamb.c,v 1.34 2004-06-02 06:51:06 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include <yaz/xmalloc.h>
41 #define ISAMB_DATA_OFFSET 3
43 /* maximum size of encoded buffer */
44 #define DST_ITEM_MAX 256
46 #define ISAMB_MAX_LEVEL 10
47 /* approx 2*max page + max size of item */
48 #define DST_BUF_SIZE 16840
50 #define ISAMB_CACHE_ENTRY_SIZE 4096
52 /* CAT_MAX: _must_ be power of 2 */
54 #define CAT_MASK (CAT_MAX-1)
55 /* CAT_NO: <= CAT_MAX */
58 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
59 #define ISAMB_PTR_CODEC 1
61 struct ISAMB_cache_entry {
66 struct ISAMB_cache_entry *next;
72 struct ISAMB_head head;
73 struct ISAMB_cache_entry *cache_entries;
80 struct ISAMB_file *file;
82 int cache; /* 0=no cache, 1=use cache, -1=dummy isam (for testing only) */
83 int log_io; /* log level for bf_read/bf_write calls */
84 int log_freelist; /* log level for freelist handling */
85 int skipped_numbers; /* on a leaf node */
87 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
88 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
101 void *decodeClientData;
109 int maxlevel; /* total depth */
112 int skipped_numbers; /* on a leaf node */
113 int returned_numbers;
114 int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
115 int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
116 struct ISAMB_block **block;
120 static void encode_ptr (char **dst, unsigned pos)
122 unsigned char *bp = (unsigned char*) *dst;
126 *bp++ = 128 | (pos & 127);
133 static void encode_ptr (char **dst, unsigned pos)
135 memcpy(*dst, &pos, sizeof(pos));
136 (*dst) += sizeof(pos);
141 static void decode_ptr (char **src1, int *pos)
143 unsigned char **src = (unsigned char **) src1;
148 while (((c = *(*src)++) & 128))
150 d += ((c & 127) << r);
157 static void decode_ptr (char **src, int *pos)
159 memcpy (pos, *src, sizeof(*pos));
160 (*src) += sizeof(*pos);
164 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
167 ISAMB isamb = xmalloc (sizeof(*isamb));
171 isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
172 memcpy (isamb->method, method, sizeof(*method));
173 isamb->no_cat = CAT_NO;
175 isamb->log_freelist = 0;
176 isamb->cache = cache;
177 isamb->skipped_numbers=0;
178 isamb->returned_numbers=0;
179 for (i=0;i<ISAMB_MAX_LEVEL;i++)
180 isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
183 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
184 for (i = 0; i<isamb->no_cat; i++)
186 char fname[DST_BUF_SIZE];
187 isamb->file[i].cache_entries = 0;
188 isamb->file[i].head_dirty = 0;
189 sprintf (fname, "%s%c", name, i+'A');
191 isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
194 isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
197 if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
198 &isamb->file[i].head))
200 isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
201 isamb->file[i].head.last_block = isamb->file[i].head.first_block;
202 isamb->file[i].head.block_size = b_size;
203 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
204 isamb->file[i].head.free_list = 0;
206 assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
207 isamb->file[i].head_dirty = 0;
208 assert(isamb->file[i].head.block_size == b_size);
212 logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
217 static void flush_blocks (ISAMB b, int cat)
219 while (b->file[cat].cache_entries)
221 struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
222 b->file[cat].cache_entries = ce_this->next;
226 yaz_log (b->log_io, "bf_write: flush_blocks");
227 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
229 xfree (ce_this->buf);
234 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
236 int cat = pos&CAT_MASK;
237 int off = ((pos/CAT_MAX) &
238 (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
239 * b->file[cat].head.block_size;
240 int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
242 struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
247 assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
248 for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
251 if ((*ce)->pos == norm)
254 *ce = (*ce)->next; /* remove from list */
256 ce_this->next = b->file[cat].cache_entries; /* move to front */
257 b->file[cat].cache_entries = ce_this;
261 memcpy (ce_this->buf + off, userbuf,
262 b->file[cat].head.block_size);
266 memcpy (userbuf, ce_this->buf + off,
267 b->file[cat].head.block_size);
274 assert (ce_last && *ce_last);
276 *ce_last = 0; /* remove the last entry from list */
279 yaz_log (b->log_io, "bf_write: get_block");
280 bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
282 xfree (ce_this->buf);
285 ce_this = xmalloc (sizeof(*ce_this));
286 ce_this->next = b->file[cat].cache_entries;
287 b->file[cat].cache_entries = ce_this;
288 ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
290 yaz_log (b->log_io, "bf_read: get_block");
291 if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
292 memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
295 memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
301 memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
307 void isamb_close (ISAMB isamb)
310 for (i=0;isamb->accessed_nodes[i];i++)
311 logf(LOG_DEBUG,"isamb_close level leaf-%d: %d read, %d skipped",
312 i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
313 logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
314 isamb->skipped_numbers, isamb->returned_numbers);
315 for (i = 0; i<isamb->no_cat; i++)
317 flush_blocks (isamb, i);
318 if (isamb->file[i].head_dirty)
319 bf_write (isamb->file[i].bf, 0, 0,
320 sizeof(struct ISAMB_head), &isamb->file[i].head);
322 bf_close (isamb->file[i].bf);
325 xfree (isamb->method);
330 struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
332 int cat = pos&CAT_MASK;
333 struct ISAMB_block *p;
336 p = xmalloc (sizeof(*p));
338 p->cat = pos & CAT_MASK;
339 p->buf = xmalloc (b->file[cat].head.block_size);
341 if (!get_block (b, pos, p->buf, 0))
343 yaz_log (b->log_io, "bf_read: open_block");
344 if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
346 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
347 (long) pos, (long) pos/CAT_MAX);
351 p->bytes = p->buf + ISAMB_DATA_OFFSET;
353 p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
356 yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
358 assert (p->size >= 0);
362 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
363 yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
367 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
369 struct ISAMB_block *p;
371 p = xmalloc (sizeof(*p));
372 p->buf = xmalloc (b->file[cat].head.block_size);
374 if (!b->file[cat].head.free_list)
377 block_no = b->file[cat].head.last_block++;
378 p->pos = block_no * CAT_MAX + cat;
382 p->pos = b->file[cat].head.free_list;
383 assert((p->pos & CAT_MASK) == cat);
384 if (!get_block (b, p->pos, p->buf, 0))
386 yaz_log (b->log_io, "bf_read: new_block");
387 if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
389 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
390 (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
394 yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
395 cat, p->pos/CAT_MAX);
396 memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
399 b->file[cat].head_dirty = 1;
400 memset (p->buf, 0, b->file[cat].head.block_size);
401 p->bytes = p->buf + ISAMB_DATA_OFFSET;
407 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
411 struct ISAMB_block *new_leaf (ISAMB b, int cat)
413 return new_block (b, 1, cat);
417 struct ISAMB_block *new_int (ISAMB b, int cat)
419 return new_block (b, 0, cat);
422 static void check_block (ISAMB b, struct ISAMB_block *p)
431 char *startp = p->bytes;
433 char *endp = p->bytes + p->size;
436 decode_ptr (&src, &pos);
437 assert ((pos&CAT_MASK) == p->cat);
441 decode_ptr (&src, &item_len);
442 assert (item_len > 0 && item_len < 30);
444 decode_ptr (&src, &pos);
445 assert ((pos&CAT_MASK) == p->cat);
450 void close_block (ISAMB b, struct ISAMB_block *p)
456 yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
457 p->pos, p->cat, p->pos/CAT_MAX);
458 memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
459 b->file[p->cat].head.free_list = p->pos;
460 if (!get_block (b, p->pos, p->buf, 1))
462 yaz_log (b->log_io, "bf_write: close_block (deleted)");
463 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
468 int size = p->size + ISAMB_DATA_OFFSET;
469 assert (p->size >= 0);
471 p->buf[1] = size & 255;
472 p->buf[2] = size >> 8;
474 if (!get_block (b, p->pos, p->buf, 1))
476 yaz_log (b->log_io, "bf_write: close_block");
477 bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
480 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
485 int insert_sub (ISAMB b, struct ISAMB_block **p,
486 void *new_item, int *mode,
488 struct ISAMB_block **sp,
489 void *sub_item, int *sub_size,
492 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
494 ISAMC_I *stream, struct ISAMB_block **sp,
495 void *split_item, int *split_size, void *last_max_item)
497 char *startp = p->bytes;
499 char *endp = p->bytes + p->size;
501 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
502 char sub_item[DST_ITEM_MAX];
508 assert(p->size >= 0);
509 decode_ptr (&src, &pos);
515 decode_ptr (&src, &item_len);
516 d = (*b->method->compare_item)(src, lookahead_item);
519 sub_p1 = open_block (b, pos);
521 more = insert_sub (b, &sub_p1, lookahead_item, mode,
523 sub_item, &sub_size, src);
528 decode_ptr (&src, &pos);
532 sub_p1 = open_block (b, pos);
534 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
535 sub_item, &sub_size, last_max_item);
539 /* there was a split - must insert pointer in this one */
540 char dst_buf[DST_BUF_SIZE];
543 assert (sub_size < 30 && sub_size > 1);
545 memcpy (dst, startp, src - startp);
549 encode_ptr (&dst, sub_size); /* sub length and item */
550 memcpy (dst, sub_item, sub_size);
553 encode_ptr (&dst, sub_p2->pos); /* pos */
555 if (endp - src) /* remaining data */
557 memcpy (dst, src, endp - src);
560 p->size = dst - dst_buf;
561 assert (p->size >= 0);
562 if (p->size <= b->file[p->cat].head.block_max)
564 memcpy (startp, dst_buf, dst - dst_buf);
573 half = src + b->file[p->cat].head.block_size/2;
574 decode_ptr (&src, &pos);
577 decode_ptr (&src, split_size);
579 decode_ptr (&src, &pos);
581 p_new_size = src - dst_buf;
582 memcpy (p->bytes, dst_buf, p_new_size);
584 decode_ptr (&src, split_size);
585 memcpy (split_item, src, *split_size);
588 *sp = new_int (b, p->cat);
589 (*sp)->size = endp - src;
590 memcpy ((*sp)->bytes, src, (*sp)->size);
592 p->size = p_new_size;
595 close_block (b, sub_p2);
597 close_block (b, sub_p1);
602 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
603 int *lookahead_mode, ISAMC_I *stream,
604 struct ISAMB_block **sp2,
605 void *sub_item, int *sub_size,
608 struct ISAMB_block *p = *sp1;
609 char *src = 0, *endp = 0;
610 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
612 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
613 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
615 int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
616 char *cut = dst_buf + quater * 2;
617 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
620 char cut_item_buf[DST_ITEM_MAX];
621 int cut_item_size = 0;
625 char file_item_buf[DST_ITEM_MAX];
626 char *file_item = file_item_buf;
629 endp = p->bytes + p->size;
630 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
635 char *lookahead_next;
639 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
643 dst_item = lookahead_item;
644 if (!*lookahead_mode)
646 yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
647 assert (*lookahead_mode);
651 dst_item = file_item_buf;
652 if (!*lookahead_mode && d == 0)
656 else if (!half1 && dst > cut)
658 char *dst_item_0 = dst_item;
659 half1 = dst; /* candidate for splitting */
661 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
663 cut_item_size = dst_item - dst_item_0;
664 memcpy (cut_item_buf, dst_item_0, cut_item_size);
669 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
679 lookahead_next = lookahead_item;
680 if (!(*stream->read_item)(stream->clientData,
687 if (lookahead_item && max_item &&
688 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
699 lookahead_next = lookahead_item;
700 if (!(*stream->read_item)(stream->clientData,
701 &lookahead_next, lookahead_mode))
708 file_item = file_item_buf;
709 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
715 file_item = file_item_buf;
716 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
720 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
721 while (lookahead_item)
723 char *dst_item = lookahead_item;
727 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
732 if (!*lookahead_mode)
734 yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
737 else if (!half1 && dst > cut)
739 char *dst_item_0 = dst_item;
740 half1 = dst; /* candidate for splitting */
742 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
744 cut_item_size = dst_item - dst_item_0;
745 memcpy (cut_item_buf, dst_item_0, cut_item_size);
750 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
759 dst_item = lookahead_item;
760 if (!(*stream->read_item)(stream->clientData, &dst_item,
767 new_size = dst - dst_buf;
768 if (p && p->cat != b->no_cat-1 &&
769 new_size > b->file[p->cat].head.block_max)
771 /* non-btree block will be removed */
774 /* delete it too!! */
775 p = 0; /* make a new one anyway */
778 { /* must create a new one */
780 for (i = 0; i < b->no_cat; i++)
781 if (new_size <= b->file[i].head.block_max)
787 if (new_size > b->file[p->cat].head.block_max)
790 char *cut_item = cut_item_buf;
796 p->size = half1 - dst_buf;
797 memcpy (p->bytes, dst_buf, half1 - dst_buf);
800 *sp2 = new_leaf (b, p->cat);
802 (*b->method->code_reset)(c2);
804 first_dst = (*sp2)->bytes;
806 (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
808 memcpy (first_dst, half2, dst - half2);
810 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
813 memcpy (sub_item, cut_item_buf, cut_item_size);
814 *sub_size = cut_item_size;
818 memcpy (p->bytes, dst_buf, dst - dst_buf);
821 (*b->method->code_stop)(ISAMC_DECODE, c1);
822 (*b->method->code_stop)(ISAMC_ENCODE, c2);
827 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
830 struct ISAMB_block **sp,
831 void *sub_item, int *sub_size,
834 if (!*p || (*p)->leaf)
835 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
838 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
842 int isamb_unlink (ISAMB b, ISAMC_P pos)
844 struct ISAMB_block *p1;
848 p1 = open_block(b, pos);
854 char *src = p1->bytes + p1->offset;
856 decode_ptr(&src, &sub_p);
857 isamb_unlink(b, sub_p);
859 while (src != p1->bytes + p1->size)
861 decode_ptr(&src, &item_len);
863 decode_ptr(&src, &sub_p);
864 isamb_unlink(b, sub_p);
871 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
873 char item_buf[DST_ITEM_MAX];
885 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
890 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
893 struct ISAMB_block *p = 0, *sp = 0;
894 char sub_item[DST_ITEM_MAX];
898 p = open_block (b, pos);
899 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
900 sub_item, &sub_size, 0);
902 { /* increase level of tree by one */
903 struct ISAMB_block *p2 = new_int (b, p->cat);
904 char *dst = p2->bytes + p2->size;
906 encode_ptr (&dst, p->pos);
907 assert (sub_size < 20);
908 encode_ptr (&dst, sub_size);
909 memcpy (dst, sub_item, sub_size);
911 encode_ptr (&dst, sp->pos);
913 p2->size = dst - p2->bytes;
914 pos = p2->pos; /* return new super page */
919 pos = p->pos; /* return current one (again) */
925 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
927 ISAMB_PP pp = xmalloc (sizeof(*pp));
931 pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
938 pp->skipped_numbers=0;
939 pp->returned_numbers=0;
940 for (i=0;i<ISAMB_MAX_LEVEL;i++)
941 pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
944 struct ISAMB_block *p = open_block (isamb, pos);
945 char *src = p->bytes + p->offset;
946 pp->block[pp->level] = p;
948 pp->total_size += p->size;
954 decode_ptr (&src, &pos);
955 p->offset = src - p->bytes;
957 pp->accessed_nodes[pp->level]++;
959 pp->block[pp->level+1] = 0;
960 pp->maxlevel=pp->level;
966 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
968 return isamb_pp_open_x (isamb, pos, 0);
971 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
976 logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
977 pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
978 for (i=pp->maxlevel;i>=0;i--)
979 if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
980 logf(LOG_DEBUG,"isamb_pp_close level leaf-%d: %d read, %d skipped", i,
981 pp->accessed_nodes[i], pp->skipped_nodes[i]);
982 pp->isamb->skipped_numbers += pp->skipped_numbers;
983 pp->isamb->returned_numbers += pp->returned_numbers;
984 for (i=pp->maxlevel;i>=0;i--)
986 pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
987 pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
990 *size = pp->total_size;
992 *blocks = pp->no_blocks;
993 for (i = 0; i <= pp->level; i++)
994 close_block (pp->isamb, pp->block[i]);
999 int isamb_block_info (ISAMB isamb, int cat)
1001 if (cat >= 0 && cat < isamb->no_cat)
1002 return isamb->file[cat].head.block_size;
1006 void isamb_pp_close (ISAMB_PP pp)
1008 isamb_pp_close_x (pp, 0, 0);
1013 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1014 debug the more complex pp_read that also forwards. May be deleted near end
1015 of 2004, if it has not shown to be useful */
1017 int isamb_pp_read (ISAMB_PP pp, void *buf)
1021 struct ISAMB_block *p = pp->block[pp->level];
1025 while (p->offset == p->size)
1028 while (p->offset == p->size)
1032 close_block (pp->isamb, pp->block[pp->level]);
1033 pp->block[pp->level] = 0;
1035 p = pp->block[pp->level];
1038 src = p->bytes + p->offset;
1040 decode_ptr (&src, &item_len);
1042 decode_ptr (&src, &pos);
1044 p->offset = src - (char*) p->bytes;
1050 pp->block[pp->level] = p = open_block (pp->isamb, pos);
1052 pp->total_size += p->size;
1059 src = p->bytes + p->offset;
1060 decode_ptr (&src, &pos);
1061 p->offset = src - (char*) p->bytes;
1065 assert (p->offset < p->size);
1067 src = p->bytes + p->offset;
1068 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1070 p->offset = src - (char*) p->bytes;
1071 /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1076 int isamb_pp_read (ISAMB_PP pp, void *buf)
1078 return isamb_pp_forward(pp,buf,0);
1082 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1086 * while at end of node
1087 * climb higher. If out, return 0
1088 * while not on a leaf (and not at its end)
1097 * The upper nodes consist of a sequence of nodenumbers and keys
1098 * When opening a block, the first node number is read in, and
1099 * offset points to the first key, which is the upper limit of keys
1100 * in the node just read.
1104 struct ISAMB_block *p = pp->block[pp->level];
1109 int descending=0; /* used to prevent a border condition error */
1113 logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1115 (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1116 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1121 while ( (p->offset == p->size) && !descending )
1122 { /* end of this block - climb higher */
1123 assert (p->offset <= p->size);
1125 logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1131 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1133 return 0; /* at end of the root, nothing left */
1135 close_block(pp->isamb, pp->block[pp->level]);
1136 pp->block[pp->level]=0;
1138 p=pp->block[pp->level];
1140 logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1144 /* skip the child we have handled */
1145 if (p->offset != p->size)
1147 src = p->bytes + p->offset;
1148 decode_ptr(&src, &item_len);
1150 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1151 " isamb_pp_forward "
1152 "climb skipping old key");
1155 decode_ptr(&src,&pos);
1156 p->offset = src - (char*) p->bytes;
1157 break; /* even if this puts us at the end of the block, we
1158 need to descend to the last pos. UGLY coding,
1159 clean up some day */
1164 src = p->bytes + p->offset;
1165 if (p->offset == p->size)
1166 cmp=-2 ; /* descend to the last node, as we have
1170 decode_ptr(&src, &item_len);
1172 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1173 "ofs=%d sz=%d nxtpos=%d ",
1174 p->offset,p->size,pos);
1175 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1178 cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1182 decode_ptr(&src,&nxtpos);
1187 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1190 descending=1; /* prevent climbing for a while */
1192 p = open_block(pp->isamb,pos);
1193 pp->block[pp->level] = p ;
1194 pp->total_size += p->size;
1195 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1198 { /* block starts with a pos */
1199 src = p->bytes + p->offset;
1200 decode_ptr(&src,&pos);
1201 p->offset=src-(char*) p->bytes;
1203 logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1207 } /* descend to the node */
1209 { /* skip the node */
1210 p->offset = src - (char*) p->bytes;
1212 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1215 "isamb_pp_forward: skipping block on level %d, noting "
1217 pp->level, pp->maxlevel - pp->level-1 ,
1218 pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1220 /* 0 is always leafs, 1 is one level above leafs etc, no
1221 * matter how high tree */
1223 } /* not on a leaf */
1226 assert (p->offset < p->size);
1227 src = p->bytes + p->offset;
1229 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1231 p->offset = src - (char*) p->bytes;
1233 cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1237 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d",
1239 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1246 (*pp->isamb->method->log_item)(
1247 LOG_DEBUG, buf, "isamb_pp_forward returning 1");
1251 (*pp->isamb->method->log_item)(
1252 LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1255 pp->returned_numbers++;
1259 pp->skipped_numbers++;
1265 int isamb_pp_num (ISAMB_PP pp)