2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-02-07 10:08:43 adam
8 * Work on flat shadow (not finished yet).
10 * Revision 1.8 1995/12/15 12:36:52 adam
11 * Moved hash file information to union.
12 * Renamed commit files.
14 * Revision 1.7 1995/12/15 10:35:07 adam
15 * Changed names of commit files.
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.
21 * Revision 1.5 1995/12/08 16:21:14 adam
22 * Work on commit/update.
24 * Revision 1.4 1995/12/01 16:24:28 adam
25 * Commit files use separate meta file area.
27 * Revision 1.3 1995/12/01 11:37:22 adam
28 * Cached/commit files implemented as meta-files.
30 * Revision 1.2 1995/11/30 17:00:49 adam
31 * Several bug fixes. Commit system runs now.
33 * Revision 1.1 1995/11/30 08:33:11 adam
34 * Started work on commit facility.
46 static int write_head (CFile cf)
48 int left = cf->head.hash_size * sizeof(int);
50 const char *tab = (char*) cf->array;
52 while (left >= HASH_BSIZE)
54 mf_write (cf->hash_mf, bno++, 0, 0, tab);
59 mf_write (cf->hash_mf, bno, 0, left, tab);
63 static int read_head (CFile cf)
65 int left = cf->head.hash_size * sizeof(int);
67 char *tab = (char*) cf->array;
69 while (left >= HASH_BSIZE)
71 mf_read (cf->hash_mf, bno++, 0, 0, tab);
76 mf_read (cf->hash_mf, bno, 0, left, tab);
81 CFile cf_open (MFile mf, MFile_area area, const char *fname,
82 int block_size, int wflag, int *firstp)
86 CFile cf = xmalloc (sizeof(*cf));
90 sprintf (path, "%s-b", fname);
91 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
93 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
96 sprintf (path, "%s-i", fname);
97 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
99 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
103 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
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;
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++)
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);
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;
139 cf->iobuf = xmalloc (cf->head.block_size);
140 memset (cf->iobuf, 0, cf->head.block_size);
144 static int cf_hash (CFile cf, int no)
146 return (no>>3) % cf->head.hash_size;
149 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
152 p->lru_prev->lru_next = p->lru_next;
154 cf->bucket_lru_back = p->lru_next;
156 p->lru_next->lru_prev = p->lru_prev;
158 cf->bucket_lru_front = p->lru_prev;
160 *p->h_prev = p->h_next;
162 p->h_next->h_prev = p->h_prev;
164 --(cf->bucket_in_memory);
168 static void flush_bucket (CFile cf, int no_to_flush)
171 struct CFile_hash_bucket *p;
173 for (i = 0; i != no_to_flush; i++)
175 p = cf->bucket_lru_back;
180 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
183 release_bucket (cf, p);
187 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
189 struct CFile_hash_bucket *p, **pp;
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));
198 p->lru_prev = cf->bucket_lru_front;
199 if (cf->bucket_lru_front)
200 cf->bucket_lru_front->lru_next = p;
202 cf->bucket_lru_back = p;
203 cf->bucket_lru_front = p;
205 pp = cf->parray + hno;
209 (*pp)->h_prev = &p->h_next;
214 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
216 struct CFile_hash_bucket *p;
218 p = alloc_bucket (cf, block_no, hno);
219 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
221 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
224 assert (p->ph.this_bucket == block_no);
229 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
231 struct CFile_hash_bucket *p;
234 *block_no = cf->head.next_bucket++;
235 p = alloc_bucket (cf, *block_no, hno);
237 for (i = 0; i<HASH_BUCKET; i++)
242 p->ph.next_bucket = 0;
243 p->ph.this_bucket = *block_no;
248 static int cf_lookup_flat (CFile cf, int no)
250 int hno = (no*sizeof(int))/HASH_BSIZE;
251 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
254 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
258 static int cf_lookup_hash (CFile cf, int no)
260 int hno = cf_hash (cf, no);
261 struct CFile_hash_bucket *hb;
264 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
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];
270 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
272 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
274 if (hb->ph.this_bucket == block_no)
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];
287 static int cf_lookup (CFile cf, int no)
289 if (cf->head.state > 1)
290 return cf_lookup_flat (cf, no);
291 return cf_lookup_hash (cf, no);
294 int cf_new_flat (CFile cf, int no)
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)++;
300 mf_write (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
304 static int cf_new_hash (CFile cf, int no)
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)++;
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++)
324 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
325 if (hb->ph.this_bucket == *bucketpp)
327 bucketpp = &hb->ph.next_bucket;
333 hb = get_bucket (cf, *bucketpp, hno);
335 for (i = 0; i<HASH_BUCKET; i++)
343 bucketpp = &hb->ph.next_bucket;
348 hb = new_bucket (cf, bucketpp, hno);
354 int cf_new (CFile cf, int no)
356 if (cf->head.state > 1)
357 return cf_new_flat (cf, no);
358 return cf_new_hash (cf, no);
362 int cf_read (CFile cf, int no, int offset, int num, void *buf)
367 if (!(block = cf_lookup (cf, no)))
369 if (!mf_read (cf->block_mf, block, offset, num, buf))
371 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
377 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
382 if (!(block = cf_lookup (cf, no)))
384 block = cf_new (cf, no);
387 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
388 memcpy (cf->iobuf + offset, buf, num);
394 if (mf_write (cf->block_mf, block, offset, num, buf))
396 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
402 int cf_close (CFile cf)
404 flush_bucket (cf, -1);
407 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
410 mf_close (cf->hash_mf);
411 mf_close (cf->block_mf);