2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-05-26 07:49:14 adam
10 * Revision 1.15 1999/02/02 14:51:22 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.14 1997/09/09 13:38:12 adam
14 * Partial port to WIN95/NT.
16 * Revision 1.13 1996/10/29 13:56:57 adam
17 * Include of zebrautl.h instead of alexutil.h.
19 * Revision 1.12 1996/03/20 16:17:11 quinn
22 * Revision 1.11 1996/03/20 13:29:17 quinn
25 * Revision 1.10 1996/03/19 19:22:44 quinn
28 * Revision 1.9 1996/02/06 10:19:57 quinn
29 * Attempt at fixing bug. Not all blocks were read before they were unlinked
30 * prior to a remap operation.
32 * Revision 1.8 1996/01/29 09:47:11 quinn
33 * Fixed mean little bug in the read-table code.
35 * Revision 1.7 1995/12/06 14:48:27 quinn
36 * Fixed some strange bugs.
38 * Revision 1.6 1995/09/04 12:33:47 adam
39 * Various cleanup. YAZ util used instead.
41 * Revision 1.5 1994/09/28 11:29:33 quinn
42 * Added cmp parameter.
44 * Revision 1.4 1994/09/27 20:03:53 quinn
45 * Seems relatively bug-free.
47 * Revision 1.3 1994/09/26 17:11:31 quinn
50 * Revision 1.2 1994/09/26 17:06:36 quinn
53 * Revision 1.1 1994/09/26 16:07:57 quinn
54 * Most of the functionality in place.
59 * This module handles the representation of tables in the bfiles.
69 static int is_freestore_alloc(ISAM is, int type)
73 if (is->types[type].freelist >= 0)
75 tmp = is->types[type].freelist;
76 if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp),
77 &is->types[type].freelist) <=0)
79 logf (LOG_FATAL, "Failed to allocate block");
84 tmp = is->types[type].top++;
86 logf (LOG_DEBUG, "Allocating block #%d", tmp);
90 static void is_freestore_free(ISAM is, int type, int block)
94 logf (LOG_DEBUG, "Releasing block #%d", block);
95 tmp = is->types[type].freelist;
96 is->types[type].freelist = block;
97 if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0)
99 logf (LOG_FATAL, "Failed to deallocate block.");
104 /* this code must be modified to handle an index */
105 int is_p_read_partial(is_mtable *tab, is_mblock *block)
110 assert(block->state == IS_MBSTATE_UNREAD);
111 block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
112 toread = tab->is->types[tab->pos_type].blocksize;
113 if (toread > is_mbuf_size[buf->type])
115 toread = is_mbuf_size[buf->type];
116 block->state = IS_MBSTATE_PARTIAL;
119 block->state = IS_MBSTATE_CLEAN;
120 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread,
123 logf (LOG_FATAL, "bfread failed.");
126 /* extract header info */
128 memcpy(&block->num_records, buf->data, sizeof(block->num_records));
129 assert(block->num_records > 0);
130 buf->offset += sizeof(block->num_records);
131 memcpy(&block->nextpos, buf->data + buf->offset,
132 sizeof(block->nextpos));
133 buf->offset += sizeof(block->nextpos);
134 if (block == tab->data) /* first block */
136 memcpy(&tab->num_records, buf->data + buf->offset,
137 sizeof(tab->num_records));
138 buf->offset +=sizeof(tab->num_records);
140 logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d",
141 block->diskpos, block->num_records, block->nextpos,
142 block == tab->data ? tab->num_records : -1);
143 buf->num = (toread - buf->offset) / is_keysize(tab->is);
144 if (buf->num >= block->num_records)
146 buf->num = block->num_records;
147 block->state = IS_MBSTATE_CLEAN;
150 block->bread = buf->offset + buf->num * is_keysize(tab->is);
154 int is_p_read_full(is_mtable *tab, is_mblock *block)
159 if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0)
161 logf (LOG_FATAL, "partial read failed.");
164 if (block->state == IS_MBSTATE_PARTIAL)
167 dread = block->data->num;
168 while (dread < block->num_records)
170 buf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
173 toread = is_mbuf_size[buf->type] / is_keysize(tab->is);
174 if (toread > block->num_records - dread)
175 toread = block->num_records - dread;
177 if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread *
178 is_keysize(tab->is), buf->data) < 0)
180 logf (LOG_FATAL, "bfread failed.");
186 block->bread += toread * is_keysize(tab->is);
188 block->state = IS_MBSTATE_CLEAN;
190 logf (LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records);
195 * write dirty blocks to bfile.
196 * Allocate blocks as necessary.
198 void is_p_sync(is_mtable *tab)
203 isam_blocktype *type;
205 type = &tab->is->types[tab->pos_type];
206 for (p = tab->data; p; p = p->next)
208 if (p->state < IS_MBSTATE_DIRTY)
210 /* make sure that blocks are allocated. */
212 p->diskpos = is_freestore_alloc(tab->is, tab->pos_type);
215 if (p->next->diskpos < 0)
216 p->nextpos = p->next->diskpos = is_freestore_alloc(tab->is,
219 p->nextpos = p->next->diskpos;
224 memcpy(type->dbuf, &p->num_records, sizeof(p->num_records));
225 sum += sizeof(p->num_records);
226 memcpy(type->dbuf + sum, &p->nextpos, sizeof(p->nextpos));
227 sum += sizeof(p->nextpos);
228 if (p == tab->data) /* first block */
230 memcpy(type->dbuf + sum, &tab->num_records,
231 sizeof(tab->num_records));
232 sum += sizeof(tab->num_records);
234 logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos,
236 assert(p->num_records > 0);
237 for (b = p->data; b; b = b->next)
239 logf(LOG_DEBUG, " buf: offset %d, keys %d, type %d, ref %d",
240 b->offset, b->num, b->type, b->refcount);
241 if ((v = b->num * is_keysize(tab->is)) > 0)
242 memcpy(type->dbuf + sum, b->data + b->offset, v);
245 assert(sum <= type->blocksize);
247 if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0)
249 logf (LOG_FATAL, "Failed to write block.");
256 * Free all disk blocks associated with table.
258 void is_p_unmap(is_mtable *tab)
262 for (p = tab->data; p; p = p->next)
266 is_freestore_free(tab->is, tab->pos_type, p->diskpos);
272 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
274 is_mbuf *p = 0, **pp = &p, *inew;
279 while (*mb && toget >= (*mb)->num)
287 if (toget > 0 && *mb)
289 inew = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
290 inew->next = (*mb)->next;
292 inew->data = (*mb)->data;
294 inew->offset = (*mb)->offset + toget * keysize;
295 inew->num = (*mb)->num - toget;
307 * Split up individual blocks which have grown too large.
308 * is_p_align and is_p_remap are alternative functions which trade off
309 * speed in updating versus optimum usage of disk blocks.
311 void is_p_align(is_mtable *tab)
313 is_mblock *mblock, *inew, *last = 0, *next;
314 is_mbuf *mbufs, *mbp;
315 int blocks, recsblock;
317 logf (LOG_DEBUG, "Realigning table.");
318 for (mblock = tab->data; mblock; mblock = next)
321 if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0)
325 last->next = mblock->next;
326 last->state = IS_MBSTATE_DIRTY;
331 next = tab->data->next;
334 if (next->state < IS_MBSTATE_CLEAN)
336 if (is_p_read_full(tab, next) < 0)
338 logf(LOG_FATAL, "Error during re-alignment");
341 if (next->nextpos && !next->next)
343 next->next = xmalloc_mblock();
344 next->next->diskpos = next->nextpos;
345 next->next->state = IS_MBSTATE_UNREAD;
346 next->next->data = 0;
349 next->state = IS_MBSTATE_DIRTY; /* force re-process */
353 if (mblock->diskpos >= 0)
354 is_freestore_free(tab->is, tab->pos_type, mblock->diskpos);
355 xrelease_mblock(mblock);
357 else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records >
358 (mblock == tab->data ?
359 tab->is->types[tab->pos_type].max_keys_block0 :
360 tab->is->types[tab->pos_type].max_keys_block))
362 blocks = tab->num_records /
363 tab->is->types[tab->pos_type].nice_keys_block;
364 if (tab->num_records %
365 tab->is->types[tab->pos_type].nice_keys_block)
367 recsblock = tab->num_records / blocks;
370 mbufs = mblock->data;
371 while ((mbp = mbuf_takehead(&mbufs, &recsblock,
372 is_keysize(tab->is))) && recsblock)
376 inew = xmalloc_mblock();
378 inew->state = IS_MBSTATE_DIRTY;
379 inew->next = mblock->next;
383 mblock->num_records = recsblock;
385 mblock = mblock->next;
395 * Reorganize data in blocks for minimum block usage and quick access.
396 * Free surplus blocks.
397 * is_p_align and is_p_remap are alternative functions which trade off
398 * speed in updating versus optimum usage of disk blocks.
400 void is_p_remap(is_mtable *tab)
402 is_mbuf *mbufs, **bufpp, *mbp;
403 is_mblock *blockp, **blockpp;
404 int recsblock, blocks;
406 logf (LOG_DEBUG, "Remapping table.");
407 /* collect all data */
409 for (blockp = tab->data; blockp; blockp = blockp->next)
411 if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0)
413 logf (LOG_FATAL, "Read-full failed in remap.");
416 *bufpp = blockp->data;
418 bufpp = &(*bufpp)->next;
421 blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block;
422 if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block)
426 recsblock = tab->num_records / blocks + 1;
427 if (recsblock > tab->is->types[tab->pos_type].nice_keys_block)
429 blockpp = &tab->data;
430 while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) &&
435 *blockpp = xmalloc_mblock();
436 (*blockpp)->diskpos = -1;
438 (*blockpp)->data = mbp;
439 (*blockpp)->num_records = recsblock;
440 (*blockpp)->state = IS_MBSTATE_DIRTY;
441 blockpp = &(*blockpp)->next;
447 for (blockp = *blockpp; blockp; blockp = blockp->next)
448 if (blockp->diskpos >= 0)
449 is_freestore_free(tab->is, tab->pos_type, blockp->diskpos);
450 xfree_mblocks(*blockpp);