2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1999-11-30 13:48:04 adam
8 * Improved installation. Updated for inclusion of YAZ header files.
10 * Revision 1.19 1999/07/14 10:59:27 adam
11 * Changed functions isc_getmethod, isams_getmethod.
12 * Improved fatal error handling (such as missing EXPLAIN schema).
14 * Revision 1.18 1999/06/30 09:08:23 adam
15 * Added coder to reset.
17 * Revision 1.17 1999/05/26 07:49:14 adam
20 * Revision 1.16 1998/05/27 14:32:03 adam
21 * Changed default block category layout.
23 * Revision 1.15 1998/05/20 10:12:25 adam
24 * Implemented automatic EXPLAIN database maintenance.
25 * Modified Zebra to work with ASN.1 compiled version of YAZ.
27 * Revision 1.14 1998/03/19 10:04:35 adam
30 * Revision 1.13 1998/03/18 09:23:55 adam
31 * Blocks are stored in chunks on free list - up to factor 2 in speed.
32 * Fixed bug that could occur in block category rearrangemen.
34 * Revision 1.12 1998/03/16 10:37:24 adam
35 * Added more statistics.
37 * Revision 1.11 1998/03/13 15:30:50 adam
38 * New functions isc_block_used and isc_block_size. Fixed 'leak'
41 * Revision 1.10 1998/03/11 11:18:18 adam
42 * Changed the isc_merge to take into account the mfill (minimum-fill).
44 * Revision 1.9 1998/03/06 13:54:02 adam
45 * Fixed two nasty bugs in isc_merge.
47 * Revision 1.8 1997/09/17 12:19:20 adam
48 * Zebra version corresponds to YAZ version 1.4.
49 * Changed Zebra server so that it doesn't depend on global common_resource.
51 * Revision 1.7 1997/02/12 20:42:43 adam
52 * Bug fix: during isc_merge operations, some pages weren't marked dirty
53 * even though they should be. At this point the merge operation marks
54 * a page dirty if the previous page changed at all. A better approach is
55 * to mark it dirty if the last key written changed in previous page.
57 * Revision 1.6 1996/11/08 11:15:29 adam
58 * Number of keys in chain are stored in first block and the function
59 * to retrieve this information, isc_pp_num is implemented.
61 * Revision 1.5 1996/11/04 14:08:57 adam
62 * Optimized free block usage.
64 * Revision 1.4 1996/11/01 13:36:46 adam
65 * New element, max_blocks_mem, that control how many blocks of max size
66 * to store in memory during isc_merge.
67 * Function isc_merge now ignores delete/update of identical keys and
68 * the proper blocks are then non-dirty and not written in flush_blocks.
70 * Revision 1.3 1996/11/01 08:59:14 adam
71 * First version of isc_merge that supports update/delete.
73 * Revision 1.2 1996/10/29 16:44:56 adam
76 * Revision 1.1 1996/10/29 13:40:48 adam
83 * Reduction to lower categories in isc_merge
93 static void flush_block (ISAMC is, int cat);
94 static void release_fc (ISAMC is, int cat);
95 static void init_fc (ISAMC is, int cat);
97 #define ISAMC_FREELIST_CHUNK 1
101 void isc_getmethod (ISAMC_M m)
104 static struct ISAMC_filecat_s def_cat[] = {
110 { 128, 120, 100, 10 },
111 { 512, 490, 350, 10 },
112 { 2048, 1900, 1700, 10 },
113 { 8192, 8000, 7900, 10 },
114 { 32768, 32000, 31000, 0 },
117 m->filecat = def_cat;
119 m->code_start = NULL;
122 m->code_reset = NULL;
124 m->compare_item = NULL;
128 m->max_blocks_mem = 10;
131 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
134 ISAMC_filecat filecat;
136 int max_buf_size = 0;
138 is = (ISAMC) xmalloc (sizeof(*is));
140 is->method = (ISAMC_M) xmalloc (sizeof(*is->method));
141 memcpy (is->method, method, sizeof(*method));
142 filecat = is->method->filecat;
145 /* determine number of block categories */
146 if (is->method->debug)
147 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
150 if (is->method->debug)
151 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
152 filecat[i].bsize, filecat[i].ifill,
153 filecat[i].mfill, filecat[i].mblocks);
154 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
155 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
156 } while (filecat[i++].mblocks);
159 /* max_buf_size is the larget buffer to be used during merge */
160 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
161 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
162 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
163 if (is->method->debug)
164 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
166 assert (is->no_files > 0);
167 is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
170 is->merge_buf = (char *) xmalloc (max_buf_size+256);
171 memset (is->merge_buf, 0, max_buf_size+256);
174 is->merge_buf = NULL;
175 for (i = 0; i<is->no_files; i++)
179 sprintf (fname, "%s%c", name, i+'A');
180 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
182 is->files[i].head_is_dirty = 0;
183 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
186 is->files[i].head.lastblock = 1;
187 is->files[i].head.freelist = 0;
189 is->files[i].alloc_entries_num = 0;
190 is->files[i].alloc_entries_max =
191 is->method->filecat[i].bsize / sizeof(int) - 1;
192 is->files[i].alloc_buf = (char *)
193 xmalloc (is->method->filecat[i].bsize);
194 is->files[i].no_writes = 0;
195 is->files[i].no_reads = 0;
196 is->files[i].no_skip_writes = 0;
197 is->files[i].no_allocated = 0;
198 is->files[i].no_released = 0;
199 is->files[i].no_remap = 0;
200 is->files[i].no_forward = 0;
201 is->files[i].no_backward = 0;
202 is->files[i].sum_forward = 0;
203 is->files[i].sum_backward = 0;
204 is->files[i].no_next = 0;
205 is->files[i].no_prev = 0;
212 int isc_block_used (ISAMC is, int type)
214 if (type < 0 || type >= is->no_files)
216 return is->files[type].head.lastblock-1;
219 int isc_block_size (ISAMC is, int type)
221 ISAMC_filecat filecat = is->method->filecat;
222 if (type < 0 || type >= is->no_files)
224 return filecat[type].bsize;
227 int isc_close (ISAMC is)
231 if (is->method->debug)
233 logf (LOG_LOG, "isc: next forw mid-f prev backw mid-b");
234 for (i = 0; i<is->no_files; i++)
235 logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
236 is->files[i].no_next,
237 is->files[i].no_forward,
238 is->files[i].no_forward ?
239 (double) is->files[i].sum_forward/is->files[i].no_forward
241 is->files[i].no_prev,
242 is->files[i].no_backward,
243 is->files[i].no_backward ?
244 (double) is->files[i].sum_backward/is->files[i].no_backward
247 if (is->method->debug)
248 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
249 for (i = 0; i<is->no_files; i++)
252 assert (is->files[i].bf);
253 if (is->files[i].head_is_dirty)
254 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
256 if (is->method->debug)
257 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
258 is->files[i].no_writes,
259 is->files[i].no_reads,
260 is->files[i].no_skip_writes,
261 is->files[i].no_allocated,
262 is->files[i].no_released,
263 is->files[i].no_remap);
264 xfree (is->files[i].fc_list);
266 bf_close (is->files[i].bf);
269 xfree (is->merge_buf);
275 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
277 ++(is->files[cat].no_reads);
278 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
281 int isc_write_block (ISAMC is, int cat, int pos, char *src)
283 ++(is->files[cat].no_writes);
284 if (is->method->debug > 2)
285 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
286 return bf_write (is->files[cat].bf, pos, 0, 0, src);
289 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
290 int nextpos, int offset)
292 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
293 if (is->method->debug > 2)
294 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
295 (int) size, nextpos);
296 src -= ISAMC_BLOCK_OFFSET_N;
297 memcpy (src, &nextpos, sizeof(int));
298 memcpy (src + sizeof(int), &size, sizeof(size));
299 return isc_write_block (is, cat, pos, src);
302 #if ISAMC_FREELIST_CHUNK
303 static void flush_block (ISAMC is, int cat)
305 char *abuf = is->files[cat].alloc_buf;
306 int block = is->files[cat].head.freelist;
307 if (block && is->files[cat].alloc_entries_num)
309 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
310 bf_write (is->files[cat].bf, block, 0, 0, abuf);
311 is->files[cat].alloc_entries_num = 0;
316 static int alloc_block (ISAMC is, int cat)
318 int block = is->files[cat].head.freelist;
319 char *abuf = is->files[cat].alloc_buf;
321 (is->files[cat].no_allocated)++;
325 block = (is->files[cat].head.lastblock)++; /* no free list */
326 is->files[cat].head_is_dirty = 1;
330 if (!is->files[cat].alloc_entries_num) /* read first time */
332 bf_read (is->files[cat].bf, block, 0, 0, abuf);
333 memcpy (&is->files[cat].alloc_entries_num, abuf,
334 sizeof(is->files[cat].alloc_entries_num));
335 assert (is->files[cat].alloc_entries_num > 0);
337 /* have some free blocks now */
338 assert (is->files[cat].alloc_entries_num > 0);
339 is->files[cat].alloc_entries_num--;
340 if (!is->files[cat].alloc_entries_num) /* last one in block? */
342 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
344 is->files[cat].head_is_dirty = 1;
346 if (is->files[cat].head.freelist)
348 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
350 memcpy (&is->files[cat].alloc_entries_num, abuf,
351 sizeof(is->files[cat].alloc_entries_num));
352 assert (is->files[cat].alloc_entries_num);
356 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
357 is->files[cat].alloc_entries_num, sizeof(int));
362 static void release_block (ISAMC is, int cat, int pos)
364 char *abuf = is->files[cat].alloc_buf;
365 int block = is->files[cat].head.freelist;
367 (is->files[cat].no_released)++;
369 if (block && !is->files[cat].alloc_entries_num) /* must read block */
371 bf_read (is->files[cat].bf, block, 0, 0, abuf);
372 memcpy (&is->files[cat].alloc_entries_num, abuf,
373 sizeof(is->files[cat].alloc_entries_num));
374 assert (is->files[cat].alloc_entries_num > 0);
376 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
377 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
380 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
381 bf_write (is->files[cat].bf, block, 0, 0, abuf);
382 is->files[cat].alloc_entries_num = 0;
384 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
386 memcpy (abuf + sizeof(int), &block, sizeof(int));
387 is->files[cat].head.freelist = pos;
388 is->files[cat].head_is_dirty = 1;
392 memcpy (abuf + sizeof(int) +
393 is->files[cat].alloc_entries_num*sizeof(int),
396 is->files[cat].alloc_entries_num++;
399 static void flush_block (ISAMC is, int cat)
401 char *abuf = is->files[cat].alloc_buf;
405 static int alloc_block (ISAMC is, int cat)
408 char buf[sizeof(int)];
410 is->files[cat].head_is_dirty = 1;
411 (is->files[cat].no_allocated)++;
412 if ((block = is->files[cat].head.freelist))
414 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
415 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
418 block = (is->files[cat].head.lastblock)++;
422 static void release_block (ISAMC is, int cat, int pos)
424 char buf[sizeof(int)];
426 (is->files[cat].no_released)++;
427 is->files[cat].head_is_dirty = 1;
428 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
429 is->files[cat].head.freelist = pos;
430 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
434 int isc_alloc_block (ISAMC is, int cat)
438 if (is->files[cat].fc_list)
441 for (j = 0; j < is->files[cat].fc_max; j++)
442 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
444 is->files[cat].fc_list[j] = 0;
450 block = alloc_block (is, cat);
451 if (is->method->debug > 3)
452 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
456 void isc_release_block (ISAMC is, int cat, int pos)
458 if (is->method->debug > 3)
459 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
460 if (is->files[cat].fc_list)
463 for (j = 0; j<is->files[cat].fc_max; j++)
464 if (!is->files[cat].fc_list[j])
466 is->files[cat].fc_list[j] = pos;
470 release_block (is, cat, pos);
473 static void init_fc (ISAMC is, int cat)
477 is->files[cat].fc_max = j;
478 is->files[cat].fc_list = (int *)
479 xmalloc (sizeof(*is->files[0].fc_list) * j);
481 is->files[cat].fc_list[j] = 0;
484 static void release_fc (ISAMC is, int cat)
486 int b, j = is->files[cat].fc_max;
489 if ((b = is->files[cat].fc_list[j]))
491 release_block (is, cat, b);
492 is->files[cat].fc_list[j] = 0;
496 void isc_pp_close (ISAMC_PP pp)
500 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
505 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
507 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
510 pp->cat = isc_type(ipos);
511 pp->pos = isc_block(ipos);
513 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
519 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
526 isc_read_block (is, pp->cat, pp->pos, src);
527 memcpy (&pp->next, src, sizeof(pp->next));
528 src += sizeof(pp->next);
529 memcpy (&pp->size, src, sizeof(pp->size));
530 src += sizeof(pp->size);
531 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
532 src += sizeof(pp->numKeys);
533 assert (pp->next != pp->pos);
534 pp->offset = src - pp->buf;
535 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
536 if (is->method->debug > 2)
537 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
538 pp->size, pp->cat, pp->pos, pp->next);
543 /* returns non-zero if item could be read; 0 otherwise */
544 int isc_pp_read (ISAMC_PP pp, void *buf)
546 return isc_read_item (pp, (char **) &buf);
549 /* read one item from file - decode and store it in *dst.
552 1 if item could be read ok and NO boundary
553 2 if item could be read ok and boundary */
554 int isc_read_item (ISAMC_PP pp, char **dst)
557 char *src = pp->buf + pp->offset;
559 if (pp->offset >= pp->size)
564 return 0; /* end of file */
566 if (pp->next > pp->pos)
568 if (pp->next == pp->pos + 1)
569 is->files[pp->cat].no_next++;
572 is->files[pp->cat].no_forward++;
573 is->files[pp->cat].sum_forward += pp->next - pp->pos;
578 if (pp->next + 1 == pp->pos)
579 is->files[pp->cat].no_prev++;
582 is->files[pp->cat].no_backward++;
583 is->files[pp->cat].sum_backward += pp->pos - pp->next;
586 /* out new block position */
589 /* read block and save 'next' and 'size' entry */
590 isc_read_block (is, pp->cat, pp->pos, src);
591 memcpy (&pp->next, src, sizeof(pp->next));
592 src += sizeof(pp->next);
593 memcpy (&pp->size, src, sizeof(pp->size));
594 src += sizeof(pp->size);
595 /* assume block is non-empty */
596 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
597 assert (pp->next != pp->pos);
599 isc_release_block (is, pp->cat, pp->pos);
600 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
601 pp->offset = src - pp->buf;
602 if (is->method->debug > 2)
603 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
604 pp->size, pp->cat, pp->pos, pp->next);
607 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
608 pp->offset = src - pp->buf;
612 int isc_pp_num (ISAMC_PP pp)