2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-03-19 10:04:35 adam
10 * Revision 1.13 1998/03/18 09:23:55 adam
11 * Blocks are stored in chunks on free list - up to factor 2 in speed.
12 * Fixed bug that could occur in block category rearrangemen.
14 * Revision 1.12 1998/03/16 10:37:24 adam
15 * Added more statistics.
17 * Revision 1.11 1998/03/13 15:30:50 adam
18 * New functions isc_block_used and isc_block_size. Fixed 'leak'
21 * Revision 1.10 1998/03/11 11:18:18 adam
22 * Changed the isc_merge to take into account the mfill (minimum-fill).
24 * Revision 1.9 1998/03/06 13:54:02 adam
25 * Fixed two nasty bugs in isc_merge.
27 * Revision 1.8 1997/09/17 12:19:20 adam
28 * Zebra version corresponds to YAZ version 1.4.
29 * Changed Zebra server so that it doesn't depend on global common_resource.
31 * Revision 1.7 1997/02/12 20:42:43 adam
32 * Bug fix: during isc_merge operations, some pages weren't marked dirty
33 * even though they should be. At this point the merge operation marks
34 * a page dirty if the previous page changed at all. A better approach is
35 * to mark it dirty if the last key written changed in previous page.
37 * Revision 1.6 1996/11/08 11:15:29 adam
38 * Number of keys in chain are stored in first block and the function
39 * to retrieve this information, isc_pp_num is implemented.
41 * Revision 1.5 1996/11/04 14:08:57 adam
42 * Optimized free block usage.
44 * Revision 1.4 1996/11/01 13:36:46 adam
45 * New element, max_blocks_mem, that control how many blocks of max size
46 * to store in memory during isc_merge.
47 * Function isc_merge now ignores delete/update of identical keys and
48 * the proper blocks are then non-dirty and not written in flush_blocks.
50 * Revision 1.3 1996/11/01 08:59:14 adam
51 * First version of isc_merge that supports update/delete.
53 * Revision 1.2 1996/10/29 16:44:56 adam
56 * Revision 1.1 1996/10/29 13:40:48 adam
63 * Reduction to lower categories in isc_merge
73 static void flush_block (ISAMC is, int cat);
74 static void release_fc (ISAMC is, int cat);
75 static void init_fc (ISAMC is, int cat);
77 #define ISAMC_FREELIST_CHUNK 1
81 ISAMC_M isc_getmethod (void)
83 static struct ISAMC_filecat_s def_cat[] = {
91 { 2048, 1900, 1700, 10 },
92 { 8192, 8000, 7900, 10 },
93 { 32768, 32000, 31000, 0 },
96 ISAMC_M m = xmalloc (sizeof(*m));
103 m->compare_item = NULL;
107 m->max_blocks_mem = 10;
113 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
116 ISAMC_filecat filecat;
118 int max_buf_size = 0;
120 is = xmalloc (sizeof(*is));
122 is->method = xmalloc (sizeof(*is->method));
123 memcpy (is->method, method, sizeof(*method));
124 filecat = is->method->filecat;
127 /* determine number of block categories */
128 if (is->method->debug)
129 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
132 if (is->method->debug)
133 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
134 filecat[i].bsize, filecat[i].ifill,
135 filecat[i].mfill, filecat[i].mblocks);
136 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
137 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
138 } while (filecat[i++].mblocks);
141 /* max_buf_size is the larget buffer to be used during merge */
142 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
143 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
144 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
145 if (is->method->debug)
146 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
148 assert (is->no_files > 0);
149 is->files = xmalloc (sizeof(*is->files)*is->no_files);
152 is->merge_buf = xmalloc (max_buf_size+256);
153 memset (is->merge_buf, 0, max_buf_size+256);
156 is->merge_buf = NULL;
157 for (i = 0; i<is->no_files; i++)
161 sprintf (fname, "%s%c", name, i+'A');
162 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
164 is->files[i].head_is_dirty = 0;
165 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
168 is->files[i].head.lastblock = 1;
169 is->files[i].head.freelist = 0;
171 is->files[i].alloc_entries_num = 0;
172 is->files[i].alloc_entries_max =
173 is->method->filecat[i].bsize / sizeof(int) - 1;
174 is->files[i].alloc_buf = xmalloc (is->method->filecat[i].bsize);
175 is->files[i].no_writes = 0;
176 is->files[i].no_reads = 0;
177 is->files[i].no_skip_writes = 0;
178 is->files[i].no_allocated = 0;
179 is->files[i].no_released = 0;
180 is->files[i].no_remap = 0;
181 is->files[i].no_forward = 0;
182 is->files[i].no_backward = 0;
183 is->files[i].sum_forward = 0;
184 is->files[i].sum_backward = 0;
185 is->files[i].no_next = 0;
186 is->files[i].no_prev = 0;
193 int isc_block_used (ISAMC is, int type)
195 if (type < 0 || type >= is->no_files)
197 return is->files[type].head.lastblock-1;
200 int isc_block_size (ISAMC is, int type)
202 ISAMC_filecat filecat = is->method->filecat;
203 if (type < 0 || type >= is->no_files)
205 return filecat[type].bsize;
208 int isc_close (ISAMC is)
212 if (is->method->debug)
214 logf (LOG_LOG, "isc: next forw mid-f prev backw mid-b");
215 for (i = 0; i<is->no_files; i++)
216 logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
217 is->files[i].no_next,
218 is->files[i].no_forward,
219 is->files[i].no_forward ?
220 (double) is->files[i].sum_forward/is->files[i].no_forward
222 is->files[i].no_prev,
223 is->files[i].no_backward,
224 is->files[i].no_backward ?
225 (double) is->files[i].sum_backward/is->files[i].no_backward
228 if (is->method->debug)
229 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
230 for (i = 0; i<is->no_files; i++)
233 assert (is->files[i].bf);
234 if (is->files[i].head_is_dirty)
235 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
237 if (is->method->debug)
238 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
239 is->files[i].no_writes,
240 is->files[i].no_reads,
241 is->files[i].no_skip_writes,
242 is->files[i].no_allocated,
243 is->files[i].no_released,
244 is->files[i].no_remap);
245 xfree (is->files[i].fc_list);
247 bf_close (is->files[i].bf);
250 xfree (is->merge_buf);
255 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
257 ++(is->files[cat].no_reads);
258 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
261 int isc_write_block (ISAMC is, int cat, int pos, char *src)
263 ++(is->files[cat].no_writes);
264 if (is->method->debug > 2)
265 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
266 return bf_write (is->files[cat].bf, pos, 0, 0, src);
269 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
270 int nextpos, int offset)
272 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
273 if (is->method->debug > 2)
274 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
275 (int) size, nextpos);
276 src -= ISAMC_BLOCK_OFFSET_N;
277 memcpy (src, &nextpos, sizeof(int));
278 memcpy (src + sizeof(int), &size, sizeof(size));
279 return isc_write_block (is, cat, pos, src);
282 #if ISAMC_FREELIST_CHUNK
283 static void flush_block (ISAMC is, int cat)
285 char *abuf = is->files[cat].alloc_buf;
286 int block = is->files[cat].head.freelist;
287 if (block && is->files[cat].alloc_entries_num)
289 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
290 bf_write (is->files[cat].bf, block, 0, 0, abuf);
291 is->files[cat].alloc_entries_num = 0;
296 static int alloc_block (ISAMC is, int cat)
298 int block = is->files[cat].head.freelist;
299 char *abuf = is->files[cat].alloc_buf;
301 (is->files[cat].no_allocated)++;
305 block = (is->files[cat].head.lastblock)++; /* no free list */
306 is->files[cat].head_is_dirty = 1;
310 if (!is->files[cat].alloc_entries_num) /* read first time */
312 bf_read (is->files[cat].bf, block, 0, 0, abuf);
313 memcpy (&is->files[cat].alloc_entries_num, abuf,
314 sizeof(is->files[cat].alloc_entries_num));
315 assert (is->files[cat].alloc_entries_num > 0);
317 /* have some free blocks now */
318 assert (is->files[cat].alloc_entries_num > 0);
319 is->files[cat].alloc_entries_num--;
320 if (!is->files[cat].alloc_entries_num) /* last one in block? */
322 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
324 is->files[cat].head_is_dirty = 1;
326 if (is->files[cat].head.freelist)
328 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
330 memcpy (&is->files[cat].alloc_entries_num, abuf,
331 sizeof(is->files[cat].alloc_entries_num));
332 assert (is->files[cat].alloc_entries_num);
336 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
337 is->files[cat].alloc_entries_num, sizeof(int));
342 static void release_block (ISAMC is, int cat, int pos)
344 char *abuf = is->files[cat].alloc_buf;
345 int block = is->files[cat].head.freelist;
347 (is->files[cat].no_released)++;
349 if (block && !is->files[cat].alloc_entries_num) /* must read block */
351 bf_read (is->files[cat].bf, block, 0, 0, abuf);
352 memcpy (&is->files[cat].alloc_entries_num, abuf,
353 sizeof(is->files[cat].alloc_entries_num));
354 assert (is->files[cat].alloc_entries_num > 0);
356 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
357 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
360 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
361 bf_write (is->files[cat].bf, block, 0, 0, abuf);
362 is->files[cat].alloc_entries_num = 0;
364 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
366 memcpy (abuf + sizeof(int), &block, sizeof(int));
367 is->files[cat].head.freelist = pos;
368 is->files[cat].head_is_dirty = 1;
372 memcpy (abuf + sizeof(int) +
373 is->files[cat].alloc_entries_num*sizeof(int),
376 is->files[cat].alloc_entries_num++;
379 static void flush_block (ISAMC is, int cat)
381 char *abuf = is->files[cat].alloc_buf;
385 static int alloc_block (ISAMC is, int cat)
388 char buf[sizeof(int)];
390 is->files[cat].head_is_dirty = 1;
391 (is->files[cat].no_allocated)++;
392 if ((block = is->files[cat].head.freelist))
394 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
395 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
398 block = (is->files[cat].head.lastblock)++;
402 static void release_block (ISAMC is, int cat, int pos)
404 char buf[sizeof(int)];
406 (is->files[cat].no_released)++;
407 is->files[cat].head_is_dirty = 1;
408 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
409 is->files[cat].head.freelist = pos;
410 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
414 int isc_alloc_block (ISAMC is, int cat)
418 if (is->files[cat].fc_list)
421 for (j = 0; j < is->files[cat].fc_max; j++)
422 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
424 is->files[cat].fc_list[j] = 0;
430 block = alloc_block (is, cat);
431 if (is->method->debug > 3)
432 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
436 void isc_release_block (ISAMC is, int cat, int pos)
438 if (is->method->debug > 3)
439 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
440 if (is->files[cat].fc_list)
443 for (j = 0; j<is->files[cat].fc_max; j++)
444 if (!is->files[cat].fc_list[j])
446 is->files[cat].fc_list[j] = pos;
450 release_block (is, cat, pos);
453 static void init_fc (ISAMC is, int cat)
457 is->files[cat].fc_max = j;
458 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
460 is->files[cat].fc_list[j] = 0;
463 static void release_fc (ISAMC is, int cat)
465 int b, j = is->files[cat].fc_max;
468 if ((b = is->files[cat].fc_list[j]))
470 release_block (is, cat, b);
471 is->files[cat].fc_list[j] = 0;
475 void isc_pp_close (ISAMC_PP pp)
479 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
484 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
486 ISAMC_PP pp = xmalloc (sizeof(*pp));
489 pp->cat = isc_type(ipos);
490 pp->pos = isc_block(ipos);
492 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
498 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
505 isc_read_block (is, pp->cat, pp->pos, src);
506 memcpy (&pp->next, src, sizeof(pp->next));
507 src += sizeof(pp->next);
508 memcpy (&pp->size, src, sizeof(pp->size));
509 src += sizeof(pp->size);
510 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
511 src += sizeof(pp->numKeys);
512 assert (pp->next != pp->pos);
513 pp->offset = src - pp->buf;
514 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
515 if (is->method->debug > 2)
516 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
517 pp->size, pp->cat, pp->pos, pp->next);
522 /* returns non-zero if item could be read; 0 otherwise */
523 int isc_pp_read (ISAMC_PP pp, void *buf)
525 return isc_read_item (pp, (char **) &buf);
528 /* read one item from file - decode and store it in *dst.
531 1 if item could be read ok and NO boundary
532 2 if item could be read ok and boundary */
533 int isc_read_item (ISAMC_PP pp, char **dst)
536 char *src = pp->buf + pp->offset;
538 if (pp->offset >= pp->size)
543 return 0; /* end of file */
545 if (pp->next > pp->pos)
547 if (pp->next == pp->pos + 1)
548 is->files[pp->cat].no_next++;
551 is->files[pp->cat].no_forward++;
552 is->files[pp->cat].sum_forward += pp->next - pp->pos;
557 if (pp->next + 1 == pp->pos)
558 is->files[pp->cat].no_prev++;
561 is->files[pp->cat].no_backward++;
562 is->files[pp->cat].sum_backward += pp->pos - pp->next;
565 /* out new block position */
568 /* read block and save 'next' and 'size' entry */
569 isc_read_block (is, pp->cat, pp->pos, src);
570 memcpy (&pp->next, src, sizeof(pp->next));
571 src += sizeof(pp->next);
572 memcpy (&pp->size, src, sizeof(pp->size));
573 src += sizeof(pp->size);
574 /* assume block is non-empty */
575 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
576 assert (pp->next != pp->pos);
578 isc_release_block (is, pp->cat, pp->pos);
579 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
580 pp->offset = src - pp->buf;
581 if (is->method->debug > 2)
582 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
583 pp->size, pp->cat, pp->pos, pp->next);
586 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
587 pp->offset = src - pp->buf;
591 int isc_pp_num (ISAMC_PP pp)