Optimized free block usage.
[idzebra-moved-to-github.git] / isamc / merge.c
1 /*
2  * Copyright (c) 1996, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: merge.c,v $
7  * Revision 1.3  1996-11-04 14:08:59  adam
8  * Optimized free block usage.
9  *
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.
15  *
16  * Revision 1.1  1996/11/01  08:59:15  adam
17  * First version of isc_merge that supports update/delete.
18  *
19  */
20
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <string.h>
24 #include <stdio.h>
25
26 #include <log.h>
27 #include "isamc-p.h"
28
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 */
33 };
34
35 static void flush_blocks (ISAMC is, struct isc_merge_block *mb, int ptr,
36                           char *r_buf, int *firstpos, int cat, int last)
37 {
38     int i;
39
40     for (i = 0; i<ptr; i++)
41     {
42         /* skip rest if not dirty */
43         if (!mb[i].dirty)
44         {
45             assert (mb[i].block);
46             if (!*firstpos)
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);
51             continue;
52         }
53         /* consider this block number */
54
55         if (!mb[i].block) 
56             mb[i].block = isc_alloc_block (is, cat);
57         if (!*firstpos)
58             *firstpos = mb[i].block;
59
60         /* consider next block pointer */
61         if (last && i == ptr-1)
62             mb[i+1].block = 0;
63         else if (!mb[i+1].block)       
64             mb[i+1].block = isc_alloc_block (is, cat);
65
66         /* write block */
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);
70     }
71 }
72
73 ISAMC_P isc_merge (ISAMC is, ISAMC_P ipos, ISAMC_I data)
74 {
75
76     char i_item[128], *i_item_ptr;
77     int i_more, i_mode, i;
78
79     ISAMC_PP pp; 
80     char f_item[128], *f_item_ptr;
81     int f_more;
82  
83     struct isc_merge_block mb[100];
84     int firstpos = 0;
85     int cat = 0;
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 */
91
92     r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
93     r_buf = is->merge_buf + ISAMC_BLOCK_OFFSET;
94
95     pp = isc_pp_open (is, ipos);
96     /* read first item from file. make sure f_more indicates no boundary */
97     f_item_ptr = f_item;
98     f_more = isc_read_item (pp, &f_item_ptr);
99     if (f_more > 0)
100         f_more = 1;
101     cat = pp->cat;
102
103     if (is->method->debug > 1)
104         logf (LOG_LOG, "isc: isc_merge begin %d %d", cat, pp->pos);
105
106     /* read first item from i */
107     i_item_ptr = i_item;
108     i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
109
110     mb[ptr].block = pp->pos;     /* is zero if no block on disk */
111     mb[ptr].dirty = 0;
112     mb[ptr].offset = 0;
113
114     while (i_more || f_more)
115     {
116         char *r_item = r_item_buf;
117         int cmp;
118
119         if (f_more > 1)
120         {
121             /* block to block boundary in the original file. */
122             f_more = 1;
123             if (cat == pp->cat) 
124             {
125                 /* the resulting output is of the same category as the
126                    the original 
127                  */
128                 if (mb[ptr].offset == r_offset)
129                 {
130                     /* the resulting output block is empty. Delete
131                        the original (if any)
132                      */
133                     if (is->method->debug > 3)
134                         logf (LOG_LOG, "isc: release A");
135                     if (mb[ptr].block)
136                         isc_release_block (is, pp->cat, mb[ptr].block);
137                     mb[ptr].block = pp->pos;
138                     mb[ptr].dirty = 0;
139                 }
140                 else
141                 {
142                     /* indicate new boundary based on the original file */
143                     mb[++ptr].block = pp->pos;
144                     mb[ptr].dirty = 0;
145                     mb[ptr].offset = r_offset;
146 #if 0
147                     if (r_item && cat == is->max_cat)
148                     {
149                         /* We are dealing with block(s) of max size. Block(s)
150                            will be flushed. Note: the block(s) are surely not
151                            the last one(s).
152                          */
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);
156                         ptr = 0;
157                         mb[ptr].block = pp->pos;
158                         mb[ptr].dirty = 0;
159                         mb[ptr].offset = 0;
160                     }
161 #endif
162                 }
163             }
164         }
165         if (!f_more)
166             cmp = -1;
167         else if (!i_more)
168             cmp = 1;
169         else
170             cmp = (*is->method->compare_item)(i_item, f_item);
171         if (cmp == 0)                   /* insert i=f */
172         {
173             if (!i_mode)   /* delete item? */
174             {
175                 /* move i */
176                 i_item_ptr = i_item;
177                 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
178                                            &i_mode);
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);
182                 if (!cmp && i_mode)
183                 {
184                     /* yes! insert as if it was an insert only */
185                     memcpy (r_item, i_item, i_item_ptr - i_item);
186                     i_item_ptr = i_item;
187                     i_more = (*data->read_item)(data->clientData, &i_item_ptr,
188                                                 &i_mode);
189                 }
190                 else
191                 {
192                     /* no! delete the item */
193                     r_item = NULL;
194                     mb[ptr].dirty = 1;
195                 }
196             }
197             else
198             {
199                 memcpy (r_item, f_item, f_item_ptr - f_item);
200
201                 /* move i */
202                 i_item_ptr = i_item;
203                 i_more = (*data->read_item)(data->clientData, &i_item_ptr,
204                                            &i_mode);
205             }
206             /* move f */
207             f_item_ptr = f_item;
208             f_more = isc_read_item (pp, &f_item_ptr);
209         }
210         else if (cmp > 0)               /* insert f */
211         {
212             memcpy (r_item, f_item, f_item_ptr - f_item);
213             /* move f */
214             f_item_ptr = f_item;
215             f_more = isc_read_item (pp, &f_item_ptr);
216         }
217         else                            /* insert i */
218         {
219             if (!i_mode)                /* delete item which isn't there? */
220             {
221                 logf (LOG_FATAL, "Inconsistent register at offset %d",
222                                  r_offset);
223                 abort ();
224             }
225             memcpy (r_item, i_item, i_item_ptr - i_item);
226             mb[ptr].dirty = 1;
227             /* move i */
228             i_item_ptr = i_item;
229             i_more = (*data->read_item)(data->clientData, &i_item_ptr,
230                                         &i_mode);
231         }
232         if (r_item)  /* insert resulting item? */
233         {
234             char *r_out_ptr = r_buf + r_offset;
235             int new_offset;
236             int border;
237
238             /* border set to initial fill or block size depending on
239                whether we are creating a new one or updating and old one
240              */
241             if (mb[ptr].block)
242                 border = mb[ptr].offset + is->method->filecat[cat].bsize
243                          -ISAMC_BLOCK_OFFSET;
244             else
245                 border = mb[ptr].offset + is->method->filecat[cat].ifill
246                          -ISAMC_BLOCK_OFFSET;
247
248             (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
249                                      &r_out_ptr, &r_item);
250             new_offset = r_out_ptr - r_buf; 
251
252             if (border < new_offset && border >= r_offset)
253             {
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 */
258                 mb[++ptr].block = 0;
259                 mb[ptr].dirty = 1;
260                 mb[ptr].offset = r_offset;
261                 if (cat == is->max_cat && ptr >= is->method->max_blocks_mem)
262                 {
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).
266                      */
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);
270
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);
275                     mb[0].offset = 0;
276
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;
283                     ptr = 1;
284                }
285             }
286             r_offset = new_offset;
287         }
288         if (cat < is->max_cat && ptr >= is->method->filecat[cat].mblocks)
289         {
290             /* Max number blocks in current category reached ->
291                must switch to next category (with larger block size) 
292             */
293             int j = 0;
294
295             (is->files[cat].no_remap)++;
296             /* delete all original block(s) read so far */
297             for (i = 0; i < ptr; i++)
298                 if (mb[i].block)
299                     isc_release_block (is, pp->cat, mb[i].block);
300             /* also delete all block to be read in the future */
301             pp->deleteFlag = 1;
302
303             /* remap block offsets */
304             assert (mb[j].offset == 0);
305             cat++;
306             mb[j].dirty = 1;
307             mb[j].block = 0;
308             for (i = 1; i < ptr; i++)
309             {
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)
315                 {
316                     if (is->method->debug > 3)
317                         logf (LOG_LOG, "isc:  to %d %d", j, mb[i].offset);
318                     mb[++j].dirty = 1;
319                     mb[j].block = 0;
320                     mb[j].offset = mb[i].offset;
321                 }
322             }
323             if (is->method->debug > 2)
324                 logf (LOG_LOG, "isc: remap from %d to %d sections to cat %d",
325                       ptr, j, cat);
326             ptr = j;
327         }
328     }
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;
334     }
335     else
336     {   /* empty output. Release last block if any */
337         if (cat == pp->cat && mb[ptr].block)
338         {
339             if (is->method->debug > 3)
340                 logf (LOG_LOG, "isc: release C");
341             isc_release_block (is, pp->cat, mb[ptr].block);
342             mb[ptr].block = 0;
343             mb[ptr].dirty = 1;
344         }
345     }
346
347     if (is->method->debug > 2)
348         logf (LOG_LOG, "isc: flush C, %d sections", ptr);
349
350     /* flush rest of block(s) in r_buf */
351     flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1);
352
353     (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
354     if (!firstpos)
355         cat = 0;
356     if (is->method->debug > 1)
357         logf (LOG_LOG, "isc: isc_merge return %d %d", cat, firstpos);
358     return cat + firstpos * 8;
359 }
360