1 /* $Id: isamc.c,v 1.31 2006-05-10 08:13:27 adam Exp $
2 Copyright (C) 1995-2005
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 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++)
124 sprintf (fname, "%s%c", name, i+'A');
125 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
127 is->files[i].head_is_dirty = 0;
128 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
131 is->files[i].head.lastblock = 1;
132 is->files[i].head.freelist = 0;
134 is->files[i].alloc_entries_num = 0;
135 is->files[i].alloc_entries_max =
136 is->method->filecat[i].bsize / sizeof(zint) - 1;
137 is->files[i].alloc_buf = (char *)
138 xmalloc (is->method->filecat[i].bsize);
139 is->files[i].no_writes = 0;
140 is->files[i].no_reads = 0;
141 is->files[i].no_skip_writes = 0;
142 is->files[i].no_allocated = 0;
143 is->files[i].no_released = 0;
144 is->files[i].no_remap = 0;
145 is->files[i].no_forward = 0;
146 is->files[i].no_backward = 0;
147 is->files[i].sum_forward = 0;
148 is->files[i].sum_backward = 0;
149 is->files[i].no_next = 0;
150 is->files[i].no_prev = 0;
157 zint isamc_block_used (ISAMC is, int type)
159 if (type < 0 || type >= is->no_files)
161 return is->files[type].head.lastblock-1;
164 int isamc_block_size (ISAMC is, int type)
166 ISAMC_filecat filecat = is->method->filecat;
167 if (type < 0 || type >= is->no_files)
169 return filecat[type].bsize;
172 int isamc_close (ISAMC is)
176 if (is->method->debug)
178 yaz_log (YLOG_LOG, "isc: next forw mid-f prev backw mid-b");
179 for (i = 0; i<is->no_files; i++)
180 yaz_log (YLOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
181 is->files[i].no_next,
182 is->files[i].no_forward,
183 is->files[i].no_forward ?
184 (double) is->files[i].sum_forward/is->files[i].no_forward
186 is->files[i].no_prev,
187 is->files[i].no_backward,
188 is->files[i].no_backward ?
189 (double) is->files[i].sum_backward/is->files[i].no_backward
192 if (is->method->debug)
193 yaz_log (YLOG_LOG, "isc: writes reads skipped alloc released remap");
194 for (i = 0; i<is->no_files; i++)
197 assert (is->files[i].bf);
198 if (is->files[i].head_is_dirty)
199 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
201 if (is->method->debug)
202 yaz_log (YLOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
203 is->files[i].no_writes,
204 is->files[i].no_reads,
205 is->files[i].no_skip_writes,
206 is->files[i].no_allocated,
207 is->files[i].no_released,
208 is->files[i].no_remap);
209 xfree (is->files[i].fc_list);
211 bf_close (is->files[i].bf);
214 xfree (is->merge_buf);
220 int isamc_read_block (ISAMC is, int cat, zint pos, char *dst)
222 ++(is->files[cat].no_reads);
223 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
226 int isamc_write_block (ISAMC is, int cat, zint pos, char *src)
228 ++(is->files[cat].no_writes);
229 if (is->method->debug > 2)
230 yaz_log (YLOG_LOG, "isc: write_block %d " ZINT_FORMAT, cat, pos);
231 return bf_write (is->files[cat].bf, pos, 0, 0, src);
234 int isamc_write_dblock (ISAMC is, int cat, zint pos, char *src,
235 zint nextpos, int offset)
237 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
238 if (is->method->debug > 2)
239 yaz_log (YLOG_LOG, "isc: write_dblock. size=%d nextpos=" ZINT_FORMAT,
240 (int) size, nextpos);
241 src -= ISAMC_BLOCK_OFFSET_N;
242 memcpy (src, &nextpos, sizeof(nextpos));
243 memcpy (src + sizeof(nextpos), &size, sizeof(size));
244 return isamc_write_block (is, cat, pos, src);
247 #if ISAMC_FREELIST_CHUNK
248 static void flush_block (ISAMC is, int cat)
250 char *abuf = is->files[cat].alloc_buf;
251 zint block = is->files[cat].head.freelist;
252 if (block && is->files[cat].alloc_entries_num)
254 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(block));
255 bf_write (is->files[cat].bf, block, 0, 0, abuf);
256 is->files[cat].alloc_entries_num = 0;
261 static zint alloc_block (ISAMC is, int cat)
263 zint block = is->files[cat].head.freelist;
264 char *abuf = is->files[cat].alloc_buf;
266 (is->files[cat].no_allocated)++;
270 block = (is->files[cat].head.lastblock)++; /* no free list */
271 is->files[cat].head_is_dirty = 1;
275 if (!is->files[cat].alloc_entries_num) /* read first time */
277 bf_read (is->files[cat].bf, block, 0, 0, abuf);
278 memcpy (&is->files[cat].alloc_entries_num, abuf,
279 sizeof(is->files[cat].alloc_entries_num));
280 assert (is->files[cat].alloc_entries_num > 0);
282 /* have some free blocks now */
283 assert (is->files[cat].alloc_entries_num > 0);
284 is->files[cat].alloc_entries_num--;
285 if (!is->files[cat].alloc_entries_num) /* last one in block? */
287 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
289 is->files[cat].head_is_dirty = 1;
291 if (is->files[cat].head.freelist)
293 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
295 memcpy (&is->files[cat].alloc_entries_num, abuf,
296 sizeof(is->files[cat].alloc_entries_num));
297 assert (is->files[cat].alloc_entries_num);
301 memcpy (&block, abuf + sizeof(zint) + sizeof(int) *
302 is->files[cat].alloc_entries_num, sizeof(zint));
307 static void release_block (ISAMC is, int cat, zint pos)
309 char *abuf = is->files[cat].alloc_buf;
310 zint block = is->files[cat].head.freelist;
312 (is->files[cat].no_released)++;
314 if (block && !is->files[cat].alloc_entries_num) /* must read block */
316 bf_read (is->files[cat].bf, block, 0, 0, abuf);
317 memcpy (&is->files[cat].alloc_entries_num, abuf,
318 sizeof(is->files[cat].alloc_entries_num));
319 assert (is->files[cat].alloc_entries_num > 0);
321 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
322 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
325 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
326 bf_write (is->files[cat].bf, block, 0, 0, abuf);
327 is->files[cat].alloc_entries_num = 0;
329 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
331 memcpy (abuf + sizeof(int), &block, sizeof(zint));
332 is->files[cat].head.freelist = pos;
333 is->files[cat].head_is_dirty = 1;
337 memcpy (abuf + sizeof(int) +
338 is->files[cat].alloc_entries_num*sizeof(zint),
341 is->files[cat].alloc_entries_num++;
344 static void flush_block (ISAMC is, int cat)
346 char *abuf = is->files[cat].alloc_buf;
350 static zint alloc_block (ISAMC is, int cat)
353 char buf[sizeof(zint)];
355 is->files[cat].head_is_dirty = 1;
356 (is->files[cat].no_allocated)++;
357 if ((block = is->files[cat].head.freelist))
359 bf_read (is->files[cat].bf, block, 0, sizeof(zint), buf);
360 memcpy (&is->files[cat].head.freelist, buf, sizeof(zint));
363 block = (is->files[cat].head.lastblock)++;
367 static void release_block (ISAMC is, int cat, zint pos)
369 char buf[sizeof(zint)];
371 (is->files[cat].no_released)++;
372 is->files[cat].head_is_dirty = 1;
373 memcpy (buf, &is->files[cat].head.freelist, sizeof(zint));
374 is->files[cat].head.freelist = pos;
375 bf_write (is->files[cat].bf, pos, 0, sizeof(zint), buf);
379 zint isamc_alloc_block (ISAMC is, int cat)
383 if (is->files[cat].fc_list)
387 for (j = 0; j < is->files[cat].fc_max; j++)
388 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
390 is->files[cat].fc_list[j] = 0;
396 block = alloc_block (is, cat);
397 if (is->method->debug > 3)
398 yaz_log (YLOG_LOG, "isc: alloc_block in cat %d: " ZINT_FORMAT, cat, block);
402 void isamc_release_block (ISAMC is, int cat, zint pos)
404 if (is->method->debug > 3)
405 yaz_log (YLOG_LOG, "isc: release_block in cat %d:" ZINT_FORMAT, cat, pos);
406 if (is->files[cat].fc_list)
409 for (j = 0; j<is->files[cat].fc_max; j++)
410 if (!is->files[cat].fc_list[j])
412 is->files[cat].fc_list[j] = pos;
416 release_block (is, cat, pos);
419 static void init_fc (ISAMC is, int cat)
423 is->files[cat].fc_max = j;
424 is->files[cat].fc_list = (zint *)
425 xmalloc (sizeof(*is->files[0].fc_list) * j);
427 is->files[cat].fc_list[j] = 0;
430 static void release_fc (ISAMC is, int cat)
432 int j = is->files[cat].fc_max;
436 if ((b = is->files[cat].fc_list[j]))
438 release_block (is, cat, b);
439 is->files[cat].fc_list[j] = 0;
443 void isamc_pp_close (ISAMC_PP pp)
447 (*is->method->codec.stop)(pp->decodeClientData);
452 ISAMC_PP isamc_pp_open (ISAMC is, ISAM_P ipos)
454 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
457 pp->cat = (int) isamc_type(ipos);
458 pp->pos = isamc_block(ipos);
460 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
466 pp->decodeClientData = (*is->method->codec.start)();
473 isamc_read_block (is, pp->cat, pp->pos, src);
474 memcpy (&pp->next, src, sizeof(pp->next));
475 src += sizeof(pp->next);
476 memcpy (&pp->size, src, sizeof(pp->size));
477 src += sizeof(pp->size);
478 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
479 src += sizeof(pp->numKeys);
480 if (pp->next == pp->pos)
482 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
483 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
484 assert (pp->next != pp->pos);
486 pp->offset = src - pp->buf;
487 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
488 if (is->method->debug > 2)
489 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
490 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
495 /* returns non-zero if item could be read; 0 otherwise */
496 int isamc_pp_read (ISAMC_PP pp, void *buf)
499 return isamc_read_item (pp, &cp);
502 /* read one item from file - decode and store it in *dst.
505 1 if item could be read ok and NO boundary
506 2 if item could be read ok and boundary */
507 int isamc_read_item (ISAMC_PP pp, char **dst)
510 const char *src = pp->buf + pp->offset;
512 if (pp->offset >= pp->size)
517 return 0; /* end of file */
519 if (pp->next > pp->pos)
521 if (pp->next == pp->pos + 1)
522 is->files[pp->cat].no_next++;
525 is->files[pp->cat].no_forward++;
526 is->files[pp->cat].sum_forward += pp->next - pp->pos;
531 if (pp->next + 1 == pp->pos)
532 is->files[pp->cat].no_prev++;
535 is->files[pp->cat].no_backward++;
536 is->files[pp->cat].sum_backward += pp->pos - pp->next;
539 /* out new block position */
542 /* read block and save 'next' and 'size' entry */
543 isamc_read_block (is, pp->cat, pp->pos, pp->buf);
544 memcpy (&pp->next, src, sizeof(pp->next));
545 src += sizeof(pp->next);
546 memcpy (&pp->size, src, sizeof(pp->size));
547 src += sizeof(pp->size);
548 /* assume block is non-empty */
549 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
551 if (pp->next == pp->pos)
553 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
554 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
555 assert (pp->next != pp->pos);
559 isamc_release_block (is, pp->cat, pp->pos);
560 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
561 pp->offset = src - pp->buf;
562 if (is->method->debug > 2)
563 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
564 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
567 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
568 pp->offset = src - pp->buf;
572 zint isamc_pp_num (ISAMC_PP pp)
580 * indent-tabs-mode: nil
582 * vim: shiftwidth=4 tabstop=8 expandtab