- for (i = 1; i < info->p.rset_no; i++)
- {
- if (!p->more[i])
- {
- p->more[0] = 0; /* saves us a goto out of while loop. */
- break;
- }
- cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
- if (cmp > 1)
- {
- p->more[i-1] = rset_forward (info->p.rset[i-1], p->rfd[i-1],
- p->buf[i-1], &dummy,
- info->p.cmp,
- p->buf[i]);
- break;
- }
- else if (cmp == 1)
- {
- if ((*info->p.getseq)(p->buf[i-1]) +1 != (*info->p.getseq)(p->buf[i]))
- {
- p->more[i-1] = rset_read (info->p.rset[i-1], p->rfd[i-1],
- p->buf[i-1], &dummy);
- break;
- }
- }
- else
- {
- p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
- p->buf[i], &dummy,
- info->p.cmp,
- p->buf[i-1]);
- break;
- }
- }
- if (i == p->info->p.rset_no)
- {
- memcpy (buf, p->buf[0], info->p.key_size);
- *term_index = 0;
-
- p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
- p->buf[0], &dummy);
- return 1;
- }
+ while (p->more[0] && p->more[1])
+ {
+ int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
+ if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
+ p->more[0] = rset_forward(p->rfd[0], p->buf[0],
+ &p->terms[0],p->buf[1]);
+ else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
+ p->more[1] = rset_forward(p->rfd[1], p->buf[1],
+ &p->terms[1],p->buf[0]);
+ else
+ {
+ zint seqno[500]; /* FIXME - why 500 ?? */
+ int n = 0;
+
+ seqno[n++] = (*kctrl->getseq)(p->buf[0]);
+ while ((p->more[0] = rset_read(p->rfd[0],
+ p->buf[0], &p->terms[0])))
+ {
+ cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
+ if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
+ break;
+ if (n < 500)
+ seqno[n++] = (*kctrl->getseq)(p->buf[0]);
+ }
+ /* set up return buffer.. (save buf[1]) */
+ memcpy(buf, p->buf[1], kctrl->key_size);
+ if (term)
+ *term = p->terms[1];
+ while (1)
+ {
+ for (i = 0; i < n; i++)
+ {
+ zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
+ int excl = info->exclusion;
+ if (!info->ordered && diff < 0)
+ diff = -diff;
+ switch (info->relation)
+ {
+ case 1: /* < */
+ if (diff < info->distance && diff >= 0)
+ excl = !excl;
+ break;
+ case 2: /* <= */
+ if (diff <= info->distance && diff >= 0)
+ excl = !excl;
+ break;
+ case 3: /* == */
+ if (diff == info->distance && diff >= 0)
+ excl = !excl;
+ break;
+ case 4: /* >= */
+ if (diff >= info->distance && diff >= 0)
+ excl = !excl;
+ break;
+ case 5: /* > */
+ if (diff > info->distance && diff >= 0)
+ excl = !excl;
+ break;
+ case 6: /* != */
+ if (diff != info->distance && diff >= 0)
+ excl = !excl;
+ break;
+ }
+ if (excl)
+ {
+ p->more[1] = rset_read( p->rfd[1], p->buf[1],
+ &p->terms[1]);
+ p->hits++;
+ return 1;
+ }
+ }
+ p->more[1] = rset_read(p->rfd[1], p->buf[1], &p->terms[1]);
+ if (!p->more[1])
+ break;
+ cmp = (*kctrl->cmp)(buf, p->buf[1]);
+ if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
+ break;
+ }
+ }
+ }