2 * Copyright (C) 1995-1999, Index Data ApS
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1999-05-12 13:08:06 adam
8 * First version of ISAMS.
10 * Revision 1.23 1998/10/15 13:09:29 adam
13 * Revision 1.22 1998/10/13 20:07:22 adam
14 * Changed some log messages.
16 * Revision 1.21 1998/08/24 17:29:52 adam
19 * Revision 1.20 1998/08/07 15:07:13 adam
20 * Fixed but in cf_commit_flat.
22 * Revision 1.19 1997/02/12 20:37:17 adam
23 * Changed the messages logged. No real code changed.
25 * Revision 1.18 1996/10/29 13:56:15 adam
26 * Include of zebrautl.h instead of alexutil.h.
28 * Revision 1.17 1996/04/19 16:49:00 adam
31 * Revision 1.16 1996/04/19 16:23:47 adam
32 * Serious bug fix in shadow implementation; function new_bucket might
33 * set wrong bucket number on new bucket.
35 * Revision 1.15 1996/04/18 16:02:56 adam
36 * Changed logging a bit.
37 * Removed warning message when commiting flat shadow files.
39 * Revision 1.14 1996/04/12 07:01:55 adam
40 * Yet another bug fix (next_block was initialized to 0; now set to 1).
42 * Revision 1.13 1996/04/09 14:48:49 adam
43 * Bug fix: offset calculation when using flat files was completely broken.
45 * Revision 1.12 1996/04/09 06:47:28 adam
46 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
48 * Revision 1.11 1996/03/26 15:59:05 adam
49 * The directory of the shadow table file can be specified by the new
52 * Revision 1.10 1996/02/07 14:03:46 adam
53 * Work on flat indexed shadow files.
55 * Revision 1.9 1996/02/07 10:08:43 adam
56 * Work on flat shadow (not finished yet).
58 * Revision 1.8 1995/12/15 12:36:52 adam
59 * Moved hash file information to union.
60 * Renamed commit files.
62 * Revision 1.7 1995/12/15 10:35:07 adam
63 * Changed names of commit files.
65 * Revision 1.6 1995/12/11 09:03:53 adam
66 * New function: cf_unlink.
67 * New member of commit file head: state (0) deleted, (1) hash file.
69 * Revision 1.5 1995/12/08 16:21:14 adam
70 * Work on commit/update.
72 * Revision 1.4 1995/12/01 16:24:28 adam
73 * Commit files use separate meta file area.
75 * Revision 1.3 1995/12/01 11:37:22 adam
76 * Cached/commit files implemented as meta-files.
78 * Revision 1.2 1995/11/30 17:00:49 adam
79 * Several bug fixes. Commit system runs now.
81 * Revision 1.1 1995/11/30 08:33:11 adam
82 * Started work on commit facility.
94 static int write_head (CFile cf)
96 int left = cf->head.hash_size * sizeof(int);
98 const char *tab = (char*) cf->array;
102 while (left >= HASH_BSIZE)
104 mf_write (cf->hash_mf, bno++, 0, 0, tab);
109 mf_write (cf->hash_mf, bno, 0, left, tab);
113 static int read_head (CFile cf)
115 int left = cf->head.hash_size * sizeof(int);
117 char *tab = (char*) cf->array;
121 while (left >= HASH_BSIZE)
123 mf_read (cf->hash_mf, bno++, 0, 0, tab);
128 mf_read (cf->hash_mf, bno, 0, left, tab);
133 CFile cf_open (MFile mf, MFile_area area, const char *fname,
134 int block_size, int wflag, int *firstp)
138 CFile cf = (CFile) xmalloc (sizeof(*cf));
142 logf (LOG_DEBUG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
143 sprintf (path, "%s-b", fname);
144 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
146 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
149 sprintf (path, "%s-i", fname);
150 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
152 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
156 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
161 cf->head.block_size = block_size;
162 cf->head.hash_size = 199;
163 hash_bytes = cf->head.hash_size * sizeof(int);
164 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
165 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
166 cf->head.next_block = 1;
168 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
169 cf->array = xmalloc (hash_bytes);
170 for (i = 0; i<cf->head.hash_size; i++)
178 assert (cf->head.block_size == block_size);
179 assert (cf->head.hash_size > 2);
180 hash_bytes = cf->head.hash_size * sizeof(int);
181 assert (cf->head.next_bucket > 0);
182 assert (cf->head.next_block > 0);
183 if (cf->head.state == 1)
184 cf->array = xmalloc (hash_bytes);
189 if (cf->head.state == 1)
191 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
192 for (i = 0; i<cf->head.hash_size; i++)
193 cf->parray[i] = NULL;
197 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
198 cf->bucket_in_memory = 0;
199 cf->max_bucket_in_memory = 100;
201 cf->iobuf = xmalloc (cf->head.block_size);
202 memset (cf->iobuf, 0, cf->head.block_size);
208 static int cf_hash (CFile cf, int no)
210 return (no>>3) % cf->head.hash_size;
213 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
216 p->lru_prev->lru_next = p->lru_next;
218 cf->bucket_lru_back = p->lru_next;
220 p->lru_next->lru_prev = p->lru_prev;
222 cf->bucket_lru_front = p->lru_prev;
224 *p->h_prev = p->h_next;
226 p->h_next->h_prev = p->h_prev;
228 --(cf->bucket_in_memory);
232 static void flush_bucket (CFile cf, int no_to_flush)
235 struct CFile_hash_bucket *p;
237 for (i = 0; i != no_to_flush; i++)
239 p = cf->bucket_lru_back;
244 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
247 release_bucket (cf, p);
251 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
253 struct CFile_hash_bucket *p, **pp;
255 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
256 flush_bucket (cf, 1);
257 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
258 ++(cf->bucket_in_memory);
259 p = xmalloc (sizeof(*p));
262 p->lru_prev = cf->bucket_lru_front;
263 if (cf->bucket_lru_front)
264 cf->bucket_lru_front->lru_next = p;
266 cf->bucket_lru_back = p;
267 cf->bucket_lru_front = p;
269 pp = cf->parray + hno;
273 (*pp)->h_prev = &p->h_next;
278 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
280 struct CFile_hash_bucket *p;
282 p = alloc_bucket (cf, block_no, hno);
283 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
285 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
288 assert (p->ph.this_bucket == block_no);
293 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
295 struct CFile_hash_bucket *p;
298 block_no = *block_nop = cf->head.next_bucket++;
299 p = alloc_bucket (cf, block_no, hno);
301 for (i = 0; i<HASH_BUCKET; i++)
306 p->ph.next_bucket = 0;
307 p->ph.this_bucket = block_no;
312 static int cf_lookup_flat (CFile cf, int no)
314 int hno = (no*sizeof(int))/HASH_BSIZE;
315 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
318 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
322 static int cf_lookup_hash (CFile cf, int no)
324 int hno = cf_hash (cf, no);
325 struct CFile_hash_bucket *hb;
328 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
330 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
331 if (hb->ph.no[i] == no)
334 return hb->ph.vno[i];
337 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
339 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
341 if (hb->ph.this_bucket == block_no)
347 /* extra check ... */
348 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
350 if (hb->ph.this_bucket == block_no)
352 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
355 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
356 if (hb->ph.no[i] == no)
358 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
364 hb = get_bucket (cf, block_no, hno);
365 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
366 if (hb->ph.no[i] == no)
367 return hb->ph.vno[i];
372 static void cf_write_flat (CFile cf, int no, int vno)
374 int hno = (no*sizeof(int))/HASH_BSIZE;
375 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
377 hno += cf->head.next_bucket;
378 if (hno >= cf->head.flat_bucket)
379 cf->head.flat_bucket = hno+1;
381 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
384 static void cf_moveto_flat (CFile cf)
386 struct CFile_hash_bucket *p;
389 logf (LOG_DEBUG, "cf: Moving to flat shadow: %s", cf->rmf->name);
390 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
391 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
392 cf->head.next_bucket - cf->head.first_bucket);
393 assert (cf->head.state == 1);
394 flush_bucket (cf, -1);
395 assert (cf->bucket_in_memory == 0);
396 p = xmalloc (sizeof(*p));
397 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
399 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
401 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
404 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
405 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
416 static int cf_lookup (CFile cf, int no)
418 if (cf->head.state > 1)
419 return cf_lookup_flat (cf, no);
420 return cf_lookup_hash (cf, no);
423 static int cf_new_flat (CFile cf, int no)
425 int vno = (cf->head.next_block)++;
427 cf_write_flat (cf, no, vno);
431 static int cf_new_hash (CFile cf, int no)
433 int hno = cf_hash (cf, no);
434 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
435 int *bucketpp = &cf->array[hno];
436 int i, vno = (cf->head.next_block)++;
438 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
439 if (!hb->ph.vno[HASH_BUCKET-1])
440 for (i = 0; i<HASH_BUCKET; i++)
452 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
453 if (hb->ph.this_bucket == *bucketpp)
455 bucketpp = &hb->ph.next_bucket;
463 /* extra check ... */
464 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
466 if (hb->ph.this_bucket == *bucketpp)
468 logf (LOG_FATAL, "Found hash bucket on other chain");
474 hb = get_bucket (cf, *bucketpp, hno);
476 for (i = 0; i<HASH_BUCKET; i++)
484 bucketpp = &hb->ph.next_bucket;
489 hb = new_bucket (cf, bucketpp, hno);
495 int cf_new (CFile cf, int no)
497 if (cf->head.state > 1)
498 return cf_new_flat (cf, no);
499 if (cf->no_miss*2 > cf->no_hits)
502 assert (cf->head.state > 1);
503 return cf_new_flat (cf, no);
505 return cf_new_hash (cf, no);
509 int cf_read (CFile cf, int no, int offset, int nbytes, void *buf)
514 if (!(block = cf_lookup (cf, no)))
516 if (!mf_read (cf->block_mf, block, offset, nbytes, buf))
518 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
524 int cf_write (CFile cf, int no, int offset, int nbytes, const void *buf)
529 if (!(block = cf_lookup (cf, no)))
531 block = cf_new (cf, no);
532 if (offset || nbytes)
534 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
535 memcpy (cf->iobuf + offset, buf, nbytes);
541 if (mf_write (cf->block_mf, block, offset, nbytes, buf))
543 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
549 int cf_close (CFile cf)
551 logf (LOG_DEBUG, "cf: close hits=%d miss=%d bucket_in_memory=%d total=%d",
552 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
553 cf->head.next_bucket - cf->head.first_bucket);
554 flush_bucket (cf, -1);
557 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
560 mf_close (cf->hash_mf);
561 mf_close (cf->block_mf);