57276792006f35fd4aa70041fc18bc13398f0ba2
[idzebra-moved-to-github.git] / bfile / commit.c
1 /* $Id: commit.c,v 1.16 2002-08-02 19:26:55 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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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 void cf_unlink (CFile cf)
35 {
36     if (cf->bucket_in_memory)
37     {
38         logf (LOG_FATAL, "Cannot unlink potential dirty cache");
39         exit (1);
40     }
41     cf->head.state = 0;
42     cf->dirty = 1;
43     mf_unlink (cf->block_mf);
44     mf_unlink (cf->hash_mf);
45 }
46
47
48 #if CF_OPTIMIZE_COMMIT
49 struct map_cache_entity {
50     int from;
51     int to;
52 };
53
54 struct map_cache {
55     int max;
56     int no;
57
58     struct map_cache_entity *map;
59     char *buf;
60     CFile cf;
61 };
62
63 static struct map_cache *map_cache_init (CFile cf)
64 {
65     int mem_max = 2000000;
66     struct map_cache *m_p;
67
68     m_p = xmalloc (sizeof(*m_p));
69     m_p->cf = cf;
70     m_p->max = mem_max / cf->head.block_size;
71     m_p->buf = xmalloc (mem_max);
72     m_p->no = 0;
73     m_p->map = xmalloc (sizeof(*m_p->map) * m_p->max);
74     return m_p;
75 }
76
77 static int map_cache_cmp_from (const void *p1, const void *p2)
78 {
79     return ((struct map_cache_entity*) p1)->from -
80         ((struct map_cache_entity*) p2)->from;
81 }
82
83 static int map_cache_cmp_to (const void *p1, const void *p2)
84 {
85     return ((struct map_cache_entity*) p1)->to -
86         ((struct map_cache_entity*) p2)->to;
87 }
88
89 static void map_cache_flush (struct map_cache *m_p)
90 {
91     int i;
92
93     qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_from);
94     assert (m_p->no < 2 || m_p->map[0].from < m_p->map[1].from);
95     for (i = 0; i<m_p->no; i++)
96     {
97         if (!mf_read (m_p->cf->block_mf, m_p->map[i].from, 0, 0,
98                       m_p->buf + i * m_p->cf->head.block_size))
99         {
100             logf (LOG_FATAL, "read commit block at position %d",
101                   m_p->map[i].from);
102             exit (1);
103         }
104         m_p->map[i].from = i;
105     }
106     qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_to);
107     assert (m_p->no < 2 || m_p->map[0].to < m_p->map[1].to);
108     for (i = 0; i<m_p->no; i++)
109     {
110         mf_write (m_p->cf->rmf, m_p->map[i].to, 0, 0,
111                   m_p->buf + m_p->map[i].from * m_p->cf->head.block_size);
112     }    
113     m_p->no = 0;
114 }
115
116 static void map_cache_del (struct map_cache *m_p)
117 {
118     map_cache_flush (m_p);
119     xfree (m_p->map);
120     xfree (m_p->buf);
121     xfree (m_p);
122 }
123
124 static void map_cache_add (struct map_cache *m_p, int from, int to)
125 {
126     int i = m_p->no;
127
128     m_p->map[i].from = from;
129     m_p->map[i].to = to;
130     m_p->no = ++i;
131     if (i == m_p->max)
132         map_cache_flush (m_p);
133 }
134
135 /* CF_OPTIMIZE_COMMIT */
136 #endif
137
138 static void cf_commit_hash (CFile cf)
139
140     int i, bucket_no;
141     int hash_bytes;
142     struct CFile_ph_bucket *p;
143 #if CF_OPTIMIZE_COMMIT
144     struct map_cache *m_p;
145 #endif
146
147 #if CF_OPTIMIZE_COMMIT
148     m_p = map_cache_init (cf);
149 #endif
150
151     p = (struct CFile_ph_bucket *) xmalloc (sizeof(*p));
152     hash_bytes = cf->head.hash_size * sizeof(int);
153     bucket_no = cf->head.first_bucket;
154     for (; bucket_no < cf->head.next_bucket; bucket_no++)
155     {
156         if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
157         {
158             logf (LOG_FATAL, "read commit hash");
159             exit (1);
160         }
161         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
162         {
163 #if CF_OPTIMIZE_COMMIT
164             map_cache_add (m_p, p->vno[i], p->no[i]);
165 #else
166             if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
167             {
168                 logf (LOG_FATAL, "read commit block");
169                 exit (1);
170             }
171             mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
172 #endif
173         }
174     }
175 #if CF_OPTIMIZE_COMMIT
176     map_cache_del (m_p);
177 #endif
178     xfree (p);
179 }
180
181 static void cf_commit_flat (CFile cf)
182 {
183     int *fp;
184     int hno;
185     int i, vno = 0;
186
187 #if CF_OPTIMIZE_COMMIT
188     struct map_cache *m_p;
189 #endif
190
191
192 #if CF_OPTIMIZE_COMMIT
193     m_p = map_cache_init (cf);
194 #endif
195     fp = (int *) xmalloc (HASH_BSIZE);
196     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
197     {
198         for (i = 0; i < (int) (HASH_BSIZE/sizeof(int)); i++)
199             fp[i] = 0;
200         if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
201             hno != cf->head.flat_bucket-1)
202         {
203             logf (LOG_FATAL, "read index block hno=%d (%d-%d) commit",
204                   hno, cf->head.next_bucket, cf->head.flat_bucket-1);
205         }
206         for (i = 0; i < (int) (HASH_BSIZE/sizeof(int)); i++)
207         {
208             if (fp[i])
209             {
210 #if CF_OPTIMIZE_COMMIT
211                 map_cache_add (m_p, fp[i], vno);
212 #else
213                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
214                 {
215                     logf (LOG_FATAL, "read data block hno=%d (%d-%d) "
216                                      "i=%d commit block at %d (->%d)",
217                           hno, cf->head.next_bucket, cf->head.flat_bucket-1,
218                           i, fp[i], vno);
219                     exit (1);
220                 }
221                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
222
223 #endif
224             }
225             vno++;
226         }
227     }
228 #if CF_OPTIMIZE_COMMIT
229     map_cache_del (m_p);
230 #endif
231     xfree (fp);
232 }
233
234 void cf_commit (CFile cf)
235 {
236
237     if (cf->bucket_in_memory)
238     {
239         logf (LOG_FATAL, "Cannot commit potential dirty cache");
240         exit (1);
241     }
242     if (cf->head.state == 1)
243         cf_commit_hash (cf);
244     else if (cf->head.state == 2)
245         cf_commit_flat (cf);
246 }
247