Updated footer comment
[idzebra-moved-to-github.git] / index / zsets.c
index 10ecb8c..bb548dc 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the Zebra server.
-   Copyright (C) 1995-2008 Index Data
+   Copyright (C) 1994-2009 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
@@ -562,6 +562,7 @@ void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
     struct zset_sort_entry *new_entry = NULL;
     struct zset_sort_info *sort_info = sset->sort_info;
     int i, j;
+    WRBUF w = wrbuf_alloc();
 
     zebra_sort_sysno(zh->reg->sort_index, sysno);
     for (i = 0; i<num_criteria; i++)
@@ -574,13 +575,40 @@ void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
             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]);
-            zebra_sort_read(zh->reg->sort_index, this_entry_buf);
+            wrbuf_rewind(w);
+            if (zebra_sort_read(zh->reg->sort_index, 0, w))
+            {
+                /* 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))
+                {
+                    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)
     {
@@ -595,12 +623,6 @@ void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
                 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);
@@ -1501,6 +1523,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