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