2 * Copyright (c) 1996-1998, Index Data.
3 * See the file LICENSE for details.
6 * $Id: merge-d.c,v 1.20 1999-09-20 15:48:06 heikki Exp $
14 * - Input filter: Eliminate del-ins pairs, tell if only one entry (or none)
15 * - single-entry optimizing (keep the one entry in the dict, no block)
16 * - study and optimize block sizes (later)
17 * - find a way to decide the size of an empty diffblock (after merge)
18 * - On allocating more blocks (in append and merge), check the order of
19 * blocks, and if needed, swap them.
20 * - Write a routine to save/load indexes into a block, save only as many
21 * bytes as needed (size, diff, diffindexes)
25 * There is a confusion about the block addresses. cat or type is the category,
26 * pos or block is the block number. pp structures keep these two separate,
27 * and combine when saving the pp. The next pointer in the pp structure is
28 * also a combined address, but needs to be combined every time it is needed,
29 * and separated when the partss are needed... This is done with the isamd_
30 * _block, _type, and _addr macros. The _addr takes block and type as args,
31 * in that order. This conflicts with the order these are often mentioned in
32 * the debug log calls, and other places, leading to small mistakes here
35 * Needs cleaning! The way diff blocks are handled in append and reading is
36 * quite different, and likely to give maintenance problems.
38 * log levels (set isamddebug=x in zebra.cfg (or what ever cfg file you use) )
39 * 0 = no logging. Default
40 * 1 = no logging here. isamd logs overall statistics
41 * 2 = Each call to isamd_append with start address and no more
42 * 3 = Start and type of append, start of merge, and result of append
43 * 4 = Block allocations
44 * 5 = Block-level operations (read/write)
45 * 6 = Details about diff blocks etc.
46 * 7 = Log each record as it passes the system (once)
47 * 8 = Log raw and (de)coded data
48 * 9 = Anything else that may be useful
49 * .. = Anything needed to hunt a specific bug
50 * (note that all tests in the code are like debug>3, which means 4 or above!)
52 * Design for the new and improved isamd
54 * - The first block is only diffs, no straight data
55 * - Additional blocks are straight data
56 * - When a diff block gets filled up, a data block is created by
57 * merging the diffs with the data
60 * - Isamd_pp: buffer for diffs and for data
61 * keep both pos, type, and combined address
62 * routine to set the address
63 * - diffbuf: lengths as short ints, or bytes for small blocks
64 * - keys are of key_struct, not just a number of bytes.
68 * - create_new_block if needed
71 * - get diffend, start encoding
74 * - if no room, then realloc block in larger size
75 * - if still no room, merge and exit
76 * - append in the block
79 * - just as before, except that merges also input data directly
80 * - writes into new data blocks
83 * - isamd.c: load firstpp, load datablock
84 * save firstpp, save datablock
85 * - Readlength, writelength - handling right size of len fields
86 * - isamd_read_main_item: take also a merge input structure, and merge it too
87 * - prefilter: cache two inputs, and check if they cancel.
88 * - single-item optimization
90 * questions: Should we realloc firstblocks in a different size as the main
91 * blocks. Makes a sideways seek, which is bound to be slowe. But saves some
92 * update time. Compromise: alloc the first one in the size of the datablock,
93 * but increase if necessary. Large blocks get a large diff, ok. Small ones
94 * may get an extra seek in read, but save merges.
98 #define NEW_ISAM_D 1 /* not yet ready to delete the old one! */
105 #include "../index/index.h"
109 struct ISAMD_DIFF_s {
118 #define DT_NONE 0 // no diff, marks end of sequence
119 #define DT_DIFF 1 // ordinarry diff
120 #define DT_MAIN 2 // main data
121 #define DT_INPU 3 // input data to be merged
122 #define DT_DONE 4 // done with all input here
126 /***************************************************************
127 * Input preprocess filter
128 ***************************************************************/
130 struct ISAMD_FILTER_s {
131 ISAMD_I data; /* where the data comes from */
132 struct it_key k1; /* the next item to be returned */
133 struct it_key k2; /* the one after that */
136 static void init_filter( ISAMD_I data )
140 static void close_filter ()
144 static int filter_read( struct it_key *k)
149 static int filter_empty()
154 static int filter_only_one()
160 /***************************************************************
161 * General support routines
162 ***************************************************************/
166 static char *hexdump(unsigned char *p, int len, char *buff) {
167 static char localbuff[128];
169 if (!buff) buff=localbuff;
172 sprintf(bytebuff,"%02x",*p);
174 strcat(buff,bytebuff);
175 if (len) strcat(buff,",");
182 static void isamd_reduceblock(ISAMD_PP pp)
183 /* takes a large block, and reduces its category if possible */
184 /* Presumably the first block in an isam-list */
187 return; /* existing block, do not touch */
188 /* TODO: Probably we may touch anyway? */
189 if (pp->is->method->debug > 5)
190 logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
191 pp->pos, pp->cat, pp->size);
192 while ( ( pp->cat > 0 ) && (!pp->next) &&
193 (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
195 pp->pos = isamd_alloc_block(pp->is, pp->cat);
196 if (pp->is->method->debug > 5)
197 logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
198 pp->pos, pp->cat, pp->size);
202 static int save_first_pp ( ISAMD_PP firstpp)
204 isamd_buildfirstblock(firstpp);
205 isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
206 return isamd_addr(firstpp->pos,firstpp->cat);
210 static void save_last_pp (ISAMD_PP pp)
212 pp->next = 0;/* just to be sure */
213 isamd_buildlaterblock(pp);
214 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
218 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
220 /* order of things: Better to save firstpp first, if there are just two */
221 /* blocks, but last if there are blocks in between, as these have already */
222 /* been saved... optimise later (that's why this is in its own func...*/
223 int retval = save_first_pp(firstpp);
228 isamd_pp_close(firstpp);
230 } /* save_both_pps */
235 /***************************************************************
237 ***************************************************************/
239 void isamd_free_diffs(ISAMD_PP pp)
242 if (pp->is->method->debug > 5)
243 logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
246 for (i=0;pp->diffinfo[i].difftype!=DT_NONE;i++)
247 if(pp->diffinfo[i].decodeData)
249 if (pp->is->method->debug > 8)
250 logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i,
251 pp->diffinfo[i].decodeData);
252 (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
255 if (pp->diffbuf != pp->buf)
259 } /* isamd_free_diffs */
262 static void getDiffInfo(ISAMD_PP pp )
263 { /* builds the diff info structures from a diffblock */
264 int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +2;
265 /* Each diff takes at least 5 bytes. Probably more, but this is safe */
266 int i=1; /* [0] is used for the main data, [n+1] for merge inputs */
267 int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
268 int maxsz = pp->is->method->filecat[pp->is->max_cat].bsize;
269 int diffidx = ISAMD_BLOCK_OFFSET_1;
271 pp->diffinfo = xmalloc( diffsz );
272 pp->offset = pp->size+1; /* used this block up */
273 memset(pp->diffinfo,'\0',diffsz);
274 if (pp->is->method->debug > 5)
275 logf(LOG_LOG,"isamd_getDiffInfo: %d=%d:%d->%d, ix=%d mx=%d",
276 isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->next,
279 /* duplicate the buffer for diffs */
280 /* (so that we can read the next real buffer(s) */
281 assert(0==pp->diffbuf);
282 pp->diffbuf=xmalloc(maxsz);
283 memcpy(pp->diffbuf, pp->buf, maxsz);
285 pp->diffinfo[0].maxidx=-1; /* mark as special */
286 pp->diffinfo[0].difftype=DT_MAIN;
290 if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
292 if (pp->is->method->debug > 5)
293 logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
295 return; /* whole block done */
297 memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
298 pp->diffinfo[i].difftype=DT_DIFF;
299 if (pp->is->method->debug > 5)
300 logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
301 pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
303 if ( (pp->is->method->debug > 0) &&
304 (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize) )
305 { /* bug-hunting, this fails on some long runs that log too much */
306 logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d",
307 __FILE__,__LINE__, diffidx);
308 logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
309 pp->is->method->filecat[pp->cat].bsize);
310 logf(LOG_LOG,"pp=%d=%d:%d pp->nx=%d=%d:%d",
311 isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
312 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
314 assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
316 if (0==pp->diffinfo[i].maxidx)
318 if (pp->is->method->debug > 5) //!!! 4
319 logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
321 return; /* end marker */
323 diffidx += sizeof(int);
324 pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
325 pp->diffinfo[i].diffidx = diffidx;
326 if (pp->is->method->debug > 5)
327 logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
328 i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
329 hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
330 diffidx=pp->diffinfo[i].maxidx;
331 if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
332 return; /* whole block done */
335 assert (!"too many diff sequences in the block");
338 /***************************************************************
339 * Main block operations
340 ***************************************************************/
343 static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
344 { /* allocates a new block for the main data, and links it in */
346 if (0 == firstpp->next)
347 { /* special case, pp not yet allocated. */
348 /*Started as largest size, that's fine */
349 pp->pos = isamd_alloc_block(pp->is,pp->cat);
350 firstpp->next = isamd_addr(pp->pos,pp->cat);
351 if (pp->is->method->debug >3)
352 logf(LOG_LOG,"isamd_build: Alloc 1. dblock p=%d=%d:%d",
353 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
355 newblock=isamd_alloc_block(pp->is,pp->cat);
356 pp->next=isamd_addr(newblock,pp->cat);
357 isamd_buildlaterblock(pp);
358 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
359 if (pp->is->method->debug >3)
360 logf(LOG_LOG,"isamd_build: Alloc nxt %d=%d:%d -> %d=%d:%d",
361 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
362 isamd_addr(newblock,pp->cat), pp->cat, newblock);
365 pp->size=pp->offset=ISAMD_BLOCK_OFFSET_N;
367 } /* get_new_main_block */
370 static ISAMD_PP append_main_item(ISAMD_PP firstpp,
372 struct it_key *i_key)
373 { /* appends one item in the main data block, allocates new if needed */
374 char *i_item= (char *) i_key; /* same as char */
377 char *c_ptr = codebuff;
381 int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
385 (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
387 codelen = c_ptr - codebuff;
388 assert ( (codelen<128) && (codelen>0));
389 if (pp->is->method->debug >7)
390 logf(LOG_LOG,"isamd:build: coded %s nk=%d,ofs=%d-%d",
391 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1,
392 pp->offset, pp->offset+codelen);
394 if (pp->offset + codelen > maxsize )
395 { /* oops, block full - get a new one */
396 pp = get_new_main_block( firstpp, pp );
397 /* reset encoging and code again */
398 (*pp->is->method->code_reset)(pp->decodeClientData);
401 (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
403 codelen = c_ptr - codebuff;
404 assert ( (codelen<128) && (codelen>0));
405 if (pp->is->method->debug >7)
406 logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
407 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
410 assert (pp->offset + codelen <= maxsize );
412 /* write the data into pp, now we must have room */
413 memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
414 pp->offset += codelen;
417 /* clear the next 4 bytes in block, to avoid confusions with diff lens */
418 /* dirty, it should not be done here, but something slips somewhere, and */
419 /* I hope this fixes it... - Heikki */
420 codelen = pp->offset;
421 while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
422 pp->buf[codelen++] = '\0';
424 } /* append_main_item */
427 /***************************************************************
429 ***************************************************************/
431 /* Reads one item and corrects for the diffs, if any */
432 /* return 1 for ok, 0 for eof */
433 int isamd_read_item_merge (
436 struct it_key *p_key, /* the data item that didn't fit*/
437 ISAMD_I data) /* more input data comes here */
438 { /* The last two args can be null for ordinary reads */
442 int winner=0; /* which diff holds the day */
443 int i; /* looping diffs */
453 for(i=1; pp->diffinfo[i].difftype!=DT_NONE; i++)
454 ; /* find last diff */
456 { /* we have an extra item to inject into the merge */
457 if (pp->is->method->debug >9) //!!!!!
458 logf(LOG_LOG,"isamd_read_item: going to merge with %d.%d",
459 p_key->sysno, p_key->seqno);
460 pp->diffinfo[i].key = *p_key; /* the key merge could not handle */
461 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
462 pp->diffinfo[i].key.seqno >>= 1;
463 pp->diffinfo[i].difftype=DT_INPU;
464 if (pp->is->method->debug > 7)
465 logf(LOG_LOG,"isamd_read_item: inpu key %d sys=%d seq=%d=2*%d+%d",
467 pp->diffinfo[i].key.seqno*2 + pp->diffinfo[1].mode,
468 pp->diffinfo[i].key.seqno,
469 pp->diffinfo[i].mode);
470 p_key->sysno=p_key->seqno=0; /* used it up */
474 { /* we have a whole input stream to inject */
475 pp->diffinfo[i].difftype=DT_INPU;
486 if (0==pp->diffinfo[0].key.sysno)
487 { /* 0 is special case, main data. */
489 keyptr=(char*) &(pp->diffinfo[0].key);
490 pp->diffinfo[0].mode = ! isamd_read_main_item(pp,&keyptr);
491 if (pp->is->method->debug > 7)
492 logf(LOG_LOG,"isamd_read_item: read main at %d-%d %d.%d (%x.%x)",
494 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno,
495 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno);
496 } /* get main data */
498 if ( (0==pp->diffinfo[1].key.sysno) && (-1==pp->diffinfo[1].maxidx) )
499 { /* 1 is another special case, the input data at merge */
500 keyptr = (char *) &pp->diffinfo[1].key;
501 i = (*data->read_item)(data->clientData, &keyptr, &pp->diffinfo[1].mode);
503 { /* did not get it */
504 pp->diffinfo[1].key.sysno=0;
505 pp->diffinfo[1].maxidx=0; /* signal the end */
507 if (pp->is->method->debug >7)
508 logf(LOG_LOG,"merge: read diff m=%d %d.%d (%x.%x)",
509 pp->diffinfo[1].mode,
510 pp->diffinfo[1].key.sysno, pp->diffinfo[1].key.seqno,
511 pp->diffinfo[1].key.sysno, pp->diffinfo[1].key.seqno );
512 } /* get input data */
518 for (i=0; (!retry) && (pp->diffinfo[i].difftype); i++)
520 if (0==pp->diffinfo[i].key.sysno)
521 {/* read a new one, if possible */
522 if ((pp->diffinfo[i].difftype==DT_DIFF) &&
523 (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
524 { /* a normal kind of diff */
525 oldoffs=pp->diffinfo[i].diffidx;
526 codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
527 keyptr=(char *)&(pp->diffinfo[i].key);
528 (*pp->is->method->code_item)(ISAMD_DECODE,
529 pp->diffinfo[i].decodeData, &keyptr, &codeptr);
530 pp->diffinfo[i].diffidx += codeptr-codestart;
531 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
532 pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
533 if (pp->is->method->debug > 9)
534 logf(LOG_LOG,"isamd_read_item: dif[%d] at %d-%d: %s",
535 i,oldoffs, pp->diffinfo[i].diffidx,
536 hexdump(pp->buf+oldoffs, pp->diffinfo[i].diffidx-oldoffs,0));
537 if (pp->is->method->debug > 7)
538 logf(LOG_LOG,"isamd_read_item: rd dif[%d] %d.%d (%x.%x)",
540 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
541 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
543 else if ( pp->diffinfo[i].difftype==DT_MAIN)
544 { /* read a main item */
545 assert(i==0); /* main data goes before any diffs */
547 keyptr=(char*) &(pp->diffinfo[0].key);
548 rc= isamd_read_main_item(pp,&keyptr);
551 if (pp->is->method->debug > 7)
552 logf(LOG_LOG,"isamd_read_item: eof (rc=%d) main ",
554 pp->diffinfo[i].maxidx=-1;
555 pp->diffinfo[i].key.sysno=0;
556 pp->diffinfo[i].key.seqno=0;
557 pp->diffinfo[i].difftype= DT_DONE;
561 pp->diffinfo[i].mode = 1;
562 if (pp->is->method->debug > 7)
563 logf(LOG_LOG,"isamd_read_item: rd main %d-%d %d.%d (%x.%x) m=%d",
565 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
566 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
567 pp->diffinfo[i].mode);
570 else if (pp->diffinfo[i].difftype==DT_INPU)
572 keyptr = (char *) &pp->diffinfo[i].key;
573 rc = (*data->read_item)(data->clientData, &keyptr, &pp->diffinfo[i].mode);
575 { /* did not get it */
576 pp->diffinfo[i].key.sysno=0;
577 pp->diffinfo[i].maxidx=0; /* signal the end */
578 pp->diffinfo[i].difftype=DT_DONE;
580 if (pp->is->method->debug >7)
581 logf(LOG_LOG,"merge: read inpu m=%d %d.%d (%x.%x)",
582 pp->diffinfo[i].mode,
583 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
584 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno );
585 } /* read an input item */
586 } /* read a new one */
588 if (pp->is->method->debug > 8)
589 logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
590 i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
591 pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
593 if ( 0!= pp->diffinfo[i].key.sysno)
594 { /* got a key, compare */
596 cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
599 if (0==pp->diffinfo[winner].key.sysno)
600 cmp=-1; /* end of main sequence, take all diffs */
603 if (pp->is->method->debug > 8)
604 logf(LOG_LOG,"isamd_read_item: ins [%d]%d.%d < [%d]%d.%d",
606 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
608 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
609 if (pp->diffinfo[i].mode) /* insert diff, should always be */
613 if (pp->is->method->debug > 1)
614 logf(LOG_LOG,"delete diff for nonexisting item");
615 assert(!"delete diff for nonexisting item");
616 /* is an assert too steep here? Not really.*/
621 if (!pp->diffinfo[i].mode) /* delete diff. should always be */
623 if (pp->is->method->debug > 8)
624 logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
626 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
627 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
628 pp->diffinfo[winner].key.sysno=0; /* delete it */
631 if (pp->is->method->debug > 2)
632 logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
634 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
635 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
636 /* skip the insert, since we already have it in the base */
637 /* Should we fail an assertion here??? */
638 pp->diffinfo[i].key.sysno=0; /* done with the delete */
639 retry=1; /* start all over again */
641 /* else it is a later key, its turn will come */
643 } /* for each diff */
646 if ( pp->diffinfo[winner].key.sysno)
648 if (pp->is->method->debug > 7)
649 logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
651 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
652 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
653 memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
654 *dst += sizeof(struct it_key);
655 pp->diffinfo[winner].key.sysno=0; /* used that one up */
660 if (pp->is->method->debug > 7)
661 logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
663 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
664 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
665 assert(winner==0); /* if nothing found, nothing comes from a diff */
668 if (pp->is->method->debug >9)
669 logf(LOG_LOG,"mergeDB4: sysno[1]=%d", pp->diffinfo[1].key.sysno); /*!*/
672 } /* isamd_read_item */
675 int isamd_read_item (ISAMD_PP pp, char **dst)
677 return isamd_read_item_merge(pp,dst,0,0);
681 /***************************************************************
683 ***************************************************************/
685 static int merge ( ISAMD_PP firstpp, /* first pp (with diffs) */
686 struct it_key *p_key, /* the data item that didn't fit*/
687 ISAMD_I data) /* more input data comes here */
695 ISAMD_PP readpp=firstpp;
697 int diffcat = firstpp->cat; /* keep the category of the diffblock even */
698 /* if it is going to be empty now. */
699 /* Alternative: Make it the minimal, and */
700 /* resize later. Saves disk, but will lead */
701 /* into bad seeks. */
703 ++(readpp->is->files[0].no_merges);
705 /* set up diffs as they should be for reading */
706 diffidx = ISAMD_BLOCK_OFFSET_1;
707 //readpp->diffbuf=readpp->buf; // diffinfo has to duplicate it!
708 //getDiffInfo(readpp); // first read will make the diffinfo, at init
710 if (readpp->is->method->debug >4)
711 logf(LOG_LOG,"isamd_merge: f=%d=%d:%d n=%d=%d:%d",
712 isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos,
713 firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next));
715 /* release our data block. Do before reading, when pos is stable ! */
716 killblk=firstpp->pos;
719 isamd_release_block(firstpp->is, firstpp->cat, killblk);
720 if (readpp->is->method->debug >3)
721 logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
722 isamd_addr(killblk,firstpp->cat), firstpp->cat, killblk );
726 r_ptr= (char *) &r_key;
727 r_more = isamd_read_item_merge( readpp, &r_ptr, p_key, data);
729 { /* oops, all data has been deleted! what to do??? */
730 /* never mind, we have at least one more delta to add to the block */
731 /* pray that is not a delete as well... */
734 if (readpp->is->method->debug >5)
735 logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
737 //assert (readpp->numKeys == 0); /* no longer true! */
741 /* set up the new blocks for simple writing */
742 firstpp=isamd_pp_open(readpp->is,isamd_addr(0, diffcat));
743 firstpp->pos=isamd_alloc_block(firstpp->is,diffcat);
744 if (readpp->is->method->debug >3)
745 logf(LOG_LOG,"isamd_merge: allocated new firstpp %d=%d:%d",
746 isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos );
748 pp=isamd_pp_open(readpp->is,isamd_addr(0,readpp->is->max_cat) );
749 pp->offset=pp->size=ISAMD_BLOCK_OFFSET_N;
753 if (readpp->is->method->debug >6)
754 logf(LOG_LOG,"isamd_merge: got key %d.%d",
755 r_key.sysno, r_key.seqno );
756 pp= append_main_item(firstpp, pp, &r_key);
758 if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
759 { /* pos can get to 0 at end of main seq, if still diffs left...*/
760 if (readpp->is->method->debug >3)
761 logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
762 isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
763 isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
764 isamd_release_block(readpp->is, readpp->cat, readpp->pos);
768 /* (try to) read next item */
769 r_ptr= (char *) &r_key;
770 r_more = isamd_read_item_merge( readpp, &r_ptr,0,data);
778 isamd_reduceblock(pp); /* reduce size if possible */
779 if (0==firstpp->next)
780 firstpp->next = isamd_addr(pp->pos,pp->cat);
782 if (readpp->is->method->debug >4)
783 logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
784 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
787 if (readpp->is->method->debug >5)
788 logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
789 isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
791 isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
793 if (readpp->is->method->debug >2)
794 logf(LOG_LOG,"isamd_merge: merge ret f=%d=%d:%d pp=%d=%d:%d",
795 isamd_addr(firstpp->pos,pp->cat), firstpp->cat, firstpp->pos,
796 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
798 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1; /* nothing there */
799 memset(firstpp->buf,'\0',firstpp->is->method->filecat[firstpp->cat].bsize);
800 save_first_pp(firstpp);
801 retval = isamd_addr(firstpp->pos, firstpp->cat);
802 isamd_pp_close(firstpp);
811 /***************************************************************
813 ***************************************************************/
817 static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
819 struct it_key i_key; /* one input item */
820 char *i_item = (char *) &i_key; /* same as chars */
823 int i_mode; /* 0 for delete, 1 for insert */
831 char *c_ptr = codebuff;
838 firstpp=isamd_pp_open(is, isamd_addr(0,0) );
839 firstpp->size=firstpp->offset=ISAMD_BLOCK_OFFSET_1;
840 /* create in smallest category, will expand later */
841 ++(is->files[0].no_fbuilds);
845 firstpp=isamd_pp_open(is, ipos);
846 ++(is->files[0].no_appds);
849 if (is->method->debug >2)
850 logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d n=%d=%d:%d nk=%d",
851 ipos, isamd_type(ipos), isamd_block(ipos),
852 firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next),
854 maxsize = is->method->filecat[firstpp->cat].bsize;
856 difflenidx = diffidx = firstpp->size;
858 diffidx+=sizeof(int); /* difflen will be stored here */
860 /* read first input */
862 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
864 if (is->method->debug >6)
865 logf(LOG_LOG,"isamd_appd: start m=%d %d.%d=%x.%x: %d",
867 i_key.sysno, i_key.seqno,
868 i_key.sysno, i_key.seqno,
869 i_key.sysno*2+i_mode);
873 /* store the mode bit inside key */
874 assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
875 i_key.seqno = i_key.seqno * 2 + i_mode;
879 (*is->method->code_item)(ISAMD_ENCODE, firstpp->decodeClientData,
881 codelen = c_ptr - codebuff;
882 assert ( (codelen<128) && (codelen>0));
883 if (is->method->debug >7)
884 logf(LOG_LOG,"isamd_appd: coded %d: %s (nk=%d) (ix=%d)",
885 codelen, hexdump(codebuff, codelen,hexbuff),
886 firstpp->numKeys,diffidx);
888 if (diffidx + codelen > maxsize )
890 if (firstpp->cat < firstpp->is->max_cat)
891 { /* just increase the block size */
892 if (firstpp->pos > 0) /* free the old block if allocated */
893 isamd_release_block(is, firstpp->cat, firstpp->pos);
895 maxsize = is->method->filecat[firstpp->cat].bsize;
896 firstpp->pos=0; /* need to allocate it when saving */
897 if (is->method->debug >3)
898 logf(LOG_LOG,"isamd_appd: increased diff block to %d (%d)",
899 firstpp->cat, maxsize);
902 { /* max size already - can't help, need to merge it */
903 if (is->method->debug >7)
904 logf(LOG_LOG,"isamd_appd: block full");
905 if (is->method->debug >9) //!!!!!
906 logf(LOG_LOG,"isamd_appd: going to merge with m=%d %d.%d",
907 i_mode, i_key.sysno, i_key.seqno);
908 merge_rc = merge (firstpp, &i_key, data);
910 return merge_rc; /* merge handled them all ! */
911 assert(!"merge returned zero ??");
912 } /* need to merge */
915 assert ( diffidx+codelen <= maxsize );
918 memcpy(&(firstpp->buf[diffidx]),codebuff,codelen);
920 firstpp->size = firstpp->offset = diffidx;
923 firstpp->numKeys++; /* insert diff */
925 firstpp->numKeys--; /* delete diff */
927 /* update length of this diff run */
928 memcpy(&(firstpp->buf[difflenidx]),&diffidx,sizeof(diffidx));
930 /* (try to) read the next input */
932 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
933 if ( (i_more) && (is->method->debug >6) )
934 logf(LOG_LOG,"isamd_appd: got m=%d %d.%d=%x.%x: %d",
936 i_key.sysno, i_key.seqno,
937 i_key.sysno, i_key.seqno,
938 i_key.sysno*2+i_mode);
941 /* clear the next difflen, if room for such */
942 difflenidx = diffidx;
943 while ( (difflenidx-diffidx<=sizeof(int)+1) && (difflenidx<maxsize))
944 firstpp->buf[difflenidx++]='\0';
946 if (0==firstpp->pos) /* need to (re)alloc the block */
947 firstpp->pos = isamd_alloc_block(is, firstpp->cat);
949 retval = save_first_pp( firstpp );
950 isamd_pp_close(firstpp);
958 /*************************************************************
959 * isamd_append itself, Sweet, isn't it
960 *************************************************************/
962 ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
964 return append_diffs(is,ipos,data);
974 * $Log: merge-d.c,v $
975 * Revision 1.20 1999-09-20 15:48:06 heikki
978 * Revision 1.19 1999/09/13 13:28:28 heikki
979 * isam-d optimizing: merging input data in the same go
981 * Revision 1.18 1999/08/25 18:09:24 heikki
982 * Starting to optimize
984 * Revision 1.17 1999/08/24 13:17:42 heikki
985 * Block sizes, comments
987 * Revision 1.16 1999/08/24 10:12:02 heikki
988 * Comments about optimising
990 * Revision 1.15 1999/08/22 08:26:34 heikki
993 * Revision 1.14 1999/08/20 12:25:58 heikki
994 * Statistics in isamd
996 * Revision 1.13 1999/08/18 13:59:19 heikki
997 * Fixed another unlikely difflen bug
999 * Revision 1.12 1999/08/18 13:28:17 heikki
1000 * Set log levels to decent values
1002 * Revision 1.11 1999/08/18 10:37:11 heikki
1003 * Fixed (another) difflen bug
1005 * Revision 1.10 1999/08/18 09:13:31 heikki
1008 * Revision 1.9 1999/08/17 19:46:53 heikki
1009 * Fixed a memory leak
1011 * Revision 1.8 1999/08/07 11:30:59 heikki
1012 * Bug fixing (still a mem leak somewhere)
1014 * Revision 1.7 1999/08/04 14:21:18 heikki
1015 * isam-d seems to be working.
1017 * Revision 1.6 1999/07/23 15:43:05 heikki
1018 * Hunted a few bugs in isam-d. Still crashes on the long test run
1020 * Revision 1.5 1999/07/23 13:58:52 heikki
1021 * merged closer to working, still fails on filling a separate, large block
1023 * Revision 1.4 1999/07/21 14:53:55 heikki
1024 * isamd read and write functions work, except when block full
1025 * Merge missing still. Need to split some functions
1027 * Revision 1.1 1999/07/14 13:14:47 heikki