1 /* $Id: commit.c,v 1.32 2007-01-15 15:10:14 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
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
27 #include <idzebra/util.h>
28 #include <yaz/xmalloc.h>
32 #define CF_OPTIMIZE_COMMIT 0
34 static int log_level = 0;
36 #if CF_OPTIMIZE_COMMIT
37 struct map_cache_entity {
46 struct map_cache_entity *map;
51 static struct map_cache *map_cache_init (CFile cf)
53 int mem_max = 2000000;
54 struct map_cache *m_p;
56 m_p = xmalloc (sizeof(*m_p));
58 m_p->max = mem_max / cf->head.block_size;
59 m_p->buf = xmalloc (mem_max);
61 m_p->map = xmalloc (sizeof(*m_p->map) * m_p->max);
65 static int map_cache_cmp_from (const void *p1, const void *p2)
67 return ((struct map_cache_entity*) p1)->from -
68 ((struct map_cache_entity*) p2)->from;
71 static int map_cache_cmp_to(const void *p1, const void *p2)
73 return ((struct map_cache_entity*) p1)->to -
74 ((struct map_cache_entity*) p2)->to;
77 static int map_cache_flush(struct map_cache *m_p)
81 qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_from);
82 assert (m_p->no < 2 || m_p->map[0].from < m_p->map[1].from);
83 for (i = 0; i<m_p->no; i++)
85 if (mf_read(m_p->cf->block_mf, m_p->map[i].from, 0, 0,
86 m_p->buf + i * m_p->cf->head.block_size) != 1)
88 yaz_log (YLOG_FATAL, "read commit block at position %d",
94 qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_to);
95 assert (m_p->no < 2 || m_p->map[0].to < m_p->map[1].to);
96 for (i = 0; i<m_p->no; i++)
98 if (mf_write(m_p->cf->rmf, m_p->map[i].to, 0, 0,
99 m_p->buf + m_p->map[i].from * m_p->cf->head.block_size))
106 static int map_cache_del(struct map_cache *m_p)
108 int r = map_cache_flush(m_p);
115 static int map_cache_add(struct map_cache *m_p, int from, int to)
119 m_p->map[i].from = from;
123 return map_cache_flush(m_p);
127 /* CF_OPTIMIZE_COMMIT */
130 static int cf_commit_hash (CFile cf)
136 struct CFile_ph_bucket *p;
137 #if CF_OPTIMIZE_COMMIT
138 struct map_cache *m_p;
141 #if CF_OPTIMIZE_COMMIT
142 m_p = map_cache_init (cf);
145 p = (struct CFile_ph_bucket *) xmalloc (sizeof(*p));
146 hash_bytes = cf->head.hash_size * sizeof(zint);
147 bucket_no = cf->head.first_bucket;
148 for (; bucket_no < cf->head.next_bucket; bucket_no++)
150 if (mf_read (cf->hash_mf, bucket_no, 0, 0, p) != 1)
152 yaz_log (YLOG_FATAL, "read commit hash");
156 for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
158 #if CF_OPTIMIZE_COMMIT
159 if (map_cache_add(m_p, p->vno[i], p->no[i]))
165 if (mf_read(cf->block_mf, p->vno[i], 0, 0, cf->iobuf) != 1)
167 yaz_log (YLOG_FATAL, "read commit block");
171 if (mf_write(cf->rmf, p->no[i], 0, 0, cf->iobuf))
173 yaz_log (YLOG_FATAL, "write commit block");
181 #if CF_OPTIMIZE_COMMIT
182 if (map_cache_del(m_p))
189 static int cf_commit_flat(CFile cf)
197 #if CF_OPTIMIZE_COMMIT
198 struct map_cache *m_p;
202 #if CF_OPTIMIZE_COMMIT
203 m_p = map_cache_init (cf);
205 fp = (zint *) xmalloc (HASH_BSIZE);
206 for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
208 for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
210 if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
211 hno != cf->head.flat_bucket-1)
213 yaz_log (YLOG_FATAL, "read index block hno=" ZINT_FORMAT
214 " (" ZINT_FORMAT "-" ZINT_FORMAT ") commit",
215 hno, cf->head.next_bucket, cf->head.flat_bucket-1);
219 for (i = 0; i < (int) (HASH_BSIZE/sizeof(zint)); i++)
223 #if CF_OPTIMIZE_COMMIT
224 if (map_cache_add(m_p, fp[i], vno))
230 if (mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf) != 1)
232 yaz_log (YLOG_FATAL, "read data block hno=" ZINT_FORMAT " (" ZINT_FORMAT "-" ZINT_FORMAT ") "
233 "i=%d commit block at " ZINT_FORMAT " (->" ZINT_FORMAT")",
234 hno, cf->head.next_bucket, cf->head.flat_bucket-1,
239 if (mf_write(cf->rmf, vno, 0, 0, cf->iobuf))
250 #if CF_OPTIMIZE_COMMIT
251 if (map_cache_del(m_p))
254 yaz_log(log_level, "cf_commit_flat r=%d", r);
259 int cf_commit(CFile cf)
261 if (cf->bucket_in_memory)
263 yaz_log(YLOG_FATAL, "cf_commit: dirty cache");
266 yaz_log(log_level, "cf_commit: state=%d", cf->head.state);
267 if (cf->head.state == 1)
268 return cf_commit_hash(cf);
269 else if (cf->head.state == 2)
270 return cf_commit_flat(cf);
273 yaz_log(YLOG_FATAL, "cf_commit: bad state=%d", cf->head.state);
281 * indent-tabs-mode: nil
283 * vim: shiftwidth=4 tabstop=8 expandtab