1 /* $Id: merge.c,v 1.23 2003-06-23 15:36:11 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
32 struct isc_merge_block {
33 int offset; /* offset in r_buf */
34 int block; /* block number of file (0 if none) */
35 int dirty; /* block is different from that on file */
39 static void opt_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
43 for (i = 0; i<ptr; i++)
46 if (no_dirty*4 < ptr*3)
49 for (i = 0; i<ptr; i++)
51 int tmp, j, j_min = -1;
52 for (j = i; j<ptr; j++)
54 if (j_min < 0 || mb[j_min].block > mb[j].block)
58 tmp = mb[j_min].block;
59 mb[j_min].block = mb[i].block;
68 static void flush_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
69 char *r_buf, int *firstpos, int cat, int last,
74 for (i = 0; i<ptr; i++)
76 /* consider this block number */
79 mb[i].block = isc_alloc_block (is, cat);
83 /* consider next block pointer */
84 if (last && i == ptr-1)
86 else if (!mb[i+1].block)
88 mb[i+1].block = isc_alloc_block (is, cat);
94 for (i = 0; i<ptr; i++)
97 ISAMC_BLOCK_SIZE ssize = mb[i+1].offset - mb[i].offset;
101 /* skip rest if not dirty */
104 assert (mb[i].block);
106 *firstpos = mb[i].block;
107 if (is->method->debug > 2)
108 logf (LOG_LOG, "isc: skip ptr=%d size=%d %d %d",
109 i, ssize, cat, mb[i].block);
110 ++(is->files[cat].no_skip_writes);
117 *firstpos = mb[i].block;
118 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_1;
119 ssize += ISAMC_BLOCK_OFFSET_1;
121 memcpy (src+sizeof(int)+sizeof(ssize), numkeys,
123 if (is->method->debug > 2)
124 logf (LOG_LOG, "isc: flush ptr=%d numk=%d size=%d nextpos=%d",
125 i, *numkeys, (int) ssize, mb[i+1].block);
129 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_N;
130 ssize += ISAMC_BLOCK_OFFSET_N;
131 if (is->method->debug > 2)
132 logf (LOG_LOG, "isc: flush ptr=%d size=%d nextpos=%d",
133 i, (int) ssize, mb[i+1].block);
135 memcpy (src, &mb[i+1].block, sizeof(int));
136 memcpy (src+sizeof(int), &ssize, sizeof(ssize));
137 isc_write_block (is, cat, mb[i].block, src);
141 static int get_border (ISAMC is, struct isc_merge_block *mb, int ptr,
142 int cat, int firstpos)
144 /* Border set to initial fill or block size depending on
145 whether we are creating a new one or updating and old one.
148 int fill = mb[ptr].block ? is->method->filecat[cat].bsize :
149 is->method->filecat[cat].ifill;
150 int off = (ptr||firstpos) ? ISAMC_BLOCK_OFFSET_N : ISAMC_BLOCK_OFFSET_1;
154 return mb[ptr].offset + fill - off;
157 ISAMC_P isc_merge (ISAMC is, ISAMC_P ipos, ISAMC_I *data)
160 char i_item[128], *i_item_ptr;
161 int i_more, i_mode, i;
164 char f_item[128], *f_item_ptr;
167 int debug = is->method->debug;
169 struct isc_merge_block mb[200];
173 char r_item_buf[128]; /* temporary result output */
174 char *r_buf; /* block with resulting data */
175 int r_offset = 0; /* current offset in r_buf */
176 int ptr = 0; /* pointer */
177 void *r_clientData; /* encode client data */
181 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
182 r_buf = is->merge_buf + 128;
184 pp = isc_pp_open (is, ipos);
185 /* read first item from file. make sure f_more indicates no boundary */
187 f_more = isc_read_item (pp, &f_item_ptr);
193 logf (LOG_LOG, "isc: isc_merge begin %d %d", cat, pp->pos);
195 /* read first item from i */
197 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
199 mb[ptr].block = pp->pos; /* is zero if no block on disk */
203 border = get_border (is, mb, ptr, cat, firstpos);
204 while (i_more || f_more)
206 char *r_item = r_item_buf;
211 /* block to block boundary in the original file. */
215 /* the resulting output is of the same category as the
218 if (r_offset <= mb[ptr].offset +is->method->filecat[cat].mfill)
220 /* the resulting output block is too small/empty. Delete
221 the original (if any)
224 logf (LOG_LOG, "isc: release A");
226 isc_release_block (is, pp->cat, mb[ptr].block);
227 mb[ptr].block = pp->pos;
235 /* indicate new boundary based on the original file */
236 mb[++ptr].block = pp->pos;
237 mb[ptr].dirty = last_dirty;
238 mb[ptr].offset = r_offset;
240 logf (LOG_LOG, "isc: bound ptr=%d,offset=%d",
242 if (cat==is->max_cat && ptr >= is->method->max_blocks_mem)
244 /* We are dealing with block(s) of max size. Block(s)
245 except 1 will be flushed.
248 logf (LOG_LOG, "isc: flush A %d sections", ptr);
249 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
251 mb[0].block = mb[ptr-1].block;
252 mb[0].dirty = mb[ptr-1].dirty;
253 memcpy (r_buf, r_buf + mb[ptr-1].offset,
254 mb[ptr].offset - mb[ptr-1].offset);
257 mb[1].block = mb[ptr].block;
258 mb[1].dirty = mb[ptr].dirty;
259 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
261 r_offset = mb[ptr].offset;
265 border = get_border (is, mb, ptr, cat, firstpos);
273 cmp = (*is->method->compare_item)(i_item, f_item);
274 if (cmp == 0) /* insert i=f */
276 if (!i_mode) /* delete item? */
280 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
282 /* is next input item the same as current except
283 for the delete flag? */
284 cmp = (*is->method->compare_item)(i_item, f_item);
285 if (!cmp && i_mode) /* delete/insert nop? */
287 /* yes! insert as if it was an insert only */
288 memcpy (r_item, i_item, i_item_ptr - i_item);
290 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
295 /* no! delete the item */
303 memcpy (r_item, f_item, f_item_ptr - f_item);
307 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
312 f_more = isc_read_item (pp, &f_item_ptr);
314 else if (cmp > 0) /* insert f */
316 memcpy (r_item, f_item, f_item_ptr - f_item);
319 f_more = isc_read_item (pp, &f_item_ptr);
323 if (!i_mode) /* delete item which isn't there? */
325 logf (LOG_FATAL, "Inconsistent register at offset %d",
329 memcpy (r_item, i_item, i_item_ptr - i_item);
334 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
337 if (r_item) /* insert resulting item? */
339 char *r_out_ptr = r_buf + r_offset;
342 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
343 &r_out_ptr, &r_item);
344 new_offset = r_out_ptr - r_buf;
348 if (border < new_offset && border >= r_offset)
351 logf (LOG_LOG, "isc: border %d %d", ptr, border);
352 /* Max size of current block category reached ...
353 make new virtual block entry */
356 mb[ptr].offset = r_offset;
357 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
359 /* We are dealing with block(s) of max size. Block(s)
360 except one will be flushed. Note: the block(s) are
361 surely not the last one(s).
364 logf (LOG_LOG, "isc: flush B %d sections", ptr-1);
365 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
367 mb[0].block = mb[ptr-1].block;
368 mb[0].dirty = mb[ptr-1].dirty;
369 memcpy (r_buf, r_buf + mb[ptr-1].offset,
370 mb[ptr].offset - mb[ptr-1].offset);
373 mb[1].block = mb[ptr].block;
374 mb[1].dirty = mb[0].dirty;
375 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
376 memcpy (r_buf + mb[1].offset, r_buf + r_offset,
377 new_offset - r_offset);
378 new_offset = (new_offset - r_offset) + mb[1].offset;
381 border = get_border (is, mb, ptr, cat, firstpos);
383 r_offset = new_offset;
385 if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
387 /* Max number blocks in current category reached ->
388 must switch to next category (with larger block size)
392 (is->files[cat].no_remap)++;
393 /* delete all original block(s) read so far */
394 for (i = 0; i < ptr; i++)
396 isc_release_block (is, pp->cat, mb[i].block);
397 /* also delete all block to be read in the future */
400 /* remap block offsets */
401 assert (mb[j].offset == 0);
405 mb[ptr].offset = r_offset;
406 for (i = 1; i < ptr; i++)
408 int border = is->method->filecat[cat].ifill -
409 ISAMC_BLOCK_OFFSET_1 + mb[j].offset;
411 logf (LOG_LOG, "isc: remap %d border=%d", i, border);
412 if (mb[i+1].offset > border && mb[i].offset <= border)
415 logf (LOG_LOG, "isc: to %d %d", j, mb[i].offset);
418 mb[j].offset = mb[i].offset;
422 logf (LOG_LOG, "isc: remap from %d to %d sections to cat %d",
425 border = get_border (is, mb, ptr, cat, firstpos);
427 logf (LOG_LOG, "isc: border=%d r_offset=%d", border, r_offset);
430 if (mb[ptr].offset < r_offset)
431 { /* make the final boundary offset */
434 mb[ptr].offset = r_offset;
437 { /* empty output. Release last block if any */
438 if (cat == pp->cat && mb[ptr].block)
441 logf (LOG_LOG, "isc: release C");
442 isc_release_block (is, pp->cat, mb[ptr].block);
450 logf (LOG_LOG, "isc: flush C, %d sections", ptr);
454 /* we have to patch initial block with num keys if that
456 if (numKeys != isc_pp_num (pp))
459 logf (LOG_LOG, "isc: patch num keys firstpos=%d num=%d",
461 bf_write (is->files[cat].bf, firstpos, ISAMC_BLOCK_OFFSET_N,
462 sizeof(numKeys), &numKeys);
466 { /* we haven't flushed initial block yet and there surely are some
467 blocks to flush. Make first block dirty if numKeys differ */
468 if (numKeys != isc_pp_num (pp))
471 /* flush rest of block(s) in r_buf */
472 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1, &numKeys);
474 (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
478 logf (LOG_LOG, "isc: isc_merge return %d %d", cat, firstpos);
480 return cat + firstpos * 8;
485 * Revision 1.23 2003-06-23 15:36:11 adam
486 * Implemented isamb_unlink.
488 * Revision 1.22 2003/03/05 16:41:10 adam
491 * Revision 1.21 2002/08/02 19:26:56 adam
494 * Revision 1.20 1999/11/30 13:48:04 adam
495 * Improved installation. Updated for inclusion of YAZ header files.
497 * Revision 1.19 1999/07/14 12:12:07 heikki
498 * Large-block isam-h (may not work too well... Abandoning for isam-d)
500 * Revision 1.17 1999/07/13 14:22:17 heikki
501 * Better allocation strategy in isamh_merge
503 * Revision 1.16 1999/07/08 14:23:27 heikki
504 * Fixed a bug in isamh_pp_read and cleaned up a bit
506 * Revision 1.15 1999/07/07 09:36:04 heikki
507 * Fixed an assertion in isamh
509 * Revision 1.13 1999/07/06 09:37:05 heikki
510 * Working on isamh - not ready yet.
512 * Revision 1.12 1999/06/30 15:03:55 heikki
513 * first take on isamh, the append-only isam structure
515 * Revision 1.11 1999/05/26 07:49:14 adam
518 * Revision 1.10 1998/03/19 12:22:09 adam
521 * Revision 1.9 1998/03/19 10:04:38 adam
524 * Revision 1.8 1998/03/18 09:23:55 adam
525 * Blocks are stored in chunks on free list - up to factor 2 in speed.
526 * Fixed bug that could occur in block category rearrangemen.
528 * Revision 1.7 1998/03/11 11:18:18 adam
529 * Changed the isc_merge to take into account the mfill (minimum-fill).
531 * Revision 1.6 1998/03/06 13:54:03 adam
532 * Fixed two nasty bugs in isc_merge.
534 * Revision 1.5 1997/02/12 20:42:43 adam
535 * Bug fix: during isc_merge operations, some pages weren't marked dirty
536 * even though they should be. At this point the merge operation marks
537 * a page dirty if the previous page changed at all. A better approach is
538 * to mark it dirty if the last key written changed in previous page.
540 * Revision 1.4 1996/11/08 11:15:31 adam
541 * Number of keys in chain are stored in first block and the function
542 * to retrieve this information, isc_pp_num is implemented.
544 * Revision 1.3 1996/11/04 14:08:59 adam
545 * Optimized free block usage.
547 * Revision 1.2 1996/11/01 13:36:46 adam
548 * New element, max_blocks_mem, that control how many blocks of max size
549 * to store in memory during isc_merge.
550 * Function isc_merge now ignores delete/update of identical keys and
551 * the proper blocks are then non-dirty and not written in flush_blocks.
553 * Revision 1.1 1996/11/01 08:59:15 adam
554 * First version of isc_merge that supports update/delete.