2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1998-05-27 14:32:03 adam
8 * Changed default block category layout.
10 * Revision 1.15 1998/05/20 10:12:25 adam
11 * Implemented automatic EXPLAIN database maintenance.
12 * Modified Zebra to work with ASN.1 compiled version of YAZ.
14 * Revision 1.14 1998/03/19 10:04:35 adam
17 * Revision 1.13 1998/03/18 09:23:55 adam
18 * Blocks are stored in chunks on free list - up to factor 2 in speed.
19 * Fixed bug that could occur in block category rearrangemen.
21 * Revision 1.12 1998/03/16 10:37:24 adam
22 * Added more statistics.
24 * Revision 1.11 1998/03/13 15:30:50 adam
25 * New functions isc_block_used and isc_block_size. Fixed 'leak'
28 * Revision 1.10 1998/03/11 11:18:18 adam
29 * Changed the isc_merge to take into account the mfill (minimum-fill).
31 * Revision 1.9 1998/03/06 13:54:02 adam
32 * Fixed two nasty bugs in isc_merge.
34 * Revision 1.8 1997/09/17 12:19:20 adam
35 * Zebra version corresponds to YAZ version 1.4.
36 * Changed Zebra server so that it doesn't depend on global common_resource.
38 * Revision 1.7 1997/02/12 20:42:43 adam
39 * Bug fix: during isc_merge operations, some pages weren't marked dirty
40 * even though they should be. At this point the merge operation marks
41 * a page dirty if the previous page changed at all. A better approach is
42 * to mark it dirty if the last key written changed in previous page.
44 * Revision 1.6 1996/11/08 11:15:29 adam
45 * Number of keys in chain are stored in first block and the function
46 * to retrieve this information, isc_pp_num is implemented.
48 * Revision 1.5 1996/11/04 14:08:57 adam
49 * Optimized free block usage.
51 * Revision 1.4 1996/11/01 13:36:46 adam
52 * New element, max_blocks_mem, that control how many blocks of max size
53 * to store in memory during isc_merge.
54 * Function isc_merge now ignores delete/update of identical keys and
55 * the proper blocks are then non-dirty and not written in flush_blocks.
57 * Revision 1.3 1996/11/01 08:59:14 adam
58 * First version of isc_merge that supports update/delete.
60 * Revision 1.2 1996/10/29 16:44:56 adam
63 * Revision 1.1 1996/10/29 13:40:48 adam
70 * Reduction to lower categories in isc_merge
80 static void flush_block (ISAMC is, int cat);
81 static void release_fc (ISAMC is, int cat);
82 static void init_fc (ISAMC is, int cat);
84 #define ISAMC_FREELIST_CHUNK 1
88 ISAMC_M isc_getmethod (void)
90 static struct ISAMC_filecat_s def_cat[] = {
96 { 128, 120, 100, 10 },
97 { 512, 490, 350, 10 },
98 { 2048, 1900, 1700, 10 },
99 { 8192, 8000, 7900, 10 },
100 { 32768, 32000, 31000, 0 },
103 ISAMC_M m = xmalloc (sizeof(*m));
104 m->filecat = def_cat;
106 m->code_start = NULL;
110 m->compare_item = NULL;
114 m->max_blocks_mem = 10;
120 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
123 ISAMC_filecat filecat;
125 int max_buf_size = 0;
127 is = xmalloc (sizeof(*is));
129 is->method = xmalloc (sizeof(*is->method));
130 memcpy (is->method, method, sizeof(*method));
131 filecat = is->method->filecat;
134 /* determine number of block categories */
135 if (is->method->debug)
136 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
139 if (is->method->debug)
140 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
141 filecat[i].bsize, filecat[i].ifill,
142 filecat[i].mfill, filecat[i].mblocks);
143 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
144 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
145 } while (filecat[i++].mblocks);
148 /* max_buf_size is the larget buffer to be used during merge */
149 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
150 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
151 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
152 if (is->method->debug)
153 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
155 assert (is->no_files > 0);
156 is->files = xmalloc (sizeof(*is->files)*is->no_files);
159 is->merge_buf = xmalloc (max_buf_size+256);
160 memset (is->merge_buf, 0, max_buf_size+256);
163 is->merge_buf = NULL;
164 for (i = 0; i<is->no_files; i++)
168 sprintf (fname, "%s%c", name, i+'A');
169 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
171 is->files[i].head_is_dirty = 0;
172 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
175 is->files[i].head.lastblock = 1;
176 is->files[i].head.freelist = 0;
178 is->files[i].alloc_entries_num = 0;
179 is->files[i].alloc_entries_max =
180 is->method->filecat[i].bsize / sizeof(int) - 1;
181 is->files[i].alloc_buf = xmalloc (is->method->filecat[i].bsize);
182 is->files[i].no_writes = 0;
183 is->files[i].no_reads = 0;
184 is->files[i].no_skip_writes = 0;
185 is->files[i].no_allocated = 0;
186 is->files[i].no_released = 0;
187 is->files[i].no_remap = 0;
188 is->files[i].no_forward = 0;
189 is->files[i].no_backward = 0;
190 is->files[i].sum_forward = 0;
191 is->files[i].sum_backward = 0;
192 is->files[i].no_next = 0;
193 is->files[i].no_prev = 0;
200 int isc_block_used (ISAMC is, int type)
202 if (type < 0 || type >= is->no_files)
204 return is->files[type].head.lastblock-1;
207 int isc_block_size (ISAMC is, int type)
209 ISAMC_filecat filecat = is->method->filecat;
210 if (type < 0 || type >= is->no_files)
212 return filecat[type].bsize;
215 int isc_close (ISAMC is)
219 if (is->method->debug)
221 logf (LOG_LOG, "isc: next forw mid-f prev backw mid-b");
222 for (i = 0; i<is->no_files; i++)
223 logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
224 is->files[i].no_next,
225 is->files[i].no_forward,
226 is->files[i].no_forward ?
227 (double) is->files[i].sum_forward/is->files[i].no_forward
229 is->files[i].no_prev,
230 is->files[i].no_backward,
231 is->files[i].no_backward ?
232 (double) is->files[i].sum_backward/is->files[i].no_backward
235 if (is->method->debug)
236 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
237 for (i = 0; i<is->no_files; i++)
240 assert (is->files[i].bf);
241 if (is->files[i].head_is_dirty)
242 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
244 if (is->method->debug)
245 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
246 is->files[i].no_writes,
247 is->files[i].no_reads,
248 is->files[i].no_skip_writes,
249 is->files[i].no_allocated,
250 is->files[i].no_released,
251 is->files[i].no_remap);
252 xfree (is->files[i].fc_list);
254 bf_close (is->files[i].bf);
257 xfree (is->merge_buf);
263 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
265 ++(is->files[cat].no_reads);
266 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
269 int isc_write_block (ISAMC is, int cat, int pos, char *src)
271 ++(is->files[cat].no_writes);
272 if (is->method->debug > 2)
273 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
274 return bf_write (is->files[cat].bf, pos, 0, 0, src);
277 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
278 int nextpos, int offset)
280 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
281 if (is->method->debug > 2)
282 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
283 (int) size, nextpos);
284 src -= ISAMC_BLOCK_OFFSET_N;
285 memcpy (src, &nextpos, sizeof(int));
286 memcpy (src + sizeof(int), &size, sizeof(size));
287 return isc_write_block (is, cat, pos, src);
290 #if ISAMC_FREELIST_CHUNK
291 static void flush_block (ISAMC is, int cat)
293 char *abuf = is->files[cat].alloc_buf;
294 int block = is->files[cat].head.freelist;
295 if (block && is->files[cat].alloc_entries_num)
297 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
298 bf_write (is->files[cat].bf, block, 0, 0, abuf);
299 is->files[cat].alloc_entries_num = 0;
304 static int alloc_block (ISAMC is, int cat)
306 int block = is->files[cat].head.freelist;
307 char *abuf = is->files[cat].alloc_buf;
309 (is->files[cat].no_allocated)++;
313 block = (is->files[cat].head.lastblock)++; /* no free list */
314 is->files[cat].head_is_dirty = 1;
318 if (!is->files[cat].alloc_entries_num) /* read first time */
320 bf_read (is->files[cat].bf, block, 0, 0, abuf);
321 memcpy (&is->files[cat].alloc_entries_num, abuf,
322 sizeof(is->files[cat].alloc_entries_num));
323 assert (is->files[cat].alloc_entries_num > 0);
325 /* have some free blocks now */
326 assert (is->files[cat].alloc_entries_num > 0);
327 is->files[cat].alloc_entries_num--;
328 if (!is->files[cat].alloc_entries_num) /* last one in block? */
330 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
332 is->files[cat].head_is_dirty = 1;
334 if (is->files[cat].head.freelist)
336 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
338 memcpy (&is->files[cat].alloc_entries_num, abuf,
339 sizeof(is->files[cat].alloc_entries_num));
340 assert (is->files[cat].alloc_entries_num);
344 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
345 is->files[cat].alloc_entries_num, sizeof(int));
350 static void release_block (ISAMC is, int cat, int pos)
352 char *abuf = is->files[cat].alloc_buf;
353 int block = is->files[cat].head.freelist;
355 (is->files[cat].no_released)++;
357 if (block && !is->files[cat].alloc_entries_num) /* must read block */
359 bf_read (is->files[cat].bf, block, 0, 0, abuf);
360 memcpy (&is->files[cat].alloc_entries_num, abuf,
361 sizeof(is->files[cat].alloc_entries_num));
362 assert (is->files[cat].alloc_entries_num > 0);
364 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
365 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
368 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
369 bf_write (is->files[cat].bf, block, 0, 0, abuf);
370 is->files[cat].alloc_entries_num = 0;
372 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
374 memcpy (abuf + sizeof(int), &block, sizeof(int));
375 is->files[cat].head.freelist = pos;
376 is->files[cat].head_is_dirty = 1;
380 memcpy (abuf + sizeof(int) +
381 is->files[cat].alloc_entries_num*sizeof(int),
384 is->files[cat].alloc_entries_num++;
387 static void flush_block (ISAMC is, int cat)
389 char *abuf = is->files[cat].alloc_buf;
393 static int alloc_block (ISAMC is, int cat)
396 char buf[sizeof(int)];
398 is->files[cat].head_is_dirty = 1;
399 (is->files[cat].no_allocated)++;
400 if ((block = is->files[cat].head.freelist))
402 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
403 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
406 block = (is->files[cat].head.lastblock)++;
410 static void release_block (ISAMC is, int cat, int pos)
412 char buf[sizeof(int)];
414 (is->files[cat].no_released)++;
415 is->files[cat].head_is_dirty = 1;
416 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
417 is->files[cat].head.freelist = pos;
418 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
422 int isc_alloc_block (ISAMC is, int cat)
426 if (is->files[cat].fc_list)
429 for (j = 0; j < is->files[cat].fc_max; j++)
430 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
432 is->files[cat].fc_list[j] = 0;
438 block = alloc_block (is, cat);
439 if (is->method->debug > 3)
440 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
444 void isc_release_block (ISAMC is, int cat, int pos)
446 if (is->method->debug > 3)
447 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
448 if (is->files[cat].fc_list)
451 for (j = 0; j<is->files[cat].fc_max; j++)
452 if (!is->files[cat].fc_list[j])
454 is->files[cat].fc_list[j] = pos;
458 release_block (is, cat, pos);
461 static void init_fc (ISAMC is, int cat)
465 is->files[cat].fc_max = j;
466 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
468 is->files[cat].fc_list[j] = 0;
471 static void release_fc (ISAMC is, int cat)
473 int b, j = is->files[cat].fc_max;
476 if ((b = is->files[cat].fc_list[j]))
478 release_block (is, cat, b);
479 is->files[cat].fc_list[j] = 0;
483 void isc_pp_close (ISAMC_PP pp)
487 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
492 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
494 ISAMC_PP pp = xmalloc (sizeof(*pp));
497 pp->cat = isc_type(ipos);
498 pp->pos = isc_block(ipos);
500 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
506 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
513 isc_read_block (is, pp->cat, pp->pos, src);
514 memcpy (&pp->next, src, sizeof(pp->next));
515 src += sizeof(pp->next);
516 memcpy (&pp->size, src, sizeof(pp->size));
517 src += sizeof(pp->size);
518 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
519 src += sizeof(pp->numKeys);
520 assert (pp->next != pp->pos);
521 pp->offset = src - pp->buf;
522 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
523 if (is->method->debug > 2)
524 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
525 pp->size, pp->cat, pp->pos, pp->next);
530 /* returns non-zero if item could be read; 0 otherwise */
531 int isc_pp_read (ISAMC_PP pp, void *buf)
533 return isc_read_item (pp, (char **) &buf);
536 /* read one item from file - decode and store it in *dst.
539 1 if item could be read ok and NO boundary
540 2 if item could be read ok and boundary */
541 int isc_read_item (ISAMC_PP pp, char **dst)
544 char *src = pp->buf + pp->offset;
546 if (pp->offset >= pp->size)
551 return 0; /* end of file */
553 if (pp->next > pp->pos)
555 if (pp->next == pp->pos + 1)
556 is->files[pp->cat].no_next++;
559 is->files[pp->cat].no_forward++;
560 is->files[pp->cat].sum_forward += pp->next - pp->pos;
565 if (pp->next + 1 == pp->pos)
566 is->files[pp->cat].no_prev++;
569 is->files[pp->cat].no_backward++;
570 is->files[pp->cat].sum_backward += pp->pos - pp->next;
573 /* out new block position */
576 /* read block and save 'next' and 'size' entry */
577 isc_read_block (is, pp->cat, pp->pos, src);
578 memcpy (&pp->next, src, sizeof(pp->next));
579 src += sizeof(pp->next);
580 memcpy (&pp->size, src, sizeof(pp->size));
581 src += sizeof(pp->size);
582 /* assume block is non-empty */
583 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
584 assert (pp->next != pp->pos);
586 isc_release_block (is, pp->cat, pp->pos);
587 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
588 pp->offset = src - pp->buf;
589 if (is->method->debug > 2)
590 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
591 pp->size, pp->cat, pp->pos, pp->next);
594 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
595 pp->offset = src - pp->buf;
599 int isc_pp_num (ISAMC_PP pp)