+ return cmp;
+
+} /* isamd_read_item */
+
+
+static void isamd_reduceblock(ISAMD_PP pp)
+/* takes a large block, and reduces its category if possible */
+/* Presumably the first block in an isam-list */
+{
+ if (pp->pos)
+ return; /* existing block, do not touch */
+ if (pp->is->method->debug > 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)
+ logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
+ pp->pos, pp->cat, pp->size);
+} /* reduceblock */
+
+
+static int isamd_build_first_block(ISAMD is, ISAMD_I data)
+{
+ char i_item[128]; /* one input item */
+ char *i_ptr=i_item;
+ int i_more =1;
+ int i_mode; /* 0 for delete, 1 for insert */
+
+ ISAMD_PP firstpp;
+ ISAMD_PP pp;
+ void *encoder_data;
+ int maxsize;
+
+ char codebuff[128];
+ char *c_ptr = codebuff;
+ int codelen;
+
+ char hexbuff[64];
+ int newblock;
+ int retval=0;
+
+ 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);
+ maxsize = is->method->filecat[is->max_cat].bsize;
+
+ if (is->method->debug >3)
+ logf(LOG_LOG,"isamd_bld start: p=%d c=%d sz=%d maxsz=%d ",
+ pp->pos, pp->cat, pp->size, maxsize);
+
+ /* read first input */
+ i_ptr = i_item;
+ i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
+
+ if (is->method->debug >3)
+ logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
+ i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
+
+ while (i_more)
+ {
+ if (i_mode!=0)
+ { /* ignore deletes here, should not happen */
+
+ 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:build: coded into %s (nk=%d)",
+ hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
+
+ if (pp->offset + codelen > maxsize )
+ { /* oops, block full - get a new one */
+ if (firstpp==pp)
+ { /* special case: it was the first block. Save much later */
+ if (0==firstpp->pos)
+ { /* firstpp not allocated yet, do so now, */
+ /* to keep blocks in order. Don't save yet, though */
+ firstpp->pos = isamd_alloc_block(is, firstpp->cat);
+ }
+ newblock = isamd_alloc_block(is, firstpp->cat);
+ firstpp->next = isamd_addr(newblock,firstpp->cat);
+ /* keep the largest category */
+ pp=isamd_pp_open(is,isamd_addr(0,firstpp->cat));/*don't load*/
+ pp->pos=newblock;
+ pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
+ pp->next=0;
+ if (is->method->debug >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,
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos );
+ }
+ else
+ { /* it was not the first block */
+ newblock = isamd_alloc_block(is, firstpp->cat);
+ pp->next = isamd_addr(newblock,firstpp->cat);
+ isamd_buildlaterblock(pp);
+ isamd_write_block(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);
+ }
+ /* reset encoging and code again */
+ (*is->method->code_reset)(encoder_data);
+ 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:build: recoded into %s (nk=%d)",
+ hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
+ } /* block full */
+
+ /* write the data into pp, now we have room */
+ memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
+ pp->offset += codelen;
+ pp->size += codelen;
+ firstpp->numKeys++;
+ } /* not delete */
+
+ /* (try to) read the next item */
+ i_ptr = i_item;
+ i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
+
+ if ( (i_more) && (is->method->debug >3) )
+ logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
+ i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
+
+
+ } /* i_more */
+
+ /* 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 */
+
+ /* save the first block */
+ isamd_reduceblock(firstpp);
+ isamd_buildfirstblock(firstpp);
+ isamd_write_block(is,firstpp->cat,firstpp->pos,firstpp->buf);
+ retval = isamd_addr(firstpp->pos,firstpp->cat);
+
+ if (firstpp!=pp){ /* and the last one */
+ pp->next = 0;/* just to be sure */
+ isamd_buildlaterblock(pp);
+ isamd_write_block(is,pp->cat,pp->pos,pp->buf);
+ isamd_pp_close(pp);
+ }
+
+ isamd_pp_close(firstpp);
+
+ return retval;
+} /* build_first_block */
+
+
+
+static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
+{
+ struct it_key i_key; /* one input item */
+ char *i_item = (char *) &i_key; /* same as chars */
+ char *i_ptr=i_item;
+ int i_more =1;
+ int i_mode; /* 0 for delete, 1 for insert */
+
+ ISAMD_PP firstpp;
+ ISAMD_PP pp;
+ void *encoder_data;
+ char hexbuff[64];
+ int retval=ipos; /* by default we do not change the firstblock addr */
+ int diffidx=0;
+ int maxsize=0;
+ int difflenidx;
+ char codebuff[128];
+ char *c_ptr = codebuff;
+ int codelen;
+
+ pp=firstpp=isamd_pp_open(is, ipos);
+
+ /* TODO: Turn these ifs around! Check first diffs==0, and create new */
+ /* according to separateDiffBlock. if !=0, read according to its type */
+ /* bit. Much more robust this way around! */
+
+ if (separateDiffBlock(firstpp))
+ { /* multi-block item, get the diff block */
+ if (firstpp->diffs==0)
+ { /* allocate first diff block */
+ pp=isamd_pp_open(is,isamd_addr(0,firstpp->cat));
+ pp->pos = isamd_alloc_block(pp->is, pp->cat);
+ firstpp->diffs = pp->pos*2 +1;
+ diffidx = pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
+ if (is->method->debug >3)
+ logf(LOG_LOG,"isamd_appd: alloc diff (%d) %d %d:%d ix=%d",
+ firstpp->diffs,
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+ diffidx);
+ }
+ else
+ { /* find pointers within the existing block */
+ pp=isamd_pp_open(is, firstpp->diffs/2);
+ diffidx = pp->offset= pp->size;
+ if (is->method->debug >3)
+ logf(LOG_LOG,"isamd_appd: load diff (%d) %d (%d:%d) ix=%d",
+ firstpp->diffs,
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+ diffidx);
+ }
+ }
+ else
+ { /* single-block item, get idx right */
+ diffidx= pp->diffs/2;
+ if (is->method->debug >3)
+ logf(LOG_LOG,"isamd_appd: diffs in head %d (%d:%d) ix=%d sz=%d",
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+ diffidx, pp->size);
+ if (0==diffidx)
+ { /* no diffs yet, make them */
+ diffidx = pp->size;
+ pp->diffs = diffidx *2 +0;
+ }
+ }
+
+ encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
+ maxsize = is->method->filecat[pp->cat].bsize;
+
+ difflenidx = diffidx;
+ diffidx+=sizeof(int); /* difflen will be stored here */
+
+ /* read first input */
+ i_ptr = i_item;
+ i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
+
+ if (is->method->debug >3)
+ logf(LOG_LOG,"isamd_appd: start with m=%d %s",
+ i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
+
+ while (i_more)
+ {
+ assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
+ i_key.seqno = i_key.seqno * 2 + i_mode;
+ 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: coded into %d: %s (nk=%d) (ix=%d)",
+ codelen, hexdump(codebuff, codelen,hexbuff),
+ firstpp->numKeys,diffidx);
+
+ if (diffidx + codelen > maxsize )
+ { /* block full */
+ logf(LOG_LOG,"isamd_appd: block full (ix=%d mx=%d)",
+ diffidx, maxsize);
+ return 0; /*!*/ /* do something about it!!! */
+ } /* block full */
+
+ /* save the diff */
+ memcpy(&(pp->buf[diffidx]),codebuff,codelen);
+ diffidx += codelen;
+ if (i_mode)
+ firstpp->numKeys++; /* insert diff */
+ else
+ firstpp->numKeys--; /* delete diff */
+ memcpy(&(pp->buf[difflenidx]),&diffidx,sizeof(diffidx));
+ if (firstpp==pp)
+ firstpp->diffs =diffidx*2+0;
+ else
+ pp->size =diffidx;
+
+ /* try to read the next input */
+ i_ptr = i_item;
+ i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
+ if ( (i_more) && (is->method->debug >3) )
+ logf(LOG_LOG,"isamd_appd: got m=%d %s",
+ i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
+ } /* more loop */
+
+ /* clear the next difflen, if room for such */
+ difflenidx = diffidx;
+ while ( (difflenidx-diffidx<=sizeof(int)) && (difflenidx<maxsize))
+ pp->buf[difflenidx++]='\0';
+
+ /* ok, save the block(s) */
+ if (firstpp != pp)
+ { /* save the diff block */
+ pp->next = 0; /* just to be sure */
+ isamd_buildlaterblock(pp);
+ isamd_write_block(is,pp->cat,pp->pos,pp->buf);
+ isamd_pp_close(pp);
+ }
+
+ isamd_buildfirstblock(firstpp);
+ isamd_write_block(is,firstpp->cat,firstpp->pos,firstpp->buf);
+ retval = isamd_addr(firstpp->pos,firstpp->cat);
+ isamd_pp_close(firstpp);
+
+ return retval;
+} /* append_diffs */
+
+
+
+ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
+{
+ int retval=0;
+
+ if (0==ipos)
+ retval = isamd_build_first_block(is,data);
+ else
+ retval = append_diffs(is,ipos,data);
+
+ return retval;
+} /* isamd_append */