2 * Copyright (C) 1995-1998, Index Data ApS
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1998-08-24 17:29:52 adam
10 * Revision 1.20 1998/08/07 15:07:13 adam
11 * Fixed but in cf_commit_flat.
13 * Revision 1.19 1997/02/12 20:37:17 adam
14 * Changed the messages logged. No real code changed.
16 * Revision 1.18 1996/10/29 13:56:15 adam
17 * Include of zebrautl.h instead of alexutil.h.
19 * Revision 1.17 1996/04/19 16:49:00 adam
22 * Revision 1.16 1996/04/19 16:23:47 adam
23 * Serious bug fix in shadow implementation; function new_bucket might
24 * set wrong bucket number on new bucket.
26 * Revision 1.15 1996/04/18 16:02:56 adam
27 * Changed logging a bit.
28 * Removed warning message when commiting flat shadow files.
30 * Revision 1.14 1996/04/12 07:01:55 adam
31 * Yet another bug fix (next_block was initialized to 0; now set to 1).
33 * Revision 1.13 1996/04/09 14:48:49 adam
34 * Bug fix: offset calculation when using flat files was completely broken.
36 * Revision 1.12 1996/04/09 06:47:28 adam
37 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
39 * Revision 1.11 1996/03/26 15:59:05 adam
40 * The directory of the shadow table file can be specified by the new
43 * Revision 1.10 1996/02/07 14:03:46 adam
44 * Work on flat indexed shadow files.
46 * Revision 1.9 1996/02/07 10:08:43 adam
47 * Work on flat shadow (not finished yet).
49 * Revision 1.8 1995/12/15 12:36:52 adam
50 * Moved hash file information to union.
51 * Renamed commit files.
53 * Revision 1.7 1995/12/15 10:35:07 adam
54 * Changed names of commit files.
56 * Revision 1.6 1995/12/11 09:03:53 adam
57 * New function: cf_unlink.
58 * New member of commit file head: state (0) deleted, (1) hash file.
60 * Revision 1.5 1995/12/08 16:21:14 adam
61 * Work on commit/update.
63 * Revision 1.4 1995/12/01 16:24:28 adam
64 * Commit files use separate meta file area.
66 * Revision 1.3 1995/12/01 11:37:22 adam
67 * Cached/commit files implemented as meta-files.
69 * Revision 1.2 1995/11/30 17:00:49 adam
70 * Several bug fixes. Commit system runs now.
72 * Revision 1.1 1995/11/30 08:33:11 adam
73 * Started work on commit facility.
85 static int write_head (CFile cf)
87 int left = cf->head.hash_size * sizeof(int);
89 const char *tab = (char*) cf->array;
93 while (left >= HASH_BSIZE)
95 mf_write (cf->hash_mf, bno++, 0, 0, tab);
100 mf_write (cf->hash_mf, bno, 0, left, tab);
104 static int read_head (CFile cf)
106 int left = cf->head.hash_size * sizeof(int);
108 char *tab = (char*) cf->array;
112 while (left >= HASH_BSIZE)
114 mf_read (cf->hash_mf, bno++, 0, 0, tab);
119 mf_read (cf->hash_mf, bno, 0, left, tab);
124 CFile cf_open (MFile mf, MFile_area area, const char *fname,
125 int block_size, int wflag, int *firstp)
129 CFile cf = xmalloc (sizeof(*cf));
133 logf (LOG_LOG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
134 sprintf (path, "%s-b", fname);
135 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
137 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
140 sprintf (path, "%s-i", fname);
141 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
143 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
147 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
152 cf->head.block_size = block_size;
153 cf->head.hash_size = 199;
154 hash_bytes = cf->head.hash_size * sizeof(int);
155 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
156 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
157 cf->head.next_block = 1;
159 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
160 cf->array = xmalloc (hash_bytes);
161 for (i = 0; i<cf->head.hash_size; i++)
169 assert (cf->head.block_size == block_size);
170 assert (cf->head.hash_size > 2);
171 hash_bytes = cf->head.hash_size * sizeof(int);
172 assert (cf->head.next_bucket > 0);
173 assert (cf->head.next_block > 0);
174 if (cf->head.state == 1)
175 cf->array = xmalloc (hash_bytes);
180 if (cf->head.state == 1)
182 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
183 for (i = 0; i<cf->head.hash_size; i++)
184 cf->parray[i] = NULL;
188 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
189 cf->bucket_in_memory = 0;
190 cf->max_bucket_in_memory = 100;
192 cf->iobuf = xmalloc (cf->head.block_size);
193 memset (cf->iobuf, 0, cf->head.block_size);
199 static int cf_hash (CFile cf, int no)
201 return (no>>3) % cf->head.hash_size;
204 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
207 p->lru_prev->lru_next = p->lru_next;
209 cf->bucket_lru_back = p->lru_next;
211 p->lru_next->lru_prev = p->lru_prev;
213 cf->bucket_lru_front = p->lru_prev;
215 *p->h_prev = p->h_next;
217 p->h_next->h_prev = p->h_prev;
219 --(cf->bucket_in_memory);
223 static void flush_bucket (CFile cf, int no_to_flush)
226 struct CFile_hash_bucket *p;
228 for (i = 0; i != no_to_flush; i++)
230 p = cf->bucket_lru_back;
235 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
238 release_bucket (cf, p);
242 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
244 struct CFile_hash_bucket *p, **pp;
246 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
247 flush_bucket (cf, 1);
248 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
249 ++(cf->bucket_in_memory);
250 p = xmalloc (sizeof(*p));
253 p->lru_prev = cf->bucket_lru_front;
254 if (cf->bucket_lru_front)
255 cf->bucket_lru_front->lru_next = p;
257 cf->bucket_lru_back = p;
258 cf->bucket_lru_front = p;
260 pp = cf->parray + hno;
264 (*pp)->h_prev = &p->h_next;
269 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
271 struct CFile_hash_bucket *p;
273 p = alloc_bucket (cf, block_no, hno);
274 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
276 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
279 assert (p->ph.this_bucket == block_no);
284 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
286 struct CFile_hash_bucket *p;
289 block_no = *block_nop = cf->head.next_bucket++;
290 p = alloc_bucket (cf, block_no, hno);
292 for (i = 0; i<HASH_BUCKET; i++)
297 p->ph.next_bucket = 0;
298 p->ph.this_bucket = block_no;
303 static int cf_lookup_flat (CFile cf, int no)
305 int hno = (no*sizeof(int))/HASH_BSIZE;
306 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
309 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
313 static int cf_lookup_hash (CFile cf, int no)
315 int hno = cf_hash (cf, no);
316 struct CFile_hash_bucket *hb;
319 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
321 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
322 if (hb->ph.no[i] == no)
325 return hb->ph.vno[i];
328 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
330 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
332 if (hb->ph.this_bucket == block_no)
338 /* extra check ... */
339 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
341 if (hb->ph.this_bucket == block_no)
343 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
346 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
347 if (hb->ph.no[i] == no)
349 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
355 hb = get_bucket (cf, block_no, hno);
356 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
357 if (hb->ph.no[i] == no)
358 return hb->ph.vno[i];
363 static void cf_write_flat (CFile cf, int no, int vno)
365 int hno = (no*sizeof(int))/HASH_BSIZE;
366 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
368 hno += cf->head.next_bucket;
369 if (hno >= cf->head.flat_bucket)
370 cf->head.flat_bucket = hno+1;
372 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
375 static void cf_moveto_flat (CFile cf)
377 struct CFile_hash_bucket *p;
380 logf (LOG_LOG, "cf: Moving to flat shadow: %s", cf->rmf->name);
381 logf (LOG_LOG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
382 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
383 cf->head.next_bucket - cf->head.first_bucket);
384 assert (cf->head.state == 1);
385 flush_bucket (cf, -1);
386 assert (cf->bucket_in_memory == 0);
387 p = xmalloc (sizeof(*p));
388 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
390 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
392 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
395 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
396 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
407 static int cf_lookup (CFile cf, int no)
409 if (cf->head.state > 1)
410 return cf_lookup_flat (cf, no);
411 return cf_lookup_hash (cf, no);
414 static int cf_new_flat (CFile cf, int no)
416 int vno = (cf->head.next_block)++;
418 cf_write_flat (cf, no, vno);
422 static int cf_new_hash (CFile cf, int no)
424 int hno = cf_hash (cf, no);
425 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
426 int *bucketpp = &cf->array[hno];
427 int i, vno = (cf->head.next_block)++;
429 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
430 if (!hb->ph.vno[HASH_BUCKET-1])
431 for (i = 0; i<HASH_BUCKET; i++)
443 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
444 if (hb->ph.this_bucket == *bucketpp)
446 bucketpp = &hb->ph.next_bucket;
454 /* extra check ... */
455 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
457 if (hb->ph.this_bucket == *bucketpp)
459 logf (LOG_FATAL, "Found hash bucket on other chain");
465 hb = get_bucket (cf, *bucketpp, hno);
467 for (i = 0; i<HASH_BUCKET; i++)
475 bucketpp = &hb->ph.next_bucket;
480 hb = new_bucket (cf, bucketpp, hno);
486 int cf_new (CFile cf, int no)
488 if (cf->head.state > 1)
489 return cf_new_flat (cf, no);
490 if (cf->no_miss*2 > cf->no_hits)
493 assert (cf->head.state > 1);
494 return cf_new_flat (cf, no);
496 return cf_new_hash (cf, no);
500 int cf_read (CFile cf, int no, int offset, int num, void *buf)
505 if (!(block = cf_lookup (cf, no)))
507 if (!mf_read (cf->block_mf, block, offset, num, buf))
509 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
515 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
520 if (!(block = cf_lookup (cf, no)))
522 block = cf_new (cf, no);
525 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
526 memcpy (cf->iobuf + offset, buf, num);
532 if (mf_write (cf->block_mf, block, offset, num, buf))
534 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
540 int cf_close (CFile cf)
542 logf (LOG_LOG, "cf: close hits=%d miss=%d bucket_in_memory=%d total=%d",
543 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
544 cf->head.next_bucket - cf->head.first_bucket);
545 flush_bucket (cf, -1);
548 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
551 mf_close (cf->hash_mf);
552 mf_close (cf->block_mf);