Improve pos of rset_create_or
[idzebra-moved-to-github.git] / rset / rsmultiandor.c
index ccf57de..f65039a 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: rsmultiandor.c,v 1.20 2006-05-10 08:13:33 adam Exp $
-   Copyright (C) 1995-2005
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1994-2011 Index Data
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -15,14 +12,15 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 
 */
 
 
-/*
- * This module implements the rsmulti_or and rsmulti_and result sets
+/**
+ * \file rsmultiandor.c
+ * \brief This module implements the rsmulti_or and rsmulti_and result sets
  *
  * rsmultior is based on a heap, from which we find the next hit.
  *
  *
  * rsmultior is based on a heap, from which we find the next hit.
  *
@@ -34,6 +32,9 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  */
 
 
  */
 
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -54,10 +55,11 @@ static int r_forward_and(RSFD rfd, void *buf, TERMID *term,
                      const void *untilbuf);
 static int r_forward_or(RSFD rfd, void *buf, TERMID *term,
                      const void *untilbuf);
                      const void *untilbuf);
 static int r_forward_or(RSFD rfd, void *buf, TERMID *term,
                      const void *untilbuf);
-static void r_pos (RSFD rfd, double *current, double *total);
+static void r_pos_and(RSFD rfd, double *current, double *total);
+static void r_pos_or(RSFD rfd, double *current, double *total);
 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
 
 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
 
-static const struct rset_control control_or = 
+static const struct rset_control control_or =
 {
     "multi-or",
     r_delete,
 {
     "multi-or",
     r_delete,
@@ -65,12 +67,12 @@ static const struct rset_control control_or =
     r_open_or,
     r_close,
     r_forward_or,
     r_open_or,
     r_close,
     r_forward_or,
-    r_pos,
+    r_pos_or,
     r_read_or,
     r_write,
 };
 
     r_read_or,
     r_write,
 };
 
-static const struct rset_control control_and = 
+static const struct rset_control control_and =
 {
     "multi-and",
     r_delete,
 {
     "multi-and",
     r_delete,
@@ -78,17 +80,17 @@ static const struct rset_control control_and =
     r_open_and,
     r_close,
     r_forward_and,
     r_open_and,
     r_close,
     r_forward_and,
-    r_pos,
+    r_pos_and,
     r_read_and,
     r_write,
 };
 
     r_read_and,
     r_write,
 };
 
-/* The heap structure: 
- * The rset contains a list or rsets we are ORing together 
+/* The heap structure:
+ * The rset contains a list or rsets we are ORing together
  * The rfd contains a heap of heap-items, which contain
  * a rfd opened to those rsets, and a buffer for one key.
  * The rfd contains a heap of heap-items, which contain
  * a rfd opened to those rsets, and a buffer for one key.
- * They also contain a ptr to the rset list in the rset 
- * itself, for practical reasons. 
+ * They also contain a ptr to the rset list in the rset
+ * itself, for practical reasons.
  */
 
 struct heap_item {
  */
 
 struct heap_item {
@@ -119,6 +121,8 @@ struct rfd_private {
     zint hits; /* returned so far */
     int eof; /* seen the end of it */
     int tailcount; /* how many items are tailing */
     zint hits; /* returned so far */
     int eof; /* seen the end of it */
     int tailcount; /* how many items are tailing */
+    zint segment;
+    int skip;
     char *tailbits;
 };
 
     char *tailbits;
 };
 
@@ -135,7 +139,7 @@ static void heap_dump_item( HEAP h, int i, int level)
     if (i>h->heapnum)
         return;
     (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
     if (i>h->heapnum)
         return;
     (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
-    yaz_log(log_level," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",  
+    yaz_log(log_level," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",
                     &(h->heap[i]), h->heap[i]->buf, cur,tot );
     heap_dump_item(h, 2*i, level+1);
     heap_dump_item(h, 2*i+1, level+1);
                     &(h->heap[i]), h->heap[i]->buf, cur,tot );
     heap_dump_item(h, 2*i, level+1);
     heap_dump_item(h, 2*i+1, level+1);
@@ -164,8 +168,10 @@ static int heap_empty(HEAP h)
     return ( 0==h->heapnum );
 }
 
     return ( 0==h->heapnum );
 }
 
+/** \brief deletes the first item in the heap, and balances the rest
+ */
 static void heap_delete (HEAP h)
 static void heap_delete (HEAP h)
-{ /* deletes the first item in the heap, and balances the rest */
+{
     int cur = 1, child = 2;
     h->heap[1] = 0; /* been deleted */
     heap_swap (h, 1, h->heapnum--);
     int cur = 1, child = 2;
     h->heap[1] = 0; /* been deleted */
     heap_swap (h, 1, h->heapnum--);
@@ -183,9 +189,12 @@ static void heap_delete (HEAP h)
     }
 }
 
     }
 }
 
+/** \brief puts item into heap.
+    The heap root element has changed value (to bigger)
+    Swap downwards until the heap is ordered again
+*/
 static void heap_balance (HEAP h)
 static void heap_balance (HEAP h)
-{ /* The heap root element has changed value (to bigger) */
-  /* swap downwards until the heap is ordered again */
+{
     int cur = 1, child = 2;
     while (child <= h->heapnum) {
         if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
     int cur = 1, child = 2;
     while (child <= h->heapnum) {
         if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
@@ -245,10 +254,12 @@ static void heap_destroy (HEAP h)
     /* nothing to delete, all is nmem'd, and will go away in due time */
 }
 
     /* nothing to delete, all is nmem'd, and will go away in due time */
 }
 
+/** \brief compare and items for quicksort
+    used in qsort to get the multi-and args in optimal order
+    that is, those with fewest occurrences first
+*/
 int compare_ands(const void *x, const void *y)
 int compare_ands(const void *x, const void *y)
-{ /* used in qsort to get the multi-and args in optimal order */
-  /* that is, those with fewest occurrences first */
-    const struct heap_item *hx = x;
+{   const struct heap_item *hx = x;
     const struct heap_item *hy = y;
     double cur, totx, toty;
     rset_pos(hx->fd, &cur, &totx);
     const struct heap_item *hy = y;
     double cur, totx, toty;
     rset_pos(hx->fd, &cur, &totx);
@@ -260,12 +271,10 @@ int compare_ands(const void *x, const void *y)
     return 0;  /* return totx - toty, except for overflows and rounding */
 }
 
     return 0;  /* return totx - toty, except for overflows and rounding */
 }
 
-/* Creating and deleting rsets ***********************/
-
 static RSET rsmulti_andor_create(NMEM nmem,
 static RSET rsmulti_andor_create(NMEM nmem,
-                                struct rset_key_control *kcontrol, 
+                                struct rset_key_control *kcontrol,
                                 int scope, TERMID termid,
                                 int scope, TERMID termid,
-                                int no_rsets, RSET* rsets, 
+                                int no_rsets, RSET* rsets,
                                 const struct rset_control *ctrl)
 {
     RSET rnew = rset_create_base(ctrl, nmem, kcontrol, scope, termid,
                                 const struct rset_control *ctrl)
 {
     RSET rnew = rset_create_base(ctrl, nmem, kcontrol, scope, termid,
@@ -282,15 +291,15 @@ static RSET rsmulti_andor_create(NMEM nmem,
     return rnew;
 }
 
     return rnew;
 }
 
-RSET rsmulti_or_create(NMEM nmem, struct rset_key_control *kcontrol,
-                      int scope, TERMID termid, int no_rsets, RSET* rsets)
+RSET rset_create_or(NMEM nmem, struct rset_key_control *kcontrol,
+                    int scope, TERMID termid, int no_rsets, RSET* rsets)
 {
     return rsmulti_andor_create(nmem, kcontrol, scope, termid,
                                 no_rsets, rsets, &control_or);
 }
 
 {
     return rsmulti_andor_create(nmem, kcontrol, scope, termid,
                                 no_rsets, rsets, &control_or);
 }
 
-RSET rsmulti_and_create(NMEM nmem, struct rset_key_control *kcontrol,
-                       int scope, int no_rsets, RSET* rsets)
+RSET rset_create_and(NMEM nmem, struct rset_key_control *kcontrol,
+                     int scope, int no_rsets, RSET* rsets)
 {
     return rsmulti_andor_create(nmem, kcontrol, scope, 0,
                                 no_rsets, rsets, &control_and);
 {
     return rsmulti_andor_create(nmem, kcontrol, scope, 0,
                                 no_rsets, rsets, &control_and);
@@ -300,8 +309,6 @@ static void r_delete (RSET ct)
 {
 }
 
 {
 }
 
-/* Opening and closing fd's on them *********************/
-
 static RSFD r_open_andor (RSET ct, int flag, int is_and)
 {
     RSFD rfd;
 static RSFD r_open_andor (RSET ct, int flag, int is_and)
 {
     RSFD rfd;
@@ -322,16 +329,17 @@ static RSFD r_open_andor (RSET ct, int flag, int is_and)
         assert(p->items);
         /* all other pointers shouls already be allocated, in right sizes! */
     }
         assert(p->items);
         /* all other pointers shouls already be allocated, in right sizes! */
     }
-    else {
+    else
+    {
         p = (struct rfd_private *) nmem_malloc (ct->nmem,sizeof(*p));
         rfd->priv = p;
         p->h = 0;
         p->tailbits = 0;
         if (is_and)
             p->tailbits = nmem_malloc(ct->nmem, ct->no_children*sizeof(char) );
         p = (struct rfd_private *) nmem_malloc (ct->nmem,sizeof(*p));
         rfd->priv = p;
         p->h = 0;
         p->tailbits = 0;
         if (is_and)
             p->tailbits = nmem_malloc(ct->nmem, ct->no_children*sizeof(char) );
-        else 
+        else
             p->h = heap_create( ct->nmem, ct->no_children, kctrl);
             p->h = heap_create( ct->nmem, ct->no_children, kctrl);
-        p->items = (struct heap_item *) 
+        p->items = (struct heap_item *)
            nmem_malloc(ct->nmem, ct->no_children*sizeof(*p->items));
         for (i = 0; i<ct->no_children; i++)
        {
            nmem_malloc(ct->nmem, ct->no_children*sizeof(*p->items));
         for (i = 0; i<ct->no_children; i++)
        {
@@ -352,7 +360,8 @@ static RSFD r_open_andor (RSET ct, int flag, int is_and)
             p->tailbits[i] = 0;
         }
         qsort(p->items, ct->no_children, sizeof(p->items[0]), compare_ands);
             p->tailbits[i] = 0;
         }
         qsort(p->items, ct->no_children, sizeof(p->items[0]), compare_ands);
-    } else
+    }
+    else
     { /* fill the heap for ORing */
         for (i = 0; i<ct->no_children; i++){
             p->items[i].fd = rset_open(ct->children[i],RSETF_READ);
     { /* fill the heap for ORing */
         for (i = 0; i<ct->no_children; i++){
             p->items[i].fd = rset_open(ct->children[i],RSETF_READ);
@@ -381,14 +390,12 @@ static void r_close (RSFD rfd)
 
     if (p->h)
         heap_destroy (p->h);
 
     if (p->h)
         heap_destroy (p->h);
-    for (i = 0; i<rfd->rset->no_children; i++) 
+    for (i = 0; i<rfd->rset->no_children; i++)
         if (p->items[i].fd)
             rset_close(p->items[i].fd);
 }
 
         if (p->items[i].fd)
             rset_close(p->items[i].fd);
 }
 
-
-
-static int r_forward_or(RSFD rfd, void *buf, 
+static int r_forward_or(RSFD rfd, void *buf,
                         TERMID *term, const void *untilbuf)
 { /* while heap head behind untilbuf, forward it and rebalance heap */
     struct rfd_private *p = rfd->priv;
                         TERMID *term, const void *untilbuf)
 { /* while heap head behind untilbuf, forward it and rebalance heap */
     struct rfd_private *p = rfd->priv;
@@ -400,7 +407,7 @@ static int r_forward_or(RSFD rfd, void *buf,
         if (rset_forward(p->h->heap[1]->fd,p->h->heap[1]->buf,
                          &p->h->heap[1]->term, untilbuf))
             heap_balance(p->h);
         if (rset_forward(p->h->heap[1]->fd,p->h->heap[1]->buf,
                          &p->h->heap[1]->term, untilbuf))
             heap_balance(p->h);
-        else 
+        else
         {
             heap_delete(p->h);
             if (heap_empty(p->h))
         {
             heap_delete(p->h);
             if (heap_empty(p->h))
@@ -412,6 +419,13 @@ static int r_forward_or(RSFD rfd, void *buf,
 }
 
 
 }
 
 
+/** \brief reads one item key from an 'or' set
+    \param rfd set handle
+    \param buf resulting item buffer
+    \param term resulting term
+    \retval 0 EOF
+    \retval 1 item could be read
+*/
 static int r_read_or (RSFD rfd, void *buf, TERMID *term)
 {
     RSET rset = rfd->rset;
 static int r_read_or (RSFD rfd, void *buf, TERMID *term)
 {
     RSET rset = rfd->rset;
@@ -429,7 +443,6 @@ static int r_read_or (RSFD rfd, void *buf, TERMID *term)
            *term = rset->term;
        else
            *term = it->term;
            *term = rset->term;
        else
            *term = it->term;
-       assert(*term);
     }
     (mrfd->hits)++;
     rdres = rset_read(it->fd, it->buf, &it->term);
     }
     (mrfd->hits)++;
     rdres = rset_read(it->fd, it->buf, &it->term);
@@ -441,97 +454,130 @@ static int r_read_or (RSFD rfd, void *buf, TERMID *term)
 
 }
 
 
 }
 
+/** \brief reads one item key from an 'and' set
+    \param rfd set handle
+    \param buf resulting item buffer
+    \param term resulting term
+    \retval 0 EOF
+    \retval 1 item could be read
+
+    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
+*/
 static int r_read_and (RSFD rfd, void *buf, TERMID *term)
 static int r_read_and (RSFD rfd, void *buf, TERMID *term)
-{ /* 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 rfd_private *p = rfd->priv;
+{   struct rfd_private *p = rfd->priv;
     RSET ct = rfd->rset;
     const struct rset_key_control *kctrl = ct->keycontrol;
     RSET ct = rfd->rset;
     const struct rset_key_control *kctrl = ct->keycontrol;
-    int i, mintail;
-    int cmp;
+    int i;
 
     while (1) {
 
     while (1) {
-        if (p->tailcount) 
+        if (p->tailcount)
         { /* we are tailing, find lowest tail and return it */
         { /* we are tailing, find lowest tail and return it */
-            mintail = 0;
-            while ((mintail<ct->no_children) && !p->tailbits[mintail])
-                mintail++; /* first tail */
-            for (i = mintail+1; i<ct->no_children; i++)
+            int mintail = -1;
+            int cmp;
+
+            for (i = 0; i<ct->no_children; i++)
             {
                 if (p->tailbits[i])
                 {
             {
                 if (p->tailbits[i])
                 {
-                    cmp=(*kctrl->cmp)(p->items[i].buf,p->items[mintail].buf);
-                    if (cmp<0) 
+                    if (mintail >= 0)
+                        cmp = (*kctrl->cmp)
+                            (p->items[i].buf, p->items[mintail].buf);
+                    else
+                        cmp = -1;
+                    if (cmp < 0)
                         mintail = i;
                         mintail = i;
+
+                    if (kctrl->get_segment)
+                    {   /* segments enabled */
+                        zint segment =  kctrl->get_segment(p->items[i].buf);
+                        /* store segment if not stored already */
+                        if (!p->segment && segment)
+                            p->segment = segment;
+
+                        /* skip rest entirely if segments don't match */
+                        if (p->segment && segment && p->segment != segment)
+                            p->skip = 1;
+                    }
                 }
             }
             /* return the lowest tail */
                 }
             }
             /* return the lowest tail */
-            memcpy(buf, p->items[mintail].buf, kctrl->key_size); 
+            memcpy(buf, p->items[mintail].buf, kctrl->key_size);
             if (term)
                 *term = p->items[mintail].term;
             if (!rset_read(p->items[mintail].fd, p->items[mintail].buf,
                            &p->items[mintail].term))
             {
                 p->eof = 1; /* game over, once tails have been returned */
             if (term)
                 *term = p->items[mintail].term;
             if (!rset_read(p->items[mintail].fd, p->items[mintail].buf,
                            &p->items[mintail].term))
             {
                 p->eof = 1; /* game over, once tails have been returned */
-                p->tailbits[mintail] = 0; 
-                (p->tailcount)--;
-               (p->hits)++;
-                return 1;
-            }
-            /* still a tail? */
-            cmp = (*kctrl->cmp)(p->items[mintail].buf,buf);
-            if (cmp >= rfd->rset->scope){
                 p->tailbits[mintail] = 0;
                 (p->tailcount)--;
             }
                 p->tailbits[mintail] = 0;
                 (p->tailcount)--;
             }
-           (p->hits)++;
+            else
+            {
+                /* still a tail? */
+                cmp = (*kctrl->cmp)(p->items[mintail].buf,buf);
+                if (cmp >= rfd->rset->scope)
+                {
+                    p->tailbits[mintail] = 0;
+                    (p->tailcount)--;
+                }
+            }
+            if (p->skip)
+                continue;  /* skip again.. eventually tailcount will be 0 */
+            if (p->tailcount == 0)
+                (p->hits)++;
             return 1;
             return 1;
-        } 
-        /* not tailing, forward until all reocrds match, and set up */
+        }
+        /* not tailing, forward until all records 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 */
         /* 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<ct->no_children) {
-            cmp = (*kctrl->cmp)(p->items[0].buf, p->items[i].buf);
+        while (i < ct->no_children)
+        {
+            int cmp = (*kctrl->cmp)(p->items[0].buf, p->items[i].buf);
             if (cmp <= -rfd->rset->scope) { /* [0] was behind, forward it */
             if (cmp <= -rfd->rset->scope) { /* [0] was behind, forward it */
-                if (!rset_forward(p->items[0].fd, p->items[0].buf, 
+                if (!rset_forward(p->items[0].fd, p->items[0].buf,
                                   &p->items[0].term, p->items[i].buf))
                 {
                     p->eof = 1; /* game over */
                     return 0;
                 }
                                   &p->items[0].term, p->items[i].buf))
                 {
                     p->eof = 1; /* game over */
                     return 0;
                 }
-                i = 0; /* start frowarding from scratch */
-            } else if (cmp>=rfd->rset->scope)
+                i = 0; /* start forwarding from scratch */
+            }
+            else if (cmp>=rfd->rset->scope)
             { /* [0] was ahead, forward i */
             { /* [0] was ahead, forward i */
-                if (!rset_forward(p->items[i].fd, p->items[i].buf, 
+                if (!rset_forward(p->items[i].fd, p->items[i].buf,
                                   &p->items[i].term, p->items[0].buf))
                 {
                     p->eof = 1; /* game over */
                     return 0;
                 }
                                   &p->items[i].term, p->items[0].buf))
                 {
                     p->eof = 1; /* game over */
                     return 0;
                 }
-            } else
+            }
+            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 */
                 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<ct->no_children; i++)
+        for (i = 0; i < ct->no_children; i++)
             p->tailbits[i] = 1;
         p->tailcount = ct->no_children;
             p->tailbits[i] = 1;
         p->tailcount = ct->no_children;
+        p->segment = 0;
+        p->skip = 0;
     } /* while 1 */
 }
 
 
     } /* while 1 */
 }
 
 
-static int r_forward_and(RSFD rfd, void *buf, TERMID *term, 
+static int r_forward_and(RSFD rfd, void *buf, TERMID *term,
                          const void *untilbuf)
                          const void *untilbuf)
-{ 
+{
     struct rfd_private *p = rfd->priv;
     RSET ct = rfd->rset;
     const struct rset_key_control *kctrl = ct->keycontrol;
     struct rfd_private *p = rfd->priv;
     RSET ct = rfd->rset;
     const struct rset_key_control *kctrl = ct->keycontrol;
@@ -545,7 +591,7 @@ static int r_forward_and(RSFD rfd, void *buf, TERMID *term,
         if (cmp <= -rfd->rset->scope)
         {
             killtail = 1; /* we are moving to a different hit */
         if (cmp <= -rfd->rset->scope)
         {
             killtail = 1; /* we are moving to a different hit */
-            if (!rset_forward(p->items[i].fd, p->items[i].buf, 
+            if (!rset_forward(p->items[i].fd, p->items[i].buf,
                               &p->items[i].term, untilbuf))
             {
                 p->eof = 1; /* game over */
                               &p->items[i].term, untilbuf))
             {
                 p->eof = 1; /* game over */
@@ -554,7 +600,7 @@ static int r_forward_and(RSFD rfd, void *buf, TERMID *term,
             }
         }
     }
             }
         }
     }
-    if (killtail) 
+    if (killtail)
     {
         for (i = 0; i<ct->no_children; i++)
             p->tailbits[i] = 0;
     {
         for (i = 0; i<ct->no_children; i++)
             p->tailbits[i] = 0;
@@ -563,21 +609,35 @@ static int r_forward_and(RSFD rfd, void *buf, TERMID *term,
     return r_read_and(rfd,buf,term);
 }
 
     return r_read_and(rfd,buf,term);
 }
 
-static void r_pos (RSFD rfd, double *current, double *total)
+static void r_pos_x(RSFD rfd, double *current, double *total, int and_op)
 {
     RSET ct = rfd->rset;
 {
     RSET ct = rfd->rset;
-    struct rfd_private *mrfd = 
+    struct rfd_private *mrfd =
        (struct rfd_private *)(rfd->priv);
        (struct rfd_private *)(rfd->priv);
-    double cur, tot;
-    double scur = 0.0, stot = 0.0;
+    double ratio = and_op ? 0.0 : 1.0;
     int i;
     int i;
+    double sum_cur = 0.0;
+    double sum_tot = 0.0;
     for (i = 0; i<ct->no_children; i++){
     for (i = 0; i<ct->no_children; i++){
+        double nratio, cur, tot;
         rset_pos(mrfd->items[i].fd, &cur, &tot);
         rset_pos(mrfd->items[i].fd, &cur, &tot);
-        yaz_log(log_level, "r_pos: %d %0.1f %0.1f", i, cur,tot); 
-        scur += cur;
-        stot += tot;
+        yaz_log(log_level, "r_pos: %d %0.1f %0.1f", i, cur,tot);
+        nratio = cur / tot;
+        if (and_op)
+        {
+            if (nratio > ratio)
+                ratio = nratio;
+        }
+        else
+        {
+            if (cur > 0)
+                sum_cur += (cur - 1);
+            sum_tot += tot;
+        }
     }
     }
-    if (stot < 1.0) { /* nothing there */
+    if (!and_op)
+        ratio = sum_cur / sum_tot;
+    if (ratio == 0.0 || ratio == 1.0) { /* nothing there */
         *current = 0;
         *total = 0;
         yaz_log(log_level, "r_pos: NULL  %0.1f %0.1f",  *current, *total);
         *current = 0;
         *total = 0;
         yaz_log(log_level, "r_pos: NULL  %0.1f %0.1f",  *current, *total);
@@ -585,11 +645,21 @@ static void r_pos (RSFD rfd, double *current, double *total)
     else
     {
        *current = (double) (mrfd->hits);
     else
     {
        *current = (double) (mrfd->hits);
-       *total = *current*stot/scur;
+       *total = *current / ratio;
        yaz_log(log_level, "r_pos: =  %0.1f %0.1f",  *current, *total);
     }
 }
 
        yaz_log(log_level, "r_pos: =  %0.1f %0.1f",  *current, *total);
     }
 }
 
+static void r_pos_and(RSFD rfd, double *current, double *total)
+{
+    r_pos_x(rfd, current, total, 1);
+}
+
+static void r_pos_or(RSFD rfd, double *current, double *total)
+{
+    r_pos_x(rfd, current, total, 0);
+}
+
 static int r_write (RSFD rfd, const void *buf)
 {
     yaz_log (YLOG_FATAL, "multior set type is read-only");
 static int r_write (RSFD rfd, const void *buf)
 {
     yaz_log (YLOG_FATAL, "multior set type is read-only");
@@ -602,9 +672,9 @@ static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
        rset_get_one_term(ct, terms, maxterms, curterm);
     else
     {
        rset_get_one_term(ct, terms, maxterms, curterm);
     else
     {
-       /* Special case: Some multi-ors have all terms pointing to the same 
+       /* Special case: Some multi-ors have all terms pointing to the same
           term. We do not want to duplicate those. Other multiors (and ands)
           term. We do not want to duplicate those. Other multiors (and ands)
-          have different terms under them. Those we want. 
+          have different terms under them. Those we want.
        */
        int firstterm= *curterm;
        int i;
        */
        int firstterm= *curterm;
        int i;
@@ -614,7 +684,7 @@ static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
            rset_getterms(ct->children[i], terms, maxterms, curterm);
            if ( ( *curterm > firstterm+1 ) &&
                 ( *curterm <= maxterms ) &&
            rset_getterms(ct->children[i], terms, maxterms, curterm);
            if ( ( *curterm > firstterm+1 ) &&
                 ( *curterm <= maxterms ) &&
-                ( terms[(*curterm)-1] == terms[firstterm] ) 
+                ( terms[(*curterm)-1] == terms[firstterm] )
                )
                (*curterm)--; /* forget the term, seen that before */
        }
                )
                (*curterm)--; /* forget the term, seen that before */
        }
@@ -625,6 +695,7 @@ static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
 /*
  * Local variables:
  * c-basic-offset: 4
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab