1 /* $Id: isamc.c,v 1.27 2004-11-19 10:27:09 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 * Reduction to lower categories in isc_merge
35 static void flush_block (ISAMC is, int cat);
36 static void release_fc (ISAMC is, int cat);
37 static void init_fc (ISAMC is, int cat);
39 #define ISAMC_FREELIST_CHUNK 0
43 void isc_getmethod (ISAMC_M *m)
46 static struct ISAMC_filecat_s def_cat[] = {
52 { 128, 120, 100, 10 },
53 { 512, 490, 350, 10 },
54 { 2048, 1900, 1700, 10 },
55 { 8192, 8000, 7900, 10 },
56 { 32768, 32000, 31000, 0 },
61 m->codec.start = NULL;
62 m->codec.decode = NULL;
63 m->codec.encode = NULL;
65 m->codec.reset = NULL;
67 m->compare_item = NULL;
72 m->max_blocks_mem = 10;
75 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
78 ISAMC_filecat filecat;
82 is = (ISAMC) xmalloc (sizeof(*is));
84 is->method = (ISAMC_M *) xmalloc (sizeof(*is->method));
85 memcpy (is->method, method, sizeof(*method));
86 filecat = is->method->filecat;
89 /* determine number of block categories */
90 if (is->method->debug)
91 yaz_log (YLOG_LOG, "isc: bsize ifill mfill mblocks");
94 if (is->method->debug)
95 yaz_log (YLOG_LOG, "isc:%6d %6d %6d %6d",
96 filecat[i].bsize, filecat[i].ifill,
97 filecat[i].mfill, filecat[i].mblocks);
98 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
99 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
100 } while (filecat[i++].mblocks);
103 /* max_buf_size is the larget buffer to be used during merge */
104 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
105 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
106 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
107 if (is->method->debug)
108 yaz_log (YLOG_LOG, "isc: max_buf_size %d", max_buf_size);
110 assert (is->no_files > 0);
111 is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
114 is->merge_buf = (char *) xmalloc (max_buf_size+256);
115 memset (is->merge_buf, 0, max_buf_size+256);
118 is->merge_buf = NULL;
119 for (i = 0; i<is->no_files; i++)
123 sprintf (fname, "%s%c", name, i+'A');
124 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
126 is->files[i].head_is_dirty = 0;
127 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
130 is->files[i].head.lastblock = 1;
131 is->files[i].head.freelist = 0;
133 is->files[i].alloc_entries_num = 0;
134 is->files[i].alloc_entries_max =
135 is->method->filecat[i].bsize / sizeof(zint) - 1;
136 is->files[i].alloc_buf = (char *)
137 xmalloc (is->method->filecat[i].bsize);
138 is->files[i].no_writes = 0;
139 is->files[i].no_reads = 0;
140 is->files[i].no_skip_writes = 0;
141 is->files[i].no_allocated = 0;
142 is->files[i].no_released = 0;
143 is->files[i].no_remap = 0;
144 is->files[i].no_forward = 0;
145 is->files[i].no_backward = 0;
146 is->files[i].sum_forward = 0;
147 is->files[i].sum_backward = 0;
148 is->files[i].no_next = 0;
149 is->files[i].no_prev = 0;
156 zint isc_block_used (ISAMC is, int type)
158 if (type < 0 || type >= is->no_files)
160 return is->files[type].head.lastblock-1;
163 int isc_block_size (ISAMC is, int type)
165 ISAMC_filecat filecat = is->method->filecat;
166 if (type < 0 || type >= is->no_files)
168 return filecat[type].bsize;
171 int isc_close (ISAMC is)
175 if (is->method->debug)
177 yaz_log (YLOG_LOG, "isc: next forw mid-f prev backw mid-b");
178 for (i = 0; i<is->no_files; i++)
179 yaz_log (YLOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
180 is->files[i].no_next,
181 is->files[i].no_forward,
182 is->files[i].no_forward ?
183 (double) is->files[i].sum_forward/is->files[i].no_forward
185 is->files[i].no_prev,
186 is->files[i].no_backward,
187 is->files[i].no_backward ?
188 (double) is->files[i].sum_backward/is->files[i].no_backward
191 if (is->method->debug)
192 yaz_log (YLOG_LOG, "isc: writes reads skipped alloc released remap");
193 for (i = 0; i<is->no_files; i++)
196 assert (is->files[i].bf);
197 if (is->files[i].head_is_dirty)
198 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
200 if (is->method->debug)
201 yaz_log (YLOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
202 is->files[i].no_writes,
203 is->files[i].no_reads,
204 is->files[i].no_skip_writes,
205 is->files[i].no_allocated,
206 is->files[i].no_released,
207 is->files[i].no_remap);
208 xfree (is->files[i].fc_list);
210 bf_close (is->files[i].bf);
213 xfree (is->merge_buf);
219 int isc_read_block (ISAMC is, int cat, zint pos, char *dst)
221 ++(is->files[cat].no_reads);
222 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
225 int isc_write_block (ISAMC is, int cat, zint pos, char *src)
227 ++(is->files[cat].no_writes);
228 if (is->method->debug > 2)
229 yaz_log (YLOG_LOG, "isc: write_block %d " ZINT_FORMAT, cat, pos);
230 return bf_write (is->files[cat].bf, pos, 0, 0, src);
233 int isc_write_dblock (ISAMC is, int cat, zint pos, char *src,
234 zint nextpos, int offset)
236 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
237 if (is->method->debug > 2)
238 yaz_log (YLOG_LOG, "isc: write_dblock. size=%d nextpos=" ZINT_FORMAT,
239 (int) size, nextpos);
240 src -= ISAMC_BLOCK_OFFSET_N;
241 memcpy (src, &nextpos, sizeof(nextpos));
242 memcpy (src + sizeof(nextpos), &size, sizeof(size));
243 return isc_write_block (is, cat, pos, src);
246 #if ISAMC_FREELIST_CHUNK
247 static void flush_block (ISAMC is, int cat)
249 char *abuf = is->files[cat].alloc_buf;
250 zint block = is->files[cat].head.freelist;
251 if (block && is->files[cat].alloc_entries_num)
253 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(block));
254 bf_write (is->files[cat].bf, block, 0, 0, abuf);
255 is->files[cat].alloc_entries_num = 0;
260 static zint alloc_block (ISAMC is, int cat)
262 zint block = is->files[cat].head.freelist;
263 char *abuf = is->files[cat].alloc_buf;
265 (is->files[cat].no_allocated)++;
269 block = (is->files[cat].head.lastblock)++; /* no free list */
270 is->files[cat].head_is_dirty = 1;
274 if (!is->files[cat].alloc_entries_num) /* read first time */
276 bf_read (is->files[cat].bf, block, 0, 0, abuf);
277 memcpy (&is->files[cat].alloc_entries_num, abuf,
278 sizeof(is->files[cat].alloc_entries_num));
279 assert (is->files[cat].alloc_entries_num > 0);
281 /* have some free blocks now */
282 assert (is->files[cat].alloc_entries_num > 0);
283 is->files[cat].alloc_entries_num--;
284 if (!is->files[cat].alloc_entries_num) /* last one in block? */
286 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
288 is->files[cat].head_is_dirty = 1;
290 if (is->files[cat].head.freelist)
292 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
294 memcpy (&is->files[cat].alloc_entries_num, abuf,
295 sizeof(is->files[cat].alloc_entries_num));
296 assert (is->files[cat].alloc_entries_num);
300 memcpy (&block, abuf + sizeof(zint) + sizeof(int) *
301 is->files[cat].alloc_entries_num, sizeof(zint));
306 static void release_block (ISAMC is, int cat, zint pos)
308 char *abuf = is->files[cat].alloc_buf;
309 zint block = is->files[cat].head.freelist;
311 (is->files[cat].no_released)++;
313 if (block && !is->files[cat].alloc_entries_num) /* must read block */
315 bf_read (is->files[cat].bf, block, 0, 0, abuf);
316 memcpy (&is->files[cat].alloc_entries_num, abuf,
317 sizeof(is->files[cat].alloc_entries_num));
318 assert (is->files[cat].alloc_entries_num > 0);
320 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
321 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
324 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
325 bf_write (is->files[cat].bf, block, 0, 0, abuf);
326 is->files[cat].alloc_entries_num = 0;
328 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
330 memcpy (abuf + sizeof(int), &block, sizeof(zint));
331 is->files[cat].head.freelist = pos;
332 is->files[cat].head_is_dirty = 1;
336 memcpy (abuf + sizeof(int) +
337 is->files[cat].alloc_entries_num*sizeof(zint),
340 is->files[cat].alloc_entries_num++;
343 static void flush_block (ISAMC is, int cat)
345 char *abuf = is->files[cat].alloc_buf;
349 static zint alloc_block (ISAMC is, int cat)
352 char buf[sizeof(zint)];
354 is->files[cat].head_is_dirty = 1;
355 (is->files[cat].no_allocated)++;
356 if ((block = is->files[cat].head.freelist))
358 bf_read (is->files[cat].bf, block, 0, sizeof(zint), buf);
359 memcpy (&is->files[cat].head.freelist, buf, sizeof(zint));
362 block = (is->files[cat].head.lastblock)++;
366 static void release_block (ISAMC is, int cat, zint pos)
368 char buf[sizeof(zint)];
370 (is->files[cat].no_released)++;
371 is->files[cat].head_is_dirty = 1;
372 memcpy (buf, &is->files[cat].head.freelist, sizeof(zint));
373 is->files[cat].head.freelist = pos;
374 bf_write (is->files[cat].bf, pos, 0, sizeof(zint), buf);
378 zint isc_alloc_block (ISAMC is, int cat)
382 if (is->files[cat].fc_list)
386 for (j = 0; j < is->files[cat].fc_max; j++)
387 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
389 is->files[cat].fc_list[j] = 0;
395 block = alloc_block (is, cat);
396 if (is->method->debug > 3)
397 yaz_log (YLOG_LOG, "isc: alloc_block in cat %d: " ZINT_FORMAT, cat, block);
401 void isc_release_block (ISAMC is, int cat, zint pos)
403 if (is->method->debug > 3)
404 yaz_log (YLOG_LOG, "isc: release_block in cat %d:" ZINT_FORMAT, cat, pos);
405 if (is->files[cat].fc_list)
408 for (j = 0; j<is->files[cat].fc_max; j++)
409 if (!is->files[cat].fc_list[j])
411 is->files[cat].fc_list[j] = pos;
415 release_block (is, cat, pos);
418 static void init_fc (ISAMC is, int cat)
422 is->files[cat].fc_max = j;
423 is->files[cat].fc_list = (zint *)
424 xmalloc (sizeof(*is->files[0].fc_list) * j);
426 is->files[cat].fc_list[j] = 0;
429 static void release_fc (ISAMC is, int cat)
431 int j = is->files[cat].fc_max;
435 if ((b = is->files[cat].fc_list[j]))
437 release_block (is, cat, b);
438 is->files[cat].fc_list[j] = 0;
442 void isc_pp_close (ISAMC_PP pp)
446 (*is->method->codec.stop)(pp->decodeClientData);
451 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
453 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
456 pp->cat = (int) isc_type(ipos);
457 pp->pos = isc_block(ipos);
459 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
465 pp->decodeClientData = (*is->method->codec.start)();
472 isc_read_block (is, pp->cat, pp->pos, src);
473 memcpy (&pp->next, src, sizeof(pp->next));
474 src += sizeof(pp->next);
475 memcpy (&pp->size, src, sizeof(pp->size));
476 src += sizeof(pp->size);
477 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
478 src += sizeof(pp->numKeys);
479 if (pp->next == pp->pos)
481 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
482 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
483 assert (pp->next != pp->pos);
485 pp->offset = src - pp->buf;
486 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
487 if (is->method->debug > 2)
488 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
489 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
494 /* returns non-zero if item could be read; 0 otherwise */
495 int isc_pp_read (ISAMC_PP pp, void *buf)
498 return isc_read_item (pp, &cp);
501 /* read one item from file - decode and store it in *dst.
504 1 if item could be read ok and NO boundary
505 2 if item could be read ok and boundary */
506 int isc_read_item (ISAMC_PP pp, char **dst)
509 const char *src = pp->buf + pp->offset;
511 if (pp->offset >= pp->size)
516 return 0; /* end of file */
518 if (pp->next > pp->pos)
520 if (pp->next == pp->pos + 1)
521 is->files[pp->cat].no_next++;
524 is->files[pp->cat].no_forward++;
525 is->files[pp->cat].sum_forward += pp->next - pp->pos;
530 if (pp->next + 1 == pp->pos)
531 is->files[pp->cat].no_prev++;
534 is->files[pp->cat].no_backward++;
535 is->files[pp->cat].sum_backward += pp->pos - pp->next;
538 /* out new block position */
541 /* read block and save 'next' and 'size' entry */
542 isc_read_block (is, pp->cat, pp->pos, pp->buf);
543 memcpy (&pp->next, src, sizeof(pp->next));
544 src += sizeof(pp->next);
545 memcpy (&pp->size, src, sizeof(pp->size));
546 src += sizeof(pp->size);
547 /* assume block is non-empty */
548 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
550 if (pp->next == pp->pos)
552 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
553 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
554 assert (pp->next != pp->pos);
558 isc_release_block (is, pp->cat, pp->pos);
559 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
560 pp->offset = src - pp->buf;
561 if (is->method->debug > 2)
562 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
563 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
566 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
567 pp->offset = src - pp->buf;
571 zint isc_pp_num (ISAMC_PP pp)