2 * Copyright (c) 1996-1998, Index Data.
3 * See the file LICENSE for details.
6 * $Id: merge-d.c,v 1.9 1999-08-17 19:46:53 heikki Exp $
9 * - Input filter: Eliminate del-ins pairs, tell if only one entry (or none)
10 * - single-entry optimizing
11 * - study and optimize block sizes (later)
12 * - Clean up the different ways diffs are handled in writing and reading
15 * - memory leak somewhere.
16 * - Some pp_opens do not get closed.
17 * - Diffinfo's get left behind.
20 * There is a confusion about the block addresses. cat or type is the category,
21 * pos or block is the block number. pp structures keep these two separate,
22 * and combine when saving the pp. The next pointer in the pp structure is
23 * also a combined address, but needs to be combined every time it is needed,
24 * and separated when the partss are needed... This is done with the isamd_
25 * _block, _type, and _addr macros. The _addr takes block and type as args,
26 * in that order. This conflicts with the order these are often mentioned in
27 * the debug log calls, and other places, leading to small mistakes here
36 #include "../index/index.h"
50 static char *hexdump(unsigned char *p, int len, char *buff) {
51 static char localbuff[128];
53 if (!buff) buff=localbuff;
56 sprintf(bytebuff,"%02x",*p);
58 strcat(buff,bytebuff);
59 if (len) strcat(buff,",");
65 static int separateDiffBlock(ISAMD_PP pp)
68 return 1; /* multi-block chains always have a separate diff block */
69 return ( pp->size + 2*sizeof(int) > pp->is->method->filecat[pp->cat].bsize);
70 /* make sure there is at least room for the length and one diff. if not, */
71 /* it goes to a separate block */
73 /* todo: Make the limit adjustable in the filecat table ! */
77 /**************************************************************
79 **************************************************************/
81 static void getDiffInfo(ISAMD_PP pp, int diffidx)
82 { /* builds the diff info structures from a diffblock */
83 int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +1;
84 /* Each diff takes at least 5 bytes. Probably more, but this is safe */
85 int i=1; /* [0] is used for the main data */
86 int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
88 pp->diffinfo = xmalloc( diffsz ); /*!!!*/ /* does not always get freed */
89 memset(pp->diffinfo,'\0',diffsz);
90 if (pp->is->method->debug > 1) //4
91 logf(LOG_LOG,"isamd_getDiffInfo: %d (%d:%d), ix=%d mx=%d",
92 isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, diffidx,maxinfos);
97 if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
99 if (pp->is->method->debug > 4)
100 logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
102 return; /* whole block done */
104 memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
106 if (pp->is->method->debug > 5)
107 logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
108 pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
110 if (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize)
111 { /* bug-hunting, this fails on some long runs that log too much */
112 logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d",
113 __FILE__,__LINE__, diffidx);
114 logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
115 pp->is->method->filecat[pp->cat].bsize);
116 logf(LOG_LOG,"pp=%d=%d:%d pp->nx=%d=%d:%d",
117 isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
118 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
120 assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
122 if (0==pp->diffinfo[i].maxidx)
124 if (pp->is->method->debug > 1) //!!! 4
125 logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
127 return; /* end marker */
129 diffidx += sizeof(int);
130 pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
131 pp->diffinfo[i].diffidx = diffidx;
132 if (pp->is->method->debug > 4)
133 logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
134 i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
135 hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
136 diffidx=pp->diffinfo[i].maxidx;
137 if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
138 return; /* whole block done */
141 assert (!"too many diff sequences in the block");
144 static void loadDiffs(ISAMD_PP pp)
145 { /* assumes pp is a firstblock! */
149 return; /* no diffs to talk about */
151 { /* separate diff block, load it */
152 pp->diffbuf= xmalloc( pp->is->method->filecat[pp->cat].bsize);
153 diffaddr=isamd_addr(pp->diffs/2, pp->cat);
154 isamd_read_block (pp->is, isamd_type(diffaddr),
155 isamd_block(diffaddr), pp->diffbuf );
156 diffidx= ISAMD_BLOCK_OFFSET_N;
157 if (pp->is->method->debug > 1) //4 !!!
158 logf(LOG_LOG,"isamd_LoadDiffs: loaded block %d=%d:%d, d=%d ix=%d",
159 diffaddr, isamd_type(diffaddr),isamd_block(diffaddr),
163 { /* integrated block, just set the pointers */
164 pp->diffbuf = pp->buf;
165 diffidx = pp->size; /* size is the beginning of diffs, diffidx the end*/
166 if (pp->is->method->debug > 1) // 4 !!!
167 logf(LOG_LOG,"isamd_LoadDiffs: within %d=%d:%d, d=%d ix=%d ",
168 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, pp->diffs, diffidx);
170 getDiffInfo(pp,diffidx);
174 void isamd_free_diffs(ISAMD_PP pp)
177 if (pp->is->method->debug > 4)
178 logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
181 for (i=1;pp->diffinfo[i].decodeData;i++)
183 if (pp->is->method->debug > 4)
184 logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i,
185 pp->diffinfo[i].decodeData);
186 (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
189 if (pp->diffbuf != pp->buf)
191 } /* isamd_free_diffs */
194 /* Reads one item and corrects for the diffs, if any */
195 /* return 1 for ok, 0 for eof */
196 int isamd_read_item (ISAMD_PP pp, char **dst)
201 int winner=0; /* which diff holds the day */
202 int i; /* looping diffs */
205 if (pp->diffs==0) /* no diffs, just read the thing */
206 return isamd_read_main_item(pp,dst);
213 if (0==pp->diffinfo[0].key.sysno)
214 { /* 0 is special case, main data. */
215 keyptr=(char*) &(pp->diffinfo[0].key);
216 pp->diffinfo[0].mode = ! isamd_read_main_item(pp,&keyptr);
217 if (pp->is->method->debug > 4)
218 logf(LOG_LOG,"isamd_read_item: read main %d.%d (%x.%x)",
219 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno,
220 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno);
221 } /* get main data */
223 for (i=1; (!retry) && (pp->diffinfo[i].decodeData); i++)
225 if (pp->is->method->debug > 4)
226 logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
227 i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
228 pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
230 if ( (0==pp->diffinfo[i].key.sysno) &&
231 (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
232 {/* read a new one, if possible */
233 codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
234 keyptr=(char *)&(pp->diffinfo[i].key);
235 (*pp->is->method->code_item)(ISAMD_DECODE,
236 pp->diffinfo[i].decodeData, &keyptr, &codeptr);
237 pp->diffinfo[i].diffidx += codeptr-codestart;
238 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
239 pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
240 if (pp->is->method->debug > 4)
241 logf(LOG_LOG,"isamd_read_item: read diff[%d] %d.%d (%x.%x)",i,
242 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
243 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
245 if ( 0!= pp->diffinfo[i].key.sysno)
246 { /* got a key, compare */
247 cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
248 if (0==pp->diffinfo[winner].key.sysno)
249 cmp=-1; /* end of main sequence, take all diffs */
252 if (pp->is->method->debug > 4)
253 logf(LOG_LOG,"isamd_read_item: ins %d<%d %d.%d (%x.%x) < %d.%d (%x.%x)",
255 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
256 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
257 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
258 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
259 if (pp->diffinfo[i].mode) /* insert diff, should always be */
262 assert(!"delete diff for nonexisting item");
263 /* is an assert too steep here?*/
267 if (!pp->diffinfo[i].mode) /* delete diff. should always be */
269 if (pp->is->method->debug > 4)
270 logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
272 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
273 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
274 pp->diffinfo[winner].key.sysno=0; /* delete it */
277 if (pp->is->method->debug > 4)
278 logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
280 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
281 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
282 /* skip the insert, since we already have it in the base */
283 pp->diffinfo[i].key.sysno=0; /* done with the delete */
284 retry=1; /* start all over again */
286 /* else it is a later key, its turn will come */
288 } /* for each diff */
291 if ( pp->diffinfo[winner].key.sysno)
293 if (pp->is->method->debug > 4)
294 logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
296 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
297 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
298 memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
299 *dst += sizeof(struct it_key);
300 pp->diffinfo[winner].key.sysno=0; /* used that one up */
305 if (pp->is->method->debug > 4)
306 logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
308 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
309 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
310 assert(winner==0); /* if nothing found, nothing comes from a diff */
315 } /* isamd_read_item */
317 /*****************************************************************
319 *****************************************************************/
321 static void isamd_reduceblock(ISAMD_PP pp)
322 /* takes a large block, and reduces its category if possible */
323 /* Presumably the first block in an isam-list */
326 return; /* existing block, do not touch */
327 if (pp->is->method->debug > 1) // 2 !!!
328 logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
329 pp->pos, pp->cat, pp->size);
330 while ( ( pp->cat > 0 ) && (!pp->next) &&
331 (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
333 pp->pos = isamd_alloc_block(pp->is, pp->cat);
334 if (pp->is->method->debug > 1) // 2 !!!
335 logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
336 pp->pos, pp->cat, pp->size);
342 static int save_first_pp ( ISAMD_PP firstpp)
344 isamd_reduceblock(firstpp);
345 isamd_buildfirstblock(firstpp);
346 isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
347 return isamd_addr(firstpp->pos,firstpp->cat);
350 static void save_last_pp (ISAMD_PP pp)
352 pp->next = 0;/* just to be sure */
353 isamd_buildlaterblock(pp);
354 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
358 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
360 /* order of things: Better to save firstpp first, if there are just two */
361 /* blocks, but last if there are blocks in between, as these have already */
362 /* been saved... optimise later (that's why this is in its own func...*/
363 int retval = save_first_pp(firstpp);
368 isamd_pp_close(firstpp);
370 } /* save_both_pps */
372 static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
373 { /* reads the diff block (if separate) and sets diffidx right */
378 { /* no diffs yet, create room for them */
379 if (separateDiffBlock(firstpp))
380 { /* create a new block */
381 pp=isamd_pp_open(pp->is,isamd_addr(0,firstpp->cat));
382 pp->pos = isamd_alloc_block(pp->is, pp->cat);
383 firstpp->diffs = pp->pos*2 +1;
384 diffidx = pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
385 if (pp->is->method->debug >3)
386 logf(LOG_LOG,"isamd_appd: alloc diff (d=%d) %d=%d:%d ix=%d",
388 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
392 { /* prepare to append diffs in head */
394 pp->diffs = diffidx *2 +0;
395 i=diffidx; /* clear the rest of the block. ??? */
396 if ( i < pp->is->method->filecat[pp->cat].bsize)
398 if (pp->is->method->debug >1) //!!! 3
399 logf(LOG_LOG,"isamd_appd: set up diffhead (d=%d) %d=%d:%d ix=%d",
401 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
406 { /* existing diffs */
408 { /* diffs in a separate block, load it */
409 pp=isamd_pp_open(pp->is, isamd_addr(firstpp->diffs/2,pp->cat));
410 diffidx = pp->offset= pp->size;
411 if (pp->is->method->debug >1) // 3 !!!
412 logf(LOG_LOG,"isamd_appd: loaded diff (d=%d) %d=%d:%d ix=%d",
414 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
418 { /* diffs within the nead */
419 diffidx= pp->diffs/2;
420 if (pp->is->method->debug >1) // 3 !!!
421 logf(LOG_LOG,"isamd_appd: diffs in head d=%d %d=%d:%d ix=%d sz=%d",
423 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
426 } /* diffs exist already */
427 *p_diffidx = diffidx;
429 } /* read_diff_block */
434 /*******************************************************************
435 * Building main blocks (no diffs)
436 *******************************************************************/
440 static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
441 { /* allocates a new block for the main data, and links it in */
444 { /* special case: it was the first block. Save much later */
446 { /* firstpp not allocated yet, do so now, */
447 /* to keep blocks in order. Don't save yet, though */
448 firstpp->pos = isamd_alloc_block(pp->is, firstpp->cat);
450 newblock = isamd_alloc_block(pp->is, firstpp->cat);
451 firstpp->next = isamd_addr(newblock,firstpp->cat);
452 /* keep the largest category */
453 pp=isamd_pp_open(pp->is,isamd_addr(0,firstpp->cat));/*don't load*/
455 pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
457 if (pp->is->method->debug >1) //!!! 3
458 logf(LOG_LOG,"isamd_build: Alloc2 f=%d (%d:%d) n=%d(%d:%d)",
459 isamd_addr(firstpp->pos,firstpp->cat),
460 firstpp->cat, firstpp->pos,
461 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos );
464 { /* it was not the first block */
465 newblock = isamd_alloc_block(pp->is, firstpp->cat);
466 pp->next = isamd_addr(newblock,firstpp->cat);
467 if (pp->is->method->debug >1) //!!! 3
468 logf(LOG_LOG,"isamd_build: Alloc new after p=%d=%d:%d n=%d=%d:%d",
469 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
470 isamd_addr(newblock,pp->cat), pp->cat, newblock );
471 isamd_buildlaterblock(pp);
472 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
473 pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
475 pp->cat = firstpp->cat;
476 // pp->pos = isamd_block(firstpp->next); /* ???? FIRSTPP ???? */
478 pp->cat = firstpp->cat; /* is already, never mind */
481 } /* get_new_main_block */
484 static ISAMD_PP append_main_item(ISAMD_PP firstpp,
486 struct it_key *i_key,
488 { /* appends one item in the main data block, allocates new if needed */
489 char *i_item= (char *) i_key; /* same as char */
492 char *c_ptr = codebuff;
496 int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
500 (*pp->is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
501 codelen = c_ptr - codebuff;
502 assert ( (codelen<128) && (codelen>0));
503 if (pp->is->method->debug >3)
504 logf(LOG_LOG,"isamd:build: coded into %s (nk=%d)",
505 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
507 if (pp->offset + codelen > maxsize )
508 { /* oops, block full - get a new one */
509 pp = get_new_main_block( firstpp, pp );
510 /* reset encoging and code again */
511 (*pp->is->method->code_reset)(encoder_data);
514 (*pp->is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
515 codelen = c_ptr - codebuff;
516 assert ( (codelen<128) && (codelen>0));
517 if (pp->is->method->debug >3)
518 logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
519 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
522 /* write the data into pp, now we must have room */
523 memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
524 pp->offset += codelen;
528 } /* append_main_item */
531 static int isamd_build_first_block(ISAMD is, ISAMD_I data)
533 struct it_key i_key; /* input key */
534 char *i_item= (char *) &i_key; /* same as char */
537 int i_mode; /* 0 for delete, 1 for insert */
545 firstpp=pp=isamd_pp_open(is, isamd_addr(0,is->max_cat));
546 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
548 encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
550 if (is->method->debug >3)
551 logf(LOG_LOG,"isamd_bld start: p=%d c=%d sz=%d maxsz=%d ",
552 pp->pos, pp->cat, pp->size,
553 pp->is->method->filecat[pp->is->max_cat].bsize);
555 /* read first input */
557 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
559 assert( i_ptr-i_item == sizeof(i_key) );
561 if (pp->is->method->debug >3)
562 logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
563 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
568 { /* ignore deletes here, should not happen */
569 pp= append_main_item(firstpp, pp, &i_key, encoder_data);
572 /* (try to) read the next item */
574 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
576 if ( (i_more) && (pp->is->method->debug >3) )
577 logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
578 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
581 (*is->method->code_stop)(ISAMD_ENCODE, encoder_data);
583 return save_both_pps( firstpp, pp );
585 } /* build_first_block */
588 /***************************************************************
590 ***************************************************************/
593 static int merge ( ISAMD_PP *p_firstpp, /* first pp of the chain */
594 ISAMD_PP *p_pp, /* diff block */
595 struct it_key *p_key ) /* not used yet */
597 ISAMD_PP readpp = *p_firstpp;
603 ISAMD_PP firstpp; /* the new first, the one we write into */
607 /* set up diffs as they should be for reading */
608 readpp->offset= ISAMD_BLOCK_OFFSET_1;
610 if ( (*p_firstpp)->diffs & 1 )
611 { /* separate diff block in *p_pp */
612 killblk = readpp->diffs/2;
613 diffidx /*size*/ = readpp->is->method->filecat[readpp->cat].bsize;
614 readpp->diffbuf= xmalloc( diffidx); /* copy diffs to where read wants*/
615 memcpy( readpp->diffbuf, &((*p_pp)->buf[0]), diffidx);
616 diffidx = ISAMD_BLOCK_OFFSET_N;
617 if (readpp->is->method->debug >1) { //!!! 3
618 logf(LOG_LOG,"isamd_merge:separate diffs at ix=%d",
620 logf(LOG_LOG,"isamd_merge: dbuf=%p (from %p) pp=%p",
621 readpp->diffbuf, &((*p_pp)->buf[0]), (*p_pp) );
625 { /* integrated diffs */
626 assert ( *p_pp == *p_firstpp ); /* can only be in the first block */
627 diffidx=readpp->size;
628 readpp->diffs = diffidx*2+0;
629 readpp->diffbuf=readpp->buf; /*? does this get freed right?? */
630 if (readpp->is->method->debug >1) //!!! 3
631 logf(LOG_LOG,"isamd_merge:local diffs at %d: %s",
632 diffidx,hexdump(&(readpp->diffbuf[diffidx]),8,0));
635 getDiffInfo(readpp,diffidx);
636 if (readpp->is->method->debug >3)
637 logf(LOG_LOG,"isamd_merge: diffinfo=%p", readpp->diffinfo);
641 { /* we had a separate diff block, release it, we have copied the data */
642 isamd_release_block(readpp->is, readpp->cat, killblk);
643 isamd_pp_close (*p_pp);
644 if (readpp->is->method->debug >1) // 3 !!!
645 logf(LOG_LOG,"isamd_merge: released diff block %d=%d:%d",
646 isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
650 /* release our data block. Do before reading, when pos is stable ! */
653 isamd_release_block(readpp->is, readpp->cat, killblk);
654 if (readpp->is->method->debug >1) //!!! 3
655 logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
656 isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
658 r_ptr= (char *) &r_key;
659 r_more = isamd_read_item( readpp, &r_ptr);
661 { /* oops, all data has been deleted! what to do??? */
662 /* never mind, we have at least one more delta to add to the block */
663 /* pray that is not a delete as well... */
666 if (readpp->is->method->debug >1)
667 logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
669 assert (readpp->numKeys == 0);
673 /* set up the new blocks for simple writing */
674 firstpp=pp=isamd_pp_open(readpp->is,isamd_addr(0, readpp->is->max_cat));
675 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
676 encoder_data = (*pp->is->method->code_start)(ISAMD_ENCODE);
680 if (readpp->is->method->debug >4)
681 logf(LOG_LOG,"isamd_merge: got key %d.%d",
682 r_key.sysno, r_key.seqno );
683 pp= append_main_item(firstpp, pp, &r_key, encoder_data);
685 if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
686 { /* pos can get to 0 at end of main seq, if still diffs left...*/
687 if (readpp->is->method->debug >1) //!!! 3
688 logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
689 isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
690 isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
691 isamd_release_block(readpp->is, readpp->cat, readpp->pos);
695 /* (try to) read next item */
696 r_ptr= (char *) &r_key;
697 r_more = isamd_read_item( readpp, &r_ptr);
701 /* TODO: while pkey is an insert, and after last key inserted, append it */
702 /* will prevent multiple merges on large insert runs */
704 /* set things up so that merge can continue */
705 isamd_reduceblock(firstpp);
709 { /* the last data block is of no interest any more */
711 if (readpp->is->method->debug >1)
712 logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
713 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
717 if (readpp->is->method->debug >3)
718 logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
719 isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
721 isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
723 (*firstpp->is->method->code_stop)(ISAMD_ENCODE, encoder_data);
725 *p_firstpp = firstpp;
727 if (readpp->is->method->debug >1) //!!! 3
728 logf(LOG_LOG,"isamd_merge: merge ret %d=%d:%d nx=%d=%d:%d d=%d=2*%d+%d",
729 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
730 pp->next, isamd_type(pp->next), isamd_block(pp->next),
731 pp->diffs, pp->diffs/2, pp->diffs &1 );
738 /***************************************************************
740 ***************************************************************/
744 static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
746 struct it_key i_key; /* one input item */
747 char *i_item = (char *) &i_key; /* same as chars */
750 int i_mode; /* 0 for delete, 1 for insert */
760 char *c_ptr = codebuff;
764 firstpp=isamd_pp_open(is, ipos);
765 if (is->method->debug >1) //!!! 3
766 logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d d=%d=%d*2+%d nk=%d",
767 ipos, isamd_type(ipos), isamd_block(ipos),
768 firstpp->diffs, firstpp->diffs/2, firstpp->diffs & 1, firstpp->numKeys);
769 pp=read_diff_block(firstpp, &diffidx);
770 encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
771 maxsize = is->method->filecat[pp->cat].bsize;
773 difflenidx = diffidx;
774 diffidx+=sizeof(int); /* difflen will be stored here */
776 /* read first input */
778 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
780 if (is->method->debug >3)
781 logf(LOG_LOG,"isamd_appd: start with m=%d %s",
782 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
786 /* store the mode bit inside key */
787 assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
788 i_key.seqno = i_key.seqno * 2 + i_mode;
792 (*is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
793 codelen = c_ptr - codebuff;
794 assert ( (codelen<128) && (codelen>0));
795 if (is->method->debug >3)
796 logf(LOG_LOG,"isamd_appd: coded into %d: %s (nk=%d) (ix=%d)",
797 codelen, hexdump(codebuff, codelen,hexbuff),
798 firstpp->numKeys,diffidx);
800 if (diffidx + codelen > maxsize )
802 if (is->method->debug >3)
803 logf(LOG_LOG,"isamd_appd: block full (ix=%d mx=%d lix=%d)",
804 diffidx, maxsize, difflenidx);
805 if (is->method->debug >5)
806 logf(LOG_LOG,"isamd_appd: block pp=%p buf=%p [%d]:%s",
808 difflenidx, hexdump(&pp->buf[difflenidx],8,0));
809 merge_rc = merge (&firstpp, &pp, &i_key);
811 return merge_rc; /* merge handled them all ! */
813 /* set things up so we can continue */
814 pp = read_diff_block(firstpp, &diffidx);
815 (*is->method->code_reset)(encoder_data);
816 maxsize = is->method->filecat[pp->cat].bsize;
818 diffidx+=sizeof(int);
820 /* code the current input key again */
823 (*is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
824 codelen = c_ptr - codebuff;
825 assert ( (codelen<128) && (codelen>0));
826 if (is->method->debug >3)
827 logf(LOG_LOG,"isamd_appd: recoded into %d: %s (nk=%d) (ix=%d)",
828 codelen, hexdump(codebuff, codelen,hexbuff),
829 firstpp->numKeys,diffidx);
834 memcpy(&(pp->buf[diffidx]),codebuff,codelen);
837 firstpp->numKeys++; /* insert diff */
839 firstpp->numKeys--; /* delete diff */
841 /* update length of this diff run */
842 memcpy(&(pp->buf[difflenidx]),&diffidx,sizeof(diffidx));
844 firstpp->diffs =diffidx*2+0;
848 /* (try to) read the next input */
850 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
851 if ( (i_more) && (is->method->debug >3) )
852 logf(LOG_LOG,"isamd_appd: got m=%d %s",
853 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
856 /* clear the next difflen, if room for such */
857 difflenidx = diffidx;
858 while ( (difflenidx-diffidx<=sizeof(int)) && (difflenidx<maxsize))
859 pp->buf[difflenidx++]='\0';
862 (*firstpp->is->method->code_stop)(ISAMD_ENCODE, encoder_data);
863 return save_both_pps( firstpp, pp );
869 /*************************************************************
870 * isamd_append itself, Sweet, isn't it
871 *************************************************************/
873 ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
878 retval = isamd_build_first_block(is,data);
880 retval = append_diffs(is,ipos,data);
884 void *p1=xmalloc(100);
885 void *p2=xmalloc(100);
886 void *p3=xmalloc(100);
887 logf(LOG_LOG,"Traversing xmalloc stuff. p1=%p p2=%p p3=%p",p1,p2,p3);
888 xmalloc_trav("end of append"); /*!*/
897 * $Log: merge-d.c,v $
898 * Revision 1.9 1999-08-17 19:46:53 heikki
899 * Fixed a memory leak
901 * Revision 1.8 1999/08/07 11:30:59 heikki
902 * Bug fixing (still a mem leak somewhere)
904 * Revision 1.7 1999/08/04 14:21:18 heikki
905 * isam-d seems to be working.
907 * Revision 1.6 1999/07/23 15:43:05 heikki
908 * Hunted a few bugs in isam-d. Still crashes on the long test run
910 * Revision 1.5 1999/07/23 13:58:52 heikki
911 * merged closer to working, still fails on filling a separate, large block
913 * Revision 1.4 1999/07/21 14:53:55 heikki
914 * isamd read and write functions work, except when block full
915 * Merge missing still. Need to split some functions
917 * Revision 1.1 1999/07/14 13:14:47 heikki