Changed the messages logged. No real code changed.
[idzebra-moved-to-github.git] / bfile / cfile.c
1 /*
2  * Copyright (C) 1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: cfile.c,v $
7  * Revision 1.19  1997-02-12 20:37:17  adam
8  * Changed the messages logged. No real code changed.
9  *
10  * Revision 1.18  1996/10/29 13:56:15  adam
11  * Include of zebrautl.h instead of alexutil.h.
12  *
13  * Revision 1.17  1996/04/19 16:49:00  adam
14  * Minor changes.
15  *
16  * Revision 1.16  1996/04/19  16:23:47  adam
17  * Serious bug fix in shadow implementation; function new_bucket might
18  * set wrong bucket number on new bucket.
19  *
20  * Revision 1.15  1996/04/18  16:02:56  adam
21  * Changed logging a bit.
22  * Removed warning message when commiting flat shadow files.
23  *
24  * Revision 1.14  1996/04/12  07:01:55  adam
25  * Yet another bug fix (next_block was initialized to 0; now set to 1).
26  *
27  * Revision 1.13  1996/04/09 14:48:49  adam
28  * Bug fix: offset calculation when using flat files was completely broken.
29  *
30  * Revision 1.12  1996/04/09  06:47:28  adam
31  * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
32  *
33  * Revision 1.11  1996/03/26 15:59:05  adam
34  * The directory of the shadow table file can be specified by the new
35  * bf_lockDir call.
36  *
37  * Revision 1.10  1996/02/07  14:03:46  adam
38  * Work on flat indexed shadow files.
39  *
40  * Revision 1.9  1996/02/07  10:08:43  adam
41  * Work on flat shadow (not finished yet).
42  *
43  * Revision 1.8  1995/12/15  12:36:52  adam
44  * Moved hash file information to union.
45  * Renamed commit files.
46  *
47  * Revision 1.7  1995/12/15  10:35:07  adam
48  * Changed names of commit files.
49  *
50  * Revision 1.6  1995/12/11  09:03:53  adam
51  * New function: cf_unlink.
52  * New member of commit file head: state (0) deleted, (1) hash file.
53  *
54  * Revision 1.5  1995/12/08  16:21:14  adam
55  * Work on commit/update.
56  *
57  * Revision 1.4  1995/12/01  16:24:28  adam
58  * Commit files use separate meta file area.
59  *
60  * Revision 1.3  1995/12/01  11:37:22  adam
61  * Cached/commit files implemented as meta-files.
62  *
63  * Revision 1.2  1995/11/30  17:00:49  adam
64  * Several bug fixes. Commit system runs now.
65  *
66  * Revision 1.1  1995/11/30  08:33:11  adam
67  * Started work on commit facility.
68  *
69  */
70
71 #include <assert.h>
72 #include <stdlib.h>
73 #include <string.h>
74
75 #include <zebrautl.h>
76 #include <mfile.h>
77 #include "cfile.h"
78
79 static int write_head (CFile cf)
80 {
81     int left = cf->head.hash_size * sizeof(int);
82     int bno = 1;
83     const char *tab = (char*) cf->array;
84
85     if (!tab)
86         return 0;
87     while (left >= HASH_BSIZE)
88     {
89         mf_write (cf->hash_mf, bno++, 0, 0, tab);
90         tab += HASH_BSIZE;
91         left -= HASH_BSIZE;
92     }
93     if (left > 0)
94         mf_write (cf->hash_mf, bno, 0, left, tab);
95     return 0;
96 }
97
98 static int read_head (CFile cf)
99 {
100     int left = cf->head.hash_size * sizeof(int);
101     int bno = 1;
102     char *tab = (char*) cf->array;
103
104     if (!tab)
105         return 0;
106     while (left >= HASH_BSIZE)
107     {
108         mf_read (cf->hash_mf, bno++, 0, 0, tab);
109         tab += HASH_BSIZE;
110         left -= HASH_BSIZE;
111     }
112     if (left > 0)
113         mf_read (cf->hash_mf, bno, 0, left, tab);
114     return 0;
115 }
116
117
118 CFile cf_open (MFile mf, MFile_area area, const char *fname,
119                int block_size, int wflag, int *firstp)
120 {
121     char path[1024];
122     int i;
123     CFile cf = xmalloc (sizeof(*cf));
124     int hash_bytes;
125    
126     cf->rmf = mf; 
127     logf (LOG_LOG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
128     sprintf (path, "%s-b", fname);
129     if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
130     {
131         logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
132         exit (1);
133     }
134     sprintf (path, "%s-i", fname);
135     if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
136     {
137         logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
138         exit (1);
139     }
140     assert (firstp);
141     if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
142         !cf->head.state)
143     {
144         *firstp = 1;
145         cf->head.state = 1;
146         cf->head.block_size = block_size;
147         cf->head.hash_size = 199;
148         hash_bytes = cf->head.hash_size * sizeof(int);
149         cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket = 
150             (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
151         cf->head.next_block = 1;
152         if (wflag)
153             mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
154         cf->array = xmalloc (hash_bytes);
155         for (i = 0; i<cf->head.hash_size; i++)
156             cf->array[i] = 0;
157         if (wflag)
158             write_head (cf);
159     }
160     else
161     {
162         *firstp = 0;
163         assert (cf->head.block_size == block_size);
164         assert (cf->head.hash_size > 2);
165         hash_bytes = cf->head.hash_size * sizeof(int);
166         assert (cf->head.next_bucket > 0);
167         assert (cf->head.next_block > 0);
168         if (cf->head.state == 1)
169             cf->array = xmalloc (hash_bytes);
170         else
171             cf->array = NULL;
172         read_head (cf);
173     }
174     if (cf->head.state == 1)
175     {
176         cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
177         for (i = 0; i<cf->head.hash_size; i++)
178             cf->parray[i] = NULL;
179     }
180     else
181         cf->parray = NULL;
182     cf->bucket_lru_front = cf->bucket_lru_back = NULL;
183     cf->bucket_in_memory = 0;
184     cf->max_bucket_in_memory = 100;
185     cf->dirty = 0;
186     cf->iobuf = xmalloc (cf->head.block_size);
187     memset (cf->iobuf, 0, cf->head.block_size);
188     cf->no_hits = 0;
189     cf->no_miss = 0;
190     return cf;
191 }
192
193 static int cf_hash (CFile cf, int no)
194 {
195     return (no>>3) % cf->head.hash_size;
196 }
197
198 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
199 {
200     if (p->lru_prev)
201         p->lru_prev->lru_next = p->lru_next;
202     else
203         cf->bucket_lru_back = p->lru_next;
204     if (p->lru_next)
205         p->lru_next->lru_prev = p->lru_prev;
206     else
207         cf->bucket_lru_front = p->lru_prev;
208
209     *p->h_prev = p->h_next;
210     if (p->h_next)
211         p->h_next->h_prev = p->h_prev;
212     
213     --(cf->bucket_in_memory);
214     xfree (p);
215 }
216
217 static void flush_bucket (CFile cf, int no_to_flush)
218 {
219     int i;
220     struct CFile_hash_bucket *p;
221
222     for (i = 0; i != no_to_flush; i++)
223     {
224         p = cf->bucket_lru_back;
225         if (!p)
226             break;
227         if (p->dirty)
228         {
229             mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
230             cf->dirty = 1;
231         }
232         release_bucket (cf, p);
233     }
234 }
235
236 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
237 {
238     struct CFile_hash_bucket *p, **pp;
239
240     if (cf->bucket_in_memory == cf->max_bucket_in_memory)
241         flush_bucket (cf, 1);
242     assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
243     ++(cf->bucket_in_memory);
244     p = xmalloc (sizeof(*p));
245
246     p->lru_next = NULL;
247     p->lru_prev = cf->bucket_lru_front;
248     if (cf->bucket_lru_front)
249         cf->bucket_lru_front->lru_next = p;
250     else
251         cf->bucket_lru_back = p;
252     cf->bucket_lru_front = p; 
253
254     pp = cf->parray + hno;
255     p->h_next = *pp;
256     p->h_prev = pp;
257     if (*pp)
258         (*pp)->h_prev = &p->h_next;
259     *pp = p;
260     return p;
261 }
262
263 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
264 {
265     struct CFile_hash_bucket *p;
266
267     p = alloc_bucket (cf, block_no, hno);
268     if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
269     {
270         logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
271         exit (1);
272     }
273     assert (p->ph.this_bucket == block_no);
274     p->dirty = 0;
275     return p;
276 }
277
278 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
279 {
280     struct CFile_hash_bucket *p;
281     int i, block_no;
282
283     block_no = *block_nop = cf->head.next_bucket++;
284     p = alloc_bucket (cf, block_no, hno);
285
286     for (i = 0; i<HASH_BUCKET; i++)
287     {
288         p->ph.vno[i] = 0;
289         p->ph.no[i] = 0;
290     }
291     p->ph.next_bucket = 0;
292     p->ph.this_bucket = block_no;
293     p->dirty = 1;
294     return p;
295 }
296
297 static int cf_lookup_flat (CFile cf, int no)
298 {
299     int hno = (no*sizeof(int))/HASH_BSIZE;
300     int off = (no*sizeof(int)) - hno*HASH_BSIZE;
301     int vno = 0;
302
303     mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
304     return vno;
305 }
306
307 static int cf_lookup_hash (CFile cf, int no)
308 {
309     int hno = cf_hash (cf, no);
310     struct CFile_hash_bucket *hb;
311     int block_no, i;
312
313     for (hb = cf->parray[hno]; hb; hb = hb->h_next)
314     {
315         for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
316             if (hb->ph.no[i] == no)
317             {
318                 (cf->no_hits)++;
319                 return hb->ph.vno[i];
320             }
321     }
322     for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
323     {
324         for (hb = cf->parray[hno]; hb; hb = hb->h_next)
325         {
326             if (hb->ph.this_bucket == block_no)
327                 break;
328         }
329         if (hb)
330             continue;
331 #if 0
332         /* extra check ... */
333         for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
334         {
335             if (hb->ph.this_bucket == block_no)
336             {
337                 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
338                 abort ();
339             }
340             for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
341                 if (hb->ph.no[i] == no)
342                 {
343                     logf (LOG_FATAL, "Found hash bucket on other chain (2)");
344                     abort ();
345                 }
346         }
347 #endif
348         (cf->no_miss)++;
349         hb = get_bucket (cf, block_no, hno);
350         for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
351             if (hb->ph.no[i] == no)
352                 return hb->ph.vno[i];
353     }
354     return 0;
355 }
356
357 static void cf_write_flat (CFile cf, int no, int vno)
358 {
359     int hno = (no*sizeof(int))/HASH_BSIZE;
360     int off = (no*sizeof(int)) - hno*HASH_BSIZE;
361
362     hno += cf->head.next_bucket;
363     if (hno >= cf->head.flat_bucket)
364         cf->head.flat_bucket = hno+1;
365     cf->dirty = 1;
366     mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
367 }
368
369 static void cf_moveto_flat (CFile cf)
370 {
371     struct CFile_hash_bucket *p;
372     int i, j;
373
374     logf (LOG_LOG, "cf: Moving to flat shadow: %s", cf->rmf->name);
375     logf (LOG_LOG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
376         cf->no_hits, cf->no_miss, cf->bucket_in_memory, 
377         cf->head.next_bucket - cf->head.first_bucket);
378     assert (cf->head.state == 1);
379     flush_bucket (cf, -1);
380     assert (cf->bucket_in_memory == 0);
381     p = xmalloc (sizeof(*p));
382     for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
383     {
384         if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
385         {
386             logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
387             exit (1);
388         }
389         for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
390             cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
391     }
392     xfree (p);
393     xfree (cf->array);
394     cf->array = NULL;
395     xfree (cf->parray);
396     cf->parray = NULL;
397     cf->head.state = 2;
398     cf->dirty = 1;
399 }
400
401 static int cf_lookup (CFile cf, int no)
402 {
403     if (cf->head.state > 1)
404         return cf_lookup_flat (cf, no);
405     return cf_lookup_hash (cf, no);
406 }
407
408 static int cf_new_flat (CFile cf, int no)
409 {
410     int vno = (cf->head.next_block)++;
411
412     cf_write_flat (cf, no, vno);
413     return vno;
414 }
415
416 static int cf_new_hash (CFile cf, int no)
417 {
418     int hno = cf_hash (cf, no);
419     struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
420     int *bucketpp = &cf->array[hno]; 
421     int i, vno = (cf->head.next_block)++;
422   
423     for (hb = cf->parray[hno]; hb; hb = hb->h_next)
424         if (!hb->ph.vno[HASH_BUCKET-1])
425             for (i = 0; i<HASH_BUCKET; i++)
426                 if (!hb->ph.vno[i])
427                 {
428                     (cf->no_hits)++;
429                     hb->ph.no[i] = no;
430                     hb->ph.vno[i] = vno;
431                     hb->dirty = 1;
432                     return vno;
433                 }
434
435     while (*bucketpp)
436     {
437         for (hb = cf->parray[hno]; hb; hb = hb->h_next)
438             if (hb->ph.this_bucket == *bucketpp)
439             {
440                 bucketpp = &hb->ph.next_bucket;
441                 hbprev = hb;
442                 break;
443             }
444         if (hb)
445             continue;
446
447 #if 0
448         /* extra check ... */
449         for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
450         {
451             if (hb->ph.this_bucket == *bucketpp)
452             {
453                 logf (LOG_FATAL, "Found hash bucket on other chain");
454                 abort ();
455             }
456         }
457 #endif
458         (cf->no_miss)++;
459         hb = get_bucket (cf, *bucketpp, hno);
460         assert (hb);
461         for (i = 0; i<HASH_BUCKET; i++)
462             if (!hb->ph.vno[i])
463             {
464                 hb->ph.no[i] = no;
465                 hb->ph.vno[i] = vno;
466                 hb->dirty = 1;
467                 return vno;
468             }
469         bucketpp = &hb->ph.next_bucket;
470         hbprev = hb;
471     }
472     if (hbprev)
473         hbprev->dirty = 1;
474     hb = new_bucket (cf, bucketpp, hno);
475     hb->ph.no[0] = no;
476     hb->ph.vno[0] = vno;
477     return vno;
478 }
479
480 int cf_new (CFile cf, int no)
481 {
482     if (cf->head.state > 1)
483         return cf_new_flat (cf, no);
484     if (cf->no_miss*2 > cf->no_hits)
485     {
486         cf_moveto_flat (cf);
487         assert (cf->head.state > 1);
488         return cf_new_flat (cf, no);
489     }
490     return cf_new_hash (cf, no);
491 }
492
493
494 int cf_read (CFile cf, int no, int offset, int num, void *buf)
495 {
496     int block;
497     
498     assert (cf);
499     if (!(block = cf_lookup (cf, no)))
500         return -1;
501     if (!mf_read (cf->block_mf, block, offset, num, buf))
502     {
503         logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
504         exit (1);
505     }
506     return 1;
507 }
508
509 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
510 {
511     int block;
512
513     assert (cf);
514     if (!(block = cf_lookup (cf, no)))
515     {
516         block = cf_new (cf, no);
517         if (offset || num)
518         {
519             mf_read (cf->rmf, no, 0, 0, cf->iobuf);
520             memcpy (cf->iobuf + offset, buf, num);
521             buf = cf->iobuf;
522             offset = 0;
523             num = 0;
524         }
525     }
526     if (mf_write (cf->block_mf, block, offset, num, buf))
527     {
528         logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
529         exit (1);
530     }
531     return 0;
532 }
533
534 int cf_close (CFile cf)
535 {
536     logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
537           cf->no_hits, cf->no_miss, cf->bucket_in_memory,
538           cf->head.next_bucket - cf->head.first_bucket);
539     flush_bucket (cf, -1);
540     if (cf->dirty)
541     {
542         mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
543         write_head (cf);
544     }
545     mf_close (cf->hash_mf);
546     mf_close (cf->block_mf);
547     xfree (cf->array);
548     xfree (cf->parray);
549     xfree (cf->iobuf);
550     xfree (cf);
551     return 0;
552 }
553