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