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 static struct reclist_sortparms *qsort_sortparms = 0; /* thread pr */
35 struct record_cluster *record;
36 struct reclist_bucket *next;
40 struct reclist_sortparms *
41 reclist_sortparms_insert_field_id(NMEM nmem,
42 struct reclist_sortparms **sortparms,
44 enum conf_sortkey_type type,
47 struct reclist_sortparms * tmp_rlsp = 0;
50 if(!sortparms || field_id < 0)
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;
60 // insert in *sortparms place, moving *sortparms one down the list
61 tmp_rlsp->next = *sortparms;
62 *sortparms = tmp_rlsp;
69 struct reclist_sortparms *
70 reclist_sortparms_insert(NMEM nmem,
71 struct reclist_sortparms **sortparms,
72 struct conf_service * service,
78 if (!sortparms || !service || !name)
81 field_id = conf_service_sortkey_field_id(service, name);
86 return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
87 service->sortkeys[field_id].type,
93 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
94 struct conf_service *service)
96 struct reclist_sortparms *res = 0;
97 struct reclist_sortparms **rp = &res;
99 if (strlen(parms) > 256)
109 enum conf_sortkey_type type;
110 struct reclist_sortparms *new;
112 if (!(cpp = strchr(parms, ',')))
113 cpp = parms + strlen(parms);
114 strncpy(parm, parms, cpp - parms);
115 parm[cpp-parms] = '\0';
117 if ((pp = strchr(parm, ':')))
119 increasing = pp[1] == '1' ? 1 : 0;
124 if (!strcmp(parm, "relevance"))
126 type = Metadata_sortkey_relevance;
130 for (i = 0; i < service->num_sortkeys; i++)
132 struct conf_sortkey *sk = &service->sortkeys[i];
133 if (!strcmp(sk->name, parm))
136 if (type == Metadata_sortkey_skiparticle)
137 type = Metadata_sortkey_string;
141 if (i >= service->num_sortkeys)
143 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
149 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
151 new->offset = offset;
153 new->increasing = increasing;
161 static int reclist_cmp(const void *p1, const void *p2)
163 struct record_cluster *r1 = (*(struct record_cluster**) p1);
164 struct record_cluster *r2 = (*(struct record_cluster**) p2);
165 struct reclist_sortparms *s;
168 for (s = qsort_sortparms; s && res == 0; s = s->next)
170 union data_types *ut1 = r1->sortkeys[s->offset];
171 union data_types *ut2 = r2->sortkeys[s->offset];
176 case Metadata_sortkey_relevance:
177 res = r2->relevance - r1->relevance;
179 case Metadata_sortkey_string:
180 s1 = ut1 ? ut1->text.sort : "";
181 s2 = ut2 ? ut2->text.sort : "";
182 res = strcmp(s2, s1);
189 case Metadata_sortkey_numeric:
193 res = ut1->number.min - ut2->number.min;
195 res = ut2->number.max - ut1->number.max;
197 else if (ut1 && !ut2)
199 else if (!ut1 && ut2)
205 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
212 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
214 qsort_sortparms = parms;
215 qsort(l->flatlist, l->num_records,
216 sizeof(struct record_cluster*), reclist_cmp);
220 struct record_cluster *reclist_read_record(struct reclist *l)
222 if (l && l->pointer < l->num_records)
223 return l->flatlist[l->pointer++];
228 void reclist_rewind(struct reclist *l)
234 // Jenkins one-at-a-time hash (from wikipedia)
235 static unsigned int hash(const unsigned char *key)
237 unsigned int hash = 0;
242 hash += (hash << 10);
246 hash ^= (hash >> 11);
247 hash += (hash << 15);
251 struct reclist *reclist_create(NMEM nmem, int numrecs)
257 while (hashsize < numrecs)
259 res = nmem_malloc(nmem, sizeof(struct reclist));
261 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
262 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
263 res->hashtable_size = hashsize;
265 res->hashmask = hashsize - 1; // Creates a bitmask
267 res->num_records = 0;
268 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
269 res->flatlist_size = numrecs;
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)
281 struct reclist_bucket **p;
282 struct record_cluster *cluster = 0;
290 bucket = hash((unsigned char*) merge_key) & l->hashmask;
292 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
294 // We found a matching record. Merge them
295 if (!strcmp(merge_key, (*p)->record->merge_key))
297 struct record_cluster *existing = (*p)->record;
298 record->next = existing->records;
299 existing->records = record;
304 if (!cluster && l->num_records < l->flatlist_size)
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));
314 newc->records = record;
315 newc->merge_key = merge_key;
317 newc->term_frequency_vec = 0;
318 newc->recid = merge_key;
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);
330 l->flatlist[l->num_records++] = newc;
340 * c-file-style: "Stroustrup"
341 * indent-tabs-mode: nil
343 * vim: shiftwidth=4 tabstop=8 expandtab