2 * Copyright (C) 1995-1998, Index Data ApS
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1998-10-13 20:07:22 adam
8 * Changed some log messages.
10 * Revision 1.21 1998/08/24 17:29:52 adam
13 * Revision 1.20 1998/08/07 15:07:13 adam
14 * Fixed but in cf_commit_flat.
16 * Revision 1.19 1997/02/12 20:37:17 adam
17 * Changed the messages logged. No real code changed.
19 * Revision 1.18 1996/10/29 13:56:15 adam
20 * Include of zebrautl.h instead of alexutil.h.
22 * Revision 1.17 1996/04/19 16:49:00 adam
25 * Revision 1.16 1996/04/19 16:23:47 adam
26 * Serious bug fix in shadow implementation; function new_bucket might
27 * set wrong bucket number on new bucket.
29 * Revision 1.15 1996/04/18 16:02:56 adam
30 * Changed logging a bit.
31 * Removed warning message when commiting flat shadow files.
33 * Revision 1.14 1996/04/12 07:01:55 adam
34 * Yet another bug fix (next_block was initialized to 0; now set to 1).
36 * Revision 1.13 1996/04/09 14:48:49 adam
37 * Bug fix: offset calculation when using flat files was completely broken.
39 * Revision 1.12 1996/04/09 06:47:28 adam
40 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
42 * Revision 1.11 1996/03/26 15:59:05 adam
43 * The directory of the shadow table file can be specified by the new
46 * Revision 1.10 1996/02/07 14:03:46 adam
47 * Work on flat indexed shadow files.
49 * Revision 1.9 1996/02/07 10:08:43 adam
50 * Work on flat shadow (not finished yet).
52 * Revision 1.8 1995/12/15 12:36:52 adam
53 * Moved hash file information to union.
54 * Renamed commit files.
56 * Revision 1.7 1995/12/15 10:35:07 adam
57 * Changed names of commit files.
59 * Revision 1.6 1995/12/11 09:03:53 adam
60 * New function: cf_unlink.
61 * New member of commit file head: state (0) deleted, (1) hash file.
63 * Revision 1.5 1995/12/08 16:21:14 adam
64 * Work on commit/update.
66 * Revision 1.4 1995/12/01 16:24:28 adam
67 * Commit files use separate meta file area.
69 * Revision 1.3 1995/12/01 11:37:22 adam
70 * Cached/commit files implemented as meta-files.
72 * Revision 1.2 1995/11/30 17:00:49 adam
73 * Several bug fixes. Commit system runs now.
75 * Revision 1.1 1995/11/30 08:33:11 adam
76 * Started work on commit facility.
88 static int write_head (CFile cf)
90 int left = cf->head.hash_size * sizeof(int);
92 const char *tab = (char*) cf->array;
96 while (left >= HASH_BSIZE)
98 mf_write (cf->hash_mf, bno++, 0, 0, tab);
103 mf_write (cf->hash_mf, bno, 0, left, tab);
107 static int read_head (CFile cf)
109 int left = cf->head.hash_size * sizeof(int);
111 char *tab = (char*) cf->array;
115 while (left >= HASH_BSIZE)
117 mf_read (cf->hash_mf, bno++, 0, 0, tab);
122 mf_read (cf->hash_mf, bno, 0, left, tab);
127 CFile cf_open (MFile mf, MFile_area area, const char *fname,
128 int block_size, int wflag, int *firstp)
132 CFile cf = xmalloc (sizeof(*cf));
136 logf (LOG_DEBUG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
137 sprintf (path, "%s-b", fname);
138 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
140 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
143 sprintf (path, "%s-i", fname);
144 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
146 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
150 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
155 cf->head.block_size = block_size;
156 cf->head.hash_size = 199;
157 hash_bytes = cf->head.hash_size * sizeof(int);
158 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
159 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
160 cf->head.next_block = 1;
162 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
163 cf->array = xmalloc (hash_bytes);
164 for (i = 0; i<cf->head.hash_size; i++)
172 assert (cf->head.block_size == block_size);
173 assert (cf->head.hash_size > 2);
174 hash_bytes = cf->head.hash_size * sizeof(int);
175 assert (cf->head.next_bucket > 0);
176 assert (cf->head.next_block > 0);
177 if (cf->head.state == 1)
178 cf->array = xmalloc (hash_bytes);
183 if (cf->head.state == 1)
185 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
186 for (i = 0; i<cf->head.hash_size; i++)
187 cf->parray[i] = NULL;
191 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
192 cf->bucket_in_memory = 0;
193 cf->max_bucket_in_memory = 100;
195 cf->iobuf = xmalloc (cf->head.block_size);
196 memset (cf->iobuf, 0, cf->head.block_size);
202 static int cf_hash (CFile cf, int no)
204 return (no>>3) % cf->head.hash_size;
207 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
210 p->lru_prev->lru_next = p->lru_next;
212 cf->bucket_lru_back = p->lru_next;
214 p->lru_next->lru_prev = p->lru_prev;
216 cf->bucket_lru_front = p->lru_prev;
218 *p->h_prev = p->h_next;
220 p->h_next->h_prev = p->h_prev;
222 --(cf->bucket_in_memory);
226 static void flush_bucket (CFile cf, int no_to_flush)
229 struct CFile_hash_bucket *p;
231 for (i = 0; i != no_to_flush; i++)
233 p = cf->bucket_lru_back;
238 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
241 release_bucket (cf, p);
245 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
247 struct CFile_hash_bucket *p, **pp;
249 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
250 flush_bucket (cf, 1);
251 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
252 ++(cf->bucket_in_memory);
253 p = xmalloc (sizeof(*p));
256 p->lru_prev = cf->bucket_lru_front;
257 if (cf->bucket_lru_front)
258 cf->bucket_lru_front->lru_next = p;
260 cf->bucket_lru_back = p;
261 cf->bucket_lru_front = p;
263 pp = cf->parray + hno;
267 (*pp)->h_prev = &p->h_next;
272 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
274 struct CFile_hash_bucket *p;
276 p = alloc_bucket (cf, block_no, hno);
277 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
279 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
282 assert (p->ph.this_bucket == block_no);
287 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
289 struct CFile_hash_bucket *p;
292 block_no = *block_nop = cf->head.next_bucket++;
293 p = alloc_bucket (cf, block_no, hno);
295 for (i = 0; i<HASH_BUCKET; i++)
300 p->ph.next_bucket = 0;
301 p->ph.this_bucket = block_no;
306 static int cf_lookup_flat (CFile cf, int no)
308 int hno = (no*sizeof(int))/HASH_BSIZE;
309 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
312 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
316 static int cf_lookup_hash (CFile cf, int no)
318 int hno = cf_hash (cf, no);
319 struct CFile_hash_bucket *hb;
322 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
324 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
325 if (hb->ph.no[i] == no)
328 return hb->ph.vno[i];
331 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
333 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
335 if (hb->ph.this_bucket == block_no)
341 /* extra check ... */
342 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
344 if (hb->ph.this_bucket == block_no)
346 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
349 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
350 if (hb->ph.no[i] == no)
352 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
358 hb = get_bucket (cf, block_no, hno);
359 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
360 if (hb->ph.no[i] == no)
361 return hb->ph.vno[i];
366 static void cf_write_flat (CFile cf, int no, int vno)
368 int hno = (no*sizeof(int))/HASH_BSIZE;
369 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
371 hno += cf->head.next_bucket;
372 if (hno >= cf->head.flat_bucket)
373 cf->head.flat_bucket = hno+1;
375 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
378 static void cf_moveto_flat (CFile cf)
380 struct CFile_hash_bucket *p;
383 logf (LOG_DEBUG, "cf: Moving to flat shadow: %s", cf->rmf->name);
384 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
385 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
386 cf->head.next_bucket - cf->head.first_bucket);
387 assert (cf->head.state == 1);
388 flush_bucket (cf, -1);
389 assert (cf->bucket_in_memory == 0);
390 p = xmalloc (sizeof(*p));
391 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
393 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
395 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
398 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
399 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
410 static int cf_lookup (CFile cf, int no)
412 if (cf->head.state > 1)
413 return cf_lookup_flat (cf, no);
414 return cf_lookup_hash (cf, no);
417 static int cf_new_flat (CFile cf, int no)
419 int vno = (cf->head.next_block)++;
421 cf_write_flat (cf, no, vno);
425 static int cf_new_hash (CFile cf, int no)
427 int hno = cf_hash (cf, no);
428 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
429 int *bucketpp = &cf->array[hno];
430 int i, vno = (cf->head.next_block)++;
432 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
433 if (!hb->ph.vno[HASH_BUCKET-1])
434 for (i = 0; i<HASH_BUCKET; i++)
446 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
447 if (hb->ph.this_bucket == *bucketpp)
449 bucketpp = &hb->ph.next_bucket;
457 /* extra check ... */
458 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
460 if (hb->ph.this_bucket == *bucketpp)
462 logf (LOG_FATAL, "Found hash bucket on other chain");
468 hb = get_bucket (cf, *bucketpp, hno);
470 for (i = 0; i<HASH_BUCKET; i++)
478 bucketpp = &hb->ph.next_bucket;
483 hb = new_bucket (cf, bucketpp, hno);
489 int cf_new (CFile cf, int no)
491 if (cf->head.state > 1)
492 return cf_new_flat (cf, no);
493 if (cf->no_miss*2 > cf->no_hits)
496 assert (cf->head.state > 1);
497 return cf_new_flat (cf, no);
499 return cf_new_hash (cf, no);
503 int cf_read (CFile cf, int no, int offset, int num, void *buf)
508 if (!(block = cf_lookup (cf, no)))
510 if (!mf_read (cf->block_mf, block, offset, num, buf))
512 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
518 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
523 if (!(block = cf_lookup (cf, no)))
525 block = cf_new (cf, no);
528 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
529 memcpy (cf->iobuf + offset, buf, num);
535 if (mf_write (cf->block_mf, block, offset, num, buf))
537 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
543 int cf_close (CFile cf)
545 logf (LOG_DEBUG, "cf: close hits=%d miss=%d bucket_in_memory=%d total=%d",
546 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
547 cf->head.next_bucket - cf->head.first_bucket);
548 flush_bucket (cf, -1);
551 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
554 mf_close (cf->hash_mf);
555 mf_close (cf->block_mf);