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