Changed logging a bit.
[idzebra-moved-to-github.git] / bfile / commit.c
1 /*
2  * Copyright (C) 1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: commit.c,v $
7  * Revision 1.10  1996-04-18 16:02:56  adam
8  * Changed logging a bit.
9  * Removed warning message when commiting flat shadow files.
10  *
11  * Revision 1.9  1996/04/12  07:01:57  adam
12  * Yet another bug fix (next_block was initialized to 0; now set to 1).
13  *
14  * Revision 1.8  1996/02/07 14:03:49  adam
15  * Work on flat indexed shadow files.
16  *
17  * Revision 1.7  1996/02/07  10:08:46  adam
18  * Work on flat shadow (not finished yet).
19  *
20  * Revision 1.6  1995/12/15  12:36:53  adam
21  * Moved hash file information to union.
22  * Renamed commit files.
23  *
24  * Revision 1.5  1995/12/12  15:57:55  adam
25  * Implemented mf_unlink. cf_unlink uses mf_unlink.
26  *
27  * Revision 1.4  1995/12/11  09:03:55  adam
28  * New function: cf_unlink.
29  * New member of commit file head: state (0) deleted, (1) hash file.
30  *
31  * Revision 1.3  1995/12/01  16:24:29  adam
32  * Commit files use separate meta file area.
33  *
34  * Revision 1.2  1995/12/01  11:37:24  adam
35  * Cached/commit files implemented as meta-files.
36  *
37  * Revision 1.1  1995/11/30  08:33:13  adam
38  * Started work on commit facility.
39  *
40  */
41
42 #include <assert.h>
43 #include <stdlib.h>
44
45 #include <alexutil.h>
46 #include <mfile.h>
47 #include "cfile.h"
48
49 void cf_unlink (CFile cf)
50 {
51     if (cf->bucket_in_memory)
52     {
53         logf (LOG_FATAL, "Cannot unlink potential dirty cache");
54         exit (1);
55     }
56     cf->head.state = 0;
57     cf->dirty = 1;
58     mf_unlink (cf->block_mf);
59     mf_unlink (cf->hash_mf);
60 }
61
62    
63 static void cf_commit_hash (CFile cf)
64
65     int i, bucket_no;
66     int hash_bytes;
67     struct CFile_ph_bucket *p;
68
69     p = xmalloc (sizeof(*p));
70     hash_bytes = cf->head.hash_size * sizeof(int);
71     bucket_no = cf->head.first_bucket;
72     for (; bucket_no < cf->head.next_bucket; bucket_no++)
73     {
74         if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
75         {
76             logf (LOG_FATAL, "read commit hash");
77             exit (1);
78         }
79         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
80         {
81             if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
82             {
83                 logf (LOG_FATAL, "read commit block");
84                 exit (1);
85             }
86             mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
87         }
88     }
89     xfree (p);
90 }
91
92 static void cf_commit_flat (CFile cf)
93 {
94     int *fp;
95     int hno;
96     int i, vno = 0;
97
98     fp = xmalloc (HASH_BSIZE);
99     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
100     {
101         if (hno == cf->head.flat_bucket-1)
102         {
103             for (i = 0; i < (HASH_BSIZE/sizeof(int)); i++)
104                 fp[i] = 0;
105         }
106         if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
107             hno != cf->head.flat_bucket-1)
108         {
109             logf (LOG_FATAL, "read index block hno=%d (%d-%d) commit",
110                   hno, cf->head.next_bucket, cf->head.flat_bucket-1);
111         }
112         for (i = 0; i < (HASH_BSIZE/sizeof(int)); i++)
113         {
114             if (fp[i])
115             {
116                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
117                 {
118                     logf (LOG_FATAL, "read data block hno=%d (%d-%d) "
119                                      "i=%d commit block at %d (->%d)",
120                           hno, cf->head.next_bucket, cf->head.flat_bucket-1,
121                           i, fp[i], vno);
122                     exit (1);
123                 }
124                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
125             }
126             vno++;
127         }
128     }
129     xfree (fp);
130 }
131
132 void cf_commit (CFile cf)
133 {
134
135     if (cf->bucket_in_memory)
136     {
137         logf (LOG_FATAL, "Cannot commit potential dirty cache");
138         exit (1);
139     }
140     if (cf->head.state == 1)
141         cf_commit_hash (cf);
142     else if (cf->head.state == 2)
143         cf_commit_flat (cf);
144 }
145