1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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
27 struct isamc_merge_block {
28 int offset; /* offset in r_buf */
29 zint block; /* block number of file (0 if none) */
30 int dirty; /* block is different from that on file */
34 static void opt_blocks (ISAMC is, struct isamc_merge_block *mb, int ptr,
38 for (i = 0; i<ptr; i++)
41 if (no_dirty*4 < ptr*3)
44 for (i = 0; i<ptr; i++)
46 int tmp, j, j_min = -1;
47 for (j = i; j<ptr; j++)
49 if (j_min < 0 || mb[j_min].block > mb[j].block)
53 tmp = mb[j_min].block;
54 mb[j_min].block = mb[i].block;
63 static void flush_blocks (ISAMC is, struct isamc_merge_block *mb, int ptr,
64 char *r_buf, zint *firstpos, int cat, int last,
69 for (i = 0; i<ptr; i++)
71 /* consider this block number */
74 mb[i].block = isamc_alloc_block (is, cat);
78 /* consider next block pointer */
79 if (last && i == ptr-1)
81 else if (!mb[i+1].block)
83 mb[i+1].block = isamc_alloc_block (is, cat);
89 for (i = 0; i<ptr; i++)
92 ISAMC_BLOCK_SIZE ssize = mb[i+1].offset - mb[i].offset;
96 /* skip rest if not dirty */
101 *firstpos = mb[i].block;
102 if (is->method->debug > 2)
103 yaz_log (YLOG_LOG, "isc: skip ptr=%d size=%d %d " ZINT_FORMAT,
104 i, ssize, cat, mb[i].block);
105 ++(is->files[cat].no_skip_writes);
112 *firstpos = mb[i].block;
113 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_1;
114 ssize += ISAMC_BLOCK_OFFSET_1;
116 memcpy (src+sizeof(zint)+sizeof(ssize), numkeys, sizeof(*numkeys));
117 if (is->method->debug > 2)
118 yaz_log (YLOG_LOG, "isc: flush ptr=%d numk=" ZINT_FORMAT " size=%d nextpos="
119 ZINT_FORMAT, i, *numkeys, (int) ssize, mb[i+1].block);
123 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_N;
124 ssize += ISAMC_BLOCK_OFFSET_N;
125 if (is->method->debug > 2)
126 yaz_log (YLOG_LOG, "isc: flush ptr=%d size=%d nextpos=" ZINT_FORMAT,
127 i, (int) ssize, mb[i+1].block);
129 memcpy (src, &mb[i+1].block, sizeof(zint));
130 memcpy (src+sizeof(zint), &ssize, sizeof(ssize));
131 isamc_write_block (is, cat, mb[i].block, src);
135 static int get_border (ISAMC is, struct isamc_merge_block *mb, zint ptr,
136 int cat, zint firstpos)
138 /* Border set to initial fill or block size depending on
139 whether we are creating a new one or updating and old one.
142 int fill = mb[ptr].block ? is->method->filecat[cat].bsize :
143 is->method->filecat[cat].ifill;
144 int off = (ptr||firstpos) ? ISAMC_BLOCK_OFFSET_N : ISAMC_BLOCK_OFFSET_1;
148 return mb[ptr].offset + fill - off;
151 void isamc_merge (ISAMC is, ISAM_P *ipos, ISAMC_I *data)
154 char i_item[128], *i_item_ptr;
155 int i_more, i_mode, i;
158 char f_item[128], *f_item_ptr;
161 int debug = is->method->debug;
163 struct isamc_merge_block mb[200];
167 char r_item_buf[128]; /* temporary result output */
168 char *r_buf; /* block with resulting data */
169 int r_offset = 0; /* current offset in r_buf */
170 int ptr = 0; /* pointer */
171 void *r_clientData; /* encode client data */
175 r_clientData = (*is->method->codec.start)();
176 r_buf = is->merge_buf + 128;
178 pp = isamc_pp_open (is, *ipos);
179 /* read first item from file. make sure f_more indicates no boundary */
181 f_more = isamc_read_item (pp, &f_item_ptr);
187 yaz_log (YLOG_LOG, "isc: isamc_merge begin %d " ZINT_FORMAT, cat, pp->pos);
189 /* read first item from i */
191 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
193 mb[ptr].block = pp->pos; /* is zero if no block on disk */
197 border = get_border (is, mb, ptr, cat, firstpos);
198 while (i_more || f_more)
200 char *r_item = r_item_buf;
205 /* block to block boundary in the original file. */
209 /* the resulting output is of the same category as the
212 if (r_offset <= mb[ptr].offset +is->method->filecat[cat].mfill)
214 /* the resulting output block is too small/empty. Delete
215 the original (if any)
218 yaz_log (YLOG_LOG, "isc: release A");
220 isamc_release_block (is, pp->cat, mb[ptr].block);
221 mb[ptr].block = pp->pos;
229 /* indicate new boundary based on the original file */
230 mb[++ptr].block = pp->pos;
231 mb[ptr].dirty = last_dirty;
232 mb[ptr].offset = r_offset;
234 yaz_log (YLOG_LOG, "isc: bound ptr=%d,offset=%d",
236 if (cat==is->max_cat && ptr >= is->method->max_blocks_mem)
238 /* We are dealing with block(s) of max size. Block(s)
239 except 1 will be flushed.
242 yaz_log (YLOG_LOG, "isc: flush A %d sections", ptr);
243 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
245 mb[0].block = mb[ptr-1].block;
246 mb[0].dirty = mb[ptr-1].dirty;
247 memcpy (r_buf, r_buf + mb[ptr-1].offset,
248 mb[ptr].offset - mb[ptr-1].offset);
251 mb[1].block = mb[ptr].block;
252 mb[1].dirty = mb[ptr].dirty;
253 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
255 r_offset = mb[ptr].offset;
259 border = get_border (is, mb, ptr, cat, firstpos);
267 cmp = (*is->method->compare_item)(i_item, f_item);
268 if (cmp == 0) /* insert i=f */
270 if (!i_mode) /* delete item? */
274 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
276 /* is next input item the same as current except
277 for the delete flag? */
278 cmp = (*is->method->compare_item)(i_item, f_item);
279 if (!cmp && i_mode) /* delete/insert nop? */
281 /* yes! insert as if it was an insert only */
282 memcpy (r_item, i_item, i_item_ptr - i_item);
284 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
289 /* no! delete the item */
297 memcpy (r_item, f_item, f_item_ptr - f_item);
301 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
306 f_more = isamc_read_item (pp, &f_item_ptr);
308 else if (cmp > 0) /* insert f */
310 memcpy (r_item, f_item, f_item_ptr - f_item);
313 f_more = isamc_read_item (pp, &f_item_ptr);
317 if (!i_mode) /* delete item which isn't there? */
319 yaz_log (YLOG_FATAL, "Inconsistent register at offset %d",
323 memcpy (r_item, i_item, i_item_ptr - i_item);
328 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
331 if (r_item) /* insert resulting item? */
333 char *r_out_ptr = r_buf + r_offset;
334 const char *src = r_item;
337 (*is->method->codec.encode)(r_clientData, &r_out_ptr, &src);
338 new_offset = r_out_ptr - r_buf;
342 if (border < new_offset && border >= r_offset)
345 yaz_log (YLOG_LOG, "isc: border %d " ZINT_FORMAT,
347 /* Max size of current block category reached ...
348 make new virtual block entry */
351 mb[ptr].offset = r_offset;
352 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
354 /* We are dealing with block(s) of max size. Block(s)
355 except one will be flushed. Note: the block(s) are
356 surely not the last one(s).
359 yaz_log (YLOG_LOG, "isc: flush B %d sections", ptr-1);
360 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
362 mb[0].block = mb[ptr-1].block;
363 mb[0].dirty = mb[ptr-1].dirty;
364 memcpy (r_buf, r_buf + mb[ptr-1].offset,
365 mb[ptr].offset - mb[ptr-1].offset);
368 mb[1].block = mb[ptr].block;
369 mb[1].dirty = mb[0].dirty;
370 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
371 memcpy (r_buf + mb[1].offset, r_buf + r_offset,
372 new_offset - r_offset);
373 new_offset = (new_offset - r_offset) + mb[1].offset;
376 border = get_border (is, mb, ptr, cat, firstpos);
378 r_offset = new_offset;
380 if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
382 /* Max number blocks in current category reached ->
383 must switch to next category (with larger block size)
387 (is->files[cat].no_remap)++;
388 /* delete all original block(s) read so far */
389 for (i = 0; i < ptr; i++)
391 isamc_release_block (is, pp->cat, mb[i].block);
392 /* also delete all block to be read in the future */
395 /* remap block offsets */
396 assert (mb[j].offset == 0);
400 mb[ptr].offset = r_offset;
401 for (i = 1; i < ptr; i++)
403 int border = is->method->filecat[cat].ifill -
404 ISAMC_BLOCK_OFFSET_1 + mb[j].offset;
406 yaz_log (YLOG_LOG, "isc: remap %d border=%d", i, border);
407 if (mb[i+1].offset > border && mb[i].offset <= border)
410 yaz_log (YLOG_LOG, "isc: to %d %d", j, mb[i].offset);
413 mb[j].offset = mb[i].offset;
417 yaz_log (YLOG_LOG, "isc: remap from %d to %d sections to cat %d",
420 border = get_border (is, mb, ptr, cat, firstpos);
422 yaz_log (YLOG_LOG, "isc: border=" ZINT_FORMAT " r_offset=%d",
426 if (mb[ptr].offset < r_offset)
427 { /* make the final boundary offset */
430 mb[ptr].offset = r_offset;
433 { /* empty output. Release last block if any */
434 if (cat == pp->cat && mb[ptr].block)
437 yaz_log (YLOG_LOG, "isc: release C");
438 isamc_release_block (is, pp->cat, mb[ptr].block);
446 yaz_log (YLOG_LOG, "isc: flush C, %d sections", ptr);
450 /* we have to patch initial block with num keys if that
452 if (numKeys != isamc_pp_num (pp))
455 yaz_log (YLOG_LOG, "isc: patch num keys firstpos=" ZINT_FORMAT " num=" ZINT_FORMAT,
457 bf_write (is->files[cat].bf, firstpos, ISAMC_BLOCK_OFFSET_N,
458 sizeof(numKeys), &numKeys);
462 { /* we haven't flushed initial block yet and there surely are some
463 blocks to flush. Make first block dirty if numKeys differ */
464 if (numKeys != isamc_pp_num (pp))
467 /* flush rest of block(s) in r_buf */
468 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1, &numKeys);
470 (*is->method->codec.stop)(r_clientData);
474 yaz_log (YLOG_LOG, "isc: isamc_merge return %d " ZINT_FORMAT, cat, firstpos);
476 *ipos = cat + firstpos * 8;
482 * c-file-style: "Stroustrup"
483 * indent-tabs-mode: nil
485 * vim: shiftwidth=4 tabstop=8 expandtab