Include of zebrautl.h instead of alexutil.h.
[idzebra-moved-to-github.git] / bfile / cfile.c
1 /*
2  * Copyright (C) 1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: cfile.c,v $
7  * Revision 1.18  1996-10-29 13:56:15  adam
8  * Include of zebrautl.h instead of alexutil.h.
9  *
10  * Revision 1.17  1996/04/19 16:49:00  adam
11  * Minor changes.
12  *
13  * Revision 1.16  1996/04/19  16:23:47  adam
14  * Serious bug fix in shadow implementation; function new_bucket might
15  * set wrong bucket number on new bucket.
16  *
17  * Revision 1.15  1996/04/18  16:02:56  adam
18  * Changed logging a bit.
19  * Removed warning message when commiting flat shadow files.
20  *
21  * Revision 1.14  1996/04/12  07:01:55  adam
22  * Yet another bug fix (next_block was initialized to 0; now set to 1).
23  *
24  * Revision 1.13  1996/04/09 14:48:49  adam
25  * Bug fix: offset calculation when using flat files was completely broken.
26  *
27  * Revision 1.12  1996/04/09  06:47:28  adam
28  * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
29  *
30  * Revision 1.11  1996/03/26 15:59:05  adam
31  * The directory of the shadow table file can be specified by the new
32  * bf_lockDir call.
33  *
34  * Revision 1.10  1996/02/07  14:03:46  adam
35  * Work on flat indexed shadow files.
36  *
37  * Revision 1.9  1996/02/07  10:08:43  adam
38  * Work on flat shadow (not finished yet).
39  *
40  * Revision 1.8  1995/12/15  12:36:52  adam
41  * Moved hash file information to union.
42  * Renamed commit files.
43  *
44  * Revision 1.7  1995/12/15  10:35:07  adam
45  * Changed names of commit files.
46  *
47  * Revision 1.6  1995/12/11  09:03:53  adam
48  * New function: cf_unlink.
49  * New member of commit file head: state (0) deleted, (1) hash file.
50  *
51  * Revision 1.5  1995/12/08  16:21:14  adam
52  * Work on commit/update.
53  *
54  * Revision 1.4  1995/12/01  16:24:28  adam
55  * Commit files use separate meta file area.
56  *
57  * Revision 1.3  1995/12/01  11:37:22  adam
58  * Cached/commit files implemented as meta-files.
59  *
60  * Revision 1.2  1995/11/30  17:00:49  adam
61  * Several bug fixes. Commit system runs now.
62  *
63  * Revision 1.1  1995/11/30  08:33:11  adam
64  * Started work on commit facility.
65  *
66  */
67
68 #include <assert.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 #include <zebrautl.h>
73 #include <mfile.h>
74 #include "cfile.h"
75
76 static int write_head (CFile cf)
77 {
78     int left = cf->head.hash_size * sizeof(int);
79     int bno = 1;
80     const char *tab = (char*) cf->array;
81
82     if (!tab)
83         return 0;
84     while (left >= HASH_BSIZE)
85     {
86         mf_write (cf->hash_mf, bno++, 0, 0, tab);
87         tab += HASH_BSIZE;
88         left -= HASH_BSIZE;
89     }
90     if (left > 0)
91         mf_write (cf->hash_mf, bno, 0, left, tab);
92     return 0;
93 }
94
95 static int read_head (CFile cf)
96 {
97     int left = cf->head.hash_size * sizeof(int);
98     int bno = 1;
99     char *tab = (char*) cf->array;
100
101     if (!tab)
102         return 0;
103     while (left >= HASH_BSIZE)
104     {
105         mf_read (cf->hash_mf, bno++, 0, 0, tab);
106         tab += HASH_BSIZE;
107         left -= HASH_BSIZE;
108     }
109     if (left > 0)
110         mf_read (cf->hash_mf, bno, 0, left, tab);
111     return 0;
112 }
113
114
115 CFile cf_open (MFile mf, MFile_area area, const char *fname,
116                int block_size, int wflag, int *firstp)
117 {
118     char path[1024];
119     int i;
120     CFile cf = xmalloc (sizeof(*cf));
121     int hash_bytes;
122    
123     cf->rmf = mf; 
124     logf (LOG_LOG, "cf_open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
125     sprintf (path, "%s-b", fname);
126     if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
127     {
128         logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
129         exit (1);
130     }
131     sprintf (path, "%s-i", fname);
132     if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
133     {
134         logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
135         exit (1);
136     }
137     assert (firstp);
138     if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
139         !cf->head.state)
140     {
141         *firstp = 1;
142         cf->head.state = 1;
143         cf->head.block_size = block_size;
144         cf->head.hash_size = 199;
145         hash_bytes = cf->head.hash_size * sizeof(int);
146         cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket = 
147             (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
148         cf->head.next_block = 1;
149         if (wflag)
150             mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
151         cf->array = xmalloc (hash_bytes);
152         for (i = 0; i<cf->head.hash_size; i++)
153             cf->array[i] = 0;
154         if (wflag)
155             write_head (cf);
156     }
157     else
158     {
159         *firstp = 0;
160         assert (cf->head.block_size == block_size);
161         assert (cf->head.hash_size > 2);
162         hash_bytes = cf->head.hash_size * sizeof(int);
163         assert (cf->head.next_bucket > 0);
164         assert (cf->head.next_block > 0);
165         if (cf->head.state == 1)
166             cf->array = xmalloc (hash_bytes);
167         else
168             cf->array = NULL;
169         read_head (cf);
170     }
171     if (cf->head.state == 1)
172     {
173         cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
174         for (i = 0; i<cf->head.hash_size; i++)
175             cf->parray[i] = NULL;
176     }
177     else
178         cf->parray = NULL;
179     cf->bucket_lru_front = cf->bucket_lru_back = NULL;
180     cf->bucket_in_memory = 0;
181     cf->max_bucket_in_memory = 100;
182     cf->dirty = 0;
183     cf->iobuf = xmalloc (cf->head.block_size);
184     memset (cf->iobuf, 0, cf->head.block_size);
185     cf->no_hits = 0;
186     cf->no_miss = 0;
187     return cf;
188 }
189
190 static int cf_hash (CFile cf, int no)
191 {
192     return (no>>3) % cf->head.hash_size;
193 }
194
195 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
196 {
197     if (p->lru_prev)
198         p->lru_prev->lru_next = p->lru_next;
199     else
200         cf->bucket_lru_back = p->lru_next;
201     if (p->lru_next)
202         p->lru_next->lru_prev = p->lru_prev;
203     else
204         cf->bucket_lru_front = p->lru_prev;
205
206     *p->h_prev = p->h_next;
207     if (p->h_next)
208         p->h_next->h_prev = p->h_prev;
209     
210     --(cf->bucket_in_memory);
211     xfree (p);
212 }
213
214 static void flush_bucket (CFile cf, int no_to_flush)
215 {
216     int i;
217     struct CFile_hash_bucket *p;
218
219     for (i = 0; i != no_to_flush; i++)
220     {
221         p = cf->bucket_lru_back;
222         if (!p)
223             break;
224         if (p->dirty)
225         {
226             mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
227             cf->dirty = 1;
228         }
229         release_bucket (cf, p);
230     }
231 }
232
233 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
234 {
235     struct CFile_hash_bucket *p, **pp;
236
237     if (cf->bucket_in_memory == cf->max_bucket_in_memory)
238         flush_bucket (cf, 1);
239     assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
240     ++(cf->bucket_in_memory);
241     p = xmalloc (sizeof(*p));
242
243     p->lru_next = NULL;
244     p->lru_prev = cf->bucket_lru_front;
245     if (cf->bucket_lru_front)
246         cf->bucket_lru_front->lru_next = p;
247     else
248         cf->bucket_lru_back = p;
249     cf->bucket_lru_front = p; 
250
251     pp = cf->parray + hno;
252     p->h_next = *pp;
253     p->h_prev = pp;
254     if (*pp)
255         (*pp)->h_prev = &p->h_next;
256     *pp = p;
257     return p;
258 }
259
260 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
261 {
262     struct CFile_hash_bucket *p;
263
264     p = alloc_bucket (cf, block_no, hno);
265     if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
266     {
267         logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
268         exit (1);
269     }
270     assert (p->ph.this_bucket == block_no);
271     p->dirty = 0;
272     return p;
273 }
274
275 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
276 {
277     struct CFile_hash_bucket *p;
278     int i, block_no;
279
280     block_no = *block_nop = cf->head.next_bucket++;
281     p = alloc_bucket (cf, block_no, hno);
282
283     for (i = 0; i<HASH_BUCKET; i++)
284     {
285         p->ph.vno[i] = 0;
286         p->ph.no[i] = 0;
287     }
288     p->ph.next_bucket = 0;
289     p->ph.this_bucket = block_no;
290     p->dirty = 1;
291     return p;
292 }
293
294 static int cf_lookup_flat (CFile cf, int no)
295 {
296     int hno = (no*sizeof(int))/HASH_BSIZE;
297     int off = (no*sizeof(int)) - hno*HASH_BSIZE;
298     int vno = 0;
299
300     mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
301     return vno;
302 }
303
304 static int cf_lookup_hash (CFile cf, int no)
305 {
306     int hno = cf_hash (cf, no);
307     struct CFile_hash_bucket *hb;
308     int block_no, i;
309
310     for (hb = cf->parray[hno]; hb; hb = hb->h_next)
311     {
312         for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
313             if (hb->ph.no[i] == no)
314             {
315                 (cf->no_hits)++;
316                 return hb->ph.vno[i];
317             }
318     }
319     for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
320     {
321         for (hb = cf->parray[hno]; hb; hb = hb->h_next)
322         {
323             if (hb->ph.this_bucket == block_no)
324                 break;
325         }
326         if (hb)
327             continue;
328 #if 0
329         /* extra check ... */
330         for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
331         {
332             if (hb->ph.this_bucket == block_no)
333             {
334                 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
335                 abort ();
336             }
337             for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
338                 if (hb->ph.no[i] == no)
339                 {
340                     logf (LOG_FATAL, "Found hash bucket on other chain (2)");
341                     abort ();
342                 }
343         }
344 #endif
345         (cf->no_miss)++;
346         hb = get_bucket (cf, block_no, hno);
347         for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
348             if (hb->ph.no[i] == no)
349                 return hb->ph.vno[i];
350     }
351     return 0;
352 }
353
354 static void cf_write_flat (CFile cf, int no, int vno)
355 {
356     int hno = (no*sizeof(int))/HASH_BSIZE;
357     int off = (no*sizeof(int)) - hno*HASH_BSIZE;
358
359     hno += cf->head.next_bucket;
360     if (hno >= cf->head.flat_bucket)
361         cf->head.flat_bucket = hno+1;
362     cf->dirty = 1;
363     mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
364 }
365
366 static void cf_moveto_flat (CFile cf)
367 {
368     struct CFile_hash_bucket *p;
369     int i, j;
370
371     logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
372     logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
373         cf->no_hits, cf->no_miss, cf->bucket_in_memory, 
374         cf->head.next_bucket - cf->head.first_bucket);
375     assert (cf->head.state == 1);
376     flush_bucket (cf, -1);
377     assert (cf->bucket_in_memory == 0);
378     p = xmalloc (sizeof(*p));
379     for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
380     {
381         if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
382         {
383             logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
384             exit (1);
385         }
386         for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
387             cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
388     }
389     xfree (p);
390     xfree (cf->array);
391     cf->array = NULL;
392     xfree (cf->parray);
393     cf->parray = NULL;
394     cf->head.state = 2;
395     cf->dirty = 1;
396 }
397
398 static int cf_lookup (CFile cf, int no)
399 {
400     if (cf->head.state > 1)
401         return cf_lookup_flat (cf, no);
402     return cf_lookup_hash (cf, no);
403 }
404
405 static int cf_new_flat (CFile cf, int no)
406 {
407     int vno = (cf->head.next_block)++;
408
409     cf_write_flat (cf, no, vno);
410     return vno;
411 }
412
413 static int cf_new_hash (CFile cf, int no)
414 {
415     int hno = cf_hash (cf, no);
416     struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
417     int *bucketpp = &cf->array[hno]; 
418     int i, vno = (cf->head.next_block)++;
419   
420     for (hb = cf->parray[hno]; hb; hb = hb->h_next)
421         if (!hb->ph.vno[HASH_BUCKET-1])
422             for (i = 0; i<HASH_BUCKET; i++)
423                 if (!hb->ph.vno[i])
424                 {
425                     (cf->no_hits)++;
426                     hb->ph.no[i] = no;
427                     hb->ph.vno[i] = vno;
428                     hb->dirty = 1;
429                     return vno;
430                 }
431
432     while (*bucketpp)
433     {
434         for (hb = cf->parray[hno]; hb; hb = hb->h_next)
435             if (hb->ph.this_bucket == *bucketpp)
436             {
437                 bucketpp = &hb->ph.next_bucket;
438                 hbprev = hb;
439                 break;
440             }
441         if (hb)
442             continue;
443
444 #if 0
445         /* extra check ... */
446         for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
447         {
448             if (hb->ph.this_bucket == *bucketpp)
449             {
450                 logf (LOG_FATAL, "Found hash bucket on other chain");
451                 abort ();
452             }
453         }
454 #endif
455         (cf->no_miss)++;
456         hb = get_bucket (cf, *bucketpp, hno);
457         assert (hb);
458         for (i = 0; i<HASH_BUCKET; i++)
459             if (!hb->ph.vno[i])
460             {
461                 hb->ph.no[i] = no;
462                 hb->ph.vno[i] = vno;
463                 hb->dirty = 1;
464                 return vno;
465             }
466         bucketpp = &hb->ph.next_bucket;
467         hbprev = hb;
468     }
469     if (hbprev)
470         hbprev->dirty = 1;
471     hb = new_bucket (cf, bucketpp, hno);
472     hb->ph.no[0] = no;
473     hb->ph.vno[0] = vno;
474     return vno;
475 }
476
477 int cf_new (CFile cf, int no)
478 {
479     if (cf->head.state > 1)
480         return cf_new_flat (cf, no);
481     if (cf->no_miss*2 > cf->no_hits)
482     {
483         cf_moveto_flat (cf);
484         assert (cf->head.state > 1);
485         return cf_new_flat (cf, no);
486     }
487     return cf_new_hash (cf, no);
488 }
489
490
491 int cf_read (CFile cf, int no, int offset, int num, void *buf)
492 {
493     int block;
494     
495     assert (cf);
496     if (!(block = cf_lookup (cf, no)))
497         return -1;
498     if (!mf_read (cf->block_mf, block, offset, num, buf))
499     {
500         logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
501         exit (1);
502     }
503     return 1;
504 }
505
506 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
507 {
508     int block;
509
510     assert (cf);
511     if (!(block = cf_lookup (cf, no)))
512     {
513         block = cf_new (cf, no);
514         if (offset || num)
515         {
516             mf_read (cf->rmf, no, 0, 0, cf->iobuf);
517             memcpy (cf->iobuf + offset, buf, num);
518             buf = cf->iobuf;
519             offset = 0;
520             num = 0;
521         }
522     }
523     if (mf_write (cf->block_mf, block, offset, num, buf))
524     {
525         logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
526         exit (1);
527     }
528     return 0;
529 }
530
531 int cf_close (CFile cf)
532 {
533     logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
534           cf->no_hits, cf->no_miss, cf->bucket_in_memory,
535           cf->head.next_bucket - cf->head.first_bucket);
536     flush_bucket (cf, -1);
537     if (cf->dirty)
538     {
539         logf (LOG_LOG, "cf_close %s, dirty", cf->rmf->name);
540         mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
541         write_head (cf);
542     }
543     else
544         logf (LOG_LOG, "cf_close %s", cf->rmf->name);
545     mf_close (cf->hash_mf);
546     mf_close (cf->block_mf);
547     xfree (cf->array);
548     xfree (cf->parray);
549     xfree (cf->iobuf);
550     xfree (cf);
551     return 0;
552 }
553