+{ /* Has to return all hits where each item points to the */
+ /* same sysno (scope), in order. Keep an extra key (hitkey) */
+ /* as long as all records do not point to hitkey, forward */
+ /* them, and update hitkey to be the highest seen so far. */
+ /* (if any item eof's, mark eof, and return 0 thereafter) */
+ /* Once a hit has been found, scan all items for the smallest */
+ /* value. Mark all as being in the tail. Read next from that */
+ /* item, and if not in the same record, clear its tail bit */
+ struct rset_multiandor_rfd *p=rfd->priv;
+ const struct key_control *kctrl=rfd->rset->keycontrol;
+ struct rset_multiandor_info *info=rfd->rset->priv;
+ int i, mintail;
+ int cmp;
+
+ while (1) {
+ if (p->tailcount)
+ { /* we are tailing, find lowest tail and return it */
+ mintail=0;
+ while ((mintail<info->no_rsets) && !p->tailbits[mintail])
+ mintail++; /* first tail */
+ for (i=mintail+1;i<info->no_rsets;i++)
+ {
+ if (p->tailbits[i])
+ {
+ cmp=(*kctrl->cmp)(p->items[i].buf,p->items[mintail].buf);
+ if (cmp<0)
+ mintail=i;
+ }
+ }
+ /* return the lowest tail */
+ memcpy(buf, p->items[mintail].buf, kctrl->key_size);
+ if (!rset_read(p->items[mintail].fd, p->items[mintail].buf))
+ {
+ p->eof=1; /* game over, once tails have been returned */
+ p->tailbits[mintail]=0;
+ (p->tailcount)--;
+ return 1;
+ }
+ /* still a tail? */
+ cmp=(*kctrl->cmp)(p->items[mintail].buf,buf);
+ if (cmp >= rfd->rset->scope){
+ p->tailbits[mintail]=0;
+ (p->tailcount)--;
+ }
+ return 1;
+ }
+ /* not tailing, forward until all reocrds match, and set up */
+ /* as tails. the earlier 'if' will then return the hits */
+ if (p->eof)
+ return 0; /* nothing more to see */
+ i=1; /* assume items[0] is highest up */
+ while (i<info->no_rsets) {
+ cmp=(*kctrl->cmp)(p->items[0].buf,p->items[i].buf);
+ if (cmp<=-rfd->rset->scope) { /* [0] was behind, forward it */
+ if (!rset_forward(p->items[0].fd, p->items[0].buf,
+ p->items[i].buf))
+ {
+ p->eof=1; /* game over */
+ return 0;
+ }
+ i=0; /* start frowarding from scratch */
+ } else if (cmp>=rfd->rset->scope)
+ { /* [0] was ahead, forward i */
+ if (!rset_forward(p->items[i].fd, p->items[i].buf,
+ p->items[0].buf))
+ {
+ p->eof=1; /* game over */
+ return 0;
+ }
+ } else
+ i++;
+ } /* while i */
+ /* if we get this far, all rsets are now within +- scope of [0] */
+ /* ergo, we have a hit. Mark them all as tailing, and let the */
+ /* upper 'if' return the hits in right order */
+ for (i=0; i<info->no_rsets;i++)
+ p->tailbits[i]=1;
+ p->tailcount=info->no_rsets;
+ } /* while 1 */