1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Reduction to lower categories in isamc_merge
30 #include <yaz/xmalloc.h>
33 static void flush_block (ISAMC is, int cat);
34 static void release_fc (ISAMC is, int cat);
35 static void init_fc (ISAMC is, int cat);
37 #define ISAMC_FREELIST_CHUNK 0
41 void isamc_getmethod (ISAMC_M *m)
44 static struct ISAMC_filecat_s def_cat[] = {
50 { 128, 120, 100, 10 },
51 { 512, 490, 350, 10 },
52 { 2048, 1900, 1700, 10 },
53 { 8192, 8000, 7900, 10 },
54 { 32768, 32000, 31000, 0 },
59 m->codec.start = NULL;
60 m->codec.decode = NULL;
61 m->codec.encode = NULL;
63 m->codec.reset = NULL;
65 m->compare_item = NULL;
70 m->max_blocks_mem = 10;
73 ISAMC isamc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
76 ISAMC_filecat filecat;
80 is = (ISAMC) xmalloc (sizeof(*is));
82 is->method = (ISAMC_M *) xmalloc (sizeof(*is->method));
83 memcpy (is->method, method, sizeof(*method));
84 filecat = is->method->filecat;
87 /* determine number of block categories */
88 if (is->method->debug)
89 yaz_log (YLOG_LOG, "isc: bsize ifill mfill mblocks");
92 if (is->method->debug)
93 yaz_log (YLOG_LOG, "isc:%6d %6d %6d %6d",
94 filecat[i].bsize, filecat[i].ifill,
95 filecat[i].mfill, filecat[i].mblocks);
96 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
97 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
98 } while (filecat[i++].mblocks);
101 /* max_buf_size is the larget buffer to be used during merge */
102 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
103 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
104 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
105 if (is->method->debug)
106 yaz_log (YLOG_LOG, "isc: max_buf_size %d", max_buf_size);
108 assert (is->no_files > 0);
109 is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
112 is->merge_buf = (char *) xmalloc (max_buf_size+256);
113 memset (is->merge_buf, 0, max_buf_size+256);
116 is->merge_buf = NULL;
117 for (i = 0; i<is->no_files; i++)
120 is->files[i].head_is_dirty = 0;
121 is->files[i].head.lastblock = 1;
122 is->files[i].head.freelist = 0;
123 is->files[i].alloc_entries_num = 0;
124 is->files[i].alloc_entries_max =
125 is->method->filecat[i].bsize / sizeof(zint) - 1;
126 is->files[i].alloc_buf = (char *)
127 xmalloc (is->method->filecat[i].bsize);
128 is->files[i].no_writes = 0;
129 is->files[i].no_reads = 0;
130 is->files[i].no_skip_writes = 0;
131 is->files[i].no_allocated = 0;
132 is->files[i].no_released = 0;
133 is->files[i].no_remap = 0;
134 is->files[i].no_forward = 0;
135 is->files[i].no_backward = 0;
136 is->files[i].sum_forward = 0;
137 is->files[i].sum_backward = 0;
138 is->files[i].no_next = 0;
139 is->files[i].no_prev = 0;
144 for (i = 0; i<is->no_files; i++)
146 char fname[FILENAME_MAX];
149 sprintf (fname, "%s%c", name, i+'A');
150 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
152 if (!is->files[i].bf)
157 r = bf_read(is->files[i].bf, 0, 0, sizeof(ISAMC_head),
168 zint isamc_block_used (ISAMC is, int type)
170 if (type < 0 || type >= is->no_files)
172 return is->files[type].head.lastblock-1;
175 int isamc_block_size (ISAMC is, int type)
177 ISAMC_filecat filecat = is->method->filecat;
178 if (type < 0 || type >= is->no_files)
180 return filecat[type].bsize;
183 int isamc_close (ISAMC is)
187 if (is->method->debug)
189 yaz_log (YLOG_LOG, "isc: next forw mid-f prev backw mid-b");
190 for (i = 0; i<is->no_files; i++)
191 yaz_log (YLOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
192 is->files[i].no_next,
193 is->files[i].no_forward,
194 is->files[i].no_forward ?
195 (double) is->files[i].sum_forward/is->files[i].no_forward
197 is->files[i].no_prev,
198 is->files[i].no_backward,
199 is->files[i].no_backward ?
200 (double) is->files[i].sum_backward/is->files[i].no_backward
203 if (is->method->debug)
204 yaz_log (YLOG_LOG, "isc: writes reads skipped alloc released remap");
205 for (i = 0; i<is->no_files; i++)
208 if (is->method->debug)
209 yaz_log (YLOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
210 is->files[i].no_writes,
211 is->files[i].no_reads,
212 is->files[i].no_skip_writes,
213 is->files[i].no_allocated,
214 is->files[i].no_released,
215 is->files[i].no_remap);
218 if (is->files[i].head_is_dirty)
219 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
222 bf_close (is->files[i].bf);
224 xfree(is->files[i].fc_list);
225 xfree(is->files[i].alloc_buf);
228 xfree (is->merge_buf);
234 int isamc_read_block (ISAMC is, int cat, zint pos, char *dst)
236 ++(is->files[cat].no_reads);
237 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
240 int isamc_write_block (ISAMC is, int cat, zint pos, char *src)
242 ++(is->files[cat].no_writes);
243 if (is->method->debug > 2)
244 yaz_log (YLOG_LOG, "isc: write_block %d " ZINT_FORMAT, cat, pos);
245 return bf_write (is->files[cat].bf, pos, 0, 0, src);
248 int isamc_write_dblock (ISAMC is, int cat, zint pos, char *src,
249 zint nextpos, int offset)
251 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
252 if (is->method->debug > 2)
253 yaz_log (YLOG_LOG, "isc: write_dblock. size=%d nextpos=" ZINT_FORMAT,
254 (int) size, nextpos);
255 src -= ISAMC_BLOCK_OFFSET_N;
256 memcpy (src, &nextpos, sizeof(nextpos));
257 memcpy (src + sizeof(nextpos), &size, sizeof(size));
258 return isamc_write_block (is, cat, pos, src);
261 #if ISAMC_FREELIST_CHUNK
262 static void flush_block (ISAMC is, int cat)
264 char *abuf = is->files[cat].alloc_buf;
265 zint block = is->files[cat].head.freelist;
266 if (block && is->files[cat].alloc_entries_num)
268 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(block));
269 bf_write (is->files[cat].bf, block, 0, 0, abuf);
270 is->files[cat].alloc_entries_num = 0;
274 static zint alloc_block (ISAMC is, int cat)
276 zint block = is->files[cat].head.freelist;
277 char *abuf = is->files[cat].alloc_buf;
279 (is->files[cat].no_allocated)++;
283 block = (is->files[cat].head.lastblock)++; /* no free list */
284 is->files[cat].head_is_dirty = 1;
288 if (!is->files[cat].alloc_entries_num) /* read first time */
290 bf_read (is->files[cat].bf, block, 0, 0, abuf);
291 memcpy (&is->files[cat].alloc_entries_num, abuf,
292 sizeof(is->files[cat].alloc_entries_num));
293 assert (is->files[cat].alloc_entries_num > 0);
295 /* have some free blocks now */
296 assert (is->files[cat].alloc_entries_num > 0);
297 is->files[cat].alloc_entries_num--;
298 if (!is->files[cat].alloc_entries_num) /* last one in block? */
300 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
302 is->files[cat].head_is_dirty = 1;
304 if (is->files[cat].head.freelist)
306 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
308 memcpy (&is->files[cat].alloc_entries_num, abuf,
309 sizeof(is->files[cat].alloc_entries_num));
310 assert (is->files[cat].alloc_entries_num);
314 memcpy (&block, abuf + sizeof(zint) + sizeof(int) *
315 is->files[cat].alloc_entries_num, sizeof(zint));
320 static void release_block (ISAMC is, int cat, zint pos)
322 char *abuf = is->files[cat].alloc_buf;
323 zint block = is->files[cat].head.freelist;
325 (is->files[cat].no_released)++;
327 if (block && !is->files[cat].alloc_entries_num) /* must read block */
329 bf_read (is->files[cat].bf, block, 0, 0, abuf);
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 > 0);
334 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
335 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
338 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
339 bf_write (is->files[cat].bf, block, 0, 0, abuf);
340 is->files[cat].alloc_entries_num = 0;
342 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
344 memcpy (abuf + sizeof(int), &block, sizeof(zint));
345 is->files[cat].head.freelist = pos;
346 is->files[cat].head_is_dirty = 1;
350 memcpy (abuf + sizeof(int) +
351 is->files[cat].alloc_entries_num*sizeof(zint),
354 is->files[cat].alloc_entries_num++;
357 static void flush_block (ISAMC is, int cat)
361 static zint alloc_block (ISAMC is, int cat)
364 char buf[sizeof(zint)];
366 is->files[cat].head_is_dirty = 1;
367 (is->files[cat].no_allocated)++;
368 if ((block = is->files[cat].head.freelist))
370 bf_read (is->files[cat].bf, block, 0, sizeof(zint), buf);
371 memcpy (&is->files[cat].head.freelist, buf, sizeof(zint));
374 block = (is->files[cat].head.lastblock)++;
378 static void release_block (ISAMC is, int cat, zint pos)
380 char buf[sizeof(zint)];
382 (is->files[cat].no_released)++;
383 is->files[cat].head_is_dirty = 1;
384 memcpy (buf, &is->files[cat].head.freelist, sizeof(zint));
385 is->files[cat].head.freelist = pos;
386 bf_write (is->files[cat].bf, pos, 0, sizeof(zint), buf);
390 zint isamc_alloc_block (ISAMC is, int cat)
394 if (is->files[cat].fc_list)
398 for (j = 0; j < is->files[cat].fc_max; j++)
399 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
401 is->files[cat].fc_list[j] = 0;
407 block = alloc_block (is, cat);
408 if (is->method->debug > 3)
409 yaz_log (YLOG_LOG, "isc: alloc_block in cat %d: " ZINT_FORMAT, cat, block);
413 void isamc_release_block (ISAMC is, int cat, zint pos)
415 if (is->method->debug > 3)
416 yaz_log (YLOG_LOG, "isc: release_block in cat %d:" ZINT_FORMAT, cat, pos);
417 if (is->files[cat].fc_list)
420 for (j = 0; j<is->files[cat].fc_max; j++)
421 if (!is->files[cat].fc_list[j])
423 is->files[cat].fc_list[j] = pos;
427 release_block (is, cat, pos);
430 static void init_fc (ISAMC is, int cat)
434 is->files[cat].fc_max = j;
435 is->files[cat].fc_list = (zint *)
436 xmalloc (sizeof(*is->files[0].fc_list) * j);
438 is->files[cat].fc_list[j] = 0;
441 static void release_fc (ISAMC is, int cat)
443 int j = is->files[cat].fc_max;
447 if ((b = is->files[cat].fc_list[j]))
449 release_block (is, cat, b);
450 is->files[cat].fc_list[j] = 0;
454 void isamc_pp_close (ISAMC_PP pp)
458 (*is->method->codec.stop)(pp->decodeClientData);
463 ISAMC_PP isamc_pp_open (ISAMC is, ISAM_P ipos)
465 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
468 pp->cat = (int) isamc_type(ipos);
469 pp->pos = isamc_block(ipos);
471 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
477 pp->decodeClientData = (*is->method->codec.start)();
484 isamc_read_block (is, pp->cat, pp->pos, src);
485 memcpy (&pp->next, src, sizeof(pp->next));
486 src += sizeof(pp->next);
487 memcpy (&pp->size, src, sizeof(pp->size));
488 src += sizeof(pp->size);
489 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
490 src += sizeof(pp->numKeys);
491 if (pp->next == pp->pos)
493 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
494 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
495 assert (pp->next != pp->pos);
497 pp->offset = src - pp->buf;
498 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
499 if (is->method->debug > 2)
500 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
501 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
506 /* returns non-zero if item could be read; 0 otherwise */
507 int isamc_pp_read (ISAMC_PP pp, void *buf)
510 return isamc_read_item (pp, &cp);
513 /* read one item from file - decode and store it in *dst.
516 1 if item could be read ok and NO boundary
517 2 if item could be read ok and boundary */
518 int isamc_read_item (ISAMC_PP pp, char **dst)
521 const char *src = pp->buf + pp->offset;
523 if (pp->offset >= pp->size)
528 return 0; /* end of file */
530 if (pp->next > pp->pos)
532 if (pp->next == pp->pos + 1)
533 is->files[pp->cat].no_next++;
536 is->files[pp->cat].no_forward++;
537 is->files[pp->cat].sum_forward += pp->next - pp->pos;
542 if (pp->next + 1 == pp->pos)
543 is->files[pp->cat].no_prev++;
546 is->files[pp->cat].no_backward++;
547 is->files[pp->cat].sum_backward += pp->pos - pp->next;
550 /* out new block position */
553 /* read block and save 'next' and 'size' entry */
554 isamc_read_block (is, pp->cat, pp->pos, pp->buf);
555 memcpy (&pp->next, src, sizeof(pp->next));
556 src += sizeof(pp->next);
557 memcpy (&pp->size, src, sizeof(pp->size));
558 src += sizeof(pp->size);
559 /* assume block is non-empty */
560 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
562 if (pp->next == pp->pos)
564 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->next = " ZINT_FORMAT, pp->next);
565 yaz_log(YLOG_FATAL|YLOG_LOG, "pp->pos = " ZINT_FORMAT, pp->pos);
566 assert (pp->next != pp->pos);
570 isamc_release_block (is, pp->cat, pp->pos);
571 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
572 pp->offset = src - pp->buf;
573 if (is->method->debug > 2)
574 yaz_log (YLOG_LOG, "isc: read_block size=%d %d " ZINT_FORMAT " next="
575 ZINT_FORMAT, pp->size, pp->cat, pp->pos, pp->next);
578 (*is->method->codec.decode)(pp->decodeClientData, dst, &src);
579 pp->offset = src - pp->buf;
583 zint isamc_pp_num (ISAMC_PP pp)
591 * c-file-style: "Stroustrup"
592 * indent-tabs-mode: nil
594 * vim: shiftwidth=4 tabstop=8 expandtab