2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1996-04-23 12:36:41 adam
8 * Started work on more efficient commit operation.
10 * Revision 1.10 1996/04/18 16:02:56 adam
11 * Changed logging a bit.
12 * Removed warning message when commiting flat shadow files.
14 * Revision 1.9 1996/04/12 07:01:57 adam
15 * Yet another bug fix (next_block was initialized to 0; now set to 1).
17 * Revision 1.8 1996/02/07 14:03:49 adam
18 * Work on flat indexed shadow files.
20 * Revision 1.7 1996/02/07 10:08:46 adam
21 * Work on flat shadow (not finished yet).
23 * Revision 1.6 1995/12/15 12:36:53 adam
24 * Moved hash file information to union.
25 * Renamed commit files.
27 * Revision 1.5 1995/12/12 15:57:55 adam
28 * Implemented mf_unlink. cf_unlink uses mf_unlink.
30 * Revision 1.4 1995/12/11 09:03:55 adam
31 * New function: cf_unlink.
32 * New member of commit file head: state (0) deleted, (1) hash file.
34 * Revision 1.3 1995/12/01 16:24:29 adam
35 * Commit files use separate meta file area.
37 * Revision 1.2 1995/12/01 11:37:24 adam
38 * Cached/commit files implemented as meta-files.
40 * Revision 1.1 1995/11/30 08:33:13 adam
41 * Started work on commit facility.
52 void cf_unlink (CFile cf)
54 if (cf->bucket_in_memory)
56 logf (LOG_FATAL, "Cannot unlink potential dirty cache");
61 mf_unlink (cf->block_mf);
62 mf_unlink (cf->hash_mf);
66 struct map_cache_entity {
75 struct map_cache_entity *map;
80 static struct map_cache *map_cache_init (CFile cf)
82 int mem_max = 2000000;
83 struct map_cache *m_p;
85 m_p = xmalloc (sizeof(*m_p));
87 m_p->max = mem_max / cf->head.block_size;
88 m_p->buf = xmalloc (mem_max);
90 m_p->map = xmalloc (sizeof(*m_p->map) * m_p->max);
94 static void map_cache_del (struct map_cache *m_p)
101 static int map_cache_cmp_from (const void *p1, const void *p2)
103 return ((struct map_cache_entity*) p1)->from -
104 ((struct map_cache_entity*) p2)->from;
107 static int map_cache_cmp_to (const void *p1, const void *p2)
109 return ((struct map_cache_entity*) p1)->to -
110 ((struct map_cache_entity*) p2)->to;
113 static void map_cache_flush (struct map_cache *m_p)
117 qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_from);
118 for (i = 0; i<m_p->no; i++)
120 if (!mf_read (m_p->cf->block_mf, m_p->map[i].from, 0, 0,
121 m_p->buf + i * m_p->cf->head.block_size))
123 logf (LOG_FATAL, "read commit block");
126 m_p->map[i].from = i;
128 qsort (m_p->map, m_p->no, sizeof(*m_p->map), map_cache_cmp_to);
129 for (i = 0; i<m_p->no; i++)
131 mf_write (m_p->cf->rmf, m_p->map[i].to, 0, 0,
132 m_p->buf + m_p->map[i].from * m_p->cf->head.block_size);
136 static void map_cache_add (struct map_cache *m_p, int from, int to)
140 m_p->map[i].from = from;
144 map_cache_flush (m_p);
147 static void cf_commit_hash (CFile cf)
151 struct CFile_ph_bucket *p;
153 p = xmalloc (sizeof(*p));
154 hash_bytes = cf->head.hash_size * sizeof(int);
155 bucket_no = cf->head.first_bucket;
156 for (; bucket_no < cf->head.next_bucket; bucket_no++)
158 if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
160 logf (LOG_FATAL, "read commit hash");
163 for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
165 if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
167 logf (LOG_FATAL, "read commit block");
170 mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
176 static void cf_commit_flat (CFile cf)
182 fp = xmalloc (HASH_BSIZE);
183 for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
185 if (hno == cf->head.flat_bucket-1)
187 for (i = 0; i < (HASH_BSIZE/sizeof(int)); i++)
190 if (!mf_read (cf->hash_mf, hno, 0, 0, fp) &&
191 hno != cf->head.flat_bucket-1)
193 logf (LOG_FATAL, "read index block hno=%d (%d-%d) commit",
194 hno, cf->head.next_bucket, cf->head.flat_bucket-1);
196 for (i = 0; i < (HASH_BSIZE/sizeof(int)); i++)
200 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
202 logf (LOG_FATAL, "read data block hno=%d (%d-%d) "
203 "i=%d commit block at %d (->%d)",
204 hno, cf->head.next_bucket, cf->head.flat_bucket-1,
208 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
216 void cf_commit (CFile cf)
219 if (cf->bucket_in_memory)
221 logf (LOG_FATAL, "Cannot commit potential dirty cache");
224 if (cf->head.state == 1)
226 else if (cf->head.state == 2)