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"
34 struct reclist_bucket **hashtable;
38 struct record_cluster **flatlist;
46 static struct reclist_sortparms *qsort_sortparms = 0; /* thread pr */
50 struct record_cluster *record;
51 struct reclist_bucket *next;
54 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
55 struct conf_service *service)
57 struct reclist_sortparms *res = 0;
58 struct reclist_sortparms **rp = &res;
60 if (strlen(parms) > 256)
70 enum conf_sortkey_type type;
71 struct reclist_sortparms *new;
73 if (!(cpp = strchr(parms, ',')))
74 cpp = parms + strlen(parms);
75 strncpy(parm, parms, cpp - parms);
76 parm[cpp-parms] = '\0';
78 if ((pp = strchr(parm, ':')))
80 increasing = pp[1] == '1' ? 1 : 0;
85 if (!strcmp(parm, "relevance"))
87 type = Metadata_sortkey_relevance;
91 for (i = 0; i < service->num_sortkeys; i++)
93 struct conf_sortkey *sk = &service->sortkeys[i];
94 if (!strcmp(sk->name, parm))
97 if (type == Metadata_sortkey_skiparticle)
98 type = Metadata_sortkey_string;
102 if (i >= service->num_sortkeys)
104 yaz_log(YLOG_FATAL, "Bad sortkey: %s", parm);
110 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
112 new->offset = offset;
114 new->increasing = increasing;
122 static int reclist_cmp(const void *p1, const void *p2)
124 struct record_cluster *r1 = (*(struct record_cluster**) p1);
125 struct record_cluster *r2 = (*(struct record_cluster**) p2);
126 struct reclist_sortparms *s;
129 for (s = qsort_sortparms; s && res == 0; s = s->next)
131 union data_types *ut1 = r1->sortkeys[s->offset];
132 union data_types *ut2 = r2->sortkeys[s->offset];
137 case Metadata_sortkey_relevance:
138 res = r2->relevance - r1->relevance;
140 case Metadata_sortkey_string:
141 s1 = ut1 ? ut1->text.sort : "";
142 s2 = ut2 ? ut2->text.sort : "";
143 res = strcmp(s2, s1);
150 case Metadata_sortkey_numeric:
154 res = ut1->number.min - ut2->number.min;
156 res = ut2->number.max - ut1->number.max;
158 else if (ut1 && !ut2)
160 else if (!ut1 && ut2)
166 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
173 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
175 qsort_sortparms = parms;
176 qsort(l->flatlist, l->num_records,
177 sizeof(struct record_cluster*), reclist_cmp);
181 struct record_cluster *reclist_read_record(struct reclist *l)
183 if (l && l->pointer < l->num_records)
184 return l->flatlist[l->pointer++];
189 void reclist_rewind(struct reclist *l)
195 struct reclist *reclist_create(NMEM nmem, int numrecs)
201 while (hashsize < numrecs)
203 res = nmem_malloc(nmem, sizeof(struct reclist));
205 = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
206 memset(res->hashtable, 0, hashsize * sizeof(struct reclist_bucket*));
207 res->hashtable_size = hashsize;
209 res->hashmask = hashsize - 1; // Creates a bitmask
211 res->num_records = 0;
212 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record_cluster*));
213 res->flatlist_size = numrecs;
218 int reclist_get_num_records(struct reclist *l)
221 return l->num_records;
225 struct record_cluster *reclist_get_cluster(struct reclist *l, int i)
227 return l->flatlist[i];
230 // Insert a record. Return record cluster (newly formed or pre-existing)
231 struct record_cluster *reclist_insert( struct reclist *l,
232 struct conf_service *service,
233 struct record *record,
234 char *merge_key, int *total)
237 struct reclist_bucket **p;
238 struct record_cluster *cluster = 0;
246 bucket = jenkins_hash((unsigned char*) merge_key) & l->hashmask;
248 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
250 // We found a matching record. Merge them
251 if (!strcmp(merge_key, (*p)->record->merge_key))
253 struct record_cluster *existing = (*p)->record;
254 record->next = existing->records;
255 existing->records = record;
260 if (!cluster && l->num_records < l->flatlist_size)
262 struct reclist_bucket *new =
263 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
264 struct record_cluster *newc =
265 nmem_malloc(l->nmem, sizeof(struct record_cluster));
270 newc->records = record;
271 newc->merge_key = merge_key;
273 newc->term_frequency_vec = 0;
274 newc->recid = merge_key;
276 newc->metadata = nmem_malloc(l->nmem,
277 sizeof(struct record_metadata*) * service->num_metadata);
278 memset(newc->metadata, 0,
279 sizeof(struct record_metadata*) * service->num_metadata);
280 newc->sortkeys = nmem_malloc(l->nmem,
281 sizeof(struct record_metadata*) * service->num_sortkeys);
282 memset(newc->sortkeys, 0,
283 sizeof(union data_types*) * service->num_sortkeys);
286 l->flatlist[l->num_records++] = newc;
296 * c-file-style: "Stroustrup"
297 * indent-tabs-mode: nil
299 * vim: shiftwidth=4 tabstop=8 expandtab