Implemented numeric sorting (sort by year). Bug #820.
[pazpar2-moved-to-github.git] / src / reclists.c
1 /* $Id: reclists.c,v 1.20 2007-07-23 12:23:30 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <assert.h>
23
24 #if HAVE_CONFIG_H
25 #include <cconfig.h>
26 #endif
27
28 #include <yaz/yaz-util.h>
29
30 #include "pazpar2.h"
31 #include "reclists.h"
32
33 extern struct parameters global_parameters;
34
35 // Not threadsafe
36 static struct reclist_sortparms *sortparms = 0;
37
38 struct reclist_bucket
39 {
40     struct record_cluster *record;
41     struct reclist_bucket *next;
42 };
43
44 #if 0
45 struct reclist_sortparms * 
46 reclist_sortparms_insert_field_id(NMEM nmem,
47                          struct reclist_sortparms **sortparms,
48                          int field_id,
49                          enum conf_sortkey_type type,
50                          int increasing)
51 {
52     struct reclist_sortparms * tmp_rlsp = 0;
53     // assert(nmem);
54
55     if(!sortparms || field_id < 0)
56         return 0;
57
58     // construct new reclist_sortparms
59     tmp_rlsp  = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
60     tmp_rlsp->offset = field_id;
61     tmp_rlsp->type = type;
62     tmp_rlsp->increasing = increasing;
63
64
65     // insert in *sortparms place, moving *sortparms one down the list
66     tmp_rlsp->next = *sortparms;
67     *sortparms = tmp_rlsp;
68
69     return *sortparms;
70 };
71 #endif
72
73 #if 0
74 struct reclist_sortparms * 
75 reclist_sortparms_insert(NMEM nmem, 
76                          struct reclist_sortparms **sortparms,
77                          struct conf_service * service,
78                          const char * name,
79                          int increasing)
80 {
81     int field_id = 0;
82
83     if (!sortparms || !service || !name)  
84         return 0;
85     
86     field_id = conf_service_sortkey_field_id(service, name);
87
88     if (-1 == field_id)
89         return 0;
90
91     return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
92                                              service->sortkeys[field_id].type,
93                                              increasing);
94 };
95 #endif
96
97
98 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms)
99 {
100     struct reclist_sortparms *res = 0;
101     struct reclist_sortparms **rp = &res;
102     struct conf_service *service = config->servers->service;
103
104     if (strlen(parms) > 256)
105         return 0;
106     while (*parms)
107     {
108         char parm[256];
109         char *pp;
110         const char *cpp;
111         int increasing;
112         int i;
113         int offset;
114         enum conf_sortkey_type type;
115         struct reclist_sortparms *new;
116
117         if (!(cpp = strchr(parms, ',')))
118             cpp = parms + strlen(parms);
119         strncpy(parm, parms, cpp - parms); 
120         parm[cpp-parms] = '\0';
121
122         if ((pp = strchr(parm, ':')))
123         {
124             increasing = pp[1] == '1' ? 1 : 0;
125             *pp = '\0';
126         }
127         else
128             increasing = 0;
129         if (!strcmp(parm, "relevance"))
130         {
131             type = Metadata_sortkey_relevance;
132             offset = -1;
133         }
134         else
135         {
136             for (i = 0; i < service->num_sortkeys; i++)
137             {
138                 struct conf_sortkey *sk = &service->sortkeys[i];
139                 if (!strcmp(sk->name, parm))
140                 {
141                     type = sk->type;
142                     if (type == Metadata_sortkey_skiparticle)
143                         type = Metadata_sortkey_string;
144                     break;
145                 }
146             }
147             if (i >= service->num_sortkeys)
148             {
149                 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
150                 return 0;
151             }
152             else
153                 offset = i;
154         }
155         new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
156         new->next = 0;
157         new->offset = offset;
158         new->type = type;
159         new->increasing = increasing;
160         rp = &new->next;
161         if (*(parms = cpp))
162             parms++;
163     }
164     return res;
165 }
166
167 static int reclist_cmp(const void *p1, const void *p2)
168 {
169     struct record_cluster *r1 = (*(struct record_cluster**) p1);
170     struct record_cluster *r2 = (*(struct record_cluster**) p2);
171     struct reclist_sortparms *s;
172     int res = 0;
173
174     for (s = sortparms; s && res == 0; s = s->next)
175     {
176         union data_types *ut1 = r1->sortkeys[s->offset];
177         union data_types *ut2 = r2->sortkeys[s->offset];
178         switch (s->type)
179         {
180             char *s1, *s2;
181             
182             case Metadata_sortkey_relevance:
183                 res = r2->relevance - r1->relevance;
184                 break;
185             case Metadata_sortkey_string:
186                 s1 = ut1 ? ut1->text : "";
187                 s2 = ut2 ? ut2->text : "";
188                 res = strcmp(s2, s1);
189                 if (res)
190                 {
191                     if (s->increasing)
192                         res *= -1;
193                 }
194                 break;
195             case Metadata_sortkey_numeric:
196                 if (ut1 && ut2)
197                 {
198                     if (s->increasing)
199                         res = ut1->number.min  - ut2->number.min;
200                     else
201                         res = ut2->number.max  - ut1->number.max;
202                 }
203                 else if (ut1 && !ut2)
204                     res = -1;
205                 else if (!ut1 && ut2)
206                     res = 1;
207                 else
208                     res = 0;
209                 break;
210             default:
211                 yaz_log(YLOG_FATAL, "Bad sort type: %d", s->type);
212                 exit(1);
213         }
214     }
215     return res;
216 }
217
218 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
219 {
220     sortparms = parms;
221     qsort(l->flatlist, l->num_records, 
222           sizeof(struct record_cluster*), reclist_cmp);
223     reclist_rewind(l);
224 }
225
226 struct record_cluster *reclist_read_record(struct reclist *l)
227 {
228     if (l && l->pointer < l->num_records)
229         return l->flatlist[l->pointer++];
230     else
231         return 0;
232 }
233
234 void reclist_rewind(struct reclist *l)
235 {
236     if (l)
237         l->pointer = 0;
238 }
239
240 // Jenkins one-at-a-time hash (from wikipedia)
241 static unsigned int hash(const unsigned char *key)
242 {
243     unsigned int hash = 0;
244
245     while (*key)
246     {
247         hash += *(key++);
248         hash += (hash << 10);
249         hash ^= (hash >> 6);
250     }
251     hash += (hash << 3);
252     hash ^= (hash >> 11);
253     hash += (hash << 15);
254     return hash;
255 }
256
257 struct reclist *reclist_create(NMEM nmem, int numrecs)
258 {
259     int hashsize = 1;
260     struct reclist *res;
261
262     assert(numrecs);
263     while (hashsize < numrecs)
264         hashsize <<= 1;
265     res = nmem_malloc(nmem, sizeof(struct reclist));
266     res->hashtable 
267         = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
268     memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
269     res->hashtable_size = hashsize;
270     res->nmem = nmem;
271     res->hashmask = hashsize - 1; // Creates a bitmask
272
273     res->num_records = 0;
274     res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
275     res->flatlist_size = numrecs;
276
277     return res;
278 }
279
280 // Insert a record. Return record cluster (newly formed or pre-existing)
281 struct record_cluster *reclist_insert( struct reclist *l,
282                                        struct conf_service *service, 
283                                        struct record  *record,
284                                        char *merge_key, int *total)
285 {
286     unsigned int bucket;
287     struct reclist_bucket **p;
288     struct record_cluster *cluster = 0;
289     
290     assert(service);
291     assert(l);
292     assert(record);
293     assert(merge_key);
294     assert(total);
295
296     bucket = hash((unsigned char*) merge_key) & l->hashmask;
297
298     for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
299     {
300         // We found a matching record. Merge them
301         if (!strcmp(merge_key, (*p)->record->merge_key))
302         {
303             struct record_cluster *existing = (*p)->record;
304             record->next = existing->records;
305             existing->records = record;
306             cluster = existing;
307             break;
308         }
309     }
310     if (!cluster && l->num_records < l->flatlist_size)
311     {
312         struct reclist_bucket *new =
313             nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
314         struct record_cluster *newc =
315             nmem_malloc(l->nmem, sizeof(struct record_cluster));
316         
317         record->next = 0;
318         new->record = newc;
319         new->next = 0;
320         newc->records = record;
321         newc->merge_key = merge_key;
322         newc->relevance = 0;
323         newc->term_frequency_vec = 0;
324         newc->recid = merge_key;
325         (*total)++;
326         newc->metadata = nmem_malloc(l->nmem,
327                 sizeof(struct record_metadata*) * service->num_metadata);
328         memset(newc->metadata, 0, 
329                sizeof(struct record_metadata*) * service->num_metadata);
330         newc->sortkeys = nmem_malloc(l->nmem,
331                 sizeof(struct record_metadata*) * service->num_sortkeys);
332         memset(newc->sortkeys, 0,
333                sizeof(union data_types*) * service->num_sortkeys);
334
335         *p = new;
336         l->flatlist[l->num_records++] = newc;
337         cluster = newc;
338     }
339     return cluster;
340 }
341
342
343 /*
344  * Local variables:
345  * c-basic-offset: 4
346  * indent-tabs-mode: nil
347  * End:
348  * vim: shiftwidth=4 tabstop=8 expandtab
349  */