2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1997-09-09 13:38:12 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.13 1996/10/29 13:56:57 adam
11 * Include of zebrautl.h instead of alexutil.h.
13 * Revision 1.12 1996/03/20 16:17:11 quinn
16 * Revision 1.11 1996/03/20 13:29:17 quinn
19 * Revision 1.10 1996/03/19 19:22:44 quinn
22 * Revision 1.9 1996/02/06 10:19:57 quinn
23 * Attempt at fixing bug. Not all blocks were read before they were unlinked
24 * prior to a remap operation.
26 * Revision 1.8 1996/01/29 09:47:11 quinn
27 * Fixed mean little bug in the read-table code.
29 * Revision 1.7 1995/12/06 14:48:27 quinn
30 * Fixed some strange bugs.
32 * Revision 1.6 1995/09/04 12:33:47 adam
33 * Various cleanup. YAZ util used instead.
35 * Revision 1.5 1994/09/28 11:29:33 quinn
36 * Added cmp parameter.
38 * Revision 1.4 1994/09/27 20:03:53 quinn
39 * Seems relatively bug-free.
41 * Revision 1.3 1994/09/26 17:11:31 quinn
44 * Revision 1.2 1994/09/26 17:06:36 quinn
47 * Revision 1.1 1994/09/26 16:07:57 quinn
48 * Most of the functionality in place.
53 * This module handles the representation of tables in the bfiles.
63 static int is_freestore_alloc(ISAM is, int type)
67 if (is->types[type].freelist >= 0)
69 tmp = is->types[type].freelist;
70 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
71 &is->types[type].freelist) <=0)
73 logf (LOG_FATAL, "Failed to allocate block");
78 tmp = is->types[type].top++;
80 logf (LOG_DEBUG, "Allocating block #%d", tmp);
84 static void is_freestore_free(ISAM is, int type, int block)
88 logf (LOG_DEBUG, "Releasing block #%d", block);
89 tmp = is->types[type].freelist;
90 is->types[type].freelist = block;
91 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
93 logf (LOG_FATAL, "Failed to deallocate block.");
98 /* this code must be modified to handle an index */
99 int is_p_read_partial(is_mtable *tab, is_mblock *block)
104 assert(block->state == IS_MBSTATE_UNREAD);
105 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
106 toread = tab->is->types[tab->pos_type].blocksize;
107 if (toread > is_mbuf_size[buf->type])
109 toread = is_mbuf_size[buf->type];
110 block->state = IS_MBSTATE_PARTIAL;
113 block->state = IS_MBSTATE_CLEAN;
114 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
117 logf (LOG_FATAL, "bfread failed.");
120 /* extract header info */
122 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
123 assert(block->num_records > 0);
124 buf->offset += sizeof(block->num_records);
125 memcpy(&block->nextpos, buf->data + buf->offset,
126 sizeof(block->nextpos));
127 buf->offset += sizeof(block->nextpos);
128 if (block == tab->data) /* first block */
130 memcpy(&tab->num_records, buf->data + buf->offset,
131 sizeof(tab->num_records));
132 buf->offset +=sizeof(tab->num_records);
134 logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d",
135 block->diskpos, block->num_records, block->nextpos,
136 block == tab->data ? tab->num_records : -1);
137 buf->num = (toread - buf->offset) / is_keysize(tab->is);
138 if (buf->num >= block->num_records)
140 buf->num = block->num_records;
141 block->state = IS_MBSTATE_CLEAN;
144 block->bread = buf->offset + buf->num * is_keysize(tab->is);
148 int is_p_read_full(is_mtable *tab, is_mblock *block)
153 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
155 logf (LOG_FATAL, "partial read failed.");
158 if (block->state == IS_MBSTATE_PARTIAL)
161 dread = block->data->num;
162 while (dread < block->num_records)
164 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
167 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
168 if (toread > block->num_records - dread)
169 toread = block->num_records - dread;
171 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
172 is_keysize(tab->is), buf->data) < 0)
174 logf (LOG_FATAL, "bfread failed.");
180 block->bread += toread * is_keysize(tab->is);
182 block->state = IS_MBSTATE_CLEAN;
184 logf (LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
189 * write dirty blocks to bfile.
190 * Allocate blocks as necessary.
192 void is_p_sync(is_mtable *tab)
197 isam_blocktype *type;
199 type = &tab->is->types[tab->pos_type];
200 for (p = tab->data; p; p = p->next)
202 if (p->state < IS_MBSTATE_DIRTY)
204 /* make sure that blocks are allocated. */
206 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
209 if (p->next->diskpos < 0)
210 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
213 p->nextpos = p->next->diskpos;
218 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
219 sum += sizeof(p->num_records);
220 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
221 sum += sizeof(p->nextpos);
222 if (p == tab->data) /* first block */
224 memcpy(type->dbuf + sum, &tab->num_records,
225 sizeof(tab->num_records));
226 sum += sizeof(tab->num_records);
228 logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos,
230 assert(p->num_records > 0);
231 for (b = p->data; b; b = b->next)
233 logf(LOG_DEBUG, " buf: offset %d, keys %d, type %d, ref %d",
234 b->offset, b->num, b->type, b->refcount);
235 if ((v = b->num * is_keysize(tab->is)) > 0)
236 memcpy(type->dbuf + sum, b->data + b->offset, v);
239 assert(sum <= type->blocksize);
241 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
243 logf (LOG_FATAL, "Failed to write block.");
250 * Free all disk blocks associated with table.
252 void is_p_unmap(is_mtable *tab)
256 for (p = tab->data; p; p = p->next)
260 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
266 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
268 is_mbuf *p = 0, **pp = &p, *new;
273 while (*mb && toget >= (*mb)->num)
281 if (toget > 0 && *mb)
283 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
284 new->next = (*mb)->next;
286 new->data = (*mb)->data;
288 new->offset = (*mb)->offset + toget * keysize;
289 new->num = (*mb)->num - toget;
301 * Split up individual blocks which have grown too large.
302 * is_p_align and is_p_remap are alternative functions which trade off
303 * speed in updating versus optimum usage of disk blocks.
305 void is_p_align(is_mtable *tab)
307 is_mblock *mblock, *new, *last = 0, *next;
308 is_mbuf *mbufs, *mbp;
309 int blocks, recsblock;
311 logf (LOG_DEBUG, "Realigning table.");
312 for (mblock = tab->data; mblock; mblock = next)
315 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
319 last->next = mblock->next;
320 last->state = IS_MBSTATE_DIRTY;
325 next = tab->data->next;
328 if (next->state < IS_MBSTATE_CLEAN)
330 if (is_p_read_full(tab, next) < 0)
332 logf(LOG_FATAL, "Error during re-alignment");
335 if (next->nextpos && !next->next)
337 next->next = xmalloc_mblock();
338 next->next->diskpos = next->nextpos;
339 next->next->state = IS_MBSTATE_UNREAD;
340 next->next->data = 0;
343 next->state = IS_MBSTATE_DIRTY; /* force re-process */
347 if (mblock->diskpos >= 0)
348 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
349 xrelease_mblock(mblock);
351 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
352 (mblock == tab->data ?
353 tab->is->types[tab->pos_type].max_keys_block0 :
354 tab->is->types[tab->pos_type].max_keys_block))
356 blocks = tab->num_records /
357 tab->is->types[tab->pos_type].nice_keys_block;
358 if (tab->num_records %
359 tab->is->types[tab->pos_type].nice_keys_block)
361 recsblock = tab->num_records / blocks;
364 mbufs = mblock->data;
365 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
366 is_keysize(tab->is))) && recsblock)
370 new = xmalloc_mblock();
372 new->state = IS_MBSTATE_DIRTY;
373 new->next = mblock->next;
377 mblock->num_records = recsblock;
379 mblock = mblock->next;
389 * Reorganize data in blocks for minimum block usage and quick access.
390 * Free surplus blocks.
391 * is_p_align and is_p_remap are alternative functions which trade off
392 * speed in updating versus optimum usage of disk blocks.
394 void is_p_remap(is_mtable *tab)
396 is_mbuf *mbufs, **bufpp, *mbp;
397 is_mblock *blockp, **blockpp;
398 int recsblock, blocks;
400 logf (LOG_DEBUG, "Remapping table.");
401 /* collect all data */
403 for (blockp = tab->data; blockp; blockp = blockp->next)
405 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
407 logf (LOG_FATAL, "Read-full failed in remap.");
410 *bufpp = blockp->data;
412 bufpp = &(*bufpp)->next;
415 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
416 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
420 recsblock = tab->num_records / blocks + 1;
421 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
423 blockpp = &tab->data;
424 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
429 *blockpp = xmalloc_mblock();
430 (*blockpp)->diskpos = -1;
432 (*blockpp)->data = mbp;
433 (*blockpp)->num_records = recsblock;
434 (*blockpp)->state = IS_MBSTATE_DIRTY;
435 blockpp = &(*blockpp)->next;
441 for (blockp = *blockpp; blockp; blockp = blockp->next)
442 if (blockp->diskpos >= 0)
443 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
444 xfree_mblocks(*blockpp);