2 * Copyright (C) 1995-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1998-08-07 15:07:13 adam
8 * Fixed but in cf_commit_flat.
10 * Revision 1.19 1997/02/12 20:37:17 adam
11 * Changed the messages logged. No real code changed.
13 * Revision 1.18 1996/10/29 13:56:15 adam
14 * Include of zebrautl.h instead of alexutil.h.
16 * Revision 1.17 1996/04/19 16:49:00 adam
19 * Revision 1.16 1996/04/19 16:23:47 adam
20 * Serious bug fix in shadow implementation; function new_bucket might
21 * set wrong bucket number on new bucket.
23 * Revision 1.15 1996/04/18 16:02:56 adam
24 * Changed logging a bit.
25 * Removed warning message when commiting flat shadow files.
27 * Revision 1.14 1996/04/12 07:01:55 adam
28 * Yet another bug fix (next_block was initialized to 0; now set to 1).
30 * Revision 1.13 1996/04/09 14:48:49 adam
31 * Bug fix: offset calculation when using flat files was completely broken.
33 * Revision 1.12 1996/04/09 06:47:28 adam
34 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
36 * Revision 1.11 1996/03/26 15:59:05 adam
37 * The directory of the shadow table file can be specified by the new
40 * Revision 1.10 1996/02/07 14:03:46 adam
41 * Work on flat indexed shadow files.
43 * Revision 1.9 1996/02/07 10:08:43 adam
44 * Work on flat shadow (not finished yet).
46 * Revision 1.8 1995/12/15 12:36:52 adam
47 * Moved hash file information to union.
48 * Renamed commit files.
50 * Revision 1.7 1995/12/15 10:35:07 adam
51 * Changed names of commit files.
53 * Revision 1.6 1995/12/11 09:03:53 adam
54 * New function: cf_unlink.
55 * New member of commit file head: state (0) deleted, (1) hash file.
57 * Revision 1.5 1995/12/08 16:21:14 adam
58 * Work on commit/update.
60 * Revision 1.4 1995/12/01 16:24:28 adam
61 * Commit files use separate meta file area.
63 * Revision 1.3 1995/12/01 11:37:22 adam
64 * Cached/commit files implemented as meta-files.
66 * Revision 1.2 1995/11/30 17:00:49 adam
67 * Several bug fixes. Commit system runs now.
69 * Revision 1.1 1995/11/30 08:33:11 adam
70 * Started work on commit facility.
82 static int write_head (CFile cf)
84 int left = cf->head.hash_size * sizeof(int);
86 const char *tab = (char*) cf->array;
90 while (left >= HASH_BSIZE)
92 mf_write (cf->hash_mf, bno++, 0, 0, tab);
97 mf_write (cf->hash_mf, bno, 0, left, tab);
101 static int read_head (CFile cf)
103 int left = cf->head.hash_size * sizeof(int);
105 char *tab = (char*) cf->array;
109 while (left >= HASH_BSIZE)
111 mf_read (cf->hash_mf, bno++, 0, 0, tab);
116 mf_read (cf->hash_mf, bno, 0, left, tab);
121 CFile cf_open (MFile mf, MFile_area area, const char *fname,
122 int block_size, int wflag, int *firstp)
126 CFile cf = xmalloc (sizeof(*cf));
130 logf (LOG_LOG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
131 sprintf (path, "%s-b", fname);
132 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
134 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
137 sprintf (path, "%s-i", fname);
138 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
140 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
144 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
149 cf->head.block_size = block_size;
150 cf->head.hash_size = 199;
151 hash_bytes = cf->head.hash_size * sizeof(int);
152 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
153 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
154 cf->head.next_block = 1;
156 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
157 cf->array = xmalloc (hash_bytes);
158 for (i = 0; i<cf->head.hash_size; i++)
166 assert (cf->head.block_size == block_size);
167 assert (cf->head.hash_size > 2);
168 hash_bytes = cf->head.hash_size * sizeof(int);
169 assert (cf->head.next_bucket > 0);
170 assert (cf->head.next_block > 0);
171 if (cf->head.state == 1)
172 cf->array = xmalloc (hash_bytes);
177 if (cf->head.state == 1)
179 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
180 for (i = 0; i<cf->head.hash_size; i++)
181 cf->parray[i] = NULL;
185 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
186 cf->bucket_in_memory = 0;
187 cf->max_bucket_in_memory = 100;
189 cf->iobuf = xmalloc (cf->head.block_size);
190 memset (cf->iobuf, 0, cf->head.block_size);
196 static int cf_hash (CFile cf, int no)
198 return (no>>3) % cf->head.hash_size;
201 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
204 p->lru_prev->lru_next = p->lru_next;
206 cf->bucket_lru_back = p->lru_next;
208 p->lru_next->lru_prev = p->lru_prev;
210 cf->bucket_lru_front = p->lru_prev;
212 *p->h_prev = p->h_next;
214 p->h_next->h_prev = p->h_prev;
216 --(cf->bucket_in_memory);
220 static void flush_bucket (CFile cf, int no_to_flush)
223 struct CFile_hash_bucket *p;
225 for (i = 0; i != no_to_flush; i++)
227 p = cf->bucket_lru_back;
232 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
235 release_bucket (cf, p);
239 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
241 struct CFile_hash_bucket *p, **pp;
243 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
244 flush_bucket (cf, 1);
245 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
246 ++(cf->bucket_in_memory);
247 p = xmalloc (sizeof(*p));
250 p->lru_prev = cf->bucket_lru_front;
251 if (cf->bucket_lru_front)
252 cf->bucket_lru_front->lru_next = p;
254 cf->bucket_lru_back = p;
255 cf->bucket_lru_front = p;
257 pp = cf->parray + hno;
261 (*pp)->h_prev = &p->h_next;
266 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
268 struct CFile_hash_bucket *p;
270 p = alloc_bucket (cf, block_no, hno);
271 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
273 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
276 assert (p->ph.this_bucket == block_no);
281 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
283 struct CFile_hash_bucket *p;
286 block_no = *block_nop = cf->head.next_bucket++;
287 p = alloc_bucket (cf, block_no, hno);
289 for (i = 0; i<HASH_BUCKET; i++)
294 p->ph.next_bucket = 0;
295 p->ph.this_bucket = block_no;
300 static int cf_lookup_flat (CFile cf, int no)
302 int hno = (no*sizeof(int))/HASH_BSIZE;
303 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
306 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
310 static int cf_lookup_hash (CFile cf, int no)
312 int hno = cf_hash (cf, no);
313 struct CFile_hash_bucket *hb;
316 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
318 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
319 if (hb->ph.no[i] == no)
322 return hb->ph.vno[i];
325 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
327 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
329 if (hb->ph.this_bucket == block_no)
335 /* extra check ... */
336 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
338 if (hb->ph.this_bucket == block_no)
340 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
343 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
344 if (hb->ph.no[i] == no)
346 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
352 hb = get_bucket (cf, block_no, hno);
353 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
354 if (hb->ph.no[i] == no)
355 return hb->ph.vno[i];
360 static void cf_write_flat (CFile cf, int no, int vno)
362 int hno = (no*sizeof(int))/HASH_BSIZE;
363 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
365 hno += cf->head.next_bucket;
366 if (hno >= cf->head.flat_bucket)
367 cf->head.flat_bucket = hno+1;
369 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
372 static void cf_moveto_flat (CFile cf)
374 struct CFile_hash_bucket *p;
377 logf (LOG_LOG, "cf: Moving to flat shadow: %s", cf->rmf->name);
378 logf (LOG_LOG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
379 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
380 cf->head.next_bucket - cf->head.first_bucket);
381 assert (cf->head.state == 1);
382 flush_bucket (cf, -1);
383 assert (cf->bucket_in_memory == 0);
384 p = xmalloc (sizeof(*p));
385 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
387 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
389 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
392 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
393 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
404 static int cf_lookup (CFile cf, int no)
406 if (cf->head.state > 1)
407 return cf_lookup_flat (cf, no);
408 return cf_lookup_hash (cf, no);
411 static int cf_new_flat (CFile cf, int no)
413 int vno = (cf->head.next_block)++;
415 cf_write_flat (cf, no, vno);
419 static int cf_new_hash (CFile cf, int no)
421 int hno = cf_hash (cf, no);
422 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
423 int *bucketpp = &cf->array[hno];
424 int i, vno = (cf->head.next_block)++;
426 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
427 if (!hb->ph.vno[HASH_BUCKET-1])
428 for (i = 0; i<HASH_BUCKET; i++)
440 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
441 if (hb->ph.this_bucket == *bucketpp)
443 bucketpp = &hb->ph.next_bucket;
451 /* extra check ... */
452 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
454 if (hb->ph.this_bucket == *bucketpp)
456 logf (LOG_FATAL, "Found hash bucket on other chain");
462 hb = get_bucket (cf, *bucketpp, hno);
464 for (i = 0; i<HASH_BUCKET; i++)
472 bucketpp = &hb->ph.next_bucket;
477 hb = new_bucket (cf, bucketpp, hno);
483 int cf_new (CFile cf, int no)
485 if (cf->head.state > 1)
486 return cf_new_flat (cf, no);
487 if (cf->no_miss*2 > cf->no_hits)
490 assert (cf->head.state > 1);
491 return cf_new_flat (cf, no);
493 return cf_new_hash (cf, no);
497 int cf_read (CFile cf, int no, int offset, int num, void *buf)
502 if (!(block = cf_lookup (cf, no)))
504 if (!mf_read (cf->block_mf, block, offset, num, buf))
506 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
512 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
517 if (!(block = cf_lookup (cf, no)))
519 block = cf_new (cf, no);
522 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
523 memcpy (cf->iobuf + offset, buf, num);
529 if (mf_write (cf->block_mf, block, offset, num, buf))
531 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
537 int cf_close (CFile cf)
539 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
540 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
541 cf->head.next_bucket - cf->head.first_bucket);
542 flush_bucket (cf, -1);
545 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
548 mf_close (cf->hash_mf);
549 mf_close (cf->block_mf);