2 * Copyright (C) 1995-2000, Index Data ApS
6 * Revision 1.26 2000-03-20 19:08:35 adam
7 * Added remote record import using Z39.50 extended services and Segment
10 * Revision 1.25 1999/05/26 07:49:12 adam
13 * Revision 1.24 1999/05/12 13:08:06 adam
14 * First version of ISAMS.
16 * Revision 1.23 1998/10/15 13:09:29 adam
19 * Revision 1.22 1998/10/13 20:07:22 adam
20 * Changed some log messages.
22 * Revision 1.21 1998/08/24 17:29:52 adam
25 * Revision 1.20 1998/08/07 15:07:13 adam
26 * Fixed but in cf_commit_flat.
28 * Revision 1.19 1997/02/12 20:37:17 adam
29 * Changed the messages logged. No real code changed.
31 * Revision 1.18 1996/10/29 13:56:15 adam
32 * Include of zebrautl.h instead of alexutil.h.
34 * Revision 1.17 1996/04/19 16:49:00 adam
37 * Revision 1.16 1996/04/19 16:23:47 adam
38 * Serious bug fix in shadow implementation; function new_bucket might
39 * set wrong bucket number on new bucket.
41 * Revision 1.15 1996/04/18 16:02:56 adam
42 * Changed logging a bit.
43 * Removed warning message when commiting flat shadow files.
45 * Revision 1.14 1996/04/12 07:01:55 adam
46 * Yet another bug fix (next_block was initialized to 0; now set to 1).
48 * Revision 1.13 1996/04/09 14:48:49 adam
49 * Bug fix: offset calculation when using flat files was completely broken.
51 * Revision 1.12 1996/04/09 06:47:28 adam
52 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
54 * Revision 1.11 1996/03/26 15:59:05 adam
55 * The directory of the shadow table file can be specified by the new
58 * Revision 1.10 1996/02/07 14:03:46 adam
59 * Work on flat indexed shadow files.
61 * Revision 1.9 1996/02/07 10:08:43 adam
62 * Work on flat shadow (not finished yet).
64 * Revision 1.8 1995/12/15 12:36:52 adam
65 * Moved hash file information to union.
66 * Renamed commit files.
68 * Revision 1.7 1995/12/15 10:35:07 adam
69 * Changed names of commit files.
71 * Revision 1.6 1995/12/11 09:03:53 adam
72 * New function: cf_unlink.
73 * New member of commit file head: state (0) deleted, (1) hash file.
75 * Revision 1.5 1995/12/08 16:21:14 adam
76 * Work on commit/update.
78 * Revision 1.4 1995/12/01 16:24:28 adam
79 * Commit files use separate meta file area.
81 * Revision 1.3 1995/12/01 11:37:22 adam
82 * Cached/commit files implemented as meta-files.
84 * Revision 1.2 1995/11/30 17:00:49 adam
85 * Several bug fixes. Commit system runs now.
87 * Revision 1.1 1995/11/30 08:33:11 adam
88 * Started work on commit facility.
100 static int write_head (CFile cf)
102 int left = cf->head.hash_size * sizeof(int);
104 const char *tab = (char*) cf->array;
108 while (left >= (int) HASH_BSIZE)
110 mf_write (cf->hash_mf, bno++, 0, 0, tab);
115 mf_write (cf->hash_mf, bno, 0, left, tab);
119 static int read_head (CFile cf)
121 int left = cf->head.hash_size * sizeof(int);
123 char *tab = (char*) cf->array;
127 while (left >= (int) HASH_BSIZE)
129 mf_read (cf->hash_mf, bno++, 0, 0, tab);
134 mf_read (cf->hash_mf, bno, 0, left, tab);
139 CFile cf_open (MFile mf, MFile_area area, const char *fname,
140 int block_size, int wflag, int *firstp)
144 CFile cf = (CFile) xmalloc (sizeof(*cf));
148 logf (LOG_DEBUG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
149 sprintf (path, "%s-b", fname);
150 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
152 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
155 sprintf (path, "%s-i", fname);
156 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
158 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
162 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
167 cf->head.block_size = block_size;
168 cf->head.hash_size = 199;
169 hash_bytes = cf->head.hash_size * sizeof(int);
170 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
171 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
172 cf->head.next_block = 1;
174 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
175 cf->array = (int *) xmalloc (hash_bytes);
176 for (i = 0; i<cf->head.hash_size; i++)
184 assert (cf->head.block_size == block_size);
185 assert (cf->head.hash_size > 2);
186 hash_bytes = cf->head.hash_size * sizeof(int);
187 assert (cf->head.next_bucket > 0);
188 assert (cf->head.next_block > 0);
189 if (cf->head.state == 1)
190 cf->array = (int *) xmalloc (hash_bytes);
195 if (cf->head.state == 1)
197 cf->parray = (struct CFile_hash_bucket **)
198 xmalloc (cf->head.hash_size * sizeof(*cf->parray));
199 for (i = 0; i<cf->head.hash_size; i++)
200 cf->parray[i] = NULL;
204 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
205 cf->bucket_in_memory = 0;
206 cf->max_bucket_in_memory = 100;
208 cf->iobuf = (char *) xmalloc (cf->head.block_size);
209 memset (cf->iobuf, 0, cf->head.block_size);
212 zebra_mutex_init (&cf->mutex);
216 static int cf_hash (CFile cf, int no)
218 return (no>>3) % cf->head.hash_size;
221 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
224 p->lru_prev->lru_next = p->lru_next;
226 cf->bucket_lru_back = p->lru_next;
228 p->lru_next->lru_prev = p->lru_prev;
230 cf->bucket_lru_front = p->lru_prev;
232 *p->h_prev = p->h_next;
234 p->h_next->h_prev = p->h_prev;
236 --(cf->bucket_in_memory);
240 static void flush_bucket (CFile cf, int no_to_flush)
243 struct CFile_hash_bucket *p;
245 for (i = 0; i != no_to_flush; i++)
247 p = cf->bucket_lru_back;
252 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
255 release_bucket (cf, p);
259 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
261 struct CFile_hash_bucket *p, **pp;
263 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
264 flush_bucket (cf, 1);
265 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
266 ++(cf->bucket_in_memory);
267 p = (struct CFile_hash_bucket *) xmalloc (sizeof(*p));
270 p->lru_prev = cf->bucket_lru_front;
271 if (cf->bucket_lru_front)
272 cf->bucket_lru_front->lru_next = p;
274 cf->bucket_lru_back = p;
275 cf->bucket_lru_front = p;
277 pp = cf->parray + hno;
281 (*pp)->h_prev = &p->h_next;
286 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
288 struct CFile_hash_bucket *p;
290 p = alloc_bucket (cf, block_no, hno);
291 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
293 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
296 assert (p->ph.this_bucket == block_no);
301 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
303 struct CFile_hash_bucket *p;
306 block_no = *block_nop = cf->head.next_bucket++;
307 p = alloc_bucket (cf, block_no, hno);
309 for (i = 0; i<HASH_BUCKET; i++)
314 p->ph.next_bucket = 0;
315 p->ph.this_bucket = block_no;
320 static int cf_lookup_flat (CFile cf, int no)
322 int hno = (no*sizeof(int))/HASH_BSIZE;
323 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
326 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
330 static int cf_lookup_hash (CFile cf, int no)
332 int hno = cf_hash (cf, no);
333 struct CFile_hash_bucket *hb;
336 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
338 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
339 if (hb->ph.no[i] == no)
342 return hb->ph.vno[i];
345 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
347 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
349 if (hb->ph.this_bucket == block_no)
355 /* extra check ... */
356 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
358 if (hb->ph.this_bucket == block_no)
360 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
363 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
364 if (hb->ph.no[i] == no)
366 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
372 hb = get_bucket (cf, block_no, hno);
373 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
374 if (hb->ph.no[i] == no)
375 return hb->ph.vno[i];
380 static void cf_write_flat (CFile cf, int no, int vno)
382 int hno = (no*sizeof(int))/HASH_BSIZE;
383 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
385 hno += cf->head.next_bucket;
386 if (hno >= cf->head.flat_bucket)
387 cf->head.flat_bucket = hno+1;
389 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
392 static void cf_moveto_flat (CFile cf)
394 struct CFile_hash_bucket *p;
397 logf (LOG_DEBUG, "cf: Moving to flat shadow: %s", cf->rmf->name);
398 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
399 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
400 cf->head.next_bucket - cf->head.first_bucket);
401 assert (cf->head.state == 1);
402 flush_bucket (cf, -1);
403 assert (cf->bucket_in_memory == 0);
404 p = (struct CFile_hash_bucket *) xmalloc (sizeof(*p));
405 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
407 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
409 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
412 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
413 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
424 static int cf_lookup (CFile cf, int no)
426 if (cf->head.state > 1)
427 return cf_lookup_flat (cf, no);
428 return cf_lookup_hash (cf, no);
431 static int cf_new_flat (CFile cf, int no)
433 int vno = (cf->head.next_block)++;
435 cf_write_flat (cf, no, vno);
439 static int cf_new_hash (CFile cf, int no)
441 int hno = cf_hash (cf, no);
442 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
443 int *bucketpp = &cf->array[hno];
444 int i, vno = (cf->head.next_block)++;
446 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
447 if (!hb->ph.vno[HASH_BUCKET-1])
448 for (i = 0; i<HASH_BUCKET; i++)
460 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
461 if (hb->ph.this_bucket == *bucketpp)
463 bucketpp = &hb->ph.next_bucket;
471 /* extra check ... */
472 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
474 if (hb->ph.this_bucket == *bucketpp)
476 logf (LOG_FATAL, "Found hash bucket on other chain");
482 hb = get_bucket (cf, *bucketpp, hno);
484 for (i = 0; i<HASH_BUCKET; i++)
492 bucketpp = &hb->ph.next_bucket;
497 hb = new_bucket (cf, bucketpp, hno);
503 int cf_new (CFile cf, int no)
505 if (cf->head.state > 1)
506 return cf_new_flat (cf, no);
507 if (cf->no_miss*2 > cf->no_hits)
510 assert (cf->head.state > 1);
511 return cf_new_flat (cf, no);
513 return cf_new_hash (cf, no);
517 int cf_read (CFile cf, int no, int offset, int nbytes, void *buf)
522 zebra_mutex_lock (&cf->mutex);
523 if (!(block = cf_lookup (cf, no)))
525 zebra_mutex_unlock (&cf->mutex);
528 zebra_mutex_unlock (&cf->mutex);
529 if (!mf_read (cf->block_mf, block, offset, nbytes, buf))
531 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
537 int cf_write (CFile cf, int no, int offset, int nbytes, const void *buf)
542 zebra_mutex_lock (&cf->mutex);
543 if (!(block = cf_lookup (cf, no)))
545 block = cf_new (cf, no);
546 if (offset || nbytes)
548 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
549 memcpy (cf->iobuf + offset, buf, nbytes);
555 zebra_mutex_unlock (&cf->mutex);
556 if (mf_write (cf->block_mf, block, offset, nbytes, buf))
558 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
564 int cf_close (CFile cf)
566 logf (LOG_DEBUG, "cf: close hits=%d miss=%d bucket_in_memory=%d total=%d",
567 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
568 cf->head.next_bucket - cf->head.first_bucket);
569 flush_bucket (cf, -1);
572 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
575 mf_close (cf->hash_mf);
576 mf_close (cf->block_mf);
580 zebra_mutex_destroy (&cf->mutex);