2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1994-09-28 11:29:33 quinn
10 * Revision 1.4 1994/09/27 20:03:53 quinn
11 * Seems relatively bug-free.
13 * Revision 1.3 1994/09/26 17:11:31 quinn
16 * Revision 1.2 1994/09/26 17:06:36 quinn
19 * Revision 1.1 1994/09/26 16:07:57 quinn
20 * Most of the functionality in place.
25 * This module handles the representation of tables in the bfiles.
32 static int is_freestore_alloc(ISAM is, int type)
36 if (is->types[type].freelist >= 0)
38 tmp = is->types[type].freelist;
39 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
40 &is->types[type].freelist) <=0)
42 log(LOG_FATAL, "Failed to allocate block");
47 tmp = is->types[type].top++;
49 log(LOG_DEBUG, "Allocating block #%d", tmp);
53 static void is_freestore_free(ISAM is, int type, int block)
57 log(LOG_DEBUG, "Releasing block #%d", block);
58 tmp = is->types[type].freelist;
59 is->types[type].freelist = block;
60 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
62 log(LOG_FATAL, "Failed to deallocate block.");
67 /* this code must be modified to handle an index */
68 int is_p_read_partial(is_mtable *tab, is_mblock *block)
73 assert(block->state == IS_MBSTATE_UNREAD);
74 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
75 toread = tab->is->types[tab->pos_type].blocksize;
76 if (toread > is_mbuf_size[buf->type])
78 toread = is_mbuf_size[buf->type];
79 block->state = IS_MBSTATE_PARTIAL;
82 block->state = IS_MBSTATE_CLEAN;
83 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
86 log(LOG_FATAL, "bfread failed.");
89 /* extract header info */
91 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
92 buf->offset += sizeof(block->num_records);
93 memcpy(&block->nextpos, buf->data + buf->offset,
94 sizeof(block->nextpos));
95 buf->offset += sizeof(block->nextpos);
96 if (block == tab->data) /* first block */
98 memcpy(&tab->num_records, buf->data + buf->offset,
99 sizeof(tab->num_records));
100 buf->offset +=sizeof(tab->num_records);
102 buf->num = (toread - buf->offset) / is_keysize(tab->is);
103 if (buf->num >= block->num_records)
105 buf->num = block->num_records;
106 block->state = IS_MBSTATE_CLEAN;
109 block->bread = buf->num * is_keysize(tab->is);
113 int is_p_read_full(is_mtable *tab, is_mblock *block)
118 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
120 log(LOG_FATAL, "partial read failed.");
123 if (block->state == IS_MBSTATE_PARTIAL)
126 dread = block->data->num;
127 while (dread < block->num_records)
129 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
132 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
133 if (toread > block->num_records - dread)
134 toread = block->num_records - dread;
136 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
137 is_keysize(tab->is), buf->data) < 0)
139 log(LOG_FATAL, "bfread failed.");
145 block->bread += toread * is_keysize(tab->is);
148 log(LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
153 * write dirty blocks to bfile.
154 * Allocate blocks as necessary.
156 void is_p_sync(is_mtable *tab)
161 isam_blocktype *type;
163 type = &tab->is->types[tab->pos_type];
164 for (p = tab->data; p; p = p->next)
166 if (p->state < IS_MBSTATE_DIRTY)
168 /* make sure that blocks are allocated. */
170 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
173 if (p->next->diskpos < 0)
174 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
177 p->nextpos = p->next->diskpos;
182 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
183 sum += sizeof(p->num_records);
184 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
185 sum += sizeof(p->nextpos);
186 if (p == tab->data) /* first block */
188 memcpy(type->dbuf + sum, &tab->num_records,
189 sizeof(tab->num_records));
190 sum += sizeof(tab->num_records);
192 for (b = p->data; b; b = b->next)
194 memcpy(type->dbuf + sum, b->data + b->offset, v = b->num *
195 is_keysize(tab->is));
197 assert(sum <= type->blocksize);
199 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
201 log(LOG_FATAL, "Failed to write block.");
204 log(LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos, p->num_records);
209 * Free all disk blocks associated with table.
211 void is_p_unmap(is_mtable *tab)
215 for (p = tab->data; p; p = p->next)
218 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
223 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
225 is_mbuf *p = 0, **pp = &p, *new;
230 while (*mb && toget >= (*mb)->num)
238 if (toget > 0 && *mb)
240 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
241 new->next = (*mb)->next;
243 new->data = (*mb)->data;
245 new->offset = (*mb)->offset + toget * keysize;
246 new->num = (*mb)->num - toget;
258 * Split up individual blocks which have grown too large.
259 * is_p_align and is_p_remap are alternative functions which trade off
260 * speed in updating versus optimum usage of disk blocks.
262 void is_p_align(is_mtable *tab)
264 is_mblock *mblock, *new, *last = 0, *next;
265 is_mbuf *mbufs, *mbp;
266 int blocks, recsblock;
268 log(LOG_DEBUG, "Realigning table.");
269 for (mblock = tab->data; mblock; mblock = next)
272 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
276 last->next = mblock->next;
277 last->state = IS_MBSTATE_DIRTY;
282 tab->data = tab->data->next;
283 tab->data->state = IS_MBSTATE_DIRTY;
286 if (mblock->diskpos >= 0)
287 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
288 xrelease_mblock(mblock);
290 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
291 (mblock == tab->data ?
292 tab->is->types[tab->pos_type].max_keys_block0 :
293 tab->is->types[tab->pos_type].max_keys_block))
295 blocks = tab->num_records /
296 tab->is->types[tab->pos_type].nice_keys_block;
297 if (tab->num_records %
298 tab->is->types[tab->pos_type].nice_keys_block)
300 recsblock = tab->num_records / blocks;
303 mbufs = mblock->data;
304 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
305 is_keysize(tab->is))) && recsblock)
309 new = xmalloc_mblock();
311 new->state = IS_MBSTATE_DIRTY;
312 new->next = mblock->next;
316 mblock->num_records = recsblock;
318 mblock = mblock->next;
328 * Reorganize data in blocks for minimum block usage and quick access.
329 * Free surplus blocks.
330 * is_p_align and is_p_remap are alternative functions which trade off
331 * speed in updating versus optimum usage of disk blocks.
333 void is_p_remap(is_mtable *tab)
335 is_mbuf *mbufs, **bufpp, *mbp;
336 is_mblock *blockp, **blockpp;
337 int recsblock, blocks;
339 log(LOG_DEBUG, "Remapping table.");
340 /* collect all data */
342 for (blockp = tab->data; blockp; blockp = blockp->next)
344 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
346 log(LOG_FATAL, "Read-full failed in remap.");
349 *bufpp = blockp->data;
351 bufpp = &(*bufpp)->next;
354 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
355 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
359 recsblock = tab->num_records / blocks + 1;
360 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
362 blockpp = &tab->data;
363 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
368 *blockpp = xmalloc_mblock();
369 (*blockpp)->diskpos = -1;
371 (*blockpp)->data = mbp;
372 (*blockpp)->num_records = recsblock;
373 (*blockpp)->state = IS_MBSTATE_DIRTY;
374 blockpp = &(*blockpp)->next;
380 for (blockp = *blockpp; blockp; blockp = blockp->next)
381 if (blockp->diskpos >= 0)
382 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
383 xfree_mblocks(*blockpp);