1 /* $Id: commit.c,v 1.16.2.3 2006-12-05 21:14:38 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
32 #define CF_OPTIMIZE_COMMIT 0
34 #if CF_OPTIMIZE_COMMIT
35 struct map_cache_entity {
44 struct map_cache_entity *map;
49 static struct map_cache *map_cache_init (CFile cf)
51 int mem_max = 2000000;
52 struct map_cache *m_p;
54 m_p = xmalloc (sizeof(*m_p));
56 m_p->max = mem_max / cf->head.block_size;
57 m_p->buf = xmalloc (mem_max);
59 m_p->map = xmalloc (sizeof(*m_p->map) * m_p->max);
63 static int map_cache_cmp_from (const void *p1, const void *p2)
65 return ((struct map_cache_entity*) p1)->from -
66 ((struct map_cache_entity*) p2)->from;
69 static int map_cache_cmp_to (const void *p1, const void *p2)
71 return ((struct map_cache_entity*) p1)->to -
72 ((struct map_cache_entity*) p2)->to;
75 static void map_cache_flush (struct map_cache *m_p)
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++)
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))
86 yaz_log(YLOG_FATAL, "read commit block at position %d",
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++)
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);
102 static void map_cache_del (struct map_cache *m_p)
104 map_cache_flush (m_p);
110 static void map_cache_add (struct map_cache *m_p, int from, int to)
114 m_p->map[i].from = from;
118 map_cache_flush (m_p);
121 /* CF_OPTIMIZE_COMMIT */
124 static void cf_commit_hash (CFile cf)
128 struct CFile_ph_bucket *p;
129 #if CF_OPTIMIZE_COMMIT
130 struct map_cache *m_p;
133 #if CF_OPTIMIZE_COMMIT
134 m_p = map_cache_init (cf);
137 p = (struct CFile_ph_bucket *) xmalloc (sizeof(*p));
138 hash_bytes = cf->head.hash_size * sizeof(int);
139 bucket_no = cf->head.first_bucket;
140 for (; bucket_no < cf->head.next_bucket; bucket_no++)
142 if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
144 yaz_log(YLOG_FATAL, "read commit hash");
147 for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
149 #if CF_OPTIMIZE_COMMIT
150 map_cache_add (m_p, p->vno[i], p->no[i]);
152 if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
154 yaz_log(YLOG_FATAL, "read commit block");
157 mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
161 #if CF_OPTIMIZE_COMMIT
167 static void cf_commit_flat (CFile cf)
173 #if CF_OPTIMIZE_COMMIT
174 struct map_cache *m_p;
178 #if CF_OPTIMIZE_COMMIT
179 m_p = map_cache_init (cf);
181 fp = (int *) xmalloc (HASH_BSIZE);
182 for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
184 for (i = 0; i < (int) (HASH_BSIZE/sizeof(int)); i++)
186 if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
187 hno != cf->head.flat_bucket-1)
189 yaz_log(YLOG_FATAL, "read index block hno=%d (%d-%d) commit",
190 hno, cf->head.next_bucket, cf->head.flat_bucket-1);
192 for (i = 0; i < (int) (HASH_BSIZE/sizeof(int)); i++)
196 #if CF_OPTIMIZE_COMMIT
197 map_cache_add (m_p, fp[i], vno);
199 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
201 yaz_log(YLOG_FATAL, "read data block hno=%d (%d-%d) "
202 "i=%d commit block at %d (->%d)",
203 hno, cf->head.next_bucket, cf->head.flat_bucket-1,
207 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
214 #if CF_OPTIMIZE_COMMIT
220 void cf_commit (CFile cf)
223 if (cf->bucket_in_memory)
225 yaz_log(YLOG_FATAL, "Cannot commit potential dirty cache");
228 if (cf->head.state == 1)
230 else if (cf->head.state == 2)