2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1996-10-29 13:56:57 adam
8 * Include of zebrautl.h instead of alexutil.h.
10 * Revision 1.12 1996/03/20 16:17:11 quinn
13 * Revision 1.11 1996/03/20 13:29:17 quinn
16 * Revision 1.10 1996/03/19 19:22:44 quinn
19 * Revision 1.9 1996/02/06 10:19:57 quinn
20 * Attempt at fixing bug. Not all blocks were read before they were unlinked
21 * prior to a remap operation.
23 * Revision 1.8 1996/01/29 09:47:11 quinn
24 * Fixed mean little bug in the read-table code.
26 * Revision 1.7 1995/12/06 14:48:27 quinn
27 * Fixed some strange bugs.
29 * Revision 1.6 1995/09/04 12:33:47 adam
30 * Various cleanup. YAZ util used instead.
32 * Revision 1.5 1994/09/28 11:29:33 quinn
33 * Added cmp parameter.
35 * Revision 1.4 1994/09/27 20:03:53 quinn
36 * Seems relatively bug-free.
38 * Revision 1.3 1994/09/26 17:11:31 quinn
41 * Revision 1.2 1994/09/26 17:06:36 quinn
44 * Revision 1.1 1994/09/26 16:07:57 quinn
45 * Most of the functionality in place.
50 * This module handles the representation of tables in the bfiles.
59 static int is_freestore_alloc(ISAM is, int type)
63 if (is->types[type].freelist >= 0)
65 tmp = is->types[type].freelist;
66 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
67 &is->types[type].freelist) <=0)
69 logf (LOG_FATAL, "Failed to allocate block");
74 tmp = is->types[type].top++;
76 logf (LOG_DEBUG, "Allocating block #%d", tmp);
80 static void is_freestore_free(ISAM is, int type, int block)
84 logf (LOG_DEBUG, "Releasing block #%d", block);
85 tmp = is->types[type].freelist;
86 is->types[type].freelist = block;
87 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
89 logf (LOG_FATAL, "Failed to deallocate block.");
94 /* this code must be modified to handle an index */
95 int is_p_read_partial(is_mtable *tab, is_mblock *block)
100 assert(block->state == IS_MBSTATE_UNREAD);
101 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
102 toread = tab->is->types[tab->pos_type].blocksize;
103 if (toread > is_mbuf_size[buf->type])
105 toread = is_mbuf_size[buf->type];
106 block->state = IS_MBSTATE_PARTIAL;
109 block->state = IS_MBSTATE_CLEAN;
110 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
113 logf (LOG_FATAL, "bfread failed.");
116 /* extract header info */
118 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
119 assert(block->num_records > 0);
120 buf->offset += sizeof(block->num_records);
121 memcpy(&block->nextpos, buf->data + buf->offset,
122 sizeof(block->nextpos));
123 buf->offset += sizeof(block->nextpos);
124 if (block == tab->data) /* first block */
126 memcpy(&tab->num_records, buf->data + buf->offset,
127 sizeof(tab->num_records));
128 buf->offset +=sizeof(tab->num_records);
130 logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d",
131 block->diskpos, block->num_records, block->nextpos,
132 block == tab->data ? tab->num_records : -1);
133 buf->num = (toread - buf->offset) / is_keysize(tab->is);
134 if (buf->num >= block->num_records)
136 buf->num = block->num_records;
137 block->state = IS_MBSTATE_CLEAN;
140 block->bread = buf->offset + buf->num * is_keysize(tab->is);
144 int is_p_read_full(is_mtable *tab, is_mblock *block)
149 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
151 logf (LOG_FATAL, "partial read failed.");
154 if (block->state == IS_MBSTATE_PARTIAL)
157 dread = block->data->num;
158 while (dread < block->num_records)
160 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
163 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
164 if (toread > block->num_records - dread)
165 toread = block->num_records - dread;
167 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
168 is_keysize(tab->is), buf->data) < 0)
170 logf (LOG_FATAL, "bfread failed.");
176 block->bread += toread * is_keysize(tab->is);
178 block->state = IS_MBSTATE_CLEAN;
180 logf (LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
185 * write dirty blocks to bfile.
186 * Allocate blocks as necessary.
188 void is_p_sync(is_mtable *tab)
193 isam_blocktype *type;
195 type = &tab->is->types[tab->pos_type];
196 for (p = tab->data; p; p = p->next)
198 if (p->state < IS_MBSTATE_DIRTY)
200 /* make sure that blocks are allocated. */
202 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
205 if (p->next->diskpos < 0)
206 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
209 p->nextpos = p->next->diskpos;
214 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
215 sum += sizeof(p->num_records);
216 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
217 sum += sizeof(p->nextpos);
218 if (p == tab->data) /* first block */
220 memcpy(type->dbuf + sum, &tab->num_records,
221 sizeof(tab->num_records));
222 sum += sizeof(tab->num_records);
224 logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos,
226 assert(p->num_records > 0);
227 for (b = p->data; b; b = b->next)
229 logf(LOG_DEBUG, " buf: offset %d, keys %d, type %d, ref %d",
230 b->offset, b->num, b->type, b->refcount);
231 if ((v = b->num * is_keysize(tab->is)) > 0)
232 memcpy(type->dbuf + sum, b->data + b->offset, v);
235 assert(sum <= type->blocksize);
237 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
239 logf (LOG_FATAL, "Failed to write block.");
246 * Free all disk blocks associated with table.
248 void is_p_unmap(is_mtable *tab)
252 for (p = tab->data; p; p = p->next)
256 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
262 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
264 is_mbuf *p = 0, **pp = &p, *new;
269 while (*mb && toget >= (*mb)->num)
277 if (toget > 0 && *mb)
279 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
280 new->next = (*mb)->next;
282 new->data = (*mb)->data;
284 new->offset = (*mb)->offset + toget * keysize;
285 new->num = (*mb)->num - toget;
297 * Split up individual blocks which have grown too large.
298 * is_p_align and is_p_remap are alternative functions which trade off
299 * speed in updating versus optimum usage of disk blocks.
301 void is_p_align(is_mtable *tab)
303 is_mblock *mblock, *new, *last = 0, *next;
304 is_mbuf *mbufs, *mbp;
305 int blocks, recsblock;
307 logf (LOG_DEBUG, "Realigning table.");
308 for (mblock = tab->data; mblock; mblock = next)
311 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
315 last->next = mblock->next;
316 last->state = IS_MBSTATE_DIRTY;
321 next = tab->data->next;
324 if (next->state < IS_MBSTATE_CLEAN)
326 if (is_p_read_full(tab, next) < 0)
328 logf(LOG_FATAL, "Error during re-alignment");
331 if (next->nextpos && !next->next)
333 next->next = xmalloc_mblock();
334 next->next->diskpos = next->nextpos;
335 next->next->state = IS_MBSTATE_UNREAD;
336 next->next->data = 0;
339 next->state = IS_MBSTATE_DIRTY; /* force re-process */
343 if (mblock->diskpos >= 0)
344 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
345 xrelease_mblock(mblock);
347 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
348 (mblock == tab->data ?
349 tab->is->types[tab->pos_type].max_keys_block0 :
350 tab->is->types[tab->pos_type].max_keys_block))
352 blocks = tab->num_records /
353 tab->is->types[tab->pos_type].nice_keys_block;
354 if (tab->num_records %
355 tab->is->types[tab->pos_type].nice_keys_block)
357 recsblock = tab->num_records / blocks;
360 mbufs = mblock->data;
361 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
362 is_keysize(tab->is))) && recsblock)
366 new = xmalloc_mblock();
368 new->state = IS_MBSTATE_DIRTY;
369 new->next = mblock->next;
373 mblock->num_records = recsblock;
375 mblock = mblock->next;
385 * Reorganize data in blocks for minimum block usage and quick access.
386 * Free surplus blocks.
387 * is_p_align and is_p_remap are alternative functions which trade off
388 * speed in updating versus optimum usage of disk blocks.
390 void is_p_remap(is_mtable *tab)
392 is_mbuf *mbufs, **bufpp, *mbp;
393 is_mblock *blockp, **blockpp;
394 int recsblock, blocks;
396 logf (LOG_DEBUG, "Remapping table.");
397 /* collect all data */
399 for (blockp = tab->data; blockp; blockp = blockp->next)
401 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
403 logf (LOG_FATAL, "Read-full failed in remap.");
406 *bufpp = blockp->data;
408 bufpp = &(*bufpp)->next;
411 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
412 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
416 recsblock = tab->num_records / blocks + 1;
417 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
419 blockpp = &tab->data;
420 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
425 *blockpp = xmalloc_mblock();
426 (*blockpp)->diskpos = -1;
428 (*blockpp)->data = mbp;
429 (*blockpp)->num_records = recsblock;
430 (*blockpp)->state = IS_MBSTATE_DIRTY;
431 blockpp = &(*blockpp)->next;
437 for (blockp = *blockpp; blockp; blockp = blockp->next)
438 if (blockp->diskpos >= 0)
439 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
440 xfree_mblocks(*blockpp);