Happy new year
[pazpar2-moved-to-github.git] / src / reclists.c
index 5822dbe..e96ee92 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2013 Index Data
+   Copyright (C) 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
@@ -84,7 +84,7 @@ struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
         int increasing = 0;
         int i;
         int offset = 0;
-        enum conf_sortkey_type type = Metadata_sortkey_string;
+        enum conf_metadata_type type = Metadata_type_generic;
         struct reclist_sortparms *new;
 
         if (!(cpp = strchr(parms, ',')))
@@ -107,23 +107,27 @@ struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
             if (pp[2])
             {
                 if (pp[2] == 'p')
-                    type = Metadata_sortkey_position;
+                    type = Metadata_type_position;
                 else
                     yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
             }
             *pp = '\0';
         }
-        if (type != Metadata_sortkey_position)
+        if (type != Metadata_type_position)
         {
             if (!strcmp(parm, "relevance"))
             {
-                type = Metadata_sortkey_relevance;
+                type = Metadata_type_relevance;
             }
             else if (!strcmp(parm, "position"))
             {
-                type = Metadata_sortkey_position;
+                type = Metadata_type_position;
             }
-            else
+            else if (!strcmp(parm, "retrieval"))
+            {
+                type = Metadata_type_retrieval;
+            }
+            else if (service)
             {
                 for (i = 0; i < service->num_sortkeys; i++)
                 {
@@ -131,8 +135,6 @@ struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
                     if (!strcmp(sk->name, parm))
                     {
                         type = sk->type;
-                        if (type == Metadata_sortkey_skiparticle)
-                            type = Metadata_sortkey_string;
                         break;
                     }
                 }
@@ -144,6 +146,12 @@ struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
                 }
                 offset = i;
             }
+            else
+            {
+                yaz_log(YLOG_FATAL, "Sortkey not defined in service: %s",
+                        parm);
+                return 0;
+            }
         }
         new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
         new->next = 0;
@@ -173,35 +181,38 @@ static int reclist_cmp(const void *p1, const void *p2)
         const char *s1, *s2;
         switch (s->type)
         {
-        case Metadata_sortkey_relevance:
-            res = r2->relevance_score - r1->relevance_score;
+        case Metadata_type_relevance:
+            res = r1->relevance_score - r2->relevance_score;
             break;
-        case Metadata_sortkey_string:
+        case Metadata_type_generic:
+        case Metadata_type_skiparticle:
             s1 = ut1 ? ut1->text.sort : "";
             s2 = ut2 ? ut2->text.sort : "";
-            res = strcmp(s2, s1);
-            if (res)
-            {
-                if (s->increasing)
-                    res *= -1;
-            }
+            res = strcmp(s1, s2);
             break;
-        case Metadata_sortkey_numeric:
+        case Metadata_type_year:
+        case Metadata_type_date:
             if (ut1 && ut2)
             {
                 if (s->increasing)
                     res = ut1->number.min  - ut2->number.min;
                 else
-                    res = ut2->number.max  - ut1->number.max;
+                    res = ut1->number.max  - ut2->number.max;
             }
             else if (ut1 && !ut2)
-                res = -1;
+            {
+                res = -1; /* without date/year: last! */
+                continue;
+            }
             else if (!ut1 && ut2)
-                res = 1;
+            {
+                res = 1; /* without date/year: last! */
+                continue;
+            }
             else
                 res = 0;
             break;
-        case Metadata_sortkey_position:
+        case Metadata_type_position:
             if (r1->records && r2->records)
             {
                 int pos1 = 0, pos2 = 0;
@@ -213,13 +224,32 @@ static int reclist_cmp(const void *p1, const void *p2)
                     if (pos2 == 0 || rec->position < pos2)
                         pos2 = rec->position;
                 res = pos1 - pos2;
+                continue;
+            }
+            break;
+        case Metadata_type_float:
+            if (ut1 && ut2)
+            {
+                if (ut1->fnumber == ut2->fnumber)
+                    res = 0;
+                else if (ut1->fnumber > ut2->fnumber)
+                    res = 1;
+                else
+                    res = -1;
             }
+            else if (ut1)
+                res = 1;
+            else if (ut2)
+                res = -1;
+            else
+                res = 0;
             break;
-        default:
-            yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
-            res = 0;
+        case Metadata_type_retrieval:
+            res = r1->retrieval_position - r2->retrieval_position;
             break;
         }
+        if (res && !s->increasing)
+            res *= -1;
     }
     if (res == 0)
         res = strcmp(r1->recid, r2->recid);
@@ -313,16 +343,19 @@ struct record_cluster *reclist_read_record(struct reclist *l)
 void reclist_enter(struct reclist *l)
 {
     yaz_mutex_enter(l->mutex);
-    if (l)
-        l->sorted_ptr = l->sorted_list;
+    l->sorted_ptr = l->sorted_list;
 }
 
 
 void reclist_leave(struct reclist *l)
 {
     yaz_mutex_leave(l->mutex);
-    if (l)
-        l->sorted_ptr = l->sorted_list;
+    l->sorted_ptr = l->sorted_list;
+}
+
+void reclist_rewind(struct reclist *l)
+{
+    l->sorted_ptr = l->sorted_list;
 }
 
 
@@ -421,6 +454,7 @@ static struct record_cluster *new_cluster(
     cluster = nmem_malloc(l->nmem, sizeof(*cluster));
 
     record->next = 0;
+    cluster->retrieval_position = l->num_records;
     cluster->records = record;
     cluster->merge_keys = 0;
     append_merge_keys(&cluster->merge_keys, merge_keys, l->nmem);
@@ -479,32 +513,28 @@ struct record_cluster *reclist_insert(struct reclist *l,
                 // We found a matching record. Merge them
                 if (!strcmp(merge_key, mkr->value))
                 {
-                    struct record **re;
-
                     rb = *p;
-                    for (re = &rb->record->records; *re; re = &(*re)->next)
+                    if (!cluster)
                     {
-                        if ((*re)->client == record->client &&
-                            record_compare(record, *re, service))
+                        struct record **re;
+                        for (re = &rb->record->records; *re; re = &(*re)->next)
                         {
-                            yaz_mutex_leave(l->mutex);
-                            return 0;
+                            if ((*re)->client == record->client &&
+                                record_compare(record, *re, service))
+                            {
+                                yaz_mutex_leave(l->mutex);
+                                return 0;
+                            }
                         }
-                    }
-
-                    if (!cluster)
-                    {
                         cluster = rb->record;
                         *re = record;
                         record->next = 0;
-                        yaz_log(YLOG_LOG, "reclist: record insert %p", cluster);
                     }
                     else
                     {
                         if (cluster != rb->record)
                         {
                             assert(rb->record->relevance_explain1);
-                            yaz_log(YLOG_LOG, "reclist: cluster merge %p %p", cluster, rb->record);
                             merge_cluster(l, r, cluster, rb->record);
                             (*total)--;
                         }
@@ -516,7 +546,6 @@ struct record_cluster *reclist_insert(struct reclist *l,
         {
             (*total)++;
             cluster = new_cluster(l, r, service, record, merge_keys);
-            yaz_log(YLOG_LOG, "reclist: new cluster p=%p", cluster);
         }
 
         if (!rb)