Small IOCHAN refactor. Introduce iochan_man_t
[pazpar2-moved-to-github.git] / src / reclists.c
index 70b091f..a4b1ac0 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) 2006-2010 Index Data
 
 Pazpar2 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
@@ -32,8 +32,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 struct reclist
 {
     struct reclist_bucket **hashtable;
-    int hashtable_size;
-    int hashmask;
+    unsigned hash_size;
 
     int num_records;
     struct reclist_bucket *sorted_list;
@@ -130,43 +129,44 @@ static int reclist_cmp(const void *p1, const void *p2)
     {
         union data_types *ut1 = r1->sortkeys[s->offset];
         union data_types *ut2 = r2->sortkeys[s->offset];
+        const char *s1, *s2;
         switch (s->type)
         {
-            const char *s1, *s2;
-            
-            case Metadata_sortkey_relevance:
-                res = r2->relevance - r1->relevance;
-                break;
-            case Metadata_sortkey_string:
-                s1 = ut1 ? ut1->text.sort : "";
-                s2 = ut2 ? ut2->text.sort : "";
-                res = strcmp(s2, s1);
-                if (res)
-                {
-                    if (s->increasing)
-                        res *= -1;
-                }
-                break;
-            case Metadata_sortkey_numeric:
-                if (ut1 && ut2)
-                {
-                    if (s->increasing)
-                        res = ut1->number.min  - ut2->number.min;
-                    else
-                        res = ut2->number.max  - ut1->number.max;
-                }
-                else if (ut1 && !ut2)
-                    res = -1;
-                else if (!ut1 && ut2)
-                    res = 1;
+        case Metadata_sortkey_relevance:
+            res = r2->relevance - r1->relevance;
+            break;
+        case Metadata_sortkey_string:
+            s1 = ut1 ? ut1->text.sort : "";
+            s2 = ut2 ? ut2->text.sort : "";
+            res = strcmp(s2, s1);
+            if (res)
+            {
+                if (s->increasing)
+                    res *= -1;
+            }
+            break;
+        case Metadata_sortkey_numeric:
+            if (ut1 && ut2)
+            {
+                if (s->increasing)
+                    res = ut1->number.min  - ut2->number.min;
                 else
-                    res = 0;
-                break;
-            default:
-                yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
+                    res = ut2->number.max  - ut1->number.max;
+            }
+            else if (ut1 && !ut2)
+                res = -1;
+            else if (!ut1 && ut2)
+                res = 1;
+            else
                 res = 0;
+            break;
+        default:
+            yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
+            res = 0;
         }
     }
+    if (res == 0)
+        res = strcmp(r1->recid, r2->recid);
     return res;
 }
 
@@ -217,21 +217,14 @@ void reclist_rewind(struct reclist *l)
         l->sorted_ptr = l->sorted_list;
 }
 
-struct reclist *reclist_create(NMEM nmem, int numrecs)
+struct reclist *reclist_create(NMEM nmem)
 {
-    int hashsize = 1;
-    struct reclist *res;
-
-    assert(numrecs);
-    while (hashsize < numrecs)
-        hashsize <<= 1;
-    res = nmem_malloc(nmem, sizeof(struct reclist));
+    struct reclist *res = nmem_malloc(nmem, sizeof(struct reclist));
+    res->hash_size = 399;
     res->hashtable 
-        = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
-    memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
-    res->hashtable_size = hashsize;
+        = nmem_malloc(nmem, res->hash_size * sizeof(struct reclist_bucket*));
+    memset(res->hashtable, 0, res->hash_size * sizeof(struct reclist_bucket*));
     res->nmem = nmem;
-    res->hashmask = hashsize - 1; // Creates a bitmask
 
     res->sorted_ptr = 0;
     res->sorted_list = 0;
@@ -264,7 +257,7 @@ struct record_cluster *reclist_insert( struct reclist *l,
     assert(merge_key);
     assert(total);
 
-    bucket = jenkins_hash((unsigned char*) merge_key) & l->hashmask;
+    bucket = jenkins_hash((unsigned char*) merge_key) % l->hash_size;
 
     for (p = &l->hashtable[bucket]; *p; p = &(*p)->hnext)
     {