9374da389c72c355c6b8426543d6017f0c7243a4
[idzebra-moved-to-github.git] / isamc / isamc.c
1 /* $Id: isamc.c,v 1.24 2004-06-01 12:56:39 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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  * TODO:
27  *   Reduction to lower categories in isc_merge
28  */
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #include <yaz/log.h>
35 #include "isamc-p.h"
36
37 static void flush_block (ISAMC is, int cat);
38 static void release_fc (ISAMC is, int cat);
39 static void init_fc (ISAMC is, int cat);
40
41 #define ISAMC_FREELIST_CHUNK 1
42
43 #define SMALL_TEST 0
44
45 void isc_getmethod (ISAMC_M *m)
46 {
47
48     static struct ISAMC_filecat_s def_cat[] = {
49 #if SMALL_TEST
50         {    32,     28,      0,  3 },
51         {    64,     54,     30,  0 },
52 #else
53         {    32,     26,     20,  10 },
54         {   128,    120,    100,  10 },
55         {   512,    490,    350,  10 },
56         {  2048,   1900,   1700,  10 },
57         {  8192,   8000,   7900,  10 },
58         { 32768,  32000,  31000,  0 },
59 #endif
60     };
61     m->filecat = def_cat;
62
63     m->code_start = NULL;
64     m->code_item = NULL;
65     m->code_stop = NULL;
66     m->code_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         logf (LOG_LOG, "isc: bsize  ifill  mfill mblocks");
93     do
94     {
95         if (is->method->debug)
96             logf (LOG_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         logf (LOG_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(int) - 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 int 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         logf (LOG_LOG, "isc:    next    forw   mid-f    prev   backw   mid-b");
179         for (i = 0; i<is->no_files; i++)
180             logf (LOG_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         logf (LOG_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             logf (LOG_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, int 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, int pos, char *src)
227 {
228     ++(is->files[cat].no_writes);
229     if (is->method->debug > 2)
230         logf (LOG_LOG, "isc: write_block %d %d", 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, int pos, char *src,
235                       int nextpos, int offset)
236 {
237     ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
238     if (is->method->debug > 2)
239         logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
240               (int) size, nextpos);
241     src -= ISAMC_BLOCK_OFFSET_N;
242     memcpy (src, &nextpos, sizeof(int));
243     memcpy (src + sizeof(int), &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     int 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(int));
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 int alloc_block (ISAMC is, int cat)
262 {
263     int 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(int));
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(int) + sizeof(int) *
302                     is->files[cat].alloc_entries_num, sizeof(int));
303     }
304     return block;
305 }
306
307 static void release_block (ISAMC is, int cat, int pos)
308 {
309     char *abuf = is->files[cat].alloc_buf;
310     int 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(int));
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(int),
339                 &pos, sizeof(int));
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 int alloc_block (ISAMC is, int cat)
351 {
352     int block;
353     char buf[sizeof(int)];
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(int), buf);
360         memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
361     }
362     else
363         block = (is->files[cat].head.lastblock)++;
364     return block;
365 }
366
367 static void release_block (ISAMC is, int cat, int pos)
368 {
369     char buf[sizeof(int)];
370    
371     (is->files[cat].no_released)++;
372     is->files[cat].head_is_dirty = 1; 
373     memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
374     is->files[cat].head.freelist = pos;
375     bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
376 }
377 #endif
378
379 int isc_alloc_block (ISAMC is, int cat)
380 {
381     int block = 0;
382
383     if (is->files[cat].fc_list)
384     {
385         int j, 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         logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
398     return block;
399 }
400
401 void isc_release_block (ISAMC is, int cat, int pos)
402 {
403     if (is->method->debug > 3)
404         logf (LOG_LOG, "isc: release_block in cat %d: %d", 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 = (int *)
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 b, j = is->files[cat].fc_max;
432
433     while (--j >= 0)
434         if ((b = is->files[cat].fc_list[j]))
435         {
436             release_block (is, cat, b);
437             is->files[cat].fc_list[j] = 0;
438         }
439 }
440
441 void isc_pp_close (ISAMC_PP pp)
442 {
443     ISAMC is = pp->is;
444
445     (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
446     xfree (pp->buf);
447     xfree (pp);
448 }
449
450 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
451 {
452     ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
453     char *src;
454    
455     pp->cat = isc_type(ipos);
456     pp->pos = isc_block(ipos); 
457
458     src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
459
460     pp->next = 0;
461     pp->size = 0;
462     pp->offset = 0;
463     pp->is = is;
464     pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
465     pp->deleteFlag = 0;
466     pp->numKeys = 0;
467
468     if (pp->pos)
469     {
470         src = pp->buf;
471         isc_read_block (is, pp->cat, pp->pos, src);
472         memcpy (&pp->next, src, sizeof(pp->next));
473         src += sizeof(pp->next);
474         memcpy (&pp->size, src, sizeof(pp->size));
475         src += sizeof(pp->size);
476         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
477         src += sizeof(pp->numKeys);
478         assert (pp->next != pp->pos);
479         pp->offset = src - pp->buf; 
480         assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
481         if (is->method->debug > 2)
482             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
483                  pp->size, pp->cat, pp->pos, pp->next);
484     }
485     return pp;
486 }
487
488 /* returns non-zero if item could be read; 0 otherwise */
489 int isc_pp_read (ISAMC_PP pp, void *buf)
490 {
491     return isc_read_item (pp, (char **) &buf);
492 }
493
494 /* read one item from file - decode and store it in *dst.
495    Returns
496      0 if end-of-file
497      1 if item could be read ok and NO boundary
498      2 if item could be read ok and boundary */
499 int isc_read_item (ISAMC_PP pp, char **dst)
500 {
501     ISAMC is = pp->is;
502     char *src = pp->buf + pp->offset;
503
504     if (pp->offset >= pp->size)
505     {
506         if (!pp->next)
507         {
508             pp->pos = 0;
509             return 0; /* end of file */
510         }
511         if (pp->next > pp->pos)
512         {
513             if (pp->next == pp->pos + 1)
514                 is->files[pp->cat].no_next++;
515             else
516             {
517                 is->files[pp->cat].no_forward++;
518                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
519             }
520         }
521         else
522         {
523             if (pp->next + 1 == pp->pos)
524                 is->files[pp->cat].no_prev++;
525             else
526             {
527                 is->files[pp->cat].no_backward++;
528                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
529             }
530         }
531         /* out new block position */
532         pp->pos = pp->next;
533         src = pp->buf;
534         /* read block and save 'next' and 'size' entry */
535         isc_read_block (is, pp->cat, pp->pos, src);
536         memcpy (&pp->next, src, sizeof(pp->next));
537         src += sizeof(pp->next);
538         memcpy (&pp->size, src, sizeof(pp->size));
539         src += sizeof(pp->size);
540         /* assume block is non-empty */
541         assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
542         assert (pp->next != pp->pos);
543         if (pp->deleteFlag)
544             isc_release_block (is, pp->cat, pp->pos);
545         (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
546         pp->offset = src - pp->buf; 
547         if (is->method->debug > 2)
548             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
549                  pp->size, pp->cat, pp->pos, pp->next);
550         return 2;
551     }
552     (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
553     pp->offset = src - pp->buf; 
554     return 1;
555 }
556
557 int isc_pp_num (ISAMC_PP pp)
558 {
559     return pp->numKeys;
560 }
561