1 /* $Id: isamc.c,v 1.34 2007-01-15 15:10:18 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 * Reduction to lower categories in isamc_merge
33 #include <yaz/xmalloc.h>
36 static void flush_block (ISAMC is, int cat);
37 static void release_fc (ISAMC is, int cat);
38 static void init_fc (ISAMC is, int cat);
40 #define ISAMC_FREELIST_CHUNK 0
44 void isamc_getmethod (ISAMC_M *m)
47 static struct ISAMC_filecat_s def_cat[] = {
53 { 128, 120, 100, 10 },
54 { 512, 490, 350, 10 },
55 { 2048, 1900, 1700, 10 },
56 { 8192, 8000, 7900, 10 },
57 { 32768, 32000, 31000, 0 },
62 m->codec.start = NULL;
63 m->codec.decode = NULL;
64 m->codec.encode = NULL;
66 m->codec.reset = NULL;
68 m->compare_item = NULL;
73 m->max_blocks_mem = 10;
76 ISAMC isamc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
79 ISAMC_filecat filecat;
83 is = (ISAMC) xmalloc (sizeof(*is));
85 is->method = (ISAMC_M *) xmalloc (sizeof(*is->method));
86 memcpy (is->method, method, sizeof(*method));
87 filecat = is->method->filecat;
90 /* determine number of block categories */
91 if (is->method->debug)
92 yaz_log (YLOG_LOG, "isc: bsize ifill mfill mblocks");
95 if (is->method->debug)
96 yaz_log (YLOG_LOG, "isc:%6d %6d %6d %6d",
97 filecat[i].bsize, filecat[i].ifill,
98 filecat[i].mfill, filecat[i].mblocks);
99 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
100 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
101 } while (filecat[i++].mblocks);
104 /* max_buf_size is the larget buffer to be used during merge */
105 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
106 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
107 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
108 if (is->method->debug)
109 yaz_log (YLOG_LOG, "isc: max_buf_size %d", max_buf_size);
111 assert (is->no_files > 0);
112 is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
115 is->merge_buf = (char *) xmalloc (max_buf_size+256);
116 memset (is->merge_buf, 0, max_buf_size+256);
119 is->merge_buf = NULL;
120 for (i = 0; i<is->no_files; i++)
123 is->files[i].head_is_dirty = 0;
124 is->files[i].head.lastblock = 1;
125 is->files[i].head.freelist = 0;
126 is->files[i].alloc_entries_num = 0;
127 is->files[i].alloc_entries_max =
128 is->method->filecat[i].bsize / sizeof(zint) - 1;
129 is->files[i].alloc_buf = (char *)
130 xmalloc (is->method->filecat[i].bsize);
131 is->files[i].no_writes = 0;
132 is->files[i].no_reads = 0;
133 is->files[i].no_skip_writes = 0;
134 is->files[i].no_allocated = 0;
135 is->files[i].no_released = 0;
136 is->files[i].no_remap = 0;
137 is->files[i].no_forward = 0;
138 is->files[i].no_backward = 0;
139 is->files[i].sum_forward = 0;
140 is->files[i].sum_backward = 0;
141 is->files[i].no_next = 0;
142 is->files[i].no_prev = 0;
147 for (i = 0; i<is->no_files; i++)
149 char fname[FILENAME_MAX];
152 sprintf (fname, "%s%c", name, i+'A');
153 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
155 if (!is->files[i].bf)
160 r = bf_read(is->files[i].bf, 0, 0, sizeof(ISAMC_head),
171 zint isamc_block_used (ISAMC is, int type)
173 if (type < 0 || type >= is->no_files)
175 return is->files[type].head.lastblock-1;
178 int isamc_block_size (ISAMC is, int type)
180 ISAMC_filecat filecat = is->method->filecat;
181 if (type < 0 || type >= is->no_files)
183 return filecat[type].bsize;
186 int isamc_close (ISAMC is)
190 if (is->method->debug)
192 yaz_log (YLOG_LOG, "isc: next forw mid-f prev backw mid-b");
193 for (i = 0; i<is->no_files; i++)
194 yaz_log (YLOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
195 is->files[i].no_next,
196 is->files[i].no_forward,
197 is->files[i].no_forward ?
198 (double) is->files[i].sum_forward/is->files[i].no_forward
200 is->files[i].no_prev,
201 is->files[i].no_backward,
202 is->files[i].no_backward ?
203 (double) is->files[i].sum_backward/is->files[i].no_backward
206 if (is->method->debug)
207 yaz_log (YLOG_LOG, "isc: writes reads skipped alloc released remap");
208 for (i = 0; i<is->no_files; i++)
211 if (is->method->debug)
212 yaz_log (YLOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
213 is->files[i].no_writes,
214 is->files[i].no_reads,
215 is->files[i].no_skip_writes,
216 is->files[i].no_allocated,
217 is->files[i].no_released,
218 is->files[i].no_remap);
221 if (is->files[i].head_is_dirty)
222 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
225 bf_close (is->files[i].bf);
227 xfree(is->files[i].fc_list);
228 xfree(is->files[i].alloc_buf);
231 xfree (is->merge_buf);
237 int isamc_read_block (ISAMC is, int cat, zint pos, char *dst)
239 ++(is->files[cat].no_reads);
240 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
243 int isamc_write_block (ISAMC is, int cat, zint pos, char *src)
245 ++(is->files[cat].no_writes);
246 if (is->method->debug > 2)
247 yaz_log (YLOG_LOG, "isc: write_block %d " ZINT_FORMAT, cat, pos);
248 return bf_write (is->files[cat].bf, pos, 0, 0, src);
251 int isamc_write_dblock (ISAMC is, int cat, zint pos, char *src,
252 zint nextpos, int offset)
254 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
255 if (is->method->debug > 2)
256 yaz_log (YLOG_LOG, "isc: write_dblock. size=%d nextpos=" ZINT_FORMAT,
257 (int) size, nextpos);
258 src -= ISAMC_BLOCK_OFFSET_N;
259 memcpy (src, &nextpos, sizeof(nextpos));
260 memcpy (src + sizeof(nextpos), &size, sizeof(size));
261 return isamc_write_block (is, cat, pos, src);
264 #if ISAMC_FREELIST_CHUNK
265 static void flush_block (ISAMC is, int cat)
267 char *abuf = is->files[cat].alloc_buf;
268 zint block = is->files[cat].head.freelist;
269 if (block && is->files[cat].alloc_entries_num)
271 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(block));
272 bf_write (is->files[cat].bf, block, 0, 0, abuf);
273 is->files[cat].alloc_entries_num = 0;
277 static zint alloc_block (ISAMC is, int cat)
279 zint block = is->files[cat].head.freelist;
280 char *abuf = is->files[cat].alloc_buf;
282 (is->files[cat].no_allocated)++;
286 block = (is->files[cat].head.lastblock)++; /* no free list */
287 is->files[cat].head_is_dirty = 1;
291 if (!is->files[cat].alloc_entries_num) /* read first time */
293 bf_read (is->files[cat].bf, block, 0, 0, abuf);
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 > 0);
298 /* have some free blocks now */
299 assert (is->files[cat].alloc_entries_num > 0);
300 is->files[cat].alloc_entries_num--;
301 if (!is->files[cat].alloc_entries_num) /* last one in block? */
303 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
305 is->files[cat].head_is_dirty = 1;
307 if (is->files[cat].head.freelist)
309 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
311 memcpy (&is->files[cat].alloc_entries_num, abuf,
312 sizeof(is->files[cat].alloc_entries_num));
313 assert (is->files[cat].alloc_entries_num);
317 memcpy (&block, abuf + sizeof(zint) + sizeof(int) *
318 is->files[cat].alloc_entries_num, sizeof(zint));
323 static void release_block (ISAMC is, int cat, zint pos)
325 char *abuf = is->files[cat].alloc_buf;
326 zint block = is->files[cat].head.freelist;
328 (is->files[cat].no_released)++;
330 if (block && !is->files[cat].alloc_entries_num) /* must read block */
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 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
338 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
341 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
342 bf_write (is->files[cat].bf, block, 0, 0, abuf);
343 is->files[cat].alloc_entries_num = 0;
345 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
347 memcpy (abuf + sizeof(int), &block, sizeof(zint));
348 is->files[cat].head.freelist = pos;
349 is->files[cat].head_is_dirty = 1;
353 memcpy (abuf + sizeof(int) +
354 is->files[cat].alloc_entries_num*sizeof(zint),
357 is->files[cat].alloc_entries_num++;
360 static void flush_block (ISAMC is, int cat)
364 static zint alloc_block (ISAMC is, int cat)
367 char buf[sizeof(zint)];
369 is->files[cat].head_is_dirty = 1;
370 (is->files[cat].no_allocated)++;
371 if ((block = is->files[cat].head.freelist))
373 bf_read (is->files[cat].bf, block, 0, sizeof(zint), buf);
374 memcpy (&is->files[cat].head.freelist, buf, sizeof(zint));
377 block = (is->files[cat].head.lastblock)++;
381 static void release_block (ISAMC is, int cat, zint pos)
383 char buf[sizeof(zint)];
385 (is->files[cat].no_released)++;
386 is->files[cat].head_is_dirty = 1;
387 memcpy (buf, &is->files[cat].head.freelist, sizeof(zint));
388 is->files[cat].head.freelist = pos;
389 bf_write (is->files[cat].bf, pos, 0, sizeof(zint), buf);
393 zint isamc_alloc_block (ISAMC is, int cat)
397 if (is->files[cat].fc_list)
401 for (j = 0; j < is->files[cat].fc_max; j++)
402 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
404 is->files[cat].fc_list[j] = 0;
410 block = alloc_block (is, cat);
411 if (is->method->debug > 3)
412 yaz_log (YLOG_LOG, "isc: alloc_block in cat %d: " ZINT_FORMAT, cat, block);
416 void isamc_release_block (ISAMC is, int cat, zint pos)
418 if (is->method->debug > 3)
419 yaz_log (YLOG_LOG, "isc: release_block in cat %d:" ZINT_FORMAT, cat, pos);
420 if (is->files[cat].fc_list)
423 for (j = 0; j<is->files[cat].fc_max; j++)
424 if (!is->files[cat].fc_list[j])
426 is->files[cat].fc_list[j] = pos;
430 release_block (is, cat, pos);
433 static void init_fc (ISAMC is, int cat)
437 is->files[cat].fc_max = j;
438 is->files[cat].fc_list = (zint *)
439 xmalloc (sizeof(*is->files[0].fc_list) * j);
441 is->files[cat].fc_list[j] = 0;
444 static void release_fc (ISAMC is, int cat)
446 int j = is->files[cat].fc_max;
450 if ((b = is->files[cat].fc_list[j]))
452 release_block (is, cat, b);
453 is->files[cat].fc_list[j] = 0;
457 void isamc_pp_close (ISAMC_PP pp)
461 (*is->method->codec.stop)(pp->decodeClientData);
466 ISAMC_PP isamc_pp_open (ISAMC is, ISAM_P ipos)
468 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
471 pp->cat = (int) isamc_type(ipos);
472 pp->pos = isamc_block(ipos);
474 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
480 pp->decodeClientData = (*is->method->codec.start)();
487 isamc_read_block (is, pp->cat, pp->pos, src);
488 memcpy (&pp->next, src, sizeof(pp->next));
489 src += sizeof(pp->next);
490 memcpy (&pp->size, src, sizeof(pp->size));
491 src += sizeof(pp->size);
492 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
493 src += sizeof(pp->numKeys);
494 if (pp->next == pp->pos)
496 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
497 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
498 assert (pp->next != pp->pos);
500 pp->offset = src - pp->buf;
501 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
502 if (is->method->debug > 2)
503 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
504 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
509 /* returns non-zero if item could be read; 0 otherwise */
510 int isamc_pp_read (ISAMC_PP pp, void *buf)
513 return isamc_read_item (pp, &cp);
516 /* read one item from file - decode and store it in *dst.
519 1 if item could be read ok and NO boundary
520 2 if item could be read ok and boundary */
521 int isamc_read_item (ISAMC_PP pp, char **dst)
524 const char *src = pp->buf + pp->offset;
526 if (pp->offset >= pp->size)
531 return 0; /* end of file */
533 if (pp->next > pp->pos)
535 if (pp->next == pp->pos + 1)
536 is->files[pp->cat].no_next++;
539 is->files[pp->cat].no_forward++;
540 is->files[pp->cat].sum_forward += pp->next - pp->pos;
545 if (pp->next + 1 == pp->pos)
546 is->files[pp->cat].no_prev++;
549 is->files[pp->cat].no_backward++;
550 is->files[pp->cat].sum_backward += pp->pos - pp->next;
553 /* out new block position */
556 /* read block and save 'next' and 'size' entry */
557 isamc_read_block (is, pp->cat, pp->pos, pp->buf);
558 memcpy (&pp->next, src, sizeof(pp->next));
559 src += sizeof(pp->next);
560 memcpy (&pp->size, src, sizeof(pp->size));
561 src += sizeof(pp->size);
562 /* assume block is non-empty */
563 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
565 if (pp->next == pp->pos)
567 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
568 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
569 assert (pp->next != pp->pos);
573 isamc_release_block (is, pp->cat, pp->pos);
574 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
575 pp->offset = src - pp->buf;
576 if (is->method->debug > 2)
577 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
578 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
581 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
582 pp->offset = src - pp->buf;
586 zint isamc_pp_num (ISAMC_PP pp)
594 * indent-tabs-mode: nil
596 * vim: shiftwidth=4 tabstop=8 expandtab