2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1996-04-12 07:01:55 adam
8 * Yet another bug fix (next_block was initialized to 0; now set to 1).
10 * Revision 1.13 1996/04/09 14:48:49 adam
11 * Bug fix: offset calculation when using flat files was completely broken.
13 * Revision 1.12 1996/04/09 06:47:28 adam
14 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
16 * Revision 1.11 1996/03/26 15:59:05 adam
17 * The directory of the shadow table file can be specified by the new
20 * Revision 1.10 1996/02/07 14:03:46 adam
21 * Work on flat indexed shadow files.
23 * Revision 1.9 1996/02/07 10:08:43 adam
24 * Work on flat shadow (not finished yet).
26 * Revision 1.8 1995/12/15 12:36:52 adam
27 * Moved hash file information to union.
28 * Renamed commit files.
30 * Revision 1.7 1995/12/15 10:35:07 adam
31 * Changed names of commit files.
33 * Revision 1.6 1995/12/11 09:03:53 adam
34 * New function: cf_unlink.
35 * New member of commit file head: state (0) deleted, (1) hash file.
37 * Revision 1.5 1995/12/08 16:21:14 adam
38 * Work on commit/update.
40 * Revision 1.4 1995/12/01 16:24:28 adam
41 * Commit files use separate meta file area.
43 * Revision 1.3 1995/12/01 11:37:22 adam
44 * Cached/commit files implemented as meta-files.
46 * Revision 1.2 1995/11/30 17:00:49 adam
47 * Several bug fixes. Commit system runs now.
49 * Revision 1.1 1995/11/30 08:33:11 adam
50 * Started work on commit facility.
62 static int write_head (CFile cf)
64 int left = cf->head.hash_size * sizeof(int);
66 const char *tab = (char*) cf->array;
70 while (left >= HASH_BSIZE)
72 mf_write (cf->hash_mf, bno++, 0, 0, tab);
77 mf_write (cf->hash_mf, bno, 0, left, tab);
81 static int read_head (CFile cf)
83 int left = cf->head.hash_size * sizeof(int);
85 char *tab = (char*) cf->array;
89 while (left >= HASH_BSIZE)
91 mf_read (cf->hash_mf, bno++, 0, 0, tab);
96 mf_read (cf->hash_mf, bno, 0, left, tab);
101 CFile cf_open (MFile mf, MFile_area area, const char *fname,
102 int block_size, int wflag, int *firstp)
106 CFile cf = xmalloc (sizeof(*cf));
110 logf (LOG_LOG, "cf_open %s", cf->rmf->name);
111 sprintf (path, "%s-b", fname);
112 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
114 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
117 sprintf (path, "%s-i", fname);
118 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
120 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
124 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
129 cf->head.block_size = block_size;
130 cf->head.hash_size = 199;
131 hash_bytes = cf->head.hash_size * sizeof(int);
132 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
133 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
134 cf->head.next_block = 1;
136 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
137 cf->array = xmalloc (hash_bytes);
138 for (i = 0; i<cf->head.hash_size; i++)
146 assert (cf->head.block_size == block_size);
147 assert (cf->head.hash_size > 2);
148 hash_bytes = cf->head.hash_size * sizeof(int);
149 assert (cf->head.next_bucket > 0);
150 if (cf->head.state == 1)
151 cf->array = xmalloc (hash_bytes);
156 if (cf->head.state == 1)
158 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
159 for (i = 0; i<cf->head.hash_size; i++)
160 cf->parray[i] = NULL;
164 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
165 cf->bucket_in_memory = 0;
166 cf->max_bucket_in_memory = 100;
168 cf->iobuf = xmalloc (cf->head.block_size);
169 memset (cf->iobuf, 0, cf->head.block_size);
175 static int cf_hash (CFile cf, int no)
177 return (no>>3) % cf->head.hash_size;
180 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
183 p->lru_prev->lru_next = p->lru_next;
185 cf->bucket_lru_back = p->lru_next;
187 p->lru_next->lru_prev = p->lru_prev;
189 cf->bucket_lru_front = p->lru_prev;
191 *p->h_prev = p->h_next;
193 p->h_next->h_prev = p->h_prev;
195 --(cf->bucket_in_memory);
199 static void flush_bucket (CFile cf, int no_to_flush)
202 struct CFile_hash_bucket *p;
204 for (i = 0; i != no_to_flush; i++)
206 p = cf->bucket_lru_back;
211 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
214 release_bucket (cf, p);
218 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
220 struct CFile_hash_bucket *p, **pp;
222 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
223 flush_bucket (cf, 1);
224 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
225 ++(cf->bucket_in_memory);
226 p = xmalloc (sizeof(*p));
229 p->lru_prev = cf->bucket_lru_front;
230 if (cf->bucket_lru_front)
231 cf->bucket_lru_front->lru_next = p;
233 cf->bucket_lru_back = p;
234 cf->bucket_lru_front = p;
236 pp = cf->parray + hno;
240 (*pp)->h_prev = &p->h_next;
245 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
247 struct CFile_hash_bucket *p;
249 p = alloc_bucket (cf, block_no, hno);
250 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
252 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
255 assert (p->ph.this_bucket == block_no);
260 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
262 struct CFile_hash_bucket *p;
265 *block_no = cf->head.next_bucket++;
266 p = alloc_bucket (cf, *block_no, hno);
268 for (i = 0; i<HASH_BUCKET; i++)
273 p->ph.next_bucket = 0;
274 p->ph.this_bucket = *block_no;
279 static int cf_lookup_flat (CFile cf, int no)
281 int hno = (no*sizeof(int))/HASH_BSIZE;
282 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
285 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
289 static int cf_lookup_hash (CFile cf, int no)
291 int hno = cf_hash (cf, no);
292 struct CFile_hash_bucket *hb;
295 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
297 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
298 if (hb->ph.no[i] == no)
301 return hb->ph.vno[i];
304 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
306 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
308 if (hb->ph.this_bucket == block_no)
314 hb = get_bucket (cf, block_no, hno);
315 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
316 if (hb->ph.no[i] == no)
317 return hb->ph.vno[i];
322 static void cf_write_flat (CFile cf, int no, int vno)
324 int hno = (no*sizeof(int))/HASH_BSIZE;
325 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
327 hno += cf->head.next_bucket;
328 if (hno >= cf->head.flat_bucket)
329 cf->head.flat_bucket = hno+1;
331 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
334 static void cf_moveto_flat (CFile cf)
336 struct CFile_hash_bucket *p;
339 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
340 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
341 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
342 cf->head.next_bucket - cf->head.first_bucket);
343 assert (cf->head.state == 1);
344 flush_bucket (cf, -1);
345 assert (cf->bucket_in_memory == 0);
346 p = xmalloc (sizeof(*p));
347 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
349 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
351 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
354 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
355 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
365 static int cf_lookup (CFile cf, int no)
367 if (cf->head.state > 1)
368 return cf_lookup_flat (cf, no);
369 return cf_lookup_hash (cf, no);
372 static int cf_new_flat (CFile cf, int no)
374 int vno = (cf->head.next_block)++;
376 cf_write_flat (cf, no, vno);
380 static int cf_new_hash (CFile cf, int no)
382 int hno = cf_hash (cf, no);
383 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
384 int *bucketpp = &cf->array[hno];
385 int i, vno = (cf->head.next_block)++;
387 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
388 if (!hb->ph.vno[HASH_BUCKET-1])
389 for (i = 0; i<HASH_BUCKET; i++)
401 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
402 if (hb->ph.this_bucket == *bucketpp)
404 bucketpp = &hb->ph.next_bucket;
411 hb = get_bucket (cf, *bucketpp, hno);
413 for (i = 0; i<HASH_BUCKET; i++)
421 bucketpp = &hb->ph.next_bucket;
426 hb = new_bucket (cf, bucketpp, hno);
432 int cf_new (CFile cf, int no)
434 if (cf->head.state > 1)
435 return cf_new_flat (cf, no);
436 if (cf->no_miss*5 > cf->no_hits)
439 assert (cf->head.state > 1);
440 return cf_new_flat (cf, no);
442 return cf_new_hash (cf, no);
446 int cf_read (CFile cf, int no, int offset, int num, void *buf)
451 if (!(block = cf_lookup (cf, no)))
453 if (!mf_read (cf->block_mf, block, offset, num, buf))
455 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
461 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
466 if (!(block = cf_lookup (cf, no)))
468 block = cf_new (cf, no);
471 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
472 memcpy (cf->iobuf + offset, buf, num);
478 if (mf_write (cf->block_mf, block, offset, num, buf))
480 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
486 int cf_close (CFile cf)
488 logf (LOG_LOG, "cf_close %s", cf->rmf->name);
489 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
490 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
491 cf->head.next_bucket - cf->head.first_bucket);
492 flush_bucket (cf, -1);
495 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
498 mf_close (cf->hash_mf);
499 mf_close (cf->block_mf);