Fixed a memory leak
[idzebra-moved-to-github.git] / isamc / merge-d.c
index 9f54c54..5dff477 100644 (file)
@@ -3,18 +3,21 @@
  * See the file LICENSE for details.
  * Heikki Levanto
  *
- * $Id: merge-d.c,v 1.6 1999-07-23 15:43:05 heikki Exp $
+ * $Id: merge-d.c,v 1.9 1999-08-17 19:46:53 heikki Exp $
  *
  * todo
- *  - merge when needed
+ *  - Input filter: Eliminate del-ins pairs, tell if only one entry (or none)
  *  - single-entry optimizing
  *  - study and optimize block sizes (later)
+ *  - Clean up the different ways diffs are handled in writing and reading
  *
  * bugs
- *  not yet ready
+ * - memory leak somewhere. 
+ *    - Some pp_opens do not get closed. 
+ *    - Diffinfo's get left behind.
  *
  * caveat
- *  There is aconfusion about the block addresses. cat or type is the category,
+ *  There is a confusion about the block addresses. cat or type is the category,
  *  pos or block is the block number. pp structures keep these two separate,
  *  and combine when saving the pp. The next pointer in the pp structure is
  *  also a combined address, but needs to be combined every time it is needed,
@@ -82,9 +85,9 @@ static void getDiffInfo(ISAMD_PP pp, int diffidx)
    int i=1;  /* [0] is used for the main data */
    int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
 
-   pp->diffinfo = xmalloc( diffsz );
+   pp->diffinfo = xmalloc( diffsz );  /*!!!*/ /* does not always get freed */
    memset(pp->diffinfo,'\0',diffsz);
-   if (pp->is->method->debug > 4)
+   if (pp->is->method->debug > 1)   //4
      logf(LOG_LOG,"isamd_getDiffInfo: %d (%d:%d), ix=%d mx=%d",
          isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, diffidx,maxinfos);
    assert(pp->diffbuf);
@@ -103,11 +106,22 @@ static void getDiffInfo(ISAMD_PP pp, int diffidx)
       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 (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize)
+      { /* bug-hunting, this fails on some long runs that log too much */
+         logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d", 
+                       __FILE__,__LINE__, diffidx);
+         logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
+                       pp->is->method->filecat[pp->cat].bsize);
+         logf(LOG_LOG,"pp=%d=%d:%d  pp->nx=%d=%d:%d",
+                       isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
+                       pp->next, isamd_type(pp->next), isamd_block(pp->next) );                      
+      }
+      assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
 
       if (0==pp->diffinfo[i].maxidx)
       {
-         if (pp->is->method->debug > 4)
+         if (pp->is->method->debug > 1)  //!!! 4
            logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
                diffidx, i);
          return; /* end marker */
@@ -140,7 +154,7 @@ static void loadDiffs(ISAMD_PP pp)
       isamd_read_block (pp->is, isamd_type(diffaddr), 
                                 isamd_block(diffaddr), pp->diffbuf );
       diffidx= ISAMD_BLOCK_OFFSET_N; 
-      if (pp->is->method->debug > 4)
+      if (pp->is->method->debug > 1) //4 !!!
         logf(LOG_LOG,"isamd_LoadDiffs: loaded block %d=%d:%d, d=%d ix=%d",
           diffaddr, isamd_type(diffaddr),isamd_block(diffaddr), 
           pp->diffs,diffidx);
@@ -149,7 +163,7 @@ static void loadDiffs(ISAMD_PP pp)
    { /* integrated block, just set the pointers */
      pp->diffbuf = pp->buf;
      diffidx = pp->size;  /* size is the beginning of diffs, diffidx the end*/
-      if (pp->is->method->debug > 4)
+      if (pp->is->method->debug > 1)  // 4 !!!
         logf(LOG_LOG,"isamd_LoadDiffs: within %d=%d:%d, d=%d ix=%d ",
           isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, pp->diffs, diffidx);
    }
@@ -160,10 +174,17 @@ static void loadDiffs(ISAMD_PP pp)
 void isamd_free_diffs(ISAMD_PP pp)
 {
   int i;
+  if (pp->is->method->debug > 4)
+     logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
   if (!pp->diffinfo) 
     return;
-  for (i=0;pp->diffinfo[i].decodeData;i++) 
-      (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);    
+  for (i=1;pp->diffinfo[i].decodeData;i++) 
+  {
+      if (pp->is->method->debug > 4)
+         logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i, 
+                       pp->diffinfo[i].decodeData);
+      (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
+  } 
   xfree(pp->diffinfo);
   if (pp->diffbuf != pp->buf)
     xfree (pp->diffbuf);  
@@ -303,14 +324,14 @@ static void isamd_reduceblock(ISAMD_PP pp)
 {
    if (pp->pos)
       return; /* existing block, do not touch */
-   if (pp->is->method->debug > 2)
+   if (pp->is->method->debug > 1)  // 2 !!!
      logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
        pp->pos, pp->cat, pp->size); 
    while ( ( pp->cat > 0 ) && (!pp->next) && 
            (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
       pp->cat--;
    pp->pos = isamd_alloc_block(pp->is, pp->cat);
-   if (pp->is->method->debug > 2)
+   if (pp->is->method->debug > 1) // 2 !!!
      logf(LOG_LOG,"isamd_reduce:  got  p=%d c=%d sz=%d",
        pp->pos, pp->cat, pp->size);    
 } /* reduceblock */
@@ -351,6 +372,7 @@ static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
 static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
 { /* reads the diff block (if separate) and sets diffidx right */
    ISAMD_PP pp=firstpp;
+   int i;
    int diffidx;   
    if (pp->diffs == 0)
    { /* no diffs yet, create room for them */
@@ -369,8 +391,11 @@ static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
       else
       { /* prepare to append diffs in head */
         diffidx = pp->size;
-        pp->diffs = diffidx *2 +0;  
-         if (pp->is->method->debug >3) 
+        pp->diffs = diffidx *2 +0; 
+        i=diffidx; /* clear the rest of the block. ??? */
+        if ( i < pp->is->method->filecat[pp->cat].bsize)
+            pp->buf[i]='\0';
+        if (pp->is->method->debug >1) //!!! 3
              logf(LOG_LOG,"isamd_appd: set up diffhead  (d=%d) %d=%d:%d ix=%d",
                    firstpp->diffs,
                    isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, 
@@ -383,7 +408,7 @@ static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
       { /* diffs in a separate block, load it */
         pp=isamd_pp_open(pp->is, isamd_addr(firstpp->diffs/2,pp->cat));
         diffidx = pp->offset= pp->size;
-        if (pp->is->method->debug >3) 
+        if (pp->is->method->debug >1)  // 3 !!! 
            logf(LOG_LOG,"isamd_appd: loaded diff (d=%d) %d=%d:%d ix=%d",
                  firstpp->diffs,
                  isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, 
@@ -392,7 +417,7 @@ static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
       else
       { /* diffs within the nead */
          diffidx= pp->diffs/2;
-         if (pp->is->method->debug >3) 
+         if (pp->is->method->debug >1)  // 3 !!! 
             logf(LOG_LOG,"isamd_appd: diffs in head d=%d %d=%d:%d ix=%d sz=%d",
                      pp->diffs,
                      isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, 
@@ -429,7 +454,7 @@ static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
       pp->pos=newblock; 
       pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N; 
       pp->next=0;
-      if (pp->is->method->debug >3)
+      if (pp->is->method->debug >1)  //!!! 3
          logf(LOG_LOG,"isamd_build: Alloc2 f=%d (%d:%d) n=%d(%d:%d)",
             isamd_addr(firstpp->pos,firstpp->cat), 
             firstpp->cat, firstpp->pos,
@@ -439,12 +464,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 >1)  //!!! 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 */
@@ -513,6 +544,7 @@ static int isamd_build_first_block(ISAMD is, ISAMD_I data)
    
    firstpp=pp=isamd_pp_open(is, isamd_addr(0,is->max_cat));
    firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
+   
    encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
    
    if (is->method->debug >3)
@@ -546,6 +578,7 @@ static int isamd_build_first_block(ISAMD is, ISAMD_I data)
             i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
       
    } /* i_more */
+   (*is->method->code_stop)(ISAMD_ENCODE, encoder_data);
 
    return save_both_pps( firstpp, pp );
 
@@ -574,30 +607,53 @@ 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: %s", 
-           diffidx,hexdump(&(readpp->diffbuf[diffidx]),8,0));
-  }
-  else
+  if ( (*p_firstpp)->diffs & 1 )
   { /* separate diff block in *p_pp */
      killblk = readpp->diffs/2;
-     diffidx = readpp->is->method->filecat[readpp->cat].bsize;
+     diffidx /*size*/ = 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);
      diffidx = ISAMD_BLOCK_OFFSET_N;
-     if (readpp->is->method->debug >3) {
+     if (readpp->is->method->debug >1) {  //!!! 3
          logf(LOG_LOG,"isamd_merge:separate diffs at ix=%d", 
                  diffidx);
          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 >1)  //!!! 3 
+         logf(LOG_LOG,"isamd_merge:local diffs at %d: %s", 
+           diffidx,hexdump(&(readpp->diffbuf[diffidx]),8,0));
+  }
+
   getDiffInfo(readpp,diffidx);
+  if (readpp->is->method->debug >3) 
+         logf(LOG_LOG,"isamd_merge: diffinfo=%p", readpp->diffinfo);
+  
+
+  if (killblk) 
+  {  /* we had a separate diff block, release it, we have copied the data */
+     isamd_release_block(readpp->is, readpp->cat, killblk);
+     isamd_pp_close (*p_pp);
+     if (readpp->is->method->debug >1)  // 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 >1)  //!!! 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);
@@ -607,25 +663,13 @@ static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
     /* pray that is not a delete as well... */
     r_key.sysno = 0;
     r_key.seqno = 0;
-     if (readpp->is->method->debug >3) 
+     if (readpp->is->method->debug >1) 
          logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
             readpp->numKeys);
     assert (readpp->numKeys == 0);
   }
 
-  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 );
-  }
-  killblk=readpp->pos;
-  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 );
-   
+
   /* 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;
@@ -638,12 +682,13 @@ 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 ( readpp->pos != killblk )
-     {
+     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 >1) //!!! 3 
+            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);
-        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=readpp->pos;
      }
 
@@ -663,14 +708,27 @@ static int merge ( ISAMD_PP *p_firstpp,   /* first pp of the chain */
   if (firstpp!=pp) 
   { /* the last data block is of no interest any more */
     save_last_pp(pp);
-    if (readpp->is->method->debug >3) 
+    if (readpp->is->method->debug >1) 
         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);
   }
+
+  if (readpp->is->method->debug >3) 
+        logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
+              isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
+              readpp->diffinfo);
+  isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
+
+  (*firstpp->is->method->code_stop)(ISAMD_ENCODE, encoder_data);
   
   *p_firstpp = firstpp; 
-    
+
+  if (readpp->is->method->debug >1)   //!!! 3
+      logf(LOG_LOG,"isamd_merge: merge ret  %d=%d:%d nx=%d=%d:%d d=%d=2*%d+%d",
+            isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+            pp->next, isamd_type(pp->next), isamd_block(pp->next),
+            pp->diffs, pp->diffs/2, pp->diffs &1 );
   return 0; 
   
 } /* merge */
@@ -704,10 +762,10 @@ static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
    int merge_rc;
 
    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",
+   if (is->method->debug >1)  //!!! 3
+      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; 
@@ -749,15 +807,26 @@ static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
                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);
-          (*is->method->code_reset)(encoder_data);
-          maxsize = is->method->filecat[pp->cat].bsize; 
-          difflenidx=diffidx;
-          diffidx+=sizeof(int);
+         /* 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 */
       
@@ -789,6 +858,8 @@ static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
    while ( (difflenidx-diffidx<=sizeof(int)) && (difflenidx<maxsize))
      pp->buf[difflenidx++]='\0';
 
+
+  (*firstpp->is->method->code_stop)(ISAMD_ENCODE, encoder_data);
    return save_both_pps( firstpp, pp );
 
 } /* append_diffs */
@@ -808,13 +879,32 @@ ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
    else
       retval = append_diffs(is,ipos,data);
 
+   if (0)  /*!*/ 
+   {
+     void *p1=xmalloc(100);
+     void *p2=xmalloc(100);
+     void *p3=xmalloc(100);
+     logf(LOG_LOG,"Traversing xmalloc stuff. p1=%p p2=%p p3=%p",p1,p2,p3);
+     xmalloc_trav("end of append"); /*!*/
+     assert(!"foo");
+   }
+   
    return retval;
 } /*  isamd_append */
 
 
 /*
  * $Log: merge-d.c,v $
- * Revision 1.6  1999-07-23 15:43:05  heikki
+ * Revision 1.9  1999-08-17 19:46:53  heikki
+ * Fixed a memory leak
+ *
+ * Revision 1.8  1999/08/07 11:30:59  heikki
+ * Bug fixing (still a mem leak somewhere)
+ *
+ * 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