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