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