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