2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-12-15 12:36:52 adam
8 * Moved hash file information to union.
9 * Renamed commit files.
11 * Revision 1.7 1995/12/15 10:35:07 adam
12 * Changed names of commit files.
14 * Revision 1.6 1995/12/11 09:03:53 adam
15 * New function: cf_unlink.
16 * New member of commit file head: state (0) deleted, (1) hash file.
18 * Revision 1.5 1995/12/08 16:21:14 adam
19 * Work on commit/update.
21 * Revision 1.4 1995/12/01 16:24:28 adam
22 * Commit files use separate meta file area.
24 * Revision 1.3 1995/12/01 11:37:22 adam
25 * Cached/commit files implemented as meta-files.
27 * Revision 1.2 1995/11/30 17:00:49 adam
28 * Several bug fixes. Commit system runs now.
30 * Revision 1.1 1995/11/30 08:33:11 adam
31 * Started work on commit facility.
43 static int write_head (CFile cf)
45 int left = cf->head.u.hash.hash_size * sizeof(int);
47 const char *tab = (char*) cf->array;
49 while (left >= HASH_BSIZE)
51 mf_write (cf->hash_mf, bno++, 0, 0, tab);
56 mf_write (cf->hash_mf, bno, 0, left, tab);
60 static int read_head (CFile cf)
62 int left = cf->head.u.hash.hash_size * sizeof(int);
64 char *tab = (char*) cf->array;
66 while (left >= HASH_BSIZE)
68 mf_read (cf->hash_mf, bno++, 0, 0, tab);
73 mf_read (cf->hash_mf, bno, 0, left, tab);
78 CFile cf_open (MFile mf, MFile_area area, const char *fname,
79 int block_size, int wflag, int *firstp)
83 CFile cf = xmalloc (sizeof(*cf));
87 sprintf (path, "%s-b", fname);
88 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
90 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
93 sprintf (path, "%s-i", fname);
94 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
96 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
100 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
105 cf->head.u.hash.block_size = block_size;
106 cf->head.u.hash.hash_size = 401;
107 hash_bytes = cf->head.u.hash.hash_size * sizeof(int);
108 cf->head.u.hash.next_bucket =
109 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
110 cf->head.u.hash.next_block = 1;
112 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
113 cf->array = xmalloc (hash_bytes);
114 for (i = 0; i<cf->head.u.hash.hash_size; i++)
122 assert (cf->head.u.hash.block_size == block_size);
123 assert (cf->head.u.hash.hash_size > 2);
124 hash_bytes = cf->head.u.hash.hash_size * sizeof(int);
125 assert (cf->head.u.hash.next_bucket > 0);
126 cf->array = xmalloc (hash_bytes);
129 cf->parray = xmalloc (cf->head.u.hash.hash_size * sizeof(*cf->parray));
130 for (i = 0; i<cf->head.u.hash.hash_size; i++)
131 cf->parray[i] = NULL;
132 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
133 cf->bucket_in_memory = 0;
134 cf->max_bucket_in_memory = 400;
136 cf->iobuf = xmalloc (cf->head.u.hash.block_size);
137 memset (cf->iobuf, 0, cf->head.u.hash.block_size);
141 static int cf_hash (CFile cf, int no)
143 return (no>>3) % cf->head.u.hash.hash_size;
146 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
149 p->lru_prev->lru_next = p->lru_next;
151 cf->bucket_lru_back = p->lru_next;
153 p->lru_next->lru_prev = p->lru_prev;
155 cf->bucket_lru_front = p->lru_prev;
157 *p->h_prev = p->h_next;
159 p->h_next->h_prev = p->h_prev;
161 --(cf->bucket_in_memory);
165 static void flush_bucket (CFile cf, int no_to_flush)
168 struct CFile_hash_bucket *p;
170 for (i = 0; i != no_to_flush; i++)
172 p = cf->bucket_lru_back;
177 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
180 release_bucket (cf, p);
184 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
186 struct CFile_hash_bucket *p, **pp;
188 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
189 flush_bucket (cf, 1);
190 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
191 ++(cf->bucket_in_memory);
192 p = xmalloc (sizeof(*p));
195 p->lru_prev = cf->bucket_lru_front;
196 if (cf->bucket_lru_front)
197 cf->bucket_lru_front->lru_next = p;
199 cf->bucket_lru_back = p;
200 cf->bucket_lru_front = p;
202 pp = cf->parray + hno;
206 (*pp)->h_prev = &p->h_next;
211 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
213 struct CFile_hash_bucket *p;
215 p = alloc_bucket (cf, block_no, hno);
216 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
218 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
221 assert (p->ph.this_bucket == block_no);
226 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
228 struct CFile_hash_bucket *p;
231 *block_no = cf->head.u.hash.next_bucket++;
232 p = alloc_bucket (cf, *block_no, hno);
234 for (i = 0; i<HASH_BUCKET; i++)
239 p->ph.next_bucket = 0;
240 p->ph.this_bucket = *block_no;
245 int cf_lookup (CFile cf, int no)
247 int hno = cf_hash (cf, no);
248 struct CFile_hash_bucket *hb;
251 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
253 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
254 if (hb->ph.no[i] == no)
255 return hb->ph.vno[i];
257 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
259 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
261 if (hb->ph.this_bucket == block_no)
266 hb = get_bucket (cf, block_no, hno);
267 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
268 if (hb->ph.no[i] == no)
269 return hb->ph.vno[i];
274 int cf_new (CFile cf, int no)
276 int hno = cf_hash (cf, no);
277 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
278 int *bucketpp = &cf->array[hno];
280 int vno = (cf->head.u.hash.next_block)++;
282 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
283 if (!hb->ph.vno[HASH_BUCKET-1])
284 for (i = 0; i<HASH_BUCKET; i++)
295 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
296 if (hb->ph.this_bucket == *bucketpp)
298 bucketpp = &hb->ph.next_bucket;
304 hb = get_bucket (cf, *bucketpp, hno);
306 for (i = 0; i<HASH_BUCKET; i++)
314 bucketpp = &hb->ph.next_bucket;
319 hb = new_bucket (cf, bucketpp, hno);
325 int cf_read (CFile cf, int no, int offset, int num, void *buf)
330 if (!(block = cf_lookup (cf, no)))
332 if (!mf_read (cf->block_mf, block, offset, num, buf))
334 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
340 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
345 if (!(block = cf_lookup (cf, no)))
347 block = cf_new (cf, no);
350 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
351 memcpy (cf->iobuf + offset, buf, num);
357 if (mf_write (cf->block_mf, block, offset, num, buf))
359 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
365 int cf_close (CFile cf)
367 flush_bucket (cf, -1);
370 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
373 mf_close (cf->hash_mf);
374 mf_close (cf->block_mf);