Towards GPL
[idzebra-moved-to-github.git] / isam / physical.c
index e02598e..48b04c5 100644 (file)
@@ -1,28 +1,26 @@
-/*
- * Copyright (C) 1994, Index Data I/S 
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Log: physical.c,v $
- * 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
- * Back again...
- *
- * Revision 1.1  1994/09/26  16:07:57  quinn
- * Most of the functionality in place.
- *
- */
+/* $Id: physical.c,v 1.18 2002-08-02 19:26:56 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 
 /*
  * This module handles the representation of tables in the bfiles.
@@ -30,7 +28,9 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <string.h>
 
+#include <yaz/log.h>
 #include <isam.h>
 
 static int is_freestore_alloc(ISAM is, int type)
@@ -75,7 +75,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])
     {
@@ -93,6 +93,7 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block)
     /* 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));
@@ -103,6 +104,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)
     {
@@ -110,7 +114,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;
 }
 
@@ -148,6 +152,7 @@ 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;
@@ -193,10 +198,16 @@ 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);
        }
@@ -205,7 +216,6 @@ void is_p_sync(is_mtable *tab)
            logf (LOG_FATAL, "Failed to write block.");
            exit(1);
        }
-       logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos, p->num_records);
     }
 }
 
@@ -217,16 +227,18 @@ 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)
@@ -241,13 +253,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;
@@ -265,7 +277,7 @@ static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
  */
 void is_p_align(is_mtable *tab)
 {
-    is_mblock *mblock, *new, *last = 0, *next;
+    is_mblock *mblock, *inew, *last = 0, *next;
     is_mbuf *mbufs, *mbp;
     int blocks, recsblock;
 
@@ -283,9 +295,27 @@ void is_p_align(is_mtable *tab)
            }
            else
            {
-               tab->data = tab->data->next;
-               tab->data->state = IS_MBSTATE_DIRTY;
-               next = tab->data;
+               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);
@@ -310,11 +340,11 @@ void is_p_align(is_mtable *tab)
            {
                if (mbufs)
                {
-                   new = xmalloc_mblock();
-                   new->diskpos = -1;
-                   new->state = IS_MBSTATE_DIRTY;
-                   new->next = mblock->next;
-                   mblock->next = new;
+                   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;