2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1996-03-20 16:17:11 quinn
10 * Revision 1.11 1996/03/20 13:29:17 quinn
13 * Revision 1.10 1996/03/19 19:22:44 quinn
16 * Revision 1.9 1996/02/06 10:19:57 quinn
17 * Attempt at fixing bug. Not all blocks were read before they were unlinked
18 * prior to a remap operation.
20 * Revision 1.8 1996/01/29 09:47:11 quinn
21 * Fixed mean little bug in the read-table code.
23 * Revision 1.7 1995/12/06 14:48:27 quinn
24 * Fixed some strange bugs.
26 * Revision 1.6 1995/09/04 12:33:47 adam
27 * Various cleanup. YAZ util used instead.
29 * Revision 1.5 1994/09/28 11:29:33 quinn
30 * Added cmp parameter.
32 * Revision 1.4 1994/09/27 20:03:53 quinn
33 * Seems relatively bug-free.
35 * Revision 1.3 1994/09/26 17:11:31 quinn
38 * Revision 1.2 1994/09/26 17:06:36 quinn
41 * Revision 1.1 1994/09/26 16:07:57 quinn
42 * Most of the functionality in place.
47 * This module handles the representation of tables in the bfiles.
55 static int is_freestore_alloc(ISAM is, int type)
59 if (is->types[type].freelist >= 0)
61 tmp = is->types[type].freelist;
62 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
63 &is->types[type].freelist) <=0)
65 logf (LOG_FATAL, "Failed to allocate block");
70 tmp = is->types[type].top++;
72 logf (LOG_DEBUG, "Allocating block #%d", tmp);
76 static void is_freestore_free(ISAM is, int type, int block)
80 logf (LOG_DEBUG, "Releasing block #%d", block);
81 tmp = is->types[type].freelist;
82 is->types[type].freelist = block;
83 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
85 logf (LOG_FATAL, "Failed to deallocate block.");
90 /* this code must be modified to handle an index */
91 int is_p_read_partial(is_mtable *tab, is_mblock *block)
96 assert(block->state == IS_MBSTATE_UNREAD);
97 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
98 toread = tab->is->types[tab->pos_type].blocksize;
99 if (toread > is_mbuf_size[buf->type])
101 toread = is_mbuf_size[buf->type];
102 block->state = IS_MBSTATE_PARTIAL;
105 block->state = IS_MBSTATE_CLEAN;
106 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
109 logf (LOG_FATAL, "bfread failed.");
112 /* extract header info */
114 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
115 assert(block->num_records > 0);
116 buf->offset += sizeof(block->num_records);
117 memcpy(&block->nextpos, buf->data + buf->offset,
118 sizeof(block->nextpos));
119 buf->offset += sizeof(block->nextpos);
120 if (block == tab->data) /* first block */
122 memcpy(&tab->num_records, buf->data + buf->offset,
123 sizeof(tab->num_records));
124 buf->offset +=sizeof(tab->num_records);
126 logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d",
127 block->diskpos, block->num_records, block->nextpos,
128 block == tab->data ? tab->num_records : -1);
129 buf->num = (toread - buf->offset) / is_keysize(tab->is);
130 if (buf->num >= block->num_records)
132 buf->num = block->num_records;
133 block->state = IS_MBSTATE_CLEAN;
136 block->bread = buf->offset + buf->num * is_keysize(tab->is);
140 int is_p_read_full(is_mtable *tab, is_mblock *block)
145 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
147 logf (LOG_FATAL, "partial read failed.");
150 if (block->state == IS_MBSTATE_PARTIAL)
153 dread = block->data->num;
154 while (dread < block->num_records)
156 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
159 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
160 if (toread > block->num_records - dread)
161 toread = block->num_records - dread;
163 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
164 is_keysize(tab->is), buf->data) < 0)
166 logf (LOG_FATAL, "bfread failed.");
172 block->bread += toread * is_keysize(tab->is);
174 block->state = IS_MBSTATE_CLEAN;
176 logf (LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
181 * write dirty blocks to bfile.
182 * Allocate blocks as necessary.
184 void is_p_sync(is_mtable *tab)
189 isam_blocktype *type;
191 type = &tab->is->types[tab->pos_type];
192 for (p = tab->data; p; p = p->next)
194 if (p->state < IS_MBSTATE_DIRTY)
196 /* make sure that blocks are allocated. */
198 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
201 if (p->next->diskpos < 0)
202 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
205 p->nextpos = p->next->diskpos;
210 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
211 sum += sizeof(p->num_records);
212 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
213 sum += sizeof(p->nextpos);
214 if (p == tab->data) /* first block */
216 memcpy(type->dbuf + sum, &tab->num_records,
217 sizeof(tab->num_records));
218 sum += sizeof(tab->num_records);
220 logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos,
222 assert(p->num_records > 0);
223 for (b = p->data; b; b = b->next)
225 logf(LOG_DEBUG, " buf: offset %d, keys %d, type %d, ref %d",
226 b->offset, b->num, b->type, b->refcount);
227 if ((v = b->num * is_keysize(tab->is)) > 0)
228 memcpy(type->dbuf + sum, b->data + b->offset, v);
231 assert(sum <= type->blocksize);
233 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
235 logf (LOG_FATAL, "Failed to write block.");
242 * Free all disk blocks associated with table.
244 void is_p_unmap(is_mtable *tab)
248 for (p = tab->data; p; p = p->next)
252 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
258 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
260 is_mbuf *p = 0, **pp = &p, *new;
265 while (*mb && toget >= (*mb)->num)
273 if (toget > 0 && *mb)
275 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
276 new->next = (*mb)->next;
278 new->data = (*mb)->data;
280 new->offset = (*mb)->offset + toget * keysize;
281 new->num = (*mb)->num - toget;
293 * Split up individual blocks which have grown too large.
294 * is_p_align and is_p_remap are alternative functions which trade off
295 * speed in updating versus optimum usage of disk blocks.
297 void is_p_align(is_mtable *tab)
299 is_mblock *mblock, *new, *last = 0, *next;
300 is_mbuf *mbufs, *mbp;
301 int blocks, recsblock;
303 logf (LOG_DEBUG, "Realigning table.");
304 for (mblock = tab->data; mblock; mblock = next)
307 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
311 last->next = mblock->next;
312 last->state = IS_MBSTATE_DIRTY;
317 next = tab->data->next;
320 if (next->state < IS_MBSTATE_CLEAN)
322 if (is_p_read_full(tab, next) < 0)
324 logf(LOG_FATAL, "Error during re-alignment");
327 if (next->nextpos && !next->next)
329 next->next = xmalloc_mblock();
330 next->next->diskpos = next->nextpos;
331 next->next->state = IS_MBSTATE_UNREAD;
332 next->next->data = 0;
335 next->state = IS_MBSTATE_DIRTY; /* force re-process */
339 if (mblock->diskpos >= 0)
340 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
341 xrelease_mblock(mblock);
343 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
344 (mblock == tab->data ?
345 tab->is->types[tab->pos_type].max_keys_block0 :
346 tab->is->types[tab->pos_type].max_keys_block))
348 blocks = tab->num_records /
349 tab->is->types[tab->pos_type].nice_keys_block;
350 if (tab->num_records %
351 tab->is->types[tab->pos_type].nice_keys_block)
353 recsblock = tab->num_records / blocks;
356 mbufs = mblock->data;
357 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
358 is_keysize(tab->is))) && recsblock)
362 new = xmalloc_mblock();
364 new->state = IS_MBSTATE_DIRTY;
365 new->next = mblock->next;
369 mblock->num_records = recsblock;
371 mblock = mblock->next;
381 * Reorganize data in blocks for minimum block usage and quick access.
382 * Free surplus blocks.
383 * is_p_align and is_p_remap are alternative functions which trade off
384 * speed in updating versus optimum usage of disk blocks.
386 void is_p_remap(is_mtable *tab)
388 is_mbuf *mbufs, **bufpp, *mbp;
389 is_mblock *blockp, **blockpp;
390 int recsblock, blocks;
392 logf (LOG_DEBUG, "Remapping table.");
393 /* collect all data */
395 for (blockp = tab->data; blockp; blockp = blockp->next)
397 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
399 logf (LOG_FATAL, "Read-full failed in remap.");
402 *bufpp = blockp->data;
404 bufpp = &(*bufpp)->next;
407 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
408 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
412 recsblock = tab->num_records / blocks + 1;
413 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
415 blockpp = &tab->data;
416 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
421 *blockpp = xmalloc_mblock();
422 (*blockpp)->diskpos = -1;
424 (*blockpp)->data = mbp;
425 (*blockpp)->num_records = recsblock;
426 (*blockpp)->state = IS_MBSTATE_DIRTY;
427 blockpp = &(*blockpp)->next;
433 for (blockp = *blockpp; blockp; blockp = blockp->next)
434 if (blockp->diskpos >= 0)
435 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
436 xfree_mblocks(*blockpp);