Attempt at fixing bug. Not all blocks were read before they were unlinked
[idzebra-moved-to-github.git] / isam / isam.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: isam.c,v $
7  * Revision 1.18  1996-02-06 10:19:56  quinn
8  * Attempt at fixing bug. Not all blocks were read before they were unlinked
9  * prior to a remap operation.
10  *
11  * Revision 1.17  1995/12/06  15:48:44  quinn
12  * Fixed update-problem.
13  *
14  * Revision 1.16  1995/12/06  14:48:26  quinn
15  * Fixed some strange bugs.
16  *
17  * Revision 1.15  1995/12/06  09:59:45  quinn
18  * Fixed memory-consumption bug in memory.c
19  * Added more blocksizes to the default ISAM configuration.
20  *
21  * Revision 1.14  1995/11/24  17:26:19  quinn
22  * Mostly about making some ISAM stuff in the config file optional.
23  *
24  * Revision 1.13  1995/10/17  18:03:15  adam
25  * Commented out qsort in is_merge.
26  *
27  * Revision 1.12  1995/09/06  16:11:41  adam
28  * Keysize parameter to is_open (if non-zero).
29  *
30  * Revision 1.11  1995/09/04  12:33:46  adam
31  * Various cleanup. YAZ util used instead.
32  *
33  * Revision 1.10  1994/09/28  16:58:32  quinn
34  * Small mod.
35  *
36  * Revision 1.9  1994/09/28  12:56:15  quinn
37  * Added access functions (ISPT)
38  *
39  * Revision 1.8  1994/09/28  12:32:17  quinn
40  * Trivial
41  *
42  * Revision 1.7  1994/09/28  11:56:25  quinn
43  * Added sort of input to is_merge
44  *
45  * Revision 1.6  1994/09/28  11:29:33  quinn
46  * Added cmp parameter.
47  *
48  * Revision 1.5  1994/09/27  20:03:50  quinn
49  * Seems relatively bug-free.
50  *
51  * Revision 1.4  1994/09/26  17:11:29  quinn
52  * Trivial
53  *
54  * Revision 1.3  1994/09/26  17:06:35  quinn
55  * Back again...
56  *
57  * Revision 1.1  1994/09/12  08:02:13  quinn
58  * Not functional yet
59  *
60  */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <ctype.h>
66
67 #include <alexutil.h>
68 #include <bfile.h>
69 #include <isam.h>
70 #include <common.h>
71 #include "isutil.h"
72 #include "rootblk.h"
73 #include "keyops.h"
74
75 static int (*extcmp)(const void *p1, const void *p2);
76 static ispt_struct *ispt_freelist = 0;
77
78 static ISPT ispt_alloc()
79 {
80     ISPT p;
81
82     if (ispt_freelist)
83     {
84         p = ispt_freelist;
85         ispt_freelist = ispt_freelist->next;
86     }
87     else
88         p = xmalloc(sizeof(ispt_struct));
89     return p;
90 }
91
92 static void ispt_free(ISPT pt)
93 {
94     pt->next = ispt_freelist;
95     ispt_freelist = pt;
96 }
97
98 static int splitargs(const char *s, char *bf[], int max)
99 {
100     int ct = 0;
101     for (;;)
102     {
103         while (*s && isspace(*s))
104             s++;
105         bf[ct] = (char *) s;
106         if (!*s)
107                 return ct;
108         ct++;
109         if (ct > max)
110         {
111             logf (LOG_WARN, "Ignoring extra args to is resource");
112             bf[ct] = '\0';
113             return(ct - 1);
114         }
115         while (*s && !isspace(*s))
116             s++;
117     }
118 }
119
120 /*
121  * Open isam file.
122  * Process resources.
123  */
124 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
125     int writeflag, int keysize)
126 {
127     ISAM new;
128     char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
129     int num, size, rs, tmp, i;
130     is_type_header th;
131
132     logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
133     new = xmalloc(sizeof(*new));
134     new->writeflag = writeflag;
135     for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
136         new->types[i].index = 0;                        /* dummy */
137
138     /* determine number and size of blocktypes */
139     if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
140         "blocktypes", 0), "64 512 4K 32K")) ||
141         !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
142     {
143         logf (LOG_FATAL, "Failed to locate resource %s", nm);
144         return 0;
145     }
146     new->num_types = num;
147     for (i = 0; i < num; i++)
148     {
149         if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
150         {
151             logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
152             return 0;
153         }
154         if (rs == 1)
155                 *m = 'b';
156         switch (*m)
157         {
158                 case 'b': case 'B':
159                     new->types[i].blocksize = size; break;
160                 case 'k': case 'K':
161                     new->types[i].blocksize = size * 1024; break;
162                 case 'm': case 'M':
163                     new->types[i].blocksize = size * 1048576; break;
164                 default:
165                     logf (LOG_FATAL, "Illegal size suffix: %c", *m);
166                     return 0;
167         }
168         new->types[i].dbuf = xmalloc(new->types[i].blocksize);
169         m[0] = 'A' + i;
170         m[1] = '\0';
171         if (!(new->types[i].bf = bf_open(strconcat(name, m, 0), 
172             new->types[i].blocksize, writeflag)))
173         {
174             logf (LOG_FATAL, "bf_open failed");
175             return 0;
176         }
177         if ((rs = is_rb_read(&new->types[i], &th)) > 0)
178         {
179             if (th.blocksize != new->types[i].blocksize)
180             {
181                 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
182                 exit(1);
183             }
184             new->types[i].freelist = th.freelist;
185             new->types[i].top = th.top;
186         }
187         else if (writeflag) /* write dummy superblock to determine top */
188         {
189             if ((rs = is_rb_write(&new->types[i], &th)) <=0)  /* dummy */
190             {
191                 logf (LOG_FATAL, "Failed to write initial superblock.");
192                 exit(1);
193             }
194             new->types[i].freelist = -1;
195             new->types[i].top = rs;
196         }
197         /* ELSE: this is an empty file opened in read-only mode. */
198     }
199     if (keysize > 0)
200         new->keysize = keysize;
201     else
202     {
203         if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
204                                                               "keysize",
205                                                               0), "4")))
206         {
207             logf (LOG_FATAL, "Failed to locate resource %s", nm);
208             return 0;
209         }
210         if ((new->keysize = atoi(r)) <= 0)
211         {
212             logf (LOG_FATAL, "Must specify positive keysize.");
213             return 0;
214         }
215     }
216
217     /* determine repack percent */
218     if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
219         0), IS_DEF_REPACK_PERCENT)))
220     {
221         logf (LOG_FATAL, "Failed to locate resource %s", nm);
222         return 0;
223     }
224     new->repack = atoi(r);
225
226     /* determine max keys/blocksize */
227     if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
228         "maxkeys", 0), "50 640 10000")) || !(num = splitargs(r, pp,
229         IS_MAX_BLOCKTYPES)))
230     {
231         logf (LOG_FATAL, "Failed to locate resource %s", nm);
232         return 0;
233     }
234     if (num < new->num_types -1)
235     {
236         logf (LOG_FATAL, "Not enough elements in %s", nm);
237         return 0;
238     }
239     for (i = 0; i < num; i++)
240     {
241         if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
242         {
243             logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
244             return 0;
245         }
246         new->types[i].max_keys = tmp;
247     }
248
249     /* determine max keys/block */
250     for (i = 0; i < new->num_types; i++)
251     {
252         if (!new->types[i].index)
253         {
254             new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
255                 sizeof(int)) / new->keysize;
256             new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
257                 sizeof(int)) / new->keysize;
258         }
259         else
260             new->types[i].max_keys_block = new->types[i].max_keys_block0 /
261                 new->keysize;
262         if (new->types[i].max_keys_block0 < 1)
263         {
264             logf (LOG_FATAL, "Blocksize too small in %s", name);
265             exit(1);
266         }
267     }
268
269     /* determine nice fill rates */
270     if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
271         "nicefill", 0), "90 90 90 95")) || !(num = splitargs(r, pp,
272         IS_MAX_BLOCKTYPES)))
273     {
274         logf (LOG_FATAL, "Failed to locate resource %s", nm);
275         return 0;
276     }
277     if (num < new->num_types)
278     {
279         logf (LOG_FATAL, "Not enough elements in %s", nm);
280         return 0;
281     }
282     for (i = 0; i < num; i++)
283     {
284         if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
285         {
286             logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
287             return 0;
288         }
289         new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
290             100;
291         if (new->types[i].nice_keys_block < 1)
292                 new->types[i].nice_keys_block = 1;
293     }
294
295     new->cmp = cmp ? cmp : is_default_cmp;
296     return new;
297 }
298
299 /*
300  * Close isam file.
301  */
302 int is_close(ISAM is)
303 {
304     int i;
305     is_type_header th;
306
307     logf (LOG_DEBUG, "is_close()");
308     for (i = 0; i < is->num_types; i++)
309     {
310         if (is->types[i].bf)
311         {
312             if (is->writeflag)
313             {
314                 th.blocksize = is->types[i].blocksize;
315                 th.keysize = is->keysize;
316                 th.freelist = is->types[i].freelist;
317                 th.top = is->types[i].top;
318                 if (is_rb_write(&is->types[i], &th) < 0)
319                 {
320                     logf (LOG_FATAL, "Failed to write headerblock");
321                     exit(1);
322                 }
323             }
324             bf_close(is->types[i].bf);
325         }
326     }
327     xfree(is);
328     return 0;
329 }
330
331 static ISAM_P is_address(int type, int pos)
332 {
333     ISAM_P r;
334
335     r = pos << 2;
336     r |= type;
337     return r;
338 }
339
340 int sort_input(const void *p1, const void *p2)
341 {
342     int rs;
343
344     if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
345         return rs;
346     return *((char *)p1) - *((char*)p2);
347 }
348
349 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
350 {
351     is_mtable tab;
352     int res;
353     char keybuf[IS_MAX_RECORD];
354     int oldnum, oldtype, i;
355     char operation, *record;
356
357     extcmp = is->cmp;
358 #if 0
359     qsort(data, num, is_keysize(is) + 1, sort_input);
360 #endif
361     is_m_establish_tab(is, &tab, pos);
362     if (pos)
363         if (is_m_read_full(&tab, tab.data) < 0)
364         {
365             logf (LOG_FATAL, "read_full failed");
366             exit(1);
367         }
368     oldnum = tab.num_records;
369     oldtype = tab.pos_type;
370     while (num)
371     {
372         operation = *(data)++;
373         record = (char*) data;
374         data += is_keysize(is);
375         num--;
376         while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
377         {
378             data += 1 + is_keysize(is);
379             num--;
380         }
381         if ((res = is_m_seek_record(&tab, record)) > 0)  /* no match */
382         {
383             if (operation == KEYOP_INSERT)
384             {
385                 logf (LOG_DEBUG, "XXInserting new record.");
386                 is_m_write_record(&tab, record);
387             }
388             else
389                 logf (LOG_DEBUG, "XXDeletion failed to find match.");
390         }
391         else /* match found */
392         {
393             if (operation == KEYOP_INSERT)
394             {
395                 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
396                 continue;
397             }
398             else if (operation == KEYOP_DELETE)
399             {
400                 /* try to avoid needlessly moving data */
401                 if (num && *(data) == KEYOP_INSERT)
402                 {
403                     /* next key is identical insert? - NOOP - skip it */
404                     if (!memcmp(record, data + 1, is_keysize(is)))
405                     {
406                         logf (LOG_DEBUG, "XXNoop delete. skipping.");
407                         data += 1 + is_keysize(is);
408                         num--;
409                         continue;
410                     }
411                     /* else check if next key can fit in this position */
412                     is_m_peek_record(&tab, keybuf);
413                     res = (*is->cmp)(data + 1, keybuf);
414                     if (res < 0)
415                     {
416                         logf (LOG_DEBUG, "XXReplacing record.");
417                         is_m_replace_record(&tab, data + 1);
418                         data += 1 + is_keysize(is);
419                         num--;
420                         continue;
421                     }
422                 }
423                 logf (LOG_DEBUG, "Deleting record.");
424                 is_m_delete_record(&tab);
425             }
426         }
427     }
428     i = tab.pos_type;
429     while (i < tab.is->num_types - 1 && tab.num_records >
430         tab.is->types[i].max_keys)
431         i++;
432     if (i != tab.pos_type)
433     {
434         /* read remaining blocks */
435         for (; tab.cur_mblock; tab.cur_mblock = tab.cur_mblock->next)
436             if (tab.cur_mblock->state < IS_MBSTATE_CLEAN)
437                 is_m_read_full(&tab, tab.cur_mblock);
438         is_p_unmap(&tab);
439         tab.pos_type = i;
440     }
441     if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
442         100) / oldnum > tab.is->repack)
443         is_p_remap(&tab);
444     else
445         is_p_align(&tab);
446     if (tab.data)
447     {
448         is_p_sync(&tab);
449         pos = is_address(tab.pos_type, tab.data->diskpos);
450     }
451     else
452         pos = 0;
453     is_m_release_tab(&tab);
454     return pos;
455 }
456
457 /*
458  * Locate a table of keys in an isam file. The ISPT is an individual
459  * position marker for that table.
460  */
461 ISPT is_position(ISAM is, ISAM_P pos)
462 {
463     ispt_struct *p;
464
465     p = ispt_alloc();
466     is_m_establish_tab(is, &p->tab, pos);
467     return p;
468 }
469
470 /*
471  * Release ISPT.
472  */
473 void is_pt_free(ISPT ip)
474 {
475     is_m_release_tab(&ip->tab);
476     ispt_free(ip);
477 }
478
479 /*
480  * Read a key from a table.
481  */
482 int is_readkey(ISPT ip, void *buf)
483 {
484     return is_m_read_record(&ip->tab, buf, 0);
485 }    
486
487 int is_numkeys(ISPT ip)
488 {
489     return is_m_num_records(&ip->tab);
490 }
491
492 void is_rewind(ISPT ip)
493 {
494     is_m_rewind(&ip->tab);
495 }