2 * Copyright (c) 1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1997-02-12 20:42:43 adam
8 * Bug fix: during isc_merge operations, some pages weren't marked dirty
9 * even though they should be. At this point the merge operation marks
10 * a page dirty if the previous page changed at all. A better approach is
11 * to mark it dirty if the last key written changed in previous page.
13 * Revision 1.4 1996/11/08 11:15:31 adam
14 * Number of keys in chain are stored in first block and the function
15 * to retrieve this information, isc_pp_num is implemented.
17 * Revision 1.3 1996/11/04 14:08:59 adam
18 * Optimized free block usage.
20 * Revision 1.2 1996/11/01 13:36:46 adam
21 * New element, max_blocks_mem, that control how many blocks of max size
22 * to store in memory during isc_merge.
23 * Function isc_merge now ignores delete/update of identical keys and
24 * the proper blocks are then non-dirty and not written in flush_blocks.
26 * Revision 1.1 1996/11/01 08:59:15 adam
27 * First version of isc_merge that supports update/delete.
39 struct isc_merge_block {
40 int offset; /* offset in r_buf */
41 int block; /* block number of file (0 if none) */
42 int dirty; /* block is different from that on file */
45 static void flush_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
46 char *r_buf, int *firstpos, int cat, int last,
51 for (i = 0; i<ptr; i++)
56 /* consider this block number */
59 mb[i].block = isc_alloc_block (is, cat);
63 /* consider next block pointer */
64 if (last && i == ptr-1)
66 else if (!mb[i+1].block)
68 mb[i+1].block = isc_alloc_block (is, cat);
73 ssize = mb[i+1].offset - mb[i].offset;
76 /* skip rest if not dirty */
81 *firstpos = mb[i].block;
82 if (is->method->debug > 2)
83 logf (LOG_LOG, "isc: skip ptr=%d size=%d %d %d",
84 i, ssize, cat, mb[i].block);
85 ++(is->files[cat].no_skip_writes);
92 *firstpos = mb[i].block;
93 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_1;
94 ssize += ISAMC_BLOCK_OFFSET_1;
96 memcpy (src+sizeof(int)+sizeof(ssize), numkeys,
98 if (is->method->debug > 2)
99 logf (LOG_LOG, "isc: flush ptr=%d numk=%d size=%d nextpos=%d",
100 i, *numkeys, (int) ssize, mb[i+1].block);
104 src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_N;
105 ssize += ISAMC_BLOCK_OFFSET_N;
106 if (is->method->debug > 2)
107 logf (LOG_LOG, "isc: flush ptr=%d size=%d nextpos=%d",
108 i, (int) ssize, mb[i+1].block);
110 memcpy (src, &mb[i+1].block, sizeof(int));
111 memcpy (src+sizeof(int), &ssize, sizeof(ssize));
112 isc_write_block (is, cat, mb[i].block, src);
116 static int get_border (ISAMC is, struct isc_merge_block *mb, int ptr,
117 int cat, int firstpos)
119 /* Border set to initial fill or block size depending on
120 whether we are creating a new one or updating and old one.
123 int fill = mb[ptr].block ? is->method->filecat[cat].bsize :
124 is->method->filecat[cat].ifill;
125 int off = (ptr||firstpos) ? ISAMC_BLOCK_OFFSET_N : ISAMC_BLOCK_OFFSET_1;
129 return mb[ptr].offset + fill - off;
132 ISAMC_P isc_merge (ISAMC is, ISAMC_P ipos, ISAMC_I data)
135 char i_item[128], *i_item_ptr;
136 int i_more, i_mode, i;
139 char f_item[128], *f_item_ptr;
142 struct isc_merge_block mb[200];
146 char r_item_buf[128]; /* temporary result output */
147 char *r_buf; /* block with resulting data */
148 int r_offset = 0; /* current offset in r_buf */
149 int ptr = 0; /* pointer */
150 void *r_clientData; /* encode client data */
154 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
155 r_buf = is->merge_buf + 128;
157 pp = isc_pp_open (is, ipos);
158 /* read first item from file. make sure f_more indicates no boundary */
160 f_more = isc_read_item (pp, &f_item_ptr);
165 if (is->method->debug > 1)
166 logf (LOG_LOG, "isc: isc_merge begin %d %d", cat, pp->pos);
168 /* read first item from i */
170 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
172 mb[ptr].block = pp->pos; /* is zero if no block on disk */
176 border = get_border (is, mb, ptr, cat, firstpos);
177 while (i_more || f_more)
179 char *r_item = r_item_buf;
184 /* block to block boundary in the original file. */
188 /* the resulting output is of the same category as the
191 if (mb[ptr].offset == r_offset)
193 /* the resulting output block is empty. Delete
194 the original (if any)
196 if (is->method->debug > 3)
197 logf (LOG_LOG, "isc: release A");
199 isc_release_block (is, pp->cat, mb[ptr].block);
200 mb[ptr].block = pp->pos;
207 /* indicate new boundary based on the original file */
208 mb[++ptr].block = pp->pos;
209 mb[ptr].dirty = (mb[ptr-1].dirty > 1) ? 1 : 0;
210 mb[ptr].offset = r_offset;
211 if (is->method->debug > 3)
212 logf (LOG_LOG, "isc: bound ptr=%d,offset=%d",
214 if (cat==is->max_cat && ptr >= is->method->max_blocks_mem)
216 /* We are dealing with block(s) of max size. Block(s)
217 except 1 will be flushed.
219 if (is->method->debug > 2)
220 logf (LOG_LOG, "isc: flush A %d sections", ptr);
221 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
224 mb[0].block = mb[ptr-1].block;
225 mb[0].dirty = mb[ptr-1].dirty;
226 memcpy (r_buf, r_buf + mb[ptr-1].offset,
227 mb[ptr].offset - mb[ptr-1].offset);
230 mb[1].block = mb[ptr].block;
231 mb[1].dirty = mb[ptr].dirty;
232 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
234 r_offset = mb[ptr].offset;
238 border = get_border (is, mb, ptr, cat, firstpos);
245 cmp = (*is->method->compare_item)(i_item, f_item);
246 if (cmp == 0) /* insert i=f */
248 if (!i_mode) /* delete item? */
252 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
254 /* is next input item the same as current except
255 for the delete flag? */
256 cmp = (*is->method->compare_item)(i_item, f_item);
259 /* yes! insert as if it was an insert only */
260 memcpy (r_item, i_item, i_item_ptr - i_item);
262 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
267 /* no! delete the item */
274 memcpy (r_item, f_item, f_item_ptr - f_item);
278 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
283 f_more = isc_read_item (pp, &f_item_ptr);
285 else if (cmp > 0) /* insert f */
287 memcpy (r_item, f_item, f_item_ptr - f_item);
290 f_more = isc_read_item (pp, &f_item_ptr);
294 if (!i_mode) /* delete item which isn't there? */
296 logf (LOG_FATAL, "Inconsistent register at offset %d",
300 memcpy (r_item, i_item, i_item_ptr - i_item);
304 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
307 if (r_item) /* insert resulting item? */
309 char *r_out_ptr = r_buf + r_offset;
312 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
313 &r_out_ptr, &r_item);
314 new_offset = r_out_ptr - r_buf;
318 if (border < new_offset && border >= r_offset)
320 if (is->method->debug > 2)
321 logf (LOG_LOG, "isc: border %d %d", ptr, border);
322 /* Max size of current block category reached ...
323 make new virtual block entry */
326 mb[ptr].offset = r_offset;
327 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
329 /* We are dealing with block(s) of max size. Block(s)
330 except one will be flushed. Note: the block(s) are
331 surely not the last one(s).
333 if (is->method->debug > 2)
334 logf (LOG_LOG, "isc: flush B %d sections", ptr-1);
335 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
338 mb[0].block = mb[ptr-1].block;
339 mb[0].dirty = mb[ptr-1].dirty;
340 memcpy (r_buf, r_buf + mb[ptr-1].offset,
341 mb[ptr].offset - mb[ptr-1].offset);
344 mb[1].block = mb[ptr].block;
345 mb[1].dirty = mb[0].dirty;
346 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
347 memcpy (r_buf + mb[1].offset, r_buf + r_offset,
348 new_offset - r_offset);
349 new_offset = (new_offset - r_offset) + mb[1].offset;
352 border = get_border (is, mb, ptr, cat, firstpos);
354 r_offset = new_offset;
356 if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
358 /* Max number blocks in current category reached ->
359 must switch to next category (with larger block size)
363 (is->files[cat].no_remap)++;
364 /* delete all original block(s) read so far */
365 for (i = 0; i < ptr; i++)
367 isc_release_block (is, pp->cat, mb[i].block);
368 /* also delete all block to be read in the future */
371 /* remap block offsets */
372 assert (mb[j].offset == 0);
376 for (i = 1; i < ptr; i++)
378 int border = is->method->filecat[cat].ifill -
379 ISAMC_BLOCK_OFFSET_1 + mb[j].offset;
380 if (is->method->debug > 3)
381 logf (LOG_LOG, "isc: remap %d border=%d", i, border);
382 if (mb[i+1].offset > border && mb[i].offset <= border)
384 if (is->method->debug > 3)
385 logf (LOG_LOG, "isc: to %d %d", j, mb[i].offset);
388 mb[j].offset = mb[i].offset;
391 if (is->method->debug > 2)
392 logf (LOG_LOG, "isc: remap from %d to %d sections to cat %d",
395 border = get_border (is, mb, ptr, cat, firstpos);
398 if (mb[ptr].offset < r_offset)
399 { /* make the final boundary offset */
402 mb[ptr].offset = r_offset;
405 { /* empty output. Release last block if any */
406 if (cat == pp->cat && mb[ptr].block)
408 if (is->method->debug > 3)
409 logf (LOG_LOG, "isc: release C");
410 isc_release_block (is, pp->cat, mb[ptr].block);
416 if (is->method->debug > 2)
417 logf (LOG_LOG, "isc: flush C, %d sections", ptr);
421 /* we have to patch initial block with num keys if that
423 if (numKeys != isc_pp_num (pp))
425 if (is->method->debug > 2)
426 logf (LOG_LOG, "isc: patch num keys firstpos=%d num=%d",
428 bf_write (is->files[cat].bf, firstpos, ISAMC_BLOCK_OFFSET_N,
429 sizeof(numKeys), &numKeys);
433 { /* we haven't flushed initial block yet and there surely are some
434 blocks to flush. Make first block dirty if numKeys differ */
435 if (numKeys != isc_pp_num (pp))
438 /* flush rest of block(s) in r_buf */
439 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1, &numKeys);
441 (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
444 if (is->method->debug > 1)
445 logf (LOG_LOG, "isc: isc_merge return %d %d", cat, firstpos);
447 return cat + firstpos * 8;