1 /* $Id: merge-d.c,v 1.30 2003-03-05 16:41:10 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)) ;
160 static int filter_only_one(FILTER F)
162 return ( (0 != F->r1) && (0 == F->r2));
166 /* We may need backfilling, if we read a lonely key to make */
167 /* a singleton, but its bitw will not fit in. Then we need to */
168 /* process it normally, which means reading it again. So we */
169 /* need to unread it first. Luckily the filter is empty at that */
172 static void filter_backfill(FILTER F, struct it_key *k, int mode)
174 assert(F->r1 == FILTER_NOTYET ); /* not overwriting data! */
177 F->r1=1; /* ok read */
181 /***************************************************************
183 ***************************************************************/
184 /* When there is only a single item, we don't allocate a block
185 * for it, but code it in the directory entry directly, if it
189 #define DEC_SYSBITS 15
190 #define DEC_SEQBITS 15
191 #define DEC_MASK(n) ((1<<(n))-1)
193 #define SINGLETON_BIT (1<<(DEC_SYSBITS+DEC_SEQBITS+1))
195 int is_singleton(ISAMD_P ipos)
197 return 0; /* no singletons any more */
198 return ( ipos != 0 ) && ( ipos & SINGLETON_BIT );
202 int singleton_encode(struct it_key *k)
203 /* encodes the key into one int. If it does not fit, returns 0 */
205 return 0; /* no more singletons */
206 if ( (k->sysno & DEC_MASK(DEC_SYSBITS) ) != k->sysno )
207 return 0; /* no room dor sysno */
208 if ( (k->seqno & DEC_MASK(DEC_SYSBITS) ) != k->seqno )
209 return 0; /* no room dor sysno */
210 return (k->sysno | (k->seqno << DEC_SYSBITS) ) | SINGLETON_BIT;
213 void singleton_decode (int code, struct it_key *k)
215 assert (code & SINGLETON_BIT);
216 k->sysno = code & DEC_MASK(DEC_SYSBITS);
217 code = code >> DEC_SYSBITS;
218 k->seqno = code & DEC_MASK(DEC_SEQBITS);
222 /***************************************************************
223 * General support routines
224 ***************************************************************/
228 static char *hexdump(unsigned char *p, int len, char *buff) {
229 static char localbuff[128];
231 if (!buff) buff=localbuff;
234 sprintf(bytebuff,"%02x",*p);
236 strcat(buff,bytebuff);
237 if (len) strcat(buff,",");
244 static void isamd_reduceblock(ISAMD_PP pp)
245 /* takes a large block, and reduces its category if possible */
246 /* Presumably the first block in an isam-list */
249 return; /* existing block, do not touch */
250 /* TODO: Probably we may touch anyway? */
251 if (pp->is->method->debug > 5)
252 logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
253 pp->pos, pp->cat, pp->size);
254 while ( ( pp->cat > 0 ) && (!pp->next) &&
255 (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
257 pp->pos = isamd_alloc_block(pp->is, pp->cat);
258 if (pp->is->method->debug > 5)
259 logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
260 pp->pos, pp->cat, pp->size);
264 static int save_first_pp ( ISAMD_PP firstpp)
266 isamd_buildfirstblock(firstpp);
267 isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
268 return isamd_addr(firstpp->pos,firstpp->cat);
272 static void save_last_pp (ISAMD_PP pp)
274 pp->next = 0;/* just to be sure */
275 isamd_buildlaterblock(pp);
276 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
280 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
282 /* order of things: Better to save firstpp first, if there are just two */
283 /* blocks, but last if there are blocks in between, as these have already */
284 /* been saved... optimise later (that's why this is in its own func...*/
285 int retval = save_first_pp(firstpp);
290 isamd_pp_close(firstpp);
292 } /* save_both_pps */
297 /***************************************************************
299 ***************************************************************/
301 void isamd_free_diffs(ISAMD_PP pp)
304 if (pp->is->method->debug > 5)
305 logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
308 for (i=0;pp->diffinfo[i].difftype!=DT_NONE;i++)
309 if(pp->diffinfo[i].decodeData)
311 if (pp->is->method->debug > 8)
312 logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i,
313 pp->diffinfo[i].decodeData);
314 (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
317 if (pp->diffbuf != pp->buf)
321 } /* isamd_free_diffs */
324 static void getDiffInfo(ISAMD_PP pp )
325 { /* builds the diff info structures from a diffblock */
326 int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +2;
327 /* Each diff takes at least 5 bytes. Probably more, but this is safe */
328 int i=1; /* [0] is used for the main data, [n+1] for merge inputs */
329 int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
330 int maxsz = pp->is->method->filecat[pp->is->max_cat].bsize;
331 int diffidx = ISAMD_BLOCK_OFFSET_1;
333 pp->diffinfo = xmalloc( diffsz );
334 pp->offset = pp->size+1; /* used this block up */
335 memset(pp->diffinfo,'\0',diffsz);
336 if (pp->is->method->debug > 5)
337 logf(LOG_LOG,"isamd_getDiffInfo: %d=%d:%d->%d, ix=%d mx=%d",
338 isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->next,
341 /* duplicate the buffer for diffs */
342 /* (so that we can read the next real buffer(s) */
343 assert(0==pp->diffbuf);
344 pp->diffbuf=xmalloc(maxsz);
345 memcpy(pp->diffbuf, pp->buf, maxsz);
347 pp->diffinfo[0].maxidx=-1; /* mark as special */
348 pp->diffinfo[0].difftype=DT_MAIN;
352 if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
354 if (pp->is->method->debug > 5)
355 logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
357 return; /* whole block done */
359 memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
360 pp->diffinfo[i].difftype=DT_DIFF;
361 if (pp->is->method->debug > 5)
362 logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
363 pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
365 if ( (pp->is->method->debug > 0) &&
366 (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize) )
368 logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d",
369 __FILE__,__LINE__, diffidx);
370 logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
371 pp->is->method->filecat[pp->cat].bsize);
372 logf(LOG_LOG,"pp=%d=%d:%d pp->nx=%d=%d:%d",
373 isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
374 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
376 assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
378 if (0==pp->diffinfo[i].maxidx)
380 if (pp->is->method->debug > 5) /* !!! 4 */
381 logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
383 return; /* end marker */
385 diffidx += sizeof(int);
386 pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
387 pp->diffinfo[i].diffidx = diffidx;
388 if (pp->is->method->debug > 5)
389 logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
390 i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
391 hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
392 diffidx=pp->diffinfo[i].maxidx;
393 if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
394 return; /* whole block done */
397 assert (!"too many diff sequences in the block");
400 /***************************************************************
401 * Main block operations
402 ***************************************************************/
405 static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
406 { /* allocates a new block for the main data, and links it in */
408 if (0 == firstpp->next)
409 { /* special case, pp not yet allocated. */
410 /*Started as largest size, that's fine */
411 pp->pos = isamd_alloc_block(pp->is,pp->cat);
412 firstpp->next = isamd_addr(pp->pos,pp->cat);
413 if (pp->is->method->debug >3)
414 logf(LOG_LOG,"isamd_build: Alloc 1. dblock p=%d=%d:%d",
415 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
417 newblock=isamd_alloc_block(pp->is,pp->cat);
418 pp->next=isamd_addr(newblock,pp->cat);
419 isamd_buildlaterblock(pp);
420 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
421 if (pp->is->method->debug >3)
422 logf(LOG_LOG,"isamd_build: Alloc nxt %d=%d:%d -> %d=%d:%d",
423 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
424 isamd_addr(newblock,pp->cat), pp->cat, newblock);
427 pp->size=pp->offset=ISAMD_BLOCK_OFFSET_N;
429 } /* get_new_main_block */
432 static ISAMD_PP append_main_item(ISAMD_PP firstpp,
434 struct it_key *i_key)
435 { /* appends one item in the main data block, allocates new if needed */
436 char *i_item= (char *) i_key; /* same as char */
439 char *c_ptr = codebuff;
443 int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
447 (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
449 codelen = c_ptr - codebuff;
450 assert ( (codelen<128) && (codelen>0));
451 if (pp->is->method->debug >7)
452 logf(LOG_LOG,"isamd:build: coded %s nk=%d,ofs=%d-%d",
453 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1,
454 pp->offset, pp->offset+codelen);
456 if (pp->offset + codelen > maxsize )
457 { /* oops, block full - get a new one */
458 pp = get_new_main_block( firstpp, pp );
459 /* reset encoging and code again */
460 (*pp->is->method->code_reset)(pp->decodeClientData);
463 (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
465 codelen = c_ptr - codebuff;
466 assert ( (codelen<128) && (codelen>0));
467 if (pp->is->method->debug >7)
468 logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
469 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
472 assert (pp->offset + codelen <= maxsize );
474 /* write the data into pp, now we must have room */
475 memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
476 pp->offset += codelen;
479 /* clear the next 4 bytes in block, to avoid confusions with diff lens */
480 /* dirty, it should not be done here, but something slips somewhere, and */
481 /* I hope this fixes it... - Heikki */
482 codelen = pp->offset;
483 while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
484 pp->buf[codelen++] = '\0';
486 } /* append_main_item */
489 /***************************************************************
491 ***************************************************************/
493 /* Reads one item and corrects for the diffs, if any */
494 /* return 1 for ok, 0 for eof */
495 int isamd_read_item_merge (
498 struct it_key *p_key, /* the data item that didn't fit*/
499 /* ISAMD_I data) */ /* more input data comes here */
500 FILTER filt) /* more input data comes here */
501 { /* The last two args can be null for ordinary reads */
505 int winner=0; /* which diff holds the day */
506 int i; /* looping diffs */
516 for(i=1; pp->diffinfo[i].difftype!=DT_NONE; i++)
517 ; /* find last diff */
519 { /* we have an extra item to inject into the merge */
520 if (pp->is->method->debug >9) /* !!!!! */
521 logf(LOG_LOG,"isamd_read_item: going to merge with %d.%d",
522 p_key->sysno, p_key->seqno);
523 pp->diffinfo[i].key = *p_key; /* the key merge could not handle */
524 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
525 pp->diffinfo[i].key.seqno >>= 1;
526 pp->diffinfo[i].difftype=DT_INPU;
527 if (pp->is->method->debug > 7)
528 logf(LOG_LOG,"isamd_read_item: inpu key %d sys=%d seq=%d=2*%d+%d",
530 pp->diffinfo[i].key.seqno*2 + pp->diffinfo[1].mode,
531 pp->diffinfo[i].key.seqno,
532 pp->diffinfo[i].mode);
533 p_key->sysno=p_key->seqno=0; /* used it up */
537 { /* we have a whole input stream to inject */
538 pp->diffinfo[i].difftype=DT_INPU;
547 for (i=0; (!retry) && (pp->diffinfo[i].difftype); i++)
549 if (0==pp->diffinfo[i].key.sysno)
550 {/* read a new one, if possible */
551 if ((pp->diffinfo[i].difftype==DT_DIFF) &&
552 (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
553 { /* a normal kind of diff */
554 oldoffs=pp->diffinfo[i].diffidx;
555 codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
556 keyptr=(char *)&(pp->diffinfo[i].key);
557 (*pp->is->method->code_item)(ISAMD_DECODE,
558 pp->diffinfo[i].decodeData, &keyptr, &codeptr);
559 pp->diffinfo[i].diffidx += codeptr-codestart;
560 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
561 pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
562 if (pp->is->method->debug > 9)
563 logf(LOG_LOG,"isamd_read_item: dif[%d] at %d-%d: %s",
564 i,oldoffs, pp->diffinfo[i].diffidx,
565 hexdump(pp->buf+oldoffs, pp->diffinfo[i].diffidx-oldoffs,0));
566 if (pp->is->method->debug > 7)
567 logf(LOG_LOG,"isamd_read_item: rd dif[%d] %d.%d (%x.%x)",
569 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
570 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
572 else if ( pp->diffinfo[i].difftype==DT_MAIN)
573 { /* read a main item */
574 assert(i==0); /* main data goes before any diffs */
576 keyptr=(char*) &(pp->diffinfo[0].key);
577 rc= isamd_read_main_item(pp,&keyptr);
580 if (pp->is->method->debug > 7)
581 logf(LOG_LOG,"isamd_read_item: eof (rc=%d) main ",
583 pp->diffinfo[i].maxidx=-1;
584 pp->diffinfo[i].key.sysno=0;
585 pp->diffinfo[i].key.seqno=0;
586 pp->diffinfo[i].difftype= DT_DONE;
590 pp->diffinfo[i].mode = 1;
591 if (pp->is->method->debug > 7)
592 logf(LOG_LOG,"isamd_read_item: rd main %d-%d %d.%d (%x.%x) m=%d",
594 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
595 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
596 pp->diffinfo[i].mode);
599 else if (pp->diffinfo[i].difftype==DT_INPU)
601 keyptr = (char *) &pp->diffinfo[i].key;
602 /* rc = (*data->read_item)(data->clientData, &keyptr, &pp->diffinfo[i].mode); */
603 rc = filter_read(filt, &pp->diffinfo[i].key,
604 &pp->diffinfo[i].mode);
606 { /* did not get it */
607 pp->diffinfo[i].key.sysno=0;
608 pp->diffinfo[i].maxidx=0; /* signal the end */
609 pp->diffinfo[i].difftype=DT_DONE;
611 if (pp->is->method->debug >7)
612 logf(LOG_LOG,"merge: read inpu m=%d %d.%d (%x.%x)",
613 pp->diffinfo[i].mode,
614 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
615 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno );
616 } /* read an input item */
617 } /* read a new one */
619 if (pp->is->method->debug > 8)
620 logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
621 i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
622 pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
624 if ( 0!= pp->diffinfo[i].key.sysno)
625 { /* got a key, compare */
627 cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
630 if (0==pp->diffinfo[winner].key.sysno)
631 cmp=-1; /* end of main sequence, take all diffs */
634 if (pp->is->method->debug > 8)
635 logf(LOG_LOG,"isamd_read_item: ins [%d]%d.%d < [%d]%d.%d",
637 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
639 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
640 if (pp->diffinfo[i].mode) /* insert diff, should always be */
644 if (pp->is->method->debug > 1)
645 logf(LOG_LOG,"delete diff for nonexisting item");
646 assert(!"delete diff for nonexisting item");
647 /* is an assert too steep here? Not really.*/
652 if (!pp->diffinfo[i].mode) /* delete diff. should always be */
654 if (pp->is->method->debug > 8)
655 logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
657 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
658 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
659 pp->diffinfo[winner].key.sysno=0; /* delete it */
662 if (pp->is->method->debug > 2)
663 logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
665 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
666 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
667 /* skip the insert, since we already have it in the base */
668 /* Should we fail an assertion here??? */
669 pp->diffinfo[i].key.sysno=0; /* done with the delete */
670 retry=1; /* start all over again */
672 /* else it is a later key, its turn will come */
674 } /* for each diff */
677 if ( pp->diffinfo[winner].key.sysno)
679 if (pp->is->method->debug > 7)
680 logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
682 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
683 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
684 memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
685 *dst += sizeof(struct it_key);
686 pp->diffinfo[winner].key.sysno=0; /* used that one up */
691 if (pp->is->method->debug > 7)
692 logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
694 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
695 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
696 assert(winner==0); /* if nothing found, nothing comes from a diff */
700 ++(pp->is->no_read_keys);
702 ++(pp->is->no_read_eof);
706 } /* isamd_read_item */
709 int isamd_read_item (ISAMD_PP pp, char **dst)
711 return isamd_read_item_merge(pp,dst,0,0);
715 /***************************************************************
717 ***************************************************************/
719 static int merge ( ISAMD_PP firstpp, /* first pp (with diffs) */
720 struct it_key *p_key, /* the data item that didn't fit*/
721 FILTER filt, /* more input data arriving here */
722 char *dictentry, /* the thin in the dictionary */
723 int dictlen) /* and its size */
731 ISAMD_PP readpp=firstpp;
733 int diffcat = firstpp->cat; /* keep the category of the diffblock even */
734 /* if it is going to be empty now. */
735 /* Alternative: Make it the minimal, and */
736 /* resize later. Saves disk, but will lead */
737 /* into bad seeks. */
739 ++(readpp->is->no_merges);
741 /* set up diffs as they should be for reading */
742 diffidx = ISAMD_BLOCK_OFFSET_1;
744 if (readpp->is->method->debug >4)
745 logf(LOG_LOG,"isamd_merge: f=%d=%d:%d n=%d=%d:%d",
746 isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos,
747 firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next));
749 /* release our data block. Do before reading, when pos is stable ! */
750 killblk=firstpp->pos;
753 isamd_release_block(firstpp->is, firstpp->cat, killblk);
754 if (readpp->is->method->debug >3)
755 logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
756 isamd_addr(killblk,firstpp->cat), firstpp->cat, killblk );
760 r_ptr= (char *) &r_key;
761 r_more = isamd_read_item_merge( readpp, &r_ptr, p_key, filt);
763 { /* oops, all data has been deleted! what to do??? */
764 /* never mind, we have at least one more delta to add to the block */
765 /* pray that is not a delete as well... */
768 if (readpp->is->method->debug >5)
769 logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
774 /* set up the new blocks for simple writing */
775 /* firstpp=isamd_pp_open(readpp->is,isamd_addr(0, diffcat)); */
776 firstpp=isamd_pp_create(readpp->is, diffcat);
777 firstpp->pos=isamd_alloc_block(firstpp->is,diffcat);
778 if (readpp->is->method->debug >3)
779 logf(LOG_LOG,"isamd_merge: allocated new firstpp %d=%d:%d",
780 isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos );
782 pp=isamd_pp_create(readpp->is,readpp->is->max_cat );
783 pp->offset=pp->size=ISAMD_BLOCK_OFFSET_N;
787 if (readpp->is->method->debug >6)
788 logf(LOG_LOG,"isamd_merge: got key %d.%d",
789 r_key.sysno, r_key.seqno );
790 pp= append_main_item(firstpp, pp, &r_key);
792 if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
793 { /* pos can get to 0 at end of main seq, if still diffs left...*/
794 if (readpp->is->method->debug >3)
795 logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
796 isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
797 isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
798 isamd_release_block(readpp->is, readpp->cat, readpp->pos);
802 /* (try to) read next item */
803 r_ptr= (char *) &r_key;
804 r_more = isamd_read_item_merge( readpp, &r_ptr,0,filt);
809 isamd_reduceblock(pp); /* reduce size if possible */
810 if (0==firstpp->next)
811 firstpp->next = isamd_addr(pp->pos,pp->cat);
813 if (readpp->is->method->debug >4)
814 logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
815 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
818 if (readpp->is->method->debug >5)
819 logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
820 isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
822 isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
824 if (readpp->is->method->debug >2)
825 logf(LOG_LOG,"isamd_merge: merge ret f=%d=%d:%d pp=%d=%d:%d",
826 isamd_addr(firstpp->pos,pp->cat), firstpp->cat, firstpp->pos,
827 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
829 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1; /* nothing there */
830 memset(firstpp->buf,'\0',firstpp->is->method->filecat[firstpp->cat].bsize);
831 save_first_pp(firstpp);
832 retpos = isamd_addr(firstpp->pos, firstpp->cat);
833 isamd_pp_close(firstpp);
835 /* Create the dict entry */
836 /*!*/ /* it could be this could go in the dict as well, if there's */
837 /* been really many deletes. Somehow I suspect that is not the */
838 /* case. FIXME: Collect statistics and see if needed */
839 dictentry[0]=0; /* mark as a real isam */
840 memcpy(dictentry+1, &retpos, sizeof(ISAMD_P));
841 dictlen=sizeof(ISAMD_P)+1;
849 /***************************************************************
851 ***************************************************************/
855 static int append_diffs(
857 char *dictentry, int dictlen,
861 struct it_key i_key; /* one input item */
862 char *i_item = (char *) &i_key; /* same as chars */
865 int i_mode; /* 0 for delete, 1 for insert */
873 char *c_ptr = codebuff;
881 firstpp=isamd_pp_create(is, 0 );
882 firstpp->size=firstpp->offset=ISAMD_BLOCK_OFFSET_1;
883 /* create in smallest category, will expand later */
888 firstpp=isamd_pp_open(is, dictentry, dictlen);
892 memcpy(&ipos,dictentry+1,sizeof(ISAMD_P));
896 if (is->method->debug >2)
897 logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d n=%d=%d:%d nk=%d sz=%d",
898 ipos, isamd_type(ipos), isamd_block(ipos),
899 firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next),
900 firstpp->numKeys, firstpp->size);
901 maxsize = is->method->filecat[firstpp->cat].bsize;
903 difflenidx = diffidx = firstpp->size;
905 diffidx+=sizeof(int); /* difflen will be stored here */
907 /* read first input */
908 i_more = filter_read(filt, &i_key, &i_mode);
909 /* i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode); */
911 if (is->method->debug >6)
912 logf(LOG_LOG,"isamd_appd: start m=%d %d.%d=%x.%x: %d",
914 i_key.sysno, i_key.seqno,
915 i_key.sysno, i_key.seqno,
916 i_key.sysno*2+i_mode);
920 /* store the mode bit inside key */
921 assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
922 i_key.seqno = i_key.seqno * 2 + i_mode;
926 (*is->method->code_item)(ISAMD_ENCODE, firstpp->decodeClientData,
928 codelen = c_ptr - codebuff;
929 assert ( (codelen<128) && (codelen>0));
930 if (is->method->debug >7)
931 logf(LOG_LOG,"isamd_appd: coded %d: %s (nk=%d) (ix=%d)",
932 codelen, hexdump(codebuff, codelen,hexbuff),
933 firstpp->numKeys,diffidx);
935 if (diffidx + codelen > maxsize )
937 while ( (firstpp->cat < firstpp->is->max_cat) &&
938 (diffidx + codelen > maxsize) )
939 { /* try to increase the block size */
940 if (firstpp->pos > 0) /* free the old block if allocated */
941 isamd_release_block(is, firstpp->cat, firstpp->pos);
943 maxsize = is->method->filecat[firstpp->cat].bsize;
944 firstpp->pos=0; /* need to allocate it when saving */
945 if (is->method->debug >3)
946 logf(LOG_LOG,"isamd_appd: increased diff block sz to %d (%d)",
947 firstpp->cat, maxsize);
949 if ((firstpp->cat >= firstpp->is->max_cat) &&
950 (diffidx + codelen > maxsize) )
951 { /* max size - can't help, need to merge it */
952 if (is->method->debug >7)
953 logf(LOG_LOG,"isamd_appd: need to merge");
954 if (is->method->debug >9) /* !!!!! */
955 logf(LOG_LOG,"isamd_appd: going to merge with m=%d %d.%d",
956 i_mode, i_key.sysno, i_key.seqno);
957 merge_rc = merge (firstpp, &i_key, filt, dictentry, dictlen);
959 return merge_rc; /* merge handled them all ! */
960 assert(!"merge returned zero ??");
961 } /* need to merge */
964 if (!( diffidx+codelen <= maxsize ))
966 logf(LOG_LOG,"OOPS, diffidx problem: d=%d c=%d s=%d > m=%d",
967 diffidx, codelen, diffidx+codelen, maxsize);
968 logf(LOG_LOG,"ipos=%d f=%d=%d:%d",
970 isamd_addr(firstpp->pos, firstpp->cat),
971 firstpp->cat, firstpp->pos );
973 assert ( diffidx+codelen <= maxsize );
976 memcpy(&(firstpp->buf[diffidx]),codebuff,codelen);
978 firstpp->size = firstpp->offset = diffidx;
981 firstpp->numKeys++; /* insert diff */
983 firstpp->numKeys--; /* delete diff */
985 /* update length of this diff run */
986 memcpy(&(firstpp->buf[difflenidx]),&diffidx,sizeof(diffidx));
988 /* (try to) read the next input */
990 i_more = filter_read(filt, &i_key, &i_mode);
991 /* i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode); */
992 if ( (i_more) && (is->method->debug >6) )
993 logf(LOG_LOG,"isamd_appd: got m=%d %d.%d=%x.%x: %d",
995 i_key.sysno, i_key.seqno,
996 i_key.sysno, i_key.seqno,
997 i_key.sysno*2+i_mode);
1000 /* clear the next difflen, if room for such */
1001 difflenidx = diffidx;
1002 while ( (difflenidx-diffidx<=sizeof(int)+1) && (difflenidx<maxsize))
1003 firstpp->buf[difflenidx++]='\0';
1005 if (firstpp->numKeys==0)
1007 /* FIXME: Release blocks that may be allocated !!! */
1008 return 0; /* don't bother storing this! */
1011 dsize=diffidx-ISAMD_BLOCK_OFFSET_1;
1012 /* logf(LOG_LOG,"!! nxt=%d diffidx=%d ds=%d",
1013 firstpp->next, diffidx, dsize); */
1015 if ( (0==firstpp->next) && (dsize <ISAMD_MAX_DICT_LEN))
1017 /* logf(LOG_LOG,"building a dict entry!!"); */
1018 assert(firstpp->numKeys < 128);
1019 assert(firstpp->numKeys >0);
1020 /* actually, 255 is good enough, but sign mismatches... */
1021 /* in real life, 4-5 is as much as we can hope for, as long */
1022 /* as ISAMD_MAX_DICT_LEN is reasonably small (8) */
1023 dictentry[0]=firstpp->numKeys;
1024 memcpy(dictentry+1, firstpp->buf+ISAMD_BLOCK_OFFSET_1, dsize);
1029 if (0==firstpp->pos) /* need to (re)alloc the block */
1030 firstpp->pos = isamd_alloc_block(is, firstpp->cat);
1031 retpos = save_first_pp( firstpp );
1032 isamd_pp_close(firstpp);
1033 dictentry[0]=0; /* mark as a real isam */
1034 memcpy(dictentry+1, &retpos, sizeof(ISAMD_P));
1035 dictlen=sizeof(ISAMD_P)+1;
1039 } /* append_diffs */
1044 /*************************************************************
1045 * isamd_append itself
1046 *************************************************************/
1048 int isamd_append (ISAMD is, char *dictentry, int dictlen, ISAMD_I data)
1049 /*ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data) */
1051 FILTER F = filter_open(is,data);
1054 if ( filter_isempty(F) ) /* can be, if del-ins of the same */
1056 if (is->method->debug >3)
1057 logf(LOG_LOG,"isamd_appd: nothing to do ");
1060 return dictlen; /* without doing anything at all */
1064 /* The old way to handle singletons */
1065 if ( ( 0==ipos) && filter_only_one(F) )
1069 filter_read(F,&k,&mode);
1071 rc = singleton_encode(&k);
1074 if (is->method->debug >9)
1075 logf(LOG_LOG,"isamd_appd: singleton didn't fit, backfilling");
1076 filter_backfill(F,&k, mode);
1078 if (is->method->debug >9)
1079 logf(LOG_LOG,"isamd_appd: singleton %d (%x)",
1083 assert ( (rc==0) || is_singleton(rc) );
1085 newlen = append_diffs(is,ipos,F);
1087 newlen = append_diffs(is,dictentry,dictlen,F);
1090 if (is->method->debug >2)
1091 logf(LOG_LOG,"isamd_appd: ret len=%d ", newlen);
1093 } /* isamd_append */
1102 * $Log: merge-d.c,v $
1103 * Revision 1.30 2003-03-05 16:41:10 adam
1106 * Revision 1.29 2002/11/26 22:18:34 adam
1107 * Remove // comments
1109 * Revision 1.28 2002/08/02 19:26:56 adam
1112 * Revision 1.27 2002/07/12 18:12:21 heikki
1113 * Isam-D now stores small entries directly in the dictionary.
1114 * Needs more tuning and cleaning...
1116 * Revision 1.26 2002/07/11 16:16:00 heikki
1117 * Fixed a bug in isamd, failed to store a single key when its bits
1118 * did not fit into a singleton.
1120 * Revision 1.25 1999/11/30 13:48:04 adam
1121 * Improved installation. Updated for inclusion of YAZ header files.
1123 * Revision 1.24 1999/10/05 09:57:40 heikki
1124 * Tuning the isam-d (and fixed a small "detail")
1126 * Revision 1.23 1999/09/27 14:36:36 heikki
1129 * Revision 1.22 1999/09/23 18:01:18 heikki
1130 * singleton optimising
1132 * Revision 1.21 1999/09/21 17:36:43 heikki
1133 * Added filter function. Not much of effect on the small test set...
1135 * Revision 1.20 1999/09/20 15:48:06 heikki
1138 * Revision 1.19 1999/09/13 13:28:28 heikki
1139 * isam-d optimizing: merging input data in the same go
1141 * Revision 1.18 1999/08/25 18:09:24 heikki
1142 * Starting to optimize
1144 * Revision 1.17 1999/08/24 13:17:42 heikki
1145 * Block sizes, comments
1147 * Revision 1.16 1999/08/24 10:12:02 heikki
1148 * Comments about optimising
1150 * Revision 1.15 1999/08/22 08:26:34 heikki
1153 * Revision 1.14 1999/08/20 12:25:58 heikki
1154 * Statistics in isamd
1156 * Revision 1.13 1999/08/18 13:59:19 heikki
1157 * Fixed another unlikely difflen bug
1159 * Revision 1.12 1999/08/18 13:28:17 heikki
1160 * Set log levels to decent values
1162 * Revision 1.11 1999/08/18 10:37:11 heikki
1163 * Fixed (another) difflen bug
1165 * Revision 1.10 1999/08/18 09:13:31 heikki
1168 * Revision 1.9 1999/08/17 19:46:53 heikki
1169 * Fixed a memory leak
1171 * Revision 1.8 1999/08/07 11:30:59 heikki
1172 * Bug fixing (still a mem leak somewhere)
1174 * Revision 1.7 1999/08/04 14:21:18 heikki
1175 * isam-d seems to be working.
1177 * Revision 1.6 1999/07/23 15:43:05 heikki
1178 * Hunted a few bugs in isam-d. Still crashes on the long test run
1180 * Revision 1.5 1999/07/23 13:58:52 heikki
1181 * merged closer to working, still fails on filling a separate, large block
1183 * Revision 1.4 1999/07/21 14:53:55 heikki
1184 * isamd read and write functions work, except when block full
1185 * Merge missing still. Need to split some functions
1187 * Revision 1.1 1999/07/14 13:14:47 heikki