More clean-up (remove util.c).
[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 static struct reclist_sortparms *qsort_sortparms = 0; /* thread pr */
32
33 struct reclist_bucket
34 {
35     struct record_cluster *record;
36     struct reclist_bucket *next;
37 };
38
39 #if 0
40 struct reclist_sortparms * 
41 reclist_sortparms_insert_field_id(NMEM nmem,
42                          struct reclist_sortparms **sortparms,
43                          int field_id,
44                          enum conf_sortkey_type type,
45                          int increasing)
46 {
47     struct reclist_sortparms * tmp_rlsp = 0;
48     // assert(nmem);
49
50     if(!sortparms || field_id < 0)
51         return 0;
52
53     // construct new reclist_sortparms
54     tmp_rlsp  = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
55     tmp_rlsp->offset = field_id;
56     tmp_rlsp->type = type;
57     tmp_rlsp->increasing = increasing;
58
59
60     // insert in *sortparms place, moving *sortparms one down the list
61     tmp_rlsp->next = *sortparms;
62     *sortparms = tmp_rlsp;
63
64     return *sortparms;
65 };
66 #endif
67
68 #if 0
69 struct reclist_sortparms * 
70 reclist_sortparms_insert(NMEM nmem, 
71                          struct reclist_sortparms **sortparms,
72                          struct conf_service * service,
73                          const char * name,
74                          int increasing)
75 {
76     int field_id = 0;
77
78     if (!sortparms || !service || !name)  
79         return 0;
80     
81     field_id = conf_service_sortkey_field_id(service, name);
82
83     if (-1 == field_id)
84         return 0;
85
86     return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
87                                              service->sortkeys[field_id].type,
88                                              increasing);
89 };
90 #endif
91
92
93 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
94     struct conf_service *service)
95 {
96     struct reclist_sortparms *res = 0;
97     struct reclist_sortparms **rp = &res;
98
99     if (strlen(parms) > 256)
100         return 0;
101     while (*parms)
102     {
103         char parm[256];
104         char *pp;
105         const char *cpp;
106         int increasing;
107         int i;
108         int offset = 0;
109         enum conf_sortkey_type type;
110         struct reclist_sortparms *new;
111
112         if (!(cpp = strchr(parms, ',')))
113             cpp = parms + strlen(parms);
114         strncpy(parm, parms, cpp - parms); 
115         parm[cpp-parms] = '\0';
116
117         if ((pp = strchr(parm, ':')))
118         {
119             increasing = pp[1] == '1' ? 1 : 0;
120             *pp = '\0';
121         }
122         else
123             increasing = 0;
124         if (!strcmp(parm, "relevance"))
125         {
126             type = Metadata_sortkey_relevance;
127         }
128         else
129         {
130             for (i = 0; i < service->num_sortkeys; i++)
131             {
132                 struct conf_sortkey *sk = &service->sortkeys[i];
133                 if (!strcmp(sk->name, parm))
134                 {
135                     type = sk->type;
136                     if (type == Metadata_sortkey_skiparticle)
137                         type = Metadata_sortkey_string;
138                     break;
139                 }
140             }
141             if (i >= service->num_sortkeys)
142             {
143                 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
144                 return 0;
145             }
146             else
147                 offset = i;
148         }
149         new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
150         new->next = 0;
151         new->offset = offset;
152         new->type = type;
153         new->increasing = increasing;
154         rp = &new->next;
155         if (*(parms = cpp))
156             parms++;
157     }
158     return res;
159 }
160
161 static int reclist_cmp(const void *p1, const void *p2)
162 {
163     struct record_cluster *r1 = (*(struct record_cluster**) p1);
164     struct record_cluster *r2 = (*(struct record_cluster**) p2);
165     struct reclist_sortparms *s;
166     int res = 0;
167
168     for (s = qsort_sortparms; s && res == 0; s = s->next)
169     {
170         union data_types *ut1 = r1->sortkeys[s->offset];
171         union data_types *ut2 = r2->sortkeys[s->offset];
172         switch (s->type)
173         {
174             const char *s1, *s2;
175             
176             case Metadata_sortkey_relevance:
177                 res = r2->relevance - r1->relevance;
178                 break;
179             case Metadata_sortkey_string:
180                 s1 = ut1 ? ut1->text.sort : "";
181                 s2 = ut2 ? ut2->text.sort : "";
182                 res = strcmp(s2, s1);
183                 if (res)
184                 {
185                     if (s->increasing)
186                         res *= -1;
187                 }
188                 break;
189             case Metadata_sortkey_numeric:
190                 if (ut1 && ut2)
191                 {
192                     if (s->increasing)
193                         res = ut1->number.min  - ut2->number.min;
194                     else
195                         res = ut2->number.max  - ut1->number.max;
196                 }
197                 else if (ut1 && !ut2)
198                     res = -1;
199                 else if (!ut1 && ut2)
200                     res = 1;
201                 else
202                     res = 0;
203                 break;
204             default:
205                 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
206                 res = 0;
207         }
208     }
209     return res;
210 }
211
212 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
213 {
214     qsort_sortparms = parms;
215     qsort(l->flatlist, l->num_records, 
216           sizeof(struct record_cluster*), reclist_cmp);
217     reclist_rewind(l);
218 }
219
220 struct record_cluster *reclist_read_record(struct reclist *l)
221 {
222     if (l && l->pointer < l->num_records)
223         return l->flatlist[l->pointer++];
224     else
225         return 0;
226 }
227
228 void reclist_rewind(struct reclist *l)
229 {
230     if (l)
231         l->pointer = 0;
232 }
233
234 // Jenkins one-at-a-time hash (from wikipedia)
235 static unsigned int hash(const unsigned char *key)
236 {
237     unsigned int hash = 0;
238
239     while (*key)
240     {
241         hash += *(key++);
242         hash += (hash << 10);
243         hash ^= (hash >> 6);
244     }
245     hash += (hash << 3);
246     hash ^= (hash >> 11);
247     hash += (hash << 15);
248     return hash;
249 }
250
251 struct reclist *reclist_create(NMEM nmem, int numrecs)
252 {
253     int hashsize = 1;
254     struct reclist *res;
255
256     assert(numrecs);
257     while (hashsize < numrecs)
258         hashsize <<= 1;
259     res = nmem_malloc(nmem, sizeof(struct reclist));
260     res->hashtable 
261         = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
262     memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
263     res->hashtable_size = hashsize;
264     res->nmem = nmem;
265     res->hashmask = hashsize - 1; // Creates a bitmask
266
267     res->num_records = 0;
268     res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
269     res->flatlist_size = numrecs;
270
271     return res;
272 }
273
274 // Insert a record. Return record cluster (newly formed or pre-existing)
275 struct record_cluster *reclist_insert( struct reclist *l,
276                                        struct conf_service *service, 
277                                        struct record  *record,
278                                        char *merge_key, int *total)
279 {
280     unsigned int bucket;
281     struct reclist_bucket **p;
282     struct record_cluster *cluster = 0;
283     
284     assert(service);
285     assert(l);
286     assert(record);
287     assert(merge_key);
288     assert(total);
289
290     bucket = hash((unsigned char*) merge_key) & l->hashmask;
291
292     for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
293     {
294         // We found a matching record. Merge them
295         if (!strcmp(merge_key, (*p)->record->merge_key))
296         {
297             struct record_cluster *existing = (*p)->record;
298             record->next = existing->records;
299             existing->records = record;
300             cluster = existing;
301             break;
302         }
303     }
304     if (!cluster && l->num_records < l->flatlist_size)
305     {
306         struct reclist_bucket *new =
307             nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
308         struct record_cluster *newc =
309             nmem_malloc(l->nmem, sizeof(struct record_cluster));
310         
311         record->next = 0;
312         new->record = newc;
313         new->next = 0;
314         newc->records = record;
315         newc->merge_key = merge_key;
316         newc->relevance = 0;
317         newc->term_frequency_vec = 0;
318         newc->recid = merge_key;
319         (*total)++;
320         newc->metadata = nmem_malloc(l->nmem,
321                 sizeof(struct record_metadata*) * service->num_metadata);
322         memset(newc->metadata, 0, 
323                sizeof(struct record_metadata*) * service->num_metadata);
324         newc->sortkeys = nmem_malloc(l->nmem,
325                 sizeof(struct record_metadata*) * service->num_sortkeys);
326         memset(newc->sortkeys, 0,
327                sizeof(union data_types*) * service->num_sortkeys);
328
329         *p = new;
330         l->flatlist[l->num_records++] = newc;
331         cluster = newc;
332     }
333     return cluster;
334 }
335
336
337 /*
338  * Local variables:
339  * c-basic-offset: 4
340  * c-file-style: "Stroustrup"
341  * indent-tabs-mode: nil
342  * End:
343  * vim: shiftwidth=4 tabstop=8 expandtab
344  */
345