2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1996-04-19 16:49:00 adam
10 * Revision 1.16 1996/04/19 16:23:47 adam
11 * Serious bug fix in shadow implementation; function new_bucket might
12 * set wrong bucket number on new bucket.
14 * Revision 1.15 1996/04/18 16:02:56 adam
15 * Changed logging a bit.
16 * Removed warning message when commiting flat shadow files.
18 * Revision 1.14 1996/04/12 07:01:55 adam
19 * Yet another bug fix (next_block was initialized to 0; now set to 1).
21 * Revision 1.13 1996/04/09 14:48:49 adam
22 * Bug fix: offset calculation when using flat files was completely broken.
24 * Revision 1.12 1996/04/09 06:47:28 adam
25 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
27 * Revision 1.11 1996/03/26 15:59:05 adam
28 * The directory of the shadow table file can be specified by the new
31 * Revision 1.10 1996/02/07 14:03:46 adam
32 * Work on flat indexed shadow files.
34 * Revision 1.9 1996/02/07 10:08:43 adam
35 * Work on flat shadow (not finished yet).
37 * Revision 1.8 1995/12/15 12:36:52 adam
38 * Moved hash file information to union.
39 * Renamed commit files.
41 * Revision 1.7 1995/12/15 10:35:07 adam
42 * Changed names of commit files.
44 * Revision 1.6 1995/12/11 09:03:53 adam
45 * New function: cf_unlink.
46 * New member of commit file head: state (0) deleted, (1) hash file.
48 * Revision 1.5 1995/12/08 16:21:14 adam
49 * Work on commit/update.
51 * Revision 1.4 1995/12/01 16:24:28 adam
52 * Commit files use separate meta file area.
54 * Revision 1.3 1995/12/01 11:37:22 adam
55 * Cached/commit files implemented as meta-files.
57 * Revision 1.2 1995/11/30 17:00:49 adam
58 * Several bug fixes. Commit system runs now.
60 * Revision 1.1 1995/11/30 08:33:11 adam
61 * Started work on commit facility.
73 static int write_head (CFile cf)
75 int left = cf->head.hash_size * sizeof(int);
77 const char *tab = (char*) cf->array;
81 while (left >= HASH_BSIZE)
83 mf_write (cf->hash_mf, bno++, 0, 0, tab);
88 mf_write (cf->hash_mf, bno, 0, left, tab);
92 static int read_head (CFile cf)
94 int left = cf->head.hash_size * sizeof(int);
96 char *tab = (char*) cf->array;
100 while (left >= HASH_BSIZE)
102 mf_read (cf->hash_mf, bno++, 0, 0, tab);
107 mf_read (cf->hash_mf, bno, 0, left, tab);
112 CFile cf_open (MFile mf, MFile_area area, const char *fname,
113 int block_size, int wflag, int *firstp)
117 CFile cf = xmalloc (sizeof(*cf));
121 logf (LOG_LOG, "cf_open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
122 sprintf (path, "%s-b", fname);
123 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
125 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
128 sprintf (path, "%s-i", fname);
129 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
131 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
135 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
140 cf->head.block_size = block_size;
141 cf->head.hash_size = 199;
142 hash_bytes = cf->head.hash_size * sizeof(int);
143 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
144 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
145 cf->head.next_block = 1;
147 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
148 cf->array = xmalloc (hash_bytes);
149 for (i = 0; i<cf->head.hash_size; i++)
157 assert (cf->head.block_size == block_size);
158 assert (cf->head.hash_size > 2);
159 hash_bytes = cf->head.hash_size * sizeof(int);
160 assert (cf->head.next_bucket > 0);
161 assert (cf->head.next_block > 0);
162 if (cf->head.state == 1)
163 cf->array = xmalloc (hash_bytes);
168 if (cf->head.state == 1)
170 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
171 for (i = 0; i<cf->head.hash_size; i++)
172 cf->parray[i] = NULL;
176 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
177 cf->bucket_in_memory = 0;
178 cf->max_bucket_in_memory = 100;
180 cf->iobuf = xmalloc (cf->head.block_size);
181 memset (cf->iobuf, 0, cf->head.block_size);
187 static int cf_hash (CFile cf, int no)
189 return (no>>3) % cf->head.hash_size;
192 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
195 p->lru_prev->lru_next = p->lru_next;
197 cf->bucket_lru_back = p->lru_next;
199 p->lru_next->lru_prev = p->lru_prev;
201 cf->bucket_lru_front = p->lru_prev;
203 *p->h_prev = p->h_next;
205 p->h_next->h_prev = p->h_prev;
207 --(cf->bucket_in_memory);
211 static void flush_bucket (CFile cf, int no_to_flush)
214 struct CFile_hash_bucket *p;
216 for (i = 0; i != no_to_flush; i++)
218 p = cf->bucket_lru_back;
223 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
226 release_bucket (cf, p);
230 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
232 struct CFile_hash_bucket *p, **pp;
234 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
235 flush_bucket (cf, 1);
236 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
237 ++(cf->bucket_in_memory);
238 p = xmalloc (sizeof(*p));
241 p->lru_prev = cf->bucket_lru_front;
242 if (cf->bucket_lru_front)
243 cf->bucket_lru_front->lru_next = p;
245 cf->bucket_lru_back = p;
246 cf->bucket_lru_front = p;
248 pp = cf->parray + hno;
252 (*pp)->h_prev = &p->h_next;
257 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
259 struct CFile_hash_bucket *p;
261 p = alloc_bucket (cf, block_no, hno);
262 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
264 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
267 assert (p->ph.this_bucket == block_no);
272 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
274 struct CFile_hash_bucket *p;
277 block_no = *block_nop = cf->head.next_bucket++;
278 p = alloc_bucket (cf, block_no, hno);
280 for (i = 0; i<HASH_BUCKET; i++)
285 p->ph.next_bucket = 0;
286 p->ph.this_bucket = block_no;
291 static int cf_lookup_flat (CFile cf, int no)
293 int hno = (no*sizeof(int))/HASH_BSIZE;
294 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
297 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
301 static int cf_lookup_hash (CFile cf, int no)
303 int hno = cf_hash (cf, no);
304 struct CFile_hash_bucket *hb;
307 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
309 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
310 if (hb->ph.no[i] == no)
313 return hb->ph.vno[i];
316 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
318 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
320 if (hb->ph.this_bucket == block_no)
326 /* extra check ... */
327 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
329 if (hb->ph.this_bucket == block_no)
331 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
334 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
335 if (hb->ph.no[i] == no)
337 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
343 hb = get_bucket (cf, block_no, hno);
344 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
345 if (hb->ph.no[i] == no)
346 return hb->ph.vno[i];
351 static void cf_write_flat (CFile cf, int no, int vno)
353 int hno = (no*sizeof(int))/HASH_BSIZE;
354 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
356 hno += cf->head.next_bucket;
357 if (hno >= cf->head.flat_bucket)
358 cf->head.flat_bucket = hno+1;
360 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
363 static void cf_moveto_flat (CFile cf)
365 struct CFile_hash_bucket *p;
368 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
369 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
370 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
371 cf->head.next_bucket - cf->head.first_bucket);
372 assert (cf->head.state == 1);
373 flush_bucket (cf, -1);
374 assert (cf->bucket_in_memory == 0);
375 p = xmalloc (sizeof(*p));
376 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
378 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
380 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
383 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
384 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
395 static int cf_lookup (CFile cf, int no)
397 if (cf->head.state > 1)
398 return cf_lookup_flat (cf, no);
399 return cf_lookup_hash (cf, no);
402 static int cf_new_flat (CFile cf, int no)
404 int vno = (cf->head.next_block)++;
406 cf_write_flat (cf, no, vno);
410 static int cf_new_hash (CFile cf, int no)
412 int hno = cf_hash (cf, no);
413 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
414 int *bucketpp = &cf->array[hno];
415 int i, vno = (cf->head.next_block)++;
417 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
418 if (!hb->ph.vno[HASH_BUCKET-1])
419 for (i = 0; i<HASH_BUCKET; i++)
431 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
432 if (hb->ph.this_bucket == *bucketpp)
434 bucketpp = &hb->ph.next_bucket;
442 /* extra check ... */
443 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
445 if (hb->ph.this_bucket == *bucketpp)
447 logf (LOG_FATAL, "Found hash bucket on other chain");
453 hb = get_bucket (cf, *bucketpp, hno);
455 for (i = 0; i<HASH_BUCKET; i++)
463 bucketpp = &hb->ph.next_bucket;
468 hb = new_bucket (cf, bucketpp, hno);
474 int cf_new (CFile cf, int no)
476 if (cf->head.state > 1)
477 return cf_new_flat (cf, no);
478 if (cf->no_miss*2 > cf->no_hits)
481 assert (cf->head.state > 1);
482 return cf_new_flat (cf, no);
484 return cf_new_hash (cf, no);
488 int cf_read (CFile cf, int no, int offset, int num, void *buf)
493 if (!(block = cf_lookup (cf, no)))
495 if (!mf_read (cf->block_mf, block, offset, num, buf))
497 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
503 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
508 if (!(block = cf_lookup (cf, no)))
510 block = cf_new (cf, no);
513 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
514 memcpy (cf->iobuf + offset, buf, num);
520 if (mf_write (cf->block_mf, block, offset, num, buf))
522 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
528 int cf_close (CFile cf)
530 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
531 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
532 cf->head.next_bucket - cf->head.first_bucket);
533 flush_bucket (cf, -1);
536 logf (LOG_LOG, "cf_close %s, dirty", cf->rmf->name);
537 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
541 logf (LOG_LOG, "cf_close %s", cf->rmf->name);
542 mf_close (cf->hash_mf);
543 mf_close (cf->block_mf);