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