2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1996-10-29 13:56:15 adam
8 * Include of zebrautl.h instead of alexutil.h.
10 * Revision 1.17 1996/04/19 16:49:00 adam
13 * Revision 1.16 1996/04/19 16:23:47 adam
14 * Serious bug fix in shadow implementation; function new_bucket might
15 * set wrong bucket number on new bucket.
17 * Revision 1.15 1996/04/18 16:02:56 adam
18 * Changed logging a bit.
19 * Removed warning message when commiting flat shadow files.
21 * Revision 1.14 1996/04/12 07:01:55 adam
22 * Yet another bug fix (next_block was initialized to 0; now set to 1).
24 * Revision 1.13 1996/04/09 14:48:49 adam
25 * Bug fix: offset calculation when using flat files was completely broken.
27 * Revision 1.12 1996/04/09 06:47:28 adam
28 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
30 * Revision 1.11 1996/03/26 15:59:05 adam
31 * The directory of the shadow table file can be specified by the new
34 * Revision 1.10 1996/02/07 14:03:46 adam
35 * Work on flat indexed shadow files.
37 * Revision 1.9 1996/02/07 10:08:43 adam
38 * Work on flat shadow (not finished yet).
40 * Revision 1.8 1995/12/15 12:36:52 adam
41 * Moved hash file information to union.
42 * Renamed commit files.
44 * Revision 1.7 1995/12/15 10:35:07 adam
45 * Changed names of commit files.
47 * Revision 1.6 1995/12/11 09:03:53 adam
48 * New function: cf_unlink.
49 * New member of commit file head: state (0) deleted, (1) hash file.
51 * Revision 1.5 1995/12/08 16:21:14 adam
52 * Work on commit/update.
54 * Revision 1.4 1995/12/01 16:24:28 adam
55 * Commit files use separate meta file area.
57 * Revision 1.3 1995/12/01 11:37:22 adam
58 * Cached/commit files implemented as meta-files.
60 * Revision 1.2 1995/11/30 17:00:49 adam
61 * Several bug fixes. Commit system runs now.
63 * Revision 1.1 1995/11/30 08:33:11 adam
64 * Started work on commit facility.
76 static int write_head (CFile cf)
78 int left = cf->head.hash_size * sizeof(int);
80 const char *tab = (char*) cf->array;
84 while (left >= HASH_BSIZE)
86 mf_write (cf->hash_mf, bno++, 0, 0, tab);
91 mf_write (cf->hash_mf, bno, 0, left, tab);
95 static int read_head (CFile cf)
97 int left = cf->head.hash_size * sizeof(int);
99 char *tab = (char*) cf->array;
103 while (left >= HASH_BSIZE)
105 mf_read (cf->hash_mf, bno++, 0, 0, tab);
110 mf_read (cf->hash_mf, bno, 0, left, tab);
115 CFile cf_open (MFile mf, MFile_area area, const char *fname,
116 int block_size, int wflag, int *firstp)
120 CFile cf = xmalloc (sizeof(*cf));
124 logf (LOG_LOG, "cf_open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
125 sprintf (path, "%s-b", fname);
126 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
128 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
131 sprintf (path, "%s-i", fname);
132 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
134 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
138 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
143 cf->head.block_size = block_size;
144 cf->head.hash_size = 199;
145 hash_bytes = cf->head.hash_size * sizeof(int);
146 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
147 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
148 cf->head.next_block = 1;
150 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
151 cf->array = xmalloc (hash_bytes);
152 for (i = 0; i<cf->head.hash_size; i++)
160 assert (cf->head.block_size == block_size);
161 assert (cf->head.hash_size > 2);
162 hash_bytes = cf->head.hash_size * sizeof(int);
163 assert (cf->head.next_bucket > 0);
164 assert (cf->head.next_block > 0);
165 if (cf->head.state == 1)
166 cf->array = xmalloc (hash_bytes);
171 if (cf->head.state == 1)
173 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
174 for (i = 0; i<cf->head.hash_size; i++)
175 cf->parray[i] = NULL;
179 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
180 cf->bucket_in_memory = 0;
181 cf->max_bucket_in_memory = 100;
183 cf->iobuf = xmalloc (cf->head.block_size);
184 memset (cf->iobuf, 0, cf->head.block_size);
190 static int cf_hash (CFile cf, int no)
192 return (no>>3) % cf->head.hash_size;
195 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
198 p->lru_prev->lru_next = p->lru_next;
200 cf->bucket_lru_back = p->lru_next;
202 p->lru_next->lru_prev = p->lru_prev;
204 cf->bucket_lru_front = p->lru_prev;
206 *p->h_prev = p->h_next;
208 p->h_next->h_prev = p->h_prev;
210 --(cf->bucket_in_memory);
214 static void flush_bucket (CFile cf, int no_to_flush)
217 struct CFile_hash_bucket *p;
219 for (i = 0; i != no_to_flush; i++)
221 p = cf->bucket_lru_back;
226 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
229 release_bucket (cf, p);
233 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
235 struct CFile_hash_bucket *p, **pp;
237 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
238 flush_bucket (cf, 1);
239 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
240 ++(cf->bucket_in_memory);
241 p = xmalloc (sizeof(*p));
244 p->lru_prev = cf->bucket_lru_front;
245 if (cf->bucket_lru_front)
246 cf->bucket_lru_front->lru_next = p;
248 cf->bucket_lru_back = p;
249 cf->bucket_lru_front = p;
251 pp = cf->parray + hno;
255 (*pp)->h_prev = &p->h_next;
260 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
262 struct CFile_hash_bucket *p;
264 p = alloc_bucket (cf, block_no, hno);
265 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
267 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
270 assert (p->ph.this_bucket == block_no);
275 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
277 struct CFile_hash_bucket *p;
280 block_no = *block_nop = cf->head.next_bucket++;
281 p = alloc_bucket (cf, block_no, hno);
283 for (i = 0; i<HASH_BUCKET; i++)
288 p->ph.next_bucket = 0;
289 p->ph.this_bucket = block_no;
294 static int cf_lookup_flat (CFile cf, int no)
296 int hno = (no*sizeof(int))/HASH_BSIZE;
297 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
300 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
304 static int cf_lookup_hash (CFile cf, int no)
306 int hno = cf_hash (cf, no);
307 struct CFile_hash_bucket *hb;
310 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
312 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
313 if (hb->ph.no[i] == no)
316 return hb->ph.vno[i];
319 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
321 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
323 if (hb->ph.this_bucket == block_no)
329 /* extra check ... */
330 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
332 if (hb->ph.this_bucket == block_no)
334 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
337 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
338 if (hb->ph.no[i] == no)
340 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
346 hb = get_bucket (cf, block_no, hno);
347 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
348 if (hb->ph.no[i] == no)
349 return hb->ph.vno[i];
354 static void cf_write_flat (CFile cf, int no, int vno)
356 int hno = (no*sizeof(int))/HASH_BSIZE;
357 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
359 hno += cf->head.next_bucket;
360 if (hno >= cf->head.flat_bucket)
361 cf->head.flat_bucket = hno+1;
363 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
366 static void cf_moveto_flat (CFile cf)
368 struct CFile_hash_bucket *p;
371 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
372 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
373 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
374 cf->head.next_bucket - cf->head.first_bucket);
375 assert (cf->head.state == 1);
376 flush_bucket (cf, -1);
377 assert (cf->bucket_in_memory == 0);
378 p = xmalloc (sizeof(*p));
379 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
381 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
383 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
386 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
387 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
398 static int cf_lookup (CFile cf, int no)
400 if (cf->head.state > 1)
401 return cf_lookup_flat (cf, no);
402 return cf_lookup_hash (cf, no);
405 static int cf_new_flat (CFile cf, int no)
407 int vno = (cf->head.next_block)++;
409 cf_write_flat (cf, no, vno);
413 static int cf_new_hash (CFile cf, int no)
415 int hno = cf_hash (cf, no);
416 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
417 int *bucketpp = &cf->array[hno];
418 int i, vno = (cf->head.next_block)++;
420 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
421 if (!hb->ph.vno[HASH_BUCKET-1])
422 for (i = 0; i<HASH_BUCKET; i++)
434 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
435 if (hb->ph.this_bucket == *bucketpp)
437 bucketpp = &hb->ph.next_bucket;
445 /* extra check ... */
446 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
448 if (hb->ph.this_bucket == *bucketpp)
450 logf (LOG_FATAL, "Found hash bucket on other chain");
456 hb = get_bucket (cf, *bucketpp, hno);
458 for (i = 0; i<HASH_BUCKET; i++)
466 bucketpp = &hb->ph.next_bucket;
471 hb = new_bucket (cf, bucketpp, hno);
477 int cf_new (CFile cf, int no)
479 if (cf->head.state > 1)
480 return cf_new_flat (cf, no);
481 if (cf->no_miss*2 > cf->no_hits)
484 assert (cf->head.state > 1);
485 return cf_new_flat (cf, no);
487 return cf_new_hash (cf, no);
491 int cf_read (CFile cf, int no, int offset, int num, void *buf)
496 if (!(block = cf_lookup (cf, no)))
498 if (!mf_read (cf->block_mf, block, offset, num, buf))
500 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
506 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
511 if (!(block = cf_lookup (cf, no)))
513 block = cf_new (cf, no);
516 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
517 memcpy (cf->iobuf + offset, buf, num);
523 if (mf_write (cf->block_mf, block, offset, num, buf))
525 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
531 int cf_close (CFile cf)
533 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
534 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
535 cf->head.next_bucket - cf->head.first_bucket);
536 flush_bucket (cf, -1);
539 logf (LOG_LOG, "cf_close %s, dirty", cf->rmf->name);
540 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
544 logf (LOG_LOG, "cf_close %s", cf->rmf->name);
545 mf_close (cf->hash_mf);
546 mf_close (cf->block_mf);