2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-12-01 11:37:22 adam
8 * Cached/commit files implemented as meta-files.
10 * Revision 1.2 1995/11/30 17:00:49 adam
11 * Several bug fixes. Commit system runs now.
13 * Revision 1.1 1995/11/30 08:33:11 adam
14 * Started work on commit facility.
26 static int write_head (CFile cf)
28 int left = cf->head.hash_size * sizeof(int);
30 const char *tab = (char*) cf->array;
32 while (left >= HASH_BSIZE)
34 mf_write (cf->hash_mf, bno++, 0, 0, tab);
39 mf_write (cf->hash_mf, bno, 0, left, tab);
43 static int read_head (CFile cf)
45 int left = cf->head.hash_size * sizeof(int);
47 char *tab = (char*) cf->array;
49 while (left >= HASH_BSIZE)
51 mf_read (cf->hash_mf, bno++, 0, 0, tab);
56 mf_read (cf->hash_mf, bno, 0, left, tab);
61 CFile cf_open (MFile mf, const char *fname, int block_size, int wflag,
66 CFile cf = xmalloc (sizeof(*cf));
70 sprintf (path, "%s.b", fname);
71 if (!(cf->block_mf = mf_open (0, path, block_size, wflag)))
73 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
76 sprintf (path, "%s.h", fname);
77 if (!(cf->hash_mf = mf_open (0, path, HASH_BSIZE, wflag)))
79 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
82 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head))
85 cf->head.block_size = block_size;
86 cf->head.hash_size = 401;
87 hash_bytes = cf->head.hash_size * sizeof(int);
88 cf->head.next_bucket =
89 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
90 cf->head.next_block = 1;
92 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
93 cf->array = xmalloc (hash_bytes);
94 for (i = 0; i<cf->head.hash_size; i++)
102 assert (cf->head.block_size == block_size);
103 assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
104 hash_bytes = cf->head.hash_size * sizeof(int);
105 assert (cf->head.next_bucket > 0);
106 cf->array = xmalloc (hash_bytes);
109 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
110 for (i = 0; i<cf->head.hash_size; i++)
111 cf->parray[i] = NULL;
112 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
113 cf->bucket_in_memory = 0;
114 cf->max_bucket_in_memory = 200;
116 cf->iobuf = xmalloc (cf->head.block_size);
117 memset (cf->iobuf, 0, cf->head.block_size);
121 static int cf_hash (CFile cf, int no)
123 return (no>>3) % cf->head.hash_size;
126 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
129 p->lru_prev->lru_next = p->lru_next;
131 cf->bucket_lru_back = p->lru_next;
133 p->lru_next->lru_prev = p->lru_prev;
135 cf->bucket_lru_front = p->lru_prev;
137 *p->h_prev = p->h_next;
139 p->h_next->h_prev = p->h_prev;
141 --(cf->bucket_in_memory);
145 static void flush_bucket (CFile cf, int no_to_flush)
148 struct CFile_hash_bucket *p;
150 for (i = 0; i != no_to_flush; i++)
152 p = cf->bucket_lru_back;
157 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
160 release_bucket (cf, p);
164 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
166 struct CFile_hash_bucket *p, **pp;
168 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
169 flush_bucket (cf, 1);
170 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
171 ++(cf->bucket_in_memory);
172 p = xmalloc (sizeof(*p));
175 p->lru_prev = cf->bucket_lru_front;
176 if (cf->bucket_lru_front)
177 cf->bucket_lru_front->lru_next = p;
179 cf->bucket_lru_back = p;
180 cf->bucket_lru_front = p;
182 pp = cf->parray + hno;
186 (*pp)->h_prev = &p->h_next;
191 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
193 struct CFile_hash_bucket *p;
195 p = alloc_bucket (cf, block_no, hno);
196 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
198 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
201 assert (p->ph.this_bucket == block_no);
206 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
208 struct CFile_hash_bucket *p;
211 *block_no = cf->head.next_bucket++;
212 p = alloc_bucket (cf, *block_no, hno);
214 for (i = 0; i<HASH_BUCKET; i++)
219 p->ph.next_bucket = 0;
220 p->ph.this_bucket = *block_no;
225 int cf_lookup (CFile cf, int no)
227 int hno = cf_hash (cf, no);
228 struct CFile_hash_bucket *hb;
231 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
233 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
234 if (hb->ph.no[i] == no)
235 return hb->ph.vno[i];
237 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
239 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
241 if (hb->ph.this_bucket == block_no)
246 hb = get_bucket (cf, block_no, hno);
247 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
248 if (hb->ph.no[i] == no)
249 return hb->ph.vno[i];
254 int cf_new (CFile cf, int no)
256 int hno = cf_hash (cf, no);
257 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
258 int *bucketpp = &cf->array[hno];
260 int vno = (cf->head.next_block)++;
262 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
263 if (!hb->ph.vno[HASH_BUCKET-1])
264 for (i = 0; i<HASH_BUCKET; i++)
275 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
276 if (hb->ph.this_bucket == *bucketpp)
278 bucketpp = &hb->ph.next_bucket;
284 hb = get_bucket (cf, *bucketpp, hno);
286 for (i = 0; i<HASH_BUCKET; i++)
294 bucketpp = &hb->ph.next_bucket;
299 hb = new_bucket (cf, bucketpp, hno);
305 int cf_read (CFile cf, int no, int offset, int num, void *buf)
310 if (!(block = cf_lookup (cf, no)))
312 if (!mf_read (cf->block_mf, block, offset, num, buf))
314 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
320 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
325 if (!(block = cf_lookup (cf, no)))
327 block = cf_new (cf, no);
330 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
331 memcpy (cf->iobuf + offset, buf, num);
337 if (mf_write (cf->block_mf, block, offset, num, buf))
339 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
345 int cf_close (CFile cf)
347 flush_bucket (cf, -1);
350 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
353 mf_close (cf->hash_mf);
354 mf_close (cf->block_mf);