X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=isam%2Fphysical.c;h=65bfe19023ba42120117b4a652de24fca858aa8d;hb=49f1c8c877affdeb9e9221047c48beed3f475afb;hp=afb5343e89caee89e2d565a95aba720aa02d119a;hpb=5bcaf79fcb3264b26d896eb2bdda907cf9fb9f5f;p=idzebra-moved-to-github.git diff --git a/isam/physical.c b/isam/physical.c index afb5343..65bfe19 100644 --- a/isam/physical.c +++ b/isam/physical.c @@ -1,10 +1,53 @@ /* - * Copyright (C) 1994, Index Data I/S + * Copyright (C) 1994-1999, Index Data * All rights reserved. * Sebastian Hammer, Adam Dickmeiss * * $Log: physical.c,v $ - * Revision 1.3 1994-09-26 17:11:31 quinn + * Revision 1.17 1999-11-30 13:48:04 adam + * Improved installation. Updated for inclusion of YAZ header files. + * + * Revision 1.16 1999/05/26 07:49:14 adam + * C++ compilation. + * + * Revision 1.15 1999/02/02 14:51:22 adam + * Updated WIN32 code specific sections. Changed header. + * + * Revision 1.14 1997/09/09 13:38:12 adam + * Partial port to WIN95/NT. + * + * Revision 1.13 1996/10/29 13:56:57 adam + * Include of zebrautl.h instead of alexutil.h. + * + * Revision 1.12 1996/03/20 16:17:11 quinn + * Bug hunting + * + * Revision 1.11 1996/03/20 13:29:17 quinn + * Bug-fix + * + * Revision 1.10 1996/03/19 19:22:44 quinn + * Fixed update-bug + * + * Revision 1.9 1996/02/06 10:19:57 quinn + * Attempt at fixing bug. Not all blocks were read before they were unlinked + * prior to a remap operation. + * + * Revision 1.8 1996/01/29 09:47:11 quinn + * Fixed mean little bug in the read-table code. + * + * Revision 1.7 1995/12/06 14:48:27 quinn + * Fixed some strange bugs. + * + * Revision 1.6 1995/09/04 12:33:47 adam + * Various cleanup. YAZ util used instead. + * + * Revision 1.5 1994/09/28 11:29:33 quinn + * Added cmp parameter. + * + * Revision 1.4 1994/09/27 20:03:53 quinn + * Seems relatively bug-free. + * + * Revision 1.3 1994/09/26 17:11:31 quinn * Trivial * * Revision 1.2 1994/09/26 17:06:36 quinn @@ -20,9 +63,11 @@ */ #include +#include +#include +#include #include -#include "memory.h" static int is_freestore_alloc(ISAM is, int type) { @@ -34,13 +79,14 @@ static int is_freestore_alloc(ISAM is, int type) if (bf_read(is->types[type].bf, tmp, 0, sizeof(tmp), &is->types[type].freelist) <=0) { - log(LOG_FATAL, "Failed to allocate block"); + logf (LOG_FATAL, "Failed to allocate block"); exit(1); } } else tmp = is->types[type].top++; + logf (LOG_DEBUG, "Allocating block #%d", tmp); return tmp; } @@ -48,11 +94,12 @@ static void is_freestore_free(ISAM is, int type, int block) { int tmp; + logf (LOG_DEBUG, "Releasing block #%d", block); tmp = is->types[type].freelist; is->types[type].freelist = block; if (bf_write(is->types[type].bf, block, 0, sizeof(tmp), &tmp) < 0) { - log(LOG_FATAL, "Failed to deallocate block."); + logf (LOG_FATAL, "Failed to deallocate block."); exit(1); } } @@ -64,7 +111,7 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block) is_mbuf *buf; assert(block->state == IS_MBSTATE_UNREAD); - block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE); + block->data = buf = xmalloc_mbuf(IS_MBUF_TYPE_LARGE); toread = tab->is->types[tab->pos_type].blocksize; if (toread > is_mbuf_size[buf->type]) { @@ -76,12 +123,13 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block) if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, 0, toread, buf->data) < 0) { - log(LOG_FATAL, "bfread failed."); + logf (LOG_FATAL, "bfread failed."); return -1; } /* extract header info */ buf->offset = 0; memcpy(&block->num_records, buf->data, sizeof(block->num_records)); + assert(block->num_records > 0); buf->offset += sizeof(block->num_records); memcpy(&block->nextpos, buf->data + buf->offset, sizeof(block->nextpos)); @@ -92,6 +140,9 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block) sizeof(tab->num_records)); buf->offset +=sizeof(tab->num_records); } + logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d", + block->diskpos, block->num_records, block->nextpos, + block == tab->data ? tab->num_records : -1); buf->num = (toread - buf->offset) / is_keysize(tab->is); if (buf->num >= block->num_records) { @@ -99,7 +150,7 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block) block->state = IS_MBSTATE_CLEAN; } else - block->bread = buf->num * is_keysize(tab->is); + block->bread = buf->offset + buf->num * is_keysize(tab->is); return 0; } @@ -110,7 +161,7 @@ int is_p_read_full(is_mtable *tab, is_mblock *block) if (block->state == IS_MBSTATE_UNREAD && is_p_read_partial(tab, block) < 0) { - log(LOG_FATAL, "partial read failed."); + logf (LOG_FATAL, "partial read failed."); return -1; } if (block->state == IS_MBSTATE_PARTIAL) @@ -129,7 +180,7 @@ int is_p_read_full(is_mtable *tab, is_mblock *block) if (bf_read(tab->is->types[tab->pos_type].bf, block->diskpos, block->bread, toread * is_keysize(tab->is), buf->data) < 0) { - log(LOG_FATAL, "bfread failed."); + logf (LOG_FATAL, "bfread failed."); return -1; } buf->offset = 0; @@ -137,7 +188,9 @@ int is_p_read_full(is_mtable *tab, is_mblock *block) dread += toread; block->bread += toread * is_keysize(tab->is); } + block->state = IS_MBSTATE_CLEAN; } + logf (LOG_DEBUG, "R: Block #%d contains %d records.", block->diskpos, block->num_records); return 0; } @@ -155,6 +208,8 @@ void is_p_sync(is_mtable *tab) type = &tab->is->types[tab->pos_type]; for (p = tab->data; p; p = p->next) { + if (p->state < IS_MBSTATE_DIRTY) + continue; /* make sure that blocks are allocated. */ if (p->diskpos < 0) p->diskpos = is_freestore_alloc(tab->is, tab->pos_type); @@ -166,6 +221,8 @@ void is_p_sync(is_mtable *tab) else p->nextpos = p->next->diskpos; } + else + p->nextpos = 0; sum = 0; memcpy(type->dbuf, &p->num_records, sizeof(p->num_records)); sum += sizeof(p->num_records); @@ -177,16 +234,22 @@ void is_p_sync(is_mtable *tab) sizeof(tab->num_records)); sum += sizeof(tab->num_records); } + logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos, + p->num_records); + assert(p->num_records > 0); for (b = p->data; b; b = b->next) { - memcpy(type->dbuf + sum, b->data + b->offset, v = b->num * - is_keysize(tab->is)); + logf(LOG_DEBUG, " buf: offset %d, keys %d, type %d, ref %d", + b->offset, b->num, b->type, b->refcount); + if ((v = b->num * is_keysize(tab->is)) > 0) + memcpy(type->dbuf + sum, b->data + b->offset, v); + sum += v; assert(sum <= type->blocksize); } if (bf_write(type->bf, p->diskpos, 0, sum, type->dbuf) < 0) { - log(LOG_FATAL, "Failed to write block."); + logf (LOG_FATAL, "Failed to write block."); exit(1); } } @@ -200,18 +263,22 @@ void is_p_unmap(is_mtable *tab) is_mblock *p; for (p = tab->data; p; p = p->next) + { if (p->diskpos >= 0) { is_freestore_free(tab->is, tab->pos_type, p->diskpos); p->diskpos = -1; } + } } static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize) { - is_mbuf *p = 0, **pp = &p, *new; + is_mbuf *p = 0, **pp = &p, *inew; int toget = *num; + if (!toget) + return 0; while (*mb && toget >= (*mb)->num) { toget -= (*mb)->num; @@ -222,13 +289,13 @@ static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize) } if (toget > 0 && *mb) { - new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL); - new->next = (*mb)->next; - (*mb)->next = new; - new->data = (*mb)->data; + inew = xmalloc_mbuf(IS_MBUF_TYPE_SMALL); + inew->next = (*mb)->next; + (*mb)->next = inew; + inew->data = (*mb)->data; (*mb)->refcount++; - new->offset = (*mb)->offset + toget * keysize; - new->num = (*mb)->num - toget; + inew->offset = (*mb)->offset + toget * keysize; + inew->num = (*mb)->num - toget; (*mb)->num = toget; *pp = *mb; *mb = (*mb)->next; @@ -246,14 +313,51 @@ static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize) */ void is_p_align(is_mtable *tab) { - is_mblock *mblock, *new; + is_mblock *mblock, *inew, *last = 0, *next; is_mbuf *mbufs, *mbp; int blocks, recsblock; - log(LOG_DEBUG, "Realigning table."); - for (mblock = tab->data; mblock; mblock = mblock->next) + logf (LOG_DEBUG, "Realigning table."); + for (mblock = tab->data; mblock; mblock = next) { - if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records > + next = mblock->next; + if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records == 0) + { + if (last) + { + last->next = mblock->next; + last->state = IS_MBSTATE_DIRTY; + next = mblock->next; + } + else + { + next = tab->data->next; + if (next) + { + if (next->state < IS_MBSTATE_CLEAN) + { + if (is_p_read_full(tab, next) < 0) + { + logf(LOG_FATAL, "Error during re-alignment"); + abort(); + } + if (next->nextpos && !next->next) + { + next->next = xmalloc_mblock(); + next->next->diskpos = next->nextpos; + next->next->state = IS_MBSTATE_UNREAD; + next->next->data = 0; + } + } + next->state = IS_MBSTATE_DIRTY; /* force re-process */ + tab->data = next; + } + } + if (mblock->diskpos >= 0) + is_freestore_free(tab->is, tab->pos_type, mblock->diskpos); + xrelease_mblock(mblock); + } + else if (mblock->state == IS_MBSTATE_DIRTY && mblock->num_records > (mblock == tab->data ? tab->is->types[tab->pos_type].max_keys_block0 : tab->is->types[tab->pos_type].max_keys_block)) @@ -268,18 +372,25 @@ void is_p_align(is_mtable *tab) recsblock = 1; mbufs = mblock->data; while ((mbp = mbuf_takehead(&mbufs, &recsblock, - is_keysize(tab->is)))) + is_keysize(tab->is))) && recsblock) { - new = xmalloc_mblock(); - new->diskpos = -1; - new->state = IS_MBSTATE_DIRTY; - new->next = mblock->next; - mblock->next = new; + if (mbufs) + { + inew = xmalloc_mblock(); + inew->diskpos = -1; + inew->state = IS_MBSTATE_DIRTY; + inew->next = mblock->next; + mblock->next = inew; + } mblock->data = mbp; mblock->num_records = recsblock; + last = mblock; mblock = mblock->next; } + next = mblock; } + else + last = mblock; } } @@ -295,11 +406,16 @@ void is_p_remap(is_mtable *tab) is_mblock *blockp, **blockpp; int recsblock, blocks; - log(LOG_DEBUG, "Remapping table."); + logf (LOG_DEBUG, "Remapping table."); /* collect all data */ bufpp = &mbufs; for (blockp = tab->data; blockp; blockp = blockp->next) { + if (blockp->state < IS_MBSTATE_CLEAN && is_m_read_full(tab, blockp) < 0) + { + logf (LOG_FATAL, "Read-full failed in remap."); + exit(1); + } *bufpp = blockp->data; while (*bufpp) bufpp = &(*bufpp)->next; @@ -308,11 +424,14 @@ void is_p_remap(is_mtable *tab) blocks = tab->num_records / tab->is->types[tab->pos_type].nice_keys_block; if (tab->num_records % tab->is->types[tab->pos_type].nice_keys_block) blocks++; - recsblock = tab->num_records / blocks; - if (recsblock < 1) - recsblock = 1; + if (blocks == 0) + blocks = 1; + recsblock = tab->num_records / blocks + 1; + if (recsblock > tab->is->types[tab->pos_type].nice_keys_block) + recsblock--; blockpp = &tab->data; - while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is)))) + while ((mbp = mbuf_takehead(&mbufs, &recsblock, is_keysize(tab->is))) && + recsblock) { if (!*blockpp) { @@ -324,4 +443,14 @@ void is_p_remap(is_mtable *tab) (*blockpp)->state = IS_MBSTATE_DIRTY; blockpp = &(*blockpp)->next; } + if (mbp) + xfree_mbufs(mbp); + if (*blockpp) + { + for (blockp = *blockpp; blockp; blockp = blockp->next) + if (blockp->diskpos >= 0) + is_freestore_free(tab->is, tab->pos_type, blockp->diskpos); + xfree_mblocks(*blockpp); + *blockpp = 0; + } }