1 /* $Id: merge-d.c,v 1.29 2002-11-26 22:18:34 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #define NEW_ISAM_D 1 /* not yet ready to delete the old one! */
33 #include "../index/index.h"
46 #define DT_NONE 0 /* no diff, marks end of sequence */
47 #define DT_DIFF 1 /* ordinarry diff */
48 #define DT_MAIN 2 /* main data */
49 #define DT_INPU 3 /* input data to be merged */
50 #define DT_DONE 4 /* done with all input here */
54 /***************************************************************
55 * Input preprocess filter
56 ***************************************************************/
59 #define FILTER_NOTYET -1 /* no data read in yet, to be done */
61 struct ISAMD_FILTER_s {
62 ISAMD_I data; /* where the data comes from */
63 ISAMD is; /* for debug flags */
64 struct it_key k1; /* the next item to be returned */
65 int m1; /* mode for k1 */
66 int r1; /* result for read of k1, or NOTYET */
67 struct it_key k2; /* the one after that */
72 typedef struct ISAMD_FILTER_s *FILTER;
75 void filter_fill(FILTER F)
77 while ( (F->r1 == FILTER_NOTYET) || (F->r2 == FILTER_NOTYET) )
79 if (F->r1==FILTER_NOTYET)
80 { /* move data forward in the filter */
84 if ( 0 != F->r1 ) /* not eof */
85 F->r2 = FILTER_NOTYET; /* say we want more */
86 if (F->is->method->debug > 9)
87 logf(LOG_LOG,"filt_fill: shift %d.%d m=%d r=%d",
92 if (F->r2==FILTER_NOTYET)
93 { /* read new bottom value */
94 char *k_ptr = (char*) &F->k2;
95 F->r2 = (F->data->read_item)(F->data->clientData, &k_ptr, &F->m2);
96 if (F->is->method->debug > 9)
97 logf(LOG_LOG,"filt_fill: read %d.%d m=%d r=%d",
98 F->k2.sysno, F->k2.seqno, F->m2, F->r2);
100 if ( (F->k1.sysno == F->k2.sysno) &&
101 (F->k1.seqno == F->k2.seqno) &&
103 (F->r1 >0 ) && (F->r2 >0) )
104 { /* del-ins pair of same key (not eof) , ignore both */
105 if (F->is->method->debug > 9)
106 logf(LOG_LOG,"filt_fill: skipped %d.%d m=%d/%d r=%d/%d",
107 F->k1.sysno, F->k1.seqno,
108 F->m1,F->m2, F->r1,F->r2);
109 F->r1 = FILTER_NOTYET;
110 F->r2 = FILTER_NOTYET;
116 FILTER filter_open( ISAMD is, ISAMD_I data )
118 FILTER F = (FILTER) xmalloc(sizeof(struct ISAMD_FILTER_s));
125 F->r1 = F->r2 = FILTER_NOTYET;
130 static void filter_close (FILTER F)
135 static int filter_read( FILTER F,
141 if (F->is->method->debug > 9)
142 logf(LOG_LOG,"filt_read: reading %d.%d m=%d r=%d",
143 F->k1.sysno, F->k1.seqno, F->m1, F->r1);
150 F->r1 = FILTER_NOTYET;
154 static int filter_isempty(FILTER F)
156 return ( (0 == F->r1) && (0 == F->r2)) ;
159 static int filter_only_one(FILTER F)
161 return ( (0 != F->r1) && (0 == F->r2));
164 /* We may need backfilling, if we read a lonely key to make */
165 /* a singleton, but its bitw will not fit in. Then we need to */
166 /* process it normally, which means reading it again. So we */
167 /* need to unread it first. Luckily the filter is empty at that */
169 static void filter_backfill(FILTER F, struct it_key *k, int mode)
171 assert(F->r1 == FILTER_NOTYET ); /* not overwriting data! */
174 F->r1=1; /* ok read */
178 /***************************************************************
180 ***************************************************************/
181 /* When there is only a single item, we don't allocate a block
182 * for it, but code it in the directory entry directly, if it
186 #define DEC_SYSBITS 15
187 #define DEC_SEQBITS 15
188 #define DEC_MASK(n) ((1<<(n))-1)
190 #define SINGLETON_BIT (1<<(DEC_SYSBITS+DEC_SEQBITS+1))
192 int is_singleton(ISAMD_P ipos)
194 return 0; /* no singletons any more */
195 return ( ipos != 0 ) && ( ipos & SINGLETON_BIT );
199 int singleton_encode(struct it_key *k)
200 /* encodes the key into one int. If it does not fit, returns 0 */
202 return 0; /* no more singletons */
203 if ( (k->sysno & DEC_MASK(DEC_SYSBITS) ) != k->sysno )
204 return 0; /* no room dor sysno */
205 if ( (k->seqno & DEC_MASK(DEC_SYSBITS) ) != k->seqno )
206 return 0; /* no room dor sysno */
207 return (k->sysno | (k->seqno << DEC_SYSBITS) ) | SINGLETON_BIT;
210 void singleton_decode (int code, struct it_key *k)
212 assert (code & SINGLETON_BIT);
213 k->sysno = code & DEC_MASK(DEC_SYSBITS);
214 code = code >> DEC_SYSBITS;
215 k->seqno = code & DEC_MASK(DEC_SEQBITS);
219 /***************************************************************
220 * General support routines
221 ***************************************************************/
225 static char *hexdump(unsigned char *p, int len, char *buff) {
226 static char localbuff[128];
228 if (!buff) buff=localbuff;
231 sprintf(bytebuff,"%02x",*p);
233 strcat(buff,bytebuff);
234 if (len) strcat(buff,",");
241 static void isamd_reduceblock(ISAMD_PP pp)
242 /* takes a large block, and reduces its category if possible */
243 /* Presumably the first block in an isam-list */
246 return; /* existing block, do not touch */
247 /* TODO: Probably we may touch anyway? */
248 if (pp->is->method->debug > 5)
249 logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
250 pp->pos, pp->cat, pp->size);
251 while ( ( pp->cat > 0 ) && (!pp->next) &&
252 (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
254 pp->pos = isamd_alloc_block(pp->is, pp->cat);
255 if (pp->is->method->debug > 5)
256 logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
257 pp->pos, pp->cat, pp->size);
261 static int save_first_pp ( ISAMD_PP firstpp)
263 isamd_buildfirstblock(firstpp);
264 isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
265 return isamd_addr(firstpp->pos,firstpp->cat);
269 static void save_last_pp (ISAMD_PP pp)
271 pp->next = 0;/* just to be sure */
272 isamd_buildlaterblock(pp);
273 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
277 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
279 /* order of things: Better to save firstpp first, if there are just two */
280 /* blocks, but last if there are blocks in between, as these have already */
281 /* been saved... optimise later (that's why this is in its own func...*/
282 int retval = save_first_pp(firstpp);
287 isamd_pp_close(firstpp);
289 } /* save_both_pps */
294 /***************************************************************
296 ***************************************************************/
298 void isamd_free_diffs(ISAMD_PP pp)
301 if (pp->is->method->debug > 5)
302 logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
305 for (i=0;pp->diffinfo[i].difftype!=DT_NONE;i++)
306 if(pp->diffinfo[i].decodeData)
308 if (pp->is->method->debug > 8)
309 logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i,
310 pp->diffinfo[i].decodeData);
311 (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
314 if (pp->diffbuf != pp->buf)
318 } /* isamd_free_diffs */
321 static void getDiffInfo(ISAMD_PP pp )
322 { /* builds the diff info structures from a diffblock */
323 int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +2;
324 /* Each diff takes at least 5 bytes. Probably more, but this is safe */
325 int i=1; /* [0] is used for the main data, [n+1] for merge inputs */
326 int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
327 int maxsz = pp->is->method->filecat[pp->is->max_cat].bsize;
328 int diffidx = ISAMD_BLOCK_OFFSET_1;
330 pp->diffinfo = xmalloc( diffsz );
331 pp->offset = pp->size+1; /* used this block up */
332 memset(pp->diffinfo,'\0',diffsz);
333 if (pp->is->method->debug > 5)
334 logf(LOG_LOG,"isamd_getDiffInfo: %d=%d:%d->%d, ix=%d mx=%d",
335 isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->next,
338 /* duplicate the buffer for diffs */
339 /* (so that we can read the next real buffer(s) */
340 assert(0==pp->diffbuf);
341 pp->diffbuf=xmalloc(maxsz);
342 memcpy(pp->diffbuf, pp->buf, maxsz);
344 pp->diffinfo[0].maxidx=-1; /* mark as special */
345 pp->diffinfo[0].difftype=DT_MAIN;
349 if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
351 if (pp->is->method->debug > 5)
352 logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
354 return; /* whole block done */
356 memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
357 pp->diffinfo[i].difftype=DT_DIFF;
358 if (pp->is->method->debug > 5)
359 logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
360 pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
362 if ( (pp->is->method->debug > 0) &&
363 (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize) )
365 logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d",
366 __FILE__,__LINE__, diffidx);
367 logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
368 pp->is->method->filecat[pp->cat].bsize);
369 logf(LOG_LOG,"pp=%d=%d:%d pp->nx=%d=%d:%d",
370 isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
371 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
373 assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
375 if (0==pp->diffinfo[i].maxidx)
377 if (pp->is->method->debug > 5) /* !!! 4 */
378 logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
380 return; /* end marker */
382 diffidx += sizeof(int);
383 pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
384 pp->diffinfo[i].diffidx = diffidx;
385 if (pp->is->method->debug > 5)
386 logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
387 i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
388 hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
389 diffidx=pp->diffinfo[i].maxidx;
390 if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
391 return; /* whole block done */
394 assert (!"too many diff sequences in the block");
397 /***************************************************************
398 * Main block operations
399 ***************************************************************/
402 static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
403 { /* allocates a new block for the main data, and links it in */
405 if (0 == firstpp->next)
406 { /* special case, pp not yet allocated. */
407 /*Started as largest size, that's fine */
408 pp->pos = isamd_alloc_block(pp->is,pp->cat);
409 firstpp->next = isamd_addr(pp->pos,pp->cat);
410 if (pp->is->method->debug >3)
411 logf(LOG_LOG,"isamd_build: Alloc 1. dblock p=%d=%d:%d",
412 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
414 newblock=isamd_alloc_block(pp->is,pp->cat);
415 pp->next=isamd_addr(newblock,pp->cat);
416 isamd_buildlaterblock(pp);
417 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
418 if (pp->is->method->debug >3)
419 logf(LOG_LOG,"isamd_build: Alloc nxt %d=%d:%d -> %d=%d:%d",
420 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
421 isamd_addr(newblock,pp->cat), pp->cat, newblock);
424 pp->size=pp->offset=ISAMD_BLOCK_OFFSET_N;
426 } /* get_new_main_block */
429 static ISAMD_PP append_main_item(ISAMD_PP firstpp,
431 struct it_key *i_key)
432 { /* appends one item in the main data block, allocates new if needed */
433 char *i_item= (char *) i_key; /* same as char */
436 char *c_ptr = codebuff;
440 int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
444 (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
446 codelen = c_ptr - codebuff;
447 assert ( (codelen<128) && (codelen>0));
448 if (pp->is->method->debug >7)
449 logf(LOG_LOG,"isamd:build: coded %s nk=%d,ofs=%d-%d",
450 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1,
451 pp->offset, pp->offset+codelen);
453 if (pp->offset + codelen > maxsize )
454 { /* oops, block full - get a new one */
455 pp = get_new_main_block( firstpp, pp );
456 /* reset encoging and code again */
457 (*pp->is->method->code_reset)(pp->decodeClientData);
460 (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
462 codelen = c_ptr - codebuff;
463 assert ( (codelen<128) && (codelen>0));
464 if (pp->is->method->debug >7)
465 logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
466 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
469 assert (pp->offset + codelen <= maxsize );
471 /* write the data into pp, now we must have room */
472 memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
473 pp->offset += codelen;
476 /* clear the next 4 bytes in block, to avoid confusions with diff lens */
477 /* dirty, it should not be done here, but something slips somewhere, and */
478 /* I hope this fixes it... - Heikki */
479 codelen = pp->offset;
480 while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
481 pp->buf[codelen++] = '\0';
483 } /* append_main_item */
486 /***************************************************************
488 ***************************************************************/
490 /* Reads one item and corrects for the diffs, if any */
491 /* return 1 for ok, 0 for eof */
492 int isamd_read_item_merge (
495 struct it_key *p_key, /* the data item that didn't fit*/
496 /* ISAMD_I data) */ /* more input data comes here */
497 FILTER filt) /* more input data comes here */
498 { /* The last two args can be null for ordinary reads */
502 int winner=0; /* which diff holds the day */
503 int i; /* looping diffs */
513 for(i=1; pp->diffinfo[i].difftype!=DT_NONE; i++)
514 ; /* find last diff */
516 { /* we have an extra item to inject into the merge */
517 if (pp->is->method->debug >9) /* !!!!! */
518 logf(LOG_LOG,"isamd_read_item: going to merge with %d.%d",
519 p_key->sysno, p_key->seqno);
520 pp->diffinfo[i].key = *p_key; /* the key merge could not handle */
521 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
522 pp->diffinfo[i].key.seqno >>= 1;
523 pp->diffinfo[i].difftype=DT_INPU;
524 if (pp->is->method->debug > 7)
525 logf(LOG_LOG,"isamd_read_item: inpu key %d sys=%d seq=%d=2*%d+%d",
527 pp->diffinfo[i].key.seqno*2 + pp->diffinfo[1].mode,
528 pp->diffinfo[i].key.seqno,
529 pp->diffinfo[i].mode);
530 p_key->sysno=p_key->seqno=0; /* used it up */
534 { /* we have a whole input stream to inject */
535 pp->diffinfo[i].difftype=DT_INPU;
544 for (i=0; (!retry) && (pp->diffinfo[i].difftype); i++)
546 if (0==pp->diffinfo[i].key.sysno)
547 {/* read a new one, if possible */
548 if ((pp->diffinfo[i].difftype==DT_DIFF) &&
549 (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
550 { /* a normal kind of diff */
551 oldoffs=pp->diffinfo[i].diffidx;
552 codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
553 keyptr=(char *)&(pp->diffinfo[i].key);
554 (*pp->is->method->code_item)(ISAMD_DECODE,
555 pp->diffinfo[i].decodeData, &keyptr, &codeptr);
556 pp->diffinfo[i].diffidx += codeptr-codestart;
557 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
558 pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
559 if (pp->is->method->debug > 9)
560 logf(LOG_LOG,"isamd_read_item: dif[%d] at %d-%d: %s",
561 i,oldoffs, pp->diffinfo[i].diffidx,
562 hexdump(pp->buf+oldoffs, pp->diffinfo[i].diffidx-oldoffs,0));
563 if (pp->is->method->debug > 7)
564 logf(LOG_LOG,"isamd_read_item: rd dif[%d] %d.%d (%x.%x)",
566 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
567 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
569 else if ( pp->diffinfo[i].difftype==DT_MAIN)
570 { /* read a main item */
571 assert(i==0); /* main data goes before any diffs */
573 keyptr=(char*) &(pp->diffinfo[0].key);
574 rc= isamd_read_main_item(pp,&keyptr);
577 if (pp->is->method->debug > 7)
578 logf(LOG_LOG,"isamd_read_item: eof (rc=%d) main ",
580 pp->diffinfo[i].maxidx=-1;
581 pp->diffinfo[i].key.sysno=0;
582 pp->diffinfo[i].key.seqno=0;
583 pp->diffinfo[i].difftype= DT_DONE;
587 pp->diffinfo[i].mode = 1;
588 if (pp->is->method->debug > 7)
589 logf(LOG_LOG,"isamd_read_item: rd main %d-%d %d.%d (%x.%x) m=%d",
591 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
592 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
593 pp->diffinfo[i].mode);
596 else if (pp->diffinfo[i].difftype==DT_INPU)
598 keyptr = (char *) &pp->diffinfo[i].key;
599 /* rc = (*data->read_item)(data->clientData, &keyptr, &pp->diffinfo[i].mode); */
600 rc = filter_read(filt, &pp->diffinfo[i].key,
601 &pp->diffinfo[i].mode);
603 { /* did not get it */
604 pp->diffinfo[i].key.sysno=0;
605 pp->diffinfo[i].maxidx=0; /* signal the end */
606 pp->diffinfo[i].difftype=DT_DONE;
608 if (pp->is->method->debug >7)
609 logf(LOG_LOG,"merge: read inpu m=%d %d.%d (%x.%x)",
610 pp->diffinfo[i].mode,
611 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
612 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno );
613 } /* read an input item */
614 } /* read a new one */
616 if (pp->is->method->debug > 8)
617 logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
618 i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
619 pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
621 if ( 0!= pp->diffinfo[i].key.sysno)
622 { /* got a key, compare */
624 cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
627 if (0==pp->diffinfo[winner].key.sysno)
628 cmp=-1; /* end of main sequence, take all diffs */
631 if (pp->is->method->debug > 8)
632 logf(LOG_LOG,"isamd_read_item: ins [%d]%d.%d < [%d]%d.%d",
634 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
636 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
637 if (pp->diffinfo[i].mode) /* insert diff, should always be */
641 if (pp->is->method->debug > 1)
642 logf(LOG_LOG,"delete diff for nonexisting item");
643 assert(!"delete diff for nonexisting item");
644 /* is an assert too steep here? Not really.*/
649 if (!pp->diffinfo[i].mode) /* delete diff. should always be */
651 if (pp->is->method->debug > 8)
652 logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
654 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
655 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
656 pp->diffinfo[winner].key.sysno=0; /* delete it */
659 if (pp->is->method->debug > 2)
660 logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
662 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
663 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
664 /* skip the insert, since we already have it in the base */
665 /* Should we fail an assertion here??? */
666 pp->diffinfo[i].key.sysno=0; /* done with the delete */
667 retry=1; /* start all over again */
669 /* else it is a later key, its turn will come */
671 } /* for each diff */
674 if ( pp->diffinfo[winner].key.sysno)
676 if (pp->is->method->debug > 7)
677 logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
679 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
680 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
681 memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
682 *dst += sizeof(struct it_key);
683 pp->diffinfo[winner].key.sysno=0; /* used that one up */
688 if (pp->is->method->debug > 7)
689 logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
691 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
692 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
693 assert(winner==0); /* if nothing found, nothing comes from a diff */
697 ++(pp->is->no_read_keys);
699 ++(pp->is->no_read_eof);
703 } /* isamd_read_item */
706 int isamd_read_item (ISAMD_PP pp, char **dst)
708 return isamd_read_item_merge(pp,dst,0,0);
712 /***************************************************************
714 ***************************************************************/
716 static int merge ( ISAMD_PP firstpp, /* first pp (with diffs) */
717 struct it_key *p_key, /* the data item that didn't fit*/
718 FILTER filt, /* more input data arriving here */
719 char *dictentry, /* the thin in the dictionary */
720 int dictlen) /* and its size */
728 ISAMD_PP readpp=firstpp;
730 int diffcat = firstpp->cat; /* keep the category of the diffblock even */
731 /* if it is going to be empty now. */
732 /* Alternative: Make it the minimal, and */
733 /* resize later. Saves disk, but will lead */
734 /* into bad seeks. */
736 ++(readpp->is->no_merges);
738 /* set up diffs as they should be for reading */
739 diffidx = ISAMD_BLOCK_OFFSET_1;
741 if (readpp->is->method->debug >4)
742 logf(LOG_LOG,"isamd_merge: f=%d=%d:%d n=%d=%d:%d",
743 isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos,
744 firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next));
746 /* release our data block. Do before reading, when pos is stable ! */
747 killblk=firstpp->pos;
750 isamd_release_block(firstpp->is, firstpp->cat, killblk);
751 if (readpp->is->method->debug >3)
752 logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
753 isamd_addr(killblk,firstpp->cat), firstpp->cat, killblk );
757 r_ptr= (char *) &r_key;
758 r_more = isamd_read_item_merge( readpp, &r_ptr, p_key, filt);
760 { /* oops, all data has been deleted! what to do??? */
761 /* never mind, we have at least one more delta to add to the block */
762 /* pray that is not a delete as well... */
765 if (readpp->is->method->debug >5)
766 logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
771 /* set up the new blocks for simple writing */
772 /* firstpp=isamd_pp_open(readpp->is,isamd_addr(0, diffcat)); */
773 firstpp=isamd_pp_create(readpp->is, diffcat);
774 firstpp->pos=isamd_alloc_block(firstpp->is,diffcat);
775 if (readpp->is->method->debug >3)
776 logf(LOG_LOG,"isamd_merge: allocated new firstpp %d=%d:%d",
777 isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos );
779 pp=isamd_pp_create(readpp->is,readpp->is->max_cat );
780 pp->offset=pp->size=ISAMD_BLOCK_OFFSET_N;
784 if (readpp->is->method->debug >6)
785 logf(LOG_LOG,"isamd_merge: got key %d.%d",
786 r_key.sysno, r_key.seqno );
787 pp= append_main_item(firstpp, pp, &r_key);
789 if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
790 { /* pos can get to 0 at end of main seq, if still diffs left...*/
791 if (readpp->is->method->debug >3)
792 logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
793 isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
794 isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
795 isamd_release_block(readpp->is, readpp->cat, readpp->pos);
799 /* (try to) read next item */
800 r_ptr= (char *) &r_key;
801 r_more = isamd_read_item_merge( readpp, &r_ptr,0,filt);
806 isamd_reduceblock(pp); /* reduce size if possible */
807 if (0==firstpp->next)
808 firstpp->next = isamd_addr(pp->pos,pp->cat);
810 if (readpp->is->method->debug >4)
811 logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
812 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
815 if (readpp->is->method->debug >5)
816 logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
817 isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
819 isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
821 if (readpp->is->method->debug >2)
822 logf(LOG_LOG,"isamd_merge: merge ret f=%d=%d:%d pp=%d=%d:%d",
823 isamd_addr(firstpp->pos,pp->cat), firstpp->cat, firstpp->pos,
824 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
826 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1; /* nothing there */
827 memset(firstpp->buf,'\0',firstpp->is->method->filecat[firstpp->cat].bsize);
828 save_first_pp(firstpp);
829 retpos = isamd_addr(firstpp->pos, firstpp->cat);
830 isamd_pp_close(firstpp);
832 /* Create the dict entry */
833 /*!*/ /* it could be this could go in the dict as well, if there's */
834 /* been really many deletes. Somehow I suspect that is not the */
835 /* case. FIXME: Collect statistics and see if needed */
836 dictentry[0]=0; /* mark as a real isam */
837 memcpy(dictentry+1, &retpos, sizeof(ISAMD_P));
838 dictlen=sizeof(ISAMD_P)+1;
846 /***************************************************************
848 ***************************************************************/
852 static int append_diffs(
854 char *dictentry, int dictlen,
858 struct it_key i_key; /* one input item */
859 char *i_item = (char *) &i_key; /* same as chars */
862 int i_mode; /* 0 for delete, 1 for insert */
870 char *c_ptr = codebuff;
878 firstpp=isamd_pp_create(is, 0 );
879 firstpp->size=firstpp->offset=ISAMD_BLOCK_OFFSET_1;
880 /* create in smallest category, will expand later */
885 firstpp=isamd_pp_open(is, dictentry, dictlen);
889 memcpy(&ipos,dictentry+1,sizeof(ISAMD_P));
893 if (is->method->debug >2)
894 logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d n=%d=%d:%d nk=%d sz=%d",
895 ipos, isamd_type(ipos), isamd_block(ipos),
896 firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next),
897 firstpp->numKeys, firstpp->size);
898 maxsize = is->method->filecat[firstpp->cat].bsize;
900 difflenidx = diffidx = firstpp->size;
902 diffidx+=sizeof(int); /* difflen will be stored here */
904 /* read first input */
905 i_more = filter_read(filt, &i_key, &i_mode);
906 /* i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode); */
908 if (is->method->debug >6)
909 logf(LOG_LOG,"isamd_appd: start m=%d %d.%d=%x.%x: %d",
911 i_key.sysno, i_key.seqno,
912 i_key.sysno, i_key.seqno,
913 i_key.sysno*2+i_mode);
917 /* store the mode bit inside key */
918 assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
919 i_key.seqno = i_key.seqno * 2 + i_mode;
923 (*is->method->code_item)(ISAMD_ENCODE, firstpp->decodeClientData,
925 codelen = c_ptr - codebuff;
926 assert ( (codelen<128) && (codelen>0));
927 if (is->method->debug >7)
928 logf(LOG_LOG,"isamd_appd: coded %d: %s (nk=%d) (ix=%d)",
929 codelen, hexdump(codebuff, codelen,hexbuff),
930 firstpp->numKeys,diffidx);
932 if (diffidx + codelen > maxsize )
934 while ( (firstpp->cat < firstpp->is->max_cat) &&
935 (diffidx + codelen > maxsize) )
936 { /* try to increase the block size */
937 if (firstpp->pos > 0) /* free the old block if allocated */
938 isamd_release_block(is, firstpp->cat, firstpp->pos);
940 maxsize = is->method->filecat[firstpp->cat].bsize;
941 firstpp->pos=0; /* need to allocate it when saving */
942 if (is->method->debug >3)
943 logf(LOG_LOG,"isamd_appd: increased diff block sz to %d (%d)",
944 firstpp->cat, maxsize);
946 if ((firstpp->cat >= firstpp->is->max_cat) &&
947 (diffidx + codelen > maxsize) )
948 { /* max size - can't help, need to merge it */
949 if (is->method->debug >7)
950 logf(LOG_LOG,"isamd_appd: need to merge");
951 if (is->method->debug >9) /* !!!!! */
952 logf(LOG_LOG,"isamd_appd: going to merge with m=%d %d.%d",
953 i_mode, i_key.sysno, i_key.seqno);
954 merge_rc = merge (firstpp, &i_key, filt, dictentry, dictlen);
956 return merge_rc; /* merge handled them all ! */
957 assert(!"merge returned zero ??");
958 } /* need to merge */
961 if (!( diffidx+codelen <= maxsize ))
963 logf(LOG_LOG,"OOPS, diffidx problem: d=%d c=%d s=%d > m=%d",
964 diffidx, codelen, diffidx+codelen, maxsize);
965 logf(LOG_LOG,"ipos=%d f=%d=%d:%d",
967 isamd_addr(firstpp->pos, firstpp->cat),
968 firstpp->cat, firstpp->pos );
970 assert ( diffidx+codelen <= maxsize );
973 memcpy(&(firstpp->buf[diffidx]),codebuff,codelen);
975 firstpp->size = firstpp->offset = diffidx;
978 firstpp->numKeys++; /* insert diff */
980 firstpp->numKeys--; /* delete diff */
982 /* update length of this diff run */
983 memcpy(&(firstpp->buf[difflenidx]),&diffidx,sizeof(diffidx));
985 /* (try to) read the next input */
987 i_more = filter_read(filt, &i_key, &i_mode);
988 /* i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode); */
989 if ( (i_more) && (is->method->debug >6) )
990 logf(LOG_LOG,"isamd_appd: got m=%d %d.%d=%x.%x: %d",
992 i_key.sysno, i_key.seqno,
993 i_key.sysno, i_key.seqno,
994 i_key.sysno*2+i_mode);
997 /* clear the next difflen, if room for such */
998 difflenidx = diffidx;
999 while ( (difflenidx-diffidx<=sizeof(int)+1) && (difflenidx<maxsize))
1000 firstpp->buf[difflenidx++]='\0';
1002 if (firstpp->numKeys==0)
1004 /* FIXME: Release blocks that may be allocated !!! */
1005 return 0; /* don't bother storing this! */
1008 dsize=diffidx-ISAMD_BLOCK_OFFSET_1;
1009 /* logf(LOG_LOG,"!! nxt=%d diffidx=%d ds=%d",
1010 firstpp->next, diffidx, dsize); */
1012 if ( (0==firstpp->next) && (dsize <ISAMD_MAX_DICT_LEN))
1014 /* logf(LOG_LOG,"building a dict entry!!"); */
1015 assert(firstpp->numKeys < 128);
1016 assert(firstpp->numKeys >0);
1017 /* actually, 255 is good enough, but sign mismatches... */
1018 /* in real life, 4-5 is as much as we can hope for, as long */
1019 /* as ISAMD_MAX_DICT_LEN is reasonably small (8) */
1020 dictentry[0]=firstpp->numKeys;
1021 memcpy(dictentry+1, firstpp->buf+ISAMD_BLOCK_OFFSET_1, dsize);
1026 if (0==firstpp->pos) /* need to (re)alloc the block */
1027 firstpp->pos = isamd_alloc_block(is, firstpp->cat);
1028 retpos = save_first_pp( firstpp );
1029 isamd_pp_close(firstpp);
1030 dictentry[0]=0; /* mark as a real isam */
1031 memcpy(dictentry+1, &retpos, sizeof(ISAMD_P));
1032 dictlen=sizeof(ISAMD_P)+1;
1036 } /* append_diffs */
1041 /*************************************************************
1042 * isamd_append itself
1043 *************************************************************/
1045 int isamd_append (ISAMD is, char *dictentry, int dictlen, ISAMD_I data)
1046 /*ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data) */
1048 FILTER F = filter_open(is,data);
1051 if ( filter_isempty(F) ) /* can be, if del-ins of the same */
1053 if (is->method->debug >3)
1054 logf(LOG_LOG,"isamd_appd: nothing to do ");
1057 return dictlen; /* without doing anything at all */
1061 /* The old way to handle singletons */
1062 if ( ( 0==ipos) && filter_only_one(F) )
1066 filter_read(F,&k,&mode);
1068 rc = singleton_encode(&k);
1071 if (is->method->debug >9)
1072 logf(LOG_LOG,"isamd_appd: singleton didn't fit, backfilling");
1073 filter_backfill(F,&k, mode);
1075 if (is->method->debug >9)
1076 logf(LOG_LOG,"isamd_appd: singleton %d (%x)",
1080 assert ( (rc==0) || is_singleton(rc) );
1082 newlen = append_diffs(is,ipos,F);
1084 newlen = append_diffs(is,dictentry,dictlen,F);
1087 if (is->method->debug >2)
1088 logf(LOG_LOG,"isamd_appd: ret len=%d ", newlen);
1090 } /* isamd_append */
1099 * $Log: merge-d.c,v $
1100 * Revision 1.29 2002-11-26 22:18:34 adam
1101 * Remove // comments
1103 * Revision 1.28 2002/08/02 19:26:56 adam
1106 * Revision 1.27 2002/07/12 18:12:21 heikki
1107 * Isam-D now stores small entries directly in the dictionary.
1108 * Needs more tuning and cleaning...
1110 * Revision 1.26 2002/07/11 16:16:00 heikki
1111 * Fixed a bug in isamd, failed to store a single key when its bits
1112 * did not fit into a singleton.
1114 * Revision 1.25 1999/11/30 13:48:04 adam
1115 * Improved installation. Updated for inclusion of YAZ header files.
1117 * Revision 1.24 1999/10/05 09:57:40 heikki
1118 * Tuning the isam-d (and fixed a small "detail")
1120 * Revision 1.23 1999/09/27 14:36:36 heikki
1123 * Revision 1.22 1999/09/23 18:01:18 heikki
1124 * singleton optimising
1126 * Revision 1.21 1999/09/21 17:36:43 heikki
1127 * Added filter function. Not much of effect on the small test set...
1129 * Revision 1.20 1999/09/20 15:48:06 heikki
1132 * Revision 1.19 1999/09/13 13:28:28 heikki
1133 * isam-d optimizing: merging input data in the same go
1135 * Revision 1.18 1999/08/25 18:09:24 heikki
1136 * Starting to optimize
1138 * Revision 1.17 1999/08/24 13:17:42 heikki
1139 * Block sizes, comments
1141 * Revision 1.16 1999/08/24 10:12:02 heikki
1142 * Comments about optimising
1144 * Revision 1.15 1999/08/22 08:26:34 heikki
1147 * Revision 1.14 1999/08/20 12:25:58 heikki
1148 * Statistics in isamd
1150 * Revision 1.13 1999/08/18 13:59:19 heikki
1151 * Fixed another unlikely difflen bug
1153 * Revision 1.12 1999/08/18 13:28:17 heikki
1154 * Set log levels to decent values
1156 * Revision 1.11 1999/08/18 10:37:11 heikki
1157 * Fixed (another) difflen bug
1159 * Revision 1.10 1999/08/18 09:13:31 heikki
1162 * Revision 1.9 1999/08/17 19:46:53 heikki
1163 * Fixed a memory leak
1165 * Revision 1.8 1999/08/07 11:30:59 heikki
1166 * Bug fixing (still a mem leak somewhere)
1168 * Revision 1.7 1999/08/04 14:21:18 heikki
1169 * isam-d seems to be working.
1171 * Revision 1.6 1999/07/23 15:43:05 heikki
1172 * Hunted a few bugs in isam-d. Still crashes on the long test run
1174 * Revision 1.5 1999/07/23 13:58:52 heikki
1175 * merged closer to working, still fails on filling a separate, large block
1177 * Revision 1.4 1999/07/21 14:53:55 heikki
1178 * isamd read and write functions work, except when block full
1179 * Merge missing still. Need to split some functions
1181 * Revision 1.1 1999/07/14 13:14:47 heikki