2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1998-03-13 15:30:50 adam
8 * New functions isc_block_used and isc_block_size. Fixed 'leak'
11 * Revision 1.10 1998/03/11 11:18:18 adam
12 * Changed the isc_merge to take into account the mfill (minimum-fill).
14 * Revision 1.9 1998/03/06 13:54:02 adam
15 * Fixed two nasty bugs in isc_merge.
17 * Revision 1.8 1997/09/17 12:19:20 adam
18 * Zebra version corresponds to YAZ version 1.4.
19 * Changed Zebra server so that it doesn't depend on global common_resource.
21 * Revision 1.7 1997/02/12 20:42:43 adam
22 * Bug fix: during isc_merge operations, some pages weren't marked dirty
23 * even though they should be. At this point the merge operation marks
24 * a page dirty if the previous page changed at all. A better approach is
25 * to mark it dirty if the last key written changed in previous page.
27 * Revision 1.6 1996/11/08 11:15:29 adam
28 * Number of keys in chain are stored in first block and the function
29 * to retrieve this information, isc_pp_num is implemented.
31 * Revision 1.5 1996/11/04 14:08:57 adam
32 * Optimized free block usage.
34 * Revision 1.4 1996/11/01 13:36:46 adam
35 * New element, max_blocks_mem, that control how many blocks of max size
36 * to store in memory during isc_merge.
37 * Function isc_merge now ignores delete/update of identical keys and
38 * the proper blocks are then non-dirty and not written in flush_blocks.
40 * Revision 1.3 1996/11/01 08:59:14 adam
41 * First version of isc_merge that supports update/delete.
43 * Revision 1.2 1996/10/29 16:44:56 adam
46 * Revision 1.1 1996/10/29 13:40:48 adam
53 * Reduction to lower categories in isc_merge
63 static void release_fc (ISAMC is, int cat);
64 static void init_fc (ISAMC is, int cat);
68 ISAMC_M isc_getmethod (void)
70 static struct ISAMC_filecat_s def_cat[] = {
76 { 512, 490, 350, 20 },
77 { 4096, 3950, 4200, 20 },
78 {32768, 32000, 30000, 0 },
81 ISAMC_M m = xmalloc (sizeof(*m));
88 m->compare_item = NULL;
92 m->max_blocks_mem = 10;
98 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
101 ISAMC_filecat filecat;
103 int max_buf_size = 0;
105 is = xmalloc (sizeof(*is));
107 is->method = xmalloc (sizeof(*is->method));
108 memcpy (is->method, method, sizeof(*method));
109 filecat = is->method->filecat;
112 /* determine number of block categories */
113 if (is->method->debug)
114 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
117 if (is->method->debug)
118 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
119 filecat[i].bsize, filecat[i].ifill,
120 filecat[i].mfill, filecat[i].mblocks);
121 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
122 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
123 } while (filecat[i++].mblocks);
126 /* max_buf_size is the larget buffer to be used during merge */
127 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
128 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
129 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
130 if (is->method->debug)
131 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
133 assert (is->no_files > 0);
134 is->files = xmalloc (sizeof(*is->files)*is->no_files);
137 is->merge_buf = xmalloc (max_buf_size+256);
138 memset (is->merge_buf, 0, max_buf_size+256);
141 is->merge_buf = NULL;
142 for (i = 0; i<is->no_files; i++)
146 sprintf (fname, "%s%c", name, i+'A');
147 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
149 is->files[i].head_is_dirty = 0;
150 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
153 is->files[i].head.lastblock = 1;
154 is->files[i].head.freelist = 0;
156 is->files[i].no_writes = 0;
157 is->files[i].no_reads = 0;
158 is->files[i].no_skip_writes = 0;
159 is->files[i].no_allocated = 0;
160 is->files[i].no_released = 0;
161 is->files[i].no_remap = 0;
168 int isc_block_used (ISAMC is, int type)
170 if (type < 0 || type >= is->no_files)
172 return is->files[type].head.lastblock-1;
175 int isc_block_size (ISAMC is, int type)
177 ISAMC_filecat filecat = is->method->filecat;
178 if (type < 0 || type >= is->no_files)
180 return filecat[type].bsize;
183 int isc_close (ISAMC is)
187 if (is->method->debug)
188 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
189 for (i = 0; i<is->no_files; i++)
192 assert (is->files[i].bf);
193 if (is->files[i].head_is_dirty)
194 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
196 if (is->method->debug)
197 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
198 is->files[i].no_writes,
199 is->files[i].no_reads,
200 is->files[i].no_skip_writes,
201 is->files[i].no_allocated,
202 is->files[i].no_released,
203 is->files[i].no_remap);
204 xfree (is->files[i].fc_list);
205 bf_close (is->files[i].bf);
208 xfree (is->merge_buf);
213 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
215 ++(is->files[cat].no_reads);
216 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
219 int isc_write_block (ISAMC is, int cat, int pos, char *src)
221 ++(is->files[cat].no_writes);
222 if (is->method->debug > 2)
223 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
224 return bf_write (is->files[cat].bf, pos, 0, 0, src);
227 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
228 int nextpos, int offset)
230 unsigned short size = offset + ISAMC_BLOCK_OFFSET_N;
231 if (is->method->debug > 2)
232 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
233 (int) size, nextpos);
234 src -= ISAMC_BLOCK_OFFSET_N;
235 memcpy (src, &nextpos, sizeof(int));
236 memcpy (src + sizeof(int), &size, sizeof(size));
237 return isc_write_block (is, cat, pos, src);
240 static int alloc_block (ISAMC is, int cat)
243 char buf[sizeof(int)];
245 is->files[cat].head_is_dirty = 1;
246 (is->files[cat].no_allocated)++;
247 if ((block = is->files[cat].head.freelist))
249 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
250 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
253 block = (is->files[cat].head.lastblock)++;
257 int isc_alloc_block (ISAMC is, int cat)
261 if (is->files[cat].fc_list)
264 for (j = 0; j < is->files[cat].fc_max; j++)
265 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
267 is->files[cat].fc_list[j] = 0;
273 block = alloc_block (is, cat);
274 if (is->method->debug > 3)
275 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
279 static void release_block (ISAMC is, int cat, int pos)
281 char buf[sizeof(int)];
283 (is->files[cat].no_released)++;
284 is->files[cat].head_is_dirty = 1;
285 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
286 is->files[cat].head.freelist = pos;
287 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
290 void isc_release_block (ISAMC is, int cat, int pos)
292 if (is->method->debug > 3)
293 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
294 if (is->files[cat].fc_list)
297 for (j = 0; j<is->files[cat].fc_max; j++)
298 if (!is->files[cat].fc_list[j])
300 is->files[cat].fc_list[j] = pos;
304 release_block (is, cat, pos);
307 static void init_fc (ISAMC is, int cat)
311 is->files[cat].fc_max = j;
312 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
314 is->files[cat].fc_list[j] = 0;
317 static void release_fc (ISAMC is, int cat)
319 int b, j = is->files[cat].fc_max;
322 if ((b = is->files[cat].fc_list[j]))
324 release_block (is, cat, b);
325 is->files[cat].fc_list[j] = 0;
329 void isc_pp_close (ISAMC_PP pp)
333 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
338 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
340 ISAMC_PP pp = xmalloc (sizeof(*pp));
343 pp->cat = isc_type(ipos);
344 pp->pos = isc_block(ipos);
346 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
352 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
359 isc_read_block (is, pp->cat, pp->pos, src);
360 memcpy (&pp->next, src, sizeof(pp->next));
361 src += sizeof(pp->next);
362 memcpy (&pp->size, src, sizeof(pp->size));
363 src += sizeof(pp->size);
364 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
365 src += sizeof(pp->numKeys);
366 assert (pp->next != pp->pos);
367 pp->offset = src - pp->buf;
368 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
369 if (is->method->debug > 2)
370 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
371 pp->size, pp->cat, pp->pos, pp->next);
376 /* returns non-zero if item could be read; 0 otherwise */
377 int isc_pp_read (ISAMC_PP pp, void *buf)
379 return isc_read_item (pp, (char **) &buf);
382 /* read one item from file - decode and store it in *dst.
385 1 if item could be read ok and NO boundary
386 2 if item could be read ok and boundary */
387 int isc_read_item (ISAMC_PP pp, char **dst)
390 char *src = pp->buf + pp->offset;
392 if (pp->offset >= pp->size)
394 /* out new block position */
397 return 0; /* end of file */
399 /* read block and save 'next' and 'size' entry */
400 isc_read_block (is, pp->cat, pp->pos, src);
401 memcpy (&pp->next, src, sizeof(pp->next));
402 src += sizeof(pp->next);
403 memcpy (&pp->size, src, sizeof(pp->size));
404 src += sizeof(pp->size);
405 /* assume block is non-empty */
406 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
407 assert (pp->next != pp->pos);
409 isc_release_block (is, pp->cat, pp->pos);
410 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
411 pp->offset = src - pp->buf;
412 if (is->method->debug > 2)
413 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
414 pp->size, pp->cat, pp->pos, pp->next);
417 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
418 pp->offset = src - pp->buf;
422 int isc_pp_num (ISAMC_PP pp)