2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1999-06-30 09:08:23 adam
8 * Added coder to reset.
10 * Revision 1.17 1999/05/26 07:49:14 adam
13 * Revision 1.16 1998/05/27 14:32:03 adam
14 * Changed default block category layout.
16 * Revision 1.15 1998/05/20 10:12:25 adam
17 * Implemented automatic EXPLAIN database maintenance.
18 * Modified Zebra to work with ASN.1 compiled version of YAZ.
20 * Revision 1.14 1998/03/19 10:04:35 adam
23 * Revision 1.13 1998/03/18 09:23:55 adam
24 * Blocks are stored in chunks on free list - up to factor 2 in speed.
25 * Fixed bug that could occur in block category rearrangemen.
27 * Revision 1.12 1998/03/16 10:37:24 adam
28 * Added more statistics.
30 * Revision 1.11 1998/03/13 15:30:50 adam
31 * New functions isc_block_used and isc_block_size. Fixed 'leak'
34 * Revision 1.10 1998/03/11 11:18:18 adam
35 * Changed the isc_merge to take into account the mfill (minimum-fill).
37 * Revision 1.9 1998/03/06 13:54:02 adam
38 * Fixed two nasty bugs in isc_merge.
40 * Revision 1.8 1997/09/17 12:19:20 adam
41 * Zebra version corresponds to YAZ version 1.4.
42 * Changed Zebra server so that it doesn't depend on global common_resource.
44 * Revision 1.7 1997/02/12 20:42:43 adam
45 * Bug fix: during isc_merge operations, some pages weren't marked dirty
46 * even though they should be. At this point the merge operation marks
47 * a page dirty if the previous page changed at all. A better approach is
48 * to mark it dirty if the last key written changed in previous page.
50 * Revision 1.6 1996/11/08 11:15:29 adam
51 * Number of keys in chain are stored in first block and the function
52 * to retrieve this information, isc_pp_num is implemented.
54 * Revision 1.5 1996/11/04 14:08:57 adam
55 * Optimized free block usage.
57 * Revision 1.4 1996/11/01 13:36:46 adam
58 * New element, max_blocks_mem, that control how many blocks of max size
59 * to store in memory during isc_merge.
60 * Function isc_merge now ignores delete/update of identical keys and
61 * the proper blocks are then non-dirty and not written in flush_blocks.
63 * Revision 1.3 1996/11/01 08:59:14 adam
64 * First version of isc_merge that supports update/delete.
66 * Revision 1.2 1996/10/29 16:44:56 adam
69 * Revision 1.1 1996/10/29 13:40:48 adam
76 * Reduction to lower categories in isc_merge
86 static void flush_block (ISAMC is, int cat);
87 static void release_fc (ISAMC is, int cat);
88 static void init_fc (ISAMC is, int cat);
90 #define ISAMC_FREELIST_CHUNK 1
94 ISAMC_M isc_getmethod (void)
96 static struct ISAMC_filecat_s def_cat[] = {
102 { 128, 120, 100, 10 },
103 { 512, 490, 350, 10 },
104 { 2048, 1900, 1700, 10 },
105 { 8192, 8000, 7900, 10 },
106 { 32768, 32000, 31000, 0 },
109 ISAMC_M m = (ISAMC_M) xmalloc (sizeof(*m));
110 m->filecat = def_cat;
112 m->code_start = NULL;
115 m->code_reset = NULL;
117 m->compare_item = NULL;
121 m->max_blocks_mem = 10;
127 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
130 ISAMC_filecat filecat;
132 int max_buf_size = 0;
134 is = (ISAMC) xmalloc (sizeof(*is));
136 is->method = (ISAMC_M) xmalloc (sizeof(*is->method));
137 memcpy (is->method, method, sizeof(*method));
138 filecat = is->method->filecat;
141 /* determine number of block categories */
142 if (is->method->debug)
143 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
146 if (is->method->debug)
147 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
148 filecat[i].bsize, filecat[i].ifill,
149 filecat[i].mfill, filecat[i].mblocks);
150 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
151 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
152 } while (filecat[i++].mblocks);
155 /* max_buf_size is the larget buffer to be used during merge */
156 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
157 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
158 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
159 if (is->method->debug)
160 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
162 assert (is->no_files > 0);
163 is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
166 is->merge_buf = (char *) xmalloc (max_buf_size+256);
167 memset (is->merge_buf, 0, max_buf_size+256);
170 is->merge_buf = NULL;
171 for (i = 0; i<is->no_files; i++)
175 sprintf (fname, "%s%c", name, i+'A');
176 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
178 is->files[i].head_is_dirty = 0;
179 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
182 is->files[i].head.lastblock = 1;
183 is->files[i].head.freelist = 0;
185 is->files[i].alloc_entries_num = 0;
186 is->files[i].alloc_entries_max =
187 is->method->filecat[i].bsize / sizeof(int) - 1;
188 is->files[i].alloc_buf = (char *)
189 xmalloc (is->method->filecat[i].bsize);
190 is->files[i].no_writes = 0;
191 is->files[i].no_reads = 0;
192 is->files[i].no_skip_writes = 0;
193 is->files[i].no_allocated = 0;
194 is->files[i].no_released = 0;
195 is->files[i].no_remap = 0;
196 is->files[i].no_forward = 0;
197 is->files[i].no_backward = 0;
198 is->files[i].sum_forward = 0;
199 is->files[i].sum_backward = 0;
200 is->files[i].no_next = 0;
201 is->files[i].no_prev = 0;
208 int isc_block_used (ISAMC is, int type)
210 if (type < 0 || type >= is->no_files)
212 return is->files[type].head.lastblock-1;
215 int isc_block_size (ISAMC is, int type)
217 ISAMC_filecat filecat = is->method->filecat;
218 if (type < 0 || type >= is->no_files)
220 return filecat[type].bsize;
223 int isc_close (ISAMC is)
227 if (is->method->debug)
229 logf (LOG_LOG, "isc: next forw mid-f prev backw mid-b");
230 for (i = 0; i<is->no_files; i++)
231 logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
232 is->files[i].no_next,
233 is->files[i].no_forward,
234 is->files[i].no_forward ?
235 (double) is->files[i].sum_forward/is->files[i].no_forward
237 is->files[i].no_prev,
238 is->files[i].no_backward,
239 is->files[i].no_backward ?
240 (double) is->files[i].sum_backward/is->files[i].no_backward
243 if (is->method->debug)
244 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
245 for (i = 0; i<is->no_files; i++)
248 assert (is->files[i].bf);
249 if (is->files[i].head_is_dirty)
250 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
252 if (is->method->debug)
253 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
254 is->files[i].no_writes,
255 is->files[i].no_reads,
256 is->files[i].no_skip_writes,
257 is->files[i].no_allocated,
258 is->files[i].no_released,
259 is->files[i].no_remap);
260 xfree (is->files[i].fc_list);
262 bf_close (is->files[i].bf);
265 xfree (is->merge_buf);
271 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
273 ++(is->files[cat].no_reads);
274 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
277 int isc_write_block (ISAMC is, int cat, int pos, char *src)
279 ++(is->files[cat].no_writes);
280 if (is->method->debug > 2)
281 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
282 return bf_write (is->files[cat].bf, pos, 0, 0, src);
285 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
286 int nextpos, int offset)
288 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
289 if (is->method->debug > 2)
290 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
291 (int) size, nextpos);
292 src -= ISAMC_BLOCK_OFFSET_N;
293 memcpy (src, &nextpos, sizeof(int));
294 memcpy (src + sizeof(int), &size, sizeof(size));
295 return isc_write_block (is, cat, pos, src);
298 #if ISAMC_FREELIST_CHUNK
299 static void flush_block (ISAMC is, int cat)
301 char *abuf = is->files[cat].alloc_buf;
302 int block = is->files[cat].head.freelist;
303 if (block && is->files[cat].alloc_entries_num)
305 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
306 bf_write (is->files[cat].bf, block, 0, 0, abuf);
307 is->files[cat].alloc_entries_num = 0;
312 static int alloc_block (ISAMC is, int cat)
314 int block = is->files[cat].head.freelist;
315 char *abuf = is->files[cat].alloc_buf;
317 (is->files[cat].no_allocated)++;
321 block = (is->files[cat].head.lastblock)++; /* no free list */
322 is->files[cat].head_is_dirty = 1;
326 if (!is->files[cat].alloc_entries_num) /* read first time */
328 bf_read (is->files[cat].bf, block, 0, 0, abuf);
329 memcpy (&is->files[cat].alloc_entries_num, abuf,
330 sizeof(is->files[cat].alloc_entries_num));
331 assert (is->files[cat].alloc_entries_num > 0);
333 /* have some free blocks now */
334 assert (is->files[cat].alloc_entries_num > 0);
335 is->files[cat].alloc_entries_num--;
336 if (!is->files[cat].alloc_entries_num) /* last one in block? */
338 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
340 is->files[cat].head_is_dirty = 1;
342 if (is->files[cat].head.freelist)
344 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
346 memcpy (&is->files[cat].alloc_entries_num, abuf,
347 sizeof(is->files[cat].alloc_entries_num));
348 assert (is->files[cat].alloc_entries_num);
352 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
353 is->files[cat].alloc_entries_num, sizeof(int));
358 static void release_block (ISAMC is, int cat, int pos)
360 char *abuf = is->files[cat].alloc_buf;
361 int block = is->files[cat].head.freelist;
363 (is->files[cat].no_released)++;
365 if (block && !is->files[cat].alloc_entries_num) /* must read block */
367 bf_read (is->files[cat].bf, block, 0, 0, abuf);
368 memcpy (&is->files[cat].alloc_entries_num, abuf,
369 sizeof(is->files[cat].alloc_entries_num));
370 assert (is->files[cat].alloc_entries_num > 0);
372 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
373 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
376 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
377 bf_write (is->files[cat].bf, block, 0, 0, abuf);
378 is->files[cat].alloc_entries_num = 0;
380 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
382 memcpy (abuf + sizeof(int), &block, sizeof(int));
383 is->files[cat].head.freelist = pos;
384 is->files[cat].head_is_dirty = 1;
388 memcpy (abuf + sizeof(int) +
389 is->files[cat].alloc_entries_num*sizeof(int),
392 is->files[cat].alloc_entries_num++;
395 static void flush_block (ISAMC is, int cat)
397 char *abuf = is->files[cat].alloc_buf;
401 static int alloc_block (ISAMC is, int cat)
404 char buf[sizeof(int)];
406 is->files[cat].head_is_dirty = 1;
407 (is->files[cat].no_allocated)++;
408 if ((block = is->files[cat].head.freelist))
410 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
411 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
414 block = (is->files[cat].head.lastblock)++;
418 static void release_block (ISAMC is, int cat, int pos)
420 char buf[sizeof(int)];
422 (is->files[cat].no_released)++;
423 is->files[cat].head_is_dirty = 1;
424 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
425 is->files[cat].head.freelist = pos;
426 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
430 int isc_alloc_block (ISAMC is, int cat)
434 if (is->files[cat].fc_list)
437 for (j = 0; j < is->files[cat].fc_max; j++)
438 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
440 is->files[cat].fc_list[j] = 0;
446 block = alloc_block (is, cat);
447 if (is->method->debug > 3)
448 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
452 void isc_release_block (ISAMC is, int cat, int pos)
454 if (is->method->debug > 3)
455 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
456 if (is->files[cat].fc_list)
459 for (j = 0; j<is->files[cat].fc_max; j++)
460 if (!is->files[cat].fc_list[j])
462 is->files[cat].fc_list[j] = pos;
466 release_block (is, cat, pos);
469 static void init_fc (ISAMC is, int cat)
473 is->files[cat].fc_max = j;
474 is->files[cat].fc_list = (int *)
475 xmalloc (sizeof(*is->files[0].fc_list) * j);
477 is->files[cat].fc_list[j] = 0;
480 static void release_fc (ISAMC is, int cat)
482 int b, j = is->files[cat].fc_max;
485 if ((b = is->files[cat].fc_list[j]))
487 release_block (is, cat, b);
488 is->files[cat].fc_list[j] = 0;
492 void isc_pp_close (ISAMC_PP pp)
496 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
501 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
503 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
506 pp->cat = isc_type(ipos);
507 pp->pos = isc_block(ipos);
509 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
515 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
522 isc_read_block (is, pp->cat, pp->pos, src);
523 memcpy (&pp->next, src, sizeof(pp->next));
524 src += sizeof(pp->next);
525 memcpy (&pp->size, src, sizeof(pp->size));
526 src += sizeof(pp->size);
527 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
528 src += sizeof(pp->numKeys);
529 assert (pp->next != pp->pos);
530 pp->offset = src - pp->buf;
531 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
532 if (is->method->debug > 2)
533 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
534 pp->size, pp->cat, pp->pos, pp->next);
539 /* returns non-zero if item could be read; 0 otherwise */
540 int isc_pp_read (ISAMC_PP pp, void *buf)
542 return isc_read_item (pp, (char **) &buf);
545 /* read one item from file - decode and store it in *dst.
548 1 if item could be read ok and NO boundary
549 2 if item could be read ok and boundary */
550 int isc_read_item (ISAMC_PP pp, char **dst)
553 char *src = pp->buf + pp->offset;
555 if (pp->offset >= pp->size)
560 return 0; /* end of file */
562 if (pp->next > pp->pos)
564 if (pp->next == pp->pos + 1)
565 is->files[pp->cat].no_next++;
568 is->files[pp->cat].no_forward++;
569 is->files[pp->cat].sum_forward += pp->next - pp->pos;
574 if (pp->next + 1 == pp->pos)
575 is->files[pp->cat].no_prev++;
578 is->files[pp->cat].no_backward++;
579 is->files[pp->cat].sum_backward += pp->pos - pp->next;
582 /* out new block position */
585 /* read block and save 'next' and 'size' entry */
586 isc_read_block (is, pp->cat, pp->pos, src);
587 memcpy (&pp->next, src, sizeof(pp->next));
588 src += sizeof(pp->next);
589 memcpy (&pp->size, src, sizeof(pp->size));
590 src += sizeof(pp->size);
591 /* assume block is non-empty */
592 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
593 assert (pp->next != pp->pos);
595 isc_release_block (is, pp->cat, pp->pos);
596 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
597 pp->offset = src - pp->buf;
598 if (is->method->debug > 2)
599 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
600 pp->size, pp->cat, pp->pos, pp->next);
603 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
604 pp->offset = src - pp->buf;
608 int isc_pp_num (ISAMC_PP pp)