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