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;
34 static struct reclist_sortparms *sortparms = 0;
38 struct record_cluster *record;
39 struct reclist_bucket *next;
43 struct reclist_sortparms *
44 reclist_sortparms_insert_field_id(NMEM nmem,
45 struct reclist_sortparms **sortparms,
47 enum conf_sortkey_type type,
50 struct reclist_sortparms * tmp_rlsp = 0;
53 if(!sortparms || field_id < 0)
56 // construct new reclist_sortparms
57 tmp_rlsp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
58 tmp_rlsp->offset = field_id;
59 tmp_rlsp->type = type;
60 tmp_rlsp->increasing = increasing;
63 // insert in *sortparms place, moving *sortparms one down the list
64 tmp_rlsp->next = *sortparms;
65 *sortparms = tmp_rlsp;
72 struct reclist_sortparms *
73 reclist_sortparms_insert(NMEM nmem,
74 struct reclist_sortparms **sortparms,
75 struct conf_service * service,
81 if (!sortparms || !service || !name)
84 field_id = conf_service_sortkey_field_id(service, name);
89 return reclist_sortparms_insert_field_id(nmem, sortparms, field_id,
90 service->sortkeys[field_id].type,
96 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms)
98 struct reclist_sortparms *res = 0;
99 struct reclist_sortparms **rp = &res;
100 struct conf_service *service = config->servers->service;
102 if (strlen(parms) > 256)
112 enum conf_sortkey_type type;
113 struct reclist_sortparms *new;
115 if (!(cpp = strchr(parms, ',')))
116 cpp = parms + strlen(parms);
117 strncpy(parm, parms, cpp - parms);
118 parm[cpp-parms] = '\0';
120 if ((pp = strchr(parm, ':')))
122 increasing = pp[1] == '1' ? 1 : 0;
127 if (!strcmp(parm, "relevance"))
129 type = Metadata_sortkey_relevance;
133 for (i = 0; i < service->num_sortkeys; i++)
135 struct conf_sortkey *sk = &service->sortkeys[i];
136 if (!strcmp(sk->name, parm))
139 if (type == Metadata_sortkey_skiparticle)
140 type = Metadata_sortkey_string;
144 if (i >= service->num_sortkeys)
146 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
152 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
154 new->offset = offset;
156 new->increasing = increasing;
164 static int reclist_cmp(const void *p1, const void *p2)
166 struct record_cluster *r1 = (*(struct record_cluster**) p1);
167 struct record_cluster *r2 = (*(struct record_cluster**) p2);
168 struct reclist_sortparms *s;
171 for (s = sortparms; s && res == 0; s = s->next)
173 union data_types *ut1 = r1->sortkeys[s->offset];
174 union data_types *ut2 = r2->sortkeys[s->offset];
179 case Metadata_sortkey_relevance:
180 res = r2->relevance - r1->relevance;
182 case Metadata_sortkey_string:
183 s1 = ut1 ? ut1->text.sort : "";
184 s2 = ut2 ? ut2->text.sort : "";
185 res = strcmp(s2, s1);
192 case Metadata_sortkey_numeric:
196 res = ut1->number.min - ut2->number.min;
198 res = ut2->number.max - ut1->number.max;
200 else if (ut1 && !ut2)
202 else if (!ut1 && ut2)
208 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
215 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
218 qsort(l->flatlist, l->num_records,
219 sizeof(struct record_cluster*), reclist_cmp);
223 struct record_cluster *reclist_read_record(struct reclist *l)
225 if (l && l->pointer < l->num_records)
226 return l->flatlist[l->pointer++];
231 void reclist_rewind(struct reclist *l)
237 // Jenkins one-at-a-time hash (from wikipedia)
238 static unsigned int hash(const unsigned char *key)
240 unsigned int hash = 0;
245 hash += (hash << 10);
249 hash ^= (hash >> 11);
250 hash += (hash << 15);
254 struct reclist *reclist_create(NMEM nmem, int numrecs)
260 while (hashsize < numrecs)
262 res = nmem_malloc(nmem, sizeof(struct reclist));
264 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
265 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
266 res->hashtable_size = hashsize;
268 res->hashmask = hashsize - 1; // Creates a bitmask
270 res->num_records = 0;
271 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
272 res->flatlist_size = numrecs;
277 // Insert a record. Return record cluster (newly formed or pre-existing)
278 struct record_cluster *reclist_insert( struct reclist *l,
279 struct conf_service *service,
280 struct record *record,
281 char *merge_key, int *total)
284 struct reclist_bucket **p;
285 struct record_cluster *cluster = 0;
293 bucket = hash((unsigned char*) merge_key) & l->hashmask;
295 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
297 // We found a matching record. Merge them
298 if (!strcmp(merge_key, (*p)->record->merge_key))
300 struct record_cluster *existing = (*p)->record;
301 record->next = existing->records;
302 existing->records = record;
307 if (!cluster && l->num_records < l->flatlist_size)
309 struct reclist_bucket *new =
310 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
311 struct record_cluster *newc =
312 nmem_malloc(l->nmem, sizeof(struct record_cluster));
317 newc->records = record;
318 newc->merge_key = merge_key;
320 newc->term_frequency_vec = 0;
321 newc->recid = merge_key;
323 newc->metadata = nmem_malloc(l->nmem,
324 sizeof(struct record_metadata*) * service->num_metadata);
325 memset(newc->metadata, 0,
326 sizeof(struct record_metadata*) * service->num_metadata);
327 newc->sortkeys = nmem_malloc(l->nmem,
328 sizeof(struct record_metadata*) * service->num_sortkeys);
329 memset(newc->sortkeys, 0,
330 sizeof(union data_types*) * service->num_sortkeys);
333 l->flatlist[l->num_records++] = newc;
343 * c-file-style: "Stroustrup"
344 * indent-tabs-mode: nil
346 * vim: shiftwidth=4 tabstop=8 expandtab