Avoid full rset count for rset_count. Proper break for result set
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 17 Jan 2007 15:35:47 +0000 (15:35 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 17 Jan 2007 15:35:47 +0000 (15:35 +0000)
sort/rank.

include/idzebra/api.h
include/rset.h
index/index.h
index/zebraapi.c
index/zebrasrv.c
index/zsets.c
rset/rset.c

index 52154c4..f1c7e84 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: api.h,v 1.45 2007-01-17 13:51:36 adam Exp $
+/* $Id: api.h,v 1.46 2007-01-17 15:35:47 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -498,9 +498,9 @@ YAZ_EXPORT
 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids);
 
 YAZ_EXPORT
-ZEBRA_RES zebra_set_busy_handler(ZebraHandle zh, 
-                                 int (*f)(void *client_data),
-                                 void *client_data);
+ZEBRA_RES zebra_set_break_handler(ZebraHandle zh, 
+                                  int (*f)(void *client_data),
+                                  void *client_data);
 
 YAZ_END_CDECL                                
 
index 7427147..0c72586 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: rset.h,v 1.62 2007-01-15 20:08:24 adam Exp $
+/* $Id: rset.h,v 1.63 2007-01-17 15:35:47 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -260,6 +260,8 @@ RSET rsisams_create(NMEM nmem, struct rset_key_control *kcontrol,
 
 void rset_visit(RSET rset, int level);
 
+void rset_set_hits_limit(RSET rs, zint l);
+
 YAZ_END_CDECL
 
 #endif
index 7a66bad..8a04b9b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: index.h,v 1.193 2007-01-17 13:22:53 adam Exp $
+/* $Id: index.h,v 1.194 2007-01-17 15:35:48 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -234,8 +234,8 @@ struct zebra_session {
 
     struct zebra_limit *m_limit;
 
-    int (*busy_handler_func)(void *client_data);
-    void *busy_handler_data;
+    int (*break_handler_func)(void *client_data);
+    void *break_handler_data;
 };
 
 
index 9f907d0..5805f9f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: zebraapi.c,v 1.246 2007-01-17 13:51:36 adam Exp $
+/* $Id: zebraapi.c,v 1.247 2007-01-17 15:35:48 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -143,8 +143,8 @@ ZebraHandle zebra_open(ZebraService zs, Res res)
     zh->m_staticrank = 0;
     zh->m_segment_indexing = 0;
 
-    zh->busy_handler_func = 0;
-    zh->busy_handler_data = 0;
+    zh->break_handler_func = 0;
+    zh->break_handler_data = 0;
 
     default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
 
@@ -1024,12 +1024,12 @@ void zebra_set_partial_result(ZebraHandle zh)
 }
 
 
-ZEBRA_RES zebra_set_busy_handler(ZebraHandle zh,
-                                 int (*f)(void *client_data),
-                                 void *client_data)
+ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
+                                  int (*f)(void *client_data),
+                                  void *client_data)
 {
-    zh->busy_handler_func = f;
-    zh->busy_handler_data = client_data;
+    zh->break_handler_func = f;
+    zh->break_handler_data = client_data;
     return ZEBRA_OK;
 }
 
index 47e21cf..6ecbecf 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: zebrasrv.c,v 1.8 2007-01-17 13:51:36 adam Exp $
+/* $Id: zebrasrv.c,v 1.9 2007-01-17 15:35:48 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -262,7 +262,7 @@ static void search_terms(ZebraHandle zh, bend_search_rr *r)
 }
 
 
-static int busy_handler(void *client_data)
+static int break_handler(void *client_data)
 {
     bend_association assoc =(bend_association) client_data;    
     if (!bend_assoc_is_alive(assoc))
@@ -283,7 +283,7 @@ int bend_search(void *handle, bend_search_rr *r)
         zebra_result (zh, &r->errcode, &r->errstring);
         return 0;
     }
-    zebra_set_busy_handler(zh, busy_handler, r->association);
+    zebra_set_break_handler(zh, break_handler, r->association);
     yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
     switch (r->query->which)
     {
@@ -309,7 +309,7 @@ int bend_search(void *handle, bend_search_rr *r)
     default:
         r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
     }
-    zebra_set_busy_handler(zh, 0, 0);
+    zebra_set_break_handler(zh, 0, 0);
     return 0;
 }
 
index 74a4733..111649d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: zsets.c,v 1.118 2007-01-17 13:22:53 adam Exp $
+/* $Id: zsets.c,v 1.119 2007-01-17 15:35:48 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -936,6 +936,14 @@ ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
        kno++;
         if (this_sys != psysno)
         {
+            if ((sset->hits & 255) == 0 && zh->break_handler_func)
+            {
+                if (zh->break_handler_func(zh->break_handler_data))
+                {
+                    rset_set_hits_limit(rset, 0);
+                    break;
+                }
+            }
             (sset->hits)++;
             psysno = this_sys;
             resultSetInsertSort(zh, sset,
@@ -997,7 +1005,6 @@ ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
     terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
     rset_getterms(rset, terms, n, &numTerms);
 
-
     rank_class = zebraRankLookup(zh, rank_handler_name);
     if (!rank_class)
     {
@@ -1011,10 +1018,8 @@ ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
        struct rank_control *rc = rank_class->control;
        int score;
        zint count = 0;
-       
-       void *handle =
-           (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
-                         terms, numTerms);
+        void *handle = (*rc->begin) (zh->reg, rank_class->class_handle, rset,
+                                     nmem, terms, numTerms);
        zint psysno = 0;  /* previous doc id / sys no */
        zint pstaticrank = 0; /* previous static rank */
        int stop_flag = 0;
@@ -1028,20 +1033,16 @@ ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
                key_logdump_txt(log_level_searchhits, &key, termid->name);
            if (this_sys != psysno) 
            {   /* new record .. */
-                if (zh->busy_handler_func)
+                if (!(rfd->counted_items & 255) && zh->break_handler_func)
                 {
-                    if (zh->busy_handler_func(zh->busy_handler_data))
+                    if (zh->break_handler_func(zh->break_handler_data))
                     {
-                        yaz_log(YLOG_LOG, "Session end. Stop search");
-                        zebraSet->estimated_hit_count = 1;
-                        break;
+                        yaz_log(YLOG_LOG, "Aborted search");
+                        stop_flag = 1;
                     }
                 }
                if (rfd->counted_items > rset->hits_limit)
-                {
-                    zebraSet->estimated_hit_count = 1;
-                   break;
-                }
+                    stop_flag = 1;
                if (psysno)
                {   /* only if we did have a previous record */
                    score = (*rc->calc) (handle, psysno, pstaticrank,
@@ -1049,9 +1050,13 @@ ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
                    /* insert the hit. A=Ascending */
                    resultSetInsertRank (zh, sort_info, psysno, score, 'A');
                    count++;
-                   if (stop_flag)
-                       break;
                }
+                if (stop_flag)
+                {
+                    zebraSet->estimated_hit_count = 1;
+                    rset_set_hits_limit(rset, 0);
+                    break;
+                }
                psysno = this_sys;
                if (zh->m_staticrank)
                    pstaticrank = key.mem[0];
index e6cb176..52377bb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: rset.c,v 1.57 2007-01-15 15:10:19 adam Exp $
+/* $Id: rset.c,v 1.58 2007-01-17 15:35:48 adam Exp $
    Copyright (C) 1995-2007
    Index Data ApS
 
@@ -65,19 +65,43 @@ RSFD rfd_create_base(RSET rs)
     return rnew;
 }
 
+static void rset_close_int(RSET rs, RSFD rfd)
+{
+    RSFD *pfd;
+    (*rs->control->f_close)(rfd);
+    
+    yaz_log(log_level, "rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
+            rfd, rs, rfd->priv, rs->free_list); 
+    for (pfd = &rs->use_list; *pfd; pfd = &(*pfd)->next)
+       if (*pfd == rfd)
+       {
+           *pfd = (*pfd)->next;
+           rfd->next = rs->free_list;
+           rs->free_list = rfd;
+           return;
+       }
+    yaz_log(YLOG_WARN, "rset_close handle not found. type=%s",
+           rs->control->desc);
+}
+
+void rset_set_hits_limit(RSET rs, zint l)
+{
+    rs->hits_limit = l;
+}
+
 /**
    \brief Closes a result set RFD handle
    \param rfd the RFD handle.
 */
 void rset_close(RSFD rfd)
 {
-    RSFD *pfd;
     RSET rs = rfd->rset;
 
     if (rs->hits_count == 0)
     {
        TERMID termid;
        char buf[100];
+
        while(rfd->counted_items <= rs->hits_limit
              && rset_default_read(rfd, buf, &termid))
            ;
@@ -116,20 +140,7 @@ void rset_close(RSFD rfd)
        yaz_log(log_level, "rset_close p=%p count=" ZINT_FORMAT, rs,
                rs->hits_count);
     }
-    (*rs->control->f_close)(rfd);
-    
-    yaz_log(log_level, "rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
-            rfd, rs, rfd->priv, rs->free_list); 
-    for (pfd = &rs->use_list; *pfd; pfd = &(*pfd)->next)
-       if (*pfd == rfd)
-       {
-           *pfd = (*pfd)->next;
-           rfd->next = rs->free_list;
-           rs->free_list = rfd;
-           return;
-       }
-    yaz_log(YLOG_WARN, "rset_close handle not found. type=%s",
-           rs->control->desc);
+    rset_close_int(rs, rfd);
 }
 
 /**
@@ -261,7 +272,7 @@ zint rset_count(RSET rs)
     double cur, tot;
     RSFD rfd = rset_open(rs, 0);
     rset_pos(rfd, &cur, &tot);
-    rset_close(rfd);
+    rset_close_int(rs, rfd);
     return (zint) tot;
 }