Added framework for the pos calls. rsisamb and rsnull have a real one,
[idzebra-moved-to-github.git] / isamb / isamb.c
1 /* $Id: isamb.c,v 1.47 2004-08-03 14:54:41 heikki 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  0
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
121 #if ISAMB_PTR_CODEC
122 static void encode_ptr (char **dst, unsigned pos)
123 {
124     unsigned char *bp = (unsigned char*) *dst;
125
126     while (pos > 127)
127     {
128          *bp++ = 128 | (pos & 127);
129          pos = pos >> 7;
130     }
131     *bp++ = pos;
132     *dst = (char *) bp;
133 }
134 #else
135 static void encode_ptr (char **dst, unsigned pos)
136 {
137     memcpy(*dst, &pos, sizeof(pos));
138     (*dst) += sizeof(pos);
139 }
140 #endif
141
142 #if ISAMB_PTR_CODEC
143 static void decode_ptr (char **src1, int *pos)
144 {
145     unsigned char **src = (unsigned char **) src1;
146     unsigned d = 0;
147     unsigned char c;
148     unsigned r = 0;
149
150     while (((c = *(*src)++) & 128))
151     {
152         d += ((c & 127) << r);
153         r += 7;
154     }
155     d += (c << r);
156     *pos = d;
157 }
158 #else
159 static void decode_ptr (char **src, int *pos)
160 {
161      memcpy (pos, *src, sizeof(*pos));
162      (*src) += sizeof(*pos);
163 }
164 #endif
165
166 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
167                   int cache)
168 {
169     ISAMB isamb = xmalloc (sizeof(*isamb));
170     int i, b_size = 32;
171
172     isamb->bfs = bfs;
173     isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
174     memcpy (isamb->method, method, sizeof(*method));
175     isamb->no_cat = CAT_NO;
176     isamb->log_io = 0;
177     isamb->log_freelist = 0;
178     isamb->cache = cache;
179     isamb->skipped_numbers=0;
180     isamb->returned_numbers=0;
181     for (i=0;i<ISAMB_MAX_LEVEL;i++)
182       isamb->skipped_nodes[i]= isamb->accessed_nodes[i]=0;
183
184     assert (cache == 0);
185     isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
186     for (i = 0; i<isamb->no_cat; i++)
187     {
188         char fname[DST_BUF_SIZE];
189         isamb->file[i].cache_entries = 0;
190         isamb->file[i].head_dirty = 0;
191         sprintf (fname, "%s%c", name, i+'A');
192         if (cache)
193             isamb->file[i].bf = bf_open (bfs, fname, ISAMB_CACHE_ENTRY_SIZE,
194                                          writeflag);
195         else
196             isamb->file[i].bf = bf_open (bfs, fname, b_size, writeflag);
197
198         
199         if (!bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
200                       &isamb->file[i].head))
201         {
202             isamb->file[i].head.first_block = ISAMB_CACHE_ENTRY_SIZE/b_size+1;
203             isamb->file[i].head.last_block = isamb->file[i].head.first_block;
204             isamb->file[i].head.block_size = b_size;
205             isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
206             isamb->file[i].head.free_list = 0;
207         }
208         assert (isamb->file[i].head.block_size >= ISAMB_DATA_OFFSET);
209         isamb->file[i].head_dirty = 0;
210         assert(isamb->file[i].head.block_size == b_size);
211         b_size = b_size * 4;
212     }
213 #if ISAMB_DEBUG
214     logf(LOG_WARN, "isamb debug enabled. Things will be slower than usual");
215 #endif
216     return isamb;
217 }
218
219 static void flush_blocks (ISAMB b, int cat)
220 {
221     while (b->file[cat].cache_entries)
222     {
223         struct ISAMB_cache_entry *ce_this = b->file[cat].cache_entries;
224         b->file[cat].cache_entries = ce_this->next;
225
226         if (ce_this->dirty)
227         {
228             yaz_log (b->log_io, "bf_write: flush_blocks");
229             bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
230         }
231         xfree (ce_this->buf);
232         xfree (ce_this);
233     }
234 }
235
236 static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
237 {
238     int cat = pos&CAT_MASK;
239     int off = ((pos/CAT_MAX) & 
240                (ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
241         * b->file[cat].head.block_size;
242     int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
243     int no = 0;
244     struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
245
246     if (!b->cache)
247         return 0;
248
249     assert (ISAMB_CACHE_ENTRY_SIZE >= b->file[cat].head.block_size);
250     for (ce = &b->file[cat].cache_entries; *ce; ce = &(*ce)->next, no++)
251     {
252         ce_last = ce;
253         if ((*ce)->pos == norm)
254         {
255             ce_this = *ce;
256             *ce = (*ce)->next;   /* remove from list */
257             
258             ce_this->next = b->file[cat].cache_entries;  /* move to front */
259             b->file[cat].cache_entries = ce_this;
260             
261             if (wr)
262             {
263                 memcpy (ce_this->buf + off, userbuf, 
264                         b->file[cat].head.block_size);
265                 ce_this->dirty = 1;
266             }
267             else
268                 memcpy (userbuf, ce_this->buf + off,
269                         b->file[cat].head.block_size);
270             return 1;
271         }
272     }
273     if (no >= 40)
274     {
275         assert (no == 40);
276         assert (ce_last && *ce_last);
277         ce_this = *ce_last;
278         *ce_last = 0;  /* remove the last entry from list */
279         if (ce_this->dirty)
280         {
281             yaz_log (b->log_io, "bf_write: get_block");
282             bf_write (b->file[cat].bf, ce_this->pos, 0, 0, ce_this->buf);
283         }
284         xfree (ce_this->buf);
285         xfree (ce_this);
286     }
287     ce_this = xmalloc (sizeof(*ce_this));
288     ce_this->next = b->file[cat].cache_entries;
289     b->file[cat].cache_entries = ce_this;
290     ce_this->buf = xmalloc (ISAMB_CACHE_ENTRY_SIZE);
291     ce_this->pos = norm;
292     yaz_log (b->log_io, "bf_read: get_block");
293     if (!bf_read (b->file[cat].bf, norm, 0, 0, ce_this->buf))
294         memset (ce_this->buf, 0, ISAMB_CACHE_ENTRY_SIZE);
295     if (wr)
296     {
297         memcpy (ce_this->buf + off, userbuf, b->file[cat].head.block_size);
298         ce_this->dirty = 1;
299     }
300     else
301     {
302         ce_this->dirty = 0;
303         memcpy (userbuf, ce_this->buf + off, b->file[cat].head.block_size);
304     }
305     return 1;
306 }
307
308
309 void isamb_close (ISAMB isamb)
310 {
311     int i;
312     for (i=0;isamb->accessed_nodes[i];i++)
313         logf(LOG_DEBUG,"isamb_close  level leaf-%d: %d read, %d skipped",
314              i, isamb->accessed_nodes[i], isamb->skipped_nodes[i]);
315     logf(LOG_DEBUG,"isamb_close returned %d values, skipped %d",
316          isamb->skipped_numbers, isamb->returned_numbers);
317     for (i = 0; i<isamb->no_cat; i++)
318     {
319         flush_blocks (isamb, i);
320         if (isamb->file[i].head_dirty)
321             bf_write (isamb->file[i].bf, 0, 0,
322                       sizeof(struct ISAMB_head), &isamb->file[i].head);
323         
324         bf_close (isamb->file[i].bf);
325     }
326     xfree (isamb->file);
327     xfree (isamb->method);
328     xfree (isamb);
329 }
330
331 static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
332 {
333     int cat = pos&CAT_MASK;
334     struct ISAMB_block *p;
335     if (!pos)
336         return 0;
337     p = xmalloc (sizeof(*p));
338     p->pos = pos;
339     p->cat = pos & CAT_MASK;
340     p->buf = xmalloc (b->file[cat].head.block_size);
341     p->cbuf = 0;
342
343     if (!get_block (b, pos, p->buf, 0))
344     {
345         yaz_log (b->log_io, "bf_read: open_block");
346         if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
347         {
348             yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
349                      (long) pos, (long) pos/CAT_MAX);
350             abort();
351         }
352     }
353     p->bytes = p->buf + ISAMB_DATA_OFFSET;
354     p->leaf = p->buf[0];
355     p->size = (p->buf[1] + 256 * p->buf[2]) - ISAMB_DATA_OFFSET;
356     if (p->size < 0)
357     {
358         yaz_log (LOG_FATAL, "Bad block size %d in pos=%d\n", p->size, pos);
359     }
360     assert (p->size >= 0);
361     p->offset = 0;
362     p->dirty = 0;
363     p->deleted = 0;
364     p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
365     yaz_log (LOG_DEBUG, "isamb_open_block: Opened block %d ofs=%d",pos, p->offset);
366     return p;
367 }
368
369 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
370 {
371     struct ISAMB_block *p;
372
373     p = xmalloc (sizeof(*p));
374     p->buf = xmalloc (b->file[cat].head.block_size);
375
376     if (!b->file[cat].head.free_list)
377     {
378         int block_no;
379         block_no = b->file[cat].head.last_block++;
380         p->pos = block_no * CAT_MAX + cat;
381     }
382     else
383     {
384         p->pos = b->file[cat].head.free_list;
385         assert((p->pos & CAT_MASK) == cat);
386         if (!get_block (b, p->pos, p->buf, 0))
387         {
388             yaz_log (b->log_io, "bf_read: new_block");
389             if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
390             {
391                 yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
392                          (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
393                 abort ();
394             }
395         }
396         yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
397                  cat, p->pos/CAT_MAX);
398         memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
399     }
400     p->cat = cat;
401     b->file[cat].head_dirty = 1;
402     memset (p->buf, 0, b->file[cat].head.block_size);
403     p->bytes = p->buf + ISAMB_DATA_OFFSET;
404     p->leaf = leaf;
405     p->size = 0;
406     p->dirty = 1;
407     p->deleted = 0;
408     p->offset = 0;
409     p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
410     return p;
411 }
412
413 struct ISAMB_block *new_leaf (ISAMB b, int cat)
414 {
415     return new_block (b, 1, cat);
416 }
417
418
419 struct ISAMB_block *new_int (ISAMB b, int cat)
420 {
421     return new_block (b, 0, cat);
422 }
423
424 static void check_block (ISAMB b, struct ISAMB_block *p)
425 {
426     if (p->leaf)
427     {
428         ;
429     }
430     else
431     {
432         /* sanity check */
433         char *startp = p->bytes;
434         char *src = startp;
435         char *endp = p->bytes + p->size;
436         int pos;
437             
438         decode_ptr (&src, &pos);
439         assert ((pos&CAT_MASK) == p->cat);
440         while (src != endp)
441         {
442             int item_len;
443             decode_ptr (&src, &item_len);
444             assert (item_len > 0 && item_len < 30);
445             src += item_len;
446             decode_ptr (&src, &pos);
447             assert ((pos&CAT_MASK) == p->cat);
448         }
449     }
450 }
451
452 void close_block (ISAMB b, struct ISAMB_block *p)
453 {
454     if (!p)
455         return;
456     if (p->deleted)
457     {
458         yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
459                  p->pos, p->cat, p->pos/CAT_MAX);
460         memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
461         b->file[p->cat].head.free_list = p->pos;
462         if (!get_block (b, p->pos, p->buf, 1))
463         {
464             yaz_log (b->log_io, "bf_write: close_block (deleted)");
465             bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
466         }
467     }
468     else if (p->dirty)
469     {
470         int size = p->size + ISAMB_DATA_OFFSET;
471         assert (p->size >= 0);
472         p->buf[0] = p->leaf;
473         p->buf[1] = size & 255;
474         p->buf[2] = size >> 8;
475         check_block(b, p);
476         if (!get_block (b, p->pos, p->buf, 1))
477         {
478             yaz_log (b->log_io, "bf_write: close_block");
479             bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
480         }
481     }
482     (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
483     xfree (p->buf);
484     xfree (p);
485 }
486
487 int insert_sub (ISAMB b, struct ISAMB_block **p,
488                 void *new_item, int *mode,
489                 ISAMC_I *stream,
490                 struct ISAMB_block **sp,
491                 void *sub_item, int *sub_size,
492                 void *max_item);
493
494 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
495                 int *mode,
496                 ISAMC_I *stream, struct ISAMB_block **sp,
497                 void *split_item, int *split_size, void *last_max_item)
498 {
499     char *startp = p->bytes;
500     char *src = startp;
501     char *endp = p->bytes + p->size;
502     int pos;
503     struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
504     char sub_item[DST_ITEM_MAX];
505     int sub_size;
506     int more = 0;
507
508     *sp = 0;
509
510     assert(p->size >= 0);
511     decode_ptr (&src, &pos);
512     while (src != endp)
513     {
514         int item_len;
515         int d;
516         char *src0 = src;
517         decode_ptr (&src, &item_len);
518         d = (*b->method->compare_item)(src, lookahead_item);
519         if (d > 0)
520         {
521             sub_p1 = open_block (b, pos);
522             assert (sub_p1);
523             more = insert_sub (b, &sub_p1, lookahead_item, mode,
524                                stream, &sub_p2, 
525                                sub_item, &sub_size, src);
526             src = src0;
527             break;
528         }
529         src += item_len;
530         decode_ptr (&src, &pos);
531     }
532     if (!sub_p1)
533     {
534         sub_p1 = open_block (b, pos);
535         assert (sub_p1);
536         more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2, 
537                            sub_item, &sub_size, last_max_item);
538     }
539     if (sub_p2)
540     {
541         /* there was a split - must insert pointer in this one */
542         char dst_buf[DST_BUF_SIZE];
543         char *dst = dst_buf;
544
545         assert (sub_size < 30 && sub_size > 1);
546
547         memcpy (dst, startp, src - startp);
548                 
549         dst += src - startp;
550
551         encode_ptr (&dst, sub_size);      /* sub length and item */
552         memcpy (dst, sub_item, sub_size);
553         dst += sub_size;
554
555         encode_ptr (&dst, sub_p2->pos);   /* pos */
556
557         if (endp - src)                   /* remaining data */
558         {
559             memcpy (dst, src, endp - src);
560             dst += endp - src;
561         }
562         p->size = dst - dst_buf;
563         assert (p->size >= 0);
564         if (p->size <= b->file[p->cat].head.block_max)
565         {
566             memcpy (startp, dst_buf, dst - dst_buf);
567         }
568         else
569         {
570             int p_new_size;
571             char *half;
572             src = dst_buf;
573             endp = dst;
574
575             half = src + b->file[p->cat].head.block_size/2;
576             decode_ptr (&src, &pos);
577             while (src <= half)
578             {
579                 decode_ptr (&src, split_size);
580                 src += *split_size;
581                 decode_ptr (&src, &pos);
582             }
583             p_new_size = src - dst_buf;
584             memcpy (p->bytes, dst_buf, p_new_size);
585
586             decode_ptr (&src, split_size);
587             memcpy (split_item, src, *split_size);
588             src += *split_size;
589
590             *sp = new_int (b, p->cat);
591             (*sp)->size = endp - src;
592             memcpy ((*sp)->bytes, src, (*sp)->size);
593
594             p->size = p_new_size;
595         }
596         p->dirty = 1;
597         close_block (b, sub_p2);
598     }
599     close_block (b, sub_p1);
600     return more;
601 }
602
603
604 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
605                  int *lookahead_mode, ISAMC_I *stream,
606                  struct ISAMB_block **sp2,
607                  void *sub_item, int *sub_size,
608                  void *max_item)
609 {
610     struct ISAMB_block *p = *sp1;
611     char *src = 0, *endp = 0;
612     char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
613     int new_size;
614     void *c1 = (*b->method->code_start)(ISAMC_DECODE);
615     void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
616     int more = 1;
617     int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
618     char *cut = dst_buf + quater * 2;
619     char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
620     char *half1 = 0;
621     char *half2 = 0;
622     char cut_item_buf[DST_ITEM_MAX];
623     int cut_item_size = 0;
624
625     if (p && p->size)
626     {
627         char file_item_buf[DST_ITEM_MAX];
628         char *file_item = file_item_buf;
629             
630         src = p->bytes;
631         endp = p->bytes + p->size;
632         (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
633         while (1)
634         {
635             char *dst_item = 0;
636             char *dst_0 = dst;
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                 {
676                     dst = dst_0;
677                     lookahead_item = 0;
678                 }
679                 else
680                 {
681                     lookahead_next = lookahead_item;
682                     if (!(*stream->read_item)(stream->clientData,
683                                               &lookahead_next,
684                                               lookahead_mode))
685                     {
686                         lookahead_item = 0;
687                         more = 0;
688                     }
689                     if (lookahead_item && max_item &&
690                         (*b->method->compare_item)(max_item, lookahead_item) <= 0)
691                     {
692                         /* max_item 1 */
693                         lookahead_item = 0;
694                     }
695                     
696                     p->dirty = 1;
697                 }
698             }
699             else if (d == 0)
700             {
701                 lookahead_next = lookahead_item;
702                 if (!(*stream->read_item)(stream->clientData,
703                                           &lookahead_next, lookahead_mode))
704                 {
705                     lookahead_item = 0;
706                     more = 0;
707                 }
708                 if (src == endp)
709                     break;
710                 file_item = file_item_buf;
711                 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
712             }
713             else
714             {
715                 if (src == endp)
716                     break;
717                 file_item = file_item_buf;
718                 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
719             }
720         }
721     }
722     maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
723     while (lookahead_item)
724     {
725         char *dst_item = lookahead_item;
726         char *dst_0 = dst;
727         
728         if (max_item &&
729             (*b->method->compare_item)(max_item, lookahead_item) <= 0)
730         {
731             /* max_item 2 */
732             break;
733         }
734         if (!*lookahead_mode)
735         {
736             yaz_log (LOG_WARN, "isamb: Inconsistent register (2)");
737             abort();
738         }
739         else if (!half1 && dst > cut)   
740         {
741             char *dst_item_0 = dst_item;
742             half1 = dst; /* candidate for splitting */
743             
744             (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
745             
746             cut_item_size = dst_item - dst_item_0;
747             memcpy (cut_item_buf, dst_item_0, cut_item_size);
748             
749             half2 = dst;
750         }
751         else
752             (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
753
754         if (dst > maxp)
755         {
756             dst = dst_0;
757             break;
758         }
759         if (p)
760             p->dirty = 1;
761         dst_item = lookahead_item;
762         if (!(*stream->read_item)(stream->clientData, &dst_item,
763                                   lookahead_mode))
764         {
765             lookahead_item = 0;
766             more = 0;
767         }
768     }
769     new_size = dst - dst_buf;
770     if (p && p->cat != b->no_cat-1 && 
771         new_size > b->file[p->cat].head.block_max)
772     {
773         /* non-btree block will be removed */
774         p->deleted = 1;
775         close_block (b, p);
776         /* delete it too!! */
777         p = 0; /* make a new one anyway */
778     }
779     if (!p)
780     {   /* must create a new one */
781         int i;
782         for (i = 0; i < b->no_cat; i++)
783             if (new_size <= b->file[i].head.block_max)
784                 break;
785         if (i == b->no_cat)
786             i = b->no_cat - 1;
787         p = new_leaf (b, i);
788     }
789     if (new_size > b->file[p->cat].head.block_max)
790     {
791         char *first_dst;
792         char *cut_item = cut_item_buf;
793
794         assert (half1);
795         assert (half2);
796
797        /* first half */
798         p->size = half1 - dst_buf;
799         memcpy (p->bytes, dst_buf, half1 - dst_buf);
800
801         /* second half */
802         *sp2 = new_leaf (b, p->cat);
803
804         (*b->method->code_reset)(c2);
805
806         first_dst = (*sp2)->bytes;
807
808         (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
809
810         memcpy (first_dst, half2, dst - half2);
811
812         (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
813         (*sp2)->dirty = 1;
814         p->dirty = 1;
815         memcpy (sub_item, cut_item_buf, cut_item_size);
816         *sub_size = cut_item_size;
817     }
818     else
819     {
820         memcpy (p->bytes, dst_buf, dst - dst_buf);
821         p->size = new_size;
822     }
823     (*b->method->code_stop)(ISAMC_DECODE, c1);
824     (*b->method->code_stop)(ISAMC_ENCODE, c2);
825     *sp1 = p;
826     return more;
827 }
828
829 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
830                 int *mode,
831                 ISAMC_I *stream,
832                 struct ISAMB_block **sp,
833                 void *sub_item, int *sub_size,
834                 void *max_item)
835 {
836     if (!*p || (*p)->leaf)
837         return insert_leaf (b, p, new_item, mode, stream, sp, sub_item, 
838                             sub_size, max_item);
839     else
840         return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
841                            sub_size, max_item);
842 }
843
844 int isamb_unlink (ISAMB b, ISAMC_P pos)
845 {
846     struct ISAMB_block *p1;
847
848     if (!pos)
849         return 0;
850     p1 = open_block(b, pos);
851     p1->deleted = 1;
852     if (!p1->leaf)
853     {
854         int sub_p;
855         int item_len;
856         char *src = p1->bytes + p1->offset;
857
858         decode_ptr(&src, &sub_p);
859         isamb_unlink(b, sub_p);
860         
861         while (src != p1->bytes + p1->size)
862         {
863             decode_ptr(&src, &item_len);
864             src += item_len;
865             decode_ptr(&src, &sub_p);
866             isamb_unlink(b, sub_p);
867         }
868     }
869     close_block(b, p1);
870     return 0;
871 }
872
873 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
874 {
875     char item_buf[DST_ITEM_MAX];
876     char *item_ptr;
877     int i_mode;
878     int more;
879
880     if (b->cache < 0)
881     {
882         int more = 1;
883         while (more)
884         {
885             item_ptr = item_buf;
886             more =
887                 (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
888         }
889         return 1;
890     }
891     item_ptr = item_buf;
892     more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
893     while (more)
894     {
895         struct ISAMB_block *p = 0, *sp = 0;
896         char sub_item[DST_ITEM_MAX];
897         int sub_size;
898         
899         if (pos)
900             p = open_block (b, pos);
901         more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
902                             sub_item, &sub_size, 0);
903         if (sp)
904         {    /* increase level of tree by one */
905             struct ISAMB_block *p2 = new_int (b, p->cat);
906             char *dst = p2->bytes + p2->size;
907             
908             encode_ptr (&dst, p->pos);
909             assert (sub_size < 20);
910             encode_ptr (&dst, sub_size);
911             memcpy (dst, sub_item, sub_size);
912             dst += sub_size;
913             encode_ptr (&dst, sp->pos);
914             
915             p2->size = dst - p2->bytes;
916             pos = p2->pos;  /* return new super page */
917             close_block (b, sp);
918             close_block (b, p2);
919         }
920         else
921             pos = p->pos;   /* return current one (again) */
922         close_block (b, p);
923     }
924     return pos;
925 }
926
927 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
928 {
929     ISAMB_PP pp = xmalloc (sizeof(*pp));
930     int i;
931
932     pp->isamb = isamb;
933     pp->block = xmalloc (ISAMB_MAX_LEVEL * sizeof(*pp->block));
934
935     pp->pos = pos;
936     pp->level = 0;
937     pp->maxlevel=0;
938     pp->total_size = 0;
939     pp->no_blocks = 0;
940     pp->skipped_numbers=0;
941     pp->returned_numbers=0;
942     for (i=0;i<ISAMB_MAX_LEVEL;i++)
943         pp->skipped_nodes[i] = pp->accessed_nodes[i]=0;
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                                         
956         decode_ptr (&src, &pos);
957         p->offset = src - p->bytes;
958         pp->level++;
959         pp->accessed_nodes[pp->level]++; 
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     return 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->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     assert(p->offset <= p->size);
1266     if (pp->level==0)
1267     {
1268 #if ISAMB_DEBUG
1269         logf(LOG_DEBUG,"isamb_pp_climb_level returning 0 at root");
1270 #endif
1271         return 0;
1272     }
1273     assert(pp->level>0); 
1274     close_block(pp->isamb, pp->block[pp->level]);
1275     pp->block[pp->level]=0;
1276     (pp->level)--;
1277     p=pp->block[pp->level];
1278 #if ISAMB_DEBUG
1279     logf(LOG_DEBUG,"isamb_pp_climb_level climbed to level %d node %d ofs=%d",
1280                     pp->level, p->pos, p->offset);
1281 #endif
1282     assert(!p->leaf);
1283     assert(p->offset <= p->size);
1284     if (p->offset == p->size ) {
1285         /* we came from the last pointer, climb on */
1286         if (!isamb_pp_climb_level(pp,pos))
1287             return 0;
1288         p=pp->block[pp->level];
1289     }
1290     else
1291     {
1292         /* skip the child we just came from */
1293 #if ISAMB_DEBUG
1294         logf(LOG_DEBUG,"isam_pp_climb_level: skipping lev=%d ofs=%d sz=%d", 
1295                         pp->level, p->offset, p->size);
1296 #endif
1297         assert (p->offset < p->size );
1298         src=p->bytes + p->offset;
1299         decode_ptr(&src, &item_len);
1300         src += item_len;
1301         decode_ptr(&src, pos);
1302         p->offset=src - (char *)p->bytes;
1303             
1304     }
1305     return 1;
1306 } /* climb_level */
1307
1308
1309 static int isamb_pp_forward_unode(ISAMB_PP pp, int pos, const void *untilbuf)
1310 { /* scans a upper node until it finds a child <= untilbuf */
1311   /* pp points to the key value, as always. pos is the child read from */
1312   /* the buffer */
1313   /* if all values are too small, returns the last child in the node */
1314   /* FIXME - this can be detected, and avoided by looking at the */
1315   /* parent node, but that gets messy. Presumably the cost is */
1316   /* pretty low anyway */
1317     struct ISAMB_block *p = pp->block[pp->level];
1318     char *src=p->bytes + p->offset;
1319     int item_len;
1320     int cmp;
1321     int nxtpos;
1322 #if ISAMB_DEBUG
1323     int skips=0;
1324     logf(LOG_DEBUG,"isamb_pp_forward_unode starting "
1325                    "at level %d node %d ofs=%di sz=%d",
1326                     pp->level, p->pos, p->offset, p->size);
1327 #endif
1328     assert(!p->leaf);
1329     assert(p->offset <= p->size);
1330     if (p->offset == p->size) {
1331 #if ISAMB_DEBUG
1332             logf(LOG_DEBUG,"isamb_pp_forward_unode returning at end "
1333                    "at level %d node %d ofs=%di sz=%d",
1334                     pp->level, p->pos, p->offset, p->size);
1335 #endif
1336         return pos; /* already at the end of it */
1337     }
1338     while(p->offset < p->size) {
1339         decode_ptr(&src,&item_len);
1340         cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1341         src+=item_len;
1342         decode_ptr(&src,&nxtpos);
1343         if (cmp<2)
1344         {
1345 #if ISAMB_DEBUG
1346             logf(LOG_DEBUG,"isamb_pp_forward_unode returning a hit "
1347                    "at level %d node %d ofs=%d sz=%d",
1348                     pp->level, p->pos, p->offset, p->size);
1349 #endif
1350             return pos;
1351         } /* found one */
1352         pos=nxtpos;
1353         p->offset=src-(char*)p->bytes;
1354         (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1355 #if ISAMB_DEBUG
1356         skips++;
1357 #endif
1358     }
1359 #if ISAMB_DEBUG
1360             logf(LOG_DEBUG,"isamb_pp_forward_unode returning at tail "
1361                    "at level %d node %d ofs=%d sz=%d skips=%d",
1362                     pp->level, p->pos, p->offset, p->size, skips);
1363 #endif
1364     return pos; /* that's the last one in the line */
1365     
1366 } /* forward_unode */
1367
1368 static void isamb_pp_descend_to_leaf(ISAMB_PP pp, int pos, const void *untilbuf)
1369 { /* climbs down the tree, from pos, to the leftmost leaf */
1370     struct ISAMB_block *p = pp->block[pp->level];
1371     char *src;
1372     assert(!p->leaf);
1373 #if ISAMB_DEBUG
1374     logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1375                    "starting at lev %d node %d ofs=%d lf=%d u=%p", 
1376                    pp->level, p->pos, p->offset, p->leaf, untilbuf);
1377 #endif
1378     if (untilbuf)
1379         pos=isamb_pp_forward_unode(pp,pos,untilbuf);
1380     ++(pp->level);
1381     assert(pos);
1382     p=open_block(pp->isamb, pos);
1383     pp->block[pp->level]=p;
1384     ++(pp->accessed_nodes[pp->maxlevel-pp->level]);
1385     ++(pp->no_blocks);
1386 #if ISAMB_DEBUG
1387     logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1388                    "got lev %d node %d lf=%d", 
1389                    pp->level, p->pos, p->leaf);
1390 #endif
1391     if (p->leaf)
1392         return;
1393     assert (p->offset==0 );
1394     src=p->bytes + p->offset;
1395     decode_ptr(&src, &pos);
1396     p->offset=src-(char*)p->bytes;
1397     isamb_pp_descend_to_leaf(pp,pos,untilbuf);
1398 #if ISAMB_DEBUG
1399     logf(LOG_DEBUG,"isamb_pp_descend_to_leaf "
1400                    "returning at lev %d node %d ofs=%d lf=%d", 
1401                    pp->level, p->pos, p->offset, p->leaf);
1402 #endif
1403 } /* descend_to_leaf */
1404
1405 static int isamb_pp_find_next_leaf(ISAMB_PP pp)
1406 { /* finds the next leaf by climbing up and down */
1407     int pos;
1408     if (!isamb_pp_climb_level(pp,&pos))
1409         return 0;
1410     isamb_pp_descend_to_leaf(pp, pos,0);
1411     return 1;
1412 }
1413
1414 static int isamb_pp_climb_desc(ISAMB_PP pp, void *buf, const void *untilbuf)
1415 { /* climbs up and descends to a leaf where values >= *untilbuf are found */
1416     int pos;
1417 #if ISAMB_DEBUG
1418     struct ISAMB_block *p = pp->block[pp->level];
1419     logf(LOG_DEBUG,"isamb_pp_climb_desc starting "
1420                    "at level %d node %d ofs=%d sz=%d",
1421                     pp->level, p->pos, p->offset, p->size);
1422 #endif
1423     if (!isamb_pp_climb_level(pp,&pos))
1424         return 0;
1425     /* see if it would pay to climb one higher */
1426     if (!isamb_pp_on_right_node(pp, pp->level, untilbuf))
1427         if (!isamb_pp_climb_level(pp,&pos))
1428             return 0;
1429     isamb_pp_descend_to_leaf(pp, pos,untilbuf);
1430 #if ISAMB_DEBUG
1431     p = pp->block[pp->level];
1432     logf(LOG_DEBUG,"isamb_pp_climb_desc done "
1433                    "at level %d node %d ofs=%d sz=%d",
1434                     pp->level, p->pos, p->offset, p->size);
1435 #endif
1436     return 1;
1437 } /* climb_desc */
1438
1439 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1440 {
1441 #if ISAMB_DEBUG
1442     struct ISAMB_block *p = pp->block[pp->level];
1443     assert(p->leaf);
1444     logf(LOG_DEBUG,"isamb_pp_forward starting "
1445                    "at level %d node %d ofs=%d sz=%d u=%p",
1446                     pp->level, p->pos, p->offset, p->size,untilbuf);
1447 #endif
1448     if (untilbuf) {
1449         if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1450 #if ISAMB_DEBUG
1451             logf(LOG_DEBUG,"isamb_pp_forward (f) returning (A) "
1452                    "at level %d node %d ofs=%d sz=%d",
1453                     pp->level, p->pos, p->offset, p->size);
1454 #endif
1455             return 1;
1456         }
1457         if (! isamb_pp_climb_desc( pp, buf, untilbuf)) {
1458 #if ISAMB_DEBUG
1459             logf(LOG_DEBUG,"isamb_pp_forward (f) returning notfound (B) "
1460                    "at level %d node %d ofs=%d sz=%d",
1461                     pp->level, p->pos, p->offset, p->size);
1462 #endif
1463             return 0; /* could not find a leaf */
1464         }
1465         do{
1466             if (isamb_pp_forward_on_leaf( pp, buf, untilbuf)) {
1467 #if ISAMB_DEBUG
1468             logf(LOG_DEBUG,"isamb_pp_forward (f) returning (C) "
1469                    "at level %d node %d ofs=%d sz=%d",
1470                     pp->level, p->pos, p->offset, p->size);
1471 #endif
1472                 return 1;
1473             }
1474         }while ( isamb_pp_find_next_leaf(pp));
1475         return 0; /* could not find at all */
1476     }
1477     else { /* no untilbuf, a straight read */
1478         /* FIXME - this should be moved
1479          * directly into the pp_read */
1480         /* keeping here now, to keep same
1481          * interface as the old fwd */
1482         if (isamb_pp_read_on_leaf( pp, buf)) {
1483 #if ISAMB_DEBUG
1484             logf(LOG_DEBUG,"isamb_pp_forward (read) returning (D) "
1485                    "at level %d node %d ofs=%d sz=%d",
1486                     pp->level, p->pos, p->offset, p->size);
1487 #endif
1488             return 1;
1489         }
1490         if (isamb_pp_find_next_leaf(pp)) {
1491 #if ISAMB_DEBUG
1492             logf(LOG_DEBUG,"isamb_pp_forward (read) returning (E) "
1493                    "at level %d node %d ofs=%d sz=%d",
1494                     pp->level, p->pos, p->offset, p->size);
1495 #endif
1496             return isamb_pp_read_on_leaf(pp, buf);
1497         }
1498         else
1499             return 0;
1500     }
1501 } /* isam_pp_forward (new version) */
1502
1503 #elif NEW_FORWARD == 0
1504
1505 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
1506 {
1507     /* pseudocode:
1508      *   while 1
1509      *     while at end of node
1510      *       climb higher. If out, return 0
1511      *     while not on a leaf (and not at its end)
1512      *       decode next
1513      *       if cmp
1514      *         descend to node
1515      *     decode next
1516      *     if cmp
1517      *       return 1
1518      */
1519      /* 
1520       * The upper nodes consist of a sequence of nodenumbers and keys
1521       * When opening a block,  the first node number is read in, and
1522       * offset points to the first key, which is the upper limit of keys
1523       * in the node just read.
1524       */
1525     char *dst = buf;
1526     char *src;
1527     struct ISAMB_block *p = pp->block[pp->level];
1528     int cmp;
1529     int item_len;
1530     int pos;
1531     int nxtpos;
1532     int descending=0; /* used to prevent a border condition error */
1533     if (!p)
1534         return 0;
1535 #if ISAMB_DEBUG
1536     logf(LOG_DEBUG,"isamb_pp_forward starting [%p] p=%d",pp,p->pos);
1537     
1538     (*pp->isamb->method->log_item)(LOG_DEBUG, untilbuf, "until");
1539     (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "buf");
1540 #endif
1541
1542     while (1)
1543     {
1544         while ( (p->offset == p->size) && !descending )
1545         {  /* end of this block - climb higher */
1546             assert (p->offset <= p->size);
1547 #if ISAMB_DEBUG
1548             logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
1549                             pp->level);
1550 #endif
1551             if (pp->level == 0)
1552             {
1553 #if ISAMB_DEBUG
1554                 logf(LOG_DEBUG,"isamb_pp_forward returning 0 at root");
1555 #endif
1556                 return 0; /* at end of the root, nothing left */
1557             }
1558             close_block(pp->isamb, pp->block[pp->level]);
1559             pp->block[pp->level]=0;
1560             (pp->level)--;
1561             p=pp->block[pp->level];
1562 #if ISAMB_DEBUG
1563             logf(LOG_DEBUG,"isamb_pp_forward climbed to node %d off=%d",
1564                             p->pos, p->offset);
1565 #endif
1566             assert(!p->leaf);
1567             assert(p->offset <= p->size);
1568             /* skip the child we have handled */
1569             if (p->offset != p->size)
1570             { 
1571                 src = p->bytes + p->offset;
1572                 decode_ptr(&src, &item_len);
1573 #if ISAMB_DEBUG         
1574                 (*pp->isamb->method->log_item)(LOG_DEBUG, src,
1575                                                " isamb_pp_forward "
1576                                                "climb skipping old key");
1577 #endif
1578                 src += item_len;
1579                 decode_ptr(&src,&pos);
1580                 p->offset = src - (char*) p->bytes;
1581                 break; /* even if this puts us at the end of the block, we
1582                           need to descend to the last pos. UGLY coding,
1583                           clean up some day */
1584             }
1585         }
1586         if (!p->leaf)
1587         { 
1588             src = p->bytes + p->offset;
1589             if (p->offset == p->size)
1590                 cmp=-2 ; /* descend to the last node, as we have
1591                             no value to cmp */
1592             else
1593             {
1594                 decode_ptr(&src, &item_len);
1595 #if ISAMB_DEBUG
1596                 logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
1597                      "ofs=%d sz=%d nxtpos=%d ",
1598                         p->offset,p->size,pos);
1599                 (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
1600 #endif
1601                 if (untilbuf)
1602                     cmp=(*pp->isamb->method->compare_item)(untilbuf,src);
1603                 else
1604                     cmp=-2;
1605                 src += item_len;
1606                 decode_ptr(&src,&nxtpos);
1607             }
1608             if (cmp<2)
1609             { 
1610 #if ISAMB_DEBUG
1611                 logf(LOG_DEBUG,"isambb_pp_forward descending l=%d p=%d ",
1612                             pp->level, pos);
1613 #endif
1614                 descending=1; /* prevent climbing for a while */
1615                 ++(pp->level);
1616                 p = open_block(pp->isamb,pos);
1617                 pp->block[pp->level] = p ;
1618                 pp->total_size += p->size;
1619                 (pp->accessed_nodes[pp->maxlevel - pp->level])++;
1620                 pp->no_blocks++;
1621                 if ( !p->leaf)
1622                 { /* block starts with a pos */
1623                     src = p->bytes + p->offset;
1624                     decode_ptr(&src,&pos);
1625                     p->offset=src-(char*) p->bytes;
1626 #if ISAMB_DEBUG
1627                     logf(LOG_DEBUG,"isamb_pp_forward: block %d starts with %d",
1628                                     p->pos, pos);
1629 #endif
1630                 } 
1631             } /* descend to the node */
1632             else
1633             { /* skip the node */
1634                 p->offset = src - (char*) p->bytes;
1635                 pos=nxtpos;
1636                 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
1637 #if ISAMB_DEBUG
1638                 logf(LOG_DEBUG,
1639                     "isamb_pp_forward: skipping block on level %d, noting "
1640                      "on %d (%d)",
1641                     pp->level, pp->maxlevel - pp->level-1 , 
1642                     pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
1643 #endif
1644                 /* 0 is always leafs, 1 is one level above leafs etc, no
1645                  * matter how high tree */
1646             }
1647         } /* not on a leaf */
1648         else
1649         { /* on a leaf */
1650             if (p->offset == p->size) { 
1651                 descending = 0;
1652             }
1653             else
1654             {
1655                 assert (p->offset < p->size);
1656                 src = p->bytes + p->offset;
1657                 dst=buf;
1658                 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1659                                                 &dst, &src);
1660                 p->offset = src - (char*) p->bytes;
1661                 if (untilbuf)
1662                     cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
1663                 else
1664                     cmp=-2;
1665 #if ISAMB_DEBUG
1666                 logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d", 
1667                      cmp);
1668                 (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
1669 #endif
1670                 if (cmp <2)
1671                 {
1672 #if ISAMB_DEBUG
1673                     if (untilbuf)
1674                     {
1675                         (*pp->isamb->method->log_item)(
1676                             LOG_DEBUG, buf,  "isamb_pp_forward returning 1");
1677                     }
1678                     else
1679                     {
1680                         (*pp->isamb->method->log_item)(
1681                             LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
1682                     }
1683 #endif
1684                     pp->returned_numbers++;
1685                     return 1;
1686                 }
1687                 else
1688                     pp->skipped_numbers++;
1689             }
1690         } /* leaf */
1691     } /* main loop */
1692 }
1693
1694 #elif NEW_FORWARD == 2
1695
1696 int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilb)
1697 {
1698     char *dst = buf;
1699     char *src;
1700     struct ISAMB_block *p = pp->block[pp->level];
1701     if (!p)
1702         return 0;
1703
1704 again:
1705     while (p->offset == p->size)
1706     {
1707         int pos, item_len;
1708         while (p->offset == p->size)
1709         {
1710             if (pp->level == 0)
1711                 return 0;
1712             close_block (pp->isamb, pp->block[pp->level]);
1713             pp->block[pp->level] = 0;
1714             (pp->level)--;
1715             p = pp->block[pp->level];
1716             assert (!p->leaf);  
1717         }
1718
1719         assert(!p->leaf);
1720         src = p->bytes + p->offset;
1721         
1722         decode_ptr (&src, &item_len);
1723         src += item_len;
1724         decode_ptr (&src, &pos);
1725         
1726         p->offset = src - (char*) p->bytes;
1727
1728         src = p->bytes + p->offset;
1729
1730         while(1)
1731         {
1732             if (!untilb || p->offset == p->size)
1733                 break;
1734             assert(p->offset < p->size);
1735             decode_ptr (&src, &item_len);
1736             if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1737                 break;
1738             src += item_len;
1739             decode_ptr (&src, &pos);
1740             p->offset = src - (char*) p->bytes;
1741         }
1742
1743         pp->level++;
1744
1745         while (1)
1746         {
1747             pp->block[pp->level] = p = open_block (pp->isamb, pos);
1748
1749             pp->total_size += p->size;
1750             pp->no_blocks++;
1751             
1752             if (p->leaf) 
1753             {
1754                 break;
1755             }
1756             
1757             src = p->bytes + p->offset;
1758             while(1)
1759             {
1760                 decode_ptr (&src, &pos);
1761                 p->offset = src - (char*) p->bytes;
1762                 
1763                 if (!untilb || p->offset == p->size)
1764                     break;
1765                 assert(p->offset < p->size);
1766                 decode_ptr (&src, &item_len);
1767                 if ((*pp->isamb->method->compare_item)(untilb, src) <= 1)
1768                     break;
1769                 src += item_len;
1770             }
1771             pp->level++;
1772         }
1773     }
1774     assert (p->offset < p->size);
1775     assert (p->leaf);
1776     while(1)
1777     {
1778         char *dst0 = dst;
1779         src = p->bytes + p->offset;
1780         (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
1781                                     &dst, &src);
1782         p->offset = src - (char*) p->bytes;
1783         if (!untilb || (*pp->isamb->method->compare_item)(untilb, dst0) <= 1)
1784             break;
1785         dst = dst0;
1786         if (p->offset == p->size) goto again;
1787     }
1788     /* key_logdump_txt(LOG_DEBUG,buf, "isamb_pp_read returning 1"); */
1789     return 1;
1790 }
1791
1792 #endif
1793
1794 int isamb_pp_num (ISAMB_PP pp)
1795 {
1796     return 1;
1797 }
1798
1799 static void isamb_pp_leaf_pos( ISAMB_PP pp, 
1800                                int *current, int *total, void *dummybuf )
1801 {
1802     struct ISAMB_block *p = pp->block[pp->level];
1803     char *src=p->bytes;
1804     char *end=p->bytes+p->size;
1805     char *cur=p->bytes+p->offset;
1806     char *dst;
1807     assert(p->offset <= p->size);
1808     assert(cur <= end);
1809     assert(p->leaf);
1810     *current=0;
1811     *total=0;
1812
1813     while(src < end) {
1814         dst=dummybuf;
1815         (*pp->isamb->method->code_item)
1816            (ISAMC_DECODE, p->decodeClientData,&dst, &src);
1817         assert(dst<(char*) dummybuf+100); /*FIXME */
1818         (*total)++;
1819         if (src<=cur)
1820              (*current)++;
1821     }
1822     logf(LOG_DEBUG, "isamb_pp_leaf_pos: cur=%d tot=%d ofs=%d sz=%d lev=%d",
1823                     *current, *total, p->offset, p->size, pp->level);
1824     assert(src==end);
1825 }
1826
1827 static void isamb_pp_upper_pos( ISAMB_PP pp, int *current, int *total, 
1828                                 int size, int level )
1829 { /* estimates total/current occurrences from here up, excl leaf */
1830     struct ISAMB_block *p = pp->block[level];
1831     char *src=p->bytes;
1832     char *end=p->bytes+p->size;
1833     char *cur=p->bytes+p->offset;
1834     int item_size;
1835     int child;
1836     assert(level>=0);
1837     assert(!p->leaf);
1838     logf(LOG_DEBUG,"isamb_pp_upper_pos at beginning     l=%d "
1839                    "cur=%d tot=%d ofs=%d sz=%d pos=%d", 
1840                    level, *current, *total, p->offset, p->size, p->pos);
1841     assert (p->offset <= p->size);
1842         decode_ptr (&src, &child ); /* first child */
1843     while(src < end) {
1844         if (src!=cur) {
1845             *total += size;
1846             if (src < cur)
1847                 *current +=size;
1848         }
1849             decode_ptr (&src, &item_size ); 
1850         assert(src+item_size<=end);
1851         src += item_size;
1852             decode_ptr (&src, &child );
1853     }
1854     if (level>0)
1855         isamb_pp_upper_pos(pp, current, total, *total, level-1);
1856 } /* upper_pos */
1857
1858 void isamb_pp_pos( ISAMB_PP pp, int *current, int *total )
1859 { /* return an estimate of the current position and of the total number of */
1860   /* occureences in the isam tree, based on the current leaf */
1861     struct ISAMB_block *p = pp->block[pp->level];
1862     char dummy[100]; /* 100 bytes/entry must be enough */
1863     assert(total);
1864     assert(current);
1865     assert(p->leaf);
1866     isamb_pp_leaf_pos(pp,current, total, dummy);
1867     if (pp->level>0)
1868         isamb_pp_upper_pos(pp, current, total, *total, pp->level-1);
1869     /*
1870     logf(LOG_DEBUG,"isamb_pp_pos: C=%d T=%d =%6.2f%%",
1871                     *current, *total, 100.0*(*current)/(*total));
1872     */
1873 }