Improved installation. Updated for inclusion of YAZ header files.
[idzebra-moved-to-github.git] / isamc / isamc.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: isamc.c,v $
7  * Revision 1.20  1999-11-30 13:48:04  adam
8  * Improved installation. Updated for inclusion of YAZ header files.
9  *
10  * Revision 1.19  1999/07/14 10:59:27  adam
11  * Changed functions isc_getmethod, isams_getmethod.
12  * Improved fatal error handling (such as missing EXPLAIN schema).
13  *
14  * Revision 1.18  1999/06/30 09:08:23  adam
15  * Added coder to reset.
16  *
17  * Revision 1.17  1999/05/26 07:49:14  adam
18  * C++ compilation.
19  *
20  * Revision 1.16  1998/05/27 14:32:03  adam
21  * Changed default block category layout.
22  *
23  * Revision 1.15  1998/05/20 10:12:25  adam
24  * Implemented automatic EXPLAIN database maintenance.
25  * Modified Zebra to work with ASN.1 compiled version of YAZ.
26  *
27  * Revision 1.14  1998/03/19 10:04:35  adam
28  * Minor changes.
29  *
30  * Revision 1.13  1998/03/18 09:23:55  adam
31  * Blocks are stored in chunks on free list - up to factor 2 in speed.
32  * Fixed bug that could occur in block category rearrangemen.
33  *
34  * Revision 1.12  1998/03/16 10:37:24  adam
35  * Added more statistics.
36  *
37  * Revision 1.11  1998/03/13 15:30:50  adam
38  * New functions isc_block_used and isc_block_size. Fixed 'leak'
39  * in isc_alloc_block.
40  *
41  * Revision 1.10  1998/03/11 11:18:18  adam
42  * Changed the isc_merge to take into account the mfill (minimum-fill).
43  *
44  * Revision 1.9  1998/03/06 13:54:02  adam
45  * Fixed two nasty bugs in isc_merge.
46  *
47  * Revision 1.8  1997/09/17 12:19:20  adam
48  * Zebra version corresponds to YAZ version 1.4.
49  * Changed Zebra server so that it doesn't depend on global common_resource.
50  *
51  * Revision 1.7  1997/02/12 20:42:43  adam
52  * Bug fix: during isc_merge operations, some pages weren't marked dirty
53  * even though they should be. At this point the merge operation marks
54  * a page dirty if the previous page changed at all. A better approach is
55  * to mark it dirty if the last key written changed in previous page.
56  *
57  * Revision 1.6  1996/11/08 11:15:29  adam
58  * Number of keys in chain are stored in first block and the function
59  * to retrieve this information, isc_pp_num is implemented.
60  *
61  * Revision 1.5  1996/11/04 14:08:57  adam
62  * Optimized free block usage.
63  *
64  * Revision 1.4  1996/11/01 13:36:46  adam
65  * New element, max_blocks_mem, that control how many blocks of max size
66  * to store in memory during isc_merge.
67  * Function isc_merge now ignores delete/update of identical keys and
68  * the proper blocks are then non-dirty and not written in flush_blocks.
69  *
70  * Revision 1.3  1996/11/01  08:59:14  adam
71  * First version of isc_merge that supports update/delete.
72  *
73  * Revision 1.2  1996/10/29 16:44:56  adam
74  * Work on isc_merge.
75  *
76  * Revision 1.1  1996/10/29  13:40:48  adam
77  * First work.
78  *
79  */
80
81 /* 
82  * TODO:
83  *   Reduction to lower categories in isc_merge
84  */
85 #include <stdlib.h>
86 #include <assert.h>
87 #include <string.h>
88 #include <stdio.h>
89
90 #include <yaz/log.h>
91 #include "isamc-p.h"
92
93 static void flush_block (ISAMC is, int cat);
94 static void release_fc (ISAMC is, int cat);
95 static void init_fc (ISAMC is, int cat);
96
97 #define ISAMC_FREELIST_CHUNK 1
98
99 #define SMALL_TEST 0
100
101 void isc_getmethod (ISAMC_M m)
102 {
103
104     static struct ISAMC_filecat_s def_cat[] = {
105 #if SMALL_TEST
106         {    32,     28,      0,  3 },
107         {    64,     54,     30,  0 },
108 #else
109         {    24,     22,     18,  10 },
110         {   128,    120,    100,  10 },
111         {   512,    490,    350,  10 },
112         {  2048,   1900,   1700,  10 },
113         {  8192,   8000,   7900,  10 },
114         { 32768,  32000,  31000,  0 },
115 #endif
116     };
117     m->filecat = def_cat;
118
119     m->code_start = NULL;
120     m->code_item = NULL;
121     m->code_stop = NULL;
122     m->code_reset = NULL;
123
124     m->compare_item = NULL;
125
126     m->debug = 1;
127
128     m->max_blocks_mem = 10;
129 }
130
131 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
132 {
133     ISAMC is;
134     ISAMC_filecat filecat;
135     int i = 0;
136     int max_buf_size = 0;
137
138     is = (ISAMC) xmalloc (sizeof(*is));
139
140     is->method = (ISAMC_M) xmalloc (sizeof(*is->method));
141     memcpy (is->method, method, sizeof(*method));
142     filecat = is->method->filecat;
143     assert (filecat);
144
145     /* determine number of block categories */
146     if (is->method->debug)
147         logf (LOG_LOG, "isc: bsize  ifill  mfill mblocks");
148     do
149     {
150         if (is->method->debug)
151             logf (LOG_LOG, "isc:%6d %6d %6d %6d",
152                   filecat[i].bsize, filecat[i].ifill, 
153                   filecat[i].mfill, filecat[i].mblocks);
154         if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
155             max_buf_size = filecat[i].mblocks * filecat[i].bsize;
156     } while (filecat[i++].mblocks);
157     is->no_files = i;
158     is->max_cat = --i;
159     /* max_buf_size is the larget buffer to be used during merge */
160     max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
161     if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
162         max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
163     if (is->method->debug)
164         logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
165     
166     assert (is->no_files > 0);
167     is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
168     if (writeflag)
169     {
170         is->merge_buf = (char *) xmalloc (max_buf_size+256);
171         memset (is->merge_buf, 0, max_buf_size+256);
172     }
173     else
174         is->merge_buf = NULL;
175     for (i = 0; i<is->no_files; i++)
176     {
177         char fname[512];
178
179         sprintf (fname, "%s%c", name, i+'A');
180         is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
181                                    writeflag);
182         is->files[i].head_is_dirty = 0;
183         if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
184                      &is->files[i].head))
185         {
186             is->files[i].head.lastblock = 1;
187             is->files[i].head.freelist = 0;
188         }
189         is->files[i].alloc_entries_num = 0;
190         is->files[i].alloc_entries_max =
191             is->method->filecat[i].bsize / sizeof(int) - 1;
192         is->files[i].alloc_buf = (char *)
193             xmalloc (is->method->filecat[i].bsize);
194         is->files[i].no_writes = 0;
195         is->files[i].no_reads = 0;
196         is->files[i].no_skip_writes = 0;
197         is->files[i].no_allocated = 0;
198         is->files[i].no_released = 0;
199         is->files[i].no_remap = 0;
200         is->files[i].no_forward = 0;
201         is->files[i].no_backward = 0;
202         is->files[i].sum_forward = 0;
203         is->files[i].sum_backward = 0;
204         is->files[i].no_next = 0;
205         is->files[i].no_prev = 0;
206
207         init_fc (is, i);
208     }
209     return is;
210 }
211
212 int isc_block_used (ISAMC is, int type)
213 {
214     if (type < 0 || type >= is->no_files)
215         return -1;
216     return is->files[type].head.lastblock-1;
217 }
218
219 int isc_block_size (ISAMC is, int type)
220 {
221     ISAMC_filecat filecat = is->method->filecat;
222     if (type < 0 || type >= is->no_files)
223         return -1;
224     return filecat[type].bsize;
225 }
226
227 int isc_close (ISAMC is)
228 {
229     int i;
230
231     if (is->method->debug)
232     {
233         logf (LOG_LOG, "isc:    next    forw   mid-f    prev   backw   mid-b");
234         for (i = 0; i<is->no_files; i++)
235             logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
236                   is->files[i].no_next,
237                   is->files[i].no_forward,
238                   is->files[i].no_forward ?
239                   (double) is->files[i].sum_forward/is->files[i].no_forward
240                   : 0.0,
241                   is->files[i].no_prev,
242                   is->files[i].no_backward,
243                   is->files[i].no_backward ?
244                   (double) is->files[i].sum_backward/is->files[i].no_backward
245                   : 0.0);
246     }
247     if (is->method->debug)
248         logf (LOG_LOG, "isc:  writes   reads skipped   alloc released  remap");
249     for (i = 0; i<is->no_files; i++)
250     {
251         release_fc (is, i);
252         assert (is->files[i].bf);
253         if (is->files[i].head_is_dirty)
254             bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
255                  &is->files[i].head);
256         if (is->method->debug)
257             logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
258                   is->files[i].no_writes,
259                   is->files[i].no_reads,
260                   is->files[i].no_skip_writes,
261                   is->files[i].no_allocated,
262                   is->files[i].no_released,
263                   is->files[i].no_remap);
264         xfree (is->files[i].fc_list);
265         flush_block (is, i);
266         bf_close (is->files[i].bf);
267     }
268     xfree (is->files);
269     xfree (is->merge_buf);
270     xfree (is->method);
271     xfree (is);
272     return 0;
273 }
274
275 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
276 {
277     ++(is->files[cat].no_reads);
278     return bf_read (is->files[cat].bf, pos, 0, 0, dst);
279 }
280
281 int isc_write_block (ISAMC is, int cat, int pos, char *src)
282 {
283     ++(is->files[cat].no_writes);
284     if (is->method->debug > 2)
285         logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
286     return bf_write (is->files[cat].bf, pos, 0, 0, src);
287 }
288
289 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
290                       int nextpos, int offset)
291 {
292     ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
293     if (is->method->debug > 2)
294         logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
295               (int) size, nextpos);
296     src -= ISAMC_BLOCK_OFFSET_N;
297     memcpy (src, &nextpos, sizeof(int));
298     memcpy (src + sizeof(int), &size, sizeof(size));
299     return isc_write_block (is, cat, pos, src);
300 }
301
302 #if ISAMC_FREELIST_CHUNK
303 static void flush_block (ISAMC is, int cat)
304 {
305     char *abuf = is->files[cat].alloc_buf;
306     int block = is->files[cat].head.freelist;
307     if (block && is->files[cat].alloc_entries_num)
308     {
309         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
310         bf_write (is->files[cat].bf, block, 0, 0, abuf);
311         is->files[cat].alloc_entries_num = 0;
312     }
313     xfree (abuf);
314 }
315
316 static int alloc_block (ISAMC is, int cat)
317 {
318     int block = is->files[cat].head.freelist;
319     char *abuf = is->files[cat].alloc_buf;
320
321     (is->files[cat].no_allocated)++;
322
323     if (!block)
324     {
325         block = (is->files[cat].head.lastblock)++;   /* no free list */
326         is->files[cat].head_is_dirty = 1;
327     }
328     else
329     {
330         if (!is->files[cat].alloc_entries_num) /* read first time */
331         {
332             bf_read (is->files[cat].bf, block, 0, 0, abuf);
333             memcpy (&is->files[cat].alloc_entries_num, abuf,
334                     sizeof(is->files[cat].alloc_entries_num));
335             assert (is->files[cat].alloc_entries_num > 0);
336         }
337         /* have some free blocks now */
338         assert (is->files[cat].alloc_entries_num > 0);
339         is->files[cat].alloc_entries_num--;
340         if (!is->files[cat].alloc_entries_num)  /* last one in block? */
341         {
342             memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
343                     sizeof(int));
344             is->files[cat].head_is_dirty = 1;
345
346             if (is->files[cat].head.freelist)
347             {
348                 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
349                          0, 0, abuf);
350                 memcpy (&is->files[cat].alloc_entries_num, abuf,
351                         sizeof(is->files[cat].alloc_entries_num));
352                 assert (is->files[cat].alloc_entries_num);
353             }
354         }
355         else
356             memcpy (&block, abuf + sizeof(int) + sizeof(int) *
357                     is->files[cat].alloc_entries_num, sizeof(int));
358     }
359     return block;
360 }
361
362 static void release_block (ISAMC is, int cat, int pos)
363 {
364     char *abuf = is->files[cat].alloc_buf;
365     int block = is->files[cat].head.freelist;
366
367     (is->files[cat].no_released)++;
368
369     if (block && !is->files[cat].alloc_entries_num) /* must read block */
370     {
371         bf_read (is->files[cat].bf, block, 0, 0, abuf);
372         memcpy (&is->files[cat].alloc_entries_num, abuf,
373                 sizeof(is->files[cat].alloc_entries_num));
374         assert (is->files[cat].alloc_entries_num > 0);
375     }
376     assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
377     if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
378     {
379         assert (block);
380         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
381         bf_write (is->files[cat].bf, block, 0, 0, abuf);
382         is->files[cat].alloc_entries_num = 0;
383     }
384     if (!is->files[cat].alloc_entries_num) /* make new buffer? */
385     {
386         memcpy (abuf + sizeof(int), &block, sizeof(int));
387         is->files[cat].head.freelist = pos;
388         is->files[cat].head_is_dirty = 1; 
389     }
390     else
391     {
392         memcpy (abuf + sizeof(int) +
393                 is->files[cat].alloc_entries_num*sizeof(int),
394                 &pos, sizeof(int));
395     }
396     is->files[cat].alloc_entries_num++;
397 }
398 #else
399 static void flush_block (ISAMC is, int cat)
400 {
401     char *abuf = is->files[cat].alloc_buf;
402     xfree (abuf);
403 }
404
405 static int alloc_block (ISAMC is, int cat)
406 {
407     int block;
408     char buf[sizeof(int)];
409
410     is->files[cat].head_is_dirty = 1;
411     (is->files[cat].no_allocated)++;
412     if ((block = is->files[cat].head.freelist))
413     {
414         bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
415         memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
416     }
417     else
418         block = (is->files[cat].head.lastblock)++;
419     return block;
420 }
421
422 static void release_block (ISAMC is, int cat, int pos)
423 {
424     char buf[sizeof(int)];
425    
426     (is->files[cat].no_released)++;
427     is->files[cat].head_is_dirty = 1; 
428     memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
429     is->files[cat].head.freelist = pos;
430     bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
431 }
432 #endif
433
434 int isc_alloc_block (ISAMC is, int cat)
435 {
436     int block = 0;
437
438     if (is->files[cat].fc_list)
439     {
440         int j, nb;
441         for (j = 0; j < is->files[cat].fc_max; j++)
442             if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
443             {
444                 is->files[cat].fc_list[j] = 0;
445                 block = nb;
446                 break;
447             }
448     }
449     if (!block)
450         block = alloc_block (is, cat);
451     if (is->method->debug > 3)
452         logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
453     return block;
454 }
455
456 void isc_release_block (ISAMC is, int cat, int pos)
457 {
458     if (is->method->debug > 3)
459         logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
460     if (is->files[cat].fc_list)
461     {
462         int j;
463         for (j = 0; j<is->files[cat].fc_max; j++)
464             if (!is->files[cat].fc_list[j])
465             {
466                 is->files[cat].fc_list[j] = pos;
467                 return;
468             }
469     }
470     release_block (is, cat, pos);
471 }
472
473 static void init_fc (ISAMC is, int cat)
474 {
475     int j = 100;
476         
477     is->files[cat].fc_max = j;
478     is->files[cat].fc_list = (int *)
479         xmalloc (sizeof(*is->files[0].fc_list) * j);
480     while (--j >= 0)
481         is->files[cat].fc_list[j] = 0;
482 }
483
484 static void release_fc (ISAMC is, int cat)
485 {
486     int b, j = is->files[cat].fc_max;
487
488     while (--j >= 0)
489         if ((b = is->files[cat].fc_list[j]))
490         {
491             release_block (is, cat, b);
492             is->files[cat].fc_list[j] = 0;
493         }
494 }
495
496 void isc_pp_close (ISAMC_PP pp)
497 {
498     ISAMC is = pp->is;
499
500     (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
501     xfree (pp->buf);
502     xfree (pp);
503 }
504
505 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
506 {
507     ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
508     char *src;
509    
510     pp->cat = isc_type(ipos);
511     pp->pos = isc_block(ipos); 
512
513     src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
514
515     pp->next = 0;
516     pp->size = 0;
517     pp->offset = 0;
518     pp->is = is;
519     pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
520     pp->deleteFlag = 0;
521     pp->numKeys = 0;
522
523     if (pp->pos)
524     {
525         src = pp->buf;
526         isc_read_block (is, pp->cat, pp->pos, src);
527         memcpy (&pp->next, src, sizeof(pp->next));
528         src += sizeof(pp->next);
529         memcpy (&pp->size, src, sizeof(pp->size));
530         src += sizeof(pp->size);
531         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
532         src += sizeof(pp->numKeys);
533         assert (pp->next != pp->pos);
534         pp->offset = src - pp->buf; 
535         assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
536         if (is->method->debug > 2)
537             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
538                  pp->size, pp->cat, pp->pos, pp->next);
539     }
540     return pp;
541 }
542
543 /* returns non-zero if item could be read; 0 otherwise */
544 int isc_pp_read (ISAMC_PP pp, void *buf)
545 {
546     return isc_read_item (pp, (char **) &buf);
547 }
548
549 /* read one item from file - decode and store it in *dst.
550    Returns
551      0 if end-of-file
552      1 if item could be read ok and NO boundary
553      2 if item could be read ok and boundary */
554 int isc_read_item (ISAMC_PP pp, char **dst)
555 {
556     ISAMC is = pp->is;
557     char *src = pp->buf + pp->offset;
558
559     if (pp->offset >= pp->size)
560     {
561         if (!pp->next)
562         {
563             pp->pos = 0;
564             return 0; /* end of file */
565         }
566         if (pp->next > pp->pos)
567         {
568             if (pp->next == pp->pos + 1)
569                 is->files[pp->cat].no_next++;
570             else
571             {
572                 is->files[pp->cat].no_forward++;
573                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
574             }
575         }
576         else
577         {
578             if (pp->next + 1 == pp->pos)
579                 is->files[pp->cat].no_prev++;
580             else
581             {
582                 is->files[pp->cat].no_backward++;
583                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
584             }
585         }
586         /* out new block position */
587         pp->pos = pp->next;
588         src = pp->buf;
589         /* read block and save 'next' and 'size' entry */
590         isc_read_block (is, pp->cat, pp->pos, src);
591         memcpy (&pp->next, src, sizeof(pp->next));
592         src += sizeof(pp->next);
593         memcpy (&pp->size, src, sizeof(pp->size));
594         src += sizeof(pp->size);
595         /* assume block is non-empty */
596         assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
597         assert (pp->next != pp->pos);
598         if (pp->deleteFlag)
599             isc_release_block (is, pp->cat, pp->pos);
600         (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
601         pp->offset = src - pp->buf; 
602         if (is->method->debug > 2)
603             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
604                  pp->size, pp->cat, pp->pos, pp->next);
605         return 2;
606     }
607     (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
608     pp->offset = src - pp->buf; 
609     return 1;
610 }
611
612 int isc_pp_num (ISAMC_PP pp)
613 {
614     return pp->numKeys;
615 }
616