1 /* $Id: reclists.c,v 1.20 2007-07-23 12:23:30 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include <yaz/yaz-util.h>
33 extern struct parameters global_parameters;
36 static struct reclist_sortparms *sortparms = 0;
40 struct record_cluster *record;
41 struct reclist_bucket *next;
45 struct reclist_sortparms *
46 reclist_sortparms_insert_field_id(NMEM nmem,
47 struct reclist_sortparms **sortparms,
49 enum conf_sortkey_type type,
52 struct reclist_sortparms * tmp_rlsp = 0;
55 if(!sortparms || field_id < 0)
58 // construct new reclist_sortparms
59 tmp_rlsp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
60 tmp_rlsp->offset = field_id;
61 tmp_rlsp->type = type;
62 tmp_rlsp->increasing = increasing;
65 // insert in *sortparms place, moving *sortparms one down the list
66 tmp_rlsp->next = *sortparms;
67 *sortparms = tmp_rlsp;
74 struct reclist_sortparms *
75 reclist_sortparms_insert(NMEM nmem,
76 struct reclist_sortparms **sortparms,
77 struct conf_service * service,
83 if (!sortparms || !service || !name)
86 field_id = conf_service_sortkey_field_id(service, name);
91 return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
92 service->sortkeys[field_id].type,
98 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms)
100 struct reclist_sortparms *res = 0;
101 struct reclist_sortparms **rp = &res;
102 struct conf_service *service = config->servers->service;
104 if (strlen(parms) > 256)
114 enum conf_sortkey_type type;
115 struct reclist_sortparms *new;
117 if (!(cpp = strchr(parms, ',')))
118 cpp = parms + strlen(parms);
119 strncpy(parm, parms, cpp - parms);
120 parm[cpp-parms] = '\0';
122 if ((pp = strchr(parm, ':')))
124 increasing = pp[1] == '1' ? 1 : 0;
129 if (!strcmp(parm, "relevance"))
131 type = Metadata_sortkey_relevance;
136 for (i = 0; i < service->num_sortkeys; i++)
138 struct conf_sortkey *sk = &service->sortkeys[i];
139 if (!strcmp(sk->name, parm))
142 if (type == Metadata_sortkey_skiparticle)
143 type = Metadata_sortkey_string;
147 if (i >= service->num_sortkeys)
149 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
155 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
157 new->offset = offset;
159 new->increasing = increasing;
167 static int reclist_cmp(const void *p1, const void *p2)
169 struct record_cluster *r1 = (*(struct record_cluster**) p1);
170 struct record_cluster *r2 = (*(struct record_cluster**) p2);
171 struct reclist_sortparms *s;
174 for (s = sortparms; s && res == 0; s = s->next)
176 union data_types *ut1 = r1->sortkeys[s->offset];
177 union data_types *ut2 = r2->sortkeys[s->offset];
182 case Metadata_sortkey_relevance:
183 res = r2->relevance - r1->relevance;
185 case Metadata_sortkey_string:
186 s1 = ut1 ? ut1->text : "";
187 s2 = ut2 ? ut2->text : "";
188 res = strcmp(s2, s1);
195 case Metadata_sortkey_numeric:
199 res = ut1->number.min - ut2->number.min;
201 res = ut2->number.max - ut1->number.max;
203 else if (ut1 && !ut2)
205 else if (!ut1 && ut2)
211 yaz_log(YLOG_FATAL, "Bad sort type: %d", s->type);
218 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
221 qsort(l->flatlist, l->num_records,
222 sizeof(struct record_cluster*), reclist_cmp);
226 struct record_cluster *reclist_read_record(struct reclist *l)
228 if (l && l->pointer < l->num_records)
229 return l->flatlist[l->pointer++];
234 void reclist_rewind(struct reclist *l)
240 // Jenkins one-at-a-time hash (from wikipedia)
241 static unsigned int hash(const unsigned char *key)
243 unsigned int hash = 0;
248 hash += (hash << 10);
252 hash ^= (hash >> 11);
253 hash += (hash << 15);
257 struct reclist *reclist_create(NMEM nmem, int numrecs)
263 while (hashsize < numrecs)
265 res = nmem_malloc(nmem, sizeof(struct reclist));
267 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
268 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
269 res->hashtable_size = hashsize;
271 res->hashmask = hashsize - 1; // Creates a bitmask
273 res->num_records = 0;
274 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
275 res->flatlist_size = numrecs;
280 // Insert a record. Return record cluster (newly formed or pre-existing)
281 struct record_cluster *reclist_insert( struct reclist *l,
282 struct conf_service *service,
283 struct record *record,
284 char *merge_key, int *total)
287 struct reclist_bucket **p;
288 struct record_cluster *cluster = 0;
296 bucket = hash((unsigned char*) merge_key) & l->hashmask;
298 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
300 // We found a matching record. Merge them
301 if (!strcmp(merge_key, (*p)->record->merge_key))
303 struct record_cluster *existing = (*p)->record;
304 record->next = existing->records;
305 existing->records = record;
310 if (!cluster && l->num_records < l->flatlist_size)
312 struct reclist_bucket *new =
313 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
314 struct record_cluster *newc =
315 nmem_malloc(l->nmem, sizeof(struct record_cluster));
320 newc->records = record;
321 newc->merge_key = merge_key;
323 newc->term_frequency_vec = 0;
324 newc->recid = merge_key;
326 newc->metadata = nmem_malloc(l->nmem,
327 sizeof(struct record_metadata*) * service->num_metadata);
328 memset(newc->metadata, 0,
329 sizeof(struct record_metadata*) * service->num_metadata);
330 newc->sortkeys = nmem_malloc(l->nmem,
331 sizeof(struct record_metadata*) * service->num_sortkeys);
332 memset(newc->sortkeys, 0,
333 sizeof(union data_types*) * service->num_sortkeys);
336 l->flatlist[l->num_records++] = newc;
346 * indent-tabs-mode: nil
348 * vim: shiftwidth=4 tabstop=8 expandtab