2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1996-04-09 06:47:28 adam
8 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
10 * Revision 1.11 1996/03/26 15:59:05 adam
11 * The directory of the shadow table file can be specified by the new
14 * Revision 1.10 1996/02/07 14:03:46 adam
15 * Work on flat indexed shadow files.
17 * Revision 1.9 1996/02/07 10:08:43 adam
18 * Work on flat shadow (not finished yet).
20 * Revision 1.8 1995/12/15 12:36:52 adam
21 * Moved hash file information to union.
22 * Renamed commit files.
24 * Revision 1.7 1995/12/15 10:35:07 adam
25 * Changed names of commit files.
27 * Revision 1.6 1995/12/11 09:03:53 adam
28 * New function: cf_unlink.
29 * New member of commit file head: state (0) deleted, (1) hash file.
31 * Revision 1.5 1995/12/08 16:21:14 adam
32 * Work on commit/update.
34 * Revision 1.4 1995/12/01 16:24:28 adam
35 * Commit files use separate meta file area.
37 * Revision 1.3 1995/12/01 11:37:22 adam
38 * Cached/commit files implemented as meta-files.
40 * Revision 1.2 1995/11/30 17:00:49 adam
41 * Several bug fixes. Commit system runs now.
43 * Revision 1.1 1995/11/30 08:33:11 adam
44 * Started work on commit facility.
56 static int write_head (CFile cf)
58 int left = cf->head.hash_size * sizeof(int);
60 const char *tab = (char*) cf->array;
64 while (left >= HASH_BSIZE)
66 mf_write (cf->hash_mf, bno++, 0, 0, tab);
71 mf_write (cf->hash_mf, bno, 0, left, tab);
75 static int read_head (CFile cf)
77 int left = cf->head.hash_size * sizeof(int);
79 char *tab = (char*) cf->array;
83 while (left >= HASH_BSIZE)
85 mf_read (cf->hash_mf, bno++, 0, 0, tab);
90 mf_read (cf->hash_mf, bno, 0, left, tab);
95 CFile cf_open (MFile mf, MFile_area area, const char *fname,
96 int block_size, int wflag, int *firstp)
100 CFile cf = xmalloc (sizeof(*cf));
104 logf (LOG_LOG, "cf_open %s", cf->rmf->name);
105 sprintf (path, "%s-b", fname);
106 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
108 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
111 sprintf (path, "%s-i", fname);
112 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
114 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
118 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
123 cf->head.block_size = block_size;
124 cf->head.hash_size = 199;
125 hash_bytes = cf->head.hash_size * sizeof(int);
126 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
127 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
128 cf->head.next_block = 1;
130 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
131 cf->array = xmalloc (hash_bytes);
132 for (i = 0; i<cf->head.hash_size; i++)
140 assert (cf->head.block_size == block_size);
141 assert (cf->head.hash_size > 2);
142 hash_bytes = cf->head.hash_size * sizeof(int);
143 assert (cf->head.next_bucket > 0);
144 if (cf->head.state == 1)
145 cf->array = xmalloc (hash_bytes);
150 if (cf->head.state == 1)
152 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
153 for (i = 0; i<cf->head.hash_size; i++)
154 cf->parray[i] = NULL;
158 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
159 cf->bucket_in_memory = 0;
160 cf->max_bucket_in_memory = 100;
162 cf->iobuf = xmalloc (cf->head.block_size);
163 memset (cf->iobuf, 0, cf->head.block_size);
169 static int cf_hash (CFile cf, int no)
171 return (no>>3) % cf->head.hash_size;
174 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
177 p->lru_prev->lru_next = p->lru_next;
179 cf->bucket_lru_back = p->lru_next;
181 p->lru_next->lru_prev = p->lru_prev;
183 cf->bucket_lru_front = p->lru_prev;
185 *p->h_prev = p->h_next;
187 p->h_next->h_prev = p->h_prev;
189 --(cf->bucket_in_memory);
193 static void flush_bucket (CFile cf, int no_to_flush)
196 struct CFile_hash_bucket *p;
198 for (i = 0; i != no_to_flush; i++)
200 p = cf->bucket_lru_back;
205 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
208 release_bucket (cf, p);
212 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
214 struct CFile_hash_bucket *p, **pp;
216 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
217 flush_bucket (cf, 1);
218 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
219 ++(cf->bucket_in_memory);
220 p = xmalloc (sizeof(*p));
223 p->lru_prev = cf->bucket_lru_front;
224 if (cf->bucket_lru_front)
225 cf->bucket_lru_front->lru_next = p;
227 cf->bucket_lru_back = p;
228 cf->bucket_lru_front = p;
230 pp = cf->parray + hno;
234 (*pp)->h_prev = &p->h_next;
239 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
241 struct CFile_hash_bucket *p;
243 p = alloc_bucket (cf, block_no, hno);
244 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
246 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
249 assert (p->ph.this_bucket == block_no);
254 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
256 struct CFile_hash_bucket *p;
259 *block_no = cf->head.next_bucket++;
260 p = alloc_bucket (cf, *block_no, hno);
262 for (i = 0; i<HASH_BUCKET; i++)
267 p->ph.next_bucket = 0;
268 p->ph.this_bucket = *block_no;
273 static int cf_lookup_flat (CFile cf, int no)
275 int hno = (no*sizeof(int))/HASH_BSIZE;
276 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
279 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
283 static int cf_lookup_hash (CFile cf, int no)
285 int hno = cf_hash (cf, no);
286 struct CFile_hash_bucket *hb;
289 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
291 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
292 if (hb->ph.no[i] == no)
295 return hb->ph.vno[i];
298 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
300 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
302 if (hb->ph.this_bucket == block_no)
308 hb = get_bucket (cf, block_no, hno);
309 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
310 if (hb->ph.no[i] == no)
311 return hb->ph.vno[i];
316 static void cf_write_flat (CFile cf, int no, int vno)
318 int hno = (no*sizeof(int))/HASH_BSIZE;
319 int off = (no*sizeof(int)) - hno*sizeof(HASH_BSIZE);
321 hno += cf->head.next_bucket;
322 if (hno >= cf->head.flat_bucket)
323 cf->head.flat_bucket = hno+1;
324 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
327 static void cf_moveto_flat (CFile cf)
329 struct CFile_hash_bucket *p;
332 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
333 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
334 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
335 cf->head.next_bucket - cf->head.first_bucket);
336 assert (cf->head.state == 1);
337 flush_bucket (cf, -1);
338 assert (cf->bucket_in_memory == 0);
339 p = xmalloc (sizeof(*p));
340 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
342 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
344 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
347 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
348 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
358 static int cf_lookup (CFile cf, int no)
360 if (cf->head.state > 1)
361 return cf_lookup_flat (cf, no);
362 return cf_lookup_hash (cf, no);
365 static int cf_new_flat (CFile cf, int no)
367 int vno = (cf->head.next_block)++;
369 cf_write_flat (cf, no, vno);
373 static int cf_new_hash (CFile cf, int no)
375 int hno = cf_hash (cf, no);
376 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
377 int *bucketpp = &cf->array[hno];
378 int i, vno = (cf->head.next_block)++;
380 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
381 if (!hb->ph.vno[HASH_BUCKET-1])
382 for (i = 0; i<HASH_BUCKET; i++)
394 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
395 if (hb->ph.this_bucket == *bucketpp)
397 bucketpp = &hb->ph.next_bucket;
404 hb = get_bucket (cf, *bucketpp, hno);
406 for (i = 0; i<HASH_BUCKET; i++)
414 bucketpp = &hb->ph.next_bucket;
419 hb = new_bucket (cf, bucketpp, hno);
425 int cf_new (CFile cf, int no)
427 if (cf->head.state > 1)
428 return cf_new_flat (cf, no);
429 if (cf->no_miss*5 > cf->no_hits)
432 assert (cf->head.state > 1);
433 return cf_new_flat (cf, no);
435 return cf_new_hash (cf, no);
439 int cf_read (CFile cf, int no, int offset, int num, void *buf)
444 if (!(block = cf_lookup (cf, no)))
446 if (!mf_read (cf->block_mf, block, offset, num, buf))
448 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
454 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
459 if (!(block = cf_lookup (cf, no)))
461 block = cf_new (cf, no);
464 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
465 memcpy (cf->iobuf + offset, buf, num);
471 if (mf_write (cf->block_mf, block, offset, num, buf))
473 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
479 int cf_close (CFile cf)
481 logf (LOG_LOG, "cf_close %s", cf->rmf->name);
482 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
483 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
484 cf->head.next_bucket - cf->head.first_bucket);
485 flush_bucket (cf, -1);
488 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
491 mf_close (cf->hash_mf);
492 mf_close (cf->block_mf);