Implemented isamb_unlink.
[idzebra-moved-to-github.git] / isamc / isamc.c
1 /* $Id: isamc.c,v 1.23 2003-06-23 15:36:11 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 /* 
26  * TODO:
27  *   Reduction to lower categories in isc_merge
28  */
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #include <yaz/log.h>
35 #include "isamc-p.h"
36
37 static void flush_block (ISAMC is, int cat);
38 static void release_fc (ISAMC is, int cat);
39 static void init_fc (ISAMC is, int cat);
40
41 #define ISAMC_FREELIST_CHUNK 1
42
43 #define SMALL_TEST 0
44
45 void isc_getmethod (ISAMC_M *m)
46 {
47
48     static struct ISAMC_filecat_s def_cat[] = {
49 #if SMALL_TEST
50         {    32,     28,      0,  3 },
51         {    64,     54,     30,  0 },
52 #else
53         {    32,     26,     20,  10 },
54         {   128,    120,    100,  10 },
55         {   512,    490,    350,  10 },
56         {  2048,   1900,   1700,  10 },
57         {  8192,   8000,   7900,  10 },
58         { 32768,  32000,  31000,  0 },
59 #endif
60     };
61     m->filecat = def_cat;
62
63     m->code_start = NULL;
64     m->code_item = NULL;
65     m->code_stop = NULL;
66     m->code_reset = NULL;
67
68     m->compare_item = NULL;
69
70     m->debug = 1;
71
72     m->max_blocks_mem = 10;
73 }
74
75 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
76 {
77     ISAMC is;
78     ISAMC_filecat filecat;
79     int i = 0;
80     int max_buf_size = 0;
81
82     is = (ISAMC) xmalloc (sizeof(*is));
83
84     is->method = (ISAMC_M *) xmalloc (sizeof(*is->method));
85     memcpy (is->method, method, sizeof(*method));
86     filecat = is->method->filecat;
87     assert (filecat);
88
89     /* determine number of block categories */
90     if (is->method->debug)
91         logf (LOG_LOG, "isc: bsize  ifill  mfill mblocks");
92     do
93     {
94         if (is->method->debug)
95             logf (LOG_LOG, "isc:%6d %6d %6d %6d",
96                   filecat[i].bsize, filecat[i].ifill, 
97                   filecat[i].mfill, filecat[i].mblocks);
98         if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
99             max_buf_size = filecat[i].mblocks * filecat[i].bsize;
100     } while (filecat[i++].mblocks);
101     is->no_files = i;
102     is->max_cat = --i;
103     /* max_buf_size is the larget buffer to be used during merge */
104     max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
105     if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
106         max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
107     if (is->method->debug)
108         logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
109     
110     assert (is->no_files > 0);
111     is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
112     if (writeflag)
113     {
114         is->merge_buf = (char *) xmalloc (max_buf_size+256);
115         memset (is->merge_buf, 0, max_buf_size+256);
116     }
117     else
118         is->merge_buf = NULL;
119     for (i = 0; i<is->no_files; i++)
120     {
121         char fname[512];
122
123         sprintf (fname, "%s%c", name, i+'A');
124         is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
125                                    writeflag);
126         is->files[i].head_is_dirty = 0;
127         if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
128                      &is->files[i].head))
129         {
130             is->files[i].head.lastblock = 1;
131             is->files[i].head.freelist = 0;
132         }
133         is->files[i].alloc_entries_num = 0;
134         is->files[i].alloc_entries_max =
135             is->method->filecat[i].bsize / sizeof(int) - 1;
136         is->files[i].alloc_buf = (char *)
137             xmalloc (is->method->filecat[i].bsize);
138         is->files[i].no_writes = 0;
139         is->files[i].no_reads = 0;
140         is->files[i].no_skip_writes = 0;
141         is->files[i].no_allocated = 0;
142         is->files[i].no_released = 0;
143         is->files[i].no_remap = 0;
144         is->files[i].no_forward = 0;
145         is->files[i].no_backward = 0;
146         is->files[i].sum_forward = 0;
147         is->files[i].sum_backward = 0;
148         is->files[i].no_next = 0;
149         is->files[i].no_prev = 0;
150
151         init_fc (is, i);
152     }
153     return is;
154 }
155
156 int isc_block_used (ISAMC is, int type)
157 {
158     if (type < 0 || type >= is->no_files)
159         return -1;
160     return is->files[type].head.lastblock-1;
161 }
162
163 int isc_block_size (ISAMC is, int type)
164 {
165     ISAMC_filecat filecat = is->method->filecat;
166     if (type < 0 || type >= is->no_files)
167         return -1;
168     return filecat[type].bsize;
169 }
170
171 int isc_close (ISAMC is)
172 {
173     int i;
174
175     if (is->method->debug)
176     {
177         logf (LOG_LOG, "isc:    next    forw   mid-f    prev   backw   mid-b");
178         for (i = 0; i<is->no_files; i++)
179             logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
180                   is->files[i].no_next,
181                   is->files[i].no_forward,
182                   is->files[i].no_forward ?
183                   (double) is->files[i].sum_forward/is->files[i].no_forward
184                   : 0.0,
185                   is->files[i].no_prev,
186                   is->files[i].no_backward,
187                   is->files[i].no_backward ?
188                   (double) is->files[i].sum_backward/is->files[i].no_backward
189                   : 0.0);
190     }
191     if (is->method->debug)
192         logf (LOG_LOG, "isc:  writes   reads skipped   alloc released  remap");
193     for (i = 0; i<is->no_files; i++)
194     {
195         release_fc (is, i);
196         assert (is->files[i].bf);
197         if (is->files[i].head_is_dirty)
198             bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
199                  &is->files[i].head);
200         if (is->method->debug)
201             logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
202                   is->files[i].no_writes,
203                   is->files[i].no_reads,
204                   is->files[i].no_skip_writes,
205                   is->files[i].no_allocated,
206                   is->files[i].no_released,
207                   is->files[i].no_remap);
208         xfree (is->files[i].fc_list);
209         flush_block (is, i);
210         bf_close (is->files[i].bf);
211     }
212     xfree (is->files);
213     xfree (is->merge_buf);
214     xfree (is->method);
215     xfree (is);
216     return 0;
217 }
218
219 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
220 {
221     ++(is->files[cat].no_reads);
222     return bf_read (is->files[cat].bf, pos, 0, 0, dst);
223 }
224
225 int isc_write_block (ISAMC is, int cat, int pos, char *src)
226 {
227     ++(is->files[cat].no_writes);
228     if (is->method->debug > 2)
229         logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
230     return bf_write (is->files[cat].bf, pos, 0, 0, src);
231 }
232
233 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
234                       int nextpos, int offset)
235 {
236     ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
237     if (is->method->debug > 2)
238         logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
239               (int) size, nextpos);
240     src -= ISAMC_BLOCK_OFFSET_N;
241     memcpy (src, &nextpos, sizeof(int));
242     memcpy (src + sizeof(int), &size, sizeof(size));
243     return isc_write_block (is, cat, pos, src);
244 }
245
246 #if ISAMC_FREELIST_CHUNK
247 static void flush_block (ISAMC is, int cat)
248 {
249     char *abuf = is->files[cat].alloc_buf;
250     int block = is->files[cat].head.freelist;
251     if (block && is->files[cat].alloc_entries_num)
252     {
253         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
254         bf_write (is->files[cat].bf, block, 0, 0, abuf);
255         is->files[cat].alloc_entries_num = 0;
256     }
257     xfree (abuf);
258 }
259
260 static int alloc_block (ISAMC is, int cat)
261 {
262     int block = is->files[cat].head.freelist;
263     char *abuf = is->files[cat].alloc_buf;
264
265     (is->files[cat].no_allocated)++;
266
267     if (!block)
268     {
269         block = (is->files[cat].head.lastblock)++;   /* no free list */
270         is->files[cat].head_is_dirty = 1;
271     }
272     else
273     {
274         if (!is->files[cat].alloc_entries_num) /* read first time */
275         {
276             bf_read (is->files[cat].bf, block, 0, 0, abuf);
277             memcpy (&is->files[cat].alloc_entries_num, abuf,
278                     sizeof(is->files[cat].alloc_entries_num));
279             assert (is->files[cat].alloc_entries_num > 0);
280         }
281         /* have some free blocks now */
282         assert (is->files[cat].alloc_entries_num > 0);
283         is->files[cat].alloc_entries_num--;
284         if (!is->files[cat].alloc_entries_num)  /* last one in block? */
285         {
286             memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
287                     sizeof(int));
288             is->files[cat].head_is_dirty = 1;
289
290             if (is->files[cat].head.freelist)
291             {
292                 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
293                          0, 0, abuf);
294                 memcpy (&is->files[cat].alloc_entries_num, abuf,
295                         sizeof(is->files[cat].alloc_entries_num));
296                 assert (is->files[cat].alloc_entries_num);
297             }
298         }
299         else
300             memcpy (&block, abuf + sizeof(int) + sizeof(int) *
301                     is->files[cat].alloc_entries_num, sizeof(int));
302     }
303     return block;
304 }
305
306 static void release_block (ISAMC is, int cat, int pos)
307 {
308     char *abuf = is->files[cat].alloc_buf;
309     int block = is->files[cat].head.freelist;
310
311     (is->files[cat].no_released)++;
312
313     if (block && !is->files[cat].alloc_entries_num) /* must read block */
314     {
315         bf_read (is->files[cat].bf, block, 0, 0, abuf);
316         memcpy (&is->files[cat].alloc_entries_num, abuf,
317                 sizeof(is->files[cat].alloc_entries_num));
318         assert (is->files[cat].alloc_entries_num > 0);
319     }
320     assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
321     if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
322     {
323         assert (block);
324         memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
325         bf_write (is->files[cat].bf, block, 0, 0, abuf);
326         is->files[cat].alloc_entries_num = 0;
327     }
328     if (!is->files[cat].alloc_entries_num) /* make new buffer? */
329     {
330         memcpy (abuf + sizeof(int), &block, sizeof(int));
331         is->files[cat].head.freelist = pos;
332         is->files[cat].head_is_dirty = 1; 
333     }
334     else
335     {
336         memcpy (abuf + sizeof(int) +
337                 is->files[cat].alloc_entries_num*sizeof(int),
338                 &pos, sizeof(int));
339     }
340     is->files[cat].alloc_entries_num++;
341 }
342 #else
343 static void flush_block (ISAMC is, int cat)
344 {
345     char *abuf = is->files[cat].alloc_buf;
346     xfree (abuf);
347 }
348
349 static int alloc_block (ISAMC is, int cat)
350 {
351     int block;
352     char buf[sizeof(int)];
353
354     is->files[cat].head_is_dirty = 1;
355     (is->files[cat].no_allocated)++;
356     if ((block = is->files[cat].head.freelist))
357     {
358         bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
359         memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
360     }
361     else
362         block = (is->files[cat].head.lastblock)++;
363     return block;
364 }
365
366 static void release_block (ISAMC is, int cat, int pos)
367 {
368     char buf[sizeof(int)];
369    
370     (is->files[cat].no_released)++;
371     is->files[cat].head_is_dirty = 1; 
372     memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
373     is->files[cat].head.freelist = pos;
374     bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
375 }
376 #endif
377
378 int isc_alloc_block (ISAMC is, int cat)
379 {
380     int block = 0;
381
382     if (is->files[cat].fc_list)
383     {
384         int j, nb;
385         for (j = 0; j < is->files[cat].fc_max; j++)
386             if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
387             {
388                 is->files[cat].fc_list[j] = 0;
389                 block = nb;
390                 break;
391             }
392     }
393     if (!block)
394         block = alloc_block (is, cat);
395     if (is->method->debug > 3)
396         logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
397     return block;
398 }
399
400 void isc_release_block (ISAMC is, int cat, int pos)
401 {
402     if (is->method->debug > 3)
403         logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
404     if (is->files[cat].fc_list)
405     {
406         int j;
407         for (j = 0; j<is->files[cat].fc_max; j++)
408             if (!is->files[cat].fc_list[j])
409             {
410                 is->files[cat].fc_list[j] = pos;
411                 return;
412             }
413     }
414     release_block (is, cat, pos);
415 }
416
417 static void init_fc (ISAMC is, int cat)
418 {
419     int j = 100;
420         
421     is->files[cat].fc_max = j;
422     is->files[cat].fc_list = (int *)
423         xmalloc (sizeof(*is->files[0].fc_list) * j);
424     while (--j >= 0)
425         is->files[cat].fc_list[j] = 0;
426 }
427
428 static void release_fc (ISAMC is, int cat)
429 {
430     int b, j = is->files[cat].fc_max;
431
432     while (--j >= 0)
433         if ((b = is->files[cat].fc_list[j]))
434         {
435             release_block (is, cat, b);
436             is->files[cat].fc_list[j] = 0;
437         }
438 }
439
440 void isc_pp_close (ISAMC_PP pp)
441 {
442     ISAMC is = pp->is;
443
444     (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
445     xfree (pp->buf);
446     xfree (pp);
447 }
448
449 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
450 {
451     ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
452     char *src;
453    
454     pp->cat = isc_type(ipos);
455     pp->pos = isc_block(ipos); 
456
457     src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
458
459     pp->next = 0;
460     pp->size = 0;
461     pp->offset = 0;
462     pp->is = is;
463     pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
464     pp->deleteFlag = 0;
465     pp->numKeys = 0;
466
467     if (pp->pos)
468     {
469         src = pp->buf;
470         isc_read_block (is, pp->cat, pp->pos, src);
471         memcpy (&pp->next, src, sizeof(pp->next));
472         src += sizeof(pp->next);
473         memcpy (&pp->size, src, sizeof(pp->size));
474         src += sizeof(pp->size);
475         memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
476         src += sizeof(pp->numKeys);
477         assert (pp->next != pp->pos);
478         pp->offset = src - pp->buf; 
479         assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
480         if (is->method->debug > 2)
481             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
482                  pp->size, pp->cat, pp->pos, pp->next);
483     }
484     return pp;
485 }
486
487 /* returns non-zero if item could be read; 0 otherwise */
488 int isc_pp_read (ISAMC_PP pp, void *buf)
489 {
490     return isc_read_item (pp, (char **) &buf);
491 }
492
493 /* read one item from file - decode and store it in *dst.
494    Returns
495      0 if end-of-file
496      1 if item could be read ok and NO boundary
497      2 if item could be read ok and boundary */
498 int isc_read_item (ISAMC_PP pp, char **dst)
499 {
500     ISAMC is = pp->is;
501     char *src = pp->buf + pp->offset;
502
503     if (pp->offset >= pp->size)
504     {
505         if (!pp->next)
506         {
507             pp->pos = 0;
508             return 0; /* end of file */
509         }
510         if (pp->next > pp->pos)
511         {
512             if (pp->next == pp->pos + 1)
513                 is->files[pp->cat].no_next++;
514             else
515             {
516                 is->files[pp->cat].no_forward++;
517                 is->files[pp->cat].sum_forward += pp->next - pp->pos;
518             }
519         }
520         else
521         {
522             if (pp->next + 1 == pp->pos)
523                 is->files[pp->cat].no_prev++;
524             else
525             {
526                 is->files[pp->cat].no_backward++;
527                 is->files[pp->cat].sum_backward += pp->pos - pp->next;
528             }
529         }
530         /* out new block position */
531         pp->pos = pp->next;
532         src = pp->buf;
533         /* read block and save 'next' and 'size' entry */
534         isc_read_block (is, pp->cat, pp->pos, src);
535         memcpy (&pp->next, src, sizeof(pp->next));
536         src += sizeof(pp->next);
537         memcpy (&pp->size, src, sizeof(pp->size));
538         src += sizeof(pp->size);
539         /* assume block is non-empty */
540         assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
541         assert (pp->next != pp->pos);
542         if (pp->deleteFlag)
543             isc_release_block (is, pp->cat, pp->pos);
544         (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
545         pp->offset = src - pp->buf; 
546         if (is->method->debug > 2)
547             logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
548                  pp->size, pp->cat, pp->pos, pp->next);
549         return 2;
550     }
551     (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
552     pp->offset = src - pp->buf; 
553     return 1;
554 }
555
556 int isc_pp_num (ISAMC_PP pp)
557 {
558     return pp->numKeys;
559 }
560