44155d22c9cf06166a3b96fa4db1a9e2479aaf65
[idzebra-moved-to-github.git] / isamc / isamc.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: isamc.c,v $
7  * Revision 1.11  1998-03-13 15:30:50  adam
8  * New functions isc_block_used and isc_block_size. Fixed 'leak'
9  * in isc_alloc_block.
10  *
11  * Revision 1.10  1998/03/11 11:18:18  adam
12  * Changed the isc_merge to take into account the mfill (minimum-fill).
13  *
14  * Revision 1.9  1998/03/06 13:54:02  adam
15  * Fixed two nasty bugs in isc_merge.
16  *
17  * Revision 1.8  1997/09/17 12:19:20  adam
18  * Zebra version corresponds to YAZ version 1.4.
19  * Changed Zebra server so that it doesn't depend on global common_resource.
20  *
21  * Revision 1.7  1997/02/12 20:42:43  adam
22  * Bug fix: during isc_merge operations, some pages weren't marked dirty
23  * even though they should be. At this point the merge operation marks
24  * a page dirty if the previous page changed at all. A better approach is
25  * to mark it dirty if the last key written changed in previous page.
26  *
27  * Revision 1.6  1996/11/08 11:15:29  adam
28  * Number of keys in chain are stored in first block and the function
29  * to retrieve this information, isc_pp_num is implemented.
30  *
31  * Revision 1.5  1996/11/04 14:08:57  adam
32  * Optimized free block usage.
33  *
34  * Revision 1.4  1996/11/01 13:36:46  adam
35  * New element, max_blocks_mem, that control how many blocks of max size
36  * to store in memory during isc_merge.
37  * Function isc_merge now ignores delete/update of identical keys and
38  * the proper blocks are then non-dirty and not written in flush_blocks.
39  *
40  * Revision 1.3  1996/11/01  08:59:14  adam
41  * First version of isc_merge that supports update/delete.
42  *
43  * Revision 1.2  1996/10/29 16:44:56  adam
44  * Work on isc_merge.
45  *
46  * Revision 1.1  1996/10/29  13:40:48  adam
47  * First work.
48  *
49  */
50
51 /* 
52  * TODO:
53  *   Reduction to lower categories in isc_merge
54  */
55 #include <stdlib.h>
56 #include <assert.h>
57 #include <string.h>
58 #include <stdio.h>
59
60 #include <log.h>
61 #include "isamc-p.h"
62
63 static void release_fc (ISAMC is, int cat);
64 static void init_fc (ISAMC is, int cat);
65
66 #define SMALL_TEST 0
67
68 ISAMC_M isc_getmethod (void)
69 {
70     static struct ISAMC_filecat_s def_cat[] = {
71 #if SMALL_TEST
72         {   32,    28,     0,    3 },
73         {   64,    54,    30,    0 },
74 #else
75         {   32,    28,    24,    20 },
76         {  512,   490,   350,    20 },
77         { 4096,  3950,  4200,    20 },
78         {32768, 32000, 30000,     0 },
79 #endif
80     };
81     ISAMC_M m = xmalloc (sizeof(*m));
82     m->filecat = def_cat;
83
84     m->code_start = NULL;
85     m->code_item = NULL;
86     m->code_stop = NULL;
87
88     m->compare_item = NULL;
89
90     m->debug = 1;
91
92     m->max_blocks_mem = 10;
93
94     return m;
95 }
96
97
98 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
99 {
100     ISAMC is;
101     ISAMC_filecat filecat;
102     int i = 0;
103     int max_buf_size = 0;
104
105     is = xmalloc (sizeof(*is));
106
107     is->method = xmalloc (sizeof(*is->method));
108     memcpy (is->method, method, sizeof(*method));
109     filecat = is->method->filecat;
110     assert (filecat);
111
112     /* determine number of block categories */
113     if (is->method->debug)
114         logf (LOG_LOG, "isc: bsize  ifill  mfill mblocks");
115     do
116     {
117         if (is->method->debug)
118             logf (LOG_LOG, "isc:%6d %6d %6d %6d",
119                   filecat[i].bsize, filecat[i].ifill, 
120                   filecat[i].mfill, filecat[i].mblocks);
121         if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
122             max_buf_size = filecat[i].mblocks * filecat[i].bsize;
123     } while (filecat[i++].mblocks);
124     is->no_files = i;
125     is->max_cat = --i;
126     /* max_buf_size is the larget buffer to be used during merge */
127     max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
128     if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
129         max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
130     if (is->method->debug)
131         logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
132     
133     assert (is->no_files > 0);
134     is->files = xmalloc (sizeof(*is->files)*is->no_files);
135     if (writeflag)
136     {
137         is->merge_buf = xmalloc (max_buf_size+256);
138         memset (is->merge_buf, 0, max_buf_size+256);
139     }
140     else
141         is->merge_buf = NULL;
142     for (i = 0; i<is->no_files; i++)
143     {
144         char fname[512];
145
146         sprintf (fname, "%s%c", name, i+'A');
147         is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
148                                    writeflag);
149         is->files[i].head_is_dirty = 0;
150         if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
151                      &is->files[i].head))
152         {
153             is->files[i].head.lastblock = 1;
154             is->files[i].head.freelist = 0;
155         }
156         is->files[i].no_writes = 0;
157         is->files[i].no_reads = 0;
158         is->files[i].no_skip_writes = 0;
159         is->files[i].no_allocated = 0;
160         is->files[i].no_released = 0;
161         is->files[i].no_remap = 0;
162
163         init_fc (is, i);
164     }
165     return is;
166 }
167
168 int isc_block_used (ISAMC is, int type)
169 {
170     if (type < 0 || type >= is->no_files)
171         return -1;
172     return is->files[type].head.lastblock-1;
173 }
174
175 int isc_block_size (ISAMC is, int type)
176 {
177     ISAMC_filecat filecat = is->method->filecat;
178     if (type < 0 || type >= is->no_files)
179         return -1;
180     return filecat[type].bsize;
181 }
182
183 int isc_close (ISAMC is)
184 {
185     int i;
186
187     if (is->method->debug)
188         logf (LOG_LOG, "isc:  writes   reads skipped   alloc released  remap");
189     for (i = 0; i<is->no_files; i++)
190     {
191         release_fc (is, i);
192         assert (is->files[i].bf);
193         if (is->files[i].head_is_dirty)
194             bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
195                  &is->files[i].head);
196         if (is->method->debug)
197             logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
198                   is->files[i].no_writes,
199                   is->files[i].no_reads,
200                   is->files[i].no_skip_writes,
201                   is->files[i].no_allocated,
202                   is->files[i].no_released,
203                   is->files[i].no_remap);
204         xfree (is->files[i].fc_list);
205         bf_close (is->files[i].bf);
206     }
207     xfree (is->files);
208     xfree (is->merge_buf);
209     xfree (is);
210     return 0;
211 }
212
213 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
214 {
215     ++(is->files[cat].no_reads);
216     return bf_read (is->files[cat].bf, pos, 0, 0, dst);
217 }
218
219 int isc_write_block (ISAMC is, int cat, int pos, char *src)
220 {
221     ++(is->files[cat].no_writes);
222     if (is->method->debug > 2)
223         logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
224     return bf_write (is->files[cat].bf, pos, 0, 0, src);
225 }
226
227 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
228                       int nextpos, int offset)
229 {
230     unsigned short size = offset + ISAMC_BLOCK_OFFSET_N;
231     if (is->method->debug > 2)
232         logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
233               (int) size, nextpos);
234     src -= ISAMC_BLOCK_OFFSET_N;
235     memcpy (src, &nextpos, sizeof(int));
236     memcpy (src + sizeof(int), &size, sizeof(size));
237     return isc_write_block (is, cat, pos, src);
238 }
239
240 static int alloc_block (ISAMC is, int cat)
241 {
242     int block;
243     char buf[sizeof(int)];
244
245     is->files[cat].head_is_dirty = 1;
246     (is->files[cat].no_allocated)++;
247     if ((block = is->files[cat].head.freelist))
248     {
249         bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
250         memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
251     }
252     else
253         block = (is->files[cat].head.lastblock)++;
254     return block;
255 }
256
257 int isc_alloc_block (ISAMC is, int cat)
258 {
259     int block = 0;
260
261     if (is->files[cat].fc_list)
262     {
263         int j, nb;
264         for (j = 0; j < is->files[cat].fc_max; j++)
265             if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
266             {
267                 is->files[cat].fc_list[j] = 0;
268                 block = nb;
269                 break;
270             }
271     }
272     if (!block)
273         block = alloc_block (is, cat);
274     if (is->method->debug > 3)
275         logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
276     return block;
277 }
278
279 static void release_block (ISAMC is, int cat, int pos)
280 {
281     char buf[sizeof(int)];
282    
283     (is->files[cat].no_released)++;
284     is->files[cat].head_is_dirty = 1; 
285     memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
286     is->files[cat].head.freelist = pos;
287     bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
288 }
289
290 void isc_release_block (ISAMC is, int cat, int pos)
291 {
292     if (is->method->debug > 3)
293         logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
294     if (is->files[cat].fc_list)
295     {
296         int j;
297         for (j = 0; j<is->files[cat].fc_max; j++)
298             if (!is->files[cat].fc_list[j])
299             {
300                 is->files[cat].fc_list[j] = pos;
301                 return;
302             }
303     }
304     release_block (is, cat, pos);
305 }
306
307 static void init_fc (ISAMC is, int cat)
308 {
309     int j = 100;
310         
311     is->files[cat].fc_max = j;
312     is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
313     while (--j >= 0)
314         is->files[cat].fc_list[j] = 0;
315 }
316
317 static void release_fc (ISAMC is, int cat)
318 {
319     int b, j = is->files[cat].fc_max;
320
321     while (--j >= 0)
322         if ((b = is->files[cat].fc_list[j]))
323         {
324             release_block (is, cat, b);
325             is->files[cat].fc_list[j] = 0;
326         }
327 }
328
329 void isc_pp_close (ISAMC_PP pp)
330 {
331     ISAMC is = pp->is;
332
333     (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
334     xfree (pp->buf);
335     xfree (pp);
336 }
337
338 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
339 {
340     ISAMC_PP pp = xmalloc (sizeof(*pp));
341     char *src;
342    
343     pp->cat = isc_type(ipos);
344     pp->pos = isc_block(ipos); 
345
346     src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
347
348     pp->next = 0;
349     pp->size = 0;
350     pp->offset = 0;
351     pp->is = is;
352     pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
353     pp->deleteFlag = 0;
354     pp->numKeys = 0;
355
356     if (pp->pos)
357     {
358         src = pp->buf;
359         isc_read_block (is, pp->cat, pp->pos, src);
360         memcpy (&pp->next, src, sizeof(pp->next));
361         src += sizeof(pp->next);
362         memcpy (&pp->size, src, sizeof(pp->size));
363         src += sizeof(pp->size);
364         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
365         src += sizeof(pp->numKeys);
366         assert (pp->next != pp->pos);
367         pp->offset = src - pp->buf; 
368         assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
369         if (is->method->debug > 2)
370             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
371                  pp->size, pp->cat, pp->pos, pp->next);
372     }
373     return pp;
374 }
375
376 /* returns non-zero if item could be read; 0 otherwise */
377 int isc_pp_read (ISAMC_PP pp, void *buf)
378 {
379     return isc_read_item (pp, (char **) &buf);
380 }
381
382 /* read one item from file - decode and store it in *dst.
383    Returns
384      0 if end-of-file
385      1 if item could be read ok and NO boundary
386      2 if item could be read ok and boundary */
387 int isc_read_item (ISAMC_PP pp, char **dst)
388 {
389     ISAMC is = pp->is;
390     char *src = pp->buf + pp->offset;
391
392     if (pp->offset >= pp->size)
393     {
394         /* out new block position */
395         pp->pos = pp->next;
396         if (!pp->pos)
397             return 0;    /* end of file */
398         src = pp->buf;
399         /* read block and save 'next' and 'size' entry */
400         isc_read_block (is, pp->cat, pp->pos, src);
401         memcpy (&pp->next, src, sizeof(pp->next));
402         src += sizeof(pp->next);
403         memcpy (&pp->size, src, sizeof(pp->size));
404         src += sizeof(pp->size);
405         /* assume block is non-empty */
406         assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
407         assert (pp->next != pp->pos);
408         if (pp->deleteFlag)
409             isc_release_block (is, pp->cat, pp->pos);
410         (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
411         pp->offset = src - pp->buf; 
412         if (is->method->debug > 2)
413             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
414                  pp->size, pp->cat, pp->pos, pp->next);
415         return 2;
416     }
417     (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
418     pp->offset = src - pp->buf; 
419     return 1;
420 }
421
422 int isc_pp_num (ISAMC_PP pp)
423 {
424     return pp->numKeys;
425 }
426