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