2 * Copyright (c) 1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1996-11-04 14:08:59 adam
8 * Optimized free block usage.
10 * Revision 1.2 1996/11/01 13:36:46 adam
11 * New element, max_blocks_mem, that control how many blocks of max size
12 * to store in memory during isc_merge.
13 * Function isc_merge now ignores delete/update of identical keys and
14 * the proper blocks are then non-dirty and not written in flush_blocks.
16 * Revision 1.1 1996/11/01 08:59:15 adam
17 * First version of isc_merge that supports update/delete.
29 struct isc_merge_block {
30 int offset; /* offset in r_buf */
31 int block; /* block number of file (0 if none) */
32 int dirty; /* block is different from that on file */
35 static void flush_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
36 char *r_buf, int *firstpos, int cat, int last)
40 for (i = 0; i<ptr; i++)
42 /* skip rest if not dirty */
47 *firstpos = mb[i].block;
48 if (is->method->debug > 2)
49 logf (LOG_LOG, "isc: skip block %d %d", cat, mb[i].block);
50 ++(is->files[cat].no_skip_writes);
53 /* consider this block number */
56 mb[i].block = isc_alloc_block (is, cat);
58 *firstpos = mb[i].block;
60 /* consider next block pointer */
61 if (last && i == ptr-1)
63 else if (!mb[i+1].block)
64 mb[i+1].block = isc_alloc_block (is, cat);
67 assert (mb[i+1].offset > mb[i].offset);
68 isc_write_dblock (is, cat, mb[i].block, r_buf + mb[i].offset,
69 mb[i+1].block, mb[i+1].offset - mb[i].offset);
73 ISAMC_P isc_merge (ISAMC is, ISAMC_P ipos, ISAMC_I data)
76 char i_item[128], *i_item_ptr;
77 int i_more, i_mode, i;
80 char f_item[128], *f_item_ptr;
83 struct isc_merge_block mb[100];
86 char r_item_buf[128]; /* temporary result output */
87 char *r_buf; /* block with resulting data */
88 int r_offset = 0; /* current offset in r_buf */
89 int ptr = 0; /* pointer */
90 void *r_clientData; /* encode client data */
92 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
93 r_buf = is->merge_buf + ISAMC_BLOCK_OFFSET;
95 pp = isc_pp_open (is, ipos);
96 /* read first item from file. make sure f_more indicates no boundary */
98 f_more = isc_read_item (pp, &f_item_ptr);
103 if (is->method->debug > 1)
104 logf (LOG_LOG, "isc: isc_merge begin %d %d", cat, pp->pos);
106 /* read first item from i */
108 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
110 mb[ptr].block = pp->pos; /* is zero if no block on disk */
114 while (i_more || f_more)
116 char *r_item = r_item_buf;
121 /* block to block boundary in the original file. */
125 /* the resulting output is of the same category as the
128 if (mb[ptr].offset == r_offset)
130 /* the resulting output block is empty. Delete
131 the original (if any)
133 if (is->method->debug > 3)
134 logf (LOG_LOG, "isc: release A");
136 isc_release_block (is, pp->cat, mb[ptr].block);
137 mb[ptr].block = pp->pos;
142 /* indicate new boundary based on the original file */
143 mb[++ptr].block = pp->pos;
145 mb[ptr].offset = r_offset;
147 if (r_item && cat == is->max_cat)
149 /* We are dealing with block(s) of max size. Block(s)
150 will be flushed. Note: the block(s) are surely not
153 if (is->method->debug > 2)
154 logf (LOG_LOG, "isc: flush A %d sections", ptr);
155 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat, 0);
157 mb[ptr].block = pp->pos;
170 cmp = (*is->method->compare_item)(i_item, f_item);
171 if (cmp == 0) /* insert i=f */
173 if (!i_mode) /* delete item? */
177 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
179 /* is next input item the same as current except
180 for the delete flag? */
181 cmp = (*is->method->compare_item)(i_item, f_item);
184 /* yes! insert as if it was an insert only */
185 memcpy (r_item, i_item, i_item_ptr - i_item);
187 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
192 /* no! delete the item */
199 memcpy (r_item, f_item, f_item_ptr - f_item);
203 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
208 f_more = isc_read_item (pp, &f_item_ptr);
210 else if (cmp > 0) /* insert f */
212 memcpy (r_item, f_item, f_item_ptr - f_item);
215 f_more = isc_read_item (pp, &f_item_ptr);
219 if (!i_mode) /* delete item which isn't there? */
221 logf (LOG_FATAL, "Inconsistent register at offset %d",
225 memcpy (r_item, i_item, i_item_ptr - i_item);
229 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
232 if (r_item) /* insert resulting item? */
234 char *r_out_ptr = r_buf + r_offset;
238 /* border set to initial fill or block size depending on
239 whether we are creating a new one or updating and old one
242 border = mb[ptr].offset + is->method->filecat[cat].bsize
245 border = mb[ptr].offset + is->method->filecat[cat].ifill
248 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
249 &r_out_ptr, &r_item);
250 new_offset = r_out_ptr - r_buf;
252 if (border < new_offset && border >= r_offset)
254 if (is->method->debug > 2)
255 logf (LOG_LOG, "isc: border %d %d", ptr, border);
256 /* Max size of current block category reached ...
257 make new virtual block entry */
260 mb[ptr].offset = r_offset;
261 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
263 /* We are dealing with block(s) of max size. Block(s)
264 except one will be flushed. Note: the block(s) are
265 surely not the last one(s).
267 if (is->method->debug > 2)
268 logf (LOG_LOG, "isc: flush B %d sections", ptr-1);
269 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat, 0);
271 mb[0].block = mb[ptr-1].block;
272 mb[0].dirty = mb[ptr-1].dirty;
273 memcpy (r_buf, r_buf + mb[ptr-1].offset,
274 mb[ptr].offset - mb[ptr-1].offset);
277 mb[1].block = mb[ptr].block;
278 mb[1].dirty = mb[0].dirty;
279 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
280 memcpy (r_buf + mb[1].offset, r_buf + r_offset,
281 new_offset - r_offset);
282 new_offset = (new_offset - r_offset) + mb[1].offset;
286 r_offset = new_offset;
288 if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
290 /* Max number blocks in current category reached ->
291 must switch to next category (with larger block size)
295 (is->files[cat].no_remap)++;
296 /* delete all original block(s) read so far */
297 for (i = 0; i < ptr; i++)
299 isc_release_block (is, pp->cat, mb[i].block);
300 /* also delete all block to be read in the future */
303 /* remap block offsets */
304 assert (mb[j].offset == 0);
308 for (i = 1; i < ptr; i++)
310 int border = is->method->filecat[cat].ifill -
311 ISAMC_BLOCK_OFFSET + mb[j].offset;
312 if (is->method->debug > 3)
313 logf (LOG_LOG, "isc: remap %d border=%d", i, border);
314 if (mb[i+1].offset > border && mb[i].offset <= border)
316 if (is->method->debug > 3)
317 logf (LOG_LOG, "isc: to %d %d", j, mb[i].offset);
320 mb[j].offset = mb[i].offset;
323 if (is->method->debug > 2)
324 logf (LOG_LOG, "isc: remap from %d to %d sections to cat %d",
329 if (mb[ptr].offset < r_offset)
330 { /* make the final boundary offset */
331 mb[++ptr].dirty = 1; /* ignored by flush_blocks */
332 mb[ptr].block = 0; /* ignored by flush_blocks */
333 mb[ptr].offset = r_offset;
336 { /* empty output. Release last block if any */
337 if (cat == pp->cat && mb[ptr].block)
339 if (is->method->debug > 3)
340 logf (LOG_LOG, "isc: release C");
341 isc_release_block (is, pp->cat, mb[ptr].block);
347 if (is->method->debug > 2)
348 logf (LOG_LOG, "isc: flush C, %d sections", ptr);
350 /* flush rest of block(s) in r_buf */
351 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1);
353 (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
356 if (is->method->debug > 1)
357 logf (LOG_LOG, "isc: isc_merge return %d %d", cat, firstpos);
358 return cat + firstpos * 8;