2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-12-01 16:24:28 adam
8 * Commit files use separate meta file area.
10 * Revision 1.3 1995/12/01 11:37:22 adam
11 * Cached/commit files implemented as meta-files.
13 * Revision 1.2 1995/11/30 17:00:49 adam
14 * Several bug fixes. Commit system runs now.
16 * Revision 1.1 1995/11/30 08:33:11 adam
17 * Started work on commit facility.
29 static int write_head (CFile cf)
31 int left = cf->head.hash_size * sizeof(int);
33 const char *tab = (char*) cf->array;
35 while (left >= HASH_BSIZE)
37 mf_write (cf->hash_mf, bno++, 0, 0, tab);
42 mf_write (cf->hash_mf, bno, 0, left, tab);
46 static int read_head (CFile cf)
48 int left = cf->head.hash_size * sizeof(int);
50 char *tab = (char*) cf->array;
52 while (left >= HASH_BSIZE)
54 mf_read (cf->hash_mf, bno++, 0, 0, tab);
59 mf_read (cf->hash_mf, bno, 0, left, tab);
64 CFile cf_open (MFile mf, MFile_area area, const char *fname,
65 int block_size, int wflag, int *firstp)
69 CFile cf = xmalloc (sizeof(*cf));
73 sprintf (path, "%s.b", fname);
74 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
76 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
79 sprintf (path, "%s.h", fname);
80 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
82 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
85 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head))
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;
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++)
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);
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;
119 cf->iobuf = xmalloc (cf->head.block_size);
120 memset (cf->iobuf, 0, cf->head.block_size);
124 static int cf_hash (CFile cf, int no)
126 return (no>>3) % cf->head.hash_size;
129 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
132 p->lru_prev->lru_next = p->lru_next;
134 cf->bucket_lru_back = p->lru_next;
136 p->lru_next->lru_prev = p->lru_prev;
138 cf->bucket_lru_front = p->lru_prev;
140 *p->h_prev = p->h_next;
142 p->h_next->h_prev = p->h_prev;
144 --(cf->bucket_in_memory);
148 static void flush_bucket (CFile cf, int no_to_flush)
151 struct CFile_hash_bucket *p;
153 for (i = 0; i != no_to_flush; i++)
155 p = cf->bucket_lru_back;
160 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
163 release_bucket (cf, p);
167 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
169 struct CFile_hash_bucket *p, **pp;
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));
178 p->lru_prev = cf->bucket_lru_front;
179 if (cf->bucket_lru_front)
180 cf->bucket_lru_front->lru_next = p;
182 cf->bucket_lru_back = p;
183 cf->bucket_lru_front = p;
185 pp = cf->parray + hno;
189 (*pp)->h_prev = &p->h_next;
194 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
196 struct CFile_hash_bucket *p;
198 p = alloc_bucket (cf, block_no, hno);
199 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
201 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
204 assert (p->ph.this_bucket == block_no);
209 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
211 struct CFile_hash_bucket *p;
214 *block_no = cf->head.next_bucket++;
215 p = alloc_bucket (cf, *block_no, hno);
217 for (i = 0; i<HASH_BUCKET; i++)
222 p->ph.next_bucket = 0;
223 p->ph.this_bucket = *block_no;
228 int cf_lookup (CFile cf, int no)
230 int hno = cf_hash (cf, no);
231 struct CFile_hash_bucket *hb;
234 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
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];
240 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
242 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
244 if (hb->ph.this_bucket == block_no)
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];
257 int cf_new (CFile cf, int no)
259 int hno = cf_hash (cf, no);
260 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
261 int *bucketpp = &cf->array[hno];
263 int vno = (cf->head.next_block)++;
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++)
278 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
279 if (hb->ph.this_bucket == *bucketpp)
281 bucketpp = &hb->ph.next_bucket;
287 hb = get_bucket (cf, *bucketpp, hno);
289 for (i = 0; i<HASH_BUCKET; i++)
297 bucketpp = &hb->ph.next_bucket;
302 hb = new_bucket (cf, bucketpp, hno);
308 int cf_read (CFile cf, int no, int offset, int num, void *buf)
313 if (!(block = cf_lookup (cf, no)))
315 if (!mf_read (cf->block_mf, block, offset, num, buf))
317 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
323 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
328 if (!(block = cf_lookup (cf, no)))
330 block = cf_new (cf, no);
333 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
334 memcpy (cf->iobuf + offset, buf, num);
340 if (mf_write (cf->block_mf, block, offset, num, buf))
342 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
348 int cf_close (CFile cf)
350 flush_bucket (cf, -1);
353 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
356 mf_close (cf->hash_mf);
357 mf_close (cf->block_mf);