2 * Copyright (c) 1996-1998, Index Data.
3 * See the file LICENSE for details.
6 * $Id: merge-d.c,v 1.15 1999-08-22 08:26:34 heikki Exp $
9 * - Input filter: Eliminate del-ins pairs, tell if only one entry (or none)
10 * - single-entry optimizing (keep the one entry in the dict, no block)
11 * - study and optimize block sizes (later)
12 * - Clean up the different ways diffs are handled in writing and reading
13 * - Keep a merge-count in the firstpp, and if the block has already been
14 * merged, reduce it to a larger size even if it could fit in a small one!
15 * - Keep minimum freespace in the category table, and use that in reduce!
16 * - pass a space-needed for separateDiffBlock and reduce to be able to
17 * reserve more room for diffs, or to force a separate (larger?) block
18 * - Idea: Simplify the stryucture, so that the first block is always diffs.
19 * On small blocks, that is all we have. Once a block has been merged, we
20 * allocate the first main block and a (new) firstblock ffor diffs. From
21 * that point on the word has two blocks for it. Order the allocations
22 * right, and you should always get sequential reads!
23 * - On allocating more blocks (in append), check the order of blocks, and
24 * if needed, swap them.
27 * - Still has not been able to run a complete long test on bagel!
30 * There is a confusion about the block addresses. cat or type is the category,
31 * pos or block is the block number. pp structures keep these two separate,
32 * and combine when saving the pp. The next pointer in the pp structure is
33 * also a combined address, but needs to be combined every time it is needed,
34 * and separated when the partss are needed... This is done with the isamd_
35 * _block, _type, and _addr macros. The _addr takes block and type as args,
36 * in that order. This conflicts with the order these are often mentioned in
37 * the debug log calls, and other places, leading to small mistakes here
40 * Needs cleaning! The way diff blocks are handled in append and reading is
41 * quite different, and likely to give maintenance problems.
43 * log levels (set isamd=x in zebra.cfg (or what ever cfg file you use) )
44 * 0 = no logging. Default
45 * 1 = no logging here. isamd logs overall statistics
46 * 2 = Each call to isamd_append with start address and no more
47 * 3 = Start and type of append, start of merge, and result of append
48 * 4 = Block allocations
49 * 5 = Block-level operations (read/write)
50 * 6 = Details about diff blocks etc.
51 * 7 = Log each record as it passes the system (once)
52 * 8 = Log raw and (de)coded data
53 * 9 = Anything else that may be useful
54 * .. = Anything needed to hunt a specific bug
55 * (note that all tests in the code are like debug>3, which means 4 or above!)
63 #include "../index/index.h"
77 static char *hexdump(unsigned char *p, int len, char *buff) {
78 static char localbuff[128];
80 if (!buff) buff=localbuff;
83 sprintf(bytebuff,"%02x",*p);
85 strcat(buff,bytebuff);
86 if (len) strcat(buff,",");
92 static int separateDiffBlock(ISAMD_PP pp)
94 int limit = sizeof(int) + 8;
96 return 1; /* multi-block chains always have a separate diff block */
97 return ( pp->size + limit >= pp->is->method->filecat[pp->cat].bsize);
98 /* make sure there is at least room for the length and one diff. if not, */
99 /* it goes to a separate block. Assumes max diff is 8 bytes. Not */
100 /* unreaalistic in large data sets, where first sysno may be very large, */
101 /* and even the first seqno may be quite something. */
103 /* todo: Make the limit adjustable in the filecat table ! */
107 /**************************************************************
109 **************************************************************/
111 static void getDiffInfo(ISAMD_PP pp, int diffidx)
112 { /* builds the diff info structures from a diffblock */
113 int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +1;
114 /* Each diff takes at least 5 bytes. Probably more, but this is safe */
115 int i=1; /* [0] is used for the main data */
116 int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
118 pp->diffinfo = xmalloc( diffsz );
119 memset(pp->diffinfo,'\0',diffsz);
120 if (pp->is->method->debug > 5)
121 logf(LOG_LOG,"isamd_getDiffInfo: %d (%d:%d), ix=%d mx=%d",
122 isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, diffidx,maxinfos);
127 if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
129 if (pp->is->method->debug > 5)
130 logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
132 return; /* whole block done */
134 memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
136 if (pp->is->method->debug > 5)
137 logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
138 pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
140 if ( (pp->is->method->debug > 0) &&
141 (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize) )
142 { /* bug-hunting, this fails on some long runs that log too much */
143 logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d",
144 __FILE__,__LINE__, diffidx);
145 logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
146 pp->is->method->filecat[pp->cat].bsize);
147 logf(LOG_LOG,"pp=%d=%d:%d pp->nx=%d=%d:%d",
148 isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
149 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
151 assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
153 if (0==pp->diffinfo[i].maxidx)
155 if (pp->is->method->debug > 5) //!!! 4
156 logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
158 return; /* end marker */
160 diffidx += sizeof(int);
161 pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
162 pp->diffinfo[i].diffidx = diffidx;
163 if (pp->is->method->debug > 5)
164 logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
165 i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
166 hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
167 diffidx=pp->diffinfo[i].maxidx;
168 if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
169 return; /* whole block done */
172 assert (!"too many diff sequences in the block");
175 static void loadDiffs(ISAMD_PP pp)
176 { /* assumes pp is a firstblock! */
180 return; /* no diffs to talk about */
182 { /* separate diff block, load it */
183 pp->diffbuf= xmalloc( pp->is->method->filecat[pp->cat].bsize);
184 diffaddr=isamd_addr(pp->diffs/2, pp->cat);
185 isamd_read_block (pp->is, isamd_type(diffaddr),
186 isamd_block(diffaddr), pp->diffbuf );
187 diffidx= ISAMD_BLOCK_OFFSET_N;
188 if (pp->is->method->debug > 4)
189 logf(LOG_LOG,"isamd_LoadDiffs: loaded block %d=%d:%d, d=%d ix=%d",
190 diffaddr, isamd_type(diffaddr),isamd_block(diffaddr),
194 { /* integrated block, just set the pointers */
195 pp->diffbuf = pp->buf;
196 diffidx = pp->size; /* size is the beginning of diffs, diffidx the end*/
197 if (pp->is->method->debug > 4)
198 logf(LOG_LOG,"isamd_LoadDiffs: within %d=%d:%d, d=%d ix=%d ",
199 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos, pp->diffs, diffidx);
201 getDiffInfo(pp,diffidx);
205 void isamd_free_diffs(ISAMD_PP pp)
208 if (pp->is->method->debug > 5)
209 logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
212 for (i=1;pp->diffinfo[i].decodeData;i++)
214 if (pp->is->method->debug > 8)
215 logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i,
216 pp->diffinfo[i].decodeData);
217 (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
220 if (pp->diffbuf != pp->buf)
222 } /* isamd_free_diffs */
225 /* Reads one item and corrects for the diffs, if any */
226 /* return 1 for ok, 0 for eof */
227 int isamd_read_item (ISAMD_PP pp, char **dst)
232 int winner=0; /* which diff holds the day */
233 int i; /* looping diffs */
236 if (pp->diffs==0) /* no diffs, just read the thing */
237 return isamd_read_main_item(pp,dst);
244 if (0==pp->diffinfo[0].key.sysno)
245 { /* 0 is special case, main data. */
246 keyptr=(char*) &(pp->diffinfo[0].key);
247 pp->diffinfo[0].mode = ! isamd_read_main_item(pp,&keyptr);
248 if (pp->is->method->debug > 7)
249 logf(LOG_LOG,"isamd_read_item: read main %d.%d (%x.%x)",
250 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno,
251 pp->diffinfo[0].key.sysno, pp->diffinfo[0].key.seqno);
252 } /* get main data */
254 for (i=1; (!retry) && (pp->diffinfo[i].decodeData); i++)
256 if (pp->is->method->debug > 8)
257 logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
258 i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
259 pp->diffinfo[i].diffidx, pp->diffinfo[i].maxidx);
261 if ( (0==pp->diffinfo[i].key.sysno) &&
262 (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
263 {/* read a new one, if possible */
264 codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
265 keyptr=(char *)&(pp->diffinfo[i].key);
266 (*pp->is->method->code_item)(ISAMD_DECODE,
267 pp->diffinfo[i].decodeData, &keyptr, &codeptr);
268 pp->diffinfo[i].diffidx += codeptr-codestart;
269 pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
270 pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
271 if (pp->is->method->debug > 7)
272 logf(LOG_LOG,"isamd_read_item: read diff[%d] %d.%d (%x.%x)",i,
273 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
274 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
276 if ( 0!= pp->diffinfo[i].key.sysno)
277 { /* got a key, compare */
278 cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
279 if (0==pp->diffinfo[winner].key.sysno)
280 cmp=-1; /* end of main sequence, take all diffs */
283 if (pp->is->method->debug > 8)
284 logf(LOG_LOG,"isamd_read_item: ins %d<%d %d.%d (%x.%x) < %d.%d (%x.%x)",
286 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
287 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
288 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
289 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
290 if (pp->diffinfo[i].mode) /* insert diff, should always be */
293 assert(!"delete diff for nonexisting item");
294 /* is an assert too steep here? Not really.*/
298 if (!pp->diffinfo[i].mode) /* delete diff. should always be */
300 if (pp->is->method->debug > 8)
301 logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
303 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
304 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
305 pp->diffinfo[winner].key.sysno=0; /* delete it */
308 if (pp->is->method->debug > 2)
309 logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
311 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
312 pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
313 /* skip the insert, since we already have it in the base */
314 /* Should we fail an assertion here??? */
315 pp->diffinfo[i].key.sysno=0; /* done with the delete */
316 retry=1; /* start all over again */
318 /* else it is a later key, its turn will come */
320 } /* for each diff */
323 if ( pp->diffinfo[winner].key.sysno)
325 if (pp->is->method->debug > 7)
326 logf(LOG_LOG,"isamd_read_item: got %d %d.%d (%x.%x)",
328 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
329 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
330 memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
331 *dst += sizeof(struct it_key);
332 pp->diffinfo[winner].key.sysno=0; /* used that one up */
337 if (pp->is->method->debug > 7)
338 logf(LOG_LOG,"isamd_read_item: eof w=%d %d.%d (%x.%x)",
340 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
341 pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
342 assert(winner==0); /* if nothing found, nothing comes from a diff */
347 } /* isamd_read_item */
349 /*****************************************************************
351 *****************************************************************/
353 static void isamd_reduceblock(ISAMD_PP pp)
354 /* takes a large block, and reduces its category if possible */
355 /* Presumably the first block in an isam-list */
358 return; /* existing block, do not touch */
359 if (pp->is->method->debug > 5)
360 logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
361 pp->pos, pp->cat, pp->size);
362 while ( ( pp->cat > 0 ) && (!pp->next) &&
363 (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
365 pp->pos = isamd_alloc_block(pp->is, pp->cat);
366 if (pp->is->method->debug > 5)
367 logf(LOG_LOG,"isamd_reduce: got p=%d c=%d sz=%d",
368 pp->pos, pp->cat, pp->size);
374 static int save_first_pp ( ISAMD_PP firstpp)
376 isamd_reduceblock(firstpp);
377 isamd_buildfirstblock(firstpp);
378 isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
379 return isamd_addr(firstpp->pos,firstpp->cat);
382 static void save_last_pp (ISAMD_PP pp)
384 pp->next = 0;/* just to be sure */
385 isamd_buildlaterblock(pp);
386 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
390 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
392 /* order of things: Better to save firstpp first, if there are just two */
393 /* blocks, but last if there are blocks in between, as these have already */
394 /* been saved... optimise later (that's why this is in its own func...*/
395 int retval = save_first_pp(firstpp);
400 isamd_pp_close(firstpp);
402 } /* save_both_pps */
404 static ISAMD_PP read_diff_block(ISAMD_PP firstpp, int* p_diffidx)
405 { /* reads the diff block (if separate) and sets diffidx right */
410 { /* no diffs yet, create room for them */
411 if (separateDiffBlock(firstpp))
412 { /* create a new block */
413 pp=isamd_pp_open(pp->is,isamd_addr(0,firstpp->cat));
414 pp->pos = isamd_alloc_block(pp->is, pp->cat);
415 firstpp->diffs = pp->pos*2 +1;
416 diffidx = pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
417 if (pp->is->method->debug >5)
418 logf(LOG_LOG,"isamd_appd: alloc diff (d=%d) %d=%d:%d ix=%d",
420 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
424 { /* prepare to append diffs in head */
426 pp->diffs = diffidx *2 +0;
427 i=diffidx; /* make an end marker */
428 while ( ( i < pp->is->method->filecat[pp->cat].bsize) &&
429 ( i <= diffidx + sizeof(int)))
431 if (pp->is->method->debug >5)
432 logf(LOG_LOG,"isamd_appd: set up diffhead (d=%d) %d=%d:%d ix=%d",
434 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
439 { /* existing diffs */
441 { /* diffs in a separate block, load it */
442 pp=isamd_pp_open(pp->is, isamd_addr(firstpp->diffs/2,pp->cat));
443 diffidx = pp->offset= pp->size;
444 if (pp->is->method->debug >5)
445 logf(LOG_LOG,"isamd_appd: loaded diff (d=%d) %d=%d:%d ix=%d",
447 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
451 { /* diffs within the nead */
452 diffidx= pp->diffs/2;
453 if (pp->is->method->debug >5)
454 logf(LOG_LOG,"isamd_appd: diffs in head d=%d %d=%d:%d ix=%d sz=%d",
456 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
459 } /* diffs exist already */
460 *p_diffidx = diffidx;
462 } /* read_diff_block */
467 /*******************************************************************
468 * Building main blocks (no diffs)
469 *******************************************************************/
473 static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
474 { /* allocates a new block for the main data, and links it in */
477 { /* special case: it was the first block. Save much later */
479 { /* firstpp not allocated yet, do so now, */
480 /* to keep blocks in order. Don't save yet, though */
481 firstpp->pos = isamd_alloc_block(pp->is, firstpp->cat);
483 newblock = isamd_alloc_block(pp->is, firstpp->cat);
484 firstpp->next = isamd_addr(newblock,firstpp->cat);
485 /* keep the largest category */
486 pp=isamd_pp_open(pp->is,isamd_addr(0,firstpp->cat));/*don't load*/
488 pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
490 if (pp->is->method->debug >3)
491 logf(LOG_LOG,"isamd_g_mainblk: Alloc2 f=%d=%d:%d n=%d=%d:%d",
492 isamd_addr(firstpp->pos,firstpp->cat),
493 firstpp->cat, firstpp->pos,
494 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos );
497 { /* it was not the first block */
498 newblock = isamd_alloc_block(pp->is, firstpp->cat);
499 pp->next = isamd_addr(newblock,firstpp->cat);
500 if (pp->is->method->debug >3)
501 logf(LOG_LOG,"isamd_build: Alloc1 after p=%d=%d:%d->%d=%d:%d",
502 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
503 isamd_addr(newblock,pp->cat), pp->cat, newblock );
504 isamd_buildlaterblock(pp);
505 isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
506 pp->size = pp->offset = ISAMD_BLOCK_OFFSET_N;
508 pp->cat = firstpp->cat;
510 pp->cat = firstpp->cat; /* is already, never mind */
513 } /* get_new_main_block */
516 static ISAMD_PP append_main_item(ISAMD_PP firstpp,
518 struct it_key *i_key,
520 { /* appends one item in the main data block, allocates new if needed */
521 char *i_item= (char *) i_key; /* same as char */
524 char *c_ptr = codebuff;
528 int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
532 (*pp->is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
533 codelen = c_ptr - codebuff;
534 assert ( (codelen<128) && (codelen>0));
535 if (pp->is->method->debug >7)
536 logf(LOG_LOG,"isamd:build: coded into %s (nk=%d)",
537 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
539 if (pp->offset + codelen > maxsize )
540 { /* oops, block full - get a new one */
541 pp = get_new_main_block( firstpp, pp );
542 /* reset encoging and code again */
543 (*pp->is->method->code_reset)(encoder_data);
546 (*pp->is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
547 codelen = c_ptr - codebuff;
548 assert ( (codelen<128) && (codelen>0));
549 if (pp->is->method->debug >7)
550 logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
551 hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
554 /* write the data into pp, now we must have room */
555 memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
556 pp->offset += codelen;
559 /* clear the next 4 bytes in block, to avoid confusions with diff lens */
560 /* dirty, it should not be done here, but something slips somewhere, and */
561 /* I hope this fixes it... - Heikki */
562 codelen = pp->offset;
563 while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
564 pp->buf[codelen++] = '\0';
566 } /* append_main_item */
569 static int isamd_build_first_block(ISAMD is, ISAMD_I data)
571 struct it_key i_key; /* input key */
572 char *i_item= (char *) &i_key; /* same as char */
575 int i_mode; /* 0 for delete, 1 for insert */
583 ++(is->files[0].no_fbuilds);
585 firstpp=pp=isamd_pp_open(is, isamd_addr(0,is->max_cat));
586 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
588 encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
590 if (is->method->debug >2)
591 logf(LOG_LOG,"isamd_bld start: p=%d=%d:%d sz=%d maxsz=%d ",
592 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
593 pp->size, pp->is->method->filecat[pp->is->max_cat].bsize);
595 /* read first input */
597 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
599 assert( i_ptr-i_item == sizeof(i_key) );
601 if (pp->is->method->debug >7)
602 logf(LOG_LOG,"isamd: build_fi start: m=%d %s",
603 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
608 { /* ignore deletes here, should not happen */
609 pp= append_main_item(firstpp, pp, &i_key, encoder_data);
612 /* (try to) read the next item */
614 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
616 if ( (i_more) && (pp->is->method->debug >7) )
617 logf(LOG_LOG,"isamd: build_fi read: m=%d %s",
618 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
621 (*is->method->code_stop)(ISAMD_ENCODE, encoder_data);
623 return save_both_pps( firstpp, pp );
625 } /* build_first_block */
628 /***************************************************************
630 ***************************************************************/
633 static int merge ( ISAMD_PP *p_firstpp, /* first pp of the chain */
634 ISAMD_PP *p_pp, /* diff block */
635 struct it_key *p_key ) /* not used yet */
637 ISAMD_PP readpp = *p_firstpp;
643 ISAMD_PP firstpp; /* the new first, the one we write into */
647 ++(readpp->is->files[0].no_merges);
649 /* set up diffs as they should be for reading */
650 readpp->offset= ISAMD_BLOCK_OFFSET_1;
652 if ( (*p_firstpp)->diffs & 1 )
653 { /* separate diff block in *p_pp */
654 killblk = readpp->diffs/2;
655 diffidx /*size*/ = readpp->is->method->filecat[readpp->cat].bsize;
656 readpp->diffbuf= xmalloc( diffidx); /* copy diffs to where read wants*/
657 memcpy( readpp->diffbuf, &((*p_pp)->buf[0]), diffidx);
658 diffidx = ISAMD_BLOCK_OFFSET_N;
659 if (readpp->is->method->debug >2)
661 logf(LOG_LOG,"isamd_merge:separate diffs at ix=%d",
663 logf(LOG_LOG,"isamd_merge: dbuf=%p (from %p) pp=%p",
664 readpp->diffbuf, &((*p_pp)->buf[0]), (*p_pp) );
668 { /* integrated diffs */
669 assert ( *p_pp == *p_firstpp ); /* can only be in the first block */
670 diffidx=readpp->size;
671 readpp->diffs = diffidx*2+0;
672 readpp->diffbuf=readpp->buf;
673 if (readpp->is->method->debug >2)
674 logf(LOG_LOG,"isamd_merge:local diffs at %d: %s",
675 diffidx,hexdump(&(readpp->diffbuf[diffidx]),8,0));
678 getDiffInfo(readpp,diffidx);
679 if (readpp->is->method->debug >8)
680 logf(LOG_LOG,"isamd_merge: diffinfo=%p", readpp->diffinfo);
684 { /* we had a separate diff block, release it, we have copied the data */
685 isamd_release_block(readpp->is, readpp->cat, killblk);
686 isamd_pp_close (*p_pp);
687 if (readpp->is->method->debug >3)
688 logf(LOG_LOG,"isamd_merge: released diff block %d=%d:%d",
689 isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
693 /* release our data block. Do before reading, when pos is stable ! */
696 isamd_release_block(readpp->is, readpp->cat, killblk);
697 if (readpp->is->method->debug >3)
698 logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
699 isamd_addr(killblk,readpp->cat), readpp->cat, killblk );
701 r_ptr= (char *) &r_key;
702 r_more = isamd_read_item( readpp, &r_ptr);
704 { /* oops, all data has been deleted! what to do??? */
705 /* never mind, we have at least one more delta to add to the block */
706 /* pray that is not a delete as well... */
709 if (readpp->is->method->debug >3)
710 logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
712 assert (readpp->numKeys == 0);
716 /* set up the new blocks for simple writing */
717 firstpp=pp=isamd_pp_open(readpp->is,isamd_addr(0, readpp->is->max_cat));
718 firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;
719 encoder_data = (*pp->is->method->code_start)(ISAMD_ENCODE);
723 if (readpp->is->method->debug >6)
724 logf(LOG_LOG,"isamd_merge: got key %d.%d",
725 r_key.sysno, r_key.seqno );
726 pp= append_main_item(firstpp, pp, &r_key, encoder_data);
728 if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
729 { /* pos can get to 0 at end of main seq, if still diffs left...*/
730 if (readpp->is->method->debug >3)
731 logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
732 isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
733 isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
734 isamd_release_block(readpp->is, readpp->cat, readpp->pos);
738 /* (try to) read next item */
739 r_ptr= (char *) &r_key;
740 r_more = isamd_read_item( readpp, &r_ptr);
744 /* TODO: while pkey is an insert, and after last key inserted, append it */
745 /* will prevent multiple merges on large insert runs */
747 /* set things up so that append can continue */
748 isamd_reduceblock(firstpp);
752 { /* the last data block is of no interest any more */
754 if (readpp->is->method->debug >4)
755 logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
756 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
760 if (readpp->is->method->debug >5)
761 logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
762 isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
764 isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
766 (*firstpp->is->method->code_stop)(ISAMD_ENCODE, encoder_data);
768 *p_firstpp = firstpp;
770 if (readpp->is->method->debug >2)
771 logf(LOG_LOG,"isamd_merge: merge ret %d=%d:%d nx=%d=%d:%d d=%d=2*%d+%d",
772 isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
773 pp->next, isamd_type(pp->next), isamd_block(pp->next),
774 pp->diffs, pp->diffs/2, pp->diffs &1 );
781 /***************************************************************
783 ***************************************************************/
787 static int append_diffs(ISAMD is, ISAMD_P ipos, ISAMD_I data)
789 struct it_key i_key; /* one input item */
790 char *i_item = (char *) &i_key; /* same as chars */
793 int i_mode; /* 0 for delete, 1 for insert */
803 char *c_ptr = codebuff;
808 ++(is->files[0].no_appds);
810 firstpp=isamd_pp_open(is, ipos);
811 if (is->method->debug >2)
812 logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d d=%d=%d*2+%d nk=%d",
813 ipos, isamd_type(ipos), isamd_block(ipos),
814 firstpp->diffs, firstpp->diffs/2, firstpp->diffs & 1, firstpp->numKeys);
815 pp=read_diff_block(firstpp, &diffidx);
816 encoder_data=(*is->method->code_start)(ISAMD_ENCODE);
817 maxsize = is->method->filecat[pp->cat].bsize;
819 difflenidx = diffidx;
820 diffidx+=sizeof(int); /* difflen will be stored here */
822 /* read first input */
824 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
826 if (is->method->debug >6)
827 logf(LOG_LOG,"isamd_appd: start with m=%d %s",
828 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
832 /* store the mode bit inside key */
833 assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
834 i_key.seqno = i_key.seqno * 2 + i_mode;
838 (*is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
839 codelen = c_ptr - codebuff;
840 assert ( (codelen<128) && (codelen>0));
841 if (is->method->debug >7)
842 logf(LOG_LOG,"isamd_appd: coded into %d: %s (nk=%d) (ix=%d)",
843 codelen, hexdump(codebuff, codelen,hexbuff),
844 firstpp->numKeys,diffidx);
846 if (diffidx + codelen > maxsize )
848 if (is->method->debug >3)
849 logf(LOG_LOG,"isamd_appd: block full (ix=%d mx=%d lix=%d)",
850 diffidx, maxsize, difflenidx);
851 if (is->method->debug >8)
852 logf(LOG_LOG,"isamd_appd: block pp=%p buf=%p [%d]:%s",
854 difflenidx, hexdump(&pp->buf[difflenidx],8,0));
856 ++(is->files[0].no_remerges);
857 merge_rc = merge (&firstpp, &pp, &i_key);
859 return merge_rc; /* merge handled them all ! */
861 /* set things up so we can continue */
862 pp = read_diff_block(firstpp, &diffidx);
863 (*is->method->code_reset)(encoder_data);
864 maxsize = is->method->filecat[pp->cat].bsize;
866 diffidx+=sizeof(int);
868 /* code the current input key again */
871 (*is->method->code_item)(ISAMD_ENCODE, encoder_data, &c_ptr, &i_ptr);
872 codelen = c_ptr - codebuff;
873 assert ( (codelen<128) && (codelen>0));
874 if (is->method->debug >7)
875 logf(LOG_LOG,"isamd_appd: recoded into %d: %s (nk=%d) (ix=%d)",
876 codelen, hexdump(codebuff, codelen,hexbuff),
877 firstpp->numKeys,diffidx);
881 /* Note: this goes horribly wrong if there is no room for the diff */
882 /* after the merge! The solution is to increase the limit in */
883 /* separateDiffBlock, to force a separate diff block earlier, and not */
884 /* to have absurdly small blocks */
885 assert ( diffidx+codelen <= maxsize );
888 memcpy(&(pp->buf[diffidx]),codebuff,codelen);
891 firstpp->numKeys++; /* insert diff */
893 firstpp->numKeys--; /* delete diff */
895 /* update length of this diff run */
896 memcpy(&(pp->buf[difflenidx]),&diffidx,sizeof(diffidx));
898 firstpp->diffs =diffidx*2+0;
902 /* (try to) read the next input */
904 i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode);
905 if ( (i_more) && (is->method->debug >6) )
906 logf(LOG_LOG,"isamd_appd: got m=%d %s",
907 i_mode, hexdump(i_item,i_ptr-i_item,hexbuff) );
910 /* clear the next difflen, if room for such */
911 difflenidx = diffidx;
912 while ( (difflenidx-diffidx<=sizeof(int)) && (difflenidx<maxsize))
913 pp->buf[difflenidx++]='\0';
916 (*firstpp->is->method->code_stop)(ISAMD_ENCODE, encoder_data);
917 return save_both_pps( firstpp, pp );
923 /*************************************************************
924 * isamd_append itself, Sweet, isn't it
925 *************************************************************/
927 ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data)
932 retval = isamd_build_first_block(is,data);
934 retval = append_diffs(is,ipos,data);
938 void *p1=xmalloc(100);
939 void *p2=xmalloc(100);
940 void *p3=xmalloc(100);
941 logf(LOG_LOG,"Traversing xmalloc stuff. p1=%p p2=%p p3=%p",p1,p2,p3);
942 xmalloc_trav("end of append"); /*!*/
951 * $Log: merge-d.c,v $
952 * Revision 1.15 1999-08-22 08:26:34 heikki
955 * Revision 1.14 1999/08/20 12:25:58 heikki
956 * Statistics in isamd
958 * Revision 1.13 1999/08/18 13:59:19 heikki
959 * Fixed another unlikely difflen bug
961 * Revision 1.12 1999/08/18 13:28:17 heikki
962 * Set log levels to decent values
964 * Revision 1.11 1999/08/18 10:37:11 heikki
965 * Fixed (another) difflen bug
967 * Revision 1.10 1999/08/18 09:13:31 heikki
970 * Revision 1.9 1999/08/17 19:46:53 heikki
971 * Fixed a memory leak
973 * Revision 1.8 1999/08/07 11:30:59 heikki
974 * Bug fixing (still a mem leak somewhere)
976 * Revision 1.7 1999/08/04 14:21:18 heikki
977 * isam-d seems to be working.
979 * Revision 1.6 1999/07/23 15:43:05 heikki
980 * Hunted a few bugs in isam-d. Still crashes on the long test run
982 * Revision 1.5 1999/07/23 13:58:52 heikki
983 * merged closer to working, still fails on filling a separate, large block
985 * Revision 1.4 1999/07/21 14:53:55 heikki
986 * isamd read and write functions work, except when block full
987 * Merge missing still. Need to split some functions
989 * Revision 1.1 1999/07/14 13:14:47 heikki