1 /* $Id: merge.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
30 struct isamc_merge_block {
31 int offset; /* offset in r_buf */
32 zint block; /* block number of file (0 if none) */
33 int dirty; /* block is different from that on file */
37 static void opt_blocks (ISAMC is, struct isamc_merge_block *mb, int ptr,
41 for (i = 0; i<ptr; i++)
44 if (no_dirty*4 < ptr*3)
47 for (i = 0; i<ptr; i++)
49 int tmp, j, j_min = -1;
50 for (j = i; j<ptr; j++)
52 if (j_min < 0 || mb[j_min].block > mb[j].block)
56 tmp = mb[j_min].block;
57 mb[j_min].block = mb[i].block;
66 static void flush_blocks (ISAMC is, struct isamc_merge_block *mb, int ptr,
67 char *r_buf, zint *firstpos, int cat, int last,
72 for (i = 0; i<ptr; i++)
74 /* consider this block number */
77 mb[i].block = isamc_alloc_block (is, cat);
81 /* consider next block pointer */
82 if (last && i == ptr-1)
84 else if (!mb[i+1].block)
86 mb[i+1].block = isamc_alloc_block (is, cat);
92 for (i = 0; i<ptr; i++)
95 ISAMC_BLOCK_SIZE ssize = mb[i+1].offset - mb[i].offset;
99 /* skip rest if not dirty */
102 assert (mb[i].block);
104 *firstpos = mb[i].block;
105 if (is->method->debug > 2)
106 yaz_log (YLOG_LOG, "isc: skip ptr=%d size=%d %d " ZINT_FORMAT,
107 i, ssize, cat, mb[i].block);
108 ++(is->files[cat].no_skip_writes);
115 *firstpos = mb[i].block;
116 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_1;
117 ssize += ISAMC_BLOCK_OFFSET_1;
119 memcpy (src+sizeof(zint)+sizeof(ssize), numkeys, sizeof(*numkeys));
120 if (is->method->debug > 2)
121 yaz_log (YLOG_LOG, "isc: flush ptr=%d numk=" ZINT_FORMAT " size=%d nextpos="
122 ZINT_FORMAT, i, *numkeys, (int) ssize, mb[i+1].block);
126 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_N;
127 ssize += ISAMC_BLOCK_OFFSET_N;
128 if (is->method->debug > 2)
129 yaz_log (YLOG_LOG, "isc: flush ptr=%d size=%d nextpos=" ZINT_FORMAT,
130 i, (int) ssize, mb[i+1].block);
132 memcpy (src, &mb[i+1].block, sizeof(zint));
133 memcpy (src+sizeof(zint), &ssize, sizeof(ssize));
134 isamc_write_block (is, cat, mb[i].block, src);
138 static int get_border (ISAMC is, struct isamc_merge_block *mb, zint ptr,
139 int cat, zint firstpos)
141 /* Border set to initial fill or block size depending on
142 whether we are creating a new one or updating and old one.
145 int fill = mb[ptr].block ? is->method->filecat[cat].bsize :
146 is->method->filecat[cat].ifill;
147 int off = (ptr||firstpos) ? ISAMC_BLOCK_OFFSET_N : ISAMC_BLOCK_OFFSET_1;
151 return mb[ptr].offset + fill - off;
154 void isamc_merge (ISAMC is, ISAM_P *ipos, ISAMC_I *data)
157 char i_item[128], *i_item_ptr;
158 int i_more, i_mode, i;
161 char f_item[128], *f_item_ptr;
164 int debug = is->method->debug;
166 struct isamc_merge_block mb[200];
170 char r_item_buf[128]; /* temporary result output */
171 char *r_buf; /* block with resulting data */
172 int r_offset = 0; /* current offset in r_buf */
173 int ptr = 0; /* pointer */
174 void *r_clientData; /* encode client data */
178 r_clientData = (*is->method->codec.start)();
179 r_buf = is->merge_buf + 128;
181 pp = isamc_pp_open (is, *ipos);
182 /* read first item from file. make sure f_more indicates no boundary */
184 f_more = isamc_read_item (pp, &f_item_ptr);
190 yaz_log (YLOG_LOG, "isc: isamc_merge begin %d " ZINT_FORMAT, cat, pp->pos);
192 /* read first item from i */
194 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
196 mb[ptr].block = pp->pos; /* is zero if no block on disk */
200 border = get_border (is, mb, ptr, cat, firstpos);
201 while (i_more || f_more)
203 char *r_item = r_item_buf;
208 /* block to block boundary in the original file. */
212 /* the resulting output is of the same category as the
215 if (r_offset <= mb[ptr].offset +is->method->filecat[cat].mfill)
217 /* the resulting output block is too small/empty. Delete
218 the original (if any)
221 yaz_log (YLOG_LOG, "isc: release A");
223 isamc_release_block (is, pp->cat, mb[ptr].block);
224 mb[ptr].block = pp->pos;
232 /* indicate new boundary based on the original file */
233 mb[++ptr].block = pp->pos;
234 mb[ptr].dirty = last_dirty;
235 mb[ptr].offset = r_offset;
237 yaz_log (YLOG_LOG, "isc: bound ptr=%d,offset=%d",
239 if (cat==is->max_cat && ptr >= is->method->max_blocks_mem)
241 /* We are dealing with block(s) of max size. Block(s)
242 except 1 will be flushed.
245 yaz_log (YLOG_LOG, "isc: flush A %d sections", ptr);
246 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
248 mb[0].block = mb[ptr-1].block;
249 mb[0].dirty = mb[ptr-1].dirty;
250 memcpy (r_buf, r_buf + mb[ptr-1].offset,
251 mb[ptr].offset - mb[ptr-1].offset);
254 mb[1].block = mb[ptr].block;
255 mb[1].dirty = mb[ptr].dirty;
256 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
258 r_offset = mb[ptr].offset;
262 border = get_border (is, mb, ptr, cat, firstpos);
270 cmp = (*is->method->compare_item)(i_item, f_item);
271 if (cmp == 0) /* insert i=f */
273 if (!i_mode) /* delete item? */
277 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
279 /* is next input item the same as current except
280 for the delete flag? */
281 cmp = (*is->method->compare_item)(i_item, f_item);
282 if (!cmp && i_mode) /* delete/insert nop? */
284 /* yes! insert as if it was an insert only */
285 memcpy (r_item, i_item, i_item_ptr - i_item);
287 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
292 /* no! delete the item */
300 memcpy (r_item, f_item, f_item_ptr - f_item);
304 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
309 f_more = isamc_read_item (pp, &f_item_ptr);
311 else if (cmp > 0) /* insert f */
313 memcpy (r_item, f_item, f_item_ptr - f_item);
316 f_more = isamc_read_item (pp, &f_item_ptr);
320 if (!i_mode) /* delete item which isn't there? */
322 yaz_log (YLOG_FATAL, "Inconsistent register at offset %d",
326 memcpy (r_item, i_item, i_item_ptr - i_item);
331 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
334 if (r_item) /* insert resulting item? */
336 char *r_out_ptr = r_buf + r_offset;
337 const char *src = r_item;
340 (*is->method->codec.encode)(r_clientData, &r_out_ptr, &src);
341 new_offset = r_out_ptr - r_buf;
345 if (border < new_offset && border >= r_offset)
348 yaz_log (YLOG_LOG, "isc: border %d " ZINT_FORMAT,
350 /* Max size of current block category reached ...
351 make new virtual block entry */
354 mb[ptr].offset = r_offset;
355 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
357 /* We are dealing with block(s) of max size. Block(s)
358 except one will be flushed. Note: the block(s) are
359 surely not the last one(s).
362 yaz_log (YLOG_LOG, "isc: flush B %d sections", ptr-1);
363 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
365 mb[0].block = mb[ptr-1].block;
366 mb[0].dirty = mb[ptr-1].dirty;
367 memcpy (r_buf, r_buf + mb[ptr-1].offset,
368 mb[ptr].offset - mb[ptr-1].offset);
371 mb[1].block = mb[ptr].block;
372 mb[1].dirty = mb[0].dirty;
373 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
374 memcpy (r_buf + mb[1].offset, r_buf + r_offset,
375 new_offset - r_offset);
376 new_offset = (new_offset - r_offset) + mb[1].offset;
379 border = get_border (is, mb, ptr, cat, firstpos);
381 r_offset = new_offset;
383 if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
385 /* Max number blocks in current category reached ->
386 must switch to next category (with larger block size)
390 (is->files[cat].no_remap)++;
391 /* delete all original block(s) read so far */
392 for (i = 0; i < ptr; i++)
394 isamc_release_block (is, pp->cat, mb[i].block);
395 /* also delete all block to be read in the future */
398 /* remap block offsets */
399 assert (mb[j].offset == 0);
403 mb[ptr].offset = r_offset;
404 for (i = 1; i < ptr; i++)
406 int border = is->method->filecat[cat].ifill -
407 ISAMC_BLOCK_OFFSET_1 + mb[j].offset;
409 yaz_log (YLOG_LOG, "isc: remap %d border=%d", i, border);
410 if (mb[i+1].offset > border && mb[i].offset <= border)
413 yaz_log (YLOG_LOG, "isc: to %d %d", j, mb[i].offset);
416 mb[j].offset = mb[i].offset;
420 yaz_log (YLOG_LOG, "isc: remap from %d to %d sections to cat %d",
423 border = get_border (is, mb, ptr, cat, firstpos);
425 yaz_log (YLOG_LOG, "isc: border=" ZINT_FORMAT " r_offset=%d",
429 if (mb[ptr].offset < r_offset)
430 { /* make the final boundary offset */
433 mb[ptr].offset = r_offset;
436 { /* empty output. Release last block if any */
437 if (cat == pp->cat && mb[ptr].block)
440 yaz_log (YLOG_LOG, "isc: release C");
441 isamc_release_block (is, pp->cat, mb[ptr].block);
449 yaz_log (YLOG_LOG, "isc: flush C, %d sections", ptr);
453 /* we have to patch initial block with num keys if that
455 if (numKeys != isamc_pp_num (pp))
458 yaz_log (YLOG_LOG, "isc: patch num keys firstpos=" ZINT_FORMAT " num=" ZINT_FORMAT,
460 bf_write (is->files[cat].bf, firstpos, ISAMC_BLOCK_OFFSET_N,
461 sizeof(numKeys), &numKeys);
465 { /* we haven't flushed initial block yet and there surely are some
466 blocks to flush. Make first block dirty if numKeys differ */
467 if (numKeys != isamc_pp_num (pp))
470 /* flush rest of block(s) in r_buf */
471 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1, &numKeys);
473 (*is->method->codec.stop)(r_clientData);
477 yaz_log (YLOG_LOG, "isc: isamc_merge return %d " ZINT_FORMAT, cat, firstpos);
479 *ipos = cat + firstpos * 8;
485 * indent-tabs-mode: nil
487 * vim: shiftwidth=4 tabstop=8 expandtab