isam-d seems to be working.
[idzebra-moved-to-github.git] / isamc / merge-d.c
index 6d1948c..4d43713 100644 (file)
@@ -3,7 +3,7 @@
  * See the file LICENSE for details.
  * Heikki Levanto
  *
- * $Id: merge-d.c,v 1.5 1999-07-23 13:58:52 heikki Exp $
+ * $Id: merge-d.c,v 1.7 1999-08-04 14:21:18 heikki Exp $
  *
  * todo
  *  - merge when needed
@@ -100,9 +100,10 @@ static void getDiffInfo(ISAMD_PP pp, int diffidx)
       }
       memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
 
-      if (pp->is->method->debug > 4)
+      if (pp->is->method->debug > 5)
         logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
           pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
+      assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize);
 
       if (0==pp->diffinfo[i].maxidx)
       {
@@ -438,12 +439,18 @@ static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
    { /* it was not the first block */
       newblock = isamd_alloc_block(pp->is, firstpp->cat);
       pp->next = isamd_addr(newblock,firstpp->cat);
+      if (pp->is->method->debug >3)
+         logf(LOG_LOG,"isamd_build: Alloc new after p=%d=%d:%d  n=%d=%d:%d",
+            isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+            isamd_addr(newblock,pp->cat), pp->cat, newblock );
       isamd_buildlaterblock(pp);
       isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
       pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
       pp->next=0;
       pp->cat = firstpp->cat;
-      pp->pos = isamd_block(firstpp->next);
+//      pp->pos = isamd_block(firstpp->next); /* ???? FIRSTPP ???? */
+      pp->pos = newblock;
+      pp->cat = firstpp->cat; /* is already, never mind */
    }
   return pp;
 } /* get_new_main_block */
@@ -558,7 +565,7 @@ static int isamd_build_first_block(ISAMD is, ISAMD_I data)
  
 static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
                    ISAMD_PP *p_pp,        /* diff block */
-                   struct it_key *p_key )
+                   struct it_key *p_key ) /* not used yet */
 {
   ISAMD_PP readpp = *p_firstpp;
   int diffidx;  
@@ -573,30 +580,51 @@ static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
   /* set up diffs as they should be for reading */
   readpp->offset= ISAMD_BLOCK_OFFSET_1; 
   
-  if (*p_pp == *p_firstpp)
-  { /* integrated diffs */
-     diffidx=readpp->size;
-     readpp->diffs = diffidx*2+0;
-     readpp->diffbuf=readpp->buf;  /*? does this get freed right ???  */
-     if (readpp->is->method->debug >3) 
-         logf(LOG_LOG,"isamd_merge:local diffs at %d", diffidx);
-  }
-  else
+//  if (*p_pp == *p_firstpp)  /* not the way to check it !! */
+  if ( (*p_firstpp)->diffs & 1 )
   { /* separate diff block in *p_pp */
      killblk = readpp->diffs/2;
      diffidx = readpp->is->method->filecat[readpp->cat].bsize;
      readpp->diffbuf= xmalloc( diffidx); /* copy diffs to where read wants*/
-     memcpy( &readpp->diffbuf, &((*p_pp)->buf[0]), diffidx);
+     memcpy( readpp->diffbuf, &((*p_pp)->buf[0]), diffidx);
      diffidx = ISAMD_BLOCK_OFFSET_N;
-     if (readpp->is->method->debug >3) 
+     if (readpp->is->method->debug >3) {
          logf(LOG_LOG,"isamd_merge:separate diffs at ix=%d", 
                  diffidx);
-     if (readpp->is->method->debug >3) 
          logf(LOG_LOG,"isamd_merge: dbuf=%p (from %p) pp=%p", 
                   readpp->diffbuf, &((*p_pp)->buf[0]), (*p_pp) );
+     }
   }
+  else
+  { /* integrated diffs */
+     assert ( *p_pp == *p_firstpp );  /* can only be in the first block */
+     diffidx=readpp->size;
+     readpp->diffs = diffidx*2+0;
+     readpp->diffbuf=readpp->buf;  /*? does this get freed right??  */
+     if (readpp->is->method->debug >3) 
+         logf(LOG_LOG,"isamd_merge:local diffs at %d: %s", 
+           diffidx,hexdump(&(readpp->diffbuf[diffidx]),8,0));
+  }
+
   getDiffInfo(readpp,diffidx);
 
+  if (killblk) 
+  {  /* we had a separate diff block, release it, we have the data */
+     isamd_release_block(readpp->is, readpp->cat, killblk);
+     if (readpp->is->method->debug >3) 
+         logf(LOG_LOG,"isamd_merge: released diff block %d=%d:%d",
+              isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
+  }
+
+
+  /* release our data block. Do before reading, when pos is stable! */
+  killblk=readpp->pos;
+  assert(killblk);
+  isamd_release_block(readpp->is, readpp->cat, killblk);
+  if (readpp->is->method->debug >3) 
+      logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
+               isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
+
   r_ptr= (char *) &r_key;
   r_more = isamd_read_item( readpp, &r_ptr);
   if (!r_more)  
@@ -611,20 +639,7 @@ static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
     assert (readpp->numKeys == 0);
   }
 
-  if (killblk) 
-  {
-     isamd_release_block(readpp->is, isamd_type(killblk), isamd_block(killblk));
-     /* we have the data, let the block go */
-     if (readpp->is->method->debug >3) 
-         logf(LOG_LOG,"isamd_merge: released diff block %d (%d:%d)",
-              killblk, isamd_type(killblk), isamd_block(killblk) );
-  }
-  killblk=isamd_addr(readpp->pos, readpp->cat);
-  isamd_release_block(readpp->is, isamd_type(killblk), isamd_block(killblk));
-  if (readpp->is->method->debug >3) 
-      logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
-            killblk, isamd_type(killblk), isamd_block(killblk) );
-   
+
   /* set up the new blocks for simple writing */
   firstpp=pp=isamd_pp_open(readpp->is,isamd_addr(0, readpp->is->max_cat));
   firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
@@ -637,13 +652,14 @@ static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
            r_key.sysno, r_key.seqno );
      pp= append_main_item(firstpp, pp, &r_key, encoder_data);
 
-     if ( isamd_addr(readpp->pos, readpp->cat) != killblk )
-     {
-        isamd_release_block(readpp->is, readpp->cat, readpp->pos);
+     if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
+     {  /* pos can get to 0 at end of main seq, if still diffs left...*/
         if (readpp->is->method->debug >3) 
-            logf(LOG_LOG,"isamd_merge: released data block %d (%d:%d)",
-                  killblk, isamd_type(killblk), isamd_block(killblk) );
-        killblk=isamd_addr(readpp->pos, readpp->cat);
+            logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
+                isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
+                isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
+        isamd_release_block(readpp->is, readpp->cat, readpp->pos);
+        killblk=readpp->pos;
      }
 
      /* (try to) read next item */
@@ -657,10 +673,14 @@ static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
 
   /* set things up so that merge can continue */
   isamd_reduceblock(firstpp);
+  firstpp->diffs=0; 
 
   if (firstpp!=pp) 
   { /* the last data block is of no interest any more */
     save_last_pp(pp);
+    if (readpp->is->method->debug >3) 
+        logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
+              isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
     isamd_pp_close(pp);
   }
   
@@ -700,9 +720,9 @@ static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
 
    firstpp=isamd_pp_open(is, ipos);
    if (is->method->debug >4)
-      logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d d=%d=%d*2+%d",
+      logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d d=%d=%d*2+%d nk=%d",
         ipos, isamd_type(ipos), isamd_block(ipos),
-        firstpp->diffs, firstpp->diffs/2, firstpp->diffs & 1);
+        firstpp->diffs, firstpp->diffs/2, firstpp->diffs & 1, firstpp->numKeys);
    pp=read_diff_block(firstpp, &diffidx);
    encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
    maxsize = is->method->filecat[pp->cat].bsize; 
@@ -739,15 +759,31 @@ static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
          if (is->method->debug >3)
             logf(LOG_LOG,"isamd_appd: block full (ix=%d mx=%d lix=%d)",
                diffidx, maxsize, difflenidx);
-         if (is->method->debug >3)
+         if (is->method->debug >5)
             logf(LOG_LOG,"isamd_appd: block pp=%p buf=%p [%d]:%s",
-               pp, pp->buf, difflenidx, hexdump(&pp->buf[difflenidx],8,0));
+               pp, pp->buf, 
+               difflenidx, hexdump(&pp->buf[difflenidx],8,0));
          merge_rc = merge (&firstpp, &pp, &i_key);
-         if (merge_rc)
+         if (0!=merge_rc)
            return merge_rc;  /* merge handled them all ! */
 
-          /* set things up so we can continue */
-          pp = read_diff_block(firstpp, &diffidx);
+         /* set things up so we can continue */
+         pp = read_diff_block(firstpp, &diffidx);
+         (*is->method->code_reset)(encoder_data);
+         maxsize = is->method->filecat[pp->cat].bsize; 
+         difflenidx=diffidx;
+         diffidx+=sizeof(int);
+
+         /* code the current input key again */
+         c_ptr=codebuff;
+         i_ptr=i_item;
+         (*is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
+         codelen = c_ptr - codebuff;
+         assert ( (codelen<128) && (codelen>0));
+         if (is->method->debug >3)
+            logf(LOG_LOG,"isamd_appd: recoded into %d: %s (nk=%d) (ix=%d)",
+                codelen, hexdump(codebuff, codelen,hexbuff), 
+                firstpp->numKeys,diffidx);
           
       } /* block full */
       
@@ -804,7 +840,13 @@ ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
 
 /*
  * $Log: merge-d.c,v $
- * Revision 1.5  1999-07-23 13:58:52  heikki
+ * Revision 1.7  1999-08-04 14:21:18  heikki
+ * isam-d seems to be working.
+ *
+ * Revision 1.6  1999/07/23 15:43:05  heikki
+ * Hunted a few bugs in isam-d. Still crashes on the long test run
+ *
+ * Revision 1.5  1999/07/23 13:58:52  heikki
  * merged closer to working, still fails on filling a separate, large block
  *
  * Revision 1.4  1999/07/21 14:53:55  heikki