2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1994-09-26 17:11:31 quinn
10 * Revision 1.2 1994/09/26 17:06:36 quinn
13 * Revision 1.1 1994/09/26 16:07:57 quinn
14 * Most of the functionality in place.
19 * This module handles the representation of tables in the bfiles.
27 static int is_freestore_alloc(ISAM is, int type)
31 if (is->types[type].freelist >= 0)
33 tmp = is->types[type].freelist;
34 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
35 &is->types[type].freelist) <=0)
37 log(LOG_FATAL, "Failed to allocate block");
42 tmp = is->types[type].top++;
47 static void is_freestore_free(ISAM is, int type, int block)
51 tmp = is->types[type].freelist;
52 is->types[type].freelist = block;
53 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
55 log(LOG_FATAL, "Failed to deallocate block.");
60 /* this code must be modified to handle an index */
61 int is_p_read_partial(is_mtable *tab, is_mblock *block)
66 assert(block->state == IS_MBSTATE_UNREAD);
67 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
68 toread = tab->is->types[tab->pos_type].blocksize;
69 if (toread > is_mbuf_size[buf->type])
71 toread = is_mbuf_size[buf->type];
72 block->state = IS_MBSTATE_PARTIAL;
75 block->state = IS_MBSTATE_CLEAN;
76 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
79 log(LOG_FATAL, "bfread failed.");
82 /* extract header info */
84 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
85 buf->offset += sizeof(block->num_records);
86 memcpy(&block->nextpos, buf->data + buf->offset,
87 sizeof(block->nextpos));
88 buf->offset += sizeof(block->nextpos);
89 if (block == tab->data) /* first block */
91 memcpy(&tab->num_records, buf->data + buf->offset,
92 sizeof(tab->num_records));
93 buf->offset +=sizeof(tab->num_records);
95 buf->num = (toread - buf->offset) / is_keysize(tab->is);
96 if (buf->num >= block->num_records)
98 buf->num = block->num_records;
99 block->state = IS_MBSTATE_CLEAN;
102 block->bread = buf->num * is_keysize(tab->is);
106 int is_p_read_full(is_mtable *tab, is_mblock *block)
111 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
113 log(LOG_FATAL, "partial read failed.");
116 if (block->state == IS_MBSTATE_PARTIAL)
119 dread = block->data->num;
120 while (dread < block->num_records)
122 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
125 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
126 if (toread > block->num_records - dread)
127 toread = block->num_records - dread;
129 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
130 is_keysize(tab->is), buf->data) < 0)
132 log(LOG_FATAL, "bfread failed.");
138 block->bread += toread * is_keysize(tab->is);
145 * write dirty blocks to bfile.
146 * Allocate blocks as necessary.
148 void is_p_sync(is_mtable *tab)
153 isam_blocktype *type;
155 type = &tab->is->types[tab->pos_type];
156 for (p = tab->data; p; p = p->next)
158 /* make sure that blocks are allocated. */
160 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
163 if (p->next->diskpos < 0)
164 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
167 p->nextpos = p->next->diskpos;
170 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
171 sum += sizeof(p->num_records);
172 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
173 sum += sizeof(p->nextpos);
174 if (p == tab->data) /* first block */
176 memcpy(type->dbuf + sum, &tab->num_records,
177 sizeof(tab->num_records));
178 sum += sizeof(tab->num_records);
180 for (b = p->data; b; b = b->next)
182 memcpy(type->dbuf + sum, b->data + b->offset, v = b->num *
183 is_keysize(tab->is));
185 assert(sum <= type->blocksize);
187 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
189 log(LOG_FATAL, "Failed to write block.");
196 * Free all disk blocks associated with table.
198 void is_p_unmap(is_mtable *tab)
202 for (p = tab->data; p; p = p->next)
205 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
210 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
212 is_mbuf *p = 0, **pp = &p, *new;
215 while (*mb && toget >= (*mb)->num)
223 if (toget > 0 && *mb)
225 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
226 new->next = (*mb)->next;
228 new->data = (*mb)->data;
230 new->offset = (*mb)->offset + toget * keysize;
231 new->num = (*mb)->num - toget;
243 * Split up individual blocks which have grown too large.
244 * is_p_align and is_p_remap are alternative functions which trade off
245 * speed in updating versus optimum usage of disk blocks.
247 void is_p_align(is_mtable *tab)
249 is_mblock *mblock, *new;
250 is_mbuf *mbufs, *mbp;
251 int blocks, recsblock;
253 log(LOG_DEBUG, "Realigning table.");
254 for (mblock = tab->data; mblock; mblock = mblock->next)
256 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
257 (mblock == tab->data ?
258 tab->is->types[tab->pos_type].max_keys_block0 :
259 tab->is->types[tab->pos_type].max_keys_block))
261 blocks = tab->num_records /
262 tab->is->types[tab->pos_type].nice_keys_block;
263 if (tab->num_records %
264 tab->is->types[tab->pos_type].nice_keys_block)
266 recsblock = tab->num_records / blocks;
269 mbufs = mblock->data;
270 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
271 is_keysize(tab->is))))
273 new = xmalloc_mblock();
275 new->state = IS_MBSTATE_DIRTY;
276 new->next = mblock->next;
279 mblock->num_records = recsblock;
280 mblock = mblock->next;
287 * Reorganize data in blocks for minimum block usage and quick access.
288 * Free surplus blocks.
289 * is_p_align and is_p_remap are alternative functions which trade off
290 * speed in updating versus optimum usage of disk blocks.
292 void is_p_remap(is_mtable *tab)
294 is_mbuf *mbufs, **bufpp, *mbp;
295 is_mblock *blockp, **blockpp;
296 int recsblock, blocks;
298 log(LOG_DEBUG, "Remapping table.");
299 /* collect all data */
301 for (blockp = tab->data; blockp; blockp = blockp->next)
303 *bufpp = blockp->data;
305 bufpp = &(*bufpp)->next;
308 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
309 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
311 recsblock = tab->num_records / blocks;
314 blockpp = &tab->data;
315 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))))
319 *blockpp = xmalloc_mblock();
320 (*blockpp)->diskpos = -1;
322 (*blockpp)->data = mbp;
323 (*blockpp)->num_records = recsblock;
324 (*blockpp)->state = IS_MBSTATE_DIRTY;
325 blockpp = &(*blockpp)->next;