New method result set method rs_hits that returns the number of
[idzebra-moved-to-github.git] / rset / rssbool.c
index 32855d3..d34f3c4 100644 (file)
@@ -1,10 +1,28 @@
 /*
- * Copyright (C) 1994-1995, Index Data I/S 
+ * Copyright (C) 1994-1996, Index Data I/S 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: rssbool.c,v $
- * Revision 1.1  1995-12-11 09:15:27  adam
+ * Revision 1.6  1997-12-18 10:54:25  adam
+ * New method result set method rs_hits that returns the number of
+ * hits in result-set (if known). The ranked result set returns real
+ * number of hits but only when not combined with other operands.
+ *
+ * Revision 1.5  1997/09/09 13:38:16  adam
+ * Partial port to WIN95/NT.
+ *
+ * Revision 1.4  1996/10/29 13:55:27  adam
+ * Include of zebrautl.h instead of alexutil.h.
+ *
+ * Revision 1.3  1996/10/08 13:00:41  adam
+ * Bug fix: result sets with ranked operands in boolean operations weren't
+ * sorted.
+ *
+ * Revision 1.2  1996/05/15  18:35:17  adam
+ * Implemented snot operation.
+ *
+ * Revision 1.1  1995/12/11  09:15:27  adam
  * New set types: sand/sor/snot - ranked versions of and/or/not in
  * ranked/semi-ranked result sets.
  * Note: the snot not finished yet.
  *
  */
 
-#include <stdio.h>
 #include <assert.h>
+#include <stdio.h>
+#include <string.h>
 
 #include <rsbool.h>
-#include <alexutil.h>
+#include <zebrautl.h>
 
 static void *r_create_and(const struct rset_control *sel, void *parms,
                          int *flags);
@@ -30,6 +49,7 @@ static void r_close (RSFD rfd);
 static void r_delete (RSET ct);
 static void r_rewind (RSFD rfd);
 static int r_count (RSET ct);
+static int r_hits (RSET ct, void *oi);
 static int r_read (RSFD rfd, void *buf);
 static int r_write (RSFD rfd, const void *buf);
 static int r_score (RSFD rfd, int *score);
@@ -43,6 +63,7 @@ static const rset_control control_sand =
     r_delete,
     r_rewind,
     r_count,
+    r_hits,
     r_read,
     r_write,
     r_score
@@ -57,6 +78,7 @@ static const rset_control control_sor =
     r_delete,
     r_rewind,
     r_count,
+    r_hits,
     r_read,
     r_write,
     r_score
@@ -71,6 +93,7 @@ static const rset_control control_snot =
     r_delete,
     r_rewind,
     r_count,
+    r_hits,
     r_read,
     r_write,
     r_score
@@ -87,6 +110,7 @@ struct rset_bool_info {
     RSET rset_r;
     char *key_buf;
     int *score_buf;
+    int *score_idx;
     int key_no;
     int key_max;
     int (*cmp)(const void *p1, const void *p2);
@@ -98,12 +122,21 @@ struct rset_bool_rfd {
     struct rset_bool_info *info;
     int position;
     int last_pos;
-    int open_flag;
+    int flag;
 };    
 
 static void *r_create_common (const struct rset_control *sel, 
                               rset_bool_parms *bool_parms, int *flags);
-            
+
+static struct rset_bool_info *qsort_info;
+
+static int qcomp (const void *p1, const void *p2)
+{
+    int i1 = *(int*) p1;
+    int i2 = *(int*) p2;
+    return qsort_info->score_buf[i2] - qsort_info->score_buf[i1];
+}
+
 static void key_add (struct rset_bool_info *info,
                      char *buf, int score)
 {
@@ -112,6 +145,7 @@ static void key_add (struct rset_bool_info *info,
     memcpy (info->key_buf + info->key_size * info->key_no,
             buf, info->key_size);
     info->score_buf[info->key_no] = score;
+    info->score_idx[info->key_no] = info->key_no;
     (info->key_no)++;
 }
 
@@ -161,19 +195,9 @@ static void *r_create_and (const struct rset_control *sel, void *parms,
             more_r = rset_read (info->rset_r, fd_r, buf_r);
         }
         else if (cmp > 1)
-        {
-            rset_score (info->rset_r, fd_r, &score_r);
-            if (score_r != -1)
-                key_add (info, buf_r, 1);
             more_r = rset_read (info->rset_r, fd_r, buf_r);
-        }
         else
-        {
-            rset_score (info->rset_l, fd_l, &score_l);
-            if (score_l != -1)
-                key_add (info, buf_l, 1);
             more_l = rset_read (info->rset_l, fd_l, buf_l);
-        }
     }
     rset_close (info->rset_l, fd_l);
     rset_close (info->rset_r, fd_r);
@@ -181,6 +205,8 @@ static void *r_create_and (const struct rset_control *sel, void *parms,
     rset_delete (info->rset_r);
     xfree (buf_l);
     xfree (buf_r);
+    qsort_info = info;
+    qsort (info->score_idx, info->key_no, sizeof(*info->score_idx), qcomp);
     return info;
 }
 
@@ -250,6 +276,8 @@ static void *r_create_or (const struct rset_control *sel, void *parms,
     rset_delete (info->rset_r);
     xfree (buf_l);
     xfree (buf_r);
+    qsort_info = info;
+    qsort (info->score_idx, info->key_no, sizeof(*info->score_idx), qcomp);
     return info;
 }
 
@@ -257,16 +285,55 @@ static void *r_create_not (const struct rset_control *sel, void *parms,
                            int *flags)
 {
     char *buf_l, *buf_r;
+    int more_l, more_r;
+    RSFD fd_l, fd_r;
 
     struct rset_bool_info *info;
     info = r_create_common (sel, parms, flags);
 
     buf_l = xmalloc (info->key_size);
     buf_r = xmalloc (info->key_size);
+
+    fd_l = rset_open (info->rset_l, RSETF_SORT_SYSNO|RSETF_READ);
+    fd_r = rset_open (info->rset_r, RSETF_SORT_SYSNO|RSETF_READ);
+
+    more_l = rset_read(info->rset_l, fd_l, buf_l);
+    more_r = rset_read(info->rset_r, fd_r, buf_r);
+
+    while (more_l || more_r)
+    {
+        int cmp;
+        int score;
+
+        if (more_l && more_r)
+            cmp = (*info->cmp)(buf_l, buf_r);
+        else if (more_r)
+            cmp = 2;
+        else 
+            cmp = -2;
+
+        if (cmp >= -1 && cmp <= 1)
+            more_l = rset_read (info->rset_l, fd_l, buf_l);
+        else if (cmp > 1)
+        {
+            more_r = rset_read (info->rset_r, fd_r, buf_r);
+        }
+        else
+        {
+            rset_score (info->rset_l, fd_l, &score);
+            key_add (info, buf_l, score == -1 ? 1 : score);
+            more_l = rset_read (info->rset_l, fd_l, buf_l);
+        }
+    }
+    rset_close (info->rset_l, fd_l);
+    rset_close (info->rset_r, fd_r);
+
     rset_delete (info->rset_l);
     rset_delete (info->rset_r);
     xfree (buf_l);
     xfree (buf_r);
+    qsort_info = info;
+    qsort (info->score_idx, info->key_no, sizeof(*info->score_idx), qcomp);
     return info;
 }
 
@@ -293,6 +360,7 @@ static void *r_create_common (const struct rset_control *sel,
         info->key_max = 1000;
     info->key_buf = xmalloc (info->key_size * info->key_max);
     info->score_buf = xmalloc (info->key_max * sizeof(*info->score_buf));
+    info->score_idx = xmalloc (info->key_max * sizeof(*info->score_idx));
     info->key_no = 0;
 
     return info;
@@ -315,7 +383,7 @@ static RSFD r_open (RSET ct, int flag)
 
     rfd->position = 0;
     rfd->last_pos = 0;
-    rfd->open_flag = flag;
+    rfd->flag = flag;
 
     return rfd;
 }
@@ -342,6 +410,7 @@ static void r_delete (RSET ct)
 
     assert (info->rfd_list == NULL);
     xfree (info->score_buf);
+    xfree (info->score_idx);
     xfree (info->key_buf);
     xfree (info);
 }
@@ -361,6 +430,11 @@ static int r_count (RSET ct)
     return info->key_no;
 }
 
+static int r_hits (RSET ct, void *oi)
+{
+    return -1;
+}
+
 static int r_read (RSFD rfd, void *buf)
 {
     struct rset_bool_rfd *p = rfd;
@@ -368,7 +442,10 @@ static int r_read (RSFD rfd, void *buf)
 
     if (p->position >= info->key_no)
         return 0;
-    p->last_pos = (p->position)++;
+    if (p->flag & RSETF_SORT_RANK)
+        p->last_pos = info->score_idx[(p->position)++];
+    else
+        p->last_pos = (p->position)++;
     memcpy (buf, info->key_buf + info->key_size * p->last_pos,
             info->key_size);
     return 1;