isam-d optimizing: merging input data in the same go
[idzebra-moved-to-github.git] / isamc / isamd.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * $Id: isamd.c,v 1.12 1999-09-13 13:28:28 heikki Exp $ 
5  *
6  * Isamd - isam with diffs 
7  * Programmed by: Heikki Levanto
8  *
9  * Todo
10  *  - Statistics are missing and/or completely wrong
11  *  - Lots of code stolen from isamc, not all needed any more
12  */
13
14
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <stdio.h>
19
20 #include <log.h>
21 #include "../index/index.h"  /* isamd uses the internal structure of it_key */
22 #include "isamd-p.h"
23
24 static void flush_block (ISAMD is, int cat);
25 static void release_fc (ISAMD is, int cat);
26 static void init_fc (ISAMD is, int cat);
27
28 #define ISAMD_FREELIST_CHUNK 1
29
30 #define SMALL_TEST 0
31
32 ISAMD_M isamd_getmethod (ISAMD_M me)
33 {
34     static struct ISAMD_filecat_s def_cat[] = {
35 #if SMALL_TEST
36 /*        blocksz,   max. Unused time being */
37         {    32,   40 },  /* 24 is the smallest unreasonable size! */
38         {    64,    0 },
39 #else
40         {    32,    1 },
41         {   128,    1 },
42         {   512,    1 },
43         {  2048,    1 },
44         {  8192,    1 },
45         { 32768,    1 },
46         {131072,    0 },
47 #endif 
48
49 /* old values from isamc, long time ago...
50         {    24,   40 },
51         {   128,  256 },
52         {   512, 1024 },
53         {  2048, 4096 },
54         {  8192,16384 },
55         { 32768,   0  },
56 */
57
58     };
59     ISAMD_M m = (ISAMD_M) xmalloc (sizeof(*m));  /* never released! */
60     m->filecat = def_cat;                        /* ok, only alloc'd once */
61
62     m->code_start = NULL;
63     m->code_item = NULL;
64     m->code_stop = NULL;
65     m->code_reset = NULL;
66
67     m->compare_item = NULL;
68
69     m->debug = 0; /* default to no debug */
70
71     m->max_blocks_mem = 10;
72
73     return m;
74 }
75
76
77
78 ISAMD isamd_open (BFiles bfs, const char *name, int writeflag, ISAMD_M method)
79 {
80     ISAMD is;
81     ISAMD_filecat filecat;
82     int i = 0;
83
84     is = (ISAMD) xmalloc (sizeof(*is));
85
86     is->method = (ISAMD_M) xmalloc (sizeof(*is->method));
87     memcpy (is->method, method, sizeof(*method));
88     filecat = is->method->filecat;
89     assert (filecat);
90
91     /* determine number of block categories */
92     if (is->method->debug>0)
93         logf (LOG_LOG, "isamd: bsize  maxkeys");
94     do
95     {
96         if (is->method->debug>0)
97             logf (LOG_LOG, "isamd:%6d %6d",
98                   filecat[i].bsize, filecat[i].mblocks);
99     } while (filecat[i++].mblocks);
100     is->no_files = i;
101     is->max_cat = --i;
102  
103     assert (is->no_files > 0);
104     assert (is->max_cat <=8 ); /* we have only 3 bits for it */
105     
106     is->files = (ISAMD_file) xmalloc (sizeof(*is->files)*is->no_files);
107
108     for (i = 0; i<is->no_files; i++)
109     {
110         char fname[512];
111
112         sprintf (fname, "%s%c", name, i+'A');
113         is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
114                                    writeflag);
115         is->files[i].head_is_dirty = 0;
116         if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
117                      &is->files[i].head))
118         {
119             is->files[i].head.lastblock = 1;
120             is->files[i].head.freelist = 0;
121         }
122         is->files[i].alloc_entries_num = 0;
123         is->files[i].alloc_entries_max =
124             is->method->filecat[i].bsize / sizeof(int) - 1;
125         is->files[i].alloc_buf = (char *)
126             xmalloc (is->method->filecat[i].bsize);
127         is->files[i].no_writes = 0; /* clear statistics */
128         is->files[i].no_reads = 0;
129         is->files[i].no_skip_writes = 0;
130         is->files[i].no_allocated = 0;
131         is->files[i].no_released = 0;
132         is->files[i].no_remap = 0;
133         is->files[i].no_forward = 0;
134         is->files[i].no_backward = 0;
135         is->files[i].sum_forward = 0;
136         is->files[i].sum_backward = 0;
137         is->files[i].no_next = 0;
138         is->files[i].no_prev = 0;
139         is->files[i].no_op_nodiff=0;
140         is->files[i].no_op_intdiff=0;
141         is->files[i].no_op_extdiff=0;
142         is->files[i].no_fbuilds=0;   
143         is->files[i].no_appds=0;     
144         is->files[i].no_merges=0;    
145         is->files[i].no_remerges=0;  
146
147         init_fc (is, i);
148     }
149     return is;
150 }
151
152 int isamd_block_used (ISAMD is, int type)
153 {
154     if (type < 0 || type >= is->no_files)
155         return -1;
156     return is->files[type].head.lastblock-1;
157 }
158
159 int isamd_block_size (ISAMD is, int type)
160 {
161     ISAMD_filecat filecat = is->method->filecat;
162     if (type < 0 || type >= is->no_files)
163         return -1;
164     return filecat[type].bsize;
165 }
166
167 int isamd_close (ISAMD is)
168 {
169     int i;
170
171     if (is->method->debug>0)
172     {
173         logf (LOG_LOG, "isamd statistics");
174         logf (LOG_LOG, "f      nxt    forw   mid-f    prev   backw   mid-b");
175         for (i = 0; i<is->no_files; i++)
176             logf (LOG_LOG, "%d%8d%8d%8.1f%8d%8d%8.1f",i,
177                   is->files[i].no_next,
178                   is->files[i].no_forward,
179                   is->files[i].no_forward ?
180                   (double) is->files[i].sum_forward/is->files[i].no_forward
181                   : 0.0,
182                   is->files[i].no_prev,
183                   is->files[i].no_backward,
184                   is->files[i].no_backward ?
185                   (double) is->files[i].sum_backward/is->files[i].no_backward
186                   : 0.0);
187     }
188     if (is->method->debug>0)
189         logf (LOG_LOG, "f  writes   reads skipped   alloc released ");
190     for (i = 0; i<is->no_files; i++)
191     {
192         release_fc (is, i);
193         assert (is->files[i].bf);
194         if (is->files[i].head_is_dirty)
195             bf_write (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
196                  &is->files[i].head);
197         if (is->method->debug>0)
198             logf (LOG_LOG, "%d%8d%8d%8d%8d%8d",i,
199                   is->files[i].no_writes,
200                   is->files[i].no_reads,
201                   is->files[i].no_skip_writes,
202                   is->files[i].no_allocated,
203                   is->files[i].no_released);
204         xfree (is->files[i].fc_list);
205         flush_block (is, i);
206         bf_close (is->files[i].bf);
207     }
208     
209     if (is->method->debug>0) 
210     {
211         logf (LOG_LOG, "f   opens  simple     int     ext");
212         for (i = 0; i<is->no_files; i++)
213         {
214             logf (LOG_LOG, "%d%8d%8d%8d%8d",i,
215                   is->files[i].no_op_nodiff+
216                   is->files[i].no_op_intdiff+
217                   is->files[i].no_op_extdiff,
218                   is->files[i].no_op_nodiff,
219                   is->files[i].no_op_intdiff,
220                   is->files[i].no_op_extdiff);
221         }
222         logf (LOG_LOG, "    build  append   merge   remrg");
223         logf (LOG_LOG, "=%8d%8d%8d%8d",
224                   is->files[0].no_fbuilds,
225                   is->files[0].no_appds,
226                   is->files[0].no_merges,
227                   is->files[0].no_remerges);
228     }
229     xfree (is->files);
230     xfree (is->method);
231     xfree (is);
232     return 0;
233 }
234
235 int isamd_read_block (ISAMD is, int cat, int pos, char *dst)
236 {
237     ++(is->files[cat].no_reads);
238     if (is->method->debug > 6)
239         logf (LOG_LOG, "isamd: read_block %d:%d",cat, pos);
240     return bf_read (is->files[cat].bf, pos, 0, 0, dst);
241 }
242
243 int isamd_write_block (ISAMD is, int cat, int pos, char *src)
244 {
245     ++(is->files[cat].no_writes);
246     if (is->method->debug > 6)
247         logf (LOG_LOG, "isamd: write_block %d:%d", cat, pos);
248     return bf_write (is->files[cat].bf, pos, 0, 0, src);
249 }
250
251 int isamd_write_dblock (ISAMD is, int cat, int pos, char *src,
252                       int nextpos, int offset)
253 {
254     ISAMD_BLOCK_SIZE size = offset + ISAMD_BLOCK_OFFSET_N;
255     if (is->method->debug > 4)
256         logf (LOG_LOG, "isamd: write_dblock. size=%d nextpos=%d",
257               (int) size, nextpos);
258     src -= ISAMD_BLOCK_OFFSET_N;
259     assert( ISAMD_BLOCK_OFFSET_N == sizeof(int)+sizeof(int) );
260     memcpy (src, &nextpos, sizeof(int));
261     memcpy (src + sizeof(int), &size, sizeof(size));
262     return isamd_write_block (is, cat, pos, src);
263 }
264
265 #if ISAMD_FREELIST_CHUNK
266 static void flush_block (ISAMD is, int cat)
267 {
268     char *abuf = is->files[cat].alloc_buf;
269     int block = is->files[cat].head.freelist;
270     if (block && is->files[cat].alloc_entries_num)
271     {
272         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
273         bf_write (is->files[cat].bf, block, 0, 0, abuf);
274         is->files[cat].alloc_entries_num = 0;
275     }
276     xfree (abuf);
277 }
278
279 static int alloc_block (ISAMD is, int cat)
280 {
281     int block = is->files[cat].head.freelist;
282     char *abuf = is->files[cat].alloc_buf;
283
284     (is->files[cat].no_allocated)++;
285
286     if (!block)
287     {
288         block = (is->files[cat].head.lastblock)++;   /* no free list */
289         is->files[cat].head_is_dirty = 1;
290     }
291     else
292     {
293         if (!is->files[cat].alloc_entries_num) /* read first time */
294         {
295             bf_read (is->files[cat].bf, block, 0, 0, abuf);
296             memcpy (&is->files[cat].alloc_entries_num, abuf,
297                     sizeof(is->files[cat].alloc_entries_num));
298             assert (is->files[cat].alloc_entries_num > 0);
299         }
300         /* have some free blocks now */
301         assert (is->files[cat].alloc_entries_num > 0);
302         is->files[cat].alloc_entries_num--;
303         if (!is->files[cat].alloc_entries_num)  /* last one in block? */
304         {
305             memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
306                     sizeof(int));
307             is->files[cat].head_is_dirty = 1;
308
309             if (is->files[cat].head.freelist)
310             {
311                 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
312                          0, 0, abuf);
313                 memcpy (&is->files[cat].alloc_entries_num, abuf,
314                         sizeof(is->files[cat].alloc_entries_num));
315                 assert (is->files[cat].alloc_entries_num);
316             }
317         }
318         else
319             memcpy (&block, abuf + sizeof(int) + sizeof(int) *
320                     is->files[cat].alloc_entries_num, sizeof(int));
321     }
322     return block;
323 }
324
325 static void release_block (ISAMD is, int cat, int pos)
326 {
327     char *abuf = is->files[cat].alloc_buf;
328     int block = is->files[cat].head.freelist;
329
330     (is->files[cat].no_released)++;
331
332     if (block && !is->files[cat].alloc_entries_num) /* must read block */
333     {
334         bf_read (is->files[cat].bf, block, 0, 0, abuf);
335         memcpy (&is->files[cat].alloc_entries_num, abuf,
336                 sizeof(is->files[cat].alloc_entries_num));
337         assert (is->files[cat].alloc_entries_num > 0);
338     }
339     assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
340     if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
341     {
342         assert (block);
343         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
344         bf_write (is->files[cat].bf, block, 0, 0, abuf);
345         is->files[cat].alloc_entries_num = 0;
346     }
347     if (!is->files[cat].alloc_entries_num) /* make new buffer? */
348     {
349         memcpy (abuf + sizeof(int), &block, sizeof(int));
350         is->files[cat].head.freelist = pos;
351         is->files[cat].head_is_dirty = 1; 
352     }
353     else
354     {
355         memcpy (abuf + sizeof(int) +
356                 is->files[cat].alloc_entries_num*sizeof(int),
357                 &pos, sizeof(int));
358     }
359     is->files[cat].alloc_entries_num++;
360 }
361 #else
362 static void flush_block (ISAMD is, int cat)
363 {
364     char *abuf = is->files[cat].alloc_buf;
365     xfree (abuf);
366 }
367
368 static int alloc_block (ISAMD is, int cat)
369 {
370     int block;
371     char buf[sizeof(int)];
372
373     is->files[cat].head_is_dirty = 1;
374     (is->files[cat].no_allocated)++;
375     if ((block = is->files[cat].head.freelist))
376     {
377         bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
378         memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
379     }
380     else
381         block = (is->files[cat].head.lastblock)++;
382     return block;
383 }
384
385 static void release_block (ISAMD is, int cat, int pos)
386 {
387     char buf[sizeof(int)];
388    
389     (is->files[cat].no_released)++;
390     is->files[cat].head_is_dirty = 1; 
391     memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
392     is->files[cat].head.freelist = pos;
393     bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
394 }
395 #endif
396
397 int isamd_alloc_block (ISAMD is, int cat)
398 {
399     int block = 0;
400
401     if (is->files[cat].fc_list)
402     {
403         int j, nb;
404         for (j = 0; j < is->files[cat].fc_max; j++)
405             if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
406             {
407                 is->files[cat].fc_list[j] = 0;
408                 block = nb;
409                 break;
410             }
411     }
412     if (!block)
413         block = alloc_block (is, cat);
414     if (is->method->debug > 4)
415         logf (LOG_LOG, "isamd: alloc_block in cat %d: %d", cat, block);
416     return block;
417 }
418
419 void isamd_release_block (ISAMD is, int cat, int pos)
420 {
421     if (is->method->debug > 4)
422         logf (LOG_LOG, "isamd: release_block in cat %d: %d", cat, pos);
423     assert(pos!=0);
424     
425     if (is->files[cat].fc_list)
426     {
427         int j;
428         for (j = 0; j<is->files[cat].fc_max; j++)
429             if (!is->files[cat].fc_list[j])
430             {
431                 is->files[cat].fc_list[j] = pos;
432                 return;
433             }
434     }
435     release_block (is, cat, pos);
436 }
437
438 static void init_fc (ISAMD is, int cat)
439 {
440     int j = 100;
441         
442     is->files[cat].fc_max = j;
443     is->files[cat].fc_list = (int *)
444         xmalloc (sizeof(*is->files[0].fc_list) * j);
445     while (--j >= 0)
446         is->files[cat].fc_list[j] = 0;
447 }
448
449 static void release_fc (ISAMD is, int cat)
450 {
451     int b, j = is->files[cat].fc_max;
452
453     while (--j >= 0)
454         if ((b = is->files[cat].fc_list[j]))
455         {
456             release_block (is, cat, b);
457             is->files[cat].fc_list[j] = 0;
458         }
459 }
460
461 void isamd_pp_close (ISAMD_PP pp)
462 {
463     ISAMD is = pp->is;
464
465     (*is->method->code_stop)(ISAMD_DECODE, pp->decodeClientData);
466     isamd_free_diffs(pp);  /* see merge-d.h */
467     xfree (pp->buf);
468     xfree (pp);
469     if (is->method->debug > 5)
470        logf (LOG_LOG, "isamd_pp_close %p %d=%d:%d  sz=%d n=%d=%d:%d",
471              pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size, 
472              pp->next, isamd_type(pp->next), isamd_block(pp->next) );
473 }
474
475
476
477 ISAMD_PP isamd_pp_open (ISAMD is, ISAMD_P ipos)
478 {
479     ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
480     char *src;
481     int sz = is->method->filecat[is->max_cat].bsize;
482                  /* always allocate for the largest blocks, saves trouble */
483    
484     pp->cat = isamd_type(ipos);
485     pp->pos = isamd_block(ipos); 
486
487     src = pp->buf = (char *) xmalloc (sz);
488     memset(src,'\0',sz); /* clear the buffer, for new blocks */
489     
490     pp->next = 0;
491     pp->size = 0;
492     pp->offset = 0;
493     pp->is = is;
494     pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
495     pp->numKeys = 0;
496     pp->diffs=0;
497     pp->diffbuf=0;
498     pp->diffinfo=0;
499     
500     if (pp->pos)
501     {
502         src = pp->buf;
503         isamd_read_block (is, pp->cat, pp->pos, src);
504         memcpy (&pp->next, src, sizeof(pp->next));
505         src += sizeof(pp->next);
506         memcpy (&pp->size, src, sizeof(pp->size));
507         src += sizeof(pp->size);
508         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
509         src += sizeof(pp->numKeys);
510         memcpy (&pp->diffs, src, sizeof(pp->diffs));
511         src += sizeof(pp->diffs);
512         assert (pp->next != pp->pos);
513         pp->offset = src - pp->buf; 
514         assert (pp->offset == ISAMD_BLOCK_OFFSET_1);
515         if (0==pp->diffs)
516            ++(is->files[pp->cat].no_op_nodiff);
517         else
518            if(pp->diffs&1)
519                ++(is->files[pp->cat].no_op_extdiff);
520            else
521                ++(is->files[pp->cat].no_op_intdiff);
522       //  if (!pp->diffbuf)
523       //    pp->diffbuf=pp->buf;
524     }
525     if (is->method->debug > 5)
526        logf (LOG_LOG, "isamd_pp_open  %p %d=%d:%d  sz=%d n=%d=%d:%d",
527              pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size, 
528              pp->next, isamd_type(pp->next), isamd_block(pp->next) );
529     
530       
531     return pp;
532 }
533
534
535
536 void isamd_buildfirstblock(ISAMD_PP pp){
537   char *dst=pp->buf;
538   assert(pp->buf);
539   assert(pp->next != pp->pos); 
540   memcpy(dst, &pp->next, sizeof(pp->next) );
541   dst += sizeof(pp->next);
542   memcpy(dst, &pp->size,sizeof(pp->size));
543   dst += sizeof(pp->size);
544   memcpy(dst, &pp->numKeys, sizeof(pp->numKeys));
545   dst += sizeof(pp->numKeys);
546   memcpy(dst, &pp->diffs, sizeof(pp->diffs));
547   dst += sizeof(pp->diffs);  
548   assert (dst - pp->buf  == ISAMD_BLOCK_OFFSET_1);
549   if (pp->is->method->debug > 5)
550      logf (LOG_LOG, "isamd: first: sz=%d  p=%d/%d>%d/%d nk=%d d=%d",
551            pp->size, 
552            pp->cat, pp->pos, 
553            isamd_type(pp->next), isamd_block(pp->next),
554            pp->numKeys, pp->diffs);
555 }
556
557 void isamd_buildlaterblock(ISAMD_PP pp){
558   char *dst=pp->buf;
559   assert(pp->buf);
560   assert(pp->next != isamd_addr(pp->pos,pp->cat)); 
561   memcpy(dst, &pp->next, sizeof(pp->next) );
562   dst += sizeof(pp->next);
563   memcpy(dst, &pp->size,sizeof(pp->size));
564   dst += sizeof(pp->size);
565   assert (dst - pp->buf  == ISAMD_BLOCK_OFFSET_N);
566   if (pp->is->method->debug > 5)
567      logf (LOG_LOG, "isamd: l8r: sz=%d  p=%d/%d>%d/%d",
568            pp->size, 
569            pp->pos, pp->cat, 
570            isamd_block(pp->next), isamd_type(pp->next) );
571 }
572
573
574
575 /* returns non-zero if item could be read; 0 otherwise */
576 int isamd_pp_read (ISAMD_PP pp, void *buf)
577 {
578     return isamd_read_item (pp, (char **) &buf);
579     /* note: isamd_read_item is in merge-d.c, because it is so */
580     /* convoluted with the merge process */
581 }
582
583 /* read one main item from file - decode and store it in *dst.
584    Does not worry about diffs
585    Returns
586      0 if end-of-file
587      1 if item could be read ok
588 */
589 int isamd_read_main_item (ISAMD_PP pp, char **dst)
590 {
591     ISAMD is = pp->is;
592     char *src = pp->buf + pp->offset;
593     int newcat;
594     int oldoffs;
595
596     if (pp->offset >= pp->size)
597     {
598         if (!pp->next)
599         {
600             pp->pos = 0;
601             return 0; /* end of file */
602         }
603         if (pp->next > pp->pos)
604         {
605             if (pp->next == pp->pos + 1)
606                 is->files[pp->cat].no_next++;
607             else
608             {
609                 is->files[pp->cat].no_forward++;
610                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
611             }
612         }
613         else
614         {
615             if (pp->next + 1 == pp->pos)
616                 is->files[pp->cat].no_prev++;
617             else
618             {
619                 is->files[pp->cat].no_backward++;
620                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
621             }
622         }
623         /* out new block position */
624         newcat = isamd_type(pp->next);
625         pp->pos = isamd_block(pp->next);
626         pp->cat = isamd_type(pp->next);
627         
628         src = pp->buf;
629         /* read block and save 'next' and 'size' entry */
630         isamd_read_block (is, pp->cat, pp->pos, src);
631         memcpy (&pp->next, src, sizeof(pp->next));
632         src += sizeof(pp->next);
633         memcpy (&pp->size, src, sizeof(pp->size));
634         src += sizeof(pp->size);
635         /* assume block is non-empty */
636         pp->offset = oldoffs = src - pp->buf; 
637         assert (pp->offset == ISAMD_BLOCK_OFFSET_N);
638         assert (pp->next != isamd_addr(pp->pos,pp->cat));
639         (*is->method->code_reset)(pp->decodeClientData);
640         /* finally, read the item */
641         (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
642         pp->offset = src - pp->buf; 
643         if (is->method->debug > 8)
644             logf (LOG_LOG, "isamd: read_m: block %d:%d sz=%d ofs=%d-%d next=%d",
645                  pp->cat, pp->pos, pp->size, oldoffs, pp->offset, pp->next);
646         return 2;
647     }
648     oldoffs=pp->offset;
649     (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
650     pp->offset = src - pp->buf; 
651     if (is->method->debug > 8)
652         logf (LOG_LOG, "isamd: read_m: got %d:%d sz=%d ofs=%d-%d next=%d",
653              pp->cat, pp->pos, pp->size, oldoffs, pp->offset, pp->next);
654     return 1;
655 }
656
657 int isamd_pp_num (ISAMD_PP pp)
658 {
659     return pp->numKeys;
660 }
661
662 static char *hexdump(unsigned char *p, int len, char *buff) {
663   static char localbuff[128];
664   char bytebuff[8];
665   if (!buff) buff=localbuff;
666   *buff='\0';
667   while (len--) {
668     sprintf(bytebuff,"%02x",*p);
669     p++;
670     strcat(buff,bytebuff);
671     if (len) strcat(buff," ");
672   }
673   return buff;
674 }
675
676
677 void isamd_pp_dump (ISAMD is, ISAMD_P ipos)
678 {
679   ISAMD_PP pp;
680   ISAMD_P oldaddr=0;
681   struct it_key key;
682   int i,n;
683   int occur =0;
684   int oldoffs;
685   int diffmax=1;
686   int diffidx;
687   char hexbuff[64];
688   
689   logf(LOG_LOG,"dumping isamd block %d (%d:%d)",
690                   (int)ipos, isamd_type(ipos), isamd_block(ipos) );
691   pp=isamd_pp_open(is,ipos);
692   logf(LOG_LOG,"numKeys=%d,  ofs=%d sz=%d d=%d",
693        pp->numKeys, pp->offset, pp->size, pp->diffs);
694   diffidx=oldoffs= pp->offset;
695   while ((diffidx < is->method->filecat[pp->cat].bsize) && (diffmax>0))
696   {
697     memcpy(&diffmax,&(pp->buf[diffidx]),sizeof(int));
698     logf (LOG_LOG,"diff set at %d-%d: %s", diffidx, diffmax, 
699       hexdump(pp->buf+diffidx,8,0)); 
700       /*! todo: dump the actual diffs as well !!! */
701     diffidx=diffmax;
702     
703   } /* dump diffs */
704   while(isamd_pp_read(pp, &key))
705   {
706      if (oldaddr != isamd_addr(pp->pos,pp->cat) )
707      {
708         oldaddr = isamd_addr(pp->pos,pp->cat); 
709         logf(LOG_LOG,"block %d=%d:%d sz=%d nx=%d=%d:%d ofs=%d",
710                   isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, 
711                   pp->size,
712                   pp->next, isamd_type(pp->next), isamd_block(pp->next),
713                   pp->offset);
714         i=0;      
715         while (i<pp->size) {
716           n=pp->size-i;
717           if (n>8) n=8;
718           logf(LOG_LOG,"  %05x: %s",i,hexdump(pp->buf+i,n,hexbuff));
719           i+=n;
720         }
721         if (oldoffs >  ISAMD_BLOCK_OFFSET_N)
722            oldoffs=ISAMD_BLOCK_OFFSET_N;
723      } /* new block */
724      occur++;
725      logf (LOG_LOG,"    got %d:%d=%x:%x from %s at %d=%x",
726                   key.sysno, key.seqno,
727                   key.sysno, key.seqno,
728                   hexdump(pp->buf+oldoffs, pp->offset-oldoffs, hexbuff),
729                   oldoffs, oldoffs);
730      oldoffs = pp->offset;
731   }
732   /*!*/ /*TODO: dump diffs too!!! */
733   isamd_pp_close(pp);
734 } /* dump */
735
736 /*
737  * $Log: isamd.c,v $
738  * Revision 1.12  1999-09-13 13:28:28  heikki
739  * isam-d optimizing: merging input data in the same go
740  *
741  * Revision 1.11  1999/08/25 18:09:24  heikki
742  * Starting to optimize
743  *
744  * Revision 1.10  1999/08/24 13:17:42  heikki
745  * Block sizes, comments
746  *
747  * Revision 1.9  1999/08/20 12:25:58  heikki
748  * Statistics in isamd
749  *
750  * Revision 1.8  1999/08/18 13:28:16  heikki
751  * Set log levels to decent values
752  *
753  * Revision 1.6  1999/08/17 19:44:25  heikki
754  * Fixed memory leaks
755  *
756  * Revision 1.4  1999/08/04 14:21:18  heikki
757  * isam-d seems to be working.
758  *
759  * Revision 1.3  1999/07/21 14:24:50  heikki
760  * isamd write and read functions ok, except when diff block full.
761  * (merge not yet done)
762  *
763  * Revision 1.1  1999/07/14 12:34:43  heikki
764  * Copied from isamh, starting to change things...
765  *
766  *
767  */