2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1994-09-26 16:07:57 quinn
8 * Most of the functionality in place.
13 * This module handles the representation of tables in the bfiles.
21 static int is_freestore_alloc(ISAM is, int type)
25 if (is->types[type].freelist >= 0)
27 tmp = is->types[type].freelist;
28 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
29 &is->types[type].freelist) <=0)
31 log(LOG_FATAL, "Failed to allocate block");
36 tmp = is->types[type].top++;
41 static void is_freestore_free(ISAM is, int type, int block)
45 tmp = is->types[type].freelist;
46 is->types[type].freelist = block;
47 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
49 log(LOG_FATAL, "Failed to deallocate block.");
54 /* this code must be modified to handle an index */
55 int is_p_read_partial(is_mtable *tab, is_mblock *block)
60 assert(block->state == IS_MBSTATE_UNREAD);
61 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
62 toread = tab->is->types[tab->pos_type].blocksize;
63 if (toread > is_mbuf_size[buf->type])
65 toread = is_mbuf_size[buf->type];
66 block->state = IS_MBSTATE_PARTIAL;
69 block->state = IS_MBSTATE_CLEAN;
70 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
73 log(LOG_FATAL, "bfread failed.");
76 /* extract header info */
78 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
79 buf->offset += sizeof(block->num_records);
80 memcpy(&block->nextpos, buf->data + buf->offset,
81 sizeof(block->nextpos));
82 buf->offset += sizeof(block->nextpos);
83 if (block == tab->data) /* first block */
85 memcpy(&tab->num_records, buf->data + buf->offset,
86 sizeof(tab->num_records));
87 buf->offset +=sizeof(tab->num_records);
89 buf->num = (toread - buf->offset) / is_keysize(tab->is);
90 if (buf->num >= block->num_records)
92 buf->num = block->num_records;
93 block->state = IS_MBSTATE_CLEAN;
96 block->bread = buf->num * is_keysize(tab->is);
100 int is_p_read_full(is_mtable *tab, is_mblock *block)
105 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
107 log(LOG_FATAL, "partial read failed.");
110 if (block->state == IS_MBSTATE_PARTIAL)
113 dread = block->data->num;
114 while (dread < block->num_records)
116 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
119 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
120 if (toread > block->num_records - dread)
121 toread = block->num_records - dread;
123 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
124 is_keysize(tab->is), buf->data) < 0)
126 log(LOG_FATAL, "bfread failed.");
132 block->bread += toread * is_keysize(tab->is);
139 * write dirty blocks to bfile.
140 * Allocate blocks as necessary.
142 void is_p_sync(is_mtable *tab)
147 isam_blocktype *type;
149 type = &tab->is->types[tab->pos_type];
150 for (p = tab->data; p; p = p->next)
152 /* make sure that blocks are allocated. */
154 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
157 if (p->next->diskpos < 0)
158 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
161 p->nextpos = p->next->diskpos;
164 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
165 sum += sizeof(p->num_records);
166 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
167 sum += sizeof(p->nextpos);
168 if (p == tab->data) /* first block */
170 memcpy(type->dbuf + sum, &tab->num_records,
171 sizeof(tab->num_records));
172 sum += sizeof(tab->num_records);
174 for (b = p->data; b; b = b->next)
176 memcpy(type->dbuf + sum, b->data + b->offset, v = b->num *
177 is_keysize(tab->is));
179 assert(sum <= type->blocksize);
181 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
183 log(LOG_FATAL, "Failed to write block.");
190 * Free all disk blocks associated with table.
192 void is_p_unmap(is_mtable *tab)
196 for (p = tab->data; p; p = p->next)
199 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
204 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
206 is_mbuf *p = 0, **pp = &p, *new;
209 while (*mb && toget >= (*mb)->num)
217 if (toget > 0 && *mb)
219 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
220 new->next = (*mb)->next;
222 new->data = (*mb)->data;
224 new->offset = (*mb)->offset + toget * keysize;
225 new->num = (*mb)->num - toget;
237 * Split up individual blocks which have grown too large.
238 * is_p_align and is_p_remap are alternative functions which trade off
239 * speed in updating versus optimum usage of disk blocks.
241 void is_p_align(is_mtable *tab)
243 is_mblock *mblock, *new;
244 is_mbuf *mbufs, *mbp;
245 int blocks, recsblock;
247 log(LOG_DEBUG, "Realigning table.");
248 for (mblock = tab->data; mblock; mblock = mblock->next)
250 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
251 (mblock == tab->data ?
252 tab->is->types[tab->pos_type].max_keys_block0 :
253 tab->is->types[tab->pos_type].max_keys_block))
255 blocks = tab->num_records /
256 tab->is->types[tab->pos_type].nice_keys_block;
257 if (tab->num_records %
258 tab->is->types[tab->pos_type].nice_keys_block)
260 recsblock = tab->num_records / blocks;
263 mbufs = mblock->data;
264 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
265 is_keysize(tab->is))))
267 new = xmalloc_mblock();
269 new->state = IS_MBSTATE_DIRTY;
270 new->next = mblock->next;
273 mblock->num_records = recsblock;
274 mblock = mblock->next;
281 * Reorganize data in blocks for minimum block usage and quick access.
282 * Free surplus blocks.
283 * is_p_align and is_p_remap are alternative functions which trade off
284 * speed in updating versus optimum usage of disk blocks.
286 void is_p_remap(is_mtable *tab)
288 is_mbuf *mbufs, **bufpp, *mbp;
289 is_mblock *blockp, **blockpp;
290 int recsblock, blocks;
292 log(LOG_DEBUG, "Remapping table.");
293 /* collect all data */
295 for (blockp = tab->data; blockp; blockp = blockp->next)
297 *bufpp = blockp->data;
299 bufpp = &(*bufpp)->next;
302 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
303 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
305 recsblock = tab->num_records / blocks;
308 blockpp = &tab->data;
309 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))))
313 *blockpp = xmalloc_mblock();
314 (*blockpp)->diskpos = -1;
316 (*blockpp)->data = mbp;
317 (*blockpp)->num_records = recsblock;
318 (*blockpp)->state = IS_MBSTATE_DIRTY;
319 blockpp = &(*blockpp)->next;