Remove obsolete references
[idzebra-moved-to-github.git] / index / zsets.c
index e2d3da0..a168730 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the Zebra server.
-   Copyright (C) 1995-2008 Index Data
+   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
@@ -18,6 +18,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <stdio.h>
 #include <assert.h>
 #ifdef WIN32
@@ -113,6 +116,7 @@ static ZEBRA_RES resultSetSearch(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
     rpn_get_top_approx_limit(zh, rpn->RPNStructure, &sset->approx_limit);
 
     res = rpn_search_top(zh, rpn->RPNStructure, rpn->attributeSetId,
+                         sset->approx_limit,
                         nmem, rset_nmem,
                         sort_sequence,
                         sset->num_bases, sset->basenames,
@@ -125,7 +129,8 @@ static ZEBRA_RES resultSetSearch(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
     for (i = 0; sort_sequence->specs[i]; i++)
         ;
     sort_sequence->num_specs = i;
-    rset->hits_limit = sset->approx_limit;
+    rset_set_hits_limit(rset, sset->approx_limit);
+
     if (!i)
     {
         res = resultSetRank(zh, sset, rset, rset_nmem);
@@ -554,15 +559,15 @@ struct sortKeyInfo {
 };
 
 void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
+                         int database_no,
                          struct sortKeyInfo *criteria, int num_criteria,
                          zint sysno,
-                         char *cmp_buf[], char *tmp_cmp_buf[], int *cached_success_db)
+                         char *cmp_buf[], char *tmp_cmp_buf[])
 {
     struct zset_sort_entry *new_entry = NULL;
     struct zset_sort_info *sort_info = sset->sort_info;
     int i, j;
-    int scan_db,scan_count;
-    int numbases = zh->num_basenames;
+    WRBUF w = wrbuf_alloc();
 
     zebra_sort_sysno(zh->reg->sort_index, sysno);
     for (i = 0; i<num_criteria; i++)
@@ -570,52 +575,45 @@ void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
         char *this_entry_buf = tmp_cmp_buf[i];
         memset(this_entry_buf, '\0', SORT_IDX_ENTRYSIZE);
         
-        /* if the first database doesn't have a sort index, 
-           we assume none of them will */
-        if (criteria[i].ord[0] != -1)
+        if (criteria[i].ord[database_no] != -1)
         {
-            /* now make a best guess for the database in which we think
-               the record is located if its not in our best guess, try the
-               other databases one by one, till we had them all */
-            scan_db = *cached_success_db;
-            scan_count = 0;
-            
-            while (1)
+            yaz_log(log_level_sort, "pre zebra_sort_type ord is %d",
+                    criteria[i].ord[database_no]);
+            zebra_sort_type(zh->reg->sort_index, criteria[i].ord[database_no]);
+            wrbuf_rewind(w);
+            if (zebra_sort_read(zh->reg->sort_index, 0, w))
             {
-                scan_count++;
-                if (scan_count>numbases)
-                {
-                    /* well...we scanned all databases and still nothing...give up */
-                    yaz_log(log_level_sort, "zebra_sort_read failed (record not found in indices)");
-                    break;
-                }
-                
-                /* the criteria[i].ord is the file id of the sort index */
-                yaz_log(log_level_sort, "pre zebra_sort_type ord is %d", criteria[i].ord[scan_db]);
-                zebra_sort_type(zh->reg->sort_index, criteria[i].ord[scan_db]);
-                if (zebra_sort_read(zh->reg->sort_index, this_entry_buf))
-                {
-                    /* allright, found it */
-                    /* cache this db so we start trying from this db 
-                       for next record */
-                    *cached_success_db=scan_db;
-                    break;
-                }
-                else
+                /* consider each sort entry and take lowest/highest one
+                   of the one as sorting key depending on whether sort is
+                   ascending/descending */
+                int off = 0;
+                while (off != wrbuf_len(w))
                 {
-                    yaz_log(log_level_sort, "record not found in database, trying next one");
-                    scan_db++;
-                    if (scan_db>=numbases)
-                        scan_db=0;
+                    size_t l = strlen(wrbuf_buf(w)+off);
+                    assert(off < wrbuf_len(w));
+
+                    if (l >= SORT_IDX_ENTRYSIZE)
+                        l = SORT_IDX_ENTRYSIZE-1;
+                    if ( (off == 0)
+                         || (criteria[i].relation == 'A'
+                             && strcmp(wrbuf_buf(w)+off, this_entry_buf) < 0)
+                         || (criteria[i].relation == 'D'
+                             && strcmp(wrbuf_buf(w)+off, this_entry_buf) > 0)
+                        )
+                    {
+                        memcpy(this_entry_buf, wrbuf_buf(w)+off, l);
+                        this_entry_buf[l] = '\0';
+                    }
+                    off += 1 + strlen(wrbuf_buf(w)+off);
                 }
             }
-            
         }
         else
         {
             yaz_log(log_level_sort, "criteria[i].ord is -1 so not reading from sort index");
         }
     }
+    wrbuf_destroy(w);
     i = sort_info->num_entries;
     while (--i >= 0)
     {
@@ -625,17 +623,11 @@ void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
             char *this_entry_buf = tmp_cmp_buf[j];
             char *other_entry_buf = 
                 cmp_buf[j] + i * SORT_IDX_ENTRYSIZE;
-            if (criteria[j].numerical[*cached_success_db])
+            if (criteria[j].numerical[database_no])
             {
                 char this_entry_org[1024];
                 char other_entry_org[1024];
                 double diff;
-                /* when searching multiple databases, we use the index
-                   type of the first one. So if they differ between
-                   databases, we have a problem here we could store the
-                   index_type for each database, but if we didn't find the
-                   record in any sort index, then we still don't know to
-                   which database it belongs. */
                 const char *index_type = criteria[j].index_type;
                 zebra_term_untrans(zh, index_type, this_entry_org,
                                    this_entry_buf);
@@ -880,7 +872,6 @@ ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
 {
     int i;
     int ib;
-    int cached_success_db = 0;
     int n = 0;
     zint kno = 0;
     zint psysno = 0;
@@ -917,7 +908,6 @@ ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
     {
         Z_SortKeySpec *sks = sort_sequence->specs[i];
         Z_SortKey *sk;
-        ZEBRA_RES res;
         
         sort_criteria[i].ord = (int *)
             nmem_malloc(nmem, sizeof(int)*numbases);
@@ -989,13 +979,12 @@ ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
             for (ib = 0; ib < numbases; ib++)
             {
                 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[ib]);
-                res = zebra_sort_get_ord(zh, sk->u.sortAttributes,
-                                         &sort_criteria[i].ord[ib],
-                                         &sort_criteria[i].numerical[ib]);
+                if (zebra_sort_get_ord(zh, sk->u.sortAttributes,
+                                       &sort_criteria[i].ord[ib],
+                                       &sort_criteria[i].numerical[ib]) != 
+                    ZEBRA_OK && sks->which != Z_SortKeySpec_null)
+                    return ZEBRA_FAIL;
             }
-            
-            if (sks->which != Z_SortKeySpec_null && res != ZEBRA_OK)
-                return ZEBRA_FAIL;
             break;
         }
         /* right now we look up the index type based on the first database
@@ -1030,6 +1019,7 @@ ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
        kno++;
         if (this_sys != psysno)
         {
+            int database_no = 0;
             if ((sset->hits & 255) == 0 && zh->break_handler_func)
             {
                 if (zh->break_handler_func(zh->break_handler_data))
@@ -1040,9 +1030,28 @@ ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
             }
             (sset->hits)++;
             psysno = this_sys;
-            resultSetInsertSort(zh, sset,
+
+            /* determine database from the term, but only bother if more than
+               one database is in use*/
+            if (numbases > 1 && termid->ol)
+            {
+                const char *this_db = 0;
+                if (zebraExplain_lookup_ord(zh->reg->zei, termid->ol->ord,  0, &this_db, 0)
+                    == 0 && this_db)
+                {
+                    for (ib = 0; ib < numbases; ib++)
+                        if (!strcmp(this_db, zh->basenames[ib]))
+                            database_no = ib;
+                }
+            }
+#if 0
+            yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " database_no=%d", this_sys,
+                database_no);
+            ord_list_print(termid->ol);
+#endif
+            resultSetInsertSort(zh, sset, database_no,
                                 sort_criteria, num_criteria, psysno, cmp_buf,
-                                tmp_cmp_buf, &cached_success_db);
+                                tmp_cmp_buf);
         }
     }
     rset_close(rfd);
@@ -1138,6 +1147,11 @@ ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
                 }
                if (rfd->counted_items > rset->hits_limit)
                     stop_flag = 1;
+                if (stop_flag)
+                {
+                    zebraSet->estimated_hit_count = 1;
+                    break;
+                }
                if (psysno)
                {   /* only if we did have a previous record */
                    score = (*rc->calc)(handle, psysno, pstaticrank,
@@ -1146,12 +1160,6 @@ ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
                    resultSetInsertRank(zh, sort_info, psysno, score, 'A');
                    count++;
                }
-                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];
@@ -1517,6 +1525,7 @@ void zebra_count_set(ZebraHandle zh, RSET rset, zint *count,
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab