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