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