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