3f92dde90852df5264ee58f18f2ebf4e1fdf4d7a
[idzebra-moved-to-github.git] / bfile / commit.c
1 /* $Id: commit.c,v 1.16.2.2 2006-10-04 09:07:19 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23
24
25 #include <assert.h>
26 #include <stdlib.h>
27
28 #include <zebrautl.h>
29 #include <mfile.h>
30 #include "cfile.h"
31
32 #define CF_OPTIMIZE_COMMIT 0
33
34 #if CF_OPTIMIZE_COMMIT
35 struct map_cache_entity {
36     int from;
37     int to;
38 };
39
40 struct map_cache {
41     int max;
42     int no;
43
44     struct map_cache_entity *map;
45     char *buf;
46     CFile cf;
47 };
48
49 static struct map_cache *map_cache_init (CFile cf)
50 {
51     int mem_max = 2000000;
52     struct map_cache *m_p;
53
54     m_p = xmalloc (sizeof(*m_p));
55     m_p->cf = cf;
56     m_p->max = mem_max / cf->head.block_size;
57     m_p->buf = xmalloc (mem_max);
58     m_p->no = 0;
59     m_p->map = xmalloc (sizeof(*m_p->map) * m_p->max);
60     return m_p;
61 }
62
63 static int map_cache_cmp_from (const void *p1, const void *p2)
64 {
65     return ((struct map_cache_entity*) p1)->from -
66         ((struct map_cache_entity*) p2)->from;
67 }
68
69 static int map_cache_cmp_to (const void *p1, const void *p2)
70 {
71     return ((struct map_cache_entity*) p1)->to -
72         ((struct map_cache_entity*) p2)->to;
73 }
74
75 static void map_cache_flush (struct map_cache *m_p)
76 {
77     int i;
78
79     qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_from);
80     assert (m_p->no < 2 || m_p->map[0].from < m_p->map[1].from);
81     for (i = 0; i<m_p->no; i++)
82     {
83         if (!mf_read (m_p->cf->block_mf, m_p->map[i].from, 0, 0,
84                       m_p->buf + i * m_p->cf->head.block_size))
85         {
86             logf (LOG_FATAL, "read commit block at position %d",
87                   m_p->map[i].from);
88             exit (1);
89         }
90         m_p->map[i].from = i;
91     }
92     qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_to);
93     assert (m_p->no < 2 || m_p->map[0].to < m_p->map[1].to);
94     for (i = 0; i<m_p->no; i++)
95     {
96         mf_write (m_p->cf->rmf, m_p->map[i].to, 0, 0,
97                   m_p->buf + m_p->map[i].from * m_p->cf->head.block_size);
98     }    
99     m_p->no = 0;
100 }
101
102 static void map_cache_del (struct map_cache *m_p)
103 {
104     map_cache_flush (m_p);
105     xfree (m_p->map);
106     xfree (m_p->buf);
107     xfree (m_p);
108 }
109
110 static void map_cache_add (struct map_cache *m_p, int from, int to)
111 {
112     int i = m_p->no;
113
114     m_p->map[i].from = from;
115     m_p->map[i].to = to;
116     m_p->no = ++i;
117     if (i == m_p->max)
118         map_cache_flush (m_p);
119 }
120
121 /* CF_OPTIMIZE_COMMIT */
122 #endif
123
124 static void cf_commit_hash (CFile cf)
125
126     int i, bucket_no;
127     int hash_bytes;
128     struct CFile_ph_bucket *p;
129 #if CF_OPTIMIZE_COMMIT
130     struct map_cache *m_p;
131 #endif
132
133 #if CF_OPTIMIZE_COMMIT
134     m_p = map_cache_init (cf);
135 #endif
136
137     p = (struct CFile_ph_bucket *) xmalloc (sizeof(*p));
138     hash_bytes = cf->head.hash_size * sizeof(int);
139     bucket_no = cf->head.first_bucket;
140     for (; bucket_no < cf->head.next_bucket; bucket_no++)
141     {
142         if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
143         {
144             logf (LOG_FATAL, "read commit hash");
145             exit (1);
146         }
147         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
148         {
149 #if CF_OPTIMIZE_COMMIT
150             map_cache_add (m_p, p->vno[i], p->no[i]);
151 #else
152             if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
153             {
154                 logf (LOG_FATAL, "read commit block");
155                 exit (1);
156             }
157             mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
158 #endif
159         }
160     }
161 #if CF_OPTIMIZE_COMMIT
162     map_cache_del (m_p);
163 #endif
164     xfree (p);
165 }
166
167 static void cf_commit_flat (CFile cf)
168 {
169     int *fp;
170     int hno;
171     int i, vno = 0;
172
173 #if CF_OPTIMIZE_COMMIT
174     struct map_cache *m_p;
175 #endif
176
177
178 #if CF_OPTIMIZE_COMMIT
179     m_p = map_cache_init (cf);
180 #endif
181     fp = (int *) xmalloc (HASH_BSIZE);
182     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
183     {
184         for (i = 0; i < (int) (HASH_BSIZE/sizeof(int)); i++)
185             fp[i] = 0;
186         if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
187             hno != cf->head.flat_bucket-1)
188         {
189             logf (LOG_FATAL, "read index block hno=%d (%d-%d) commit",
190                   hno, cf->head.next_bucket, cf->head.flat_bucket-1);
191         }
192         for (i = 0; i < (int) (HASH_BSIZE/sizeof(int)); i++)
193         {
194             if (fp[i])
195             {
196 #if CF_OPTIMIZE_COMMIT
197                 map_cache_add (m_p, fp[i], vno);
198 #else
199                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
200                 {
201                     logf (LOG_FATAL, "read data block hno=%d (%d-%d) "
202                                      "i=%d commit block at %d (->%d)",
203                           hno, cf->head.next_bucket, cf->head.flat_bucket-1,
204                           i, fp[i], vno);
205                     exit (1);
206                 }
207                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
208
209 #endif
210             }
211             vno++;
212         }
213     }
214 #if CF_OPTIMIZE_COMMIT
215     map_cache_del (m_p);
216 #endif
217     xfree (fp);
218 }
219
220 void cf_commit (CFile cf)
221 {
222
223     if (cf->bucket_in_memory)
224     {
225         logf (LOG_FATAL, "Cannot commit potential dirty cache");
226         exit (1);
227     }
228     if (cf->head.state == 1)
229         cf_commit_hash (cf);
230     else if (cf->head.state == 2)
231         cf_commit_flat (cf);
232 }
233