1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
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
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
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
26 #include <yaz/yaz-util.h>
30 #include "parameters.h"
32 static struct reclist_sortparms *qsort_sortparms = 0; /* thread pr */
36 struct record_cluster *record;
37 struct reclist_bucket *next;
41 struct reclist_sortparms *
42 reclist_sortparms_insert_field_id(NMEM nmem,
43 struct reclist_sortparms **sortparms,
45 enum conf_sortkey_type type,
48 struct reclist_sortparms * tmp_rlsp = 0;
51 if(!sortparms || field_id < 0)
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;
61 // insert in *sortparms place, moving *sortparms one down the list
62 tmp_rlsp->next = *sortparms;
63 *sortparms = tmp_rlsp;
70 struct reclist_sortparms *
71 reclist_sortparms_insert(NMEM nmem,
72 struct reclist_sortparms **sortparms,
73 struct conf_service * service,
79 if (!sortparms || !service || !name)
82 field_id = conf_service_sortkey_field_id(service, name);
87 return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
88 service->sortkeys[field_id].type,
94 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
95 struct conf_service *service)
97 struct reclist_sortparms *res = 0;
98 struct reclist_sortparms **rp = &res;
100 if (strlen(parms) > 256)
110 enum conf_sortkey_type type;
111 struct reclist_sortparms *new;
113 if (!(cpp = strchr(parms, ',')))
114 cpp = parms + strlen(parms);
115 strncpy(parm, parms, cpp - parms);
116 parm[cpp-parms] = '\0';
118 if ((pp = strchr(parm, ':')))
120 increasing = pp[1] == '1' ? 1 : 0;
125 if (!strcmp(parm, "relevance"))
127 type = Metadata_sortkey_relevance;
131 for (i = 0; i < service->num_sortkeys; i++)
133 struct conf_sortkey *sk = &service->sortkeys[i];
134 if (!strcmp(sk->name, parm))
137 if (type == Metadata_sortkey_skiparticle)
138 type = Metadata_sortkey_string;
142 if (i >= service->num_sortkeys)
144 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
150 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
152 new->offset = offset;
154 new->increasing = increasing;
162 static int reclist_cmp(const void *p1, const void *p2)
164 struct record_cluster *r1 = (*(struct record_cluster**) p1);
165 struct record_cluster *r2 = (*(struct record_cluster**) p2);
166 struct reclist_sortparms *s;
169 for (s = qsort_sortparms; s && res == 0; s = s->next)
171 union data_types *ut1 = r1->sortkeys[s->offset];
172 union data_types *ut2 = r2->sortkeys[s->offset];
177 case Metadata_sortkey_relevance:
178 res = r2->relevance - r1->relevance;
180 case Metadata_sortkey_string:
181 s1 = ut1 ? ut1->text.sort : "";
182 s2 = ut2 ? ut2->text.sort : "";
183 res = strcmp(s2, s1);
190 case Metadata_sortkey_numeric:
194 res = ut1->number.min - ut2->number.min;
196 res = ut2->number.max - ut1->number.max;
198 else if (ut1 && !ut2)
200 else if (!ut1 && ut2)
206 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
213 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
215 qsort_sortparms = parms;
216 qsort(l->flatlist, l->num_records,
217 sizeof(struct record_cluster*), reclist_cmp);
221 struct record_cluster *reclist_read_record(struct reclist *l)
223 if (l && l->pointer < l->num_records)
224 return l->flatlist[l->pointer++];
229 void reclist_rewind(struct reclist *l)
235 // Jenkins one-at-a-time hash (from wikipedia)
236 static unsigned int hash(const unsigned char *key)
238 unsigned int hash = 0;
243 hash += (hash << 10);
247 hash ^= (hash >> 11);
248 hash += (hash << 15);
252 struct reclist *reclist_create(NMEM nmem, int numrecs)
258 while (hashsize < numrecs)
260 res = nmem_malloc(nmem, sizeof(struct reclist));
262 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
263 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
264 res->hashtable_size = hashsize;
266 res->hashmask = hashsize - 1; // Creates a bitmask
268 res->num_records = 0;
269 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
270 res->flatlist_size = numrecs;
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)
282 struct reclist_bucket **p;
283 struct record_cluster *cluster = 0;
291 bucket = hash((unsigned char*) merge_key) & l->hashmask;
293 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
295 // We found a matching record. Merge them
296 if (!strcmp(merge_key, (*p)->record->merge_key))
298 struct record_cluster *existing = (*p)->record;
299 record->next = existing->records;
300 existing->records = record;
305 if (!cluster && l->num_records < l->flatlist_size)
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));
315 newc->records = record;
316 newc->merge_key = merge_key;
318 newc->term_frequency_vec = 0;
319 newc->recid = merge_key;
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);
331 l->flatlist[l->num_records++] = newc;
341 * c-file-style: "Stroustrup"
342 * indent-tabs-mode: nil
344 * vim: shiftwidth=4 tabstop=8 expandtab