Commit files use separate meta file area.
[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.4  1995-12-01 16:24:28  adam
8  * Commit files use separate meta file area.
9  *
10  * Revision 1.3  1995/12/01  11:37:22  adam
11  * Cached/commit files implemented as meta-files.
12  *
13  * Revision 1.2  1995/11/30  17:00:49  adam
14  * Several bug fixes. Commit system runs now.
15  *
16  * Revision 1.1  1995/11/30  08:33:11  adam
17  * Started work on commit facility.
18  *
19  */
20
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <alexutil.h>
26 #include <mfile.h>
27 #include "cfile.h"
28
29 static int write_head (CFile cf)
30 {
31     int left = cf->head.hash_size * sizeof(int);
32     int bno = 1;
33     const char *tab = (char*) cf->array;
34
35     while (left >= HASH_BSIZE)
36     {
37         mf_write (cf->hash_mf, bno++, 0, 0, tab);
38         tab += HASH_BSIZE;
39         left -= HASH_BSIZE;
40     }
41     if (left > 0)
42         mf_write (cf->hash_mf, bno, 0, left, tab);
43     return 0;
44 }
45
46 static int read_head (CFile cf)
47 {
48     int left = cf->head.hash_size * sizeof(int);
49     int bno = 1;
50     char *tab = (char*) cf->array;
51
52     while (left >= HASH_BSIZE)
53     {
54         mf_read (cf->hash_mf, bno++, 0, 0, tab);
55         tab += HASH_BSIZE;
56         left -= HASH_BSIZE;
57     }
58     if (left > 0)
59         mf_read (cf->hash_mf, bno, 0, left, tab);
60     return 0;
61 }
62
63
64 CFile cf_open (MFile mf, MFile_area area, const char *fname,
65                int block_size, int wflag, int *firstp)
66 {
67     char path[256];
68     int i;
69     CFile cf = xmalloc (sizeof(*cf));
70     int hash_bytes;
71    
72     cf->rmf = mf; 
73     sprintf (path, "%s.b", fname);
74     if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
75     {
76         logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
77         exit (1);
78     }
79     sprintf (path, "%s.h", fname);
80     if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
81     {
82         logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
83         exit (1);
84     }
85     if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head))
86     {
87         *firstp = 1;
88         cf->head.block_size = block_size;
89         cf->head.hash_size = 401;
90         hash_bytes = cf->head.hash_size * sizeof(int);
91         cf->head.next_bucket =
92             (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
93         cf->head.next_block = 1;
94         if (wflag)
95             mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
96         cf->array = xmalloc (hash_bytes);
97         for (i = 0; i<cf->head.hash_size; i++)
98             cf->array[i] = 0;
99         if (wflag)
100             write_head (cf);
101     }
102     else
103     {
104         *firstp = 0;
105         assert (cf->head.block_size == block_size);
106         assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
107         hash_bytes = cf->head.hash_size * sizeof(int);
108         assert (cf->head.next_bucket > 0);
109         cf->array = xmalloc (hash_bytes);
110         read_head (cf);
111     }
112     cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
113     for (i = 0; i<cf->head.hash_size; i++)
114         cf->parray[i] = NULL;
115     cf->bucket_lru_front = cf->bucket_lru_back = NULL;
116     cf->bucket_in_memory = 0;
117     cf->max_bucket_in_memory = 400;
118     cf->dirty = 0;
119     cf->iobuf = xmalloc (cf->head.block_size);
120     memset (cf->iobuf, 0, cf->head.block_size);
121     return cf;
122 }
123
124 static int cf_hash (CFile cf, int no)
125 {
126     return (no>>3) % cf->head.hash_size;
127 }
128
129 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
130 {
131     if (p->lru_prev)
132         p->lru_prev->lru_next = p->lru_next;
133     else
134         cf->bucket_lru_back = p->lru_next;
135     if (p->lru_next)
136         p->lru_next->lru_prev = p->lru_prev;
137     else
138         cf->bucket_lru_front = p->lru_prev;
139
140     *p->h_prev = p->h_next;
141     if (p->h_next)
142         p->h_next->h_prev = p->h_prev;
143     
144     --(cf->bucket_in_memory);
145     xfree (p);
146 }
147
148 static void flush_bucket (CFile cf, int no_to_flush)
149 {
150     int i;
151     struct CFile_hash_bucket *p;
152
153     for (i = 0; i != no_to_flush; i++)
154     {
155         p = cf->bucket_lru_back;
156         if (!p)
157             break;
158         if (p->dirty)
159         {
160             mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
161             cf->dirty = 1;
162         }
163         release_bucket (cf, p);
164     }
165 }
166
167 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
168 {
169     struct CFile_hash_bucket *p, **pp;
170
171     if (cf->bucket_in_memory == cf->max_bucket_in_memory)
172         flush_bucket (cf, 1);
173     assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
174     ++(cf->bucket_in_memory);
175     p = xmalloc (sizeof(*p));
176
177     p->lru_next = NULL;
178     p->lru_prev = cf->bucket_lru_front;
179     if (cf->bucket_lru_front)
180         cf->bucket_lru_front->lru_next = p;
181     else
182         cf->bucket_lru_back = p;
183     cf->bucket_lru_front = p; 
184
185     pp = cf->parray + hno;
186     p->h_next = *pp;
187     p->h_prev = pp;
188     if (*pp)
189         (*pp)->h_prev = &p->h_next;
190     *pp = p;
191     return p;
192 }
193
194 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
195 {
196     struct CFile_hash_bucket *p;
197
198     p = alloc_bucket (cf, block_no, hno);
199     if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
200     {
201         logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
202         exit (1);
203     }
204     assert (p->ph.this_bucket == block_no);
205     p->dirty = 0;
206     return p;
207 }
208
209 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
210 {
211     struct CFile_hash_bucket *p;
212     int i;
213
214     *block_no = cf->head.next_bucket++;
215     p = alloc_bucket (cf, *block_no, hno);
216
217     for (i = 0; i<HASH_BUCKET; i++)
218     {
219         p->ph.vno[i] = 0;
220         p->ph.no[i] = 0;
221     }
222     p->ph.next_bucket = 0;
223     p->ph.this_bucket = *block_no;
224     p->dirty = 1;
225     return p;
226 }
227
228 int cf_lookup (CFile cf, int no)
229 {
230     int hno = cf_hash (cf, no);
231     struct CFile_hash_bucket *hb;
232     int block_no, i;
233
234     for (hb = cf->parray[hno]; hb; hb = hb->h_next)
235     {
236         for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
237             if (hb->ph.no[i] == no)
238                 return hb->ph.vno[i];
239     }
240     for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
241     {
242         for (hb = cf->parray[hno]; hb; hb = hb->h_next)
243         {
244             if (hb->ph.this_bucket == block_no)
245                 break;
246         }
247         if (hb)
248             continue;
249         hb = get_bucket (cf, block_no, hno);
250         for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
251             if (hb->ph.no[i] == no)
252                 return hb->ph.vno[i];
253     }
254     return 0;
255 }
256
257 int cf_new (CFile cf, int no)
258 {
259     int hno = cf_hash (cf, no);
260     struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
261     int *bucketpp = &cf->array[hno];
262     int i;
263     int vno = (cf->head.next_block)++;
264     
265     for (hb = cf->parray[hno]; hb; hb = hb->h_next)
266         if (!hb->ph.vno[HASH_BUCKET-1])
267             for (i = 0; i<HASH_BUCKET; i++)
268                 if (!hb->ph.vno[i])
269                 {
270                     hb->ph.no[i] = no;
271                     hb->ph.vno[i] = vno;
272                     hb->dirty = 1;
273                     return vno;
274                 }
275
276     while (*bucketpp)
277     {
278         for (hb = cf->parray[hno]; hb; hb = hb->h_next)
279             if (hb->ph.this_bucket == *bucketpp)
280             {
281                 bucketpp = &hb->ph.next_bucket;
282                 hbprev = hb;
283                 break;
284             }
285         if (hb)
286             continue;
287         hb = get_bucket (cf, *bucketpp, hno);
288         assert (hb);
289         for (i = 0; i<HASH_BUCKET; i++)
290             if (!hb->ph.vno[i])
291             {
292                 hb->ph.no[i] = no;
293                 hb->ph.vno[i] = vno;
294                 hb->dirty = 1;
295                 return vno;
296             }
297         bucketpp = &hb->ph.next_bucket;
298         hbprev = hb;
299     }
300     if (hbprev)
301         hbprev->dirty = 1;
302     hb = new_bucket (cf, bucketpp, hno);
303     hb->ph.no[0] = no;
304     hb->ph.vno[0] = vno;
305     return vno;
306 }
307
308 int cf_read (CFile cf, int no, int offset, int num, void *buf)
309 {
310     int block;
311     
312     assert (cf);
313     if (!(block = cf_lookup (cf, no)))
314         return -1;
315     if (!mf_read (cf->block_mf, block, offset, num, buf))
316     {
317         logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
318         exit (1);
319     }
320     return 1;
321 }
322
323 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
324 {
325     int block;
326
327     assert (cf);
328     if (!(block = cf_lookup (cf, no)))
329     {
330         block = cf_new (cf, no);
331         if (offset || num)
332         {
333             mf_read (cf->rmf, no, 0, 0, cf->iobuf);
334             memcpy (cf->iobuf + offset, buf, num);
335             buf = cf->iobuf;
336             offset = 0;
337             num = 0;
338         }
339     }
340     if (mf_write (cf->block_mf, block, offset, num, buf))
341     {
342         logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
343         exit (1);
344     }
345     return 0;
346 }
347
348 int cf_close (CFile cf)
349 {
350     flush_bucket (cf, -1);
351     if (cf->dirty)
352     {
353         mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
354         write_head (cf);
355     }
356     mf_close (cf->hash_mf);
357     mf_close (cf->block_mf);
358     xfree (cf->array);
359     xfree (cf->parray);
360     xfree (cf->iobuf);
361     xfree (cf);
362     return 0;
363 }
364