+static int save_first_pp ( ISAMD_PP firstpp)
+{
+ isamd_buildfirstblock(firstpp);
+ isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
+ return isamd_addr(firstpp->pos,firstpp->cat);
+}
+
+
+static void save_last_pp (ISAMD_PP pp)
+{
+ pp->next = 0;/* just to be sure */
+ isamd_buildlaterblock(pp);
+ isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
+}
+
+#ifdef UNUSED
+static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
+{
+ /* order of things: Better to save firstpp first, if there are just two */
+ /* blocks, but last if there are blocks in between, as these have already */
+ /* been saved... optimise later (that's why this is in its own func...*/
+ int retval = save_first_pp(firstpp);
+ if (firstpp!=pp){
+ save_last_pp(pp);
+ isamd_pp_close(pp);
+ }
+ isamd_pp_close(firstpp);
+ return retval;
+} /* save_both_pps */
+#endif
+
+
+
+/***************************************************************
+ * Diffblock handling
+ ***************************************************************/
+
+void isamd_free_diffs(ISAMD_PP pp)
+{
+ int i;
+ if (pp->is->method->debug > 5)
+ logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
+ if (!pp->diffinfo)
+ return;
+ for (i=0;pp->diffinfo[i].difftype!=DT_NONE;i++)
+ if(pp->diffinfo[i].decodeData)
+ {
+ if (pp->is->method->debug > 8)
+ 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);
+ pp->diffbuf=0;
+ pp->diffinfo=0;
+} /* isamd_free_diffs */
+
+
+static void getDiffInfo(ISAMD_PP pp )
+{ /* builds the diff info structures from a diffblock */
+ int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +2;
+ /* Each diff takes at least 5 bytes. Probably more, but this is safe */
+ int i=1; /* [0] is used for the main data, [n+1] for merge inputs */
+ int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
+ int maxsz = pp->is->method->filecat[pp->is->max_cat].bsize;
+ int diffidx = ISAMD_BLOCK_OFFSET_1;
+
+ pp->diffinfo = xmalloc( diffsz );
+ pp->offset = pp->size+1; /* used this block up */
+ memset(pp->diffinfo,'\0',diffsz);
+ if (pp->is->method->debug > 5)
+ logf(LOG_LOG,"isamd_getDiffInfo: %d=%d:%d->%d, ix=%d mx=%d",
+ isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->next,
+ diffidx,maxinfos);
+
+ /* duplicate the buffer for diffs */
+ /* (so that we can read the next real buffer(s) */
+ assert(0==pp->diffbuf);
+ pp->diffbuf=xmalloc(maxsz);
+ memcpy(pp->diffbuf, pp->buf, maxsz);
+
+ pp->diffinfo[0].maxidx=-1; /* mark as special */
+ pp->diffinfo[0].difftype=DT_MAIN;
+
+ while (i<maxinfos)
+ {
+ if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
+ {
+ if (pp->is->method->debug > 5)
+ logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
+ diffidx, i);
+ return; /* whole block done */
+ }
+ memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
+ pp->diffinfo[i].difftype=DT_DIFF;
+ if (pp->is->method->debug > 5)
+ logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
+ pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
+
+ if ( (pp->is->method->debug > 0) &&
+ (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize) )
+ {
+ 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 > 5) //!!! 4
+ logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
+ diffidx, i);
+ return; /* end marker */
+ }
+ diffidx += sizeof(int);
+ pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
+ pp->diffinfo[i].diffidx = diffidx;
+ if (pp->is->method->debug > 5)
+ logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
+ i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
+ hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
+ diffidx=pp->diffinfo[i].maxidx;
+ if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
+ return; /* whole block done */
+ ++i;
+ }
+ assert (!"too many diff sequences in the block");
+}
+
+/***************************************************************
+ * Main block operations
+ ***************************************************************/
+
+
+static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
+{ /* allocates a new block for the main data, and links it in */
+ int newblock;
+ if (0 == firstpp->next)
+ { /* special case, pp not yet allocated. */
+ /*Started as largest size, that's fine */
+ pp->pos = isamd_alloc_block(pp->is,pp->cat);
+ firstpp->next = isamd_addr(pp->pos,pp->cat);
+ if (pp->is->method->debug >3)
+ logf(LOG_LOG,"isamd_build: Alloc 1. dblock p=%d=%d:%d",
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
+ }
+ newblock=isamd_alloc_block(pp->is,pp->cat);
+ pp->next=isamd_addr(newblock,pp->cat);
+ isamd_buildlaterblock(pp);
+ isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
+ if (pp->is->method->debug >3)
+ logf(LOG_LOG,"isamd_build: Alloc nxt %d=%d:%d -> %d=%d:%d",
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+ isamd_addr(newblock,pp->cat), pp->cat, newblock);
+ pp->next=0;
+ pp->pos=newblock;
+ pp->size=pp->offset=ISAMD_BLOCK_OFFSET_N;
+ return pp;
+} /* get_new_main_block */
+
+
+static ISAMD_PP append_main_item(ISAMD_PP firstpp,
+ ISAMD_PP pp,
+ struct it_key *i_key)
+{ /* appends one item in the main data block, allocates new if needed */
+ char *i_item= (char *) i_key; /* same as char */
+ char *i_ptr=i_item;
+ char codebuff[128];
+ char *c_ptr = codebuff;
+ int codelen;
+ char hexbuff[64];
+
+ int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
+
+ c_ptr=codebuff;
+ i_ptr=i_item;
+ (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
+ &c_ptr, &i_ptr);
+ codelen = c_ptr - codebuff;
+ assert ( (codelen<128) && (codelen>0));
+ if (pp->is->method->debug >7)
+ logf(LOG_LOG,"isamd:build: coded %s nk=%d,ofs=%d-%d",
+ hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1,
+ pp->offset, pp->offset+codelen);
+
+ if (pp->offset + codelen > maxsize )
+ { /* oops, block full - get a new one */
+ pp = get_new_main_block( firstpp, pp );
+ /* reset encoging and code again */
+ (*pp->is->method->code_reset)(pp->decodeClientData);
+ c_ptr=codebuff;
+ i_ptr=i_item;
+ (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
+ &c_ptr, &i_ptr);
+ codelen = c_ptr - codebuff;
+ assert ( (codelen<128) && (codelen>0));
+ if (pp->is->method->debug >7)
+ logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
+ hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
+ } /* block full */
+
+ assert (pp->offset + codelen <= maxsize );
+
+ /* write the data into pp, now we must have room */
+ memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
+ pp->offset += codelen;
+ pp->size += codelen;
+ firstpp->numKeys++;
+ /* clear the next 4 bytes in block, to avoid confusions with diff lens */
+ /* dirty, it should not be done here, but something slips somewhere, and */
+ /* I hope this fixes it... - Heikki */
+ codelen = pp->offset;
+ while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
+ pp->buf[codelen++] = '\0';
+ return pp;
+} /* append_main_item */
+
+
+/***************************************************************
+ * Read with merge
+ ***************************************************************/
+
+/* Reads one item and corrects for the diffs, if any */
+/* return 1 for ok, 0 for eof */
+int isamd_read_item_merge (
+ ISAMD_PP pp,
+ char **dst,
+ struct it_key *p_key, /* the data item that didn't fit*/
+ /* ISAMD_I data) */ /* more input data comes here */
+ FILTER filt) /* more input data comes here */
+{ /* The last two args can be null for ordinary reads */
+ char *keyptr;
+ char *codeptr;
+ char *codestart;
+ int winner=0; /* which diff holds the day */
+ int i; /* looping diffs */
+ int cmp;
+ int retry=1;
+ int oldoffs;
+ int rc;
+
+ if (!pp->diffinfo)
+ { /* first time */
+ getDiffInfo(pp);
+
+ for(i=1; pp->diffinfo[i].difftype!=DT_NONE; i++)
+ ; /* find last diff */
+ if (p_key)
+ { /* we have an extra item to inject into the merge */
+ if (pp->is->method->debug >9) //!!!!!
+ logf(LOG_LOG,"isamd_read_item: going to merge with %d.%d",
+ p_key->sysno, p_key->seqno);
+ pp->diffinfo[i].key = *p_key; /* the key merge could not handle */
+ pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
+ pp->diffinfo[i].key.seqno >>= 1;
+ pp->diffinfo[i].difftype=DT_INPU;
+ if (pp->is->method->debug > 7)
+ logf(LOG_LOG,"isamd_read_item: inpu key %d sys=%d seq=%d=2*%d+%d",
+ i, p_key->sysno,
+ pp->diffinfo[i].key.seqno*2 + pp->diffinfo[1].mode,
+ pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].mode);
+ p_key->sysno=p_key->seqno=0; /* used it up */
+ }
+
+ if (filt)
+ { /* we have a whole input stream to inject */
+ pp->diffinfo[i].difftype=DT_INPU;
+ }
+ } /* first time */
+
+ while (retry)
+
+ {
+ retry=0;
+ winner = 0;
+ for (i=0; (!retry) && (pp->diffinfo[i].difftype); i++)
+ {
+ if (0==pp->diffinfo[i].key.sysno)
+ {/* read a new one, if possible */
+ if ((pp->diffinfo[i].difftype==DT_DIFF) &&
+ (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
+ { /* a normal kind of diff */
+ oldoffs=pp->diffinfo[i].diffidx;
+ codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
+ keyptr=(char *)&(pp->diffinfo[i].key);
+ (*pp->is->method->code_item)(ISAMD_DECODE,
+ pp->diffinfo[i].decodeData, &keyptr, &codeptr);
+ pp->diffinfo[i].diffidx += codeptr-codestart;
+ pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
+ pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
+ if (pp->is->method->debug > 9)
+ logf(LOG_LOG,"isamd_read_item: dif[%d] at %d-%d: %s",
+ i,oldoffs, pp->diffinfo[i].diffidx,
+ hexdump(pp->buf+oldoffs, pp->diffinfo[i].diffidx-oldoffs,0));
+ if (pp->is->method->debug > 7)
+ logf(LOG_LOG,"isamd_read_item: rd dif[%d] %d.%d (%x.%x)",
+ i,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
+ }
+ else if ( pp->diffinfo[i].difftype==DT_MAIN)
+ { /* read a main item */
+ assert(i==0); /* main data goes before any diffs */
+ oldoffs=pp->offset;
+ keyptr=(char*) &(pp->diffinfo[0].key);
+ rc= isamd_read_main_item(pp,&keyptr);
+ if (0==rc)
+ { /* eof */
+ if (pp->is->method->debug > 7)
+ logf(LOG_LOG,"isamd_read_item: eof (rc=%d) main ",
+ rc);
+ pp->diffinfo[i].maxidx=-1;
+ pp->diffinfo[i].key.sysno=0;
+ pp->diffinfo[i].key.seqno=0;
+ pp->diffinfo[i].difftype= DT_DONE;
+ }
+ else
+ { /* not eof */
+ pp->diffinfo[i].mode = 1;
+ if (pp->is->method->debug > 7)
+ logf(LOG_LOG,"isamd_read_item: rd main %d-%d %d.%d (%x.%x) m=%d",
+ oldoffs,pp->offset,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].mode);
+ } /* not eof */
+ }
+ else if (pp->diffinfo[i].difftype==DT_INPU)
+ {
+ keyptr = (char *) &pp->diffinfo[i].key;
+ /* rc = (*data->read_item)(data->clientData, &keyptr, &pp->diffinfo[i].mode); */
+ rc = filter_read(filt, &pp->diffinfo[i].key,
+ &pp->diffinfo[i].mode);
+ if (!rc)
+ { /* did not get it */
+ pp->diffinfo[i].key.sysno=0;
+ pp->diffinfo[i].maxidx=0; /* signal the end */
+ pp->diffinfo[i].difftype=DT_DONE;
+ }
+ if (pp->is->method->debug >7)
+ logf(LOG_LOG,"merge: read inpu m=%d %d.%d (%x.%x)",
+ pp->diffinfo[i].mode,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno );
+ } /* read an input item */
+ } /* read a new one */
+
+ if (pp->is->method->debug > 8)
+ logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
+ i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
+
+ if ( 0!= pp->diffinfo[i].key.sysno)
+ { /* got a key, compare */
+ if (i!=winner)
+ cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
+ else
+ cmp=-1;
+ if (0==pp->diffinfo[winner].key.sysno)
+ cmp=-1; /* end of main sequence, take all diffs */
+ if (cmp<0)
+ {
+ if (pp->is->method->debug > 8)
+ logf(LOG_LOG,"isamd_read_item: ins [%d]%d.%d < [%d]%d.%d",
+ i,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ winner,
+ pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
+ if (pp->diffinfo[i].mode) /* insert diff, should always be */
+ winner = i;
+ else
+ {
+ if (pp->is->method->debug > 1)
+ logf(LOG_LOG,"delete diff for nonexisting item");
+ assert(!"delete diff for nonexisting item");
+ /* is an assert too steep here? Not really.*/
+ }
+ } /* earlier key */
+ else if (cmp==0)
+ {
+ if (!pp->diffinfo[i].mode) /* delete diff. should always be */
+ {
+ if (pp->is->method->debug > 8)
+ logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
+ i, winner,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
+ pp->diffinfo[winner].key.sysno=0; /* delete it */
+ }
+ else
+ if (pp->is->method->debug > 2)
+ logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
+ i, winner,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
+ pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
+ /* skip the insert, since we already have it in the base */
+ /* Should we fail an assertion here??? */
+ pp->diffinfo[i].key.sysno=0; /* done with the delete */
+ retry=1; /* start all over again */
+ } /* matching key */
+ /* else it is a later key, its turn will come */
+ } /* got a key */
+ } /* for each diff */
+ } /* not retry */
+
+ if ( pp->diffinfo[winner].key.sysno)
+ {
+ if (pp->is->method->debug > 7)
+ logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
+ winner,
+ pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
+ pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
+ memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
+ *dst += sizeof(struct it_key);
+ pp->diffinfo[winner].key.sysno=0; /* used that one up */
+ cmp= 1;
+ }
+ else
+ {
+ if (pp->is->method->debug > 7)
+ logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
+ winner,
+ pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
+ pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
+ assert(winner==0); /* if nothing found, nothing comes from a diff */
+ cmp= 0; /* eof */
+ }
+ if (cmp)
+ ++(pp->is->no_read_keys);
+ else
+ ++(pp->is->no_read_eof);
+
+ return cmp;
+
+} /* isamd_read_item */
+
+
+int isamd_read_item (ISAMD_PP pp, char **dst)