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