2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1996-11-04 14:08:57 adam
8 * Optimized free block usage.
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.
16 * Revision 1.3 1996/11/01 08:59:14 adam
17 * First version of isc_merge that supports update/delete.
19 * Revision 1.2 1996/10/29 16:44:56 adam
22 * Revision 1.1 1996/10/29 13:40:48 adam
29 * Reduction to lower categories in isc_merge
30 * Implementation of isc_numkeys
40 static void release_fc (ISAMC is, int cat);
41 static void init_fc (ISAMC is, int cat);
43 ISAMC_M isc_getmethod (void)
45 static struct ISAMC_filecat_s def_cat[] = {
47 { 512, 490, 100, 20 },
48 { 4096, 3950, 1000, 20 },
49 {32768, 32000, 10000, 0 },
52 ISAMC_M m = xmalloc (sizeof(*m));
59 m->compare_item = NULL;
63 m->max_blocks_mem = 10;
69 ISAMC isc_open (const char *name, int writeflag, ISAMC_M method)
72 ISAMC_filecat filecat;
76 is = xmalloc (sizeof(*is));
78 is->method = xmalloc (sizeof(*is->method));
79 memcpy (is->method, method, sizeof(*method));
80 filecat = is->method->filecat;
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++)
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;
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);
104 assert (is->no_files > 0);
105 is->files = xmalloc (sizeof(*is->files)*is->no_files);
108 is->merge_buf = xmalloc (max_buf_size+128);
109 memset (is->merge_buf, 0, max_buf_size+128);
112 is->merge_buf = NULL;
113 for (i = 0; i<is->no_files; i++)
117 sprintf (fname, "%s%c", name, i+'A');
118 is->files[i].bf = bf_open (fname, is->method->filecat[i].bsize,
120 is->files[i].head_is_dirty = 0;
121 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
124 is->files[i].head.lastblock = 1;
125 is->files[i].head.freelist = 0;
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;
139 int isc_close (ISAMC is)
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++)
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),
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);
164 xfree (is->merge_buf);
169 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
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);
177 int isc_write_block (ISAMC is, int cat, int pos, char *src)
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);
185 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
186 int nextpos, int offset)
188 int xoffset = offset + 2*sizeof(int);
189 if (is->method->debug > 2)
190 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
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);
197 static int alloc_block (ISAMC is, int cat)
200 char buf[sizeof(int)];
202 is->files[cat].head_is_dirty = 1;
203 (is->files[cat].no_allocated)++;
204 if ((block = is->files[cat].head.freelist))
206 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
207 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
210 block = (is->files[cat].head.lastblock)++;
214 int isc_alloc_block (ISAMC is, int cat)
218 if (is->files[cat].fc_list)
221 for (j = 0; j < is->files[cat].fc_max; j++)
222 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
224 is->files[cat].fc_list[j] = 0;
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);
235 static void release_block (ISAMC is, int cat, int pos)
237 char buf[sizeof(int)];
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);
246 void isc_release_block (ISAMC is, int cat, int pos)
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)
253 for (j = 0; j<is->files[cat].fc_max; j++)
254 if (!is->files[cat].fc_list[j])
256 is->files[cat].fc_list[j] = pos;
260 release_block (is, cat, pos);
263 static void init_fc (ISAMC is, int cat)
267 is->files[cat].fc_max = j;
268 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
270 is->files[cat].fc_list[j] = 0;
273 static void release_fc (ISAMC is, int cat)
275 int b, j = is->files[cat].fc_max;
278 if ((b = is->files[cat].fc_list[j]))
280 release_block (is, cat, b);
281 is->files[cat].fc_list[j] = 0;
285 void isc_pp_close (ISAMC_PP pp)
289 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
294 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
296 ISAMC_PP pp = xmalloc (sizeof(*pp));
299 pp->cat = isc_type(ipos);
300 pp->next = isc_block(ipos);
302 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
308 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
313 /* returns non-zero if item could be read; 0 otherwise */
314 int isc_read_key (ISAMC_PP pp, void *buf)
316 return isc_read_item (pp, (char **) &buf);
319 /* returns non-zero if item could be read; 0 otherwise */
320 int isc_read_item (ISAMC_PP pp, char **dst)
323 char *src = pp->buf + pp->offset;
325 if (pp->offset >= pp->size)
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);
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;
344 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
345 pp->offset = src - pp->buf;
349 int isc_numkeys (ISAMC_PP pp)