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