2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1997-02-12 20:42:43 adam
8 * Bug fix: during isc_merge operations, some pages weren't marked dirty
9 * even though they should be. At this point the merge operation marks
10 * a page dirty if the previous page changed at all. A better approach is
11 * to mark it dirty if the last key written changed in previous page.
13 * Revision 1.6 1996/11/08 11:15:29 adam
14 * Number of keys in chain are stored in first block and the function
15 * to retrieve this information, isc_pp_num is implemented.
17 * Revision 1.5 1996/11/04 14:08:57 adam
18 * Optimized free block usage.
20 * Revision 1.4 1996/11/01 13:36:46 adam
21 * New element, max_blocks_mem, that control how many blocks of max size
22 * to store in memory during isc_merge.
23 * Function isc_merge now ignores delete/update of identical keys and
24 * the proper blocks are then non-dirty and not written in flush_blocks.
26 * Revision 1.3 1996/11/01 08:59:14 adam
27 * First version of isc_merge that supports update/delete.
29 * Revision 1.2 1996/10/29 16:44:56 adam
32 * Revision 1.1 1996/10/29 13:40:48 adam
39 * Reduction to lower categories in isc_merge
49 static void release_fc (ISAMC is, int cat);
50 static void init_fc (ISAMC is, int cat);
52 ISAMC_M isc_getmethod (void)
54 static struct ISAMC_filecat_s def_cat[] = {
56 { 512, 490, 100, 20 },
57 { 4096, 3950, 1000, 20 },
58 {32768, 32000, 10000, 0 },
60 ISAMC_M m = xmalloc (sizeof(*m));
67 m->compare_item = NULL;
71 m->max_blocks_mem = 10;
77 ISAMC isc_open (const char *name, int writeflag, ISAMC_M method)
80 ISAMC_filecat filecat;
84 is = xmalloc (sizeof(*is));
86 is->method = xmalloc (sizeof(*is->method));
87 memcpy (is->method, method, sizeof(*method));
88 filecat = is->method->filecat;
91 /* determine number of block categories */
92 if (is->method->debug)
93 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
96 if (is->method->debug)
97 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
98 filecat[i].bsize, filecat[i].ifill,
99 filecat[i].mfill, filecat[i].mblocks);
100 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
101 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
102 } while (filecat[i++].mblocks);
105 /* max_buf_size is the larget buffer to be used during merge */
106 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
107 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
108 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
109 if (is->method->debug)
110 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
112 assert (is->no_files > 0);
113 is->files = xmalloc (sizeof(*is->files)*is->no_files);
116 is->merge_buf = xmalloc (max_buf_size+256);
117 memset (is->merge_buf, 0, max_buf_size+256);
120 is->merge_buf = NULL;
121 for (i = 0; i<is->no_files; i++)
125 sprintf (fname, "%s%c", name, i+'A');
126 is->files[i].bf = bf_open (fname, is->method->filecat[i].bsize,
128 is->files[i].head_is_dirty = 0;
129 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
132 is->files[i].head.lastblock = 1;
133 is->files[i].head.freelist = 0;
135 is->files[i].no_writes = 0;
136 is->files[i].no_reads = 0;
137 is->files[i].no_skip_writes = 0;
138 is->files[i].no_allocated = 0;
139 is->files[i].no_released = 0;
140 is->files[i].no_remap = 0;
147 int isc_close (ISAMC is)
151 if (is->method->debug)
152 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
153 for (i = 0; i<is->no_files; i++)
156 assert (is->files[i].bf);
157 if (is->files[i].head_is_dirty)
158 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
160 if (is->method->debug)
161 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
162 is->files[i].no_writes,
163 is->files[i].no_reads,
164 is->files[i].no_skip_writes,
165 is->files[i].no_allocated,
166 is->files[i].no_released,
167 is->files[i].no_remap);
168 xfree (is->files[i].fc_list);
169 bf_close (is->files[i].bf);
172 xfree (is->merge_buf);
177 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
179 ++(is->files[cat].no_reads);
180 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
183 int isc_write_block (ISAMC is, int cat, int pos, char *src)
185 ++(is->files[cat].no_writes);
186 if (is->method->debug > 2)
187 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
188 return bf_write (is->files[cat].bf, pos, 0, 0, src);
191 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
192 int nextpos, int offset)
194 unsigned short size = offset + ISAMC_BLOCK_OFFSET_N;
195 if (is->method->debug > 2)
196 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
197 (int) size, nextpos);
198 src -= ISAMC_BLOCK_OFFSET_N;
199 memcpy (src, &nextpos, sizeof(int));
200 memcpy (src + sizeof(int), &size, sizeof(size));
201 return isc_write_block (is, cat, pos, src);
204 static int alloc_block (ISAMC is, int cat)
207 char buf[sizeof(int)];
209 is->files[cat].head_is_dirty = 1;
210 (is->files[cat].no_allocated)++;
211 if ((block = is->files[cat].head.freelist))
213 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
214 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
217 block = (is->files[cat].head.lastblock)++;
221 int isc_alloc_block (ISAMC is, int cat)
225 if (is->files[cat].fc_list)
228 for (j = 0; j < is->files[cat].fc_max; j++)
229 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
231 is->files[cat].fc_list[j] = 0;
236 block = alloc_block (is, cat);
237 if (is->method->debug > 3)
238 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
242 static void release_block (ISAMC is, int cat, int pos)
244 char buf[sizeof(int)];
246 (is->files[cat].no_released)++;
247 is->files[cat].head_is_dirty = 1;
248 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
249 is->files[cat].head.freelist = pos;
250 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
253 void isc_release_block (ISAMC is, int cat, int pos)
255 if (is->method->debug > 3)
256 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
257 if (is->files[cat].fc_list)
260 for (j = 0; j<is->files[cat].fc_max; j++)
261 if (!is->files[cat].fc_list[j])
263 is->files[cat].fc_list[j] = pos;
267 release_block (is, cat, pos);
270 static void init_fc (ISAMC is, int cat)
274 is->files[cat].fc_max = j;
275 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
277 is->files[cat].fc_list[j] = 0;
280 static void release_fc (ISAMC is, int cat)
282 int b, j = is->files[cat].fc_max;
285 if ((b = is->files[cat].fc_list[j]))
287 release_block (is, cat, b);
288 is->files[cat].fc_list[j] = 0;
292 void isc_pp_close (ISAMC_PP pp)
296 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
301 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
303 ISAMC_PP pp = xmalloc (sizeof(*pp));
306 pp->cat = isc_type(ipos);
307 pp->pos = isc_block(ipos);
309 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
315 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
322 isc_read_block (is, pp->cat, pp->pos, src);
323 memcpy (&pp->next, src, sizeof(pp->next));
324 src += sizeof(pp->next);
325 memcpy (&pp->size, src, sizeof(pp->size));
326 src += sizeof(pp->size);
327 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
328 src += sizeof(pp->numKeys);
329 assert (pp->next != pp->pos);
330 pp->offset = src - pp->buf;
331 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
332 if (is->method->debug > 2)
333 logf (LOG_LOG, "isc: read_block size=%d %d %d",
334 pp->size, pp->cat, pp->pos);
339 /* returns non-zero if item could be read; 0 otherwise */
340 int isc_pp_read (ISAMC_PP pp, void *buf)
342 return isc_read_item (pp, (char **) &buf);
345 /* returns non-zero if item could be read; 0 otherwise */
346 int isc_read_item (ISAMC_PP pp, char **dst)
349 char *src = pp->buf + pp->offset;
351 if (pp->offset >= pp->size)
357 isc_read_block (is, pp->cat, pp->pos, src);
358 memcpy (&pp->next, src, sizeof(pp->next));
359 src += sizeof(pp->next);
360 memcpy (&pp->size, src, sizeof(pp->size));
361 src += sizeof(pp->size);
362 /* assume block is non-empty */
363 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
364 assert (pp->next != pp->pos);
366 isc_release_block (is, pp->cat, pp->pos);
367 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
368 pp->offset = src - pp->buf;
369 if (is->method->debug > 2)
370 logf (LOG_LOG, "isc: read_block size=%d %d %d",
371 pp->size, pp->cat, pp->pos);
374 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
375 pp->offset = src - pp->buf;
379 int isc_pp_num (ISAMC_PP pp)