f1494eec45eaa80524203db3cab5ecf163e8e518
[idzebra-moved-to-github.git] / isamc / merge-d.c
1 /*
2  * Copyright (c) 1996-1998, Index Data.
3  * See the file LICENSE for details.
4  * Heikki Levanto
5  *
6  * $Id: merge-d.c,v 1.27 2002-07-12 18:12:21 heikki Exp $
7  *
8  * bugs
9  *  sinleton-bit has to be in the high end, not low, so as not to confuse
10  *  ordinary small numbers, like in the next pointer..
11  *
12  * missing
13  *
14  * optimize
15  *  - study and optimize block sizes (later)
16  *  - find a way to decide the size of an empty diffblock (after merge)
17  *  - On allocating more blocks (in append and merge), check the order of 
18  *    blocks, and if needed, swap them. 
19  *  - Write a routine to save/load indexes into a block, save only as many 
20  *    bytes as needed (size, diff, diffindexes)
21  *
22  *
23  * caveat
24  *  There is a confusion about the block addresses. cat or type is the category,
25  *  pos or block is the block number. pp structures keep these two separate,
26  *  and combine when saving the pp. The next pointer in the pp structure is
27  *  also a combined address, but needs to be combined every time it is needed,
28  *  and separated when the partss are needed... This is done with the isamd_
29  *  _block, _type, and _addr macros. The _addr takes block and type as args,
30  *  in that order. This conflicts with the order these are often mentioned in 
31  *  the debug log calls, and other places, leading to small mistakes here
32  *  and there. 
33  *
34  *  Needs cleaning! The way diff blocks are handled in append and reading is
35  *  quite different, and likely to give maintenance problems.
36  *
37  *  log levels (set isamddebug=x in zebra.cfg (or what ever cfg file you use) )
38  *    0 = no logging. Default
39  *    1 = no logging here. isamd logs overall statistics
40  *    2 = Each call to isamd_append with start address and no more
41  *    3 = Start and type of append, start of merge, and result of append
42  *    4 = Block allocations
43  *    5 = Block-level operations (read/write)
44  *    6 = Details about diff blocks etc.
45  *    7 = Log each record as it passes the system (once)
46  *    8 = Log raw and (de)coded data
47  *    9 = Anything else that may be useful
48  *   .. = Anything needed to hunt a specific bug
49  *  (note that all tests in the code are like debug>3, which means 4 or above!)
50  *
51  * Design for the new and improved isamd
52  * Key points:
53  *  - The first block is only diffs, no straight data
54  *  - Additional blocks are straight data
55  *  - When a diff block gets filled up, a data block is created by
56  *    merging the diffs with the data
57  *
58  * Structure
59  *  - Isamd_pp: buffer for diffs and for data
60  *              keep both pos, type, and combined address
61  *              routine to set the address
62  *  - diffbuf: lengths as short ints, or bytes for small blocks
63  *  - keys are of key_struct, not just a number of bytes.
64  *
65  * Routines
66  *  - isamd_append
67  *    - create_new_block if needed
68  *    - append_diffs
69  *      - load_diffs 
70  *      - get diffend, start encoding
71  *      - while input data
72  *        - encode it
73  *        - if no room, then realloc block in larger size
74  *        - if still no room, merge and exit
75  *        - append in the block
76  *
77  * - merge
78  *   - just as before, except that merges also input data directly
79  *   - writes into new data blocks
80  *       
81  *      
82  * - isamd.c: load firstpp, load datablock
83  *            save firstpp, save datablock
84  * - Readlength, writelength - handling right size of len fields
85  * - isamd_read_main_item: take also a merge input structure, and merge it too
86  * - prefilter: cache two inputs, and check if they cancel.
87  * - single-item optimization
88  * 
89  * questions: Should we realloc firstblocks in a different size as the main
90  * blocks. Makes a sideways seek, which is bound to be slowe. But saves some
91  * update time. Compromise: alloc the first one in the size of the datablock,
92  * but increase if necessary. Large blocks get a large diff, ok. Small ones
93  * may get an extra seek in read, but save merges.
94  */
95
96
97 #define NEW_ISAM_D 1  /* not yet ready to delete the old one! */
98
99 #include <stdlib.h>
100 #include <assert.h>
101 #include <string.h>
102 #include <stdio.h>
103 #include <yaz/log.h>
104 #include "../index/index.h"
105 #include "isamd-p.h"
106
107
108 struct ISAMD_DIFF_s {
109   int diffidx;
110   int maxidx;
111   struct it_key key;
112   void *decodeData;
113   int mode;
114   int difftype;  
115 };
116
117 #define DT_NONE 0 // no diff, marks end of sequence
118 #define DT_DIFF 1 // ordinarry diff
119 #define DT_MAIN 2 // main data
120 #define DT_INPU 3 // input data to be merged
121 #define DT_DONE 4 // done with all input here
122
123
124
125 /***************************************************************
126  * Input preprocess filter
127  ***************************************************************/
128
129
130 #define FILTER_NOTYET -1  /* no data read in yet, to be done */
131
132 struct ISAMD_FILTER_s {
133   ISAMD_I data;          /* where the data comes from */
134   ISAMD is;              /* for debug flags */
135   struct it_key k1;      /* the next item to be returned */
136   int           m1;      /* mode for k1 */
137   int           r1;      /* result for read of k1, or NOTYET */
138   struct it_key k2;      /* the one after that */
139   int           m2;
140   int           r2;
141 };
142
143 typedef struct ISAMD_FILTER_s *FILTER;
144
145
146 void filter_fill(FILTER F)
147 {
148   while ( (F->r1 == FILTER_NOTYET) || (F->r2 == FILTER_NOTYET) )
149   {
150      if (F->r1==FILTER_NOTYET) 
151      { /* move data forward in the filter */
152         F->k1 = F->k2;
153         F->m1 = F->m2;
154         F->r1 = F->r2;
155         if ( 0 != F->r1 ) /* not eof */
156           F->r2 = FILTER_NOTYET; /* say we want more */
157         if (F->is->method->debug > 9)  
158           logf(LOG_LOG,"filt_fill: shift %d.%d m=%d r=%d",
159              F->k1.sysno, 
160              F->k1.seqno, 
161              F->m1, F->r1);
162      }
163      if (F->r2==FILTER_NOTYET)
164      { /* read new bottom value */
165         char *k_ptr = (char*) &F->k2;
166         F->r2 = (F->data->read_item)(F->data->clientData, &k_ptr, &F->m2); 
167         if (F->is->method->debug > 9)
168           logf(LOG_LOG,"filt_fill: read %d.%d m=%d r=%d",
169              F->k2.sysno, F->k2.seqno, F->m2, F->r2);
170      }  
171      if ( (F->k1.sysno == F->k2.sysno) && 
172           (F->k1.seqno == F->k2.seqno) &&
173           (F->m1 != F->m2) &&
174           (F->r1 >0 ) && (F->r2 >0) )
175      { /* del-ins pair of same key (not eof) , ignore both */
176        if (F->is->method->debug > 9)
177          logf(LOG_LOG,"filt_fill: skipped %d.%d m=%d/%d r=%d/%d",
178             F->k1.sysno, F->k1.seqno, 
179             F->m1,F->m2, F->r1,F->r2);
180        F->r1 = FILTER_NOTYET;
181        F->r2 = FILTER_NOTYET;
182      }
183   } /* while */
184 } /* filter_fill */
185
186
187 FILTER filter_open( ISAMD is, ISAMD_I data )
188 {
189   FILTER F = (FILTER) xmalloc(sizeof(struct ISAMD_FILTER_s));
190   F->is = is;
191   F->data = data;
192   F->k1.sysno=0;
193   F->k1.seqno=0;
194   F->k2=F->k1; 
195   F->m1 = F->m2 = 0;
196   F->r1 = F->r2 = FILTER_NOTYET;
197   filter_fill(F);
198   return F;
199 }
200
201 static void filter_close (FILTER F)
202 {
203   xfree(F);
204 }
205
206 static int filter_read( FILTER F, 
207                         struct it_key *k,
208                         int *mode)
209 {
210   int res;
211   filter_fill(F);
212   if (F->is->method->debug > 9)
213     logf(LOG_LOG,"filt_read: reading %d.%d m=%d r=%d",
214        F->k1.sysno, F->k1.seqno, F->m1, F->r1);
215   res  = F->r1;
216   if(res) 
217   {
218     *k = F->k1;
219     *mode= F->m1;
220   }
221   F->r1 = FILTER_NOTYET;
222   return res;
223 }
224
225 static int filter_isempty(FILTER F)
226 {
227   return ( (0 == F->r1) && (0 == F->r2)) ;
228 }
229
230 static int filter_only_one(FILTER F)
231 {
232   return ( (0 != F->r1) && (0 == F->r2));
233 }
234
235 /* We may need backfilling, if we read a lonely key to make */
236 /* a singleton, but its bitw will not fit in. Then we need to */
237 /* process it normally, which means reading it again. So we  */
238 /* need to unread it first. Luckily the filter is empty at that */
239 /* point */
240 static void filter_backfill(FILTER F, struct it_key *k, int mode)
241 {
242   assert(F->r1 == FILTER_NOTYET ); /* not overwriting data! */
243   F->k1=*k;
244   F->m1=mode;
245   F->r1=1; /* ok read */
246 }
247
248
249 /***************************************************************
250  * Singleton encoding
251  ***************************************************************/
252 /* When there is only a single item, we don't allocate a block
253  * for it, but code it in the directory entry directly, if it
254  * fits.
255  */
256
257 #define DEC_SYSBITS 15
258 #define DEC_SEQBITS 15
259 #define DEC_MASK(n) ((1<<(n))-1)
260
261 #define SINGLETON_BIT (1<<(DEC_SYSBITS+DEC_SEQBITS+1))
262
263 int is_singleton(ISAMD_P ipos)
264 {
265   return 0; /* no singletons any more */
266   return ( ipos != 0 ) && ( ipos & SINGLETON_BIT );
267 }
268
269
270 int singleton_encode(struct it_key *k)
271 /* encodes the key into one int. If it does not fit, returns 0 */
272 {
273   return 0; /* no more singletons */
274   if ( (k->sysno & DEC_MASK(DEC_SYSBITS) ) != k->sysno )
275     return 0;  /* no room dor sysno */
276   if ( (k->seqno & DEC_MASK(DEC_SYSBITS) ) != k->seqno )
277     return 0;  /* no room dor sysno */
278   return (k->sysno | (k->seqno << DEC_SYSBITS) ) | SINGLETON_BIT;
279 }
280  
281 void singleton_decode (int code, struct it_key *k)
282 {
283   assert (code & SINGLETON_BIT);
284   k->sysno = code & DEC_MASK(DEC_SYSBITS);
285   code = code >> DEC_SYSBITS; 
286   k->seqno = code & DEC_MASK(DEC_SEQBITS);
287
288  
289  
290 /***************************************************************
291  * General support routines
292  ***************************************************************/
293
294
295
296 static char *hexdump(unsigned char *p, int len, char *buff) {
297   static char localbuff[128];
298   char bytebuff[8];
299   if (!buff) buff=localbuff;
300   *buff='\0';
301   while (len--) {
302     sprintf(bytebuff,"%02x",*p);
303     p++;
304     strcat(buff,bytebuff);
305     if (len) strcat(buff,",");
306   }
307   return buff;
308 }
309
310
311
312 static void isamd_reduceblock(ISAMD_PP pp)
313 /* takes a large block, and reduces its category if possible */
314 /* Presumably the first block in an isam-list */
315 {
316    if (pp->pos)
317       return; /* existing block, do not touch */
318       /* TODO: Probably we may touch anyway? */
319    if (pp->is->method->debug > 5)  
320      logf(LOG_LOG,"isamd_reduce: start p=%d c=%d sz=%d",
321        pp->pos, pp->cat, pp->size); 
322    while ( ( pp->cat > 0 ) && (!pp->next) && 
323            (pp->offset < pp->is->method->filecat[pp->cat-1].bsize ) )
324       pp->cat--;
325    pp->pos = isamd_alloc_block(pp->is, pp->cat);
326    if (pp->is->method->debug > 5) 
327      logf(LOG_LOG,"isamd_reduce:  got  p=%d c=%d sz=%d",
328        pp->pos, pp->cat, pp->size);    
329 } /* reduceblock */
330
331
332 static int save_first_pp ( ISAMD_PP firstpp)
333 {
334    isamd_buildfirstblock(firstpp);
335    isamd_write_block(firstpp->is,firstpp->cat,firstpp->pos,firstpp->buf);
336    return isamd_addr(firstpp->pos,firstpp->cat);
337 }
338
339  
340 static void save_last_pp (ISAMD_PP pp)
341 {
342    pp->next = 0;/* just to be sure */
343    isamd_buildlaterblock(pp);
344    isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
345 }
346
347 #ifdef UNUSED
348 static int save_both_pps (ISAMD_PP firstpp, ISAMD_PP pp)
349 {
350    /* order of things: Better to save firstpp first, if there are just two */
351    /* blocks, but last if there are blocks in between, as these have already */
352    /* been saved... optimise later (that's why this is in its own func...*/
353    int retval = save_first_pp(firstpp);
354    if (firstpp!=pp){ 
355       save_last_pp(pp);
356       isamd_pp_close(pp);
357    }
358    isamd_pp_close(firstpp);
359    return retval;
360 } /* save_both_pps */
361 #endif
362
363
364
365 /***************************************************************
366  * Diffblock handling
367  ***************************************************************/
368
369 void isamd_free_diffs(ISAMD_PP pp)
370 {
371   int i;
372   if (pp->is->method->debug > 5)
373      logf(LOG_LOG,"isamd_free_diffs: pp=%p di=%p", pp, pp->diffinfo);
374   if (!pp->diffinfo) 
375     return;
376   for (i=0;pp->diffinfo[i].difftype!=DT_NONE;i++) 
377       if(pp->diffinfo[i].decodeData)
378       {
379           if (pp->is->method->debug > 8)
380              logf(LOG_LOG,"isamd_free_diffs [%d]=%p",i, 
381                            pp->diffinfo[i].decodeData);
382           (*pp->is->method->code_stop)(ISAMD_DECODE,pp->diffinfo[i].decodeData);
383       } 
384   xfree(pp->diffinfo);
385   if (pp->diffbuf != pp->buf)
386     xfree (pp->diffbuf);  
387   pp->diffbuf=0;
388   pp->diffinfo=0;
389 } /* isamd_free_diffs */
390
391
392 static void getDiffInfo(ISAMD_PP pp )
393 { /* builds the diff info structures from a diffblock */
394    int maxinfos = pp->is->method->filecat[pp->cat].bsize / 5 +2;
395     /* Each diff takes at least 5 bytes. Probably more, but this is safe */
396    int i=1;  /* [0] is used for the main data, [n+1] for merge inputs */
397    int diffsz= maxinfos * sizeof(struct ISAMD_DIFF_s);
398    int maxsz = pp->is->method->filecat[pp->is->max_cat].bsize;
399    int diffidx = ISAMD_BLOCK_OFFSET_1;
400
401    pp->diffinfo = xmalloc( diffsz ); 
402    pp->offset = pp->size+1; /* used this block up */
403    memset(pp->diffinfo,'\0',diffsz);
404    if (pp->is->method->debug > 5)   
405       logf(LOG_LOG,"isamd_getDiffInfo: %d=%d:%d->%d, ix=%d mx=%d",
406          isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->next,
407          diffidx,maxinfos);
408    
409    /* duplicate the buffer for diffs */
410    /* (so that we can read the next real buffer(s) */
411    assert(0==pp->diffbuf);
412    pp->diffbuf=xmalloc(maxsz);
413    memcpy(pp->diffbuf, pp->buf, maxsz);
414    
415    pp->diffinfo[0].maxidx=-1; /* mark as special */
416    pp->diffinfo[0].difftype=DT_MAIN;
417
418    while (i<maxinfos) 
419    {  
420       if ( diffidx+sizeof(int) > pp->is->method->filecat[pp->cat].bsize )
421       {
422          if (pp->is->method->debug > 5)
423            logf(LOG_LOG,"isamd_getDiffInfo:Near end (no room for len) at ix=%d n=%d",
424                diffidx, i);
425          return; /* whole block done */
426       }
427       memcpy( &pp->diffinfo[i].maxidx, &pp->diffbuf[diffidx], sizeof(int) );
428       pp->diffinfo[i].difftype=DT_DIFF;
429       if (pp->is->method->debug > 5)
430         logf(LOG_LOG,"isamd_getDiffInfo: max=%d ix=%d dbuf=%p",
431           pp->diffinfo[i].maxidx, diffidx, pp->diffbuf);
432
433       if ( (pp->is->method->debug > 0) &&
434          (pp->diffinfo[i].maxidx > pp->is->method->filecat[pp->cat].bsize) )
435       { 
436          logf(LOG_LOG,"Bad MaxIx!!! %s:%d: diffidx=%d", 
437                        __FILE__,__LINE__, diffidx);
438          logf(LOG_LOG,"i=%d maxix=%d bsz=%d", i, pp->diffinfo[i].maxidx,
439                        pp->is->method->filecat[pp->cat].bsize);
440          logf(LOG_LOG,"pp=%d=%d:%d  pp->nx=%d=%d:%d",
441                        isamd_addr(pp->pos,pp->cat), pp->pos, pp->cat,
442                        pp->next, isamd_type(pp->next), isamd_block(pp->next) );                      
443       }
444       assert(pp->diffinfo[i].maxidx <= pp->is->method->filecat[pp->cat].bsize+1);
445
446       if (0==pp->diffinfo[i].maxidx)
447       {
448          if (pp->is->method->debug > 5)  //!!! 4
449            logf(LOG_LOG,"isamd_getDiffInfo:End mark at ix=%d n=%d",
450                diffidx, i);
451          return; /* end marker */
452       }
453       diffidx += sizeof(int);
454       pp->diffinfo[i].decodeData = (*pp->is->method->code_start)(ISAMD_DECODE);
455       pp->diffinfo[i].diffidx = diffidx;
456       if (pp->is->method->debug > 5)
457         logf(LOG_LOG,"isamd_getDiff[%d]:%d-%d %s",
458           i,diffidx-sizeof(int),pp->diffinfo[i].maxidx,
459           hexdump((char *)&pp->diffbuf[diffidx-4],8,0) );
460       diffidx=pp->diffinfo[i].maxidx;
461       if ( diffidx > pp->is->method->filecat[pp->cat].bsize )
462         return; /* whole block done */
463       ++i;
464    }
465    assert (!"too many diff sequences in the block");
466 }
467
468 /***************************************************************
469  * Main block operations
470  ***************************************************************/
471
472
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 */
475   int newblock;
476   if (0 == firstpp->next) 
477   {  /* special case, pp not yet allocated. */
478      /*Started as largest size, that's fine */
479      pp->pos = isamd_alloc_block(pp->is,pp->cat);
480      firstpp->next = isamd_addr(pp->pos,pp->cat);
481      if (pp->is->method->debug >3) 
482         logf(LOG_LOG,"isamd_build: Alloc 1. dblock  p=%d=%d:%d",
483            isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
484   }
485   newblock=isamd_alloc_block(pp->is,pp->cat);
486   pp->next=isamd_addr(newblock,pp->cat);
487   isamd_buildlaterblock(pp);
488   isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
489   if (pp->is->method->debug >3) 
490      logf(LOG_LOG,"isamd_build: Alloc nxt %d=%d:%d -> %d=%d:%d",
491         isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
492         isamd_addr(newblock,pp->cat), pp->cat, newblock);
493   pp->next=0;
494   pp->pos=newblock;
495   pp->size=pp->offset=ISAMD_BLOCK_OFFSET_N;  
496   return pp;
497 } /* get_new_main_block */
498
499
500 static ISAMD_PP  append_main_item(ISAMD_PP firstpp, 
501                                   ISAMD_PP pp, 
502                                   struct it_key *i_key)
503 {  /* appends one item in the main data block, allocates new if needed */
504    char *i_item= (char *) i_key;  /* same as char */
505    char *i_ptr=i_item;
506    char codebuff[128];
507    char *c_ptr = codebuff;
508    int codelen;
509    char hexbuff[64];
510
511    int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize; 
512
513    c_ptr=codebuff;
514    i_ptr=i_item;
515    (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
516                                 &c_ptr, &i_ptr);
517    codelen = c_ptr - codebuff;
518    assert ( (codelen<128) && (codelen>0));
519    if (pp->is->method->debug >7)
520       logf(LOG_LOG,"isamd:build: coded %s nk=%d,ofs=%d-%d",
521           hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1,
522           pp->offset, pp->offset+codelen);
523
524    if (pp->offset + codelen > maxsize )
525    { /* oops, block full - get a new one */
526       pp =  get_new_main_block( firstpp, pp );
527       /* reset encoging and code again */
528       (*pp->is->method->code_reset)(pp->decodeClientData);
529       c_ptr=codebuff;
530       i_ptr=i_item;
531       (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData, 
532                                    &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: recoded into %s  (nk=%d)",
537              hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
538    } /* block full */    
539
540    assert (pp->offset + codelen <= maxsize );
541    
542    /* write the data into pp, now we must have room */ 
543    memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
544    pp->offset += codelen;
545    pp->size += codelen;
546    firstpp->numKeys++;
547    /* clear the next 4 bytes in block, to avoid confusions with diff lens */
548    /* dirty, it should not be done here, but something slips somewhere, and */
549    /* I hope this fixes it...  - Heikki */
550    codelen = pp->offset;
551    while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
552      pp->buf[codelen++] = '\0';
553    return pp;    
554 } /* append_main_item */
555
556
557 /***************************************************************
558  * Read with merge
559  ***************************************************************/
560
561 /* Reads one item and corrects for the diffs, if any */
562 /* return 1 for ok, 0 for eof */
563 int isamd_read_item_merge (
564                      ISAMD_PP pp, 
565                      char **dst,
566                      struct it_key *p_key,  /* the data item that didn't fit*/
567               /*       ISAMD_I data)  */    /* more input data comes here */
568                      FILTER filt)           /* more input data comes here */
569 {                    /* The last two args can be null for ordinary reads */
570   char *keyptr;
571   char *codeptr;
572   char *codestart;
573   int winner=0; /* which diff holds the day */
574   int i; /* looping diffs */
575   int cmp;
576   int retry=1;
577   int oldoffs;
578   int rc;
579
580   if (!pp->diffinfo)  
581   { /* first time */
582      getDiffInfo(pp);
583
584      for(i=1; pp->diffinfo[i].difftype!=DT_NONE; i++)
585        ;  /* find last diff */
586      if (p_key)  
587      {  /* we have an extra item to inject into the merge */
588        if (pp->is->method->debug >9)  //!!!!!
589           logf(LOG_LOG,"isamd_read_item: going to merge with %d.%d",
590                p_key->sysno, p_key->seqno);
591        pp->diffinfo[i].key = *p_key;  /* the key merge could not handle */
592        pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
593        pp->diffinfo[i].key.seqno >>= 1;
594        pp->diffinfo[i].difftype=DT_INPU;
595        if (pp->is->method->debug > 7)
596          logf(LOG_LOG,"isamd_read_item: inpu key %d sys=%d  seq=%d=2*%d+%d",
597             i, p_key->sysno,
598             pp->diffinfo[i].key.seqno*2 + pp->diffinfo[1].mode,
599             pp->diffinfo[i].key.seqno,
600             pp->diffinfo[i].mode);
601        p_key->sysno=p_key->seqno=0;  /* used it up */
602      }
603
604      if (filt)
605      { /* we have a whole input stream to inject */
606        pp->diffinfo[i].difftype=DT_INPU;
607      }
608   } /* first time */ 
609         
610   while (retry)
611
612   {
613      retry=0;     
614      winner = 0;
615      for (i=0; (!retry) && (pp->diffinfo[i].difftype); i++)
616      {
617         if (0==pp->diffinfo[i].key.sysno)
618         {/* read a new one, if possible */
619            if ((pp->diffinfo[i].difftype==DT_DIFF) &&
620              (pp->diffinfo[i].diffidx < pp->diffinfo[i].maxidx))
621            { /* a normal kind of diff */
622               oldoffs=pp->diffinfo[i].diffidx;
623               codeptr= codestart = &(pp->diffbuf[pp->diffinfo[i].diffidx]);
624               keyptr=(char *)&(pp->diffinfo[i].key);
625               (*pp->is->method->code_item)(ISAMD_DECODE,
626                     pp->diffinfo[i].decodeData, &keyptr, &codeptr);
627               pp->diffinfo[i].diffidx += codeptr-codestart;
628               pp->diffinfo[i].mode = pp->diffinfo[i].key.seqno & 1;
629               pp->diffinfo[i].key.seqno = pp->diffinfo[i].key.seqno >>1 ;
630               if (pp->is->method->debug > 9)
631                 logf(LOG_LOG,"isamd_read_item: dif[%d] at %d-%d: %s",
632                   i,oldoffs, pp->diffinfo[i].diffidx,
633                   hexdump(pp->buf+oldoffs, pp->diffinfo[i].diffidx-oldoffs,0));
634               if (pp->is->method->debug > 7)
635                 logf(LOG_LOG,"isamd_read_item: rd dif[%d] %d.%d (%x.%x)",
636                   i,
637                   pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
638                   pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
639            }
640            else if ( pp->diffinfo[i].difftype==DT_MAIN)
641            { /* read a main item */
642               assert(i==0);  /* main data goes before any diffs */
643               oldoffs=pp->offset;
644               keyptr=(char*) &(pp->diffinfo[0].key);
645               rc= isamd_read_main_item(pp,&keyptr);
646               if (0==rc) 
647               { /* eof */
648                  if (pp->is->method->debug > 7)
649                    logf(LOG_LOG,"isamd_read_item: eof (rc=%d) main ",
650                            rc);
651                 pp->diffinfo[i].maxidx=-1;
652                 pp->diffinfo[i].key.sysno=0;
653                 pp->diffinfo[i].key.seqno=0;
654                 pp->diffinfo[i].difftype= DT_DONE;
655               } 
656               else
657               { /* not eof */
658                  pp->diffinfo[i].mode = 1;
659                  if (pp->is->method->debug > 7)
660                    logf(LOG_LOG,"isamd_read_item: rd main %d-%d %d.%d (%x.%x) m=%d",
661                      oldoffs,pp->offset,
662                      pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
663                      pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
664                      pp->diffinfo[i].mode);
665               } /* not eof */
666            }
667            else if (pp->diffinfo[i].difftype==DT_INPU)
668            {
669               keyptr = (char *) &pp->diffinfo[i].key;
670          /*     rc = (*data->read_item)(data->clientData, &keyptr, &pp->diffinfo[i].mode); */
671               rc = filter_read(filt, &pp->diffinfo[i].key, 
672                                      &pp->diffinfo[i].mode); 
673               if (!rc) 
674               {  /* did not get it */
675                  pp->diffinfo[i].key.sysno=0;
676                  pp->diffinfo[i].maxidx=0; /* signal the end */
677                  pp->diffinfo[i].difftype=DT_DONE;
678               }
679               if (pp->is->method->debug >7)
680                  logf(LOG_LOG,"merge: read inpu m=%d %d.%d (%x.%x)",
681                     pp->diffinfo[i].mode, 
682                     pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
683                     pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno );        
684            } /* read an input item */
685         } /* read a new one */
686         
687         if (pp->is->method->debug > 8)
688           logf(LOG_LOG,"isamd_read_item: considering d%d %d.%d ix=%d mx=%d",
689                i, pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
690                   pp->diffinfo[i].diffidx,   pp->diffinfo[i].maxidx);
691             
692         if ( 0!= pp->diffinfo[i].key.sysno)
693         { /* got a key, compare */
694           if (i!=winner)
695              cmp=key_compare(&pp->diffinfo[i].key, &pp->diffinfo[winner].key);
696           else
697              cmp=-1;
698           if (0==pp->diffinfo[winner].key.sysno)
699             cmp=-1; /* end of main sequence, take all diffs */
700           if (cmp<0)
701           {
702              if (pp->is->method->debug > 8)
703                logf(LOG_LOG,"isamd_read_item: ins [%d]%d.%d < [%d]%d.%d",
704                  i,  
705                  pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
706                  winner, 
707                  pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
708              if (pp->diffinfo[i].mode)  /* insert diff, should always be */
709                winner = i;
710              else
711              {
712                if (pp->is->method->debug > 1)
713                  logf(LOG_LOG,"delete diff for nonexisting item");
714                assert(!"delete diff for nonexisting item");  
715                /* is an assert too steep here? Not really.*/
716              }
717           } /* earlier key */
718           else if (cmp==0)
719           {
720              if (!pp->diffinfo[i].mode) /* delete diff. should always be */
721              {
722                 if (pp->is->method->debug > 8)
723                   logf(LOG_LOG,"isamd_read_item: del %d at%d %d.%d (%x.%x)",
724                     i, winner,
725                     pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
726                     pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
727                 pp->diffinfo[winner].key.sysno=0; /* delete it */
728              }
729              else
730                 if (pp->is->method->debug > 2)
731                   logf(LOG_LOG,"isamd_read_item: duplicate ins %d at%d %d.%d (%x.%x)",
732                     i, winner,
733                     pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno,
734                     pp->diffinfo[i].key.sysno, pp->diffinfo[i].key.seqno);
735                 /* skip the insert, since we already have it in the base */
736                 /* Should we fail an assertion here??? */
737              pp->diffinfo[i].key.sysno=0; /* done with the delete */
738              retry=1; /* start all over again */
739           } /* matching key */
740           /* else it is a later key, its turn will come */
741         } /* got a key */
742      } /* for each diff */
743   } /* not retry */
744
745   if ( pp->diffinfo[winner].key.sysno)
746   {
747     if (pp->is->method->debug > 7)
748       logf(LOG_LOG,"isamd_read_item: got %d  %d.%d (%x.%x)",
749         winner,
750         pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
751         pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
752     memcpy(*dst, &pp->diffinfo[winner].key, sizeof(struct it_key) );
753     *dst += sizeof(struct it_key);
754     pp->diffinfo[winner].key.sysno=0; /* used that one up */
755     cmp= 1;
756   } 
757   else 
758   {
759     if (pp->is->method->debug > 7)
760       logf(LOG_LOG,"isamd_read_item: eof w=%d  %d.%d (%x.%x)",
761         winner,
762         pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno,
763         pp->diffinfo[winner].key.sysno, pp->diffinfo[winner].key.seqno);
764     assert(winner==0); /* if nothing found, nothing comes from a diff */
765     cmp= 0; /* eof */
766   }
767   if (cmp)
768     ++(pp->is->no_read_keys);
769   else
770     ++(pp->is->no_read_eof);
771
772   return cmp;
773    
774 } /* isamd_read_item */
775
776
777 int isamd_read_item (ISAMD_PP pp, char **dst)
778 {
779   return isamd_read_item_merge(pp,dst,0,0);
780 }
781
782
783 /***************************************************************
784  * Merge
785  ***************************************************************/
786
787 static int merge ( ISAMD_PP firstpp,      /* first pp (with diffs) */
788                    struct it_key *p_key,  /* the data item that didn't fit*/
789                    FILTER filt,           /* more input data arriving here */
790                    char *dictentry,       /* the thin in the dictionary */
791                    int dictlen)           /* and its size */
792 {
793   int diffidx;  
794   int killblk=0;
795   struct it_key r_key;
796   char * r_ptr;
797   int r_more = 1;
798   ISAMD_PP pp;
799   ISAMD_PP readpp=firstpp;
800   int retpos=0;
801   int diffcat = firstpp->cat;  /* keep the category of the diffblock even */
802                                /* if it is going to be empty now. */
803                                /* Alternative: Make it the minimal, and */
804                                /* resize later. Saves disk, but will lead */
805                                /* into bad seeks. */
806   
807   ++(readpp->is->no_merges);
808      
809   /* set up diffs as they should be for reading */
810   diffidx = ISAMD_BLOCK_OFFSET_1; 
811   //readpp->diffbuf=readpp->buf;  // diffinfo has to duplicate it!
812   //getDiffInfo(readpp);  // first read will make the diffinfo, at init
813   
814   if (readpp->is->method->debug >4) 
815       logf(LOG_LOG,"isamd_merge: f=%d=%d:%d n=%d=%d:%d",
816         isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos,
817         firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next));  
818
819   /* release our data block. Do before reading, when pos is stable ! */
820   killblk=firstpp->pos;
821   if (killblk)
822   {
823       isamd_release_block(firstpp->is, firstpp->cat, killblk);
824       if (readpp->is->method->debug >3)   
825           logf(LOG_LOG,"isamd_merge: released old firstblock %d (%d:%d)",
826               isamd_addr(killblk,firstpp->cat), firstpp->cat, killblk );
827   }
828   
829
830   r_ptr= (char *) &r_key;
831   r_more = isamd_read_item_merge( readpp, &r_ptr, p_key, filt);
832   if (!r_more)  
833   { /* oops, all data has been deleted! what to do??? */
834     /* never mind, we have at least one more delta to add to the block */
835     /* pray that is not a delete as well... */
836     r_key.sysno = 0;
837     r_key.seqno = 0;
838      if (readpp->is->method->debug >5) 
839          logf(LOG_LOG,"isamd_merge:all data has been deleted (nk=%d) ",
840             readpp->numKeys);
841   }
842
843
844   /* set up the new blocks for simple writing */
845   /* firstpp=isamd_pp_open(readpp->is,isamd_addr(0, diffcat)); */
846   firstpp=isamd_pp_create(readpp->is, diffcat);
847   firstpp->pos=isamd_alloc_block(firstpp->is,diffcat);
848   if (readpp->is->method->debug >3)   
849       logf(LOG_LOG,"isamd_merge: allocated new firstpp %d=%d:%d",
850           isamd_addr(firstpp->pos,firstpp->cat), firstpp->cat, firstpp->pos );
851   
852   pp=isamd_pp_create(readpp->is,readpp->is->max_cat );
853   pp->offset=pp->size=ISAMD_BLOCK_OFFSET_N;
854   
855   while (r_more)
856   {
857      if (readpp->is->method->debug >6) 
858          logf(LOG_LOG,"isamd_merge: got key %d.%d",
859            r_key.sysno, r_key.seqno );
860      pp= append_main_item(firstpp, pp, &r_key);
861
862      if ( (readpp->pos != killblk ) && (0!=readpp->pos) )
863      {  /* pos can get to 0 at end of main seq, if still diffs left...*/
864         if (readpp->is->method->debug >3)  
865             logf(LOG_LOG,"isamd_merge: released block %d (%d:%d) now %d=%d:%d",
866                 isamd_addr(killblk,readpp->cat), readpp->cat, killblk,
867                 isamd_addr(readpp->pos,readpp->cat),readpp->cat, readpp->pos );
868         isamd_release_block(readpp->is, readpp->cat, readpp->pos);
869         killblk=readpp->pos;
870      }
871
872      /* (try to) read next item */
873      r_ptr= (char *) &r_key;
874      r_more = isamd_read_item_merge( readpp, &r_ptr,0,filt);
875
876   } /* while read */
877   
878   
879 //  firstpp->diffs=0; 
880
881
882   isamd_reduceblock(pp);  /* reduce size if possible */
883   if (0==firstpp->next)
884     firstpp->next = isamd_addr(pp->pos,pp->cat);
885   save_last_pp(pp);
886   if (readpp->is->method->debug >4) 
887       logf(LOG_LOG,"isamd_merge: saved last block %d=%d:%d",
888             isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
889   isamd_pp_close(pp);
890
891   if (readpp->is->method->debug >5) 
892         logf(LOG_LOG,"isamd_merge: closing readpp %d=%d:%d di=%p",
893               isamd_addr(readpp->pos,readpp->cat), readpp->cat, readpp->pos,
894               readpp->diffinfo);
895   isamd_pp_close(readpp); /* pos is 0 by now, at eof. close works anyway */
896
897   if (readpp->is->method->debug >2)  
898       logf(LOG_LOG,"isamd_merge: merge ret f=%d=%d:%d pp=%d=%d:%d",
899             isamd_addr(firstpp->pos,pp->cat), firstpp->cat, firstpp->pos,
900             isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
901
902   firstpp->size = firstpp->offset = ISAMD_BLOCK_OFFSET_1;  /* nothing there */
903   memset(firstpp->buf,'\0',firstpp->is->method->filecat[firstpp->cat].bsize);
904   save_first_pp(firstpp);
905   retpos = isamd_addr(firstpp->pos, firstpp->cat);
906   isamd_pp_close(firstpp);
907
908   /* Create the dict entry */
909   /*!*/ /* it could be this could go in the dict as well, if there's */
910         /* been really many deletes. Somehow I suspect that is not the */
911         /* case. FIXME: Collect statistics and see if needed */
912   dictentry[0]=0; /* mark as a real isam */
913   memcpy(dictentry+1, &retpos, sizeof(ISAMD_P));
914   dictlen=sizeof(ISAMD_P)+1;
915   return dictlen;
916   
917 } /* merge */
918
919
920
921
922 /***************************************************************
923  * Appending diffs 
924  ***************************************************************/
925
926
927
928 static int append_diffs(
929       ISAMD is, 
930       char *dictentry, int dictlen,
931       FILTER filt)
932 {
933    ISAMD_P ipos;
934    struct it_key i_key;    /* one input item */
935    char *i_item = (char *) &i_key;  /* same as chars */
936    char *i_ptr=i_item;
937    int i_more =1;
938    int i_mode;     /* 0 for delete, 1 for insert */ 
939
940    ISAMD_PP firstpp;
941    char hexbuff[64];
942    int diffidx=0;
943    int maxsize=0;
944    int difflenidx;
945    char codebuff[128];
946    char *c_ptr = codebuff;
947    int codelen;
948    int merge_rc;
949    ISAMD_P retpos;
950    int dsize;
951
952    if (0==dictlen)
953    {
954        firstpp=isamd_pp_create(is, 0 );
955        firstpp->size=firstpp->offset=ISAMD_BLOCK_OFFSET_1;
956          /* create in smallest category, will expand later */
957        ++(is->no_fbuilds);
958    } 
959    else
960    {
961        firstpp=isamd_pp_open(is, dictentry, dictlen);
962        if (dictentry[0] )
963           ipos=0;
964        else 
965            memcpy(&ipos,dictentry+1,sizeof(ISAMD_P));
966        ++(is->no_appds);
967    }
968
969    if (is->method->debug >2) 
970       logf(LOG_LOG,"isamd_appd: Start ipos=%d=%d:%d n=%d=%d:%d nk=%d sz=%d",
971         ipos, isamd_type(ipos), isamd_block(ipos),
972         firstpp->next, isamd_type(firstpp->next), isamd_block(firstpp->next),
973         firstpp->numKeys, firstpp->size);
974    maxsize = is->method->filecat[firstpp->cat].bsize; 
975    
976    difflenidx = diffidx = firstpp->size;
977    
978    diffidx+=sizeof(int);  /* difflen will be stored here */
979    
980    /* read first input */
981    //i_ptr = i_item;   //!!!
982    i_more = filter_read(filt, &i_key, &i_mode); 
983    /* i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode); */
984
985    if (is->method->debug >6)
986       logf(LOG_LOG,"isamd_appd: start m=%d %d.%d=%x.%x: %d",
987          i_mode, 
988          i_key.sysno, i_key.seqno, 
989          i_key.sysno, i_key.seqno,
990          i_key.sysno*2+i_mode);
991
992    while (i_more)
993    {     
994       /* store the mode bit inside key */
995       assert( ((i_key.seqno<<1)>>1) == i_key.seqno); /* can spare the bit */
996       i_key.seqno = i_key.seqno * 2 + i_mode;  
997
998       c_ptr=codebuff;
999       i_ptr=i_item; 
1000       (*is->method->code_item)(ISAMD_ENCODE, firstpp->decodeClientData, 
1001                                &c_ptr, &i_ptr);
1002       codelen = c_ptr - codebuff;
1003       assert ( (codelen<128) && (codelen>0));
1004       if (is->method->debug >7)
1005          logf(LOG_LOG,"isamd_appd: coded %d: %s (nk=%d) (ix=%d)",
1006              codelen, hexdump(codebuff, codelen,hexbuff), 
1007              firstpp->numKeys,diffidx);
1008
1009       if (diffidx + codelen > maxsize )
1010       { /* block full */
1011          while ( (firstpp->cat < firstpp->is->max_cat) &&
1012                  (diffidx + codelen > maxsize) )
1013          { /* try to increase the block size */
1014              if (firstpp->pos > 0)  /* free the old block if allocated */
1015                  isamd_release_block(is, firstpp->cat, firstpp->pos);
1016              ++firstpp->cat;
1017              maxsize = is->method->filecat[firstpp->cat].bsize; 
1018              firstpp->pos=0; /* need to allocate it when saving */             
1019              if (is->method->debug >3)
1020                 logf(LOG_LOG,"isamd_appd: increased diff block sz to %d (%d)",
1021                    firstpp->cat, maxsize);
1022          }
1023          if  ((firstpp->cat >= firstpp->is->max_cat) &&
1024                  (diffidx + codelen > maxsize) )
1025          { /* max size - can't help, need to merge it */
1026              if (is->method->debug >7)
1027                 logf(LOG_LOG,"isamd_appd: need to merge");
1028              if (is->method->debug >9)  //!!!!!
1029                 logf(LOG_LOG,"isamd_appd: going to merge with m=%d %d.%d",
1030                      i_mode, i_key.sysno, i_key.seqno);
1031              merge_rc = merge (firstpp, &i_key, filt, dictentry, dictlen);
1032              if (0!=merge_rc)
1033                return merge_rc;  /* merge handled them all ! */
1034              assert(!"merge returned zero ??");
1035          } /* need to merge */
1036       } /* block full */
1037
1038       if (!( diffidx+codelen <= maxsize )) 
1039       { /* bug hunting */
1040          logf(LOG_LOG,"OOPS, diffidx problem: d=%d c=%d s=%d > m=%d",
1041            diffidx, codelen, diffidx+codelen, maxsize);
1042          logf(LOG_LOG,"ipos=%d f=%d=%d:%d",
1043            ipos, 
1044            isamd_addr(firstpp->pos, firstpp->cat),
1045            firstpp->cat, firstpp->pos );
1046       }
1047       assert ( diffidx+codelen <= maxsize );
1048       
1049       /* save the diff */ 
1050       memcpy(&(firstpp->buf[diffidx]),codebuff,codelen);
1051       diffidx += codelen;
1052       firstpp->size = firstpp->offset = diffidx;
1053       
1054       if (i_mode)
1055         firstpp->numKeys++; /* insert diff */
1056       else
1057         firstpp->numKeys--; /* delete diff */ 
1058
1059       /* update length of this diff run */
1060       memcpy(&(firstpp->buf[difflenidx]),&diffidx,sizeof(diffidx));
1061       
1062       /* (try to) read the next input */
1063       i_ptr = i_item;
1064       i_more = filter_read(filt, &i_key, &i_mode); 
1065     /*  i_more = (*data->read_item)(data->clientData, &i_ptr, &i_mode); */
1066       if ( (i_more) && (is->method->debug >6) )
1067          logf(LOG_LOG,"isamd_appd: got m=%d %d.%d=%x.%x: %d",
1068             i_mode, 
1069             i_key.sysno, i_key.seqno, 
1070             i_key.sysno, i_key.seqno,
1071             i_key.sysno*2+i_mode);
1072    } /* more loop */
1073
1074    /* clear the next difflen, if room for such */
1075    difflenidx = diffidx;
1076    while ( (difflenidx-diffidx<=sizeof(int)+1) && (difflenidx<maxsize))
1077      firstpp->buf[difflenidx++]='\0';
1078
1079    if (firstpp->numKeys==0)
1080    { 
1081        /* FIXME: Release blocks that may be allocated !!! */
1082        return 0; /* don't bother storing this! */
1083    }
1084
1085    dsize=diffidx-ISAMD_BLOCK_OFFSET_1;
1086    /* logf(LOG_LOG,"!! nxt=%d diffidx=%d ds=%d", 
1087            firstpp->next, diffidx, dsize);  */
1088
1089    if ( (0==firstpp->next) && (dsize <ISAMD_MAX_DICT_LEN))
1090    {
1091         /* logf(LOG_LOG,"building a dict entry!!"); */
1092         assert(firstpp->numKeys < 128);
1093         assert(firstpp->numKeys >0);
1094         /* actually, 255 is good enough, but sign mismatches... */
1095         /* in real life, 4-5 is as much as we can hope for, as long */
1096         /* as ISAMD_MAX_DICT_LEN is reasonably small (8) */
1097         dictentry[0]=firstpp->numKeys;
1098         memcpy(dictentry+1, firstpp->buf+ISAMD_BLOCK_OFFSET_1, dsize);
1099         dictlen=dsize+1;
1100    }
1101    else 
1102    {
1103        if (0==firstpp->pos)  /* need to (re)alloc the block */
1104            firstpp->pos = isamd_alloc_block(is, firstpp->cat);
1105        retpos = save_first_pp( firstpp );
1106        isamd_pp_close(firstpp);
1107        dictentry[0]=0; /* mark as a real isam */
1108        memcpy(dictentry+1, &retpos, sizeof(ISAMD_P));
1109        dictlen=sizeof(ISAMD_P)+1;
1110    }
1111     
1112    return dictlen;
1113 } /* append_diffs */
1114
1115
1116
1117
1118 /*************************************************************
1119  * isamd_append itself
1120  *************************************************************/
1121
1122 int isamd_append (ISAMD is, char *dictentry, int dictlen, ISAMD_I data)
1123 /*ISAMD_P isamd_append (ISAMD is, ISAMD_P ipos, ISAMD_I data) */
1124 {
1125    FILTER F = filter_open(is,data);
1126    int newlen=0;
1127
1128    if ( filter_isempty(F) ) /* can be, if del-ins of the same */
1129    {
1130       if (is->method->debug >3) 
1131          logf(LOG_LOG,"isamd_appd: nothing to do ");
1132       filter_close(F);
1133       ++(is->no_non);
1134       return dictlen; /* without doing anything at all */
1135    }
1136
1137 #ifdef SKIPTHIS 
1138    /* The old way to handle singletons */
1139    if ( ( 0==ipos) && filter_only_one(F) )
1140    {
1141       struct it_key k;
1142       int mode;
1143       filter_read(F,&k,&mode);     
1144       assert(mode); 
1145       rc = singleton_encode(&k);
1146       if (!rc) 
1147       {
1148       if (is->method->debug >9) 
1149          logf(LOG_LOG,"isamd_appd: singleton didn't fit, backfilling");
1150          filter_backfill(F,&k, mode);
1151       }
1152       if (is->method->debug >9) 
1153          logf(LOG_LOG,"isamd_appd: singleton %d (%x)",
1154            rc,rc);
1155       if (rc)
1156         is->no_singles++;
1157       assert ( (rc==0) || is_singleton(rc) );
1158    }
1159    newlen = append_diffs(is,ipos,F); 
1160 #endif
1161    newlen = append_diffs(is,dictentry,dictlen,F); 
1162    filter_close(F);
1163
1164    if (is->method->debug >2) 
1165       logf(LOG_LOG,"isamd_appd: ret len=%d ", newlen);
1166    return newlen;
1167 } /*  isamd_append */
1168
1169
1170
1171
1172
1173
1174
1175 /*
1176  * $Log: merge-d.c,v $
1177  * Revision 1.27  2002-07-12 18:12:21  heikki
1178  * Isam-D now stores small entries directly in the dictionary.
1179  * Needs more tuning and cleaning...
1180  *
1181  * Revision 1.26  2002/07/11 16:16:00  heikki
1182  * Fixed a bug in isamd, failed to store a single key when its bits
1183  * did not fit into a singleton.
1184  *
1185  * Revision 1.25  1999/11/30 13:48:04  adam
1186  * Improved installation. Updated for inclusion of YAZ header files.
1187  *
1188  * Revision 1.24  1999/10/05 09:57:40  heikki
1189  * Tuning the isam-d (and fixed a small "detail")
1190  *
1191  * Revision 1.23  1999/09/27 14:36:36  heikki
1192  * singletons
1193  *
1194  * Revision 1.22  1999/09/23 18:01:18  heikki
1195  * singleton optimising
1196  *
1197  * Revision 1.21  1999/09/21 17:36:43  heikki
1198  * Added filter function. Not much of effect on the small test set...
1199  *
1200  * Revision 1.20  1999/09/20 15:48:06  heikki
1201  * Small changes
1202  *
1203  * Revision 1.19  1999/09/13 13:28:28  heikki
1204  * isam-d optimizing: merging input data in the same go
1205  *
1206  * Revision 1.18  1999/08/25 18:09:24  heikki
1207  * Starting to optimize
1208  *
1209  * Revision 1.17  1999/08/24 13:17:42  heikki
1210  * Block sizes, comments
1211  *
1212  * Revision 1.16  1999/08/24 10:12:02  heikki
1213  * Comments about optimising
1214  *
1215  * Revision 1.15  1999/08/22 08:26:34  heikki
1216  * COmments
1217  *
1218  * Revision 1.14  1999/08/20 12:25:58  heikki
1219  * Statistics in isamd
1220  *
1221  * Revision 1.13  1999/08/18 13:59:19  heikki
1222  * Fixed another unlikely difflen bug
1223  *
1224  * Revision 1.12  1999/08/18 13:28:17  heikki
1225  * Set log levels to decent values
1226  *
1227  * Revision 1.11  1999/08/18 10:37:11  heikki
1228  * Fixed (another) difflen bug
1229  *
1230  * Revision 1.10  1999/08/18 09:13:31  heikki
1231  * Fixed a detail
1232  *
1233  * Revision 1.9  1999/08/17 19:46:53  heikki
1234  * Fixed a memory leak
1235  *
1236  * Revision 1.8  1999/08/07 11:30:59  heikki
1237  * Bug fixing (still a mem leak somewhere)
1238  *
1239  * Revision 1.7  1999/08/04 14:21:18  heikki
1240  * isam-d seems to be working.
1241  *
1242  * Revision 1.6  1999/07/23 15:43:05  heikki
1243  * Hunted a few bugs in isam-d. Still crashes on the long test run
1244  *
1245  * Revision 1.5  1999/07/23 13:58:52  heikki
1246  * merged closer to working, still fails on filling a separate, large block
1247  *
1248  * Revision 1.4  1999/07/21 14:53:55  heikki
1249  * isamd read and write functions work, except when block full
1250  * Merge missing still. Need to split some functions
1251  *
1252  * Revision 1.1  1999/07/14 13:14:47  heikki
1253  * Created empty
1254  *
1255  *
1256  */
1257
1258