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 "jenkins_hash.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 struct reclist *reclist_create(NMEM nmem, int numrecs)
241 while (hashsize < numrecs)
243 res = nmem_malloc(nmem, sizeof(struct reclist));
245 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
246 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
247 res->hashtable_size = hashsize;
249 res->hashmask = hashsize - 1; // Creates a bitmask
251 res->num_records = 0;
252 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
253 res->flatlist_size = numrecs;
258 // Insert a record. Return record cluster (newly formed or pre-existing)
259 struct record_cluster *reclist_insert( struct reclist *l,
260 struct conf_service *service,
261 struct record *record,
262 char *merge_key, int *total)
265 struct reclist_bucket **p;
266 struct record_cluster *cluster = 0;
274 bucket = jenkins_hash((unsigned char*) merge_key) & l->hashmask;
276 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
278 // We found a matching record. Merge them
279 if (!strcmp(merge_key, (*p)->record->merge_key))
281 struct record_cluster *existing = (*p)->record;
282 record->next = existing->records;
283 existing->records = record;
288 if (!cluster && l->num_records < l->flatlist_size)
290 struct reclist_bucket *new =
291 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
292 struct record_cluster *newc =
293 nmem_malloc(l->nmem, sizeof(struct record_cluster));
298 newc->records = record;
299 newc->merge_key = merge_key;
301 newc->term_frequency_vec = 0;
302 newc->recid = merge_key;
304 newc->metadata = nmem_malloc(l->nmem,
305 sizeof(struct record_metadata*) * service->num_metadata);
306 memset(newc->metadata, 0,
307 sizeof(struct record_metadata*) * service->num_metadata);
308 newc->sortkeys = nmem_malloc(l->nmem,
309 sizeof(struct record_metadata*) * service->num_sortkeys);
310 memset(newc->sortkeys, 0,
311 sizeof(union data_types*) * service->num_sortkeys);
314 l->flatlist[l->num_records++] = newc;
324 * c-file-style: "Stroustrup"
325 * indent-tabs-mode: nil
327 * vim: shiftwidth=4 tabstop=8 expandtab