2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1994-09-27 20:03:53 quinn
8 * Seems relatively bug-free.
10 * Revision 1.3 1994/09/26 17:11:31 quinn
13 * Revision 1.2 1994/09/26 17:06:36 quinn
16 * Revision 1.1 1994/09/26 16:07:57 quinn
17 * Most of the functionality in place.
22 * This module handles the representation of tables in the bfiles.
29 static int is_freestore_alloc(ISAM is, int type)
33 if (is->types[type].freelist >= 0)
35 tmp = is->types[type].freelist;
36 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
37 &is->types[type].freelist) <=0)
39 log(LOG_FATAL, "Failed to allocate block");
44 tmp = is->types[type].top++;
46 log(LOG_DEBUG, "Allocating block #%d", tmp);
50 static void is_freestore_free(ISAM is, int type, int block)
54 log(LOG_DEBUG, "Releasing block #%d", block);
55 tmp = is->types[type].freelist;
56 is->types[type].freelist = block;
57 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
59 log(LOG_FATAL, "Failed to deallocate block.");
64 /* this code must be modified to handle an index */
65 int is_p_read_partial(is_mtable *tab, is_mblock *block)
70 assert(block->state == IS_MBSTATE_UNREAD);
71 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
72 toread = tab->is->types[tab->pos_type].blocksize;
73 if (toread > is_mbuf_size[buf->type])
75 toread = is_mbuf_size[buf->type];
76 block->state = IS_MBSTATE_PARTIAL;
79 block->state = IS_MBSTATE_CLEAN;
80 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
83 log(LOG_FATAL, "bfread failed.");
86 /* extract header info */
88 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
89 buf->offset += sizeof(block->num_records);
90 memcpy(&block->nextpos, buf->data + buf->offset,
91 sizeof(block->nextpos));
92 buf->offset += sizeof(block->nextpos);
93 if (block == tab->data) /* first block */
95 memcpy(&tab->num_records, buf->data + buf->offset,
96 sizeof(tab->num_records));
97 buf->offset +=sizeof(tab->num_records);
99 buf->num = (toread - buf->offset) / is_keysize(tab->is);
100 if (buf->num >= block->num_records)
102 buf->num = block->num_records;
103 block->state = IS_MBSTATE_CLEAN;
106 block->bread = buf->num * is_keysize(tab->is);
110 int is_p_read_full(is_mtable *tab, is_mblock *block)
115 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
117 log(LOG_FATAL, "partial read failed.");
120 if (block->state == IS_MBSTATE_PARTIAL)
123 dread = block->data->num;
124 while (dread < block->num_records)
126 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
129 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
130 if (toread > block->num_records - dread)
131 toread = block->num_records - dread;
133 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
134 is_keysize(tab->is), buf->data) < 0)
136 log(LOG_FATAL, "bfread failed.");
142 block->bread += toread * is_keysize(tab->is);
145 log(LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
150 * write dirty blocks to bfile.
151 * Allocate blocks as necessary.
153 void is_p_sync(is_mtable *tab)
158 isam_blocktype *type;
160 type = &tab->is->types[tab->pos_type];
161 for (p = tab->data; p; p = p->next)
165 if (p->num_records == 0)
168 if (p->state < IS_MBSTATE_DIRTY)
170 /* make sure that blocks are allocated. */
172 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
175 if (p->next->diskpos < 0)
176 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
179 p->nextpos = p->next->diskpos;
184 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
185 sum += sizeof(p->num_records);
186 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
187 sum += sizeof(p->nextpos);
188 if (p == tab->data) /* first block */
190 memcpy(type->dbuf + sum, &tab->num_records,
191 sizeof(tab->num_records));
192 sum += sizeof(tab->num_records);
194 for (b = p->data; b; b = b->next)
196 memcpy(type->dbuf + sum, b->data + b->offset, v = b->num *
197 is_keysize(tab->is));
199 assert(sum <= type->blocksize);
201 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
203 log(LOG_FATAL, "Failed to write block.");
206 log(LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos, p->num_records);
211 * Free all disk blocks associated with table.
213 void is_p_unmap(is_mtable *tab)
217 for (p = tab->data; p; p = p->next)
220 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
225 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
227 is_mbuf *p = 0, **pp = &p, *new;
232 while (*mb && toget >= (*mb)->num)
240 if (toget > 0 && *mb)
242 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
243 new->next = (*mb)->next;
245 new->data = (*mb)->data;
247 new->offset = (*mb)->offset + toget * keysize;
248 new->num = (*mb)->num - toget;
260 * Split up individual blocks which have grown too large.
261 * is_p_align and is_p_remap are alternative functions which trade off
262 * speed in updating versus optimum usage of disk blocks.
264 void is_p_align(is_mtable *tab)
266 is_mblock *mblock, *new, *last = 0, *next;
267 is_mbuf *mbufs, *mbp;
268 int blocks, recsblock;
270 log(LOG_DEBUG, "Realigning table.");
271 for (mblock = tab->data; mblock; mblock = next)
274 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
278 last->next = mblock->next;
279 last->state = IS_MBSTATE_DIRTY;
284 tab->data = tab->data->next;
285 tab->data->state = IS_MBSTATE_DIRTY;
288 if (mblock->diskpos >= 0)
289 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
290 xrelease_mblock(mblock);
292 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
293 (mblock == tab->data ?
294 tab->is->types[tab->pos_type].max_keys_block0 :
295 tab->is->types[tab->pos_type].max_keys_block))
297 blocks = tab->num_records /
298 tab->is->types[tab->pos_type].nice_keys_block;
299 if (tab->num_records %
300 tab->is->types[tab->pos_type].nice_keys_block)
302 recsblock = tab->num_records / blocks;
305 mbufs = mblock->data;
306 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
307 is_keysize(tab->is))) && recsblock)
311 new = xmalloc_mblock();
313 new->state = IS_MBSTATE_DIRTY;
314 new->next = mblock->next;
318 mblock->num_records = recsblock;
320 mblock = mblock->next;
330 * Reorganize data in blocks for minimum block usage and quick access.
331 * Free surplus blocks.
332 * is_p_align and is_p_remap are alternative functions which trade off
333 * speed in updating versus optimum usage of disk blocks.
335 void is_p_remap(is_mtable *tab)
337 is_mbuf *mbufs, **bufpp, *mbp;
338 is_mblock *blockp, **blockpp;
339 int recsblock, blocks;
341 log(LOG_DEBUG, "Remapping table.");
342 /* collect all data */
344 for (blockp = tab->data; blockp; blockp = blockp->next)
346 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
348 log(LOG_FATAL, "Read-full failed in remap.");
351 *bufpp = blockp->data;
353 bufpp = &(*bufpp)->next;
356 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
357 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
361 recsblock = tab->num_records / blocks + 1;
362 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
364 blockpp = &tab->data;
365 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
370 *blockpp = xmalloc_mblock();
371 (*blockpp)->diskpos = -1;
373 (*blockpp)->data = mbp;
374 (*blockpp)->num_records = recsblock;
375 (*blockpp)->state = IS_MBSTATE_DIRTY;
376 blockpp = &(*blockpp)->next;
382 for (blockp = *blockpp; blockp; blockp = blockp->next)
383 if (blockp->diskpos >= 0)
384 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
385 xfree_mblocks(*blockpp);