Using the new ylog.h everywhere, and fixing what that breaks!
[idzebra-moved-to-github.git] / bfile / commit.c
1 /* $Id: commit.c,v 1.21 2004-11-19 10:26:53 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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         yaz_log (YLOG_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             yaz_log (YLOG_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;
141     zint bucket_no;
142     int hash_bytes;
143     struct CFile_ph_bucket *p;
144 #if CF_OPTIMIZE_COMMIT
145     struct map_cache *m_p;
146 #endif
147
148 #if CF_OPTIMIZE_COMMIT
149     m_p = map_cache_init (cf);
150 #endif
151
152     p = (struct CFile_ph_bucket *) xmalloc (sizeof(*p));
153     hash_bytes = cf->head.hash_size * sizeof(zint);
154     bucket_no = cf->head.first_bucket;
155     for (; bucket_no < cf->head.next_bucket; bucket_no++)
156     {
157         if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
158         {
159             yaz_log (YLOG_FATAL, "read commit hash");
160             exit (1);
161         }
162         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
163         {
164 #if CF_OPTIMIZE_COMMIT
165             map_cache_add (m_p, p->vno[i], p->no[i]);
166 #else
167             if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
168             {
169                 yaz_log (YLOG_FATAL, "read commit block");
170                 exit (1);
171             }
172             mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
173 #endif
174         }
175     }
176 #if CF_OPTIMIZE_COMMIT
177     map_cache_del (m_p);
178 #endif
179     xfree (p);
180 }
181
182 static void cf_commit_flat (CFile cf)
183 {
184     zint *fp;
185     zint hno;
186     int i;
187     zint vno = 0;
188
189 #if CF_OPTIMIZE_COMMIT
190     struct map_cache *m_p;
191 #endif
192
193
194 #if CF_OPTIMIZE_COMMIT
195     m_p = map_cache_init (cf);
196 #endif
197     fp = (zint *) xmalloc (HASH_BSIZE);
198     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
199     {
200         for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
201             fp[i] = 0;
202         if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
203             hno != cf->head.flat_bucket-1)
204         {
205             yaz_log (YLOG_FATAL, "read index block hno=" ZINT_FORMAT
206                   " (" ZINT_FORMAT "-" ZINT_FORMAT ") commit",
207                   hno, cf->head.next_bucket, cf->head.flat_bucket-1);
208         }
209         for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
210         {
211             if (fp[i])
212             {
213 #if CF_OPTIMIZE_COMMIT
214                 map_cache_add (m_p, fp[i], vno);
215 #else
216                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
217                 {
218                     yaz_log (YLOG_FATAL, "read data block hno=" ZINT_FORMAT " (" ZINT_FORMAT "-" ZINT_FORMAT ") "
219                                      "i=%d commit block at " ZINT_FORMAT " (->" ZINT_FORMAT")",
220                           hno, cf->head.next_bucket, cf->head.flat_bucket-1,
221                           i, fp[i], vno);
222                     exit (1);
223                 }
224                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
225
226 #endif
227             }
228             vno++;
229         }
230     }
231 #if CF_OPTIMIZE_COMMIT
232     map_cache_del (m_p);
233 #endif
234     xfree (fp);
235 }
236
237 void cf_commit (CFile cf)
238 {
239
240     if (cf->bucket_in_memory)
241     {
242         yaz_log (YLOG_FATAL, "Cannot commit potential dirty cache");
243         exit (1);
244     }
245     if (cf->head.state == 1)
246         cf_commit_hash (cf);
247     else if (cf->head.state == 2)
248         cf_commit_flat (cf);
249 }
250