2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1996-03-26 15:59:05 adam
8 * The directory of the shadow table file can be specified by the new
11 * Revision 1.10 1996/02/07 14:03:46 adam
12 * Work on flat indexed shadow files.
14 * Revision 1.9 1996/02/07 10:08:43 adam
15 * Work on flat shadow (not finished yet).
17 * Revision 1.8 1995/12/15 12:36:52 adam
18 * Moved hash file information to union.
19 * Renamed commit files.
21 * Revision 1.7 1995/12/15 10:35:07 adam
22 * Changed names of commit files.
24 * Revision 1.6 1995/12/11 09:03:53 adam
25 * New function: cf_unlink.
26 * New member of commit file head: state (0) deleted, (1) hash file.
28 * Revision 1.5 1995/12/08 16:21:14 adam
29 * Work on commit/update.
31 * Revision 1.4 1995/12/01 16:24:28 adam
32 * Commit files use separate meta file area.
34 * Revision 1.3 1995/12/01 11:37:22 adam
35 * Cached/commit files implemented as meta-files.
37 * Revision 1.2 1995/11/30 17:00:49 adam
38 * Several bug fixes. Commit system runs now.
40 * Revision 1.1 1995/11/30 08:33:11 adam
41 * Started work on commit facility.
53 static int write_head (CFile cf)
55 int left = cf->head.hash_size * sizeof(int);
57 const char *tab = (char*) cf->array;
61 while (left >= HASH_BSIZE)
63 mf_write (cf->hash_mf, bno++, 0, 0, tab);
68 mf_write (cf->hash_mf, bno, 0, left, tab);
72 static int read_head (CFile cf)
74 int left = cf->head.hash_size * sizeof(int);
76 char *tab = (char*) cf->array;
80 while (left >= HASH_BSIZE)
82 mf_read (cf->hash_mf, bno++, 0, 0, tab);
87 mf_read (cf->hash_mf, bno, 0, left, tab);
92 CFile cf_open (MFile mf, MFile_area area, const char *fname,
93 int block_size, int wflag, int *firstp)
97 CFile cf = xmalloc (sizeof(*cf));
101 sprintf (path, "%s-b", fname);
102 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
104 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
107 sprintf (path, "%s-i", fname);
108 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
110 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
114 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
119 cf->head.block_size = block_size;
120 cf->head.hash_size = 199;
121 hash_bytes = cf->head.hash_size * sizeof(int);
122 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
123 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
124 cf->head.next_block = 1;
126 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
127 cf->array = xmalloc (hash_bytes);
128 for (i = 0; i<cf->head.hash_size; i++)
136 assert (cf->head.block_size == block_size);
137 assert (cf->head.hash_size > 2);
138 hash_bytes = cf->head.hash_size * sizeof(int);
139 assert (cf->head.next_bucket > 0);
140 if (cf->head.state == 1)
141 cf->array = xmalloc (hash_bytes);
146 if (cf->head.state == 1)
148 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
149 for (i = 0; i<cf->head.hash_size; i++)
150 cf->parray[i] = NULL;
154 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
155 cf->bucket_in_memory = 0;
156 cf->max_bucket_in_memory = 100;
158 cf->iobuf = xmalloc (cf->head.block_size);
159 memset (cf->iobuf, 0, cf->head.block_size);
165 static int cf_hash (CFile cf, int no)
167 return (no>>3) % cf->head.hash_size;
170 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
173 p->lru_prev->lru_next = p->lru_next;
175 cf->bucket_lru_back = p->lru_next;
177 p->lru_next->lru_prev = p->lru_prev;
179 cf->bucket_lru_front = p->lru_prev;
181 *p->h_prev = p->h_next;
183 p->h_next->h_prev = p->h_prev;
185 --(cf->bucket_in_memory);
189 static void flush_bucket (CFile cf, int no_to_flush)
192 struct CFile_hash_bucket *p;
194 for (i = 0; i != no_to_flush; i++)
196 p = cf->bucket_lru_back;
201 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
204 release_bucket (cf, p);
208 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
210 struct CFile_hash_bucket *p, **pp;
212 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
213 flush_bucket (cf, 1);
214 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
215 ++(cf->bucket_in_memory);
216 p = xmalloc (sizeof(*p));
219 p->lru_prev = cf->bucket_lru_front;
220 if (cf->bucket_lru_front)
221 cf->bucket_lru_front->lru_next = p;
223 cf->bucket_lru_back = p;
224 cf->bucket_lru_front = p;
226 pp = cf->parray + hno;
230 (*pp)->h_prev = &p->h_next;
235 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
237 struct CFile_hash_bucket *p;
239 p = alloc_bucket (cf, block_no, hno);
240 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
242 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
245 assert (p->ph.this_bucket == block_no);
250 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
252 struct CFile_hash_bucket *p;
255 *block_no = cf->head.next_bucket++;
256 p = alloc_bucket (cf, *block_no, hno);
258 for (i = 0; i<HASH_BUCKET; i++)
263 p->ph.next_bucket = 0;
264 p->ph.this_bucket = *block_no;
269 static int cf_lookup_flat (CFile cf, int no)
271 int hno = (no*sizeof(int))/HASH_BSIZE;
272 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
275 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
279 static int cf_lookup_hash (CFile cf, int no)
281 int hno = cf_hash (cf, no);
282 struct CFile_hash_bucket *hb;
285 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
287 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
288 if (hb->ph.no[i] == no)
291 return hb->ph.vno[i];
294 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
296 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
298 if (hb->ph.this_bucket == block_no)
304 hb = get_bucket (cf, block_no, hno);
305 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
306 if (hb->ph.no[i] == no)
307 return hb->ph.vno[i];
312 static void cf_write_flat (CFile cf, int no, int vno)
314 int hno = (no*sizeof(int))/HASH_BSIZE;
315 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
317 hno += cf->head.next_bucket;
318 if (hno >= cf->head.flat_bucket)
319 cf->head.flat_bucket = hno+1;
320 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
323 static void cf_moveto_flat (CFile cf)
325 struct CFile_hash_bucket *p;
328 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
329 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
330 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
331 cf->head.next_bucket - cf->head.first_bucket);
332 assert (cf->head.state == 1);
333 flush_bucket (cf, -1);
334 assert (cf->bucket_in_memory == 0);
335 p = xmalloc (sizeof(*p));
336 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
338 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
340 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
343 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
344 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
354 static int cf_lookup (CFile cf, int no)
356 if (cf->head.state > 1)
357 return cf_lookup_flat (cf, no);
358 return cf_lookup_hash (cf, no);
361 static int cf_new_flat (CFile cf, int no)
363 int vno = (cf->head.next_block)++;
365 cf_write_flat (cf, no, vno);
369 static int cf_new_hash (CFile cf, int no)
371 int hno = cf_hash (cf, no);
372 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
373 int *bucketpp = &cf->array[hno];
374 int i, vno = (cf->head.next_block)++;
376 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
377 if (!hb->ph.vno[HASH_BUCKET-1])
378 for (i = 0; i<HASH_BUCKET; i++)
390 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
391 if (hb->ph.this_bucket == *bucketpp)
393 bucketpp = &hb->ph.next_bucket;
400 hb = get_bucket (cf, *bucketpp, hno);
402 for (i = 0; i<HASH_BUCKET; i++)
410 bucketpp = &hb->ph.next_bucket;
415 hb = new_bucket (cf, bucketpp, hno);
421 int cf_new (CFile cf, int no)
423 if (cf->head.state > 1)
424 return cf_new_flat (cf, no);
425 if (cf->no_miss*5 > cf->no_hits)
428 assert (cf->head.state > 1);
429 return cf_new_flat (cf, no);
431 return cf_new_hash (cf, no);
435 int cf_read (CFile cf, int no, int offset, int num, void *buf)
440 if (!(block = cf_lookup (cf, no)))
442 if (!mf_read (cf->block_mf, block, offset, num, buf))
444 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
450 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
455 if (!(block = cf_lookup (cf, no)))
457 block = cf_new (cf, no);
460 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
461 memcpy (cf->iobuf + offset, buf, num);
467 if (mf_write (cf->block_mf, block, offset, num, buf))
469 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
475 int cf_close (CFile cf)
477 logf (LOG_LOG, "cf_close");
478 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
479 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
480 cf->head.next_bucket - cf->head.first_bucket);
481 flush_bucket (cf, -1);
484 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
487 mf_close (cf->hash_mf);
488 mf_close (cf->block_mf);