2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1996-02-07 14:03:46 adam
8 * Work on flat indexed shadow files.
10 * Revision 1.9 1996/02/07 10:08:43 adam
11 * Work on flat shadow (not finished yet).
13 * Revision 1.8 1995/12/15 12:36:52 adam
14 * Moved hash file information to union.
15 * Renamed commit files.
17 * Revision 1.7 1995/12/15 10:35:07 adam
18 * Changed names of commit files.
20 * Revision 1.6 1995/12/11 09:03:53 adam
21 * New function: cf_unlink.
22 * New member of commit file head: state (0) deleted, (1) hash file.
24 * Revision 1.5 1995/12/08 16:21:14 adam
25 * Work on commit/update.
27 * Revision 1.4 1995/12/01 16:24:28 adam
28 * Commit files use separate meta file area.
30 * Revision 1.3 1995/12/01 11:37:22 adam
31 * Cached/commit files implemented as meta-files.
33 * Revision 1.2 1995/11/30 17:00:49 adam
34 * Several bug fixes. Commit system runs now.
36 * Revision 1.1 1995/11/30 08:33:11 adam
37 * Started work on commit facility.
49 static int write_head (CFile cf)
51 int left = cf->head.hash_size * sizeof(int);
53 const char *tab = (char*) cf->array;
57 while (left >= HASH_BSIZE)
59 mf_write (cf->hash_mf, bno++, 0, 0, tab);
64 mf_write (cf->hash_mf, bno, 0, left, tab);
68 static int read_head (CFile cf)
70 int left = cf->head.hash_size * sizeof(int);
72 char *tab = (char*) cf->array;
76 while (left >= HASH_BSIZE)
78 mf_read (cf->hash_mf, bno++, 0, 0, tab);
83 mf_read (cf->hash_mf, bno, 0, left, tab);
88 CFile cf_open (MFile mf, MFile_area area, const char *fname,
89 int block_size, int wflag, int *firstp)
93 CFile cf = xmalloc (sizeof(*cf));
97 sprintf (path, "%s-b", fname);
98 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
100 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
103 sprintf (path, "%s-i", fname);
104 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
106 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
110 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
115 cf->head.block_size = block_size;
116 cf->head.hash_size = 199;
117 hash_bytes = cf->head.hash_size * sizeof(int);
118 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
119 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
120 cf->head.next_block = 1;
122 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
123 cf->array = xmalloc (hash_bytes);
124 for (i = 0; i<cf->head.hash_size; i++)
132 assert (cf->head.block_size == block_size);
133 assert (cf->head.hash_size > 2);
134 hash_bytes = cf->head.hash_size * sizeof(int);
135 assert (cf->head.next_bucket > 0);
136 if (cf->head.state == 1)
137 cf->array = xmalloc (hash_bytes);
142 if (cf->head.state == 1)
144 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
145 for (i = 0; i<cf->head.hash_size; i++)
146 cf->parray[i] = NULL;
150 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
151 cf->bucket_in_memory = 0;
152 cf->max_bucket_in_memory = 100;
154 cf->iobuf = xmalloc (cf->head.block_size);
155 memset (cf->iobuf, 0, cf->head.block_size);
161 static int cf_hash (CFile cf, int no)
163 return (no>>3) % cf->head.hash_size;
166 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
169 p->lru_prev->lru_next = p->lru_next;
171 cf->bucket_lru_back = p->lru_next;
173 p->lru_next->lru_prev = p->lru_prev;
175 cf->bucket_lru_front = p->lru_prev;
177 *p->h_prev = p->h_next;
179 p->h_next->h_prev = p->h_prev;
181 --(cf->bucket_in_memory);
185 static void flush_bucket (CFile cf, int no_to_flush)
188 struct CFile_hash_bucket *p;
190 for (i = 0; i != no_to_flush; i++)
192 p = cf->bucket_lru_back;
197 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
200 release_bucket (cf, p);
204 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
206 struct CFile_hash_bucket *p, **pp;
208 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
209 flush_bucket (cf, 1);
210 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
211 ++(cf->bucket_in_memory);
212 p = xmalloc (sizeof(*p));
215 p->lru_prev = cf->bucket_lru_front;
216 if (cf->bucket_lru_front)
217 cf->bucket_lru_front->lru_next = p;
219 cf->bucket_lru_back = p;
220 cf->bucket_lru_front = p;
222 pp = cf->parray + hno;
226 (*pp)->h_prev = &p->h_next;
231 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
233 struct CFile_hash_bucket *p;
235 p = alloc_bucket (cf, block_no, hno);
236 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
238 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
241 assert (p->ph.this_bucket == block_no);
246 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
248 struct CFile_hash_bucket *p;
251 *block_no = cf->head.next_bucket++;
252 p = alloc_bucket (cf, *block_no, hno);
254 for (i = 0; i<HASH_BUCKET; i++)
259 p->ph.next_bucket = 0;
260 p->ph.this_bucket = *block_no;
265 static int cf_lookup_flat (CFile cf, int no)
267 int hno = (no*sizeof(int))/HASH_BSIZE;
268 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
271 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
275 static int cf_lookup_hash (CFile cf, int no)
277 int hno = cf_hash (cf, no);
278 struct CFile_hash_bucket *hb;
281 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
283 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
284 if (hb->ph.no[i] == no)
287 return hb->ph.vno[i];
290 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
292 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
294 if (hb->ph.this_bucket == block_no)
300 hb = get_bucket (cf, block_no, hno);
301 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
302 if (hb->ph.no[i] == no)
303 return hb->ph.vno[i];
308 static void cf_write_flat (CFile cf, int no, int vno)
310 int hno = (no*sizeof(int))/HASH_BSIZE;
311 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
313 hno += cf->head.next_bucket;
314 if (hno >= cf->head.flat_bucket)
315 cf->head.flat_bucket = hno+1;
316 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
319 static void cf_moveto_flat (CFile cf)
321 struct CFile_hash_bucket *p;
324 logf (LOG_LOG, "Moving to flat shadow.");
325 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
326 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
327 cf->head.next_bucket - cf->head.first_bucket);
328 assert (cf->head.state == 1);
329 flush_bucket (cf, -1);
330 assert (cf->bucket_in_memory == 0);
331 p = xmalloc (sizeof(*p));
332 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
334 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
336 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
339 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
340 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
350 static int cf_lookup (CFile cf, int no)
352 if (cf->head.state > 1)
353 return cf_lookup_flat (cf, no);
354 return cf_lookup_hash (cf, no);
357 static int cf_new_flat (CFile cf, int no)
359 int vno = (cf->head.next_block)++;
361 cf_write_flat (cf, no, vno);
365 static int cf_new_hash (CFile cf, int no)
367 int hno = cf_hash (cf, no);
368 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
369 int *bucketpp = &cf->array[hno];
370 int i, vno = (cf->head.next_block)++;
372 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
373 if (!hb->ph.vno[HASH_BUCKET-1])
374 for (i = 0; i<HASH_BUCKET; i++)
386 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
387 if (hb->ph.this_bucket == *bucketpp)
389 bucketpp = &hb->ph.next_bucket;
396 hb = get_bucket (cf, *bucketpp, hno);
398 for (i = 0; i<HASH_BUCKET; i++)
406 bucketpp = &hb->ph.next_bucket;
411 hb = new_bucket (cf, bucketpp, hno);
417 int cf_new (CFile cf, int no)
419 if (cf->head.state > 1)
420 return cf_new_flat (cf, no);
421 if (cf->no_miss*5 > cf->no_hits)
424 assert (cf->head.state > 1);
425 return cf_new_flat (cf, no);
427 return cf_new_hash (cf, no);
431 int cf_read (CFile cf, int no, int offset, int num, void *buf)
436 if (!(block = cf_lookup (cf, no)))
438 if (!mf_read (cf->block_mf, block, offset, num, buf))
440 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
446 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
451 if (!(block = cf_lookup (cf, no)))
453 block = cf_new (cf, no);
456 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
457 memcpy (cf->iobuf + offset, buf, num);
463 if (mf_write (cf->block_mf, block, offset, num, buf))
465 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
471 int cf_close (CFile cf)
473 logf (LOG_LOG, "cf_close");
474 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
475 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
476 cf->head.next_bucket - cf->head.first_bucket);
477 flush_bucket (cf, -1);
480 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
483 mf_close (cf->hash_mf);
484 mf_close (cf->block_mf);