2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-12-08 16:21:14 adam
8 * Work on commit/update.
10 * Revision 1.4 1995/12/01 16:24:28 adam
11 * Commit files use separate meta file area.
13 * Revision 1.3 1995/12/01 11:37:22 adam
14 * Cached/commit files implemented as meta-files.
16 * Revision 1.2 1995/11/30 17:00:49 adam
17 * Several bug fixes. Commit system runs now.
19 * Revision 1.1 1995/11/30 08:33:11 adam
20 * Started work on commit facility.
32 static int write_head (CFile cf)
34 int left = cf->head.hash_size * sizeof(int);
36 const char *tab = (char*) cf->array;
38 while (left >= HASH_BSIZE)
40 mf_write (cf->hash_mf, bno++, 0, 0, tab);
45 mf_write (cf->hash_mf, bno, 0, left, tab);
49 static int read_head (CFile cf)
51 int left = cf->head.hash_size * sizeof(int);
53 char *tab = (char*) cf->array;
55 while (left >= HASH_BSIZE)
57 mf_read (cf->hash_mf, bno++, 0, 0, tab);
62 mf_read (cf->hash_mf, bno, 0, left, tab);
67 CFile cf_open (MFile mf, MFile_area area, const char *fname,
68 int block_size, int wflag, int *firstp)
72 CFile cf = xmalloc (sizeof(*cf));
76 sprintf (path, "%s.b", fname);
77 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
79 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
82 sprintf (path, "%s.h", fname);
83 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
85 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
88 if (!firstp || !mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head))
92 cf->head.block_size = block_size;
93 cf->head.hash_size = 401;
94 hash_bytes = cf->head.hash_size * sizeof(int);
95 cf->head.next_bucket =
96 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
97 cf->head.next_block = 1;
99 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
100 cf->array = xmalloc (hash_bytes);
101 for (i = 0; i<cf->head.hash_size; i++)
109 assert (cf->head.block_size == block_size);
110 assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
111 hash_bytes = cf->head.hash_size * sizeof(int);
112 assert (cf->head.next_bucket > 0);
113 cf->array = xmalloc (hash_bytes);
116 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
117 for (i = 0; i<cf->head.hash_size; i++)
118 cf->parray[i] = NULL;
119 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
120 cf->bucket_in_memory = 0;
121 cf->max_bucket_in_memory = 400;
123 cf->iobuf = xmalloc (cf->head.block_size);
124 memset (cf->iobuf, 0, cf->head.block_size);
128 static int cf_hash (CFile cf, int no)
130 return (no>>3) % cf->head.hash_size;
133 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
136 p->lru_prev->lru_next = p->lru_next;
138 cf->bucket_lru_back = p->lru_next;
140 p->lru_next->lru_prev = p->lru_prev;
142 cf->bucket_lru_front = p->lru_prev;
144 *p->h_prev = p->h_next;
146 p->h_next->h_prev = p->h_prev;
148 --(cf->bucket_in_memory);
152 static void flush_bucket (CFile cf, int no_to_flush)
155 struct CFile_hash_bucket *p;
157 for (i = 0; i != no_to_flush; i++)
159 p = cf->bucket_lru_back;
164 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
167 release_bucket (cf, p);
171 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
173 struct CFile_hash_bucket *p, **pp;
175 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
176 flush_bucket (cf, 1);
177 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
178 ++(cf->bucket_in_memory);
179 p = xmalloc (sizeof(*p));
182 p->lru_prev = cf->bucket_lru_front;
183 if (cf->bucket_lru_front)
184 cf->bucket_lru_front->lru_next = p;
186 cf->bucket_lru_back = p;
187 cf->bucket_lru_front = p;
189 pp = cf->parray + hno;
193 (*pp)->h_prev = &p->h_next;
198 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
200 struct CFile_hash_bucket *p;
202 p = alloc_bucket (cf, block_no, hno);
203 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
205 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
208 assert (p->ph.this_bucket == block_no);
213 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
215 struct CFile_hash_bucket *p;
218 *block_no = cf->head.next_bucket++;
219 p = alloc_bucket (cf, *block_no, hno);
221 for (i = 0; i<HASH_BUCKET; i++)
226 p->ph.next_bucket = 0;
227 p->ph.this_bucket = *block_no;
232 int cf_lookup (CFile cf, int no)
234 int hno = cf_hash (cf, no);
235 struct CFile_hash_bucket *hb;
238 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
240 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
241 if (hb->ph.no[i] == no)
242 return hb->ph.vno[i];
244 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
246 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
248 if (hb->ph.this_bucket == block_no)
253 hb = get_bucket (cf, block_no, hno);
254 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
255 if (hb->ph.no[i] == no)
256 return hb->ph.vno[i];
261 int cf_new (CFile cf, int no)
263 int hno = cf_hash (cf, no);
264 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
265 int *bucketpp = &cf->array[hno];
267 int vno = (cf->head.next_block)++;
269 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
270 if (!hb->ph.vno[HASH_BUCKET-1])
271 for (i = 0; i<HASH_BUCKET; i++)
282 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
283 if (hb->ph.this_bucket == *bucketpp)
285 bucketpp = &hb->ph.next_bucket;
291 hb = get_bucket (cf, *bucketpp, hno);
293 for (i = 0; i<HASH_BUCKET; i++)
301 bucketpp = &hb->ph.next_bucket;
306 hb = new_bucket (cf, bucketpp, hno);
312 int cf_read (CFile cf, int no, int offset, int num, void *buf)
317 if (!(block = cf_lookup (cf, no)))
319 if (!mf_read (cf->block_mf, block, offset, num, buf))
321 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
327 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
332 if (!(block = cf_lookup (cf, no)))
334 block = cf_new (cf, no);
337 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
338 memcpy (cf->iobuf + offset, buf, num);
344 if (mf_write (cf->block_mf, block, offset, num, buf))
346 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
352 int cf_close (CFile cf)
354 flush_bucket (cf, -1);
357 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
360 mf_close (cf->hash_mf);
361 mf_close (cf->block_mf);