1 /* $Id: reclists.c,v 1.22 2007-08-28 21:11:21 quinn 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;
135 for (i = 0; i < service->num_sortkeys; i++)
137 struct conf_sortkey *sk = &service->sortkeys[i];
138 if (!strcmp(sk->name, parm))
141 if (type == Metadata_sortkey_skiparticle)
142 type = Metadata_sortkey_string;
146 if (i >= service->num_sortkeys)
148 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
154 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
156 new->offset = offset;
158 new->increasing = increasing;
166 static int reclist_cmp(const void *p1, const void *p2)
168 struct record_cluster *r1 = (*(struct record_cluster**) p1);
169 struct record_cluster *r2 = (*(struct record_cluster**) p2);
170 struct reclist_sortparms *s;
173 for (s = sortparms; s && res == 0; s = s->next)
175 union data_types *ut1 = r1->sortkeys[s->offset];
176 union data_types *ut2 = r2->sortkeys[s->offset];
181 case Metadata_sortkey_relevance:
182 res = r2->relevance - r1->relevance;
184 case Metadata_sortkey_string:
185 s1 = ut1 ? ut1->text : "";
186 s2 = ut2 ? ut2->text : "";
187 res = strcmp(s2, s1);
194 case Metadata_sortkey_numeric:
198 res = ut1->number.min - ut2->number.min;
200 res = ut2->number.max - ut1->number.max;
202 else if (ut1 && !ut2)
204 else if (!ut1 && ut2)
210 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
217 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
220 qsort(l->flatlist, l->num_records,
221 sizeof(struct record_cluster*), reclist_cmp);
225 struct record_cluster *reclist_read_record(struct reclist *l)
227 if (l && l->pointer < l->num_records)
228 return l->flatlist[l->pointer++];
233 void reclist_rewind(struct reclist *l)
239 // Jenkins one-at-a-time hash (from wikipedia)
240 static unsigned int hash(const unsigned char *key)
242 unsigned int hash = 0;
247 hash += (hash << 10);
251 hash ^= (hash >> 11);
252 hash += (hash << 15);
256 struct reclist *reclist_create(NMEM nmem, int numrecs)
262 while (hashsize < numrecs)
264 res = nmem_malloc(nmem, sizeof(struct reclist));
266 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
267 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
268 res->hashtable_size = hashsize;
270 res->hashmask = hashsize - 1; // Creates a bitmask
272 res->num_records = 0;
273 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
274 res->flatlist_size = numrecs;
279 // Insert a record. Return record cluster (newly formed or pre-existing)
280 struct record_cluster *reclist_insert( struct reclist *l,
281 struct conf_service *service,
282 struct record *record,
283 char *merge_key, int *total)
286 struct reclist_bucket **p;
287 struct record_cluster *cluster = 0;
295 bucket = hash((unsigned char*) merge_key) & l->hashmask;
297 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
299 // We found a matching record. Merge them
300 if (!strcmp(merge_key, (*p)->record->merge_key))
302 struct record_cluster *existing = (*p)->record;
303 record->next = existing->records;
304 existing->records = record;
309 if (!cluster && l->num_records < l->flatlist_size)
311 struct reclist_bucket *new =
312 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
313 struct record_cluster *newc =
314 nmem_malloc(l->nmem, sizeof(struct record_cluster));
319 newc->records = record;
320 newc->merge_key = merge_key;
322 newc->term_frequency_vec = 0;
323 newc->recid = merge_key;
325 newc->metadata = nmem_malloc(l->nmem,
326 sizeof(struct record_metadata*) * service->num_metadata);
327 memset(newc->metadata, 0,
328 sizeof(struct record_metadata*) * service->num_metadata);
329 newc->sortkeys = nmem_malloc(l->nmem,
330 sizeof(struct record_metadata*) * service->num_sortkeys);
331 memset(newc->sortkeys, 0,
332 sizeof(union data_types*) * service->num_sortkeys);
335 l->flatlist[l->num_records++] = newc;
345 * indent-tabs-mode: nil
347 * vim: shiftwidth=4 tabstop=8 expandtab