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