2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
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.
11 * Revision 1.5 1996/11/04 14:08:57 adam
12 * Optimized free block usage.
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.
20 * Revision 1.3 1996/11/01 08:59:14 adam
21 * First version of isc_merge that supports update/delete.
23 * Revision 1.2 1996/10/29 16:44:56 adam
26 * Revision 1.1 1996/10/29 13:40:48 adam
33 * Reduction to lower categories in isc_merge
43 static void release_fc (ISAMC is, int cat);
44 static void init_fc (ISAMC is, int cat);
46 ISAMC_M isc_getmethod (void)
48 static struct ISAMC_filecat_s def_cat[] = {
50 { 512, 490, 100, 20 },
51 { 4096, 3950, 1000, 20 },
52 {32768, 32000, 10000, 0 },
54 ISAMC_M m = xmalloc (sizeof(*m));
61 m->compare_item = NULL;
65 m->max_blocks_mem = 10;
71 ISAMC isc_open (const char *name, int writeflag, ISAMC_M method)
74 ISAMC_filecat filecat;
78 is = xmalloc (sizeof(*is));
80 is->method = xmalloc (sizeof(*is->method));
81 memcpy (is->method, method, sizeof(*method));
82 filecat = is->method->filecat;
85 /* determine number of block categories */
86 if (is->method->debug)
87 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
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);
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);
106 assert (is->no_files > 0);
107 is->files = xmalloc (sizeof(*is->files)*is->no_files);
110 is->merge_buf = xmalloc (max_buf_size+256);
111 memset (is->merge_buf, 0, max_buf_size+256);
114 is->merge_buf = NULL;
115 for (i = 0; i<is->no_files; i++)
119 sprintf (fname, "%s%c", name, i+'A');
120 is->files[i].bf = bf_open (fname, is->method->filecat[i].bsize,
122 is->files[i].head_is_dirty = 0;
123 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
126 is->files[i].head.lastblock = 1;
127 is->files[i].head.freelist = 0;
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;
141 int isc_close (ISAMC is)
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++)
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),
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);
166 xfree (is->merge_buf);
171 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
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);
179 int isc_write_block (ISAMC is, int cat, int pos, char *src)
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);
187 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
188 int nextpos, int offset)
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);
200 static int alloc_block (ISAMC is, int cat)
203 char buf[sizeof(int)];
205 is->files[cat].head_is_dirty = 1;
206 (is->files[cat].no_allocated)++;
207 if ((block = is->files[cat].head.freelist))
209 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
210 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
213 block = (is->files[cat].head.lastblock)++;
217 int isc_alloc_block (ISAMC is, int cat)
221 if (is->files[cat].fc_list)
224 for (j = 0; j < is->files[cat].fc_max; j++)
225 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
227 is->files[cat].fc_list[j] = 0;
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);
238 static void release_block (ISAMC is, int cat, int pos)
240 char buf[sizeof(int)];
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);
249 void isc_release_block (ISAMC is, int cat, int pos)
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)
256 for (j = 0; j<is->files[cat].fc_max; j++)
257 if (!is->files[cat].fc_list[j])
259 is->files[cat].fc_list[j] = pos;
263 release_block (is, cat, pos);
266 static void init_fc (ISAMC is, int cat)
270 is->files[cat].fc_max = j;
271 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
273 is->files[cat].fc_list[j] = 0;
276 static void release_fc (ISAMC is, int cat)
278 int b, j = is->files[cat].fc_max;
281 if ((b = is->files[cat].fc_list[j]))
283 release_block (is, cat, b);
284 is->files[cat].fc_list[j] = 0;
288 void isc_pp_close (ISAMC_PP pp)
292 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
297 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
299 ISAMC_PP pp = xmalloc (sizeof(*pp));
302 pp->cat = isc_type(ipos);
303 pp->pos = isc_block(ipos);
305 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
311 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
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);
332 /* returns non-zero if item could be read; 0 otherwise */
333 int isc_pp_read (ISAMC_PP pp, void *buf)
335 return isc_read_item (pp, (char **) &buf);
338 /* returns non-zero if item could be read; 0 otherwise */
339 int isc_read_item (ISAMC_PP pp, char **dst)
342 char *src = pp->buf + pp->offset;
344 if (pp->offset >= pp->size)
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);
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;
364 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
365 pp->offset = src - pp->buf;
369 int isc_pp_num (ISAMC_PP pp)