2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-12-11 09:03:53 adam
8 * New function: cf_unlink.
9 * New member of commit file head: state (0) deleted, (1) hash file.
11 * Revision 1.5 1995/12/08 16:21:14 adam
12 * Work on commit/update.
14 * Revision 1.4 1995/12/01 16:24:28 adam
15 * Commit files use separate meta file area.
17 * Revision 1.3 1995/12/01 11:37:22 adam
18 * Cached/commit files implemented as meta-files.
20 * Revision 1.2 1995/11/30 17:00:49 adam
21 * Several bug fixes. Commit system runs now.
23 * Revision 1.1 1995/11/30 08:33:11 adam
24 * Started work on commit facility.
36 static int write_head (CFile cf)
38 int left = cf->head.hash_size * sizeof(int);
40 const char *tab = (char*) cf->array;
42 while (left >= HASH_BSIZE)
44 mf_write (cf->hash_mf, bno++, 0, 0, tab);
49 mf_write (cf->hash_mf, bno, 0, left, tab);
53 static int read_head (CFile cf)
55 int left = cf->head.hash_size * sizeof(int);
57 char *tab = (char*) cf->array;
59 while (left >= HASH_BSIZE)
61 mf_read (cf->hash_mf, bno++, 0, 0, tab);
66 mf_read (cf->hash_mf, bno, 0, left, tab);
71 CFile cf_open (MFile mf, MFile_area area, const char *fname,
72 int block_size, int wflag, int *firstp)
76 CFile cf = xmalloc (sizeof(*cf));
80 sprintf (path, "%s.b", fname);
81 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
83 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
86 sprintf (path, "%s.h", fname);
87 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
89 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
93 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
98 cf->head.block_size = block_size;
99 cf->head.hash_size = 401;
100 hash_bytes = cf->head.hash_size * sizeof(int);
101 cf->head.next_bucket =
102 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
103 cf->head.next_block = 1;
105 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
106 cf->array = xmalloc (hash_bytes);
107 for (i = 0; i<cf->head.hash_size; i++)
115 assert (cf->head.block_size == block_size);
116 assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
117 hash_bytes = cf->head.hash_size * sizeof(int);
118 assert (cf->head.next_bucket > 0);
119 cf->array = xmalloc (hash_bytes);
122 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
123 for (i = 0; i<cf->head.hash_size; i++)
124 cf->parray[i] = NULL;
125 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
126 cf->bucket_in_memory = 0;
127 cf->max_bucket_in_memory = 400;
129 cf->iobuf = xmalloc (cf->head.block_size);
130 memset (cf->iobuf, 0, cf->head.block_size);
134 static int cf_hash (CFile cf, int no)
136 return (no>>3) % cf->head.hash_size;
139 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
142 p->lru_prev->lru_next = p->lru_next;
144 cf->bucket_lru_back = p->lru_next;
146 p->lru_next->lru_prev = p->lru_prev;
148 cf->bucket_lru_front = p->lru_prev;
150 *p->h_prev = p->h_next;
152 p->h_next->h_prev = p->h_prev;
154 --(cf->bucket_in_memory);
158 static void flush_bucket (CFile cf, int no_to_flush)
161 struct CFile_hash_bucket *p;
163 for (i = 0; i != no_to_flush; i++)
165 p = cf->bucket_lru_back;
170 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
173 release_bucket (cf, p);
177 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
179 struct CFile_hash_bucket *p, **pp;
181 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
182 flush_bucket (cf, 1);
183 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
184 ++(cf->bucket_in_memory);
185 p = xmalloc (sizeof(*p));
188 p->lru_prev = cf->bucket_lru_front;
189 if (cf->bucket_lru_front)
190 cf->bucket_lru_front->lru_next = p;
192 cf->bucket_lru_back = p;
193 cf->bucket_lru_front = p;
195 pp = cf->parray + hno;
199 (*pp)->h_prev = &p->h_next;
204 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
206 struct CFile_hash_bucket *p;
208 p = alloc_bucket (cf, block_no, hno);
209 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
211 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
214 assert (p->ph.this_bucket == block_no);
219 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
221 struct CFile_hash_bucket *p;
224 *block_no = cf->head.next_bucket++;
225 p = alloc_bucket (cf, *block_no, hno);
227 for (i = 0; i<HASH_BUCKET; i++)
232 p->ph.next_bucket = 0;
233 p->ph.this_bucket = *block_no;
238 int cf_lookup (CFile cf, int no)
240 int hno = cf_hash (cf, no);
241 struct CFile_hash_bucket *hb;
244 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
246 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
247 if (hb->ph.no[i] == no)
248 return hb->ph.vno[i];
250 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
252 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
254 if (hb->ph.this_bucket == block_no)
259 hb = get_bucket (cf, block_no, hno);
260 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
261 if (hb->ph.no[i] == no)
262 return hb->ph.vno[i];
267 int cf_new (CFile cf, int no)
269 int hno = cf_hash (cf, no);
270 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
271 int *bucketpp = &cf->array[hno];
273 int vno = (cf->head.next_block)++;
275 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
276 if (!hb->ph.vno[HASH_BUCKET-1])
277 for (i = 0; i<HASH_BUCKET; i++)
288 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
289 if (hb->ph.this_bucket == *bucketpp)
291 bucketpp = &hb->ph.next_bucket;
297 hb = get_bucket (cf, *bucketpp, hno);
299 for (i = 0; i<HASH_BUCKET; i++)
307 bucketpp = &hb->ph.next_bucket;
312 hb = new_bucket (cf, bucketpp, hno);
318 int cf_read (CFile cf, int no, int offset, int num, void *buf)
323 if (!(block = cf_lookup (cf, no)))
325 if (!mf_read (cf->block_mf, block, offset, num, buf))
327 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
333 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
338 if (!(block = cf_lookup (cf, no)))
340 block = cf_new (cf, no);
343 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
344 memcpy (cf->iobuf + offset, buf, num);
350 if (mf_write (cf->block_mf, block, offset, num, buf))
352 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
358 int cf_close (CFile cf)
360 flush_bucket (cf, -1);
363 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
366 mf_close (cf->hash_mf);
367 mf_close (cf->block_mf);