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