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