2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1996-04-18 16:02:56 adam
8 * Changed logging a bit.
9 * Removed warning message when commiting flat shadow files.
11 * Revision 1.14 1996/04/12 07:01:55 adam
12 * Yet another bug fix (next_block was initialized to 0; now set to 1).
14 * Revision 1.13 1996/04/09 14:48:49 adam
15 * Bug fix: offset calculation when using flat files was completely broken.
17 * Revision 1.12 1996/04/09 06:47:28 adam
18 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
20 * Revision 1.11 1996/03/26 15:59:05 adam
21 * The directory of the shadow table file can be specified by the new
24 * Revision 1.10 1996/02/07 14:03:46 adam
25 * Work on flat indexed shadow files.
27 * Revision 1.9 1996/02/07 10:08:43 adam
28 * Work on flat shadow (not finished yet).
30 * Revision 1.8 1995/12/15 12:36:52 adam
31 * Moved hash file information to union.
32 * Renamed commit files.
34 * Revision 1.7 1995/12/15 10:35:07 adam
35 * Changed names of commit files.
37 * Revision 1.6 1995/12/11 09:03:53 adam
38 * New function: cf_unlink.
39 * New member of commit file head: state (0) deleted, (1) hash file.
41 * Revision 1.5 1995/12/08 16:21:14 adam
42 * Work on commit/update.
44 * Revision 1.4 1995/12/01 16:24:28 adam
45 * Commit files use separate meta file area.
47 * Revision 1.3 1995/12/01 11:37:22 adam
48 * Cached/commit files implemented as meta-files.
50 * Revision 1.2 1995/11/30 17:00:49 adam
51 * Several bug fixes. Commit system runs now.
53 * Revision 1.1 1995/11/30 08:33:11 adam
54 * Started work on commit facility.
66 static int write_head (CFile cf)
68 int left = cf->head.hash_size * sizeof(int);
70 const char *tab = (char*) cf->array;
74 while (left >= HASH_BSIZE)
76 mf_write (cf->hash_mf, bno++, 0, 0, tab);
81 mf_write (cf->hash_mf, bno, 0, left, tab);
85 static int read_head (CFile cf)
87 int left = cf->head.hash_size * sizeof(int);
89 char *tab = (char*) cf->array;
93 while (left >= HASH_BSIZE)
95 mf_read (cf->hash_mf, bno++, 0, 0, tab);
100 mf_read (cf->hash_mf, bno, 0, left, tab);
105 CFile cf_open (MFile mf, MFile_area area, const char *fname,
106 int block_size, int wflag, int *firstp)
110 CFile cf = xmalloc (sizeof(*cf));
114 logf (LOG_LOG, "cf_open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
115 sprintf (path, "%s-b", fname);
116 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
118 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
121 sprintf (path, "%s-i", fname);
122 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
124 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
128 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
133 cf->head.block_size = block_size;
134 cf->head.hash_size = 199;
135 hash_bytes = cf->head.hash_size * sizeof(int);
136 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
137 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
138 cf->head.next_block = 1;
140 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
141 cf->array = xmalloc (hash_bytes);
142 for (i = 0; i<cf->head.hash_size; i++)
150 assert (cf->head.block_size == block_size);
151 assert (cf->head.hash_size > 2);
152 hash_bytes = cf->head.hash_size * sizeof(int);
153 assert (cf->head.next_bucket > 0);
154 assert (cf->head.next_block > 0);
155 if (cf->head.state == 1)
156 cf->array = xmalloc (hash_bytes);
161 if (cf->head.state == 1)
163 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
164 for (i = 0; i<cf->head.hash_size; i++)
165 cf->parray[i] = NULL;
169 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
170 cf->bucket_in_memory = 0;
171 cf->max_bucket_in_memory = 100;
173 cf->iobuf = xmalloc (cf->head.block_size);
174 memset (cf->iobuf, 0, cf->head.block_size);
180 static int cf_hash (CFile cf, int no)
182 return (no>>3) % cf->head.hash_size;
185 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
188 p->lru_prev->lru_next = p->lru_next;
190 cf->bucket_lru_back = p->lru_next;
192 p->lru_next->lru_prev = p->lru_prev;
194 cf->bucket_lru_front = p->lru_prev;
196 *p->h_prev = p->h_next;
198 p->h_next->h_prev = p->h_prev;
200 --(cf->bucket_in_memory);
204 static void flush_bucket (CFile cf, int no_to_flush)
207 struct CFile_hash_bucket *p;
209 for (i = 0; i != no_to_flush; i++)
211 p = cf->bucket_lru_back;
216 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
219 release_bucket (cf, p);
223 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
225 struct CFile_hash_bucket *p, **pp;
227 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
228 flush_bucket (cf, 1);
229 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
230 ++(cf->bucket_in_memory);
231 p = xmalloc (sizeof(*p));
234 p->lru_prev = cf->bucket_lru_front;
235 if (cf->bucket_lru_front)
236 cf->bucket_lru_front->lru_next = p;
238 cf->bucket_lru_back = p;
239 cf->bucket_lru_front = p;
241 pp = cf->parray + hno;
245 (*pp)->h_prev = &p->h_next;
250 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
252 struct CFile_hash_bucket *p;
254 p = alloc_bucket (cf, block_no, hno);
255 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
257 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
260 assert (p->ph.this_bucket == block_no);
265 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
267 struct CFile_hash_bucket *p;
270 *block_no = cf->head.next_bucket++;
271 p = alloc_bucket (cf, *block_no, hno);
273 for (i = 0; i<HASH_BUCKET; i++)
278 p->ph.next_bucket = 0;
279 p->ph.this_bucket = *block_no;
284 static int cf_lookup_flat (CFile cf, int no)
286 int hno = (no*sizeof(int))/HASH_BSIZE;
287 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
290 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
294 static int cf_lookup_hash (CFile cf, int no)
296 int hno = cf_hash (cf, no);
297 struct CFile_hash_bucket *hb;
300 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
302 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
303 if (hb->ph.no[i] == no)
306 return hb->ph.vno[i];
309 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
311 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
313 if (hb->ph.this_bucket == block_no)
319 hb = get_bucket (cf, block_no, hno);
320 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
321 if (hb->ph.no[i] == no)
322 return hb->ph.vno[i];
327 static void cf_write_flat (CFile cf, int no, int vno)
329 int hno = (no*sizeof(int))/HASH_BSIZE;
330 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
332 hno += cf->head.next_bucket;
333 if (hno >= cf->head.flat_bucket)
334 cf->head.flat_bucket = hno+1;
336 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
339 static void cf_moveto_flat (CFile cf)
341 struct CFile_hash_bucket *p;
344 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
345 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
346 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
347 cf->head.next_bucket - cf->head.first_bucket);
348 assert (cf->head.state == 1);
349 flush_bucket (cf, -1);
350 assert (cf->bucket_in_memory == 0);
351 p = xmalloc (sizeof(*p));
352 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
354 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
356 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
359 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
360 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
371 static int cf_lookup (CFile cf, int no)
373 if (cf->head.state > 1)
374 return cf_lookup_flat (cf, no);
375 return cf_lookup_hash (cf, no);
378 static int cf_new_flat (CFile cf, int no)
380 int vno = (cf->head.next_block)++;
382 cf_write_flat (cf, no, vno);
386 static int cf_new_hash (CFile cf, int no)
388 int hno = cf_hash (cf, no);
389 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
390 int *bucketpp = &cf->array[hno];
391 int i, vno = (cf->head.next_block)++;
393 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
394 if (!hb->ph.vno[HASH_BUCKET-1])
395 for (i = 0; i<HASH_BUCKET; i++)
407 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
408 if (hb->ph.this_bucket == *bucketpp)
410 bucketpp = &hb->ph.next_bucket;
417 hb = get_bucket (cf, *bucketpp, hno);
419 for (i = 0; i<HASH_BUCKET; i++)
427 bucketpp = &hb->ph.next_bucket;
432 hb = new_bucket (cf, bucketpp, hno);
438 int cf_new (CFile cf, int no)
440 if (cf->head.state > 1)
441 return cf_new_flat (cf, no);
442 if (cf->no_miss*5 > cf->no_hits)
445 assert (cf->head.state > 1);
446 return cf_new_flat (cf, no);
448 return cf_new_hash (cf, no);
452 int cf_read (CFile cf, int no, int offset, int num, void *buf)
457 if (!(block = cf_lookup (cf, no)))
459 if (!mf_read (cf->block_mf, block, offset, num, buf))
461 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
467 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
472 if (!(block = cf_lookup (cf, no)))
474 block = cf_new (cf, no);
477 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
478 memcpy (cf->iobuf + offset, buf, num);
484 if (mf_write (cf->block_mf, block, offset, num, buf))
486 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
492 int cf_close (CFile cf)
494 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
495 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
496 cf->head.next_bucket - cf->head.first_bucket);
497 flush_bucket (cf, -1);
500 logf (LOG_LOG, "dirty. write header");
501 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
504 mf_close (cf->hash_mf);
505 mf_close (cf->block_mf);
510 logf (LOG_LOG, "cf_close %s", cf->rmf->name);