2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1999-02-02 14:51:22 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.14 1997/09/09 13:38:12 adam
11 * Partial port to WIN95/NT.
13 * Revision 1.13 1996/10/29 13:56:57 adam
14 * Include of zebrautl.h instead of alexutil.h.
16 * Revision 1.12 1996/03/20 16:17:11 quinn
19 * Revision 1.11 1996/03/20 13:29:17 quinn
22 * Revision 1.10 1996/03/19 19:22:44 quinn
25 * Revision 1.9 1996/02/06 10:19:57 quinn
26 * Attempt at fixing bug. Not all blocks were read before they were unlinked
27 * prior to a remap operation.
29 * Revision 1.8 1996/01/29 09:47:11 quinn
30 * Fixed mean little bug in the read-table code.
32 * Revision 1.7 1995/12/06 14:48:27 quinn
33 * Fixed some strange bugs.
35 * Revision 1.6 1995/09/04 12:33:47 adam
36 * Various cleanup. YAZ util used instead.
38 * Revision 1.5 1994/09/28 11:29:33 quinn
39 * Added cmp parameter.
41 * Revision 1.4 1994/09/27 20:03:53 quinn
42 * Seems relatively bug-free.
44 * Revision 1.3 1994/09/26 17:11:31 quinn
47 * Revision 1.2 1994/09/26 17:06:36 quinn
50 * Revision 1.1 1994/09/26 16:07:57 quinn
51 * Most of the functionality in place.
56 * This module handles the representation of tables in the bfiles.
66 static int is_freestore_alloc(ISAM is, int type)
70 if (is->types[type].freelist >= 0)
72 tmp = is->types[type].freelist;
73 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
74 &is->types[type].freelist) <=0)
76 logf (LOG_FATAL, "Failed to allocate block");
81 tmp = is->types[type].top++;
83 logf (LOG_DEBUG, "Allocating block #%d", tmp);
87 static void is_freestore_free(ISAM is, int type, int block)
91 logf (LOG_DEBUG, "Releasing block #%d", block);
92 tmp = is->types[type].freelist;
93 is->types[type].freelist = block;
94 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
96 logf (LOG_FATAL, "Failed to deallocate block.");
101 /* this code must be modified to handle an index */
102 int is_p_read_partial(is_mtable *tab, is_mblock *block)
107 assert(block->state == IS_MBSTATE_UNREAD);
108 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
109 toread = tab->is->types[tab->pos_type].blocksize;
110 if (toread > is_mbuf_size[buf->type])
112 toread = is_mbuf_size[buf->type];
113 block->state = IS_MBSTATE_PARTIAL;
116 block->state = IS_MBSTATE_CLEAN;
117 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
120 logf (LOG_FATAL, "bfread failed.");
123 /* extract header info */
125 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
126 assert(block->num_records > 0);
127 buf->offset += sizeof(block->num_records);
128 memcpy(&block->nextpos, buf->data + buf->offset,
129 sizeof(block->nextpos));
130 buf->offset += sizeof(block->nextpos);
131 if (block == tab->data) /* first block */
133 memcpy(&tab->num_records, buf->data + buf->offset,
134 sizeof(tab->num_records));
135 buf->offset +=sizeof(tab->num_records);
137 logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d",
138 block->diskpos, block->num_records, block->nextpos,
139 block == tab->data ? tab->num_records : -1);
140 buf->num = (toread - buf->offset) / is_keysize(tab->is);
141 if (buf->num >= block->num_records)
143 buf->num = block->num_records;
144 block->state = IS_MBSTATE_CLEAN;
147 block->bread = buf->offset + buf->num * is_keysize(tab->is);
151 int is_p_read_full(is_mtable *tab, is_mblock *block)
156 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
158 logf (LOG_FATAL, "partial read failed.");
161 if (block->state == IS_MBSTATE_PARTIAL)
164 dread = block->data->num;
165 while (dread < block->num_records)
167 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
170 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
171 if (toread > block->num_records - dread)
172 toread = block->num_records - dread;
174 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
175 is_keysize(tab->is), buf->data) < 0)
177 logf (LOG_FATAL, "bfread failed.");
183 block->bread += toread * is_keysize(tab->is);
185 block->state = IS_MBSTATE_CLEAN;
187 logf (LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
192 * write dirty blocks to bfile.
193 * Allocate blocks as necessary.
195 void is_p_sync(is_mtable *tab)
200 isam_blocktype *type;
202 type = &tab->is->types[tab->pos_type];
203 for (p = tab->data; p; p = p->next)
205 if (p->state < IS_MBSTATE_DIRTY)
207 /* make sure that blocks are allocated. */
209 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
212 if (p->next->diskpos < 0)
213 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
216 p->nextpos = p->next->diskpos;
221 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
222 sum += sizeof(p->num_records);
223 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
224 sum += sizeof(p->nextpos);
225 if (p == tab->data) /* first block */
227 memcpy(type->dbuf + sum, &tab->num_records,
228 sizeof(tab->num_records));
229 sum += sizeof(tab->num_records);
231 logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos,
233 assert(p->num_records > 0);
234 for (b = p->data; b; b = b->next)
236 logf(LOG_DEBUG, " buf: offset %d, keys %d, type %d, ref %d",
237 b->offset, b->num, b->type, b->refcount);
238 if ((v = b->num * is_keysize(tab->is)) > 0)
239 memcpy(type->dbuf + sum, b->data + b->offset, v);
242 assert(sum <= type->blocksize);
244 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
246 logf (LOG_FATAL, "Failed to write block.");
253 * Free all disk blocks associated with table.
255 void is_p_unmap(is_mtable *tab)
259 for (p = tab->data; p; p = p->next)
263 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
269 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
271 is_mbuf *p = 0, **pp = &p, *new;
276 while (*mb && toget >= (*mb)->num)
284 if (toget > 0 && *mb)
286 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
287 new->next = (*mb)->next;
289 new->data = (*mb)->data;
291 new->offset = (*mb)->offset + toget * keysize;
292 new->num = (*mb)->num - toget;
304 * Split up individual blocks which have grown too large.
305 * is_p_align and is_p_remap are alternative functions which trade off
306 * speed in updating versus optimum usage of disk blocks.
308 void is_p_align(is_mtable *tab)
310 is_mblock *mblock, *new, *last = 0, *next;
311 is_mbuf *mbufs, *mbp;
312 int blocks, recsblock;
314 logf (LOG_DEBUG, "Realigning table.");
315 for (mblock = tab->data; mblock; mblock = next)
318 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
322 last->next = mblock->next;
323 last->state = IS_MBSTATE_DIRTY;
328 next = tab->data->next;
331 if (next->state < IS_MBSTATE_CLEAN)
333 if (is_p_read_full(tab, next) < 0)
335 logf(LOG_FATAL, "Error during re-alignment");
338 if (next->nextpos && !next->next)
340 next->next = xmalloc_mblock();
341 next->next->diskpos = next->nextpos;
342 next->next->state = IS_MBSTATE_UNREAD;
343 next->next->data = 0;
346 next->state = IS_MBSTATE_DIRTY; /* force re-process */
350 if (mblock->diskpos >= 0)
351 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
352 xrelease_mblock(mblock);
354 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
355 (mblock == tab->data ?
356 tab->is->types[tab->pos_type].max_keys_block0 :
357 tab->is->types[tab->pos_type].max_keys_block))
359 blocks = tab->num_records /
360 tab->is->types[tab->pos_type].nice_keys_block;
361 if (tab->num_records %
362 tab->is->types[tab->pos_type].nice_keys_block)
364 recsblock = tab->num_records / blocks;
367 mbufs = mblock->data;
368 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
369 is_keysize(tab->is))) && recsblock)
373 new = xmalloc_mblock();
375 new->state = IS_MBSTATE_DIRTY;
376 new->next = mblock->next;
380 mblock->num_records = recsblock;
382 mblock = mblock->next;
392 * Reorganize data in blocks for minimum block usage and quick access.
393 * Free surplus blocks.
394 * is_p_align and is_p_remap are alternative functions which trade off
395 * speed in updating versus optimum usage of disk blocks.
397 void is_p_remap(is_mtable *tab)
399 is_mbuf *mbufs, **bufpp, *mbp;
400 is_mblock *blockp, **blockpp;
401 int recsblock, blocks;
403 logf (LOG_DEBUG, "Remapping table.");
404 /* collect all data */
406 for (blockp = tab->data; blockp; blockp = blockp->next)
408 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
410 logf (LOG_FATAL, "Read-full failed in remap.");
413 *bufpp = blockp->data;
415 bufpp = &(*bufpp)->next;
418 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
419 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
423 recsblock = tab->num_records / blocks + 1;
424 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
426 blockpp = &tab->data;
427 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
432 *blockpp = xmalloc_mblock();
433 (*blockpp)->diskpos = -1;
435 (*blockpp)->data = mbp;
436 (*blockpp)->num_records = recsblock;
437 (*blockpp)->state = IS_MBSTATE_DIRTY;
438 blockpp = &(*blockpp)->next;
444 for (blockp = *blockpp; blockp; blockp = blockp->next)
445 if (blockp->diskpos >= 0)
446 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
447 xfree_mblocks(*blockpp);