2 * Copyright (c) 1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1996-11-01 13:36:46 adam
8 * New element, max_blocks_mem, that control how many blocks of max size
9 * to store in memory during isc_merge.
10 * Function isc_merge now ignoreds delete/update of identical keys and
11 * the proper blocks are then non-dirty and not written in flush_blocks.
13 * Revision 1.1 1996/11/01 08:59:15 adam
14 * First version of isc_merge that supports update/delete.
26 struct isc_merge_block {
27 int offset; /* offset in r_buf */
28 int block; /* block number of file (0 if none) */
29 int dirty; /* block is different from that on file */
32 static void flush_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
33 char *r_buf, int *firstpos, int cat, int last)
37 for (i = 0; i<ptr; i++)
39 /* skip rest if not dirty */
44 *firstpos = mb[i].block;
45 if (is->method->debug > 2)
46 logf (LOG_LOG, "isc: skip block %d %d", cat, mb[i].block);
47 ++(is->files[cat].no_skip_writes);
50 /* consider this block number */
53 mb[i].block = isc_alloc_block (is, cat);
55 *firstpos = mb[i].block;
57 /* consider next block pointer */
58 if (last && i == ptr-1)
60 else if (!mb[i+1].block)
61 mb[i+1].block = isc_alloc_block (is, cat);
64 assert (mb[i+1].offset > mb[i].offset);
65 isc_write_dblock (is, cat, mb[i].block, r_buf + mb[i].offset,
66 mb[i+1].block, mb[i+1].offset - mb[i].offset);
70 ISAMC_P isc_merge (ISAMC is, ISAMC_P ipos, ISAMC_I data)
73 char i_item[128], *i_item_ptr;
74 int i_more, i_mode, i;
77 char f_item[128], *f_item_ptr;
80 struct isc_merge_block mb[100];
83 char r_item_buf[128]; /* temporary result output */
84 char *r_buf; /* block with resulting data */
85 int r_offset = 0; /* current offset in r_buf */
86 int ptr = 0; /* pointer */
87 void *r_clientData; /* encode client data */
89 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
90 r_buf = is->merge_buf + ISAMC_BLOCK_OFFSET;
92 pp = isc_pp_open (is, ipos);
93 /* read first item from file. make sure f_more indicates no boundary */
95 f_more = isc_read_item (pp, &f_item_ptr);
100 if (is->method->debug > 1)
101 logf (LOG_LOG, "isc: isc_merge begin %d %d", cat, pp->pos);
103 /* read first item from i */
105 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
107 mb[ptr].block = pp->pos; /* is zero if no block on disk */
111 while (i_more || f_more)
113 char *r_item = r_item_buf;
118 /* block to block boundary in the original file. */
122 /* the resulting output is of the same category as the
125 if (mb[ptr].offset == r_offset)
127 /* the resulting output block is empty. Delete
128 the original (if any)
130 if (is->method->debug > 3)
131 logf (LOG_LOG, "isc: release A");
133 isc_release_block (is, pp->cat, mb[ptr].block);
134 mb[ptr].block = pp->pos;
139 /* indicate new boundary based on the original file */
140 mb[++ptr].block = pp->pos;
142 mb[ptr].offset = r_offset;
144 if (r_item && cat == is->max_cat)
146 /* We are dealing with block(s) of max size. Block(s)
147 will be flushed. Note: the block(s) are surely not
150 if (is->method->debug > 2)
151 logf (LOG_LOG, "isc: flush A %d sections", ptr);
152 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat, 0);
154 mb[ptr].block = pp->pos;
167 cmp = (*is->method->compare_item)(i_item, f_item);
168 if (cmp == 0) /* insert i=f */
170 if (!i_mode) /* delete item? */
174 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
176 /* it next input item the same as current except
177 for the delete flag? */
178 cmp = (*is->method->compare_item)(i_item, f_item);
181 /* yes! insert as if it was an insert only */
182 memcpy (r_item, i_item, i_item_ptr - i_item);
184 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
189 /* no! delete the item */
196 memcpy (r_item, f_item, f_item_ptr - f_item);
200 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
205 f_more = isc_read_item (pp, &f_item_ptr);
207 else if (cmp > 0) /* insert f */
209 memcpy (r_item, f_item, f_item_ptr - f_item);
212 f_more = isc_read_item (pp, &f_item_ptr);
216 if (!i_mode) /* delete item which isn't there? */
218 logf (LOG_FATAL, "Inconsistent register at offset %d",
222 memcpy (r_item, i_item, i_item_ptr - i_item);
226 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
229 if (r_item) /* insert resulting item? */
231 char *r_out_ptr = r_buf + r_offset;
235 /* border set to initial fill or block size depending on
236 whether we are creating a new one or updating and old one
239 border = mb[ptr].offset + is->method->filecat[cat].bsize
242 border = mb[ptr].offset + is->method->filecat[cat].ifill
245 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
246 &r_out_ptr, &r_item);
247 new_offset = r_out_ptr - r_buf;
249 if (border >= r_offset && border < new_offset)
251 if (is->method->debug > 2)
252 logf (LOG_LOG, "isc: border %d %d", ptr, border);
253 /* Max size of current block category reached ...
254 make new virtual block entry */
257 mb[ptr].offset = r_offset;
258 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
260 /* We are dealing with block(s) of max size. Block(s)
261 except one will be flushed. Note: the block(s) are
262 surely not the last one(s).
264 if (is->method->debug > 2)
265 logf (LOG_LOG, "isc: flush B %d sections", ptr-1);
266 flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat, 0);
268 mb[0].block = mb[ptr-1].block;
269 mb[0].dirty = mb[ptr-1].dirty;
270 memcpy (r_buf, r_buf + mb[ptr-1].offset,
271 mb[ptr].offset - mb[ptr-1].offset);
274 mb[1].block = mb[ptr].block;
275 mb[1].dirty = mb[0].dirty;
276 mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
277 memcpy (r_buf + mb[1].offset, r_buf + r_offset,
278 new_offset - r_offset);
279 new_offset = (new_offset - r_offset) + mb[1].offset;
283 r_offset = new_offset;
285 if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
287 /* Max number blocks in current category reached ->
288 must switch to next category (with larger block size)
292 (is->files[cat].no_remap)++;
293 /* delete all original block(s) read so far */
294 for (i = 0; i < ptr; i++)
296 isc_release_block (is, pp->cat, mb[i].block);
297 /* also delete all block to be read in the future */
300 /* remap block offsets */
301 assert (mb[j].offset == 0);
305 for (i = 1; i < ptr; i++)
307 int border = is->method->filecat[cat].ifill -
308 ISAMC_BLOCK_OFFSET + mb[j].offset;
309 if (is->method->debug > 3)
310 logf (LOG_LOG, "isc: remap %d border=%d", i, border);
311 if (mb[i+1].offset > border && mb[i].offset <= border)
313 if (is->method->debug > 3)
314 logf (LOG_LOG, "isc: to %d %d", j, mb[i].offset);
317 mb[j].offset = mb[i].offset;
320 if (is->method->debug > 2)
321 logf (LOG_LOG, "isc: remap from %d to %d sections to cat %d",
326 if (mb[ptr].offset < r_offset)
327 { /* make the final boundary offset */
328 mb[++ptr].dirty = 1; /* ignored by flush_blocks */
329 mb[ptr].block = 0; /* ignored by flush_blocks */
330 mb[ptr].offset = r_offset;
333 { /* empty output. Release last block if any */
334 if (cat == pp->cat && mb[ptr].block)
336 if (is->method->debug > 3)
337 logf (LOG_LOG, "isc: release C");
338 isc_release_block (is, pp->cat, mb[ptr].block);
344 if (is->method->debug > 2)
345 logf (LOG_LOG, "isc: flush C, %d sections", ptr);
347 /* flush rest of block(s) in r_buf */
348 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1);
350 (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
353 if (is->method->debug > 1)
354 logf (LOG_LOG, "isc: isc_merge return %d %d", cat, firstpos);
355 return cat + firstpos * 8;