Towards 1.3.15
[idzebra-moved-to-github.git] / isamc / isamd.c
1 /* $Id: isamd.c,v 1.26 2003-06-23 15:36:11 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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 <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include <yaz/log.h>
29 #include "../index/index.h"  /* isamd uses the internal structure of it_key */
30 #include "isamd-p.h"
31
32 static void flush_block (ISAMD is, int cat);
33 static void release_fc (ISAMD is, int cat);
34 static void init_fc (ISAMD is, int cat);
35
36 #define ISAMD_FREELIST_CHUNK 1
37
38 #define SMALL_TEST 0
39
40 ISAMD_M *isamd_getmethod (ISAMD_M *me)
41 {
42     static struct ISAMD_filecat_s def_cat[] = {
43 #if SMALL_TEST
44 /*        blocksz,   max. Unused time being */
45         {    32,   40 },  /* 24 is the smallest unreasonable size! */
46         {    64,    0 },
47 #else
48         {    32,    1 },
49         {   128,    1 },
50         {   256,    1 },
51         {   512,    1 },
52         {  1024,    1 },
53         {  2048,    1 },
54         {  4096,    1 },
55         {  8192,    0 },
56
57 #endif
58 #ifdef SKIPTHIS
59
60
61
62         {    32,    1 },
63         {   128,    1 },
64         {   512,    1 },
65         {  2048,    1 },
66         {  8192,    1 },
67         { 32768,    1 },
68         {131072,    0 },
69
70         {    24,    1 }, /* Experimental sizes */
71         {    32,    1 },
72         {    64,    1 },
73         {   128,    1 },
74         {   256,    1 },
75         {   512,    1 },
76         {  1024,    1 },
77         {  2048,    0 },
78 #endif 
79
80     };
81     ISAMD_M *m = (ISAMD_M *) xmalloc (sizeof(*m));  /* never released! */
82     m->filecat = def_cat;                        /* ok, only alloc'd once */
83
84     m->code_start = NULL;
85     m->code_item = NULL;
86     m->code_stop = NULL;
87     m->code_reset = NULL;
88
89     m->compare_item = NULL;
90
91     m->debug = 0; /* default to no debug */
92
93     m->max_blocks_mem = 10;
94
95     return m;
96 }
97
98
99
100 ISAMD isamd_open (BFiles bfs, const char *name, int writeflag, ISAMD_M *method)
101 {
102     ISAMD is;
103     ISAMD_filecat filecat;
104     int i = 0;
105
106     is = (ISAMD) xmalloc (sizeof(*is));
107
108     is->method = (ISAMD_M *) xmalloc (sizeof(*is->method));
109     memcpy (is->method, method, sizeof(*method));
110     filecat = is->method->filecat;
111     assert (filecat);
112
113     /* determine number of block categories */
114     if (is->method->debug>0)
115         logf (LOG_LOG, "isamd: bsize  maxkeys");
116     do
117     {
118         if (is->method->debug>0)
119             logf (LOG_LOG, "isamd:%6d %6d",
120                   filecat[i].bsize, filecat[i].mblocks);
121     } while (filecat[i++].mblocks);
122     is->no_files = i;
123     is->max_cat = --i;
124  
125     assert (is->no_files > 0);
126     assert (is->max_cat <=8 ); /* we have only 3 bits for it */
127     
128     is->files = (ISAMD_file) xmalloc (sizeof(*is->files)*is->no_files);
129
130     for (i = 0; i<is->no_files; i++)
131     {
132         char fname[512];
133
134         sprintf (fname, "%s%c", name, i+'A');
135         is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
136                                    writeflag);
137         is->files[i].head_is_dirty = 0;
138         if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
139                      &is->files[i].head))
140         {
141             is->files[i].head.lastblock = 1;
142             is->files[i].head.freelist = 0;
143         }
144         is->files[i].alloc_entries_num = 0;
145         is->files[i].alloc_entries_max =
146             is->method->filecat[i].bsize / sizeof(int) - 1;
147         is->files[i].alloc_buf = (char *)
148             xmalloc (is->method->filecat[i].bsize);
149         is->files[i].no_writes = 0; /* clear statistics */
150         is->files[i].no_reads = 0;
151         is->files[i].no_skip_writes = 0;
152         is->files[i].no_allocated = 0;
153         is->files[i].no_released = 0;
154         is->files[i].no_remap = 0;
155         is->files[i].no_forward = 0;
156         is->files[i].no_backward = 0;
157         is->files[i].sum_forward = 0;
158         is->files[i].sum_backward = 0;
159         is->files[i].no_next = 0;
160         is->files[i].no_prev = 0;
161         is->files[i].no_op_diffonly=0;
162         is->files[i].no_op_main=0;
163         init_fc (is, i);
164     }
165     is->last_pos=0;
166     is->last_cat=0;   
167     is->no_read=0;    
168     is->no_read_main=0;
169     is->no_write=0;   
170     is->no_op_single=0;
171     is->no_op_new=0;
172     is->no_read_keys=0;
173     is->no_read_eof=0;
174     is->no_seek_nxt=0;
175     is->no_seek_sam=0;
176     is->no_seek_fwd=0;
177     is->no_seek_prv=0;
178     is->no_seek_bak=0;
179     is->no_seek_cat=0;
180     is->no_fbuilds=0;
181     is->no_appds=0;
182     is->no_merges=0;
183     is->no_non=0;
184     is->no_singles=0;
185
186     return is;
187 }
188
189 int isamd_block_used (ISAMD is, int type)
190 {
191     if ( type==-1) /* singleton */
192       return 0; 
193     if (type < 0 || type >= is->no_files)
194         return -1;
195     return is->files[type].head.lastblock-1;
196 }
197
198 int isamd_block_size (ISAMD is, int type)
199 {
200     ISAMD_filecat filecat = is->method->filecat;
201     if ( type==-1) /* singleton */
202       return 0; /* no bytes used */ 
203     if (type < 0 || type >= is->no_files)
204         return -1;
205     return filecat[type].bsize;
206 }
207
208 int isamd_close (ISAMD is)
209 {
210     int i;
211     int s;
212
213     if (is->method->debug>0)
214     {
215         logf (LOG_LOG, "isamd statistics");
216         logf (LOG_LOG, "f    nxt   forw  mid-f   prev  backw  mid-b");
217         for (i = 0; i<is->no_files; i++)
218             logf (LOG_LOG, "%d%7d%7d%7.1f%7d%7d%7.1f",i,
219                   is->files[i].no_next,
220                   is->files[i].no_forward,
221                   is->files[i].no_forward ?
222                     (double) is->files[i].sum_forward/is->files[i].no_forward
223                     : 0.0,
224                   is->files[i].no_prev,
225                   is->files[i].no_backward,
226                   is->files[i].no_backward ?
227                     (double) is->files[i].sum_backward/is->files[i].no_backward
228                     : 0.0);
229     }
230     if (is->method->debug>0)
231         logf (LOG_LOG, "f  writes   reads skipped   alloc released ");
232     for (i = 0; i<is->no_files; i++)
233     {
234         release_fc (is, i);
235         assert (is->files[i].bf);
236         if (is->files[i].head_is_dirty)
237             bf_write (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
238                  &is->files[i].head);
239         if (is->method->debug>0)
240             logf (LOG_LOG, "%d%8d%8d%8d%8d%8d",i,
241                   is->files[i].no_writes,
242                   is->files[i].no_reads,
243                   is->files[i].no_skip_writes,
244                   is->files[i].no_allocated,
245                   is->files[i].no_released);
246         xfree (is->files[i].fc_list);
247         flush_block (is, i);
248         bf_close (is->files[i].bf);
249     }
250     
251     if (is->method->debug>0) 
252     {
253         logf (LOG_LOG, "f   opens    main  diffonly");
254         for (i = 0; i<is->no_files; i++)
255         {
256             logf (LOG_LOG, "%d%8d%8d%8d",i,
257                   is->files[i].no_op_main+
258                   is->files[i].no_op_diffonly,
259                   is->files[i].no_op_main,
260                   is->files[i].no_op_diffonly);
261         }
262         logf(LOG_LOG,"open single  %8d", is->no_op_single);
263         logf(LOG_LOG,"open new     %8d", is->no_op_new);
264
265         logf(LOG_LOG, "new build   %8d", is->no_fbuilds);
266         logf(LOG_LOG, "append      %8d", is->no_appds);
267         logf(LOG_LOG, "  merges    %8d", is->no_merges);
268         logf(LOG_LOG, "  singles   %8d", is->no_singles);
269         logf(LOG_LOG, "  no-ops    %8d", is->no_non);
270
271         logf(LOG_LOG, "read blocks %8d", is->no_read);
272         logf(LOG_LOG, "read keys:  %8d %8.1f k/bl", 
273                   is->no_read_keys, 
274                   1.0*(is->no_read_keys+1)/(is->no_read+1) );
275         logf(LOG_LOG, "read main-k %8d %8.1f %% of keys",
276                   is->no_read_main,
277                   100.0*(is->no_read_main+1)/(is->no_read_keys+1) );
278         logf(LOG_LOG, "read ends:  %8d %8.1f k/e",
279                   is->no_read_eof,
280                   1.0*(is->no_read_keys+1)/(is->no_read_eof+1) );
281         s= is->no_seek_nxt+ is->no_seek_sam+ is->no_seek_fwd +
282            is->no_seek_prv+ is->no_seek_bak+ is->no_seek_cat;
283         if (s==0) 
284           s++;
285         logf(LOG_LOG, "seek same   %8d %8.1f%%",
286             is->no_seek_sam, 100.0*is->no_seek_sam/s );
287         logf(LOG_LOG, "seek next   %8d %8.1f%%",
288             is->no_seek_nxt, 100.0*is->no_seek_nxt/s );
289         logf(LOG_LOG, "seek prev   %8d %8.1f%%",
290             is->no_seek_prv, 100.0*is->no_seek_prv/s );
291         logf(LOG_LOG, "seek forw   %8d %8.1f%%",
292             is->no_seek_fwd, 100.0*is->no_seek_fwd/s );
293         logf(LOG_LOG, "seek back   %8d %8.1f%%",
294             is->no_seek_bak, 100.0*is->no_seek_bak/s );
295         logf(LOG_LOG, "seek cat    %8d %8.1f%%",
296             is->no_seek_cat, 100.0*is->no_seek_cat/s );
297     }
298     xfree (is->files);
299     xfree (is->method);
300     xfree (is);
301     return 0;
302 }
303
304 static void isamd_seek_stat(ISAMD is, int cat, int pos)
305 {
306   if (cat != is->last_cat)
307      is->no_seek_cat++;
308   else if ( pos == is->last_pos)
309      is->no_seek_sam++;
310   else if ( pos == is->last_pos+1)
311      is->no_seek_nxt++;
312   else if ( pos == is->last_pos-1)
313      is->no_seek_prv++;
314   else if ( pos > is->last_pos)
315      is->no_seek_fwd++;
316   else if ( pos < is->last_pos)
317      is->no_seek_bak++;
318   is->last_cat = cat;
319   is->last_pos = pos;
320 } /* seek_stat */
321
322 int isamd_read_block (ISAMD is, int cat, int pos, char *dst)
323 {
324     isamd_seek_stat(is,cat,pos);
325     ++(is->files[cat].no_reads);
326     ++(is->no_read);
327     if (is->method->debug > 6)
328         logf (LOG_LOG, "isamd: read_block %d:%d",cat, pos);
329     return bf_read (is->files[cat].bf, pos, 0, 0, dst);
330 }
331
332 int isamd_write_block (ISAMD is, int cat, int pos, char *src)
333 {
334     isamd_seek_stat(is,cat,pos);
335     ++(is->files[cat].no_writes);
336     ++(is->no_write);
337     if (is->method->debug > 6)
338         logf (LOG_LOG, "isamd: write_block %d:%d", cat, pos);
339     return bf_write (is->files[cat].bf, pos, 0, 0, src);
340 }
341
342 int isamd_write_dblock (ISAMD is, int cat, int pos, char *src,
343                       int nextpos, int offset)
344 {
345     ISAMD_BLOCK_SIZE size = offset + ISAMD_BLOCK_OFFSET_N;
346     if (is->method->debug > 4)
347         logf (LOG_LOG, "isamd: write_dblock. size=%d nextpos=%d",
348               (int) size, nextpos);
349     src -= ISAMD_BLOCK_OFFSET_N;
350     assert( ISAMD_BLOCK_OFFSET_N == sizeof(int)+sizeof(int) );
351     memcpy (src, &nextpos, sizeof(int));
352     memcpy (src + sizeof(int), &size, sizeof(size));
353     return isamd_write_block (is, cat, pos, src);
354 }
355
356 #if ISAMD_FREELIST_CHUNK
357 static void flush_block (ISAMD is, int cat)
358 {
359     char *abuf = is->files[cat].alloc_buf;
360     int block = is->files[cat].head.freelist;
361     if (block && is->files[cat].alloc_entries_num)
362     {
363         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
364         bf_write (is->files[cat].bf, block, 0, 0, abuf);
365         is->files[cat].alloc_entries_num = 0;
366     }
367     xfree (abuf);
368 }
369
370 static int alloc_block (ISAMD is, int cat)
371 {
372     int block = is->files[cat].head.freelist;
373     char *abuf = is->files[cat].alloc_buf;
374
375     (is->files[cat].no_allocated)++;
376
377     if (!block)
378     {
379         block = (is->files[cat].head.lastblock)++;   /* no free list */
380         is->files[cat].head_is_dirty = 1;
381     }
382     else
383     {
384         if (!is->files[cat].alloc_entries_num) /* read first time */
385         {
386             bf_read (is->files[cat].bf, block, 0, 0, abuf);
387             memcpy (&is->files[cat].alloc_entries_num, abuf,
388                     sizeof(is->files[cat].alloc_entries_num));
389             assert (is->files[cat].alloc_entries_num > 0);
390         }
391         /* have some free blocks now */
392         assert (is->files[cat].alloc_entries_num > 0);
393         is->files[cat].alloc_entries_num--;
394         if (!is->files[cat].alloc_entries_num)  /* last one in block? */
395         {
396             memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
397                     sizeof(int));
398             is->files[cat].head_is_dirty = 1;
399
400             if (is->files[cat].head.freelist)
401             {
402                 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
403                          0, 0, abuf);
404                 memcpy (&is->files[cat].alloc_entries_num, abuf,
405                         sizeof(is->files[cat].alloc_entries_num));
406                 assert (is->files[cat].alloc_entries_num);
407             }
408         }
409         else
410             memcpy (&block, abuf + sizeof(int) + sizeof(int) *
411                     is->files[cat].alloc_entries_num, sizeof(int));
412     }
413     return block;
414 }
415
416 static void release_block (ISAMD is, int cat, int pos)
417 {
418     char *abuf = is->files[cat].alloc_buf;
419     int block = is->files[cat].head.freelist;
420
421     (is->files[cat].no_released)++;
422
423     if (block && !is->files[cat].alloc_entries_num) /* must read block */
424     {
425         bf_read (is->files[cat].bf, block, 0, 0, abuf);
426         memcpy (&is->files[cat].alloc_entries_num, abuf,
427                 sizeof(is->files[cat].alloc_entries_num));
428         assert (is->files[cat].alloc_entries_num > 0);
429     }
430     assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
431     if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
432     {
433         assert (block);
434         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
435         bf_write (is->files[cat].bf, block, 0, 0, abuf);
436         is->files[cat].alloc_entries_num = 0;
437     }
438     if (!is->files[cat].alloc_entries_num) /* make new buffer? */
439     {
440         memcpy (abuf + sizeof(int), &block, sizeof(int));
441         is->files[cat].head.freelist = pos;
442         is->files[cat].head_is_dirty = 1; 
443     }
444     else
445     {
446         memcpy (abuf + sizeof(int) +
447                 is->files[cat].alloc_entries_num*sizeof(int),
448                 &pos, sizeof(int));
449     }
450     is->files[cat].alloc_entries_num++;
451 }
452 #else
453 static void flush_block (ISAMD is, int cat)
454 {
455     char *abuf = is->files[cat].alloc_buf;
456     xfree (abuf);
457 }
458
459 static int alloc_block (ISAMD is, int cat)
460 {
461     int block;
462     char buf[sizeof(int)];
463
464     is->files[cat].head_is_dirty = 1;
465     (is->files[cat].no_allocated)++;
466     if ((block = is->files[cat].head.freelist))
467     {
468         bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
469         memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
470     }
471     else
472         block = (is->files[cat].head.lastblock)++;
473     return block;
474 }
475
476 static void release_block (ISAMD is, int cat, int pos)
477 {
478     char buf[sizeof(int)];
479    
480     (is->files[cat].no_released)++;
481     is->files[cat].head_is_dirty = 1; 
482     memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
483     is->files[cat].head.freelist = pos;
484     bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
485 }
486 #endif
487
488 int isamd_alloc_block (ISAMD is, int cat)
489 {
490     int block = 0;
491
492     if (is->files[cat].fc_list)
493     {
494         int j, nb;
495         for (j = 0; j < is->files[cat].fc_max; j++)
496             if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
497             {
498                 is->files[cat].fc_list[j] = 0;
499                 block = nb;
500                 break;
501             }
502     }
503     if (!block)
504         block = alloc_block (is, cat);
505     if (is->method->debug > 4)
506         logf (LOG_LOG, "isamd: alloc_block in cat %d: %d", cat, block);
507     return block;
508 }
509
510 void isamd_release_block (ISAMD is, int cat, int pos)
511 {
512     if (is->method->debug > 4)
513         logf (LOG_LOG, "isamd: release_block in cat %d: %d", cat, pos);
514     assert(pos!=0);
515     
516     if (is->files[cat].fc_list)
517     {
518         int j;
519         for (j = 0; j<is->files[cat].fc_max; j++)
520             if (!is->files[cat].fc_list[j])
521             {
522                 is->files[cat].fc_list[j] = pos;
523                 return;
524             }
525     }
526     release_block (is, cat, pos);
527 }
528
529 static void init_fc (ISAMD is, int cat)
530 {
531     int j = 100;
532         
533     is->files[cat].fc_max = j;
534     is->files[cat].fc_list = (int *)
535         xmalloc (sizeof(*is->files[0].fc_list) * j);
536     while (--j >= 0)
537         is->files[cat].fc_list[j] = 0;
538 }
539
540 static void release_fc (ISAMD is, int cat)
541 {
542     int b, j = is->files[cat].fc_max;
543
544     while (--j >= 0)
545         if ((b = is->files[cat].fc_list[j]))
546         {
547             release_block (is, cat, b);
548             is->files[cat].fc_list[j] = 0;
549         }
550 }
551
552 void isamd_pp_close (ISAMD_PP pp)
553 {
554     ISAMD is = pp->is;
555
556     (*is->method->code_stop)(ISAMD_DECODE, pp->decodeClientData);
557     isamd_free_diffs(pp);  /* see merge-d.h */
558     if (is->method->debug > 5)
559        logf (LOG_LOG, "isamd_pp_close %p %d=%d:%d  sz=%d n=%d=%d:%d nk=%d",
560              pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size, 
561              pp->next, isamd_type(pp->next), isamd_block(pp->next), 
562              pp->numKeys );
563     xfree (pp->buf);
564     xfree (pp);
565 }
566
567
568 ISAMD_PP isamd_pp_create (ISAMD is, int cat)
569 /* creates a pp_buff without data in it. pos=0, cat as given */
570 {
571     ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
572     int sz = is->method->filecat[is->max_cat].bsize;
573
574     pp->numKeys = 0;
575     pp->buf = (char *) xmalloc (sz);
576     memset(pp->buf,'\0',sz); /* clear the buffer, for new blocks */
577     
578     pp->next = 0;
579     pp->size = 0;
580     pp->offset = 0;
581     pp->is = is;
582     pp->diffs=0;
583     pp->diffbuf=0;
584     pp->diffinfo=0;
585     pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
586     pp->cat = cat;
587     pp->pos = 0;
588     is->no_op_new++; 
589     return pp;
590       
591 }
592
593
594 ISAMD_PP isamd_pp_open (ISAMD is, const char *dictbuf, int dictlen)
595 {
596     ISAMD_P ipos;
597     ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
598     char *src;
599     int sz = is->method->filecat[is->max_cat].bsize;
600                  /* always allocate for the largest blocks, saves trouble */
601     int dictnum;
602     
603     pp->numKeys = 0;
604     src = pp->buf = (char *) xmalloc (sz);
605     memset(src,'\0',sz); /* clear the buffer, for new blocks */
606     
607     pp->next = 0;
608     pp->size = 0;
609     pp->offset = 0;
610     pp->is = is;
611     pp->diffs=0;
612     pp->diffbuf=0;
613     pp->diffinfo=0;
614     pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
615     
616     dictnum=*dictbuf;  /* numkeys for internals, 0 for externals */
617
618     if (0==dictnum)
619     {
620         memcpy(&ipos, dictbuf+1, sizeof(ISAMD_P) );
621     }
622     else /* dictionary block, fake a real one */
623     {
624        pp->cat=0; 
625        pp->pos=0;
626        if (is->method->debug > 5)
627           logf (LOG_LOG, "isamd_pp_open dict");
628        pp->numKeys=(unsigned char) dictbuf[0];
629        memcpy(pp->buf+ISAMD_BLOCK_OFFSET_1, dictbuf+1,dictlen-1);
630        pp->size=pp->offset=dictlen+ISAMD_BLOCK_OFFSET_1-1;
631        is->no_op_single++;
632        return pp;
633     } /* dict block */
634    
635     pp->cat = isamd_type(ipos);
636     pp->pos = isamd_block(ipos); 
637     
638     if (0==pp->pos)
639       is->no_op_new++; 
640       
641     if (pp->pos)
642     {
643         src = pp->buf;
644         isamd_read_block (is, pp->cat, pp->pos, src);
645         memcpy (&pp->next, src, sizeof(pp->next));
646         src += sizeof(pp->next);
647         memcpy (&pp->size, src, sizeof(pp->size));
648         src += sizeof(pp->size);
649         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
650         src += sizeof(pp->numKeys);
651         assert (pp->next != isamd_addr(pp->pos,pp->cat));
652         pp->offset = src - pp->buf; 
653         assert (pp->offset == ISAMD_BLOCK_OFFSET_1);
654         assert(pp->size>=ISAMD_BLOCK_OFFSET_1); /*??*/
655         if (pp->next)
656           is->files[pp->cat].no_op_main++;
657         else
658           is->files[pp->cat].no_op_diffonly++;
659     }
660     if (is->method->debug > 5)
661        logf (LOG_LOG, "isamd_pp_open  %p %d=%d:%d  sz=%d n=%d=%d:%d",
662              pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size, 
663              pp->next, isamd_type(pp->next), isamd_block(pp->next) );
664
665     return pp;
666 }
667
668
669
670 void isamd_buildfirstblock(ISAMD_PP pp){
671   char *dst=pp->buf;
672   assert(pp->buf);
673   assert(pp->next != isamd_addr(pp->pos,pp->cat)); 
674   memcpy(dst, &pp->next, sizeof(pp->next) );
675   dst += sizeof(pp->next);
676   memcpy(dst, &pp->size,sizeof(pp->size));
677   dst += sizeof(pp->size);
678   memcpy(dst, &pp->numKeys, sizeof(pp->numKeys));
679   dst += sizeof(pp->numKeys);
680   assert (dst - pp->buf  == ISAMD_BLOCK_OFFSET_1);
681   if (pp->is->method->debug > 5)
682      logf (LOG_LOG, "isamd: bldfirst:  p=%d=%d:%d n=%d:%d:%d sz=%d nk=%d ",
683            isamd_addr(pp->pos,pp->cat),pp->cat, pp->pos, 
684            pp->next, isamd_type(pp->next), isamd_block(pp->next),
685            pp->size, pp->numKeys);
686 }
687
688 void isamd_buildlaterblock(ISAMD_PP pp){
689   char *dst=pp->buf;
690   assert(pp->buf);
691   assert(pp->next != isamd_addr(pp->pos,pp->cat)); 
692   memcpy(dst, &pp->next, sizeof(pp->next) );
693   dst += sizeof(pp->next);
694   memcpy(dst, &pp->size,sizeof(pp->size));
695   dst += sizeof(pp->size);
696   assert (dst - pp->buf  == ISAMD_BLOCK_OFFSET_N);
697   if (pp->is->method->debug > 5)
698      logf (LOG_LOG, "isamd: l8r: sz=%d  p=%d/%d>%d/%d",
699            pp->size, 
700            pp->pos, pp->cat, 
701            isamd_block(pp->next), isamd_type(pp->next) );
702 }
703
704
705
706 /* returns non-zero if item could be read; 0 otherwise */
707 int isamd_pp_read (ISAMD_PP pp, void *buf)
708 {
709
710     return isamd_read_item (pp, (char **) &buf);
711        /* note: isamd_read_item is in merge-d.c, because it is so */
712        /* convoluted with the merge process */
713 }
714
715 /* read one main item from file - decode and store it in *dst.
716    Does not worry about diffs
717    Returns
718      0 if end-of-file
719      1 if item could be read ok
720 */
721 int isamd_read_main_item (ISAMD_PP pp, char **dst)
722 {
723     ISAMD is = pp->is;
724     char *src = pp->buf + pp->offset;
725     int newcat;
726     int oldoffs;
727
728     if (pp->offset >= pp->size)
729     {
730         if (!pp->next)
731         {
732             pp->pos = 0;
733             return 0; /* end of file */
734         }
735         if (pp->next > pp->pos)
736         {
737             if (pp->next == pp->pos + 1)
738                 is->files[pp->cat].no_next++;
739             else
740             {
741                 is->files[pp->cat].no_forward++;
742                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
743             }
744         }
745         else
746         {
747             if (pp->next + 1 == pp->pos)
748                 is->files[pp->cat].no_prev++;
749             else
750             {
751                 is->files[pp->cat].no_backward++;
752                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
753             }
754         }
755         /* out new block position */
756         newcat = isamd_type(pp->next);
757         pp->pos = isamd_block(pp->next);
758         pp->cat = isamd_type(pp->next);
759         pp->is->no_read_main++;
760         src = pp->buf;
761         /* read block and save 'next' and 'size' entry */
762         isamd_read_block (is, pp->cat, pp->pos, src);
763         memcpy (&pp->next, src, sizeof(pp->next));
764         src += sizeof(pp->next);
765         memcpy (&pp->size, src, sizeof(pp->size));
766         src += sizeof(pp->size);
767         /* assume block is non-empty */
768         pp->offset = oldoffs = src - pp->buf; 
769         assert (pp->offset == ISAMD_BLOCK_OFFSET_N);
770         assert (pp->next != isamd_addr(pp->pos,pp->cat));
771         (*is->method->code_reset)(pp->decodeClientData);
772         /* finally, read the item */
773         (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
774         pp->offset = src - pp->buf; 
775         if (is->method->debug > 8)
776             logf (LOG_LOG, "isamd: read_m: block %d:%d sz=%d ofs=%d-%d next=%d",
777                  pp->cat, pp->pos, pp->size, oldoffs, pp->offset, pp->next);
778         return 2;
779     }
780     oldoffs=pp->offset;
781     (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
782     pp->offset = src - pp->buf; 
783     if (is->method->debug > 8)
784         logf (LOG_LOG, "isamd: read_m: got %d:%d sz=%d ofs=%d-%d next=%d",
785              pp->cat, pp->pos, pp->size, oldoffs, pp->offset, pp->next);
786     return 1;
787 }
788
789 int isamd_pp_num (ISAMD_PP pp)
790 {
791     return pp->numKeys;
792 }
793
794 #if 0
795 /* for testing .. */
796 static char *hexdump(unsigned char *p, int len, char *buff) {
797   static char localbuff[128];
798   char bytebuff[8];
799   if (!buff) buff=localbuff;
800   *buff='\0';
801   while (len--) {
802     sprintf(bytebuff,"%02x",*p);
803     p++;
804     strcat(buff,bytebuff);
805     if (len) strcat(buff," ");
806   }
807   return buff;
808 }
809 #endif
810
811 #ifdef SKIPTHIS
812   /* needs different arguments, or something */
813 void isamd_pp_dump (ISAMD is, ISAMD_P ipos)
814 {
815   ISAMD_PP pp;
816   ISAMD_P oldaddr=0;
817   struct it_key key;
818   int i,n;
819   int occur =0;
820   int oldoffs;
821   int diffmax=1;
822   int diffidx;
823   char hexbuff[64];
824   int olddebug= is->method->debug;
825   is->method->debug=0; /* no debug logs while reading for dump */
826   
827   logf(LOG_LOG,"dumping isamd block %d (%d:%d)",
828                   (int)ipos, isamd_type(ipos), isamd_block(ipos) );
829   pp=isamd_pp_open(is,ipos);
830   logf(LOG_LOG,"numKeys=%d,  ofs=%d sz=%d",
831        pp->numKeys, pp->offset, pp->size );
832   diffidx=oldoffs= pp->offset;
833   while ((diffidx < is->method->filecat[pp->cat].bsize) && (diffmax>0))
834   {
835     memcpy(&diffmax,&(pp->buf[diffidx]),sizeof(int));
836     logf (LOG_LOG,"diff set at %d-%d: %s", diffidx, diffmax, 
837       hexdump(pp->buf+diffidx,8,0)); 
838       /*! todo: dump the actual diffs as well !!! */
839     diffidx=diffmax;
840     
841   } /* dump diffs */
842   while(isamd_pp_read(pp, &key))
843   {
844      if (oldaddr != isamd_addr(pp->pos,pp->cat) )
845      {
846         oldaddr = isamd_addr(pp->pos,pp->cat); 
847         logf(LOG_LOG,"block %d=%d:%d sz=%d nx=%d=%d:%d ofs=%d",
848                   isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, 
849                   pp->size,
850                   pp->next, isamd_type(pp->next), isamd_block(pp->next),
851                   pp->offset);
852         i=0;      
853         while (i<pp->size) {
854           n=pp->size-i;
855           if (n>8) n=8;
856           logf(LOG_LOG,"  %05x: %s",i,hexdump(pp->buf+i,n,hexbuff));
857           i+=n;
858         }
859         if (oldoffs >  ISAMD_BLOCK_OFFSET_N)
860            oldoffs=ISAMD_BLOCK_OFFSET_N;
861      } /* new block */
862      occur++;
863      logf (LOG_LOG,"    got %d:%d=%x:%x from %s at %d=%x",
864                   key.sysno, key.seqno,
865                   key.sysno, key.seqno,
866                   hexdump(pp->buf+oldoffs, pp->offset-oldoffs, hexbuff),
867                   oldoffs, oldoffs);
868      oldoffs = pp->offset;
869   }
870   /*!*/ /*TODO: dump diffs too!!! */
871   isamd_pp_close(pp);
872   is->method->debug=olddebug;
873 } /* dump */
874
875 #endif
876