Another forward - with ideas from original isamb_pp_read
[idzebra-moved-to-github.git] / isamb / isamb.c
1 /* $Id: isamb.c,v 1.40 2004-06-02 23:05:55 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 #include <string.h>
24 #include <yaz/xmalloc.h>
25 #include <yaz/log.h>
26 #include <isamb.h>
27 #include <assert.h>
28
29 #ifndef ISAMB_DEBUG
30 #define ISAMB_DEBUG 0
31 #endif
32
33 struct ISAMB_head {
34     int first_block;
35     int last_block;
36     int block_size;
37     int block_max;
38     int free_list;
39 };
40
41 #define ISAMB_DATA_OFFSET 3
42
43 /* maximum size of encoded buffer */
44 #define DST_ITEM_MAX 256
45
46 #define ISAMB_MAX_LEVEL 10
47 /* approx 2*max page + max size of item */
48 #define DST_BUF_SIZE 16840
49
50 #define ISAMB_CACHE_ENTRY_SIZE 4096
51
52 /* CAT_MAX: _must_ be power of 2 */
53 #define CAT_MAX 4
54 #define CAT_MASK (CAT_MAX-1)
55 /* CAT_NO: <= CAT_MAX */
56 #define CAT_NO 4
57
58 /* ISAMB_PTR_CODEC=1 var, =0 fixed */
59 #define ISAMB_PTR_CODEC  1
60
61 struct ISAMB_cache_entry {
62     ISAMB_P pos;
63     unsigned char *buf;
64     int dirty;
65     int hits;
66     struct ISAMB_cache_entry *next;
67 };
68
69 struct ISAMB_file {
70     BFile bf;
71     int head_dirty;
72     struct ISAMB_head head;
73     struct ISAMB_cache_entry *cache_entries;
74 };
75
76 struct ISAMB_s {
77     BFiles bfs;
78     ISAMC_M *method;
79
80     struct ISAMB_file *file;
81     int no_cat;
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 */
86     int returned_numbers; 
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 */
89 };
90
91 struct ISAMB_block {
92     ISAMB_P pos;
93     int cat;
94     int size;
95     int leaf;
96     int dirty;
97     int deleted;
98     int offset;
99     char *bytes;
100     char *cbuf;
101     unsigned char *buf;
102     void *decodeClientData;
103     int log_rw;
104 };
105
106 struct ISAMB_PP_s {
107     ISAMB isamb;
108     ISAMB_P pos;
109     int level;
110     int maxlevel; /* total depth */
111     int total_size;
112     int no_blocks;
113     int skipped_numbers; /* on a leaf node */
114     int returned_numbers; 
115     int skipped_nodes[ISAMB_MAX_LEVEL]; /* [0]=skipped leaves, 1=higher etc */
116     int accessed_nodes[ISAMB_MAX_LEVEL]; /* nodes we did not skip */
117     struct ISAMB_block **block;
118 };
119
120 #if ISAMB_PTR_CODEC
121 static void encode_ptr (char **dst, unsigned pos)
122 {
123     unsigned char *bp = (unsigned char*) *dst;
124
125     while (pos > 127)
126     {
127          *bp++ = 128 | (pos & 127);
128          pos = pos >> 7;
129     }
130     *bp++ = pos;
131     *dst = (char *) bp;
132 }
133 #else
134 static void encode_ptr (char **dst, unsigned pos)
135 {
136     memcpy(*dst, &pos, sizeof(pos));
137     (*dst) += sizeof(pos);
138 }
139 #endif
140
141 #if ISAMB_PTR_CODEC
142 static void decode_ptr (char **src1, int *pos)
143 {
144     unsigned char **src = (unsigned char **) src1;
145     unsigned d = 0;
146     unsigned char c;
147     unsigned r = 0;
148
149     while (((c = *(*src)++) & 128))
150     {
151         d += ((c & 127) << r);
152         r += 7;
153     }
154     d += (c << r);
155     *pos = d;
156 }
157 #else
158 static void decode_ptr (char **src, int *pos)
159 {
160      memcpy (pos, *src, sizeof(*pos));
161      (*src) += sizeof(*pos);
162 }
163 #endif
164
165 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
166                   int cache)
167 {
168     ISAMB isamb = xmalloc (sizeof(*isamb));
169     int i, b_size = 32;
170
171     isamb->bfs = bfs;
172     isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
173     memcpy (isamb->method, method, sizeof(*method));
174     isamb->no_cat = CAT_NO;
175     isamb->log_io = 0;
176     isamb->log_freelist = 0;
177     isamb->cache = cache;
178     isamb->skipped_numbers=0;
179     isamb->returned_numbers=0;
180     for (i=0;i<ISAMB_MAX_LEVEL;i++)
181       isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
182
183     assert (cache == 0);
184     isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
185     for (i = 0; i<isamb->no_cat; i++)
186     {
187         char fname[DST_BUF_SIZE];
188         isamb->file[i].cache_entries = 0;
189         isamb->file[i].head_dirty = 0;
190         sprintf (fname, "%s%c", name, i+'A');
191         if (cache)
192             isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
193                                          writeflag);
194         else
195             isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
196
197         
198         if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
199                       &isamb->file[i].head))
200         {
201             isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
202             isamb->file[i].head.last_block = isamb->file[i].head.first_block;
203             isamb->file[i].head.block_size = b_size;
204             isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
205             isamb->file[i].head.free_list = 0;
206         }
207         assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
208         isamb->file[i].head_dirty = 0;
209         assert(isamb->file[i].head.block_size == b_size);
210         b_size = b_size * 4;
211     }
212 #if ISAMB_DEBUG
213     logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
214 #endif
215     return isamb;
216 }
217
218 static void flush_blocks (ISAMB b, int cat)
219 {
220     while (b->file[cat].cache_entries)
221     {
222         struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
223         b->file[cat].cache_entries = ce_this->next;
224
225         if (ce_this->dirty)
226         {
227             yaz_log (b->log_io, "bf_write: flush_blocks");
228             bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
229         }
230         xfree (ce_this->buf);
231         xfree (ce_this);
232     }
233 }
234
235 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
236 {
237     int cat = pos&CAT_MASK;
238     int off = ((pos/CAT_MAX) & 
239                (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
240         * b->file[cat].head.block_size;
241     int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
242     int no = 0;
243     struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
244
245     if (!b->cache)
246         return 0;
247
248     assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
249     for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
250     {
251         ce_last = ce;
252         if ((*ce)->pos == norm)
253         {
254             ce_this = *ce;
255             *ce = (*ce)->next;   /* remove from list */
256             
257             ce_this->next = b->file[cat].cache_entries;  /* move to front */
258             b->file[cat].cache_entries = ce_this;
259             
260             if (wr)
261             {
262                 memcpy (ce_this->buf + off, userbuf, 
263                         b->file[cat].head.block_size);
264                 ce_this->dirty = 1;
265             }
266             else
267                 memcpy (userbuf, ce_this->buf + off,
268                         b->file[cat].head.block_size);
269             return 1;
270         }
271     }
272     if (no >= 40)
273     {
274         assert (no == 40);
275         assert (ce_last && *ce_last);
276         ce_this = *ce_last;
277         *ce_last = 0;  /* remove the last entry from list */
278         if (ce_this->dirty)
279         {
280             yaz_log (b->log_io, "bf_write: get_block");
281             bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
282         }
283         xfree (ce_this->buf);
284         xfree (ce_this);
285     }
286     ce_this = xmalloc (sizeof(*ce_this));
287     ce_this->next = b->file[cat].cache_entries;
288     b->file[cat].cache_entries = ce_this;
289     ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
290     ce_this->pos = norm;
291     yaz_log (b->log_io, "bf_read: get_block");
292     if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
293         memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
294     if (wr)
295     {
296         memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
297         ce_this->dirty = 1;
298     }
299     else
300     {
301         ce_this->dirty = 0;
302         memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
303     }
304     return 1;
305 }
306
307
308 void isamb_close (ISAMB isamb)
309 {
310     int i;
311     for (i=0;isamb->accessed_nodes[i];i++)
312         logf(LOG_DEBUG,"isamb_close  level leaf-%d: %d read, %d skipped",
313              i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
314     logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
315          isamb->skipped_numbers, isamb->returned_numbers);
316     for (i = 0; i<isamb->no_cat; i++)
317     {
318         flush_blocks (isamb, i);
319         if (isamb->file[i].head_dirty)
320             bf_write (isamb->file[i].bf, 0, 0,
321                       sizeof(struct ISAMB_head), &isamb->file[i].head);
322         
323         bf_close (isamb->file[i].bf);
324     }
325     xfree (isamb->file);
326     xfree (isamb->method);
327     xfree (isamb);
328 }
329
330 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
331 {
332     int cat = pos&CAT_MASK;
333     struct ISAMB_block *p;
334     if (!pos)
335         return 0;
336     p = xmalloc (sizeof(*p));
337     p->pos = pos;
338     p->cat = pos & CAT_MASK;
339     p->buf = xmalloc (b->file[cat].head.block_size);
340     p->cbuf = 0;
341
342     if (!get_block (b, pos, p->buf, 0))
343     {
344         yaz_log (b->log_io, "bf_read: open_block");
345         if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
346         {
347             yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
348                      (long) pos, (long) pos/CAT_MAX);
349             abort();
350         }
351     }
352     p->bytes = p->buf + ISAMB_DATA_OFFSET;
353     p->leaf = p->buf[0];
354     p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
355     if (p->size < 0)
356     {
357         yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
358     }
359     assert (p->size >= 0);
360     p->offset = 0;
361     p->dirty = 0;
362     p->deleted = 0;
363     p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
364     yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
365     return p;
366 }
367
368 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
369 {
370     struct ISAMB_block *p;
371
372     p = xmalloc (sizeof(*p));
373     p->buf = xmalloc (b->file[cat].head.block_size);
374
375     if (!b->file[cat].head.free_list)
376     {
377         int block_no;
378         block_no = b->file[cat].head.last_block++;
379         p->pos = block_no * CAT_MAX + cat;
380     }
381     else
382     {
383         p->pos = b->file[cat].head.free_list;
384         assert((p->pos & CAT_MASK) == cat);
385         if (!get_block (b, p->pos, p->buf, 0))
386         {
387             yaz_log (b->log_io, "bf_read: new_block");
388             if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
389             {
390                 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
391                          (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
392                 abort ();
393             }
394         }
395         yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
396                  cat, p->pos/CAT_MAX);
397         memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
398     }
399     p->cat = cat;
400     b->file[cat].head_dirty = 1;
401     memset (p->buf, 0, b->file[cat].head.block_size);
402     p->bytes = p->buf + ISAMB_DATA_OFFSET;
403     p->leaf = leaf;
404     p->size = 0;
405     p->dirty = 1;
406     p->deleted = 0;
407     p->offset = 0;
408     p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
409     return p;
410 }
411
412 struct ISAMB_block *new_leaf (ISAMB b, int cat)
413 {
414     return new_block (b, 1, cat);
415 }
416
417
418 struct ISAMB_block *new_int (ISAMB b, int cat)
419 {
420     return new_block (b, 0, cat);
421 }
422
423 static void check_block (ISAMB b, struct ISAMB_block *p)
424 {
425     if (p->leaf)
426     {
427         ;
428     }
429     else
430     {
431         /* sanity check */
432         char *startp = p->bytes;
433         char *src = startp;
434         char *endp = p->bytes + p->size;
435         int pos;
436             
437         decode_ptr (&src, &pos);
438         assert ((pos&CAT_MASK) == p->cat);
439         while (src != endp)
440         {
441             int item_len;
442             decode_ptr (&src, &item_len);
443             assert (item_len > 0 && item_len < 30);
444             src += item_len;
445             decode_ptr (&src, &pos);
446             assert ((pos&CAT_MASK) == p->cat);
447         }
448     }
449 }
450
451 void close_block (ISAMB b, struct ISAMB_block *p)
452 {
453     if (!p)
454         return;
455     if (p->deleted)
456     {
457         yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
458                  p->pos, p->cat, p->pos/CAT_MAX);
459         memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
460         b->file[p->cat].head.free_list = p->pos;
461         if (!get_block (b, p->pos, p->buf, 1))
462         {
463             yaz_log (b->log_io, "bf_write: close_block (deleted)");
464             bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
465         }
466     }
467     else if (p->dirty)
468     {
469         int size = p->size + ISAMB_DATA_OFFSET;
470         assert (p->size >= 0);
471         p->buf[0] = p->leaf;
472         p->buf[1] = size & 255;
473         p->buf[2] = size >> 8;
474         check_block(b, p);
475         if (!get_block (b, p->pos, p->buf, 1))
476         {
477             yaz_log (b->log_io, "bf_write: close_block");
478             bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
479         }
480     }
481     (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
482     xfree (p->buf);
483     xfree (p);
484 }
485
486 int insert_sub (ISAMB b, struct ISAMB_block **p,
487                 void *new_item, int *mode,
488                 ISAMC_I *stream,
489                 struct ISAMB_block **sp,
490                 void *sub_item, int *sub_size,
491                 void *max_item);
492
493 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
494                 int *mode,
495                 ISAMC_I *stream, struct ISAMB_block **sp,
496                 void *split_item, int *split_size, void *last_max_item)
497 {
498     char *startp = p->bytes;
499     char *src = startp;
500     char *endp = p->bytes + p->size;
501     int pos;
502     struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
503     char sub_item[DST_ITEM_MAX];
504     int sub_size;
505     int more;
506
507     *sp = 0;
508
509     assert(p->size >= 0);
510     decode_ptr (&src, &pos);
511     while (src != endp)
512     {
513         int item_len;
514         int d;
515         char *src0 = src;
516         decode_ptr (&src, &item_len);
517         d = (*b->method->compare_item)(src, lookahead_item);
518         if (d > 0)
519         {
520             sub_p1 = open_block (b, pos);
521             assert (sub_p1);
522             more = insert_sub (b, &sub_p1, lookahead_item, mode,
523                                stream, &sub_p2, 
524                                sub_item, &sub_size, src);
525             src = src0;
526             break;
527         }
528         src += item_len;
529         decode_ptr (&src, &pos);
530     }
531     if (!sub_p1)
532     {
533         sub_p1 = open_block (b, pos);
534         assert (sub_p1);
535         more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2, 
536                            sub_item, &sub_size, last_max_item);
537     }
538     if (sub_p2)
539     {
540         /* there was a split - must insert pointer in this one */
541         char dst_buf[DST_BUF_SIZE];
542         char *dst = dst_buf;
543
544         assert (sub_size < 30 && sub_size > 1);
545
546         memcpy (dst, startp, src - startp);
547                 
548         dst += src - startp;
549
550         encode_ptr (&dst, sub_size);      /* sub length and item */
551         memcpy (dst, sub_item, sub_size);
552         dst += sub_size;
553
554         encode_ptr (&dst, sub_p2->pos);   /* pos */
555
556         if (endp - src)                   /* remaining data */
557         {
558             memcpy (dst, src, endp - src);
559             dst += endp - src;
560         }
561         p->size = dst - dst_buf;
562         assert (p->size >= 0);
563         if (p->size <= b->file[p->cat].head.block_max)
564         {
565             memcpy (startp, dst_buf, dst - dst_buf);
566         }
567         else
568         {
569             int p_new_size;
570             char *half;
571             src = dst_buf;
572             endp = dst;
573
574             half = src + b->file[p->cat].head.block_size/2;
575             decode_ptr (&src, &pos);
576             while (src <= half)
577             {
578                 decode_ptr (&src, split_size);
579                 src += *split_size;
580                 decode_ptr (&src, &pos);
581             }
582             p_new_size = src - dst_buf;
583             memcpy (p->bytes, dst_buf, p_new_size);
584
585             decode_ptr (&src, split_size);
586             memcpy (split_item, src, *split_size);
587             src += *split_size;
588
589             *sp = new_int (b, p->cat);
590             (*sp)->size = endp - src;
591             memcpy ((*sp)->bytes, src, (*sp)->size);
592
593             p->size = p_new_size;
594         }
595         p->dirty = 1;
596         close_block (b, sub_p2);
597     }
598     close_block (b, sub_p1);
599     return more;
600 }
601
602
603 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
604                  int *lookahead_mode, ISAMC_I *stream,
605                  struct ISAMB_block **sp2,
606                  void *sub_item, int *sub_size,
607                  void *max_item)
608 {
609     struct ISAMB_block *p = *sp1;
610     char *src = 0, *endp = 0;
611     char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
612     int new_size;
613     void *c1 = (*b->method->code_start)(ISAMC_DECODE);
614     void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
615     int more = 1;
616     int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
617     char *cut = dst_buf + quater * 2;
618     char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
619     char *half1 = 0;
620     char *half2 = 0;
621     char cut_item_buf[DST_ITEM_MAX];
622     int cut_item_size = 0;
623
624     if (p && p->size)
625     {
626         char file_item_buf[DST_ITEM_MAX];
627         char *file_item = file_item_buf;
628             
629         src = p->bytes;
630         endp = p->bytes + p->size;
631         (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
632         while (1)
633         {
634             char *dst_item = 0;
635             char *dst_0 = dst;
636             char *lookahead_next;
637             int d = -1;
638             
639             if (lookahead_item)
640                 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
641             
642             if (d > 0)
643             {
644                 dst_item = lookahead_item;
645                 if (!*lookahead_mode)
646                 {
647                     yaz_log (LOG_WARN, "isamb: Inconsistent register (1)");
648                     assert (*lookahead_mode);
649                 }
650             }
651             else
652                 dst_item = file_item_buf;
653             if (!*lookahead_mode && d == 0)
654             {
655                 p->dirty = 1;
656             }
657             else if (!half1 && dst > cut)
658             {
659                 char *dst_item_0 = dst_item;
660                 half1 = dst; /* candidate for splitting */
661                 
662                 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
663                 
664                 cut_item_size = dst_item - dst_item_0;
665                 memcpy (cut_item_buf, dst_item_0, cut_item_size);
666                 
667                 half2 = dst;
668             }
669             else
670                 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
671             if (d > 0)  
672             {
673                 if (dst > maxp)
674                 {
675                     dst = dst_0;
676                     lookahead_item = 0;
677                 }
678                 else
679                 {
680                     lookahead_next = lookahead_item;
681                     if (!(*stream->read_item)(stream->clientData,
682                                               &lookahead_next,
683                                               lookahead_mode))
684                     {
685                         lookahead_item = 0;
686                         more = 0;
687                     }
688                     if (lookahead_item && max_item &&
689                         (*b->method->compare_item)(max_item, lookahead_item) <= 0)
690                     {
691                         /* max_item 1 */
692                         lookahead_item = 0;
693                     }
694                     
695                     p->dirty = 1;
696                 }
697             }
698             else if (d == 0)
699             {
700                 lookahead_next = lookahead_item;
701                 if (!(*stream->read_item)(stream->clientData,
702                                           &lookahead_next, lookahead_mode))
703                 {
704                     lookahead_item = 0;
705                     more = 0;
706                 }
707                 if (src == endp)
708                     break;
709                 file_item = file_item_buf;
710                 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
711             }
712             else
713             {
714                 if (src == endp)
715                     break;
716                 file_item = file_item_buf;
717                 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
718             }
719         }
720     }
721     maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
722     while (lookahead_item)
723     {
724         char *dst_item = lookahead_item;
725         char *dst_0 = dst;
726         
727         if (max_item &&
728             (*b->method->compare_item)(max_item, lookahead_item) <= 0)
729         {
730             /* max_item 2 */
731             break;
732         }
733         if (!*lookahead_mode)
734         {
735             yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
736             abort();
737         }
738         else if (!half1 && dst > cut)   
739         {
740             char *dst_item_0 = dst_item;
741             half1 = dst; /* candidate for splitting */
742             
743             (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
744             
745             cut_item_size = dst_item - dst_item_0;
746             memcpy (cut_item_buf, dst_item_0, cut_item_size);
747             
748             half2 = dst;
749         }
750         else
751             (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
752
753         if (dst > maxp)
754         {
755             dst = dst_0;
756             break;
757         }
758         if (p)
759             p->dirty = 1;
760         dst_item = lookahead_item;
761         if (!(*stream->read_item)(stream->clientData, &dst_item,
762                                   lookahead_mode))
763         {
764             lookahead_item = 0;
765             more = 0;
766         }
767     }
768     new_size = dst - dst_buf;
769     if (p && p->cat != b->no_cat-1 && 
770         new_size > b->file[p->cat].head.block_max)
771     {
772         /* non-btree block will be removed */
773         p->deleted = 1;
774         close_block (b, p);
775         /* delete it too!! */
776         p = 0; /* make a new one anyway */
777     }
778     if (!p)
779     {   /* must create a new one */
780         int i;
781         for (i = 0; i < b->no_cat; i++)
782             if (new_size <= b->file[i].head.block_max)
783                 break;
784         if (i == b->no_cat)
785             i = b->no_cat - 1;
786         p = new_leaf (b, i);
787     }
788     if (new_size > b->file[p->cat].head.block_max)
789     {
790         char *first_dst;
791         char *cut_item = cut_item_buf;
792
793         assert (half1);
794         assert (half2);
795
796        /* first half */
797         p->size = half1 - dst_buf;
798         memcpy (p->bytes, dst_buf, half1 - dst_buf);
799
800         /* second half */
801         *sp2 = new_leaf (b, p->cat);
802
803         (*b->method->code_reset)(c2);
804
805         first_dst = (*sp2)->bytes;
806
807         (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
808
809         memcpy (first_dst, half2, dst - half2);
810
811         (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
812         (*sp2)->dirty = 1;
813         p->dirty = 1;
814         memcpy (sub_item, cut_item_buf, cut_item_size);
815         *sub_size = cut_item_size;
816     }
817     else
818     {
819         memcpy (p->bytes, dst_buf, dst - dst_buf);
820         p->size = new_size;
821     }
822     (*b->method->code_stop)(ISAMC_DECODE, c1);
823     (*b->method->code_stop)(ISAMC_ENCODE, c2);
824     *sp1 = p;
825     return more;
826 }
827
828 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
829                 int *mode,
830                 ISAMC_I *stream,
831                 struct ISAMB_block **sp,
832                 void *sub_item, int *sub_size,
833                 void *max_item)
834 {
835     if (!*p || (*p)->leaf)
836         return insert_leaf (b, p, new_item, mode, stream, sp, sub_item, 
837                             sub_size, max_item);
838     else
839         return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
840                            sub_size, max_item);
841 }
842
843 int isamb_unlink (ISAMB b, ISAMC_P pos)
844 {
845     struct ISAMB_block *p1;
846
847     if (!pos)
848         return 0;
849     p1 = open_block(b, pos);
850     p1->deleted = 1;
851     if (!p1->leaf)
852     {
853         int sub_p;
854         int item_len;
855         char *src = p1->bytes + p1->offset;
856
857         decode_ptr(&src, &sub_p);
858         isamb_unlink(b, sub_p);
859         
860         while (src != p1->bytes + p1->size)
861         {
862             decode_ptr(&src, &item_len);
863             src += item_len;
864             decode_ptr(&src, &sub_p);
865             isamb_unlink(b, sub_p);
866         }
867     }
868     close_block(b, p1);
869     return 0;
870 }
871
872 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
873 {
874     char item_buf[DST_ITEM_MAX];
875     char *item_ptr;
876     int i_mode;
877     int more;
878
879     if (b->cache < 0)
880     {
881         int more = 1;
882         while (more)
883         {
884             item_ptr = item_buf;
885             more =
886                 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
887         }
888         return 1;
889     }
890     item_ptr = item_buf;
891     more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
892     while (more)
893     {
894         struct ISAMB_block *p = 0, *sp = 0;
895         char sub_item[DST_ITEM_MAX];
896         int sub_size;
897         
898         if (pos)
899             p = open_block (b, pos);
900         more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
901                             sub_item, &sub_size, 0);
902         if (sp)
903         {    /* increase level of tree by one */
904             struct ISAMB_block *p2 = new_int (b, p->cat);
905             char *dst = p2->bytes + p2->size;
906             
907             encode_ptr (&dst, p->pos);
908             assert (sub_size < 20);
909             encode_ptr (&dst, sub_size);
910             memcpy (dst, sub_item, sub_size);
911             dst += sub_size;
912             encode_ptr (&dst, sp->pos);
913             
914             p2->size = dst - p2->bytes;
915             pos = p2->pos;  /* return new super page */
916             close_block (b, sp);
917             close_block (b, p2);
918         }
919         else
920             pos = p->pos;   /* return current one (again) */
921         close_block (b, p);
922     }
923     return pos;
924 }
925
926 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
927 {
928     ISAMB_PP pp = xmalloc (sizeof(*pp));
929     int i;
930
931     pp->isamb = isamb;
932     pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
933
934     pp->pos = pos;
935     pp->level = 0;
936     pp->maxlevel=0;
937     pp->total_size = 0;
938     pp->no_blocks = 0;
939     pp->skipped_numbers=0;
940     pp->returned_numbers=0;
941     for (i=0;i<ISAMB_MAX_LEVEL;i++)
942         pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
943     while (1)
944     {
945         struct ISAMB_block *p = open_block (isamb, pos);
946         char *src = p->bytes + p->offset;
947         pp->block[pp->level] = p;
948
949         pp->total_size += p->size;
950         pp->no_blocks++;
951         if (p->leaf)
952             break;
953
954                                         
955         decode_ptr (&src, &pos);
956         p->offset = src - p->bytes;
957         pp->level++;
958         pp->accessed_nodes[pp->level]++; 
959     }
960     pp->block[pp->level+1] = 0;
961     pp->maxlevel=pp->level;
962     if (level)
963         *level = pp->level;
964     return pp;
965 }
966
967 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
968 {
969     return isamb_pp_open_x (isamb, pos, 0);
970 }
971
972 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
973 {
974     int i;
975     if (!pp)
976         return;
977     logf(LOG_DEBUG,"isamb_pp_close lev=%d returned %d values, skipped %d",
978         pp->maxlevel, pp->skipped_numbers, pp->returned_numbers);
979     for (i=pp->maxlevel;i>=0;i--)
980         if ( pp->skipped_nodes[i] || pp->accessed_nodes[i])
981             logf(LOG_DEBUG,"isamb_pp_close  level leaf-%d: %d read, %d skipped", i,
982                  pp->accessed_nodes[i], pp->skipped_nodes[i]);
983     pp->isamb->skipped_numbers += pp->skipped_numbers;
984     pp->isamb->returned_numbers += pp->returned_numbers;
985     for (i=pp->maxlevel;i>=0;i--)
986     {
987         pp->isamb->accessed_nodes[i] += pp->accessed_nodes[i];
988         pp->isamb->skipped_nodes[i] += pp->skipped_nodes[i];
989     }
990     if (size)
991         *size = pp->total_size;
992     if (blocks)
993         *blocks = pp->no_blocks;
994     for (i = 0; i <= pp->level; i++)
995         close_block (pp->isamb, pp->block[i]);
996     xfree (pp->block);
997     xfree (pp);
998 }
999
1000 int isamb_block_info (ISAMB isamb, int cat)
1001 {
1002     if (cat >= 0 && cat < isamb->no_cat)
1003         return isamb->file[cat].head.block_size;
1004     return -1;
1005 }
1006
1007 void isamb_pp_close (ISAMB_PP pp)
1008 {
1009     isamb_pp_close_x (pp, 0, 0);
1010 }
1011
1012 /* simple recursive dumper .. */
1013 static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
1014                           int level)
1015 {
1016     char buf[1024];
1017     char prefix_str[1024];
1018     if (pos)
1019     {
1020         struct ISAMB_block *p = open_block (b, pos);
1021         sprintf(prefix_str, "%*s %d cat=%d size=%d max=%d", level*2, "",
1022                 pos, p->cat, p->size, b->file[p->cat].head.block_max);
1023         (*pr)(prefix_str);
1024         sprintf(prefix_str, "%*s %d", level*2, "", pos);
1025         if (p->leaf)
1026         {
1027             while (p->offset < p->size)
1028             {
1029                 char *src = p->bytes + p->offset;
1030                 char *dst = buf;
1031                 (*b->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1032                                         &dst, &src);
1033                 (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
1034                 p->offset = src - (char*) p->bytes;
1035             }
1036             assert(p->offset == p->size);
1037         }
1038         else
1039         {
1040             char *src = p->bytes + p->offset;
1041             int sub;
1042             int item_len;
1043
1044             decode_ptr (&src, &sub);
1045             p->offset = src - (char*) p->bytes;
1046
1047             isamb_dump_r(b, sub, pr, level+1);
1048             
1049             while (p->offset < p->size)
1050             {
1051                 decode_ptr (&src, &item_len);
1052                 (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
1053                 src += item_len;
1054                 decode_ptr (&src, &sub);
1055                 
1056                 p->offset = src - (char*) p->bytes;
1057                 
1058                 isamb_dump_r(b, sub, pr, level+1);
1059             }           
1060         }
1061         close_block(b,p);
1062     }
1063 }
1064
1065 void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
1066 {
1067     return isamb_dump_r(b, pos, pr, 0);
1068 }
1069
1070 #if 0
1071 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
1072    debug the more complex pp_read that also forwards. May be deleted near end
1073    of 2004, if it has not shown to be useful */
1074
1075
1076 int isamb_pp_read (ISAMB_PP pp, void *buf)
1077 {
1078     char *dst = buf;
1079     char *src;
1080     struct ISAMB_block *p = pp->block[pp->level];
1081     if (!p)
1082         return 0;
1083
1084     while (p->offset == p->size)
1085     {
1086         int pos, item_len;
1087         while (p->offset == p->size)
1088         {
1089             if (pp->level == 0)
1090                 return 0;
1091             close_block (pp->isamb, pp->block[pp->level]);
1092             pp->block[pp->level] = 0;
1093             (pp->level)--;
1094             p = pp->block[pp->level];
1095             assert (!p->leaf);  
1096         }
1097         src = p->bytes + p->offset;
1098         
1099         decode_ptr (&src, &item_len);
1100         src += item_len;
1101         decode_ptr (&src, &pos);
1102         
1103         p->offset = src - (char*) p->bytes;
1104
1105         ++(pp->level);
1106         
1107         while (1)
1108         {
1109             pp->block[pp->level] = p = open_block (pp->isamb, pos);
1110
1111             pp->total_size += p->size;
1112             pp->no_blocks++;
1113             
1114             if (p->leaf) 
1115             {
1116                 break;
1117             }
1118             src = p->bytes + p->offset;
1119             decode_ptr (&src, &pos);
1120             p->offset = src - (char*) p->bytes;
1121             pp->level++;
1122         }
1123     }
1124     assert (p->offset < p->size);
1125     assert (p->leaf);
1126     src = p->bytes + p->offset;
1127     (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1128                                     &dst, &src);
1129     p->offset = src - (char*) p->bytes;
1130     /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1131     return 1;
1132 }
1133
1134 #else
1135 int isamb_pp_read (ISAMB_PP pp, void *buf)
1136 {
1137     return isamb_pp_forward(pp,buf,0);
1138 }
1139 #endif
1140
1141 #define NEW_FORWARD 0
1142
1143 #if NEW_FORWARD == 1
1144
1145 static int isamb_pp_read_on_leaf(ISAMB_PP pp, void *buf)
1146 { /* reads the next item on the current leaf, returns 0 if end of leaf*/
1147     struct ISAMB_block *p = pp->block[pp->level];
1148     char *dst;
1149     char *src;
1150     if (p->offset == p->size) {
1151 #if ISAMB_DEBUG
1152         logf(LOG_DEBUG,"isamb_pp_read_on_leaf returning 0 on node %d",p->pos);
1153 #endif
1154         return 0; /* at end of leaf */
1155     }
1156     src=p->bytes + p->offset;
1157     dst=buf;
1158     (*pp->isamb->method->code_item)
1159            (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1160     p->offset = src - (char*) p->bytes;
1161 #if ISAMB_DEBUG
1162     (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "read_on_leaf returning 1");
1163 #endif
1164     return 1;
1165 } /* read_on_leaf */
1166
1167
1168 static int isamb_pp_climb_level(ISAMB_PP pp, int *pos)
1169 { /* climbs higher in the tree, until finds a level with data left */
1170   /* returns the node to (consider to) descend to in *pos) */
1171     struct ISAMB_block *p = pp->block[pp->level];
1172     char *src;
1173     int item_len;
1174     assert(p->offset <= p->size);
1175     if (pp->level==0)
1176     {
1177 #if ISAMB_DEBUG
1178         logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1179         return 0;
1180 #endif
1181     }
1182     close_block(pp->isamb, pp->block[pp->level]);
1183     pp->block[pp->level]=0;
1184     (pp->level)--;
1185     p=pp->block[pp->level];
1186     logf(LOG_DEBUG,"isamb_pp_climb_level climbed to node %d ofs=%d",
1187                     p->pos, p->offset);
1188     assert (!p->leaf);
1189     assert (p->offset <= p->size);
1190     if (p->offset == p->size ) {
1191         /* we came from the last pointer, climb on */
1192         if (!isamb_pp_climb_level(pp,pos))
1193             return 0;
1194         p=pp->block[pp->level];
1195     }
1196     /* skip the child we just came from */
1197     assert (p->offset < p->size );
1198     src=p->bytes + p->offset;
1199     decode_ptr(&src, &item_len);
1200     src += item_len;
1201     decode_ptr(&src, pos);
1202     p->offset=src - (char *)p->bytes;
1203     return 1;
1204 } /* climb_level */
1205
1206
1207 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1208 { /* scans a upper node until it finds a child <= untilbuf */
1209   /* pp points to the key value, as always. pos is the child read from */
1210   /* the buffer */
1211   /* if all values are too small, returns the last child in the node */
1212   /* FIXME - this can be detected, and avoided by looking at the */
1213   /* parent node, but that gets messy. Presumably the cost is */
1214   /* pretty low anyway */
1215     struct ISAMB_block *p = pp->block[pp->level];
1216     char *src=p->bytes + p->offset;
1217     int item_len;
1218     int cmp;
1219     int nxtpos;
1220     assert(!p->leaf);
1221     assert(p->offset <= p->size);
1222     if (p->offset == p->size)
1223         return pos; /* already at the end of it */
1224     while(p->offset < p->size) {
1225         decode_ptr(&src,&item_len);
1226         cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1227         src+=item_len;
1228         decode_ptr(&src,&nxtpos);
1229         if (cmp<2)
1230         {
1231             return pos;
1232         } /* found one */
1233         pos=nxtpos;
1234         p->offset=src-(char*)p->bytes;
1235     }
1236     return pos; /* that's the last one in the line */
1237     
1238 } /* forward_unode */
1239
1240 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1241 { /* climbs down the tree, from pos, to the leftmost leaf */
1242     struct ISAMB_block *p = pp->block[pp->level];
1243     char *src;
1244     assert(!p->leaf);
1245     ++(pp->level);
1246     p=open_block(pp->isamb, pos);
1247     pp->block[pp->level]=p;
1248     ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1249     ++(pp->no_blocks);
1250 #if ISAMB_DEBUG
1251     logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1252                    "got lev %d node %d lf=%d", 
1253                    pp->level, p->pos, p->leaf);
1254 #endif
1255     if (p->leaf)
1256         return;
1257     assert (p->offset==0 );
1258     src=p->bytes + p->offset;
1259     decode_ptr(&src, &pos);
1260     p->offset=src-(char*)p->bytes;
1261     if (untilbuf)
1262         pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1263     isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1264 } /* descend_to_leaf */
1265
1266 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1267 { /* finds the next leaf by climbing up and down */
1268     int pos;
1269     if (!isamb_pp_climb_level(pp,&pos))
1270         return 0;
1271     isamb_pp_descend_to_leaf(pp, pos,0);
1272     return 1;
1273 }
1274 static int isamb_pp_forward_on_leaf(ISAMB_PP pp, void *buf, const void *untilbuf)
1275 { /* forwards on the current leaf, returns 0 if not found */
1276     int cmp;
1277     int skips=0;
1278     while (1){
1279         if (!isamb_pp_read_on_leaf(pp,buf))
1280             return 0;
1281         /* FIXME - this is an extra function call, inline the read? */
1282         cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1283         if (cmp <2){  /* found a good one */
1284 #if ISAMB_DEBUG
1285             if (skips)
1286                 logf(LOG_DEBUG, "isam_pp_fwd_on_leaf skipped %d items",skips);
1287 #endif
1288             pp->returned_numbers++;
1289             return 1;
1290         }
1291         pp->skipped_numbers++;
1292         skips++;
1293     }
1294 } /* forward_on_leaf */
1295
1296 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1297 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1298     int pos;
1299     if (!isamb_pp_climb_level(pp,&pos))
1300         return 0;
1301     isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1302     return 1;
1303 } /* climb_desc */
1304
1305 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1306 {
1307 #if ISAMB_DEBUG
1308     struct ISAMB_block *p = pp->block[pp->level];
1309     assert(p->leaf);
1310 #endif
1311     if (untilbuf) {
1312         if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1313             return 1;
1314         if (! isamb_pp_climb_desc( pp, buf, untilbuf))
1315             return 0; /* could not find a leaf */
1316         do{
1317             if (isamb_pp_forward_on_leaf( pp, buf, untilbuf))
1318                 return 1;
1319         }while ( isamb_pp_find_next_leaf(pp));
1320         return 0; /* could not find at all */
1321     }
1322     else { /* no untilbuf, a straight read */
1323         /* FIXME - this should be moved
1324          * directly into the pp_read */
1325         /* keeping here now, to keep same
1326          * interface as the old fwd */
1327         if (isamb_pp_read_on_leaf( pp, buf))
1328             return 1;
1329         if (isamb_pp_find_next_leaf(pp))
1330             return isamb_pp_read_on_leaf(pp, buf);
1331         else
1332             return 0;
1333     }
1334 } /* isam_pp_forward (new version) */
1335
1336 #elif NEW_FORWARD == 0
1337
1338 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1339 {
1340     /* pseudocode:
1341      *   while 1
1342      *     while at end of node
1343      *       climb higher. If out, return 0
1344      *     while not on a leaf (and not at its end)
1345      *       decode next
1346      *       if cmp
1347      *         descend to node
1348      *     decode next
1349      *     if cmp
1350      *       return 1
1351      */
1352      /* 
1353       * The upper nodes consist of a sequence of nodenumbers and keys
1354       * When opening a block,  the first node number is read in, and
1355       * offset points to the first key, which is the upper limit of keys
1356       * in the node just read.
1357       */
1358     char *dst = buf;
1359     char *src;
1360     struct ISAMB_block *p = pp->block[pp->level];
1361     int cmp;
1362     int item_len;
1363     int pos;
1364     int nxtpos;
1365     int descending=0; /* used to prevent a border condition error */
1366     if (!p)
1367         return 0;
1368 #if ISAMB_DEBUG
1369     logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1370     
1371     (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1372     (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1373 #endif
1374
1375     while (1)
1376     {
1377         while ( (p->offset == p->size) && !descending )
1378         {  /* end of this block - climb higher */
1379             assert (p->offset <= p->size);
1380 #if ISAMB_DEBUG
1381             logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1382                             pp->level);
1383 #endif
1384             if (pp->level == 0)
1385             {
1386 #if ISAMB_DEBUG
1387                 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1388 #endif
1389                 return 0; /* at end of the root, nothing left */
1390             }
1391             close_block(pp->isamb, pp->block[pp->level]);
1392             pp->block[pp->level]=0;
1393             (pp->level)--;
1394             p=pp->block[pp->level];
1395 #if ISAMB_DEBUG
1396             logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1397                             p->pos, p->offset);
1398 #endif
1399             assert(!p->leaf);
1400             assert(p->offset <= p->size);
1401             /* skip the child we have handled */
1402             if (p->offset != p->size)
1403             { 
1404                 src = p->bytes + p->offset;
1405                 decode_ptr(&src, &item_len);
1406 #if ISAMB_DEBUG         
1407                 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1408                                                " isamb_pp_forward "
1409                                                "climb skipping old key");
1410 #endif
1411                 src += item_len;
1412                 decode_ptr(&src,&pos);
1413                 p->offset = src - (char*) p->bytes;
1414                 break; /* even if this puts us at the end of the block, we
1415                           need to descend to the last pos. UGLY coding,
1416                           clean up some day */
1417             }
1418         }
1419         if (!p->leaf)
1420         { 
1421             src = p->bytes + p->offset;
1422             if (p->offset == p->size)
1423                 cmp=-2 ; /* descend to the last node, as we have
1424                             no value to cmp */
1425             else
1426             {
1427                 decode_ptr(&src, &item_len);
1428 #if ISAMB_DEBUG
1429                 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1430                      "ofs=%d sz=%d nxtpos=%d ",
1431                         p->offset,p->size,pos);
1432                 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1433 #endif
1434                 if (untilbuf)
1435                     cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1436                 else
1437                     cmp=-2;
1438                 src += item_len;
1439                 decode_ptr(&src,&nxtpos);
1440             }
1441             if (cmp<2)
1442             { 
1443 #if ISAMB_DEBUG
1444                 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1445                             pp->level, pos);
1446 #endif
1447                 descending=1; /* prevent climbing for a while */
1448                 ++(pp->level);
1449                 p = open_block(pp->isamb,pos);
1450                 pp->block[pp->level] = p ;
1451                 pp->total_size += p->size;
1452                 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1453                 pp->no_blocks++;
1454                 if ( !p->leaf)
1455                 { /* block starts with a pos */
1456                     src = p->bytes + p->offset;
1457                     decode_ptr(&src,&pos);
1458                     p->offset=src-(char*) p->bytes;
1459 #if ISAMB_DEBUG
1460                     logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1461                                     p->pos, pos);
1462 #endif
1463                 } 
1464             } /* descend to the node */
1465             else
1466             { /* skip the node */
1467                 p->offset = src - (char*) p->bytes;
1468                 pos=nxtpos;
1469                 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1470 #if ISAMB_DEBUG
1471                 logf(LOG_DEBUG,
1472                     "isamb_pp_forward: skipping block on level %d, noting "
1473                      "on %d (%d)",
1474                     pp->level, pp->maxlevel - pp->level-1 , 
1475                     pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1476 #endif
1477                 /* 0 is always leafs, 1 is one level above leafs etc, no
1478                  * matter how high tree */
1479             }
1480         } /* not on a leaf */
1481         else
1482         { /* on a leaf */
1483             if (p->offset == p->size) { 
1484                 descending = 0;
1485             }
1486             else
1487             {
1488                 assert (p->offset < p->size);
1489                 src = p->bytes + p->offset;
1490                 dst=buf;
1491                 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1492                                                 &dst, &src);
1493                 p->offset = src - (char*) p->bytes;
1494                 if (untilbuf)
1495                     cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1496                 else
1497                     cmp=-2;
1498 #if ISAMB_DEBUG
1499                 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d", 
1500                      cmp);
1501                 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1502 #endif
1503                 if (cmp <2)
1504                 {
1505 #if ISAMB_DEBUG
1506                     if (untilbuf)
1507                     {
1508                         (*pp->isamb->method->log_item)(
1509                             LOG_DEBUG, buf,  "isamb_pp_forward returning 1");
1510                     }
1511                     else
1512                     {
1513                         (*pp->isamb->method->log_item)(
1514                             LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1515                     }
1516 #endif
1517                     pp->returned_numbers++;
1518                     return 1;
1519                 }
1520                 else
1521                     pp->skipped_numbers++;
1522             }
1523         } /* leaf */
1524     } /* main loop */
1525 }
1526
1527 #elif NEW_FORWARD == 2
1528
1529 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1530 {
1531     char *dst = buf;
1532     char *src;
1533     struct ISAMB_block *p = pp->block[pp->level];
1534     if (!p)
1535         return 0;
1536
1537     while (p->offset == p->size)
1538     {
1539         int pos, item_len;
1540         while (p->offset == p->size)
1541         {
1542             if (pp->level == 0)
1543                 return 0;
1544             close_block (pp->isamb, pp->block[pp->level]);
1545             pp->block[pp->level] = 0;
1546             (pp->level)--;
1547             p = pp->block[pp->level];
1548             assert (!p->leaf);  
1549         }
1550
1551         src = p->bytes + p->offset;
1552         
1553         decode_ptr (&src, &item_len);
1554         src += item_len;
1555         decode_ptr (&src, &pos);
1556         
1557         p->offset = src - (char*) p->bytes;
1558
1559         src = p->bytes + p->offset;
1560
1561         while(1)
1562         {
1563             if (!untilb || p->offset == p->size)
1564                 break;
1565             assert(p->offset < p->size);
1566             decode_ptr (&src, &item_len);
1567             if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1568                 break;
1569             src += item_len;
1570             decode_ptr (&src, &pos);
1571             p->offset = src - (char*) p->bytes;
1572         }
1573
1574         pp->level++;
1575
1576         while (1)
1577         {
1578             pp->block[pp->level] = p = open_block (pp->isamb, pos);
1579
1580             pp->total_size += p->size;
1581             pp->no_blocks++;
1582             
1583             if (p->leaf) 
1584             {
1585                 break;
1586             }
1587             
1588             src = p->bytes + p->offset;
1589             while(1)
1590             {
1591                 decode_ptr (&src, &pos);
1592                 p->offset = src - (char*) p->bytes;
1593                 
1594                 if (!untilb || p->offset == p->size)
1595                     break;
1596                 assert(p->offset < p->size);
1597                 decode_ptr (&src, &item_len);
1598                 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1599                     break;
1600                 src += item_len;
1601             }
1602             pp->level++;
1603         }
1604     }
1605     assert (p->offset < p->size);
1606     assert (p->leaf);
1607     src = p->bytes + p->offset;
1608     (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1609                                     &dst, &src);
1610     p->offset = src - (char*) p->bytes;
1611     /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1612     return 1;
1613 }
1614
1615 #endif
1616
1617 int isamb_pp_num (ISAMB_PP pp)
1618 {
1619     return 1;
1620 }