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