2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1997-02-12 20:37:17 adam
8 * Changed the messages logged. No real code changed.
10 * Revision 1.18 1996/10/29 13:56:15 adam
11 * Include of zebrautl.h instead of alexutil.h.
13 * Revision 1.17 1996/04/19 16:49:00 adam
16 * Revision 1.16 1996/04/19 16:23:47 adam
17 * Serious bug fix in shadow implementation; function new_bucket might
18 * set wrong bucket number on new bucket.
20 * Revision 1.15 1996/04/18 16:02:56 adam
21 * Changed logging a bit.
22 * Removed warning message when commiting flat shadow files.
24 * Revision 1.14 1996/04/12 07:01:55 adam
25 * Yet another bug fix (next_block was initialized to 0; now set to 1).
27 * Revision 1.13 1996/04/09 14:48:49 adam
28 * Bug fix: offset calculation when using flat files was completely broken.
30 * Revision 1.12 1996/04/09 06:47:28 adam
31 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
33 * Revision 1.11 1996/03/26 15:59:05 adam
34 * The directory of the shadow table file can be specified by the new
37 * Revision 1.10 1996/02/07 14:03:46 adam
38 * Work on flat indexed shadow files.
40 * Revision 1.9 1996/02/07 10:08:43 adam
41 * Work on flat shadow (not finished yet).
43 * Revision 1.8 1995/12/15 12:36:52 adam
44 * Moved hash file information to union.
45 * Renamed commit files.
47 * Revision 1.7 1995/12/15 10:35:07 adam
48 * Changed names of commit files.
50 * Revision 1.6 1995/12/11 09:03:53 adam
51 * New function: cf_unlink.
52 * New member of commit file head: state (0) deleted, (1) hash file.
54 * Revision 1.5 1995/12/08 16:21:14 adam
55 * Work on commit/update.
57 * Revision 1.4 1995/12/01 16:24:28 adam
58 * Commit files use separate meta file area.
60 * Revision 1.3 1995/12/01 11:37:22 adam
61 * Cached/commit files implemented as meta-files.
63 * Revision 1.2 1995/11/30 17:00:49 adam
64 * Several bug fixes. Commit system runs now.
66 * Revision 1.1 1995/11/30 08:33:11 adam
67 * Started work on commit facility.
79 static int write_head (CFile cf)
81 int left = cf->head.hash_size * sizeof(int);
83 const char *tab = (char*) cf->array;
87 while (left >= HASH_BSIZE)
89 mf_write (cf->hash_mf, bno++, 0, 0, tab);
94 mf_write (cf->hash_mf, bno, 0, left, tab);
98 static int read_head (CFile cf)
100 int left = cf->head.hash_size * sizeof(int);
102 char *tab = (char*) cf->array;
106 while (left >= HASH_BSIZE)
108 mf_read (cf->hash_mf, bno++, 0, 0, tab);
113 mf_read (cf->hash_mf, bno, 0, left, tab);
118 CFile cf_open (MFile mf, MFile_area area, const char *fname,
119 int block_size, int wflag, int *firstp)
123 CFile cf = xmalloc (sizeof(*cf));
127 logf (LOG_LOG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
128 sprintf (path, "%s-b", fname);
129 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
131 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
134 sprintf (path, "%s-i", fname);
135 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
137 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
141 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
146 cf->head.block_size = block_size;
147 cf->head.hash_size = 199;
148 hash_bytes = cf->head.hash_size * sizeof(int);
149 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
150 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
151 cf->head.next_block = 1;
153 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
154 cf->array = xmalloc (hash_bytes);
155 for (i = 0; i<cf->head.hash_size; i++)
163 assert (cf->head.block_size == block_size);
164 assert (cf->head.hash_size > 2);
165 hash_bytes = cf->head.hash_size * sizeof(int);
166 assert (cf->head.next_bucket > 0);
167 assert (cf->head.next_block > 0);
168 if (cf->head.state == 1)
169 cf->array = xmalloc (hash_bytes);
174 if (cf->head.state == 1)
176 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
177 for (i = 0; i<cf->head.hash_size; i++)
178 cf->parray[i] = NULL;
182 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
183 cf->bucket_in_memory = 0;
184 cf->max_bucket_in_memory = 100;
186 cf->iobuf = xmalloc (cf->head.block_size);
187 memset (cf->iobuf, 0, cf->head.block_size);
193 static int cf_hash (CFile cf, int no)
195 return (no>>3) % cf->head.hash_size;
198 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
201 p->lru_prev->lru_next = p->lru_next;
203 cf->bucket_lru_back = p->lru_next;
205 p->lru_next->lru_prev = p->lru_prev;
207 cf->bucket_lru_front = p->lru_prev;
209 *p->h_prev = p->h_next;
211 p->h_next->h_prev = p->h_prev;
213 --(cf->bucket_in_memory);
217 static void flush_bucket (CFile cf, int no_to_flush)
220 struct CFile_hash_bucket *p;
222 for (i = 0; i != no_to_flush; i++)
224 p = cf->bucket_lru_back;
229 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
232 release_bucket (cf, p);
236 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
238 struct CFile_hash_bucket *p, **pp;
240 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
241 flush_bucket (cf, 1);
242 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
243 ++(cf->bucket_in_memory);
244 p = xmalloc (sizeof(*p));
247 p->lru_prev = cf->bucket_lru_front;
248 if (cf->bucket_lru_front)
249 cf->bucket_lru_front->lru_next = p;
251 cf->bucket_lru_back = p;
252 cf->bucket_lru_front = p;
254 pp = cf->parray + hno;
258 (*pp)->h_prev = &p->h_next;
263 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
265 struct CFile_hash_bucket *p;
267 p = alloc_bucket (cf, block_no, hno);
268 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
270 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
273 assert (p->ph.this_bucket == block_no);
278 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
280 struct CFile_hash_bucket *p;
283 block_no = *block_nop = cf->head.next_bucket++;
284 p = alloc_bucket (cf, block_no, hno);
286 for (i = 0; i<HASH_BUCKET; i++)
291 p->ph.next_bucket = 0;
292 p->ph.this_bucket = block_no;
297 static int cf_lookup_flat (CFile cf, int no)
299 int hno = (no*sizeof(int))/HASH_BSIZE;
300 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
303 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
307 static int cf_lookup_hash (CFile cf, int no)
309 int hno = cf_hash (cf, no);
310 struct CFile_hash_bucket *hb;
313 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
315 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
316 if (hb->ph.no[i] == no)
319 return hb->ph.vno[i];
322 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
324 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
326 if (hb->ph.this_bucket == block_no)
332 /* extra check ... */
333 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
335 if (hb->ph.this_bucket == block_no)
337 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
340 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
341 if (hb->ph.no[i] == no)
343 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
349 hb = get_bucket (cf, block_no, hno);
350 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
351 if (hb->ph.no[i] == no)
352 return hb->ph.vno[i];
357 static void cf_write_flat (CFile cf, int no, int vno)
359 int hno = (no*sizeof(int))/HASH_BSIZE;
360 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
362 hno += cf->head.next_bucket;
363 if (hno >= cf->head.flat_bucket)
364 cf->head.flat_bucket = hno+1;
366 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
369 static void cf_moveto_flat (CFile cf)
371 struct CFile_hash_bucket *p;
374 logf (LOG_LOG, "cf: Moving to flat shadow: %s", cf->rmf->name);
375 logf (LOG_LOG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
376 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
377 cf->head.next_bucket - cf->head.first_bucket);
378 assert (cf->head.state == 1);
379 flush_bucket (cf, -1);
380 assert (cf->bucket_in_memory == 0);
381 p = xmalloc (sizeof(*p));
382 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
384 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
386 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
389 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
390 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
401 static int cf_lookup (CFile cf, int no)
403 if (cf->head.state > 1)
404 return cf_lookup_flat (cf, no);
405 return cf_lookup_hash (cf, no);
408 static int cf_new_flat (CFile cf, int no)
410 int vno = (cf->head.next_block)++;
412 cf_write_flat (cf, no, vno);
416 static int cf_new_hash (CFile cf, int no)
418 int hno = cf_hash (cf, no);
419 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
420 int *bucketpp = &cf->array[hno];
421 int i, vno = (cf->head.next_block)++;
423 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
424 if (!hb->ph.vno[HASH_BUCKET-1])
425 for (i = 0; i<HASH_BUCKET; i++)
437 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
438 if (hb->ph.this_bucket == *bucketpp)
440 bucketpp = &hb->ph.next_bucket;
448 /* extra check ... */
449 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
451 if (hb->ph.this_bucket == *bucketpp)
453 logf (LOG_FATAL, "Found hash bucket on other chain");
459 hb = get_bucket (cf, *bucketpp, hno);
461 for (i = 0; i<HASH_BUCKET; i++)
469 bucketpp = &hb->ph.next_bucket;
474 hb = new_bucket (cf, bucketpp, hno);
480 int cf_new (CFile cf, int no)
482 if (cf->head.state > 1)
483 return cf_new_flat (cf, no);
484 if (cf->no_miss*2 > cf->no_hits)
487 assert (cf->head.state > 1);
488 return cf_new_flat (cf, no);
490 return cf_new_hash (cf, no);
494 int cf_read (CFile cf, int no, int offset, int num, void *buf)
499 if (!(block = cf_lookup (cf, no)))
501 if (!mf_read (cf->block_mf, block, offset, num, buf))
503 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
509 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
514 if (!(block = cf_lookup (cf, no)))
516 block = cf_new (cf, no);
519 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
520 memcpy (cf->iobuf + offset, buf, num);
526 if (mf_write (cf->block_mf, block, offset, num, buf))
528 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
534 int cf_close (CFile cf)
536 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
537 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
538 cf->head.next_bucket - cf->head.first_bucket);
539 flush_bucket (cf, -1);
542 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
545 mf_close (cf->hash_mf);
546 mf_close (cf->block_mf);