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