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