2 * Copyright (C) 1995-1998, Index Data ApS
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1998-10-15 13:09:29 adam
10 * Revision 1.22 1998/10/13 20:07:22 adam
11 * Changed some log messages.
13 * Revision 1.21 1998/08/24 17:29:52 adam
16 * Revision 1.20 1998/08/07 15:07:13 adam
17 * Fixed but in cf_commit_flat.
19 * Revision 1.19 1997/02/12 20:37:17 adam
20 * Changed the messages logged. No real code changed.
22 * Revision 1.18 1996/10/29 13:56:15 adam
23 * Include of zebrautl.h instead of alexutil.h.
25 * Revision 1.17 1996/04/19 16:49:00 adam
28 * Revision 1.16 1996/04/19 16:23:47 adam
29 * Serious bug fix in shadow implementation; function new_bucket might
30 * set wrong bucket number on new bucket.
32 * Revision 1.15 1996/04/18 16:02:56 adam
33 * Changed logging a bit.
34 * Removed warning message when commiting flat shadow files.
36 * Revision 1.14 1996/04/12 07:01:55 adam
37 * Yet another bug fix (next_block was initialized to 0; now set to 1).
39 * Revision 1.13 1996/04/09 14:48:49 adam
40 * Bug fix: offset calculation when using flat files was completely broken.
42 * Revision 1.12 1996/04/09 06:47:28 adam
43 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
45 * Revision 1.11 1996/03/26 15:59:05 adam
46 * The directory of the shadow table file can be specified by the new
49 * Revision 1.10 1996/02/07 14:03:46 adam
50 * Work on flat indexed shadow files.
52 * Revision 1.9 1996/02/07 10:08:43 adam
53 * Work on flat shadow (not finished yet).
55 * Revision 1.8 1995/12/15 12:36:52 adam
56 * Moved hash file information to union.
57 * Renamed commit files.
59 * Revision 1.7 1995/12/15 10:35:07 adam
60 * Changed names of commit files.
62 * Revision 1.6 1995/12/11 09:03:53 adam
63 * New function: cf_unlink.
64 * New member of commit file head: state (0) deleted, (1) hash file.
66 * Revision 1.5 1995/12/08 16:21:14 adam
67 * Work on commit/update.
69 * Revision 1.4 1995/12/01 16:24:28 adam
70 * Commit files use separate meta file area.
72 * Revision 1.3 1995/12/01 11:37:22 adam
73 * Cached/commit files implemented as meta-files.
75 * Revision 1.2 1995/11/30 17:00:49 adam
76 * Several bug fixes. Commit system runs now.
78 * Revision 1.1 1995/11/30 08:33:11 adam
79 * Started work on commit facility.
91 static int write_head (CFile cf)
93 int left = cf->head.hash_size * sizeof(int);
95 const char *tab = (char*) cf->array;
99 while (left >= HASH_BSIZE)
101 mf_write (cf->hash_mf, bno++, 0, 0, tab);
106 mf_write (cf->hash_mf, bno, 0, left, tab);
110 static int read_head (CFile cf)
112 int left = cf->head.hash_size * sizeof(int);
114 char *tab = (char*) cf->array;
118 while (left >= HASH_BSIZE)
120 mf_read (cf->hash_mf, bno++, 0, 0, tab);
125 mf_read (cf->hash_mf, bno, 0, left, tab);
130 CFile cf_open (MFile mf, MFile_area area, const char *fname,
131 int block_size, int wflag, int *firstp)
135 CFile cf = (CFile) xmalloc (sizeof(*cf));
139 logf (LOG_DEBUG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
140 sprintf (path, "%s-b", fname);
141 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
143 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
146 sprintf (path, "%s-i", fname);
147 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
149 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
153 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
158 cf->head.block_size = block_size;
159 cf->head.hash_size = 199;
160 hash_bytes = cf->head.hash_size * sizeof(int);
161 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
162 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
163 cf->head.next_block = 1;
165 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
166 cf->array = xmalloc (hash_bytes);
167 for (i = 0; i<cf->head.hash_size; i++)
175 assert (cf->head.block_size == block_size);
176 assert (cf->head.hash_size > 2);
177 hash_bytes = cf->head.hash_size * sizeof(int);
178 assert (cf->head.next_bucket > 0);
179 assert (cf->head.next_block > 0);
180 if (cf->head.state == 1)
181 cf->array = xmalloc (hash_bytes);
186 if (cf->head.state == 1)
188 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
189 for (i = 0; i<cf->head.hash_size; i++)
190 cf->parray[i] = NULL;
194 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
195 cf->bucket_in_memory = 0;
196 cf->max_bucket_in_memory = 100;
198 cf->iobuf = xmalloc (cf->head.block_size);
199 memset (cf->iobuf, 0, cf->head.block_size);
205 static int cf_hash (CFile cf, int no)
207 return (no>>3) % cf->head.hash_size;
210 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
213 p->lru_prev->lru_next = p->lru_next;
215 cf->bucket_lru_back = p->lru_next;
217 p->lru_next->lru_prev = p->lru_prev;
219 cf->bucket_lru_front = p->lru_prev;
221 *p->h_prev = p->h_next;
223 p->h_next->h_prev = p->h_prev;
225 --(cf->bucket_in_memory);
229 static void flush_bucket (CFile cf, int no_to_flush)
232 struct CFile_hash_bucket *p;
234 for (i = 0; i != no_to_flush; i++)
236 p = cf->bucket_lru_back;
241 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
244 release_bucket (cf, p);
248 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
250 struct CFile_hash_bucket *p, **pp;
252 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
253 flush_bucket (cf, 1);
254 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
255 ++(cf->bucket_in_memory);
256 p = xmalloc (sizeof(*p));
259 p->lru_prev = cf->bucket_lru_front;
260 if (cf->bucket_lru_front)
261 cf->bucket_lru_front->lru_next = p;
263 cf->bucket_lru_back = p;
264 cf->bucket_lru_front = p;
266 pp = cf->parray + hno;
270 (*pp)->h_prev = &p->h_next;
275 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
277 struct CFile_hash_bucket *p;
279 p = alloc_bucket (cf, block_no, hno);
280 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
282 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
285 assert (p->ph.this_bucket == block_no);
290 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
292 struct CFile_hash_bucket *p;
295 block_no = *block_nop = cf->head.next_bucket++;
296 p = alloc_bucket (cf, block_no, hno);
298 for (i = 0; i<HASH_BUCKET; i++)
303 p->ph.next_bucket = 0;
304 p->ph.this_bucket = block_no;
309 static int cf_lookup_flat (CFile cf, int no)
311 int hno = (no*sizeof(int))/HASH_BSIZE;
312 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
315 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
319 static int cf_lookup_hash (CFile cf, int no)
321 int hno = cf_hash (cf, no);
322 struct CFile_hash_bucket *hb;
325 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
327 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
328 if (hb->ph.no[i] == no)
331 return hb->ph.vno[i];
334 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
336 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
338 if (hb->ph.this_bucket == block_no)
344 /* extra check ... */
345 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
347 if (hb->ph.this_bucket == block_no)
349 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
352 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
353 if (hb->ph.no[i] == no)
355 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
361 hb = get_bucket (cf, block_no, hno);
362 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
363 if (hb->ph.no[i] == no)
364 return hb->ph.vno[i];
369 static void cf_write_flat (CFile cf, int no, int vno)
371 int hno = (no*sizeof(int))/HASH_BSIZE;
372 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
374 hno += cf->head.next_bucket;
375 if (hno >= cf->head.flat_bucket)
376 cf->head.flat_bucket = hno+1;
378 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
381 static void cf_moveto_flat (CFile cf)
383 struct CFile_hash_bucket *p;
386 logf (LOG_DEBUG, "cf: Moving to flat shadow: %s", cf->rmf->name);
387 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
388 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
389 cf->head.next_bucket - cf->head.first_bucket);
390 assert (cf->head.state == 1);
391 flush_bucket (cf, -1);
392 assert (cf->bucket_in_memory == 0);
393 p = xmalloc (sizeof(*p));
394 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
396 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
398 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
401 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
402 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
413 static int cf_lookup (CFile cf, int no)
415 if (cf->head.state > 1)
416 return cf_lookup_flat (cf, no);
417 return cf_lookup_hash (cf, no);
420 static int cf_new_flat (CFile cf, int no)
422 int vno = (cf->head.next_block)++;
424 cf_write_flat (cf, no, vno);
428 static int cf_new_hash (CFile cf, int no)
430 int hno = cf_hash (cf, no);
431 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
432 int *bucketpp = &cf->array[hno];
433 int i, vno = (cf->head.next_block)++;
435 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
436 if (!hb->ph.vno[HASH_BUCKET-1])
437 for (i = 0; i<HASH_BUCKET; i++)
449 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
450 if (hb->ph.this_bucket == *bucketpp)
452 bucketpp = &hb->ph.next_bucket;
460 /* extra check ... */
461 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
463 if (hb->ph.this_bucket == *bucketpp)
465 logf (LOG_FATAL, "Found hash bucket on other chain");
471 hb = get_bucket (cf, *bucketpp, hno);
473 for (i = 0; i<HASH_BUCKET; i++)
481 bucketpp = &hb->ph.next_bucket;
486 hb = new_bucket (cf, bucketpp, hno);
492 int cf_new (CFile cf, int no)
494 if (cf->head.state > 1)
495 return cf_new_flat (cf, no);
496 if (cf->no_miss*2 > cf->no_hits)
499 assert (cf->head.state > 1);
500 return cf_new_flat (cf, no);
502 return cf_new_hash (cf, no);
506 int cf_read (CFile cf, int no, int offset, int num, void *buf)
511 if (!(block = cf_lookup (cf, no)))
513 if (!mf_read (cf->block_mf, block, offset, num, buf))
515 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
521 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
526 if (!(block = cf_lookup (cf, no)))
528 block = cf_new (cf, no);
531 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
532 memcpy (cf->iobuf + offset, buf, num);
538 if (mf_write (cf->block_mf, block, offset, num, buf))
540 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
546 int cf_close (CFile cf)
548 logf (LOG_DEBUG, "cf: close hits=%d miss=%d bucket_in_memory=%d total=%d",
549 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
550 cf->head.next_bucket - cf->head.first_bucket);
551 flush_bucket (cf, -1);
554 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
557 mf_close (cf->hash_mf);
558 mf_close (cf->block_mf);