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