2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1996-04-09 14:48:49 adam
8 * Bug fix: offset calculation when using flat files was completely broken.
10 * Revision 1.12 1996/04/09 06:47:28 adam
11 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
13 * Revision 1.11 1996/03/26 15:59:05 adam
14 * The directory of the shadow table file can be specified by the new
17 * Revision 1.10 1996/02/07 14:03:46 adam
18 * Work on flat indexed shadow files.
20 * Revision 1.9 1996/02/07 10:08:43 adam
21 * Work on flat shadow (not finished yet).
23 * Revision 1.8 1995/12/15 12:36:52 adam
24 * Moved hash file information to union.
25 * Renamed commit files.
27 * Revision 1.7 1995/12/15 10:35:07 adam
28 * Changed names of commit files.
30 * Revision 1.6 1995/12/11 09:03:53 adam
31 * New function: cf_unlink.
32 * New member of commit file head: state (0) deleted, (1) hash file.
34 * Revision 1.5 1995/12/08 16:21:14 adam
35 * Work on commit/update.
37 * Revision 1.4 1995/12/01 16:24:28 adam
38 * Commit files use separate meta file area.
40 * Revision 1.3 1995/12/01 11:37:22 adam
41 * Cached/commit files implemented as meta-files.
43 * Revision 1.2 1995/11/30 17:00:49 adam
44 * Several bug fixes. Commit system runs now.
46 * Revision 1.1 1995/11/30 08:33:11 adam
47 * Started work on commit facility.
59 static int write_head (CFile cf)
61 int left = cf->head.hash_size * sizeof(int);
63 const char *tab = (char*) cf->array;
67 while (left >= HASH_BSIZE)
69 mf_write (cf->hash_mf, bno++, 0, 0, tab);
74 mf_write (cf->hash_mf, bno, 0, left, tab);
78 static int read_head (CFile cf)
80 int left = cf->head.hash_size * sizeof(int);
82 char *tab = (char*) cf->array;
86 while (left >= HASH_BSIZE)
88 mf_read (cf->hash_mf, bno++, 0, 0, tab);
93 mf_read (cf->hash_mf, bno, 0, left, tab);
98 CFile cf_open (MFile mf, MFile_area area, const char *fname,
99 int block_size, int wflag, int *firstp)
103 CFile cf = xmalloc (sizeof(*cf));
107 logf (LOG_LOG, "cf_open %s", cf->rmf->name);
108 sprintf (path, "%s-b", fname);
109 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
111 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
114 sprintf (path, "%s-i", fname);
115 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
117 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
121 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
126 cf->head.block_size = block_size;
127 cf->head.hash_size = 199;
128 hash_bytes = cf->head.hash_size * sizeof(int);
129 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
130 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
131 cf->head.next_block = 0;
133 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
134 cf->array = xmalloc (hash_bytes);
135 for (i = 0; i<cf->head.hash_size; i++)
143 assert (cf->head.block_size == block_size);
144 assert (cf->head.hash_size > 2);
145 hash_bytes = cf->head.hash_size * sizeof(int);
146 assert (cf->head.next_bucket > 0);
147 if (cf->head.state == 1)
148 cf->array = xmalloc (hash_bytes);
153 if (cf->head.state == 1)
155 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
156 for (i = 0; i<cf->head.hash_size; i++)
157 cf->parray[i] = NULL;
161 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
162 cf->bucket_in_memory = 0;
163 cf->max_bucket_in_memory = 100;
165 cf->iobuf = xmalloc (cf->head.block_size);
166 memset (cf->iobuf, 0, cf->head.block_size);
172 static int cf_hash (CFile cf, int no)
174 return (no>>3) % cf->head.hash_size;
177 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
180 p->lru_prev->lru_next = p->lru_next;
182 cf->bucket_lru_back = p->lru_next;
184 p->lru_next->lru_prev = p->lru_prev;
186 cf->bucket_lru_front = p->lru_prev;
188 *p->h_prev = p->h_next;
190 p->h_next->h_prev = p->h_prev;
192 --(cf->bucket_in_memory);
196 static void flush_bucket (CFile cf, int no_to_flush)
199 struct CFile_hash_bucket *p;
201 for (i = 0; i != no_to_flush; i++)
203 p = cf->bucket_lru_back;
208 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
211 release_bucket (cf, p);
215 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
217 struct CFile_hash_bucket *p, **pp;
219 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
220 flush_bucket (cf, 1);
221 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
222 ++(cf->bucket_in_memory);
223 p = xmalloc (sizeof(*p));
226 p->lru_prev = cf->bucket_lru_front;
227 if (cf->bucket_lru_front)
228 cf->bucket_lru_front->lru_next = p;
230 cf->bucket_lru_back = p;
231 cf->bucket_lru_front = p;
233 pp = cf->parray + hno;
237 (*pp)->h_prev = &p->h_next;
242 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
244 struct CFile_hash_bucket *p;
246 p = alloc_bucket (cf, block_no, hno);
247 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
249 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
252 assert (p->ph.this_bucket == block_no);
257 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
259 struct CFile_hash_bucket *p;
262 *block_no = cf->head.next_bucket++;
263 p = alloc_bucket (cf, *block_no, hno);
265 for (i = 0; i<HASH_BUCKET; i++)
270 p->ph.next_bucket = 0;
271 p->ph.this_bucket = *block_no;
276 static int cf_lookup_flat (CFile cf, int no)
278 int hno = (no*sizeof(int))/HASH_BSIZE;
279 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
282 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
286 static int cf_lookup_hash (CFile cf, int no)
288 int hno = cf_hash (cf, no);
289 struct CFile_hash_bucket *hb;
292 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
294 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
295 if (hb->ph.no[i] == no)
298 return hb->ph.vno[i];
301 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
303 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
305 if (hb->ph.this_bucket == block_no)
311 hb = get_bucket (cf, block_no, hno);
312 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
313 if (hb->ph.no[i] == no)
314 return hb->ph.vno[i];
319 static void cf_write_flat (CFile cf, int no, int vno)
321 int hno = (no*sizeof(int))/HASH_BSIZE;
322 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
324 hno += cf->head.next_bucket;
325 if (hno >= cf->head.flat_bucket)
326 cf->head.flat_bucket = hno+1;
327 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
330 static void cf_moveto_flat (CFile cf)
332 struct CFile_hash_bucket *p;
335 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
336 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
337 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
338 cf->head.next_bucket - cf->head.first_bucket);
339 assert (cf->head.state == 1);
340 flush_bucket (cf, -1);
341 assert (cf->bucket_in_memory == 0);
342 p = xmalloc (sizeof(*p));
343 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
345 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
347 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
350 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
351 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
361 static int cf_lookup (CFile cf, int no)
363 if (cf->head.state > 1)
364 return cf_lookup_flat (cf, no);
365 return cf_lookup_hash (cf, no);
368 static int cf_new_flat (CFile cf, int no)
370 int vno = (cf->head.next_block)++;
372 cf_write_flat (cf, no, vno);
376 static int cf_new_hash (CFile cf, int no)
378 int hno = cf_hash (cf, no);
379 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
380 int *bucketpp = &cf->array[hno];
381 int i, vno = (cf->head.next_block)++;
383 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
384 if (!hb->ph.vno[HASH_BUCKET-1])
385 for (i = 0; i<HASH_BUCKET; i++)
397 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
398 if (hb->ph.this_bucket == *bucketpp)
400 bucketpp = &hb->ph.next_bucket;
407 hb = get_bucket (cf, *bucketpp, hno);
409 for (i = 0; i<HASH_BUCKET; i++)
417 bucketpp = &hb->ph.next_bucket;
422 hb = new_bucket (cf, bucketpp, hno);
428 int cf_new (CFile cf, int no)
430 if (cf->head.state > 1)
431 return cf_new_flat (cf, no);
432 if (cf->no_miss*5 > cf->no_hits)
435 assert (cf->head.state > 1);
436 return cf_new_flat (cf, no);
438 return cf_new_hash (cf, no);
442 int cf_read (CFile cf, int no, int offset, int num, void *buf)
447 if (!(block = cf_lookup (cf, no)))
449 if (!mf_read (cf->block_mf, block, offset, num, buf))
451 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
457 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
462 if (!(block = cf_lookup (cf, no)))
464 block = cf_new (cf, no);
467 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
468 memcpy (cf->iobuf + offset, buf, num);
474 if (mf_write (cf->block_mf, block, offset, num, buf))
476 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
482 int cf_close (CFile cf)
484 logf (LOG_LOG, "cf_close %s", cf->rmf->name);
485 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
486 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
487 cf->head.next_bucket - cf->head.first_bucket);
488 flush_bucket (cf, -1);
491 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
494 mf_close (cf->hash_mf);
495 mf_close (cf->block_mf);