Switching to Libtool shared libraries for Zebra.
[idzebra-moved-to-github.git] / rset / rsbetween.c
index f631f33..28fa700 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: rsbetween.c,v 1.12 2004-02-12 15:15:54 heikki Exp $
+/* $Id: rsbetween.c,v 1.19 2004-08-23 12:38:53 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
    Index Data Aps
 
@@ -35,20 +35,22 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <string.h>
 #include <assert.h>
 
-#include <rsbetween.h>
 #include <zebrautl.h>
+#include <rsbetween.h>
+
+#define RSBETWEEN_DEBUG 0 
 
 static void *r_create_between(RSET ct, const struct rset_control *sel, void *parms);
 static RSFD r_open_between (RSET ct, int flag);
 static void r_close_between (RSFD rfd);
 static void r_delete_between (RSET ct);
 static void r_rewind_between (RSFD rfd);
-static int r_forward_between(RSET ct, RSFD rfd, void *buf, int *term_index,
+static int r_forward_between(RSET ct, RSFD rfd, void *buf, 
                      int (*cmpfunc)(const void *p1, const void *p2),
                      const void *untilbuf);
-static int r_count_between (RSET ct);
-static int r_read_between (RSFD rfd, void *buf, int *term_index);
+static int r_read_between (RSFD rfd, void *buf);
 static int r_write_between (RSFD rfd, const void *buf);
+static void r_pos_between (RSFD rfd, double *current, double *total);
 
 static const struct rset_control control_between = 
 {
@@ -59,7 +61,7 @@ static const struct rset_control control_between =
     r_delete_between,
     r_rewind_between,
     r_forward_between, /* rset_default_forward, */
-    r_count_between,
+    r_pos_between,
     r_read_between,
     r_write_between,
 };
@@ -73,7 +75,6 @@ struct rset_between_info {
     RSET rset_m;
     RSET rset_r;
     RSET rset_attr;
-    int term_index_s;
     int (*cmp)(const void *p1, const void *p2);
     char *(*printer)(const void *p1, char *buf);
     struct rset_between_rfd *rfd_list;
@@ -88,9 +89,6 @@ struct rset_between_rfd {
     int  more_m;
     int  more_r;
     int  more_attr;
-    int term_index_l;
-    int term_index_m;
-    int term_index_r;
     void *buf_l;
     void *buf_m;
     void *buf_r;
@@ -98,8 +96,10 @@ struct rset_between_rfd {
     int level;
     struct rset_between_rfd *next;
     struct rset_between_info *info;
+    zint hits;
 };    
 
+#if RSBETWEEN_DEBUG
 static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
 {
     char buf_l[32];
@@ -112,6 +112,7 @@ static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
       (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r,
       p->level);
 }
+#endif
 
 static void *r_create_between (RSET ct, const struct rset_control *sel,
                                void *parms)
@@ -125,47 +126,9 @@ static void *r_create_between (RSET ct, const struct rset_control *sel,
     info->rset_m = between_parms->rset_m;
     info->rset_r = between_parms->rset_r;
     info->rset_attr = between_parms->rset_attr;
-    if (rset_is_volatile(info->rset_l) || 
-        rset_is_volatile(info->rset_m) ||
-        rset_is_volatile(info->rset_r))
-        ct->flags |= RSET_FLAG_VOLATILE;
     info->cmp = between_parms->cmp;
     info->printer = between_parms->printer;
     info->rfd_list = NULL;
-    
-    info->term_index_s = info->rset_l->no_rset_terms;
-    if (info->rset_m)
-    {
-        ct->no_rset_terms =
-            info->rset_l->no_rset_terms + 
-            info->rset_m->no_rset_terms + 
-            info->rset_r->no_rset_terms;
-        ct->rset_terms = (RSET_TERM *)
-            xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
-        memcpy (ct->rset_terms, info->rset_l->rset_terms,
-                info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
-        memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
-                info->rset_m->rset_terms,
-                info->rset_m->no_rset_terms * sizeof(*ct->rset_terms));
-        memcpy (ct->rset_terms + info->rset_l->no_rset_terms + 
-                info->rset_m->no_rset_terms,
-                info->rset_r->rset_terms,
-                info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
-    }
-    else
-    {
-        ct->no_rset_terms =
-            info->rset_l->no_rset_terms + 
-            info->rset_r->no_rset_terms;
-        ct->rset_terms = (RSET_TERM *)
-            xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
-        memcpy (ct->rset_terms, info->rset_l->rset_terms,
-                info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
-        memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
-                info->rset_r->rset_terms,
-                info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
-    }
-
     return info;
 }
 
@@ -193,20 +156,17 @@ static RSFD r_open_between (RSET ct, int flag)
     rfd->rfd_m = rset_open (info->rset_m, RSETF_READ);
     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
     
-    rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
-                             &rfd->term_index_l);
-    rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m,
-                             &rfd->term_index_m);
-    rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
-                             &rfd->term_index_r);
+    rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
+    rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m);
+    rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
     if (info->rset_attr)
     {
-        int dummy;
         rfd->rfd_attr = rset_open (info->rset_attr, RSETF_READ);
         rfd->more_attr = rset_read (info->rset_attr, rfd->rfd_attr,
-                                    rfd->buf_attr, &dummy);
+                                    rfd->buf_attr);
     }
     rfd->level=0;
+    rfd->hits=0;
     return rfd;
 }
 
@@ -241,7 +201,6 @@ static void r_delete_between (RSET ct)
     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
 
     assert (info->rfd_list == NULL);
-    xfree (ct->rset_terms);
     rset_delete (info->rset_l);
     rset_delete (info->rset_m);
     rset_delete (info->rset_r);
@@ -255,52 +214,55 @@ static void r_rewind_between (RSFD rfd)
     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
 
+#if RSBETWEEN_DEBUG
     logf (LOG_DEBUG, "rsbetween_rewind");
+#endif
     rset_rewind (info->rset_l, p->rfd_l);
     rset_rewind (info->rset_m, p->rfd_m);
     rset_rewind (info->rset_r, p->rfd_r);
-    p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
-    p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, &p->term_index_m);
-    p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
+    p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
+    p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
+    p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
     if (info->rset_attr)
     {
-        int dummy;
         rset_rewind (info->rset_attr, p->rfd_attr);
-        p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr,
-                                  &dummy);
+        p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr);
     }
     p->level=0;
+    p->hits=0;
 }
 
 
 
-static int r_forward_between(RSET ct, RSFD rfd, void *buf, int *term_index,
+static int r_forward_between(RSET ct, RSFD rfd, void *buf,
                      int (*cmpfunc)(const void *p1, const void *p2),
                      const void *untilbuf)
 {
     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
     int rc;
+#if RSBETWEEN_DEBUG
     log2( p, "fwd: before forward", 0,0);
+#endif
     /* It is enough to forward the m pointer here, the read will */
     /* naturally forward the l, m, and attr pointers */
     if (p->more_m)
         p->more_m=rset_forward(info->rset_m,p->rfd_m, p->buf_m,
-                        &p->term_index_m, info->cmp,untilbuf);
+                        info->cmp,untilbuf);
+#if RSBETWEEN_DEBUG
     log2( p, "fwd: after forward M", 0,0);
-    rc = r_read_between(rfd, buf, term_index);
+#endif
+    rc = r_read_between(rfd, buf);
+#if RSBETWEEN_DEBUG
     log2( p, "fwd: after forward", 0,0);
+#endif
     return rc;
 }
 
-static int r_count_between (RSET ct)
-{
-    return 0;
-}
 
 
 
-static int r_read_between (RSFD rfd, void *buf, int *term_index)
+static int r_read_between (RSFD rfd, void *buf)
 {
     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
     struct rset_between_info *info = p->info;
@@ -310,7 +272,9 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
 
     while (p->more_m)
     {
+#if RSBETWEEN_DEBUG
         log2( p, "start of loop", cmp_l, cmp_r);
+#endif
 
         /* forward L until past m, count levels, note rec boundaries */
         if (p->more_l)
@@ -320,7 +284,9 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
             p->level = 0;
             cmp_l=2; /* past this record */
         }
+#if RSBETWEEN_DEBUG
         log2( p, "after first L", cmp_l, cmp_r);
+#endif
 
         while (cmp_l < 0)   /* l before m */
         {
@@ -335,7 +301,6 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
                 else
                 {
                     int cmp_attr;
-                    int dummy_term;
                     attr_match = 0;
                     while (p->more_attr)
                     {
@@ -348,8 +313,8 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
                         else if (cmp_attr > 0)
                             break;
                         else if (cmp_attr==-1) 
-                            p->more_attr = rset_read (info->rset_attr, p->rfd_attr,
-                                                  p->buf_attr, &dummy_term);
+                            p->more_attr = rset_read (info->rset_attr, 
+                                                  p->rfd_attr, p->buf_attr);
                             /* if we had a forward that went all the way to
                              * the seqno, we could use that. But fwd only goes
                              * to the sysno */
@@ -357,9 +322,10 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
                         {
                             p->more_attr = rset_forward(
                                       info->rset_attr, p->rfd_attr,
-                                      p->buf_attr, &dummy_term,
-                                      info->cmp, p->buf_l);
+                                      p->buf_attr, info->cmp, p->buf_l);
+#if RSBETWEEN_DEBUG
                             logf(LOG_DEBUG, "btw: after frowarding attr m=%d",p->more_attr);
+#endif
                         }
                     } /* while more_attr */
                 }
@@ -370,24 +336,22 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
             {
                 if (p->more_l) 
                 {
-                    p->more_l=rset_forward(
-                                      info->rset_l, p->rfd_l,
-                                      p->buf_l, &p->term_index_l,
-                                      info->cmp, p->buf_m);
+                    p->more_l=rset_forward( info->rset_l, p->rfd_l,
+                                      p->buf_l, info->cmp, p->buf_m);
                     if (p->more_l)
                         cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
                     else
                         cmp_l=2;
+#if RSBETWEEN_DEBUG
                     log2( p, "after forwarding L", cmp_l, cmp_r);
+#endif
                 }
             } else
             {
-                p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
-                              &p->term_index_l);
+                p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
             }
 #else
-            p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
-                              &p->term_index_l);
+            p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
 #endif
             if (p->more_l)
             {
@@ -395,17 +359,23 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
             }
             else
                 cmp_l=2; 
+#if RSBETWEEN_DEBUG
             log2( p, "end of L loop", cmp_l, cmp_r);
+#endif
         } /* forward L */
 
             
         /* forward R until past m, count levels */
+#if RSBETWEEN_DEBUG
         log2( p, "Before moving R", cmp_l, cmp_r);
+#endif
         if (p->more_r)
             cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
         else
             cmp_r=2; 
+#if RSBETWEEN_DEBUG
         log2( p, "after first R", cmp_l, cmp_r);
+#endif
         while (cmp_r < 0)   /* r before m */
         {
              /* -2, earlier record, don't count level */
@@ -416,27 +386,25 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
 #if NEWCODE                
                 if (cmp_r==-2)
                 {
-                    p->more_r=rset_forward(
-                                      info->rset_r, p->rfd_r,
-                                      p->buf_r, &p->term_index_r,
-                                      info->cmp, p->buf_m);
+                    p->more_r=rset_forward( info->rset_r, p->rfd_r,
+                                      p->buf_r, info->cmp, p->buf_m);
                 } else
                 {
-                    p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
-                                       &p->term_index_r);
+                    p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
                 }
                 if (p->more_r)
                     cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
 
 #else
-                p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
-                                       &p->term_index_r);
+                p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
                 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
 #endif
             }
             else
                 cmp_r=2; 
+#if RSBETWEEN_DEBUG
         log2( p, "End of R loop", cmp_l, cmp_r);
+#endif
         } /* forward R */
         
         if ( ( p->level <= 0 ) && ! p->more_l)
@@ -445,42 +413,45 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index)
         if ( attr_match && p->level > 0)  /* within a tag pair (or deeper) */
         {
             memcpy (buf, p->buf_m, info->key_size);
-            *term_index = p->term_index_m;
+#if RSBETWEEN_DEBUG
             log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r);
-            p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
-                                   &p->term_index_m);
+#endif
+            p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
             if (cmp_l == 2)
                 p->level = 0;
+            p->hits++;
             return 1;
         }
         else if ( ! p->more_l )  /* not in data, no more starts */
         {
+#if RSBETWEEN_DEBUG
             log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r);
+#endif
             return 0;  /* ergo, nothing can be found. stop scanning */
         }
 #if NEWCODE                
         if (cmp_l == 2)
         {
             p->level = 0;
-            p->more_m=rset_forward(
-                              info->rset_m, p->rfd_m,
-                              p->buf_m, &p->term_index_m,
-                              info->cmp, p->buf_l);
+            p->more_m=rset_forward( info->rset_m, p->rfd_m,
+                              p->buf_m, info->cmp, p->buf_l);
         } else
         {
-            p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
-                               &p->term_index_m);
+            p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
         }
 #else
         if (cmp_l == 2)
             p->level = 0;
-        p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
-                               &p->term_index_m);
+        p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
 #endif
+#if RSBETWEEN_DEBUG
         log2( p, "End of M loop", cmp_l, cmp_r);
+#endif
     } /* while more_m */
     
+#if RSBETWEEN_DEBUG
     log2( p, "Exiting, nothing more in m", cmp_l, cmp_r);
+#endif
     return 0;  /* no more data possible */
 
 
@@ -493,3 +464,37 @@ static int r_write_between (RSFD rfd, const void *buf)
     return -1;
 }
 
+
+static void r_pos_between (RSFD rfd, double *current, double *total)
+{
+    struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
+    struct rset_between_info *info = p->info;
+    double lcur,ltot;
+    double mcur,mtot;
+    double rcur,rtot;
+    double r;
+    ltot=-1; rtot=-1;
+    rset_pos(info->rset_l, p->rfd_l,  &lcur, &ltot);
+    rset_pos(info->rset_m, p->rfd_m,  &mcur, &mtot);
+    rset_pos(info->rset_r, p->rfd_r,  &rcur, &rtot);
+    if ( (ltot<0) && (mtot<0) && (rtot<0) ) { /*no position */
+        *current=mcur;  /* return same as you got */
+        *total=mtot;    /* probably -1 for not available */
+    }
+    if ( ltot<0) { ltot=0; lcur=0;} /* if only one useful, use it */
+    if ( mtot<0) { mtot=0; mcur=0;}
+    if ( rtot<0) { rtot=0; rcur=0;}
+    if ( ltot+mtot+rtot < 1 ) { /* empty rset */
+        *current=0;
+        *total=0;
+        return;
+    }
+    r=1.0*(lcur+mcur+rcur)/(ltot+mtot+rtot); /* weighed average of l and r */
+    *current=p->hits;
+    *total=*current/r ; 
+#if RSBETWEEN_DEBUG
+    yaz_log(LOG_DEBUG,"betw_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
+                    info->rset_l->control->desc, info->rset_r->control->desc,
+                    *current, *total, r);
+#endif
+}