2 * Copyright (c) 1996-1998, Index Data.
3 * See the file LICENSE for details.
6 * $Id: merge-d.c,v 1.6 1999-07-23 15:43:05 heikki Exp $
10 * - single-entry optimizing
11 * - study and optimize block sizes (later)
17 * There is aconfusion about the block addresses. cat or type is the category,
18 * pos or block is the block number. pp structures keep these two separate,
19 * and combine when saving the pp. The next pointer in the pp structure is
20 * also a combined address, but needs to be combined every time it is needed,
21 * and separated when the partss are needed... This is done with the isamd_
22 * _block, _type, and _addr macros. The _addr takes block and type as args,
23 * in that order. This conflicts with the order these are often mentioned in
24 * the debug log calls, and other places, leading to small mistakes here
33 #include "../index/index.h"
47 static char *hexdump(unsigned char *p, int len, char *buff) {
48 static char localbuff[128];
50 if (!buff) buff=localbuff;
53 sprintf(bytebuff,"%02x",*p);
55 strcat(buff,bytebuff);
56 if (len) strcat(buff,",");
62 static int separateDiffBlock(ISAMD_PP pp)
65 return 1; /* multi-block chains always have a separate diff block */
66 return ( pp->size + 2*sizeof(int) > pp->is->method->filecat[pp->cat].bsize);
67 /* make sure there is at least room for the length and one diff. if not, */
68 /* it goes to a separate block */
70 /* todo: Make the limit adjustable in the filecat table ! */
74 /**************************************************************
76 **************************************************************/
78 static void getDiffInfo(ISAMD_PP pp, int diffidx)
79 { /* builds the diff info structures from a diffblock */
80 int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +1;
81 /* Each diff takes at least 5 bytes. Probably more, but this is safe */
82 int i=1; /* [0] is used for the main data */
83 int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
85 pp->diffinfo = xmalloc( diffsz );
86 memset(pp->diffinfo,'\0',diffsz);
87 if (pp->is->method->debug > 4)
88 logf(LOG_LOG,"isamd_getDiffInfo: %d (%d:%d), ix=%d mx=%d",
89 isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, diffidx,maxinfos);
94 if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
96 if (pp->is->method->debug > 4)
97 logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
99 return; /* whole block done */
101 memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
103 if (pp->is->method->debug > 5)
104 logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
105 pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
106 assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize);
108 if (0==pp->diffinfo[i].maxidx)
110 if (pp->is->method->debug > 4)
111 logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
113 return; /* end marker */
115 diffidx += sizeof(int);
116 pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
117 pp->diffinfo[i].diffidx = diffidx;
118 if (pp->is->method->debug > 4)
119 logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
120 i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
121 hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
122 diffidx=pp->diffinfo[i].maxidx;
123 if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
124 return; /* whole block done */
127 assert (!"too many diff sequences in the block");
130 static void loadDiffs(ISAMD_PP pp)
131 { /* assumes pp is a firstblock! */
135 return; /* no diffs to talk about */
137 { /* separate diff block, load it */
138 pp->diffbuf= xmalloc( pp->is->method->filecat[pp->cat].bsize);
139 diffaddr=isamd_addr(pp->diffs/2, pp->cat);
140 isamd_read_block (pp->is, isamd_type(diffaddr),
141 isamd_block(diffaddr), pp->diffbuf );
142 diffidx= ISAMD_BLOCK_OFFSET_N;
143 if (pp->is->method->debug > 4)
144 logf(LOG_LOG,"isamd_LoadDiffs: loaded block %d=%d:%d, d=%d ix=%d",
145 diffaddr, isamd_type(diffaddr),isamd_block(diffaddr),
149 { /* integrated block, just set the pointers */
150 pp->diffbuf = pp->buf;
151 diffidx = pp->size; /* size is the beginning of diffs, diffidx the end*/
152 if (pp->is->method->debug > 4)
153 logf(LOG_LOG,"isamd_LoadDiffs: within %d=%d:%d, d=%d ix=%d ",
154 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, pp->diffs, diffidx);
156 getDiffInfo(pp,diffidx);
160 void isamd_free_diffs(ISAMD_PP pp)
165 for (i=0;pp->diffinfo[i].decodeData;i++)
166 (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
168 if (pp->diffbuf != pp->buf)
170 } /* isamd_free_diffs */
173 /* Reads one item and corrects for the diffs, if any */
174 /* return 1 for ok, 0 for eof */
175 int isamd_read_item (ISAMD_PP pp, char **dst)
180 int winner=0; /* which diff holds the day */
181 int i; /* looping diffs */
184 if (pp->diffs==0) /* no diffs, just read the thing */
185 return isamd_read_main_item(pp,dst);
192 if (0==pp->diffinfo[0].key.sysno)
193 { /* 0 is special case, main data. */
194 keyptr=(char*) &(pp->diffinfo[0].key);
195 pp->diffinfo[0].mode = ! isamd_read_main_item(pp,&keyptr);
196 if (pp->is->method->debug > 4)
197 logf(LOG_LOG,"isamd_read_item: read main %d.%d (%x.%x)",
198 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno,
199 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno);
200 } /* get main data */
202 for (i=1; (!retry) && (pp->diffinfo[i].decodeData); i++)
204 if (pp->is->method->debug > 4)
205 logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
206 i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
207 pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
209 if ( (0==pp->diffinfo[i].key.sysno) &&
210 (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
211 {/* read a new one, if possible */
212 codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
213 keyptr=(char *)&(pp->diffinfo[i].key);
214 (*pp->is->method->code_item)(ISAMD_DECODE,
215 pp->diffinfo[i].decodeData, &keyptr, &codeptr);
216 pp->diffinfo[i].diffidx += codeptr-codestart;
217 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
218 pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
219 if (pp->is->method->debug > 4)
220 logf(LOG_LOG,"isamd_read_item: read diff[%d] %d.%d (%x.%x)",i,
221 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
222 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
224 if ( 0!= pp->diffinfo[i].key.sysno)
225 { /* got a key, compare */
226 cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
227 if (0==pp->diffinfo[winner].key.sysno)
228 cmp=-1; /* end of main sequence, take all diffs */
231 if (pp->is->method->debug > 4)
232 logf(LOG_LOG,"isamd_read_item: ins %d<%d %d.%d (%x.%x) < %d.%d (%x.%x)",
234 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
235 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
236 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
237 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
238 if (pp->diffinfo[i].mode) /* insert diff, should always be */
241 assert(!"delete diff for nonexisting item");
242 /* is an assert too steep here?*/
246 if (!pp->diffinfo[i].mode) /* delete diff. should always be */
248 if (pp->is->method->debug > 4)
249 logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
251 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
252 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
253 pp->diffinfo[winner].key.sysno=0; /* delete it */
256 if (pp->is->method->debug > 4)
257 logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
259 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
260 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
261 /* skip the insert, since we already have it in the base */
262 pp->diffinfo[i].key.sysno=0; /* done with the delete */
263 retry=1; /* start all over again */
265 /* else it is a later key, its turn will come */
267 } /* for each diff */
270 if ( pp->diffinfo[winner].key.sysno)
272 if (pp->is->method->debug > 4)
273 logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
275 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
276 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
277 memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
278 *dst += sizeof(struct it_key);
279 pp->diffinfo[winner].key.sysno=0; /* used that one up */
284 if (pp->is->method->debug > 4)
285 logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
287 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
288 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
289 assert(winner==0); /* if nothing found, nothing comes from a diff */
294 } /* isamd_read_item */
296 /*****************************************************************
298 *****************************************************************/
300 static void isamd_reduceblock(ISAMD_PP pp)
301 /* takes a large block, and reduces its category if possible */
302 /* Presumably the first block in an isam-list */
305 return; /* existing block, do not touch */
306 if (pp->is->method->debug > 2)
307 logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
308 pp->pos, pp->cat, pp->size);
309 while ( ( pp->cat > 0 ) && (!pp->next) &&
310 (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
312 pp->pos = isamd_alloc_block(pp->is, pp->cat);
313 if (pp->is->method->debug > 2)
314 logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
315 pp->pos, pp->cat, pp->size);
321 static int save_first_pp ( ISAMD_PP firstpp)
323 isamd_reduceblock(firstpp);
324 isamd_buildfirstblock(firstpp);
325 isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
326 return isamd_addr(firstpp->pos,firstpp->cat);
329 static void save_last_pp (ISAMD_PP pp)
331 pp->next = 0;/* just to be sure */
332 isamd_buildlaterblock(pp);
333 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
337 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
339 /* order of things: Better to save firstpp first, if there are just two */
340 /* blocks, but last if there are blocks in between, as these have already */
341 /* been saved... optimise later (that's why this is in its own func...*/
342 int retval = save_first_pp(firstpp);
347 isamd_pp_close(firstpp);
349 } /* save_both_pps */
351 static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
352 { /* reads the diff block (if separate) and sets diffidx right */
356 { /* no diffs yet, create room for them */
357 if (separateDiffBlock(firstpp))
358 { /* create a new block */
359 pp=isamd_pp_open(pp->is,isamd_addr(0,firstpp->cat));
360 pp->pos = isamd_alloc_block(pp->is, pp->cat);
361 firstpp->diffs = pp->pos*2 +1;
362 diffidx = pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
363 if (pp->is->method->debug >3)
364 logf(LOG_LOG,"isamd_appd: alloc diff (d=%d) %d=%d:%d ix=%d",
366 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
370 { /* prepare to append diffs in head */
372 pp->diffs = diffidx *2 +0;
373 if (pp->is->method->debug >3)
374 logf(LOG_LOG,"isamd_appd: set up diffhead (d=%d) %d=%d:%d ix=%d",
376 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
381 { /* existing diffs */
383 { /* diffs in a separate block, load it */
384 pp=isamd_pp_open(pp->is, isamd_addr(firstpp->diffs/2,pp->cat));
385 diffidx = pp->offset= pp->size;
386 if (pp->is->method->debug >3)
387 logf(LOG_LOG,"isamd_appd: loaded diff (d=%d) %d=%d:%d ix=%d",
389 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
393 { /* diffs within the nead */
394 diffidx= pp->diffs/2;
395 if (pp->is->method->debug >3)
396 logf(LOG_LOG,"isamd_appd: diffs in head d=%d %d=%d:%d ix=%d sz=%d",
398 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
401 } /* diffs exist already */
402 *p_diffidx = diffidx;
404 } /* read_diff_block */
409 /*******************************************************************
410 * Building main blocks (no diffs)
411 *******************************************************************/
415 static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
416 { /* allocates a new block for the main data, and links it in */
419 { /* special case: it was the first block. Save much later */
421 { /* firstpp not allocated yet, do so now, */
422 /* to keep blocks in order. Don't save yet, though */
423 firstpp->pos = isamd_alloc_block(pp->is, firstpp->cat);
425 newblock = isamd_alloc_block(pp->is, firstpp->cat);
426 firstpp->next = isamd_addr(newblock,firstpp->cat);
427 /* keep the largest category */
428 pp=isamd_pp_open(pp->is,isamd_addr(0,firstpp->cat));/*don't load*/
430 pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
432 if (pp->is->method->debug >3)
433 logf(LOG_LOG,"isamd_build: Alloc2 f=%d (%d:%d) n=%d(%d:%d)",
434 isamd_addr(firstpp->pos,firstpp->cat),
435 firstpp->cat, firstpp->pos,
436 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos );
439 { /* it was not the first block */
440 newblock = isamd_alloc_block(pp->is, firstpp->cat);
441 pp->next = isamd_addr(newblock,firstpp->cat);
442 isamd_buildlaterblock(pp);
443 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
444 pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
446 pp->cat = firstpp->cat;
447 pp->pos = isamd_block(firstpp->next);
450 } /* get_new_main_block */
453 static ISAMD_PP append_main_item(ISAMD_PP firstpp,
455 struct it_key *i_key,
457 { /* appends one item in the main data block, allocates new if needed */
458 char *i_item= (char *) i_key; /* same as char */
461 char *c_ptr = codebuff;
465 int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
469 (*pp->is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
470 codelen = c_ptr - codebuff;
471 assert ( (codelen<128) && (codelen>0));
472 if (pp->is->method->debug >3)
473 logf(LOG_LOG,"isamd:build: coded into %s (nk=%d)",
474 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
476 if (pp->offset + codelen > maxsize )
477 { /* oops, block full - get a new one */
478 pp = get_new_main_block( firstpp, pp );
479 /* reset encoging and code again */
480 (*pp->is->method->code_reset)(encoder_data);
483 (*pp->is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
484 codelen = c_ptr - codebuff;
485 assert ( (codelen<128) && (codelen>0));
486 if (pp->is->method->debug >3)
487 logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
488 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
491 /* write the data into pp, now we must have room */
492 memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
493 pp->offset += codelen;
497 } /* append_main_item */
500 static int isamd_build_first_block(ISAMD is, ISAMD_I data)
502 struct it_key i_key; /* input key */
503 char *i_item= (char *) &i_key; /* same as char */
506 int i_mode; /* 0 for delete, 1 for insert */
514 firstpp=pp=isamd_pp_open(is, isamd_addr(0,is->max_cat));
515 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
516 encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
518 if (is->method->debug >3)
519 logf(LOG_LOG,"isamd_bld start: p=%d c=%d sz=%d maxsz=%d ",
520 pp->pos, pp->cat, pp->size,
521 pp->is->method->filecat[pp->is->max_cat].bsize);
523 /* read first input */
525 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
527 assert( i_ptr-i_item == sizeof(i_key) );
529 if (pp->is->method->debug >3)
530 logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
531 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
536 { /* ignore deletes here, should not happen */
537 pp= append_main_item(firstpp, pp, &i_key, encoder_data);
540 /* (try to) read the next item */
542 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
544 if ( (i_more) && (pp->is->method->debug >3) )
545 logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
546 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
550 return save_both_pps( firstpp, pp );
552 } /* build_first_block */
555 /***************************************************************
557 ***************************************************************/
560 static int merge ( ISAMD_PP *p_firstpp, /* first pp of the chain */
561 ISAMD_PP *p_pp, /* diff block */
562 struct it_key *p_key ) /* not used yet */
564 ISAMD_PP readpp = *p_firstpp;
570 ISAMD_PP firstpp; /* the new first, the one we write into */
574 /* set up diffs as they should be for reading */
575 readpp->offset= ISAMD_BLOCK_OFFSET_1;
577 if (*p_pp == *p_firstpp)
578 { /* integrated diffs */
579 diffidx=readpp->size;
580 readpp->diffs = diffidx*2+0;
581 readpp->diffbuf=readpp->buf; /*? does this get freed right ??? */
582 if (readpp->is->method->debug >3)
583 logf(LOG_LOG,"isamd_merge:local diffs at %d: %s",
584 diffidx,hexdump(&(readpp->diffbuf[diffidx]),8,0));
587 { /* separate diff block in *p_pp */
588 killblk = readpp->diffs/2;
589 diffidx = readpp->is->method->filecat[readpp->cat].bsize;
590 readpp->diffbuf= xmalloc( diffidx); /* copy diffs to where read wants*/
591 memcpy( readpp->diffbuf, &((*p_pp)->buf[0]), diffidx);
592 diffidx = ISAMD_BLOCK_OFFSET_N;
593 if (readpp->is->method->debug >3) {
594 logf(LOG_LOG,"isamd_merge:separate diffs at ix=%d",
596 logf(LOG_LOG,"isamd_merge: dbuf=%p (from %p) pp=%p",
597 readpp->diffbuf, &((*p_pp)->buf[0]), (*p_pp) );
600 getDiffInfo(readpp,diffidx);
602 r_ptr= (char *) &r_key;
603 r_more = isamd_read_item( readpp, &r_ptr);
605 { /* oops, all data has been deleted! what to do??? */
606 /* never mind, we have at least one more delta to add to the block */
607 /* pray that is not a delete as well... */
610 if (readpp->is->method->debug >3)
611 logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
613 assert (readpp->numKeys == 0);
617 { /* we had a separate diff block, release it, we have the data */
618 isamd_release_block(readpp->is, readpp->cat, killblk);
619 if (readpp->is->method->debug >3)
620 logf(LOG_LOG,"isamd_merge: released diff block %d=%d:%d",
621 isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
624 isamd_release_block(readpp->is, readpp->cat, killblk);
625 if (readpp->is->method->debug >3)
626 logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
627 isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
629 /* set up the new blocks for simple writing */
630 firstpp=pp=isamd_pp_open(readpp->is,isamd_addr(0, readpp->is->max_cat));
631 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
632 encoder_data = (*pp->is->method->code_start)(ISAMD_ENCODE);
636 if (readpp->is->method->debug >4)
637 logf(LOG_LOG,"isamd_merge: got key %d.%d",
638 r_key.sysno, r_key.seqno );
639 pp= append_main_item(firstpp, pp, &r_key, encoder_data);
641 if ( readpp->pos != killblk )
643 isamd_release_block(readpp->is, readpp->cat, readpp->pos);
644 if (readpp->is->method->debug >3)
645 logf(LOG_LOG,"isamd_merge: released data block %d (%d:%d)",
646 killblk, isamd_type(killblk), isamd_block(killblk) );
650 /* (try to) read next item */
651 r_ptr= (char *) &r_key;
652 r_more = isamd_read_item( readpp, &r_ptr);
656 /* TODO: while pkey is an insert, and after last key inserted, append it */
657 /* will prevent multiple merges on large insert runs */
659 /* set things up so that merge can continue */
660 isamd_reduceblock(firstpp);
664 { /* the last data block is of no interest any more */
666 if (readpp->is->method->debug >3)
667 logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
668 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
672 *p_firstpp = firstpp;
680 /***************************************************************
682 ***************************************************************/
686 static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
688 struct it_key i_key; /* one input item */
689 char *i_item = (char *) &i_key; /* same as chars */
692 int i_mode; /* 0 for delete, 1 for insert */
702 char *c_ptr = codebuff;
706 firstpp=isamd_pp_open(is, ipos);
707 if (is->method->debug >4)
708 logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d d=%d=%d*2+%d",
709 ipos, isamd_type(ipos), isamd_block(ipos),
710 firstpp->diffs, firstpp->diffs/2, firstpp->diffs & 1);
711 pp=read_diff_block(firstpp, &diffidx);
712 encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
713 maxsize = is->method->filecat[pp->cat].bsize;
715 difflenidx = diffidx;
716 diffidx+=sizeof(int); /* difflen will be stored here */
718 /* read first input */
720 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
722 if (is->method->debug >3)
723 logf(LOG_LOG,"isamd_appd: start with m=%d %s",
724 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
728 /* store the mode bit inside key */
729 assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
730 i_key.seqno = i_key.seqno * 2 + i_mode;
734 (*is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
735 codelen = c_ptr - codebuff;
736 assert ( (codelen<128) && (codelen>0));
737 if (is->method->debug >3)
738 logf(LOG_LOG,"isamd_appd: coded into %d: %s (nk=%d) (ix=%d)",
739 codelen, hexdump(codebuff, codelen,hexbuff),
740 firstpp->numKeys,diffidx);
742 if (diffidx + codelen > maxsize )
744 if (is->method->debug >3)
745 logf(LOG_LOG,"isamd_appd: block full (ix=%d mx=%d lix=%d)",
746 diffidx, maxsize, difflenidx);
747 if (is->method->debug >5)
748 logf(LOG_LOG,"isamd_appd: block pp=%p buf=%p [%d]:%s",
750 difflenidx, hexdump(&pp->buf[difflenidx],8,0));
751 merge_rc = merge (&firstpp, &pp, &i_key);
753 return merge_rc; /* merge handled them all ! */
755 /* set things up so we can continue */
756 pp = read_diff_block(firstpp, &diffidx);
757 (*is->method->code_reset)(encoder_data);
758 maxsize = is->method->filecat[pp->cat].bsize;
760 diffidx+=sizeof(int);
765 memcpy(&(pp->buf[diffidx]),codebuff,codelen);
768 firstpp->numKeys++; /* insert diff */
770 firstpp->numKeys--; /* delete diff */
772 /* update length of this diff run */
773 memcpy(&(pp->buf[difflenidx]),&diffidx,sizeof(diffidx));
775 firstpp->diffs =diffidx*2+0;
779 /* (try to) read the next input */
781 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
782 if ( (i_more) && (is->method->debug >3) )
783 logf(LOG_LOG,"isamd_appd: got m=%d %s",
784 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
787 /* clear the next difflen, if room for such */
788 difflenidx = diffidx;
789 while ( (difflenidx-diffidx<=sizeof(int)) && (difflenidx<maxsize))
790 pp->buf[difflenidx++]='\0';
792 return save_both_pps( firstpp, pp );
798 /*************************************************************
799 * isamd_append itself, Sweet, isn't it
800 *************************************************************/
802 ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
807 retval = isamd_build_first_block(is,data);
809 retval = append_diffs(is,ipos,data);
816 * $Log: merge-d.c,v $
817 * Revision 1.6 1999-07-23 15:43:05 heikki
818 * Hunted a few bugs in isam-d. Still crashes on the long test run
820 * Revision 1.5 1999/07/23 13:58:52 heikki
821 * merged closer to working, still fails on filling a separate, large block
823 * Revision 1.4 1999/07/21 14:53:55 heikki
824 * isamd read and write functions work, except when block full
825 * Merge missing still. Need to split some functions
827 * Revision 1.1 1999/07/14 13:14:47 heikki