Simplify and optimize commit clean operation
[idzebra-moved-to-github.git] / bfile / commit.c
1 /* $Id: commit.c,v 1.29 2006-10-09 22:10:00 adam Exp $
2    Copyright (C) 1995-2006
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 #include <assert.h>
25 #include <stdlib.h>
26
27 #include <idzebra/util.h>
28 #include <yaz/xmalloc.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             yaz_log (YLOG_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;
127     zint bucket_no;
128     int hash_bytes;
129     struct CFile_ph_bucket *p;
130 #if CF_OPTIMIZE_COMMIT
131     struct map_cache *m_p;
132 #endif
133
134 #if CF_OPTIMIZE_COMMIT
135     m_p = map_cache_init (cf);
136 #endif
137
138     p = (struct CFile_ph_bucket *) xmalloc (sizeof(*p));
139     hash_bytes = cf->head.hash_size * sizeof(zint);
140     bucket_no = cf->head.first_bucket;
141     for (; bucket_no < cf->head.next_bucket; bucket_no++)
142     {
143         if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
144         {
145             yaz_log (YLOG_FATAL, "read commit hash");
146             exit (1);
147         }
148         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
149         {
150 #if CF_OPTIMIZE_COMMIT
151             map_cache_add (m_p, p->vno[i], p->no[i]);
152 #else
153             if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
154             {
155                 yaz_log (YLOG_FATAL, "read commit block");
156                 exit (1);
157             }
158             mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
159 #endif
160         }
161     }
162 #if CF_OPTIMIZE_COMMIT
163     map_cache_del (m_p);
164 #endif
165     xfree (p);
166 }
167
168 static void cf_commit_flat (CFile cf)
169 {
170     zint *fp;
171     zint hno;
172     int i;
173     zint vno = 0;
174
175 #if CF_OPTIMIZE_COMMIT
176     struct map_cache *m_p;
177 #endif
178
179
180 #if CF_OPTIMIZE_COMMIT
181     m_p = map_cache_init (cf);
182 #endif
183     fp = (zint *) xmalloc (HASH_BSIZE);
184     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
185     {
186         for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
187             fp[i] = 0;
188         if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
189             hno != cf->head.flat_bucket-1)
190         {
191             yaz_log (YLOG_FATAL, "read index block hno=" ZINT_FORMAT
192                   " (" ZINT_FORMAT "-" ZINT_FORMAT ") commit",
193                   hno, cf->head.next_bucket, cf->head.flat_bucket-1);
194         }
195         for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
196         {
197             if (fp[i])
198             {
199 #if CF_OPTIMIZE_COMMIT
200                 map_cache_add (m_p, fp[i], vno);
201 #else
202                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
203                 {
204                     yaz_log (YLOG_FATAL, "read data block hno=" ZINT_FORMAT " (" ZINT_FORMAT "-" ZINT_FORMAT ") "
205                                      "i=%d commit block at " ZINT_FORMAT " (->" ZINT_FORMAT")",
206                           hno, cf->head.next_bucket, cf->head.flat_bucket-1,
207                           i, fp[i], vno);
208                     exit (1);
209                 }
210                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
211
212 #endif
213             }
214             vno++;
215         }
216     }
217 #if CF_OPTIMIZE_COMMIT
218     map_cache_del (m_p);
219 #endif
220     xfree (fp);
221 }
222
223 void cf_commit (CFile cf)
224 {
225
226     if (cf->bucket_in_memory)
227     {
228         yaz_log (YLOG_FATAL, "Cannot commit potential dirty cache");
229         exit (1);
230     }
231     if (cf->head.state == 1)
232         cf_commit_hash (cf);
233     else if (cf->head.state == 2)
234         cf_commit_flat (cf);
235 }
236
237 /*
238  * Local variables:
239  * c-basic-offset: 4
240  * indent-tabs-mode: nil
241  * End:
242  * vim: shiftwidth=4 tabstop=8 expandtab
243  */
244