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