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>
31 extern struct parameters global_parameters;
33 static struct reclist_sortparms *qsort_sortparms = 0; /* thread pr */
37 struct record_cluster *record;
38 struct reclist_bucket *next;
42 struct reclist_sortparms *
43 reclist_sortparms_insert_field_id(NMEM nmem,
44 struct reclist_sortparms **sortparms,
46 enum conf_sortkey_type type,
49 struct reclist_sortparms * tmp_rlsp = 0;
52 if(!sortparms || field_id < 0)
55 // construct new reclist_sortparms
56 tmp_rlsp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
57 tmp_rlsp->offset = field_id;
58 tmp_rlsp->type = type;
59 tmp_rlsp->increasing = increasing;
62 // insert in *sortparms place, moving *sortparms one down the list
63 tmp_rlsp->next = *sortparms;
64 *sortparms = tmp_rlsp;
71 struct reclist_sortparms *
72 reclist_sortparms_insert(NMEM nmem,
73 struct reclist_sortparms **sortparms,
74 struct conf_service * service,
80 if (!sortparms || !service || !name)
83 field_id = conf_service_sortkey_field_id(service, name);
88 return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
89 service->sortkeys[field_id].type,
95 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
96 struct conf_service *service)
98 struct reclist_sortparms *res = 0;
99 struct reclist_sortparms **rp = &res;
101 if (strlen(parms) > 256)
111 enum conf_sortkey_type type;
112 struct reclist_sortparms *new;
114 if (!(cpp = strchr(parms, ',')))
115 cpp = parms + strlen(parms);
116 strncpy(parm, parms, cpp - parms);
117 parm[cpp-parms] = '\0';
119 if ((pp = strchr(parm, ':')))
121 increasing = pp[1] == '1' ? 1 : 0;
126 if (!strcmp(parm, "relevance"))
128 type = Metadata_sortkey_relevance;
132 for (i = 0; i < service->num_sortkeys; i++)
134 struct conf_sortkey *sk = &service->sortkeys[i];
135 if (!strcmp(sk->name, parm))
138 if (type == Metadata_sortkey_skiparticle)
139 type = Metadata_sortkey_string;
143 if (i >= service->num_sortkeys)
145 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
151 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
153 new->offset = offset;
155 new->increasing = increasing;
163 static int reclist_cmp(const void *p1, const void *p2)
165 struct record_cluster *r1 = (*(struct record_cluster**) p1);
166 struct record_cluster *r2 = (*(struct record_cluster**) p2);
167 struct reclist_sortparms *s;
170 for (s = qsort_sortparms; s && res == 0; s = s->next)
172 union data_types *ut1 = r1->sortkeys[s->offset];
173 union data_types *ut2 = r2->sortkeys[s->offset];
178 case Metadata_sortkey_relevance:
179 res = r2->relevance - r1->relevance;
181 case Metadata_sortkey_string:
182 s1 = ut1 ? ut1->text.sort : "";
183 s2 = ut2 ? ut2->text.sort : "";
184 res = strcmp(s2, s1);
191 case Metadata_sortkey_numeric:
195 res = ut1->number.min - ut2->number.min;
197 res = ut2->number.max - ut1->number.max;
199 else if (ut1 && !ut2)
201 else if (!ut1 && ut2)
207 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
214 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
216 qsort_sortparms = parms;
217 qsort(l->flatlist, l->num_records,
218 sizeof(struct record_cluster*), reclist_cmp);
222 struct record_cluster *reclist_read_record(struct reclist *l)
224 if (l && l->pointer < l->num_records)
225 return l->flatlist[l->pointer++];
230 void reclist_rewind(struct reclist *l)
236 // Jenkins one-at-a-time hash (from wikipedia)
237 static unsigned int hash(const unsigned char *key)
239 unsigned int hash = 0;
244 hash += (hash << 10);
248 hash ^= (hash >> 11);
249 hash += (hash << 15);
253 struct reclist *reclist_create(NMEM nmem, int numrecs)
259 while (hashsize < numrecs)
261 res = nmem_malloc(nmem, sizeof(struct reclist));
263 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
264 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
265 res->hashtable_size = hashsize;
267 res->hashmask = hashsize - 1; // Creates a bitmask
269 res->num_records = 0;
270 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
271 res->flatlist_size = numrecs;
276 // Insert a record. Return record cluster (newly formed or pre-existing)
277 struct record_cluster *reclist_insert( struct reclist *l,
278 struct conf_service *service,
279 struct record *record,
280 char *merge_key, int *total)
283 struct reclist_bucket **p;
284 struct record_cluster *cluster = 0;
292 bucket = hash((unsigned char*) merge_key) & l->hashmask;
294 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
296 // We found a matching record. Merge them
297 if (!strcmp(merge_key, (*p)->record->merge_key))
299 struct record_cluster *existing = (*p)->record;
300 record->next = existing->records;
301 existing->records = record;
306 if (!cluster && l->num_records < l->flatlist_size)
308 struct reclist_bucket *new =
309 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
310 struct record_cluster *newc =
311 nmem_malloc(l->nmem, sizeof(struct record_cluster));
316 newc->records = record;
317 newc->merge_key = merge_key;
319 newc->term_frequency_vec = 0;
320 newc->recid = merge_key;
322 newc->metadata = nmem_malloc(l->nmem,
323 sizeof(struct record_metadata*) * service->num_metadata);
324 memset(newc->metadata, 0,
325 sizeof(struct record_metadata*) * service->num_metadata);
326 newc->sortkeys = nmem_malloc(l->nmem,
327 sizeof(struct record_metadata*) * service->num_sortkeys);
328 memset(newc->sortkeys, 0,
329 sizeof(union data_types*) * service->num_sortkeys);
332 l->flatlist[l->num_records++] = newc;
342 * c-file-style: "Stroustrup"
343 * indent-tabs-mode: nil
345 * vim: shiftwidth=4 tabstop=8 expandtab