Changed the isc_merge to take into account the mfill (minimum-fill).
[idzebra-moved-to-github.git] / isamc / merge.c
1 /*
2  * Copyright (c) 1996-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: merge.c,v $
7  * Revision 1.7  1998-03-11 11:18:18  adam
8  * Changed the isc_merge to take into account the mfill (minimum-fill).
9  *
10  * Revision 1.6  1998/03/06 13:54:03  adam
11  * Fixed two nasty bugs in isc_merge.
12  *
13  * Revision 1.5  1997/02/12 20:42:43  adam
14  * Bug fix: during isc_merge operations, some pages weren't marked dirty
15  * even though they should be. At this point the merge operation marks
16  * a page dirty if the previous page changed at all. A better approach is
17  * to mark it dirty if the last key written changed in previous page.
18  *
19  * Revision 1.4  1996/11/08 11:15:31  adam
20  * Number of keys in chain are stored in first block and the function
21  * to retrieve this information, isc_pp_num is implemented.
22  *
23  * Revision 1.3  1996/11/04 14:08:59  adam
24  * Optimized free block usage.
25  *
26  * Revision 1.2  1996/11/01 13:36:46  adam
27  * New element, max_blocks_mem, that control how many blocks of max size
28  * to store in memory during isc_merge.
29  * Function isc_merge now ignores delete/update of identical keys and
30  * the proper blocks are then non-dirty and not written in flush_blocks.
31  *
32  * Revision 1.1  1996/11/01  08:59:15  adam
33  * First version of isc_merge that supports update/delete.
34  *
35  */
36
37 #include <stdlib.h>
38 #include <assert.h>
39 #include <string.h>
40 #include <stdio.h>
41
42 #include <log.h>
43 #include "isamc-p.h"
44
45 struct isc_merge_block {
46     int offset;       /* offset in r_buf */
47     int block;        /* block number of file (0 if none) */
48     int dirty;        /* block is different from that on file */
49 };
50
51 static void flush_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
52                           char *r_buf, int *firstpos, int cat, int last,
53                           int *numkeys)
54 {
55     int i;
56
57     for (i = 0; i<ptr; i++)
58     {
59         unsigned short ssize;
60         char *src;
61
62         /* consider this block number */
63         if (!mb[i].block) 
64         {
65             mb[i].block = isc_alloc_block (is, cat);
66             mb[i].dirty = 1;
67         }
68
69         /* consider next block pointer */
70         if (last && i == ptr-1)
71             mb[i+1].block = 0;
72         else if (!mb[i+1].block)       
73         {
74             mb[i+1].block = isc_alloc_block (is, cat);
75             mb[i+1].dirty = 1;
76             mb[i].dirty = 1;
77         }
78
79         ssize = mb[i+1].offset - mb[i].offset;
80         assert (ssize);
81
82         /* skip rest if not dirty */
83         if (!mb[i].dirty)
84         {
85             assert (mb[i].block);
86             if (!*firstpos)
87                 *firstpos = mb[i].block;
88             if (is->method->debug > 2)
89                 logf (LOG_LOG, "isc: skip ptr=%d size=%d %d %d",
90                      i, ssize, cat, mb[i].block);
91             ++(is->files[cat].no_skip_writes);
92             continue;
93         }
94         /* write block */
95
96         if (!*firstpos)
97         {
98             *firstpos = mb[i].block;
99             src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_1;
100             ssize += ISAMC_BLOCK_OFFSET_1;
101
102             memcpy (src+sizeof(int)+sizeof(ssize), numkeys,
103                     sizeof(*numkeys));
104             if (is->method->debug > 2)
105                 logf (LOG_LOG, "isc: flush ptr=%d numk=%d size=%d nextpos=%d",
106                      i, *numkeys, (int) ssize, mb[i+1].block);
107         }
108         else
109         {
110             src = r_buf + mb[i].offset - ISAMC_BLOCK_OFFSET_N;
111             ssize += ISAMC_BLOCK_OFFSET_N;
112             if (is->method->debug > 2)
113                 logf (LOG_LOG, "isc: flush ptr=%d size=%d nextpos=%d",
114                      i, (int) ssize, mb[i+1].block);
115         }
116         memcpy (src, &mb[i+1].block, sizeof(int));
117         memcpy (src+sizeof(int), &ssize, sizeof(ssize));
118         isc_write_block (is, cat, mb[i].block, src);
119     }
120 }
121
122 static int get_border (ISAMC is, struct isc_merge_block *mb, int ptr,
123                        int cat, int firstpos)
124 {
125    /* Border set to initial fill or block size depending on
126       whether we are creating a new one or updating and old one.
127     */
128     
129     int fill = mb[ptr].block ? is->method->filecat[cat].bsize :
130                                is->method->filecat[cat].ifill;
131     int off = (ptr||firstpos) ? ISAMC_BLOCK_OFFSET_N : ISAMC_BLOCK_OFFSET_1;
132     
133     assert (ptr < 199);
134
135     return mb[ptr].offset + fill - off;
136 }
137
138 ISAMC_P isc_merge (ISAMC is, ISAMC_P ipos, ISAMC_I data)
139 {
140
141     char i_item[128], *i_item_ptr;
142     int i_more, i_mode, i;
143
144     ISAMC_PP pp; 
145     char f_item[128], *f_item_ptr;
146     int f_more;
147  
148     struct isc_merge_block mb[200];
149
150     int firstpos = 0;
151     int cat = 0;
152     char r_item_buf[128]; /* temporary result output */
153     char *r_buf;          /* block with resulting data */
154     int r_offset = 0;     /* current offset in r_buf */
155     int ptr = 0;          /* pointer */
156     void *r_clientData;   /* encode client data */
157     int border;
158     int numKeys = 0;
159
160     r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
161     r_buf = is->merge_buf + 128;
162
163     pp = isc_pp_open (is, ipos);
164     /* read first item from file. make sure f_more indicates no boundary */
165     f_item_ptr = f_item;
166     f_more = isc_read_item (pp, &f_item_ptr);
167     if (f_more > 0)
168         f_more = 1;
169     cat = pp->cat;
170
171     if (is->method->debug > 1)
172         logf (LOG_LOG, "isc: isc_merge begin %d %d", cat, pp->pos);
173
174     /* read first item from i */
175     i_item_ptr = i_item;
176     i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
177
178     mb[ptr].block = pp->pos;     /* is zero if no block on disk */
179     mb[ptr].dirty = 0;
180     mb[ptr].offset = 0;
181
182     border = get_border (is, mb, ptr, cat, firstpos);
183     while (i_more || f_more)
184     {
185         char *r_item = r_item_buf;
186         int cmp;
187
188         if (f_more > 1)
189         {
190             /* block to block boundary in the original file. */
191             f_more = 1;
192             if (cat == pp->cat) 
193             {
194                 /* the resulting output is of the same category as the
195                    the original 
196                 */
197                 if (r_offset <= mb[ptr].offset +is->method->filecat[cat].mfill)
198                 {
199                     /* the resulting output block is too small/empty. Delete
200                        the original (if any)
201                     */
202                     if (is->method->debug > 3)
203                         logf (LOG_LOG, "isc: release A");
204                     if (mb[ptr].block)
205                         isc_release_block (is, pp->cat, mb[ptr].block);
206                     mb[ptr].block = pp->pos;
207                     mb[ptr].dirty = 1;
208                     if (ptr > 0)
209                         mb[ptr-1].dirty = 1;
210                 }
211                 else
212                 {
213                     /* indicate new boundary based on the original file */
214                     mb[++ptr].block = pp->pos;
215                     mb[ptr].dirty = (mb[ptr-1].dirty > 1) ? 1 : 0;
216                     mb[ptr].offset = r_offset;
217                     if (is->method->debug > 3)
218                         logf (LOG_LOG, "isc: bound ptr=%d,offset=%d",
219                             ptr, r_offset);
220                     if (cat==is->max_cat && ptr >= is->method->max_blocks_mem)
221                     {
222                         /* We are dealing with block(s) of max size. Block(s)
223                            except 1 will be flushed.
224                          */
225                         if (is->method->debug > 2)
226                             logf (LOG_LOG, "isc: flush A %d sections", ptr);
227                         flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
228                                       0, &pp->numKeys);
229                         mb[0].block = mb[ptr-1].block;
230                         mb[0].dirty = mb[ptr-1].dirty;
231                         memcpy (r_buf, r_buf + mb[ptr-1].offset,
232                                 mb[ptr].offset - mb[ptr-1].offset);
233                         mb[0].offset = 0;
234
235                         mb[1].block = mb[ptr].block;
236                         mb[1].dirty = mb[ptr].dirty;
237                         mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
238                         ptr = 1;
239                         r_offset = mb[ptr].offset;
240                     }
241                 }
242             }
243             border = get_border (is, mb, ptr, cat, firstpos);
244         }
245         if (!f_more)
246             cmp = -1;
247         else if (!i_more)
248             cmp = 1;
249         else
250             cmp = (*is->method->compare_item)(i_item, f_item);
251         if (cmp == 0)                   /* insert i=f */
252         {
253             if (!i_mode)   /* delete item? */
254             {
255                 /* move i */
256                 i_item_ptr = i_item;
257                 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
258                                            &i_mode);
259                 /* is next input item the same as current except
260                    for the delete flag? */
261                 cmp = (*is->method->compare_item)(i_item, f_item);
262                 if (!cmp && i_mode)   /* delete/insert nop? */
263                 {
264                     /* yes! insert as if it was an insert only */
265                     memcpy (r_item, i_item, i_item_ptr - i_item);
266                     i_item_ptr = i_item;
267                     i_more = (*data->read_item)(data->clientData, &i_item_ptr,
268                                                 &i_mode);
269                 }
270                 else
271                 {
272                     /* no! delete the item */
273                     r_item = NULL;
274                     mb[ptr].dirty = 2;
275                 }
276             }
277             else
278             {
279                 memcpy (r_item, f_item, f_item_ptr - f_item);
280
281                 /* move i */
282                 i_item_ptr = i_item;
283                 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
284                                            &i_mode);
285             }
286             /* move f */
287             f_item_ptr = f_item;
288             f_more = isc_read_item (pp, &f_item_ptr);
289         }
290         else if (cmp > 0)               /* insert f */
291         {
292             memcpy (r_item, f_item, f_item_ptr - f_item);
293             /* move f */
294             f_item_ptr = f_item;
295             f_more = isc_read_item (pp, &f_item_ptr);
296         }
297         else                            /* insert i */
298         {
299             if (!i_mode)                /* delete item which isn't there? */
300             {
301                 logf (LOG_FATAL, "Inconsistent register at offset %d",
302                                  r_offset);
303                 abort ();
304             }
305             memcpy (r_item, i_item, i_item_ptr - i_item);
306             mb[ptr].dirty = 2;
307             /* move i */
308             i_item_ptr = i_item;
309             i_more = (*data->read_item)(data->clientData, &i_item_ptr,
310                                         &i_mode);
311         }
312         if (r_item)  /* insert resulting item? */
313         {
314             char *r_out_ptr = r_buf + r_offset;
315             int new_offset;
316
317             (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
318                                      &r_out_ptr, &r_item);
319             new_offset = r_out_ptr - r_buf; 
320
321             numKeys++;
322
323             if (border < new_offset && border >= r_offset)
324             {
325                 if (is->method->debug > 2)
326                     logf (LOG_LOG, "isc: border %d %d", ptr, border);
327                 /* Max size of current block category reached ...
328                    make new virtual block entry */
329                 mb[++ptr].block = 0;
330                 mb[ptr].dirty = 1;
331                 mb[ptr].offset = r_offset;
332                 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
333                 {
334                     /* We are dealing with block(s) of max size. Block(s)
335                        except one will be flushed. Note: the block(s) are
336                        surely not the last one(s).
337                      */
338                     if (is->method->debug > 2)
339                         logf (LOG_LOG, "isc: flush B %d sections", ptr-1);
340                     flush_blocks (is, mb, ptr-1, r_buf, &firstpos, cat,
341                                   0, &pp->numKeys);
342                     mb[0].block = mb[ptr-1].block;
343                     mb[0].dirty = mb[ptr-1].dirty;
344                     memcpy (r_buf, r_buf + mb[ptr-1].offset,
345                             mb[ptr].offset - mb[ptr-1].offset);
346                     mb[0].offset = 0;
347
348                     mb[1].block = mb[ptr].block;
349                     mb[1].dirty = mb[0].dirty;
350                     mb[1].offset = mb[ptr].offset - mb[ptr-1].offset;
351                     memcpy (r_buf + mb[1].offset, r_buf + r_offset,
352                             new_offset - r_offset);
353                     new_offset = (new_offset - r_offset) + mb[1].offset;
354                     ptr = 1;
355                 }
356                 border = get_border (is, mb, ptr, cat, firstpos);
357             }
358             r_offset = new_offset;
359         }
360         if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
361         {
362             /* Max number blocks in current category reached ->
363                must switch to next category (with larger block size) 
364             */
365             int j = 0;
366
367             (is->files[cat].no_remap)++;
368             /* delete all original block(s) read so far */
369             for (i = 0; i < ptr; i++)
370                 if (mb[i].block)
371                     isc_release_block (is, pp->cat, mb[i].block);
372             /* also delete all block to be read in the future */
373             pp->deleteFlag = 1;
374
375             /* remap block offsets */
376             assert (mb[j].offset == 0);
377             cat++;
378             mb[j].dirty = 1;
379             mb[j].block = 0;
380             for (i = 1; i < ptr; i++)
381             {
382                 int border = is->method->filecat[cat].ifill -
383                          ISAMC_BLOCK_OFFSET_1 + mb[j].offset;
384                 if (is->method->debug > 3)
385                     logf (LOG_LOG, "isc: remap %d border=%d", i, border);
386                 if (mb[i+1].offset > border && mb[i].offset <= border)
387                 {
388                     if (is->method->debug > 3)
389                         logf (LOG_LOG, "isc:  to %d %d", j, mb[i].offset);
390                     mb[++j].dirty = 1;
391                     mb[j].block = 0;
392                     mb[j].offset = mb[i].offset;
393                 }
394             }
395             if (is->method->debug > 2)
396                 logf (LOG_LOG, "isc: remap from %d to %d sections to cat %d",
397                       ptr, j, cat);
398             ptr = j;
399             border = get_border (is, mb, ptr, cat, firstpos);
400         }
401     }
402     if (mb[ptr].offset < r_offset)
403     {   /* make the final boundary offset */
404         mb[++ptr].dirty = 1; 
405         mb[ptr].block = 0; 
406         mb[ptr].offset = r_offset;
407     }
408     else
409     {   /* empty output. Release last block if any */
410         if (cat == pp->cat && mb[ptr].block)
411         {
412             if (is->method->debug > 3)
413                 logf (LOG_LOG, "isc: release C");
414             isc_release_block (is, pp->cat, mb[ptr].block);
415             mb[ptr].block = 0;
416             if (ptr > 0)
417                 mb[ptr-1].dirty = 1;
418         }
419     }
420
421     if (is->method->debug > 2)
422         logf (LOG_LOG, "isc: flush C, %d sections", ptr);
423
424     if (firstpos)
425     {
426         /* we have to patch initial block with num keys if that
427            has changed */
428         if (numKeys != isc_pp_num (pp))
429         {
430             if (is->method->debug > 2)
431                 logf (LOG_LOG, "isc: patch num keys firstpos=%d num=%d",
432                                 firstpos, numKeys);
433             bf_write (is->files[cat].bf, firstpos, ISAMC_BLOCK_OFFSET_N,
434                       sizeof(numKeys), &numKeys);
435         }
436     }
437     else if (ptr > 0)
438     {   /* we haven't flushed initial block yet and there surely are some
439            blocks to flush. Make first block dirty if numKeys differ */
440         if (numKeys != isc_pp_num (pp))
441             mb[0].dirty = 1;
442     }
443     /* flush rest of block(s) in r_buf */
444     flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1, &numKeys);
445
446     (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
447     if (!firstpos)
448         cat = 0;
449     if (is->method->debug > 1)
450         logf (LOG_LOG, "isc: isc_merge return %d %d", cat, firstpos);
451     isc_pp_close (pp);
452     return cat + firstpos * 8;
453 }
454