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