2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-11-30 17:00:49 adam
8 * Several bug fixes. Commit system runs now.
10 * Revision 1.1 1995/11/30 08:33:11 adam
11 * Started work on commit facility.
25 static int hash_write (CFile cf, const void *buf, size_t bytes)
29 r = write (cf->hash_fd, buf, bytes);
33 logf (LOG_FATAL|LOG_ERRNO, "write in commit hash file");
35 logf (LOG_FATAL, "write in commit hash file. "
36 "%d bytes instead of %d bytes", r, bytes);
41 static int hash_read (CFile cf, void *buf, size_t bytes)
45 r = read (cf->hash_fd, buf, bytes);
49 logf (LOG_FATAL|LOG_ERRNO, "read in commit hash file");
51 logf (LOG_FATAL, "read in commit hash file. "
52 "%d bytes instead of %d bytes", r, bytes);
57 CFile cf_open (MFile mf, const char *cname, const char *fname,
58 int block_size, int wflag, int *firstp)
62 CFile cf = xmalloc (sizeof(*cf));
66 sprintf (path, "%s.%s.b", cname, fname);
68 open (path, wflag ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
70 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
73 sprintf (path, "%s.%s.h", cname, fname);
75 open (path, wflag ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
77 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
80 r = read (cf->hash_fd, &cf->head, sizeof(cf->head));
81 if (r != sizeof(cf->head))
86 logf (LOG_FATAL|LOG_ERRNO, "read head of %s", path);
91 logf (LOG_FATAL, "illegal head of %s", path);
94 cf->head.block_size = block_size;
95 cf->head.hash_size = 401;
96 hash_bytes = cf->head.hash_size * sizeof(int);
97 cf->head.next_bucket =
98 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
99 cf->head.next_block = 1;
101 hash_write (cf, &cf->head, sizeof(cf->head));
102 cf->array = xmalloc (hash_bytes);
103 for (i = 0; i<cf->head.hash_size; i++)
106 hash_write (cf, cf->array, hash_bytes);
111 assert (cf->head.block_size == block_size);
112 assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
113 hash_bytes = cf->head.hash_size * sizeof(int);
114 assert (cf->head.next_bucket > 0);
115 cf->array = xmalloc (hash_bytes);
116 hash_read (cf, cf->array, hash_bytes);
118 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
119 for (i = 0; i<cf->head.hash_size; i++)
120 cf->parray[i] = NULL;
121 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
122 cf->bucket_in_memory = 0;
123 cf->max_bucket_in_memory = 200;
125 cf->iobuf = xmalloc (cf->head.block_size);
126 memset (cf->iobuf, 0, cf->head.block_size);
130 static int cf_hash (CFile cf, int no)
132 return (no>>3) % cf->head.hash_size;
135 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
138 p->lru_prev->lru_next = p->lru_next;
140 cf->bucket_lru_back = p->lru_next;
142 p->lru_next->lru_prev = p->lru_prev;
144 cf->bucket_lru_front = p->lru_prev;
146 *p->h_prev = p->h_next;
148 p->h_next->h_prev = p->h_prev;
150 --(cf->bucket_in_memory);
154 static void flush_bucket (CFile cf, int no_to_flush)
157 struct CFile_hash_bucket *p;
159 for (i = 0; i != no_to_flush; i++)
161 p = cf->bucket_lru_back;
166 if (lseek (cf->hash_fd, p->ph.this_bucket*HASH_BSIZE, SEEK_SET) < 0)
168 logf (LOG_FATAL|LOG_ERRNO, "lseek in flush_bucket");
171 hash_write (cf, &p->ph, HASH_BSIZE);
174 release_bucket (cf, p);
178 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
180 struct CFile_hash_bucket *p, **pp;
182 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
183 flush_bucket (cf, 1);
184 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
185 ++(cf->bucket_in_memory);
186 p = xmalloc (sizeof(*p));
189 p->lru_prev = cf->bucket_lru_front;
190 if (cf->bucket_lru_front)
191 cf->bucket_lru_front->lru_next = p;
193 cf->bucket_lru_back = p;
194 cf->bucket_lru_front = p;
196 pp = cf->parray + hno;
200 (*pp)->h_prev = &p->h_next;
205 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
207 struct CFile_hash_bucket *p;
209 p = alloc_bucket (cf, block_no, hno);
211 if (lseek (cf->hash_fd, block_no * HASH_BSIZE, SEEK_SET) < 0)
213 logf (LOG_FATAL|LOG_ERRNO, "lseek in get_bucket");
216 hash_read (cf, &p->ph, HASH_BSIZE);
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 (lseek (cf->block_fd, cf->head.block_size * block + offset,
331 logf (LOG_FATAL|LOG_ERRNO, "cf_read, lseek no=%d, block=%d",
335 tor = num ? num : cf->head.block_size;
336 r = read (cf->block_fd, buf, tor);
339 logf (LOG_FATAL|LOG_ERRNO, "cf_read, read no=%d, block=%d",
346 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
351 if (!(block = cf_lookup (cf, no)))
353 block = cf_new (cf, no);
356 mf_read (cf->mf, no, 0, 0, cf->iobuf);
357 memcpy (cf->iobuf + offset, buf, num);
363 if (lseek (cf->block_fd, cf->head.block_size * block + offset,
366 logf (LOG_FATAL|LOG_ERRNO, "cf_write, lseek no=%d, block=%d",
371 tow = num ? num : cf->head.block_size;
372 r = write (cf->block_fd, buf, tow);
375 logf (LOG_FATAL|LOG_ERRNO, "cf_write, read no=%d, block=%d",
382 int cf_close (CFile cf)
384 flush_bucket (cf, -1);
387 int hash_bytes = cf->head.hash_size * sizeof(int);
388 if (lseek (cf->hash_fd, 0L, SEEK_SET) < 0)
390 logf (LOG_FATAL|LOG_ERRNO, "seek in hash fd");
393 hash_write (cf, &cf->head, sizeof(cf->head));
394 hash_write (cf, cf->array, hash_bytes);
396 if (close (cf->hash_fd) < 0)
398 logf (LOG_FATAL|LOG_ERRNO, "close hash fd");
401 if (close (cf->block_fd) < 0)
403 logf (LOG_FATAL|LOG_ERRNO, "close block fd");