2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-12-15 10:35:07 adam
8 * Changed names of commit files.
10 * Revision 1.6 1995/12/11 09:03:53 adam
11 * New function: cf_unlink.
12 * New member of commit file head: state (0) deleted, (1) hash file.
14 * Revision 1.5 1995/12/08 16:21:14 adam
15 * Work on commit/update.
17 * Revision 1.4 1995/12/01 16:24:28 adam
18 * Commit files use separate meta file area.
20 * Revision 1.3 1995/12/01 11:37:22 adam
21 * Cached/commit files implemented as meta-files.
23 * Revision 1.2 1995/11/30 17:00:49 adam
24 * Several bug fixes. Commit system runs now.
26 * Revision 1.1 1995/11/30 08:33:11 adam
27 * Started work on commit facility.
39 static int write_head (CFile cf)
41 int left = cf->head.hash_size * sizeof(int);
43 const char *tab = (char*) cf->array;
45 while (left >= HASH_BSIZE)
47 mf_write (cf->hash_mf, bno++, 0, 0, tab);
52 mf_write (cf->hash_mf, bno, 0, left, tab);
56 static int read_head (CFile cf)
58 int left = cf->head.hash_size * sizeof(int);
60 char *tab = (char*) cf->array;
62 while (left >= HASH_BSIZE)
64 mf_read (cf->hash_mf, bno++, 0, 0, tab);
69 mf_read (cf->hash_mf, bno, 0, left, tab);
74 CFile cf_open (MFile mf, MFile_area area, const char *fname,
75 int block_size, int wflag, int *firstp)
79 CFile cf = xmalloc (sizeof(*cf));
83 sprintf (path, "%s-b", fname);
84 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
86 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
89 sprintf (path, "%s-i", fname);
90 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
92 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
96 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
101 cf->head.block_size = block_size;
102 cf->head.hash_size = 401;
103 hash_bytes = cf->head.hash_size * sizeof(int);
104 cf->head.next_bucket =
105 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
106 cf->head.next_block = 1;
108 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
109 cf->array = xmalloc (hash_bytes);
110 for (i = 0; i<cf->head.hash_size; i++)
118 assert (cf->head.block_size == block_size);
119 assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
120 hash_bytes = cf->head.hash_size * sizeof(int);
121 assert (cf->head.next_bucket > 0);
122 cf->array = xmalloc (hash_bytes);
125 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
126 for (i = 0; i<cf->head.hash_size; i++)
127 cf->parray[i] = NULL;
128 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
129 cf->bucket_in_memory = 0;
130 cf->max_bucket_in_memory = 400;
132 cf->iobuf = xmalloc (cf->head.block_size);
133 memset (cf->iobuf, 0, cf->head.block_size);
137 static int cf_hash (CFile cf, int no)
139 return (no>>3) % cf->head.hash_size;
142 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
145 p->lru_prev->lru_next = p->lru_next;
147 cf->bucket_lru_back = p->lru_next;
149 p->lru_next->lru_prev = p->lru_prev;
151 cf->bucket_lru_front = p->lru_prev;
153 *p->h_prev = p->h_next;
155 p->h_next->h_prev = p->h_prev;
157 --(cf->bucket_in_memory);
161 static void flush_bucket (CFile cf, int no_to_flush)
164 struct CFile_hash_bucket *p;
166 for (i = 0; i != no_to_flush; i++)
168 p = cf->bucket_lru_back;
173 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
176 release_bucket (cf, p);
180 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
182 struct CFile_hash_bucket *p, **pp;
184 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
185 flush_bucket (cf, 1);
186 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
187 ++(cf->bucket_in_memory);
188 p = xmalloc (sizeof(*p));
191 p->lru_prev = cf->bucket_lru_front;
192 if (cf->bucket_lru_front)
193 cf->bucket_lru_front->lru_next = p;
195 cf->bucket_lru_back = p;
196 cf->bucket_lru_front = p;
198 pp = cf->parray + hno;
202 (*pp)->h_prev = &p->h_next;
207 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
209 struct CFile_hash_bucket *p;
211 p = alloc_bucket (cf, block_no, hno);
212 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
214 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
217 assert (p->ph.this_bucket == block_no);
222 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
224 struct CFile_hash_bucket *p;
227 *block_no = cf->head.next_bucket++;
228 p = alloc_bucket (cf, *block_no, hno);
230 for (i = 0; i<HASH_BUCKET; i++)
235 p->ph.next_bucket = 0;
236 p->ph.this_bucket = *block_no;
241 int cf_lookup (CFile cf, int no)
243 int hno = cf_hash (cf, no);
244 struct CFile_hash_bucket *hb;
247 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
249 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
250 if (hb->ph.no[i] == no)
251 return hb->ph.vno[i];
253 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
255 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
257 if (hb->ph.this_bucket == block_no)
262 hb = get_bucket (cf, block_no, hno);
263 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
264 if (hb->ph.no[i] == no)
265 return hb->ph.vno[i];
270 int cf_new (CFile cf, int no)
272 int hno = cf_hash (cf, no);
273 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
274 int *bucketpp = &cf->array[hno];
276 int vno = (cf->head.next_block)++;
278 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
279 if (!hb->ph.vno[HASH_BUCKET-1])
280 for (i = 0; i<HASH_BUCKET; i++)
291 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
292 if (hb->ph.this_bucket == *bucketpp)
294 bucketpp = &hb->ph.next_bucket;
300 hb = get_bucket (cf, *bucketpp, hno);
302 for (i = 0; i<HASH_BUCKET; i++)
310 bucketpp = &hb->ph.next_bucket;
315 hb = new_bucket (cf, bucketpp, hno);
321 int cf_read (CFile cf, int no, int offset, int num, void *buf)
326 if (!(block = cf_lookup (cf, no)))
328 if (!mf_read (cf->block_mf, block, offset, num, buf))
330 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
336 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
341 if (!(block = cf_lookup (cf, no)))
343 block = cf_new (cf, no);
346 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
347 memcpy (cf->iobuf + offset, buf, num);
353 if (mf_write (cf->block_mf, block, offset, num, buf))
355 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
361 int cf_close (CFile cf)
363 flush_bucket (cf, -1);
366 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
369 mf_close (cf->hash_mf);
370 mf_close (cf->block_mf);