534037f90ce4247c64b717cb0ac920ee957626b7
[idzebra-moved-to-github.git] / isamc / isamc.c
1 /* $Id: isamc.c,v 1.27 2004-11-19 10:27:09 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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  * TODO:
25  *   Reduction to lower categories in isc_merge
26  */
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include <yaz/ylog.h>
33 #include "isamc-p.h"
34
35 static void flush_block (ISAMC is, int cat);
36 static void release_fc (ISAMC is, int cat);
37 static void init_fc (ISAMC is, int cat);
38
39 #define ISAMC_FREELIST_CHUNK 0
40
41 #define SMALL_TEST 0
42
43 void isc_getmethod (ISAMC_M *m)
44 {
45
46     static struct ISAMC_filecat_s def_cat[] = {
47 #if SMALL_TEST
48         {    32,     28,      0,  3 },
49         {    64,     54,     30,  0 },
50 #else
51         {    64,     56,     40,  5 },
52         {   128,    120,    100,  10 },
53         {   512,    490,    350,  10 },
54         {  2048,   1900,   1700,  10 },
55         {  8192,   8000,   7900,  10 },
56         { 32768,  32000,  31000,  0 },
57 #endif
58     };
59     m->filecat = def_cat;
60
61     m->codec.start = NULL;
62     m->codec.decode  = NULL;
63     m->codec.encode = NULL;
64     m->codec.stop = NULL;
65     m->codec.reset = NULL;
66
67     m->compare_item = NULL;
68     m->log_item = NULL;
69
70     m->debug = 1;
71
72     m->max_blocks_mem = 10;
73 }
74
75 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
76 {
77     ISAMC is;
78     ISAMC_filecat filecat;
79     int i = 0;
80     int max_buf_size = 0;
81
82     is = (ISAMC) xmalloc (sizeof(*is));
83
84     is->method = (ISAMC_M *) xmalloc (sizeof(*is->method));
85     memcpy (is->method, method, sizeof(*method));
86     filecat = is->method->filecat;
87     assert (filecat);
88
89     /* determine number of block categories */
90     if (is->method->debug)
91         yaz_log (YLOG_LOG, "isc: bsize  ifill  mfill mblocks");
92     do
93     {
94         if (is->method->debug)
95             yaz_log (YLOG_LOG, "isc:%6d %6d %6d %6d",
96                   filecat[i].bsize, filecat[i].ifill, 
97                   filecat[i].mfill, filecat[i].mblocks);
98         if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
99             max_buf_size = filecat[i].mblocks * filecat[i].bsize;
100     } while (filecat[i++].mblocks);
101     is->no_files = i;
102     is->max_cat = --i;
103     /* max_buf_size is the larget buffer to be used during merge */
104     max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
105     if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
106         max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
107     if (is->method->debug)
108         yaz_log (YLOG_LOG, "isc: max_buf_size %d", max_buf_size);
109     
110     assert (is->no_files > 0);
111     is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
112     if (writeflag)
113     {
114         is->merge_buf = (char *) xmalloc (max_buf_size+256);
115         memset (is->merge_buf, 0, max_buf_size+256);
116     }
117     else
118         is->merge_buf = NULL;
119     for (i = 0; i<is->no_files; i++)
120     {
121         char fname[512];
122
123         sprintf (fname, "%s%c", name, i+'A');
124         is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
125                                    writeflag);
126         is->files[i].head_is_dirty = 0;
127         if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
128                      &is->files[i].head))
129         {
130             is->files[i].head.lastblock = 1;
131             is->files[i].head.freelist = 0;
132         }
133         is->files[i].alloc_entries_num = 0;
134         is->files[i].alloc_entries_max =
135             is->method->filecat[i].bsize / sizeof(zint) - 1;
136         is->files[i].alloc_buf = (char *)
137             xmalloc (is->method->filecat[i].bsize);
138         is->files[i].no_writes = 0;
139         is->files[i].no_reads = 0;
140         is->files[i].no_skip_writes = 0;
141         is->files[i].no_allocated = 0;
142         is->files[i].no_released = 0;
143         is->files[i].no_remap = 0;
144         is->files[i].no_forward = 0;
145         is->files[i].no_backward = 0;
146         is->files[i].sum_forward = 0;
147         is->files[i].sum_backward = 0;
148         is->files[i].no_next = 0;
149         is->files[i].no_prev = 0;
150
151         init_fc (is, i);
152     }
153     return is;
154 }
155
156 zint isc_block_used (ISAMC is, int type)
157 {
158     if (type < 0 || type >= is->no_files)
159         return -1;
160     return is->files[type].head.lastblock-1;
161 }
162
163 int isc_block_size (ISAMC is, int type)
164 {
165     ISAMC_filecat filecat = is->method->filecat;
166     if (type < 0 || type >= is->no_files)
167         return -1;
168     return filecat[type].bsize;
169 }
170
171 int isc_close (ISAMC is)
172 {
173     int i;
174
175     if (is->method->debug)
176     {
177         yaz_log (YLOG_LOG, "isc:    next    forw   mid-f    prev   backw   mid-b");
178         for (i = 0; i<is->no_files; i++)
179             yaz_log (YLOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
180                   is->files[i].no_next,
181                   is->files[i].no_forward,
182                   is->files[i].no_forward ?
183                   (double) is->files[i].sum_forward/is->files[i].no_forward
184                   : 0.0,
185                   is->files[i].no_prev,
186                   is->files[i].no_backward,
187                   is->files[i].no_backward ?
188                   (double) is->files[i].sum_backward/is->files[i].no_backward
189                   : 0.0);
190     }
191     if (is->method->debug)
192         yaz_log (YLOG_LOG, "isc:  writes   reads skipped   alloc released  remap");
193     for (i = 0; i<is->no_files; i++)
194     {
195         release_fc (is, i);
196         assert (is->files[i].bf);
197         if (is->files[i].head_is_dirty)
198             bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
199                  &is->files[i].head);
200         if (is->method->debug)
201             yaz_log (YLOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
202                   is->files[i].no_writes,
203                   is->files[i].no_reads,
204                   is->files[i].no_skip_writes,
205                   is->files[i].no_allocated,
206                   is->files[i].no_released,
207                   is->files[i].no_remap);
208         xfree (is->files[i].fc_list);
209         flush_block (is, i);
210         bf_close (is->files[i].bf);
211     }
212     xfree (is->files);
213     xfree (is->merge_buf);
214     xfree (is->method);
215     xfree (is);
216     return 0;
217 }
218
219 int isc_read_block (ISAMC is, int cat, zint pos, char *dst)
220 {
221     ++(is->files[cat].no_reads);
222     return bf_read (is->files[cat].bf, pos, 0, 0, dst);
223 }
224
225 int isc_write_block (ISAMC is, int cat, zint pos, char *src)
226 {
227     ++(is->files[cat].no_writes);
228     if (is->method->debug > 2)
229         yaz_log (YLOG_LOG, "isc: write_block %d " ZINT_FORMAT, cat, pos);
230     return bf_write (is->files[cat].bf, pos, 0, 0, src);
231 }
232
233 int isc_write_dblock (ISAMC is, int cat, zint pos, char *src,
234                       zint nextpos, int offset)
235 {
236     ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
237     if (is->method->debug > 2)
238         yaz_log (YLOG_LOG, "isc: write_dblock. size=%d nextpos=" ZINT_FORMAT,
239               (int) size, nextpos);
240     src -= ISAMC_BLOCK_OFFSET_N;
241     memcpy (src, &nextpos, sizeof(nextpos));
242     memcpy (src + sizeof(nextpos), &size, sizeof(size));
243     return isc_write_block (is, cat, pos, src);
244 }
245
246 #if ISAMC_FREELIST_CHUNK
247 static void flush_block (ISAMC is, int cat)
248 {
249     char *abuf = is->files[cat].alloc_buf;
250     zint block = is->files[cat].head.freelist;
251     if (block && is->files[cat].alloc_entries_num)
252     {
253         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(block));
254         bf_write (is->files[cat].bf, block, 0, 0, abuf);
255         is->files[cat].alloc_entries_num = 0;
256     }
257     xfree (abuf);
258 }
259
260 static zint alloc_block (ISAMC is, int cat)
261 {
262     zint block = is->files[cat].head.freelist;
263     char *abuf = is->files[cat].alloc_buf;
264
265     (is->files[cat].no_allocated)++;
266
267     if (!block)
268     {
269         block = (is->files[cat].head.lastblock)++;   /* no free list */
270         is->files[cat].head_is_dirty = 1;
271     }
272     else
273     {
274         if (!is->files[cat].alloc_entries_num) /* read first time */
275         {
276             bf_read (is->files[cat].bf, block, 0, 0, abuf);
277             memcpy (&is->files[cat].alloc_entries_num, abuf,
278                     sizeof(is->files[cat].alloc_entries_num));
279             assert (is->files[cat].alloc_entries_num > 0);
280         }
281         /* have some free blocks now */
282         assert (is->files[cat].alloc_entries_num > 0);
283         is->files[cat].alloc_entries_num--;
284         if (!is->files[cat].alloc_entries_num)  /* last one in block? */
285         {
286             memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
287                     sizeof(zint));
288             is->files[cat].head_is_dirty = 1;
289
290             if (is->files[cat].head.freelist)
291             {
292                 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
293                          0, 0, abuf);
294                 memcpy (&is->files[cat].alloc_entries_num, abuf,
295                         sizeof(is->files[cat].alloc_entries_num));
296                 assert (is->files[cat].alloc_entries_num);
297             }
298         }
299         else
300             memcpy (&block, abuf + sizeof(zint) + sizeof(int) *
301                     is->files[cat].alloc_entries_num, sizeof(zint));
302     }
303     return block;
304 }
305
306 static void release_block (ISAMC is, int cat, zint pos)
307 {
308     char *abuf = is->files[cat].alloc_buf;
309     zint block = is->files[cat].head.freelist;
310
311     (is->files[cat].no_released)++;
312
313     if (block && !is->files[cat].alloc_entries_num) /* must read block */
314     {
315         bf_read (is->files[cat].bf, block, 0, 0, abuf);
316         memcpy (&is->files[cat].alloc_entries_num, abuf,
317                 sizeof(is->files[cat].alloc_entries_num));
318         assert (is->files[cat].alloc_entries_num > 0);
319     }
320     assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
321     if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
322     {
323         assert (block);
324         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
325         bf_write (is->files[cat].bf, block, 0, 0, abuf);
326         is->files[cat].alloc_entries_num = 0;
327     }
328     if (!is->files[cat].alloc_entries_num) /* make new buffer? */
329     {
330         memcpy (abuf + sizeof(int), &block, sizeof(zint));
331         is->files[cat].head.freelist = pos;
332         is->files[cat].head_is_dirty = 1; 
333     }
334     else
335     {
336         memcpy (abuf + sizeof(int) +
337                 is->files[cat].alloc_entries_num*sizeof(zint),
338                 &pos, sizeof(zint));
339     }
340     is->files[cat].alloc_entries_num++;
341 }
342 #else
343 static void flush_block (ISAMC is, int cat)
344 {
345     char *abuf = is->files[cat].alloc_buf;
346     xfree (abuf);
347 }
348
349 static zint alloc_block (ISAMC is, int cat)
350 {
351     zint block;
352     char buf[sizeof(zint)];
353
354     is->files[cat].head_is_dirty = 1;
355     (is->files[cat].no_allocated)++;
356     if ((block = is->files[cat].head.freelist))
357     {
358         bf_read (is->files[cat].bf, block, 0, sizeof(zint), buf);
359         memcpy (&is->files[cat].head.freelist, buf, sizeof(zint));
360     }
361     else
362         block = (is->files[cat].head.lastblock)++;
363     return block;
364 }
365
366 static void release_block (ISAMC is, int cat, zint pos)
367 {
368     char buf[sizeof(zint)];
369    
370     (is->files[cat].no_released)++;
371     is->files[cat].head_is_dirty = 1; 
372     memcpy (buf, &is->files[cat].head.freelist, sizeof(zint));
373     is->files[cat].head.freelist = pos;
374     bf_write (is->files[cat].bf, pos, 0, sizeof(zint), buf);
375 }
376 #endif
377
378 zint isc_alloc_block (ISAMC is, int cat)
379 {
380     zint block = 0;
381
382     if (is->files[cat].fc_list)
383     {
384         int j;
385         zint nb;
386         for (j = 0; j < is->files[cat].fc_max; j++)
387             if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
388             {
389                 is->files[cat].fc_list[j] = 0;
390                 block = nb;
391                 break;
392             }
393     }
394     if (!block)
395         block = alloc_block (is, cat);
396     if (is->method->debug > 3)
397         yaz_log (YLOG_LOG, "isc: alloc_block in cat %d: " ZINT_FORMAT, cat, block);
398     return block;
399 }
400
401 void isc_release_block (ISAMC is, int cat, zint pos)
402 {
403     if (is->method->debug > 3)
404         yaz_log (YLOG_LOG, "isc: release_block in cat %d:" ZINT_FORMAT, cat, pos);
405     if (is->files[cat].fc_list)
406     {
407         int j;
408         for (j = 0; j<is->files[cat].fc_max; j++)
409             if (!is->files[cat].fc_list[j])
410             {
411                 is->files[cat].fc_list[j] = pos;
412                 return;
413             }
414     }
415     release_block (is, cat, pos);
416 }
417
418 static void init_fc (ISAMC is, int cat)
419 {
420     int j = 100;
421         
422     is->files[cat].fc_max = j;
423     is->files[cat].fc_list = (zint *)
424         xmalloc (sizeof(*is->files[0].fc_list) * j);
425     while (--j >= 0)
426         is->files[cat].fc_list[j] = 0;
427 }
428
429 static void release_fc (ISAMC is, int cat)
430 {
431     int j = is->files[cat].fc_max;
432     zint b;
433
434     while (--j >= 0)
435         if ((b = is->files[cat].fc_list[j]))
436         {
437             release_block (is, cat, b);
438             is->files[cat].fc_list[j] = 0;
439         }
440 }
441
442 void isc_pp_close (ISAMC_PP pp)
443 {
444     ISAMC is = pp->is;
445
446     (*is->method->codec.stop)(pp->decodeClientData);
447     xfree (pp->buf);
448     xfree (pp);
449 }
450
451 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
452 {
453     ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
454     char *src;
455    
456     pp->cat = (int) isc_type(ipos);
457     pp->pos = isc_block(ipos); 
458
459     src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
460
461     pp->next = 0;
462     pp->size = 0;
463     pp->offset = 0;
464     pp->is = is;
465     pp->decodeClientData = (*is->method->codec.start)();
466     pp->deleteFlag = 0;
467     pp->numKeys = 0;
468
469     if (pp->pos)
470     {
471         src = pp->buf;
472         isc_read_block (is, pp->cat, pp->pos, src);
473         memcpy (&pp->next, src, sizeof(pp->next));
474         src += sizeof(pp->next);
475         memcpy (&pp->size, src, sizeof(pp->size));
476         src += sizeof(pp->size);
477         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
478         src += sizeof(pp->numKeys);
479         if (pp->next == pp->pos)
480         {
481             yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
482             yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
483             assert (pp->next != pp->pos);
484         }
485         pp->offset = src - pp->buf; 
486         assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
487         if (is->method->debug > 2)
488             yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
489                   ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
490     }
491     return pp;
492 }
493
494 /* returns non-zero if item could be read; 0 otherwise */
495 int isc_pp_read (ISAMC_PP pp, void *buf)
496 {
497     char *cp = buf;
498     return isc_read_item (pp, &cp);
499 }
500
501 /* read one item from file - decode and store it in *dst.
502    Returns
503      0 if end-of-file
504      1 if item could be read ok and NO boundary
505      2 if item could be read ok and boundary */
506 int isc_read_item (ISAMC_PP pp, char **dst)
507 {
508     ISAMC is = pp->is;
509     const char *src = pp->buf + pp->offset;
510
511     if (pp->offset >= pp->size)
512     {
513         if (!pp->next)
514         {
515             pp->pos = 0;
516             return 0; /* end of file */
517         }
518         if (pp->next > pp->pos)
519         {
520             if (pp->next == pp->pos + 1)
521                 is->files[pp->cat].no_next++;
522             else
523             {
524                 is->files[pp->cat].no_forward++;
525                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
526             }
527         }
528         else
529         {
530             if (pp->next + 1 == pp->pos)
531                 is->files[pp->cat].no_prev++;
532             else
533             {
534                 is->files[pp->cat].no_backward++;
535                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
536             }
537         }
538         /* out new block position */
539         pp->pos = pp->next;
540         src = pp->buf;
541         /* read block and save 'next' and 'size' entry */
542         isc_read_block (is, pp->cat, pp->pos, pp->buf);
543         memcpy (&pp->next, src, sizeof(pp->next));
544         src += sizeof(pp->next);
545         memcpy (&pp->size, src, sizeof(pp->size));
546         src += sizeof(pp->size);
547         /* assume block is non-empty */
548         assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
549
550         if (pp->next == pp->pos)
551         {
552             yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
553             yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
554             assert (pp->next != pp->pos);
555         }
556
557         if (pp->deleteFlag)
558             isc_release_block (is, pp->cat, pp->pos);
559         (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
560         pp->offset = src - pp->buf; 
561         if (is->method->debug > 2)
562             yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
563                   ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
564         return 2;
565     }
566     (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
567     pp->offset = src - pp->buf; 
568     return 1;
569 }
570
571 zint isc_pp_num (ISAMC_PP pp)
572 {
573     return pp->numKeys;
574 }
575