Move/refactoring of public interfaces..
[idzebra-moved-to-github.git] / bfile / commit.c
1 /* $Id: commit.c,v 1.22 2004-12-08 12:23:08 adam 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 #include <assert.h>
25 #include <stdlib.h>
26
27 #include <zebrautl.h>
28 #include "mfile.h"
29 #include "cfile.h"
30
31 #define CF_OPTIMIZE_COMMIT 0
32
33 void cf_unlink (CFile cf)
34 {
35     if (cf->bucket_in_memory)
36     {
37         yaz_log (YLOG_FATAL, "Cannot unlink potential dirty cache");
38         exit (1);
39     }
40     cf->head.state = 0;
41     cf->dirty = 1;
42     mf_unlink (cf->block_mf);
43     mf_unlink (cf->hash_mf);
44 }
45
46
47 #if CF_OPTIMIZE_COMMIT
48 struct map_cache_entity {
49     int from;
50     int to;
51 };
52
53 struct map_cache {
54     int max;
55     int no;
56
57     struct map_cache_entity *map;
58     char *buf;
59     CFile cf;
60 };
61
62 static struct map_cache *map_cache_init (CFile cf)
63 {
64     int mem_max = 2000000;
65     struct map_cache *m_p;
66
67     m_p = xmalloc (sizeof(*m_p));
68     m_p->cf = cf;
69     m_p->max = mem_max / cf->head.block_size;
70     m_p->buf = xmalloc (mem_max);
71     m_p->no = 0;
72     m_p->map = xmalloc (sizeof(*m_p->map) * m_p->max);
73     return m_p;
74 }
75
76 static int map_cache_cmp_from (const void *p1, const void *p2)
77 {
78     return ((struct map_cache_entity*) p1)->from -
79         ((struct map_cache_entity*) p2)->from;
80 }
81
82 static int map_cache_cmp_to (const void *p1, const void *p2)
83 {
84     return ((struct map_cache_entity*) p1)->to -
85         ((struct map_cache_entity*) p2)->to;
86 }
87
88 static void map_cache_flush (struct map_cache *m_p)
89 {
90     int i;
91
92     qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_from);
93     assert (m_p->no < 2 || m_p->map[0].from < m_p->map[1].from);
94     for (i = 0; i<m_p->no; i++)
95     {
96         if (!mf_read (m_p->cf->block_mf, m_p->map[i].from, 0, 0,
97                       m_p->buf + i * m_p->cf->head.block_size))
98         {
99             yaz_log (YLOG_FATAL, "read commit block at position %d",
100                   m_p->map[i].from);
101             exit (1);
102         }
103         m_p->map[i].from = i;
104     }
105     qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_to);
106     assert (m_p->no < 2 || m_p->map[0].to < m_p->map[1].to);
107     for (i = 0; i<m_p->no; i++)
108     {
109         mf_write (m_p->cf->rmf, m_p->map[i].to, 0, 0,
110                   m_p->buf + m_p->map[i].from * m_p->cf->head.block_size);
111     }    
112     m_p->no = 0;
113 }
114
115 static void map_cache_del (struct map_cache *m_p)
116 {
117     map_cache_flush (m_p);
118     xfree (m_p->map);
119     xfree (m_p->buf);
120     xfree (m_p);
121 }
122
123 static void map_cache_add (struct map_cache *m_p, int from, int to)
124 {
125     int i = m_p->no;
126
127     m_p->map[i].from = from;
128     m_p->map[i].to = to;
129     m_p->no = ++i;
130     if (i == m_p->max)
131         map_cache_flush (m_p);
132 }
133
134 /* CF_OPTIMIZE_COMMIT */
135 #endif
136
137 static void cf_commit_hash (CFile cf)
138
139     int i;
140     zint 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(zint);
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             yaz_log (YLOG_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                 yaz_log (YLOG_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     zint *fp;
184     zint hno;
185     int i;
186     zint vno = 0;
187
188 #if CF_OPTIMIZE_COMMIT
189     struct map_cache *m_p;
190 #endif
191
192
193 #if CF_OPTIMIZE_COMMIT
194     m_p = map_cache_init (cf);
195 #endif
196     fp = (zint *) xmalloc (HASH_BSIZE);
197     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
198     {
199         for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
200             fp[i] = 0;
201         if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
202             hno != cf->head.flat_bucket-1)
203         {
204             yaz_log (YLOG_FATAL, "read index block hno=" ZINT_FORMAT
205                   " (" ZINT_FORMAT "-" ZINT_FORMAT ") commit",
206                   hno, cf->head.next_bucket, cf->head.flat_bucket-1);
207         }
208         for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
209         {
210             if (fp[i])
211             {
212 #if CF_OPTIMIZE_COMMIT
213                 map_cache_add (m_p, fp[i], vno);
214 #else
215                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
216                 {
217                     yaz_log (YLOG_FATAL, "read data block hno=" ZINT_FORMAT " (" ZINT_FORMAT "-" ZINT_FORMAT ") "
218                                      "i=%d commit block at " ZINT_FORMAT " (->" ZINT_FORMAT")",
219                           hno, cf->head.next_bucket, cf->head.flat_bucket-1,
220                           i, fp[i], vno);
221                     exit (1);
222                 }
223                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
224
225 #endif
226             }
227             vno++;
228         }
229     }
230 #if CF_OPTIMIZE_COMMIT
231     map_cache_del (m_p);
232 #endif
233     xfree (fp);
234 }
235
236 void cf_commit (CFile cf)
237 {
238
239     if (cf->bucket_in_memory)
240     {
241         yaz_log (YLOG_FATAL, "Cannot commit potential dirty cache");
242         exit (1);
243     }
244     if (cf->head.state == 1)
245         cf_commit_hash (cf);
246     else if (cf->head.state == 2)
247         cf_commit_flat (cf);
248 }
249