Isam-D now stores small entries directly in the dictionary.
[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.22 2002-07-12 18:12:21 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 <yaz/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         {   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     if (is->method->debug > 5)
551        logf (LOG_LOG, "isamd_pp_close %p %d=%d:%d  sz=%d n=%d=%d:%d nk=%d",
552              pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size, 
553              pp->next, isamd_type(pp->next), isamd_block(pp->next), 
554              pp->numKeys );
555     xfree (pp->buf);
556     xfree (pp);
557 }
558
559
560 ISAMD_PP isamd_pp_create (ISAMD is, int cat)
561 /* creates a pp_buff without data in it. pos=0, cat as given */
562 {
563     ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
564     int sz = is->method->filecat[is->max_cat].bsize;
565
566     pp->numKeys = 0;
567     pp->buf = (char *) xmalloc (sz);
568     memset(pp->buf,'\0',sz); /* clear the buffer, for new blocks */
569     
570     pp->next = 0;
571     pp->size = 0;
572     pp->offset = 0;
573     pp->is = is;
574     pp->diffs=0;
575     pp->diffbuf=0;
576     pp->diffinfo=0;
577     pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
578     pp->cat = cat;
579     pp->pos = 0;
580     is->no_op_new++; 
581     return pp;
582       
583 }
584
585
586 ISAMD_PP isamd_pp_open (ISAMD is, const char *dictbuf, int dictlen)
587 {
588     ISAMD_P ipos;
589     ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
590     char *src;
591     int sz = is->method->filecat[is->max_cat].bsize;
592                  /* always allocate for the largest blocks, saves trouble */
593     struct it_key singlekey;
594     char *c_ptr; /* for fake encoding the singlekey */
595     char *i_ptr;
596     int ofs;
597     int dictnum;
598     
599     pp->numKeys = 0;
600     src = pp->buf = (char *) xmalloc (sz);
601     memset(src,'\0',sz); /* clear the buffer, for new blocks */
602     
603     pp->next = 0;
604     pp->size = 0;
605     pp->offset = 0;
606     pp->is = is;
607     pp->diffs=0;
608     pp->diffbuf=0;
609     pp->diffinfo=0;
610     pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
611     
612     dictnum=*dictbuf;  // numkeys for internals, 0 for externals
613
614     if (0==dictnum)
615     {
616         memcpy(&ipos, dictbuf+1, sizeof(ISAMD_P) );
617     }
618     else /* dictionary block, fake a real one */
619     {
620        pp->cat=0; 
621        pp->pos=0;
622        if (is->method->debug > 5)
623           logf (LOG_LOG, "isamd_pp_open dict");
624        pp->numKeys=(unsigned char) dictbuf[0];
625        memcpy(pp->buf+ISAMD_BLOCK_OFFSET_1, dictbuf+1,dictlen-1);
626        pp->size=pp->offset=dictlen+ISAMD_BLOCK_OFFSET_1-1;
627        is->no_op_single++;
628        return pp;
629     } /* dict block */
630    
631     pp->cat = isamd_type(ipos);
632     pp->pos = isamd_block(ipos); 
633     
634     if (0==pp->pos)
635       is->no_op_new++; 
636       
637     if (pp->pos)
638     {
639         src = pp->buf;
640         isamd_read_block (is, pp->cat, pp->pos, src);
641         memcpy (&pp->next, src, sizeof(pp->next));
642         src += sizeof(pp->next);
643         memcpy (&pp->size, src, sizeof(pp->size));
644         src += sizeof(pp->size);
645         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
646         src += sizeof(pp->numKeys);
647         assert (pp->next != isamd_addr(pp->pos,pp->cat));
648         pp->offset = src - pp->buf; 
649         assert (pp->offset == ISAMD_BLOCK_OFFSET_1);
650         assert(pp->size>=ISAMD_BLOCK_OFFSET_1); /*??*/
651         if (pp->next)
652           is->files[pp->cat].no_op_main++;
653         else
654           is->files[pp->cat].no_op_diffonly++;
655     }
656     if (is->method->debug > 5)
657        logf (LOG_LOG, "isamd_pp_open  %p %d=%d:%d  sz=%d n=%d=%d:%d",
658              pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size, 
659              pp->next, isamd_type(pp->next), isamd_block(pp->next) );
660
661     return pp;
662 }
663
664
665
666 void isamd_buildfirstblock(ISAMD_PP pp){
667   char *dst=pp->buf;
668   assert(pp->buf);
669   assert(pp->next != isamd_addr(pp->pos,pp->cat)); 
670   memcpy(dst, &pp->next, sizeof(pp->next) );
671   dst += sizeof(pp->next);
672   memcpy(dst, &pp->size,sizeof(pp->size));
673   dst += sizeof(pp->size);
674   memcpy(dst, &pp->numKeys, sizeof(pp->numKeys));
675   dst += sizeof(pp->numKeys);
676   assert (dst - pp->buf  == ISAMD_BLOCK_OFFSET_1);
677   if (pp->is->method->debug > 5)
678      logf (LOG_LOG, "isamd: bldfirst:  p=%d=%d:%d n=%d:%d:%d sz=%d nk=%d ",
679            isamd_addr(pp->pos,pp->cat),pp->cat, pp->pos, 
680            pp->next, isamd_type(pp->next), isamd_block(pp->next),
681            pp->size, pp->numKeys);
682 }
683
684 void isamd_buildlaterblock(ISAMD_PP pp){
685   char *dst=pp->buf;
686   assert(pp->buf);
687   assert(pp->next != isamd_addr(pp->pos,pp->cat)); 
688   memcpy(dst, &pp->next, sizeof(pp->next) );
689   dst += sizeof(pp->next);
690   memcpy(dst, &pp->size,sizeof(pp->size));
691   dst += sizeof(pp->size);
692   assert (dst - pp->buf  == ISAMD_BLOCK_OFFSET_N);
693   if (pp->is->method->debug > 5)
694      logf (LOG_LOG, "isamd: l8r: sz=%d  p=%d/%d>%d/%d",
695            pp->size, 
696            pp->pos, pp->cat, 
697            isamd_block(pp->next), isamd_type(pp->next) );
698 }
699
700
701
702 /* returns non-zero if item could be read; 0 otherwise */
703 int isamd_pp_read (ISAMD_PP pp, void *buf)
704 {
705
706     return isamd_read_item (pp, (char **) &buf);
707        /* note: isamd_read_item is in merge-d.c, because it is so */
708        /* convoluted with the merge process */
709 }
710
711 /* read one main item from file - decode and store it in *dst.
712    Does not worry about diffs
713    Returns
714      0 if end-of-file
715      1 if item could be read ok
716 */
717 int isamd_read_main_item (ISAMD_PP pp, char **dst)
718 {
719     ISAMD is = pp->is;
720     char *src = pp->buf + pp->offset;
721     int newcat;
722     int oldoffs;
723
724     if (pp->offset >= pp->size)
725     {
726         if (!pp->next)
727         {
728             pp->pos = 0;
729             return 0; /* end of file */
730         }
731         if (pp->next > pp->pos)
732         {
733             if (pp->next == pp->pos + 1)
734                 is->files[pp->cat].no_next++;
735             else
736             {
737                 is->files[pp->cat].no_forward++;
738                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
739             }
740         }
741         else
742         {
743             if (pp->next + 1 == pp->pos)
744                 is->files[pp->cat].no_prev++;
745             else
746             {
747                 is->files[pp->cat].no_backward++;
748                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
749             }
750         }
751         /* out new block position */
752         newcat = isamd_type(pp->next);
753         pp->pos = isamd_block(pp->next);
754         pp->cat = isamd_type(pp->next);
755         pp->is->no_read_main++;
756         src = pp->buf;
757         /* read block and save 'next' and 'size' entry */
758         isamd_read_block (is, pp->cat, pp->pos, src);
759         memcpy (&pp->next, src, sizeof(pp->next));
760         src += sizeof(pp->next);
761         memcpy (&pp->size, src, sizeof(pp->size));
762         src += sizeof(pp->size);
763         /* assume block is non-empty */
764         pp->offset = oldoffs = src - pp->buf; 
765         assert (pp->offset == ISAMD_BLOCK_OFFSET_N);
766         assert (pp->next != isamd_addr(pp->pos,pp->cat));
767         (*is->method->code_reset)(pp->decodeClientData);
768         /* finally, read the item */
769         (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
770         pp->offset = src - pp->buf; 
771         if (is->method->debug > 8)
772             logf (LOG_LOG, "isamd: read_m: block %d:%d sz=%d ofs=%d-%d next=%d",
773                  pp->cat, pp->pos, pp->size, oldoffs, pp->offset, pp->next);
774         return 2;
775     }
776     oldoffs=pp->offset;
777     (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
778     pp->offset = src - pp->buf; 
779     if (is->method->debug > 8)
780         logf (LOG_LOG, "isamd: read_m: got %d:%d sz=%d ofs=%d-%d next=%d",
781              pp->cat, pp->pos, pp->size, oldoffs, pp->offset, pp->next);
782     return 1;
783 }
784
785 int isamd_pp_num (ISAMD_PP pp)
786 {
787     return pp->numKeys;
788 }
789
790 static char *hexdump(unsigned char *p, int len, char *buff) {
791   static char localbuff[128];
792   char bytebuff[8];
793   if (!buff) buff=localbuff;
794   *buff='\0';
795   while (len--) {
796     sprintf(bytebuff,"%02x",*p);
797     p++;
798     strcat(buff,bytebuff);
799     if (len) strcat(buff," ");
800   }
801   return buff;
802 }
803
804
805 #ifdef SKIPTHIS
806   /* needs different arguments, or something */
807 void isamd_pp_dump (ISAMD is, ISAMD_P ipos)
808 {
809   ISAMD_PP pp;
810   ISAMD_P oldaddr=0;
811   struct it_key key;
812   int i,n;
813   int occur =0;
814   int oldoffs;
815   int diffmax=1;
816   int diffidx;
817   char hexbuff[64];
818   int olddebug= is->method->debug;
819   is->method->debug=0; /* no debug logs while reading for dump */
820   
821   logf(LOG_LOG,"dumping isamd block %d (%d:%d)",
822                   (int)ipos, isamd_type(ipos), isamd_block(ipos) );
823   pp=isamd_pp_open(is,ipos);
824   logf(LOG_LOG,"numKeys=%d,  ofs=%d sz=%d",
825        pp->numKeys, pp->offset, pp->size );
826   diffidx=oldoffs= pp->offset;
827   while ((diffidx < is->method->filecat[pp->cat].bsize) && (diffmax>0))
828   {
829     memcpy(&diffmax,&(pp->buf[diffidx]),sizeof(int));
830     logf (LOG_LOG,"diff set at %d-%d: %s", diffidx, diffmax, 
831       hexdump(pp->buf+diffidx,8,0)); 
832       /*! todo: dump the actual diffs as well !!! */
833     diffidx=diffmax;
834     
835   } /* dump diffs */
836   while(isamd_pp_read(pp, &key))
837   {
838      if (oldaddr != isamd_addr(pp->pos,pp->cat) )
839      {
840         oldaddr = isamd_addr(pp->pos,pp->cat); 
841         logf(LOG_LOG,"block %d=%d:%d sz=%d nx=%d=%d:%d ofs=%d",
842                   isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, 
843                   pp->size,
844                   pp->next, isamd_type(pp->next), isamd_block(pp->next),
845                   pp->offset);
846         i=0;      
847         while (i<pp->size) {
848           n=pp->size-i;
849           if (n>8) n=8;
850           logf(LOG_LOG,"  %05x: %s",i,hexdump(pp->buf+i,n,hexbuff));
851           i+=n;
852         }
853         if (oldoffs >  ISAMD_BLOCK_OFFSET_N)
854            oldoffs=ISAMD_BLOCK_OFFSET_N;
855      } /* new block */
856      occur++;
857      logf (LOG_LOG,"    got %d:%d=%x:%x from %s at %d=%x",
858                   key.sysno, key.seqno,
859                   key.sysno, key.seqno,
860                   hexdump(pp->buf+oldoffs, pp->offset-oldoffs, hexbuff),
861                   oldoffs, oldoffs);
862      oldoffs = pp->offset;
863   }
864   /*!*/ /*TODO: dump diffs too!!! */
865   isamd_pp_close(pp);
866   is->method->debug=olddebug;
867 } /* dump */
868
869 #endif
870
871 /*
872  * $Log: isamd.c,v $
873  * Revision 1.22  2002-07-12 18:12:21  heikki
874  * Isam-D now stores small entries directly in the dictionary.
875  * Needs more tuning and cleaning...
876  *
877  * Revision 1.21  2002/07/11 16:16:00  heikki
878  * Fixed a bug in isamd, failed to store a single key when its bits
879  * did not fit into a singleton.
880  *
881  * Revision 1.20  2002/06/19 10:29:18  adam
882  * align block sizes for isam sys. Better plot for test
883  *
884  * Revision 1.19  1999/11/30 13:48:04  adam
885  * Improved installation. Updated for inclusion of YAZ header files.
886  *
887  * Revision 1.18  1999/10/06 15:18:13  heikki
888  *
889  * Improving block sizes again
890  *
891  * Revision 1.17  1999/10/06 11:46:36  heikki
892  * mproved statistics on isam-d
893  *
894  * Revision 1.16  1999/10/05 09:57:40  heikki
895  * Tuning the isam-d (and fixed a small "detail")
896  *
897  * Revision 1.15  1999/09/27 14:36:36  heikki
898  * singletons
899  *
900  * Revision 1.14  1999/09/23 18:01:18  heikki
901  * singleton optimising
902  *
903  * Revision 1.13  1999/09/20 15:48:06  heikki
904  * Small changes
905  *
906  * Revision 1.12  1999/09/13 13:28:28  heikki
907  * isam-d optimizing: merging input data in the same go
908  *
909  * Revision 1.11  1999/08/25 18:09:24  heikki
910  * Starting to optimize
911  *
912  * Revision 1.10  1999/08/24 13:17:42  heikki
913  * Block sizes, comments
914  *
915  * Revision 1.9  1999/08/20 12:25:58  heikki
916  * Statistics in isamd
917  *
918  * Revision 1.8  1999/08/18 13:28:16  heikki
919  * Set log levels to decent values
920  *
921  * Revision 1.6  1999/08/17 19:44:25  heikki
922  * Fixed memory leaks
923  *
924  * Revision 1.4  1999/08/04 14:21:18  heikki
925  * isam-d seems to be working.
926  *
927  * Revision 1.3  1999/07/21 14:24:50  heikki
928  * isamd write and read functions ok, except when diff block full.
929  * (merge not yet done)
930  *
931  * Revision 1.1  1999/07/14 12:34:43  heikki
932  * Copied from isamh, starting to change things...
933  *
934  *
935  */