2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-11-30 08:33:11 adam
8 * Started work on commit facility.
20 static int hash_write (CFile cf, const void *buf, size_t bytes)
24 r = write (cf->hash_fd, buf, bytes);
28 logf (LOG_FATAL|LOG_ERRNO, "write in commit hash file");
30 logf (LOG_FATAL, "write in commit hash file. "
31 "%d bytes instead of %d bytes", r, bytes);
36 static int hash_read (CFile cf, void *buf, size_t bytes)
40 r = read (cf->hash_fd, buf, bytes);
44 logf (LOG_FATAL|LOG_ERRNO, "read in commit hash file");
46 logf (LOG_FATAL, "read in commit hash file. "
47 "%d bytes instead of %d bytes", r, bytes);
52 CFile cf_open (MFile mf, const char *cname, const char *fname,
53 int block_size, int wflag, int *firstp)
57 CFile cf = xmalloc (sizeof(*cf));
61 sprintf (path, "%s.%s.b", cname, fname);
63 open (path, wflag ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
65 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
68 sprintf (path, "%s.%s.h", cname, fname);
70 open (path, wflag ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
72 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
75 r = read (cf->hash_fd, &cf->head, sizeof(cf->head));
76 if (r != sizeof(cf->head))
81 logf (LOG_FATAL|LOG_ERRNO, "read head of %s", path);
86 logf (LOG_FATAL, "illegal head of %s", path);
89 cf->head.block_size = block_size;
90 cf->head.hash_size = 401;
91 hash_bytes = cf->head.hash_size * sizeof(int);
92 cf->head.next_bucket =
93 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
94 cf->head.next_block = 1;
96 hash_write (cf, &cf->head, sizeof(cf->head));
97 cf->array = xmalloc (hash_bytes);
98 for (i = 0; i<cf->head.hash_size; i++)
101 hash_write (cf, cf->array, hash_bytes);
106 assert (cf->head.block_size == block_size);
107 assert (cf->head.hash_size > 2 && cf->head.hash_size < 200000);
108 hash_bytes = cf->head.hash_size * sizeof(int);
109 assert (cf->head.next_bucket > 0);
110 cf->array = xmalloc (hash_bytes);
111 hash_read (cf, cf->array, hash_bytes);
113 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
114 for (i = 0; i<cf->head.hash_size; i++)
115 cf->parray[i] = NULL;
116 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
117 cf->bucket_in_memory = 0;
118 cf->max_bucket_in_memory = 200;
120 cf->iobuf = xmalloc (cf->head.block_size);
124 static int cf_hash (CFile cf, int no)
126 return (no>>3) % cf->head.hash_size;
129 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
132 p->lru_prev->lru_next = p->lru_next;
134 cf->bucket_lru_back = p->lru_next;
136 p->lru_next->lru_prev = p->lru_prev;
138 cf->bucket_lru_front = p->lru_prev;
140 *p->h_prev = p->h_next;
142 p->h_next->h_prev = p->h_prev;
144 --(cf->bucket_in_memory);
148 static void flush_bucket (CFile cf, int no_to_flush)
151 struct CFile_hash_bucket *p;
153 for (i = 0; i != no_to_flush; i++)
155 p = cf->bucket_lru_back;
160 if (lseek (cf->hash_fd, p->ph.this_bucket*HASH_BSIZE, SEEK_SET) < 0)
162 logf (LOG_FATAL|LOG_ERRNO, "lseek in flush_bucket");
165 hash_write (cf, &p->ph, HASH_BSIZE);
168 release_bucket (cf, p);
172 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
174 struct CFile_hash_bucket *p, **pp;
176 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
177 flush_bucket (cf, 1);
178 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
179 ++(cf->bucket_in_memory);
180 p = xmalloc (sizeof(*p));
183 p->lru_prev = cf->bucket_lru_front;
184 if (cf->bucket_lru_front)
185 cf->bucket_lru_front->lru_next = p;
187 cf->bucket_lru_back = p;
188 cf->bucket_lru_front = p;
190 pp = cf->parray + hno;
194 (*pp)->h_prev = &p->h_next;
199 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
201 struct CFile_hash_bucket *p;
203 p = alloc_bucket (cf, block_no, hno);
205 if (lseek (cf->hash_fd, block_no * HASH_BSIZE, SEEK_SET) < 0)
207 logf (LOG_FATAL|LOG_ERRNO, "lseek in get_bucket");
210 hash_read (cf, &p->ph, HASH_BSIZE);
211 assert (p->ph.this_bucket == block_no);
216 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_no, int hno)
218 struct CFile_hash_bucket *p;
221 *block_no = cf->head.next_bucket++;
222 p = alloc_bucket (cf, *block_no, hno);
224 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 logf (LOG_LOG, "cf_lookup pass 1");
239 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
241 logf (LOG_LOG, "bucket_no=%d", hb->ph.this_bucket);
242 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
243 if (hb->ph.no[i] == no)
244 return hb->ph.vno[i];
246 logf (LOG_LOG, "cf_lookup pass 2");
247 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
249 logf (LOG_LOG, "bucket_no=%d", block_no);
250 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
251 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;
289 hb = get_bucket (cf, *bucketpp, hno);
291 for (i = 0; i<HASH_BUCKET; i++)
299 bucketpp = &hb->ph.next_bucket;
304 hb = new_bucket (cf, bucketpp, hno);
310 int cf_read (CFile cf, int no, int offset, int num, void *buf)
315 logf (LOG_LOG, "cf_read no=%d, offset=%d, num=%d", no, offset, num);
316 if (!(block = cf_lookup (cf, no)))
318 if (lseek (cf->block_fd, cf->head.block_size * block + offset,
321 logf (LOG_FATAL|LOG_ERRNO, "cf_read, lseek no=%d, block=%d",
325 r = read (cf->block_fd, buf, num ? num : cf->head.block_size);
326 if (r != cf->head.block_size)
328 logf (LOG_FATAL|LOG_ERRNO, "cf_read, read no=%d, block=%d",
335 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
341 logf (LOG_LOG, "cf_write no=%d, offset=%d, num=%d", no, offset, num);
342 if (!(block = cf_lookup (cf, no)))
344 block = cf_new (cf, no);
347 mf_read (cf->mf, no, 0, 0, cf->iobuf);
348 memcpy (cf->iobuf + offset, buf, num);
354 if (lseek (cf->block_fd, cf->head.block_size * block + offset,
357 logf (LOG_FATAL|LOG_ERRNO, "cf_write, lseek no=%d, block=%d",
361 r = write (cf->block_fd, buf, num ? num : cf->head.block_size);
362 if (r != cf->head.block_size)
364 logf (LOG_FATAL|LOG_ERRNO, "cf_write, read no=%d, block=%d",
371 int cf_close (CFile cf)
373 flush_bucket (cf, -1);
376 int hash_bytes = cf->head.hash_size * sizeof(int);
377 if (lseek (cf->hash_fd, 0L, SEEK_SET) < 0)
379 logf (LOG_FATAL|LOG_ERRNO, "seek in hash fd");
382 hash_write (cf, &cf->head, sizeof(cf->head));
383 hash_write (cf, cf->array, hash_bytes);
385 if (close (cf->hash_fd) < 0)
387 logf (LOG_FATAL|LOG_ERRNO, "close hash fd");
390 if (close (cf->block_fd) < 0)
392 logf (LOG_FATAL|LOG_ERRNO, "close block fd");