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