1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 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 #include "jenkins_hash.h"
35 struct reclist_bucket **hashtable;
39 struct reclist_bucket *sorted_list;
40 struct reclist_bucket *sorted_ptr;
41 struct reclist_bucket **last;
48 struct record_cluster *record;
49 struct reclist_bucket *hnext;
50 struct reclist_bucket *snext;
51 struct reclist_sortparms *sort_parms;
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 = Metadata_sortkey_string;
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, ':')))
82 else if (pp[1] == '0')
86 yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
93 type = Metadata_sortkey_position;
95 yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
99 if (type != Metadata_sortkey_position)
101 if (!strcmp(parm, "relevance"))
103 type = Metadata_sortkey_relevance;
105 else if (!strcmp(parm, "position"))
107 type = Metadata_sortkey_position;
111 for (i = 0; i < service->num_sortkeys; i++)
113 struct conf_sortkey *sk = &service->sortkeys[i];
114 if (!strcmp(sk->name, parm))
117 if (type == Metadata_sortkey_skiparticle)
118 type = Metadata_sortkey_string;
122 if (i >= service->num_sortkeys)
124 yaz_log(YLOG_FATAL, "Sortkey not defined in service: %s",
131 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
133 new->offset = offset;
135 new->increasing = increasing;
136 new->name = nmem_strdup(nmem, parm);
144 static int reclist_cmp(const void *p1, const void *p2)
146 struct reclist_sortparms *sortparms =
147 (*(struct reclist_bucket **) p1)->sort_parms;
148 struct record_cluster *r1 = (*(struct reclist_bucket**) p1)->record;
149 struct record_cluster *r2 = (*(struct reclist_bucket**) p2)->record;
150 struct reclist_sortparms *s;
153 for (s = sortparms; s && res == 0; s = s->next)
155 union data_types *ut1 = r1->sortkeys[s->offset];
156 union data_types *ut2 = r2->sortkeys[s->offset];
160 case Metadata_sortkey_relevance:
161 res = r2->relevance_score - r1->relevance_score;
163 case Metadata_sortkey_string:
164 s1 = ut1 ? ut1->text.sort : "";
165 s2 = ut2 ? ut2->text.sort : "";
166 res = strcmp(s2, s1);
173 case Metadata_sortkey_numeric:
177 res = ut1->number.min - ut2->number.min;
179 res = ut2->number.max - ut1->number.max;
181 else if (ut1 && !ut2)
183 else if (!ut1 && ut2)
188 case Metadata_sortkey_position:
189 if (r1->records && r2->records)
191 int pos1 = 0, pos2 = 0;
193 for (rec = r1->records; rec; rec = rec->next)
194 if (pos1 == 0 || rec->position < pos1)
195 pos1 = rec->position;
196 for (rec = r2->records; rec; rec = rec->next)
197 if (pos2 == 0 || rec->position < pos2)
198 pos2 = rec->position;
203 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
209 res = strcmp(r1->recid, r2->recid);
213 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
215 struct reclist_bucket **flatlist = xmalloc(sizeof(*flatlist) * l->num_records);
216 struct reclist_bucket *ptr;
217 struct reclist_bucket **prev;
222 ptr = l->sorted_list;
223 prev = &l->sorted_list;
226 ptr->sort_parms = parms;
231 assert(i == l->num_records);
233 qsort(flatlist, l->num_records, sizeof(*flatlist), reclist_cmp);
234 for (i = 0; i < l->num_records; i++)
237 prev = &flatlist[i]->snext;
247 struct record_cluster *reclist_read_record(struct reclist *l)
249 if (l && l->sorted_ptr)
251 struct record_cluster *t = l->sorted_ptr->record;
252 l->sorted_ptr = l->sorted_ptr->snext;
259 void reclist_enter(struct reclist *l)
261 yaz_mutex_enter(l->mutex);
263 l->sorted_ptr = l->sorted_list;
267 void reclist_leave(struct reclist *l)
269 yaz_mutex_leave(l->mutex);
271 l->sorted_ptr = l->sorted_list;
275 struct reclist *reclist_create(NMEM nmem)
277 struct reclist *res = nmem_malloc(nmem, sizeof(struct reclist));
278 res->hash_size = 399;
280 = nmem_malloc(nmem, res->hash_size * sizeof(struct reclist_bucket*));
281 memset(res->hashtable, 0, res->hash_size * sizeof(struct reclist_bucket*));
285 res->sorted_list = 0;
286 res->last = &res->sorted_list;
288 res->num_records = 0;
290 pazpar2_mutex_create(&res->mutex, "reclist");
294 void reclist_destroy(struct reclist *l)
298 struct reclist_bucket *rb;
300 for (rb = l->sorted_list; rb; rb = rb->snext)
302 wrbuf_destroy(rb->record->relevance_explain1);
303 wrbuf_destroy(rb->record->relevance_explain2);
305 yaz_mutex_destroy(&l->mutex);
309 int reclist_get_num_records(struct reclist *l)
312 return l->num_records;
316 // Insert a record. Return record cluster (newly formed or pre-existing)
317 struct record_cluster *reclist_insert(struct reclist *l,
318 struct conf_service *service,
319 struct record *record,
320 const char *merge_key, int *total)
323 struct reclist_bucket **p;
324 struct record_cluster *cluster = 0;
332 bucket = jenkins_hash((unsigned char*) merge_key) % l->hash_size;
334 yaz_mutex_enter(l->mutex);
335 for (p = &l->hashtable[bucket]; *p; p = &(*p)->hnext)
337 // We found a matching record. Merge them
338 if (!strcmp(merge_key, (*p)->record->merge_key))
340 struct record_cluster *existing = (*p)->record;
341 struct record *re = existing->records;
343 for (; re; re = re->next)
345 if (re->client == record->client &&
346 record_compare(record, re, service))
348 yaz_mutex_leave(l->mutex);
352 record->next = existing->records;
353 existing->records = record;
360 struct reclist_bucket *new =
361 nmem_malloc(l->nmem, sizeof(*new));
363 cluster = nmem_malloc(l->nmem, sizeof(*cluster));
366 new->record = cluster;
368 cluster->records = record;
369 cluster->merge_key = nmem_strdup(l->nmem, merge_key);
370 cluster->relevance_score = 0;
371 cluster->term_frequency_vec = 0;
372 cluster->recid = nmem_strdup(l->nmem, merge_key);
376 sizeof(struct record_metadata*) * service->num_metadata);
377 memset(cluster->metadata, 0,
378 sizeof(struct record_metadata*) * service->num_metadata);
380 nmem_malloc(l->nmem, sizeof(struct record_metadata*) * service->num_sortkeys);
381 memset(cluster->sortkeys, 0,
382 sizeof(union data_types*) * service->num_sortkeys);
384 cluster->relevance_explain1 = wrbuf_alloc();
385 cluster->relevance_explain2 = wrbuf_alloc();
386 /* attach to hash list */
389 /* append to sorted_list */
391 l->last = &new->snext;
392 l->sorted_ptr = l->sorted_list;
397 yaz_mutex_leave(l->mutex);
401 int reclist_sortparms_cmp(struct reclist_sortparms *sort1, struct reclist_sortparms *sort2)
406 if (sort1 == 0 || sort2 == 0)
408 rc = strcmp(sort1->name, sort2->name) || sort1->increasing != sort2->increasing || sort1->type != sort2->type;
414 * c-file-style: "Stroustrup"
415 * indent-tabs-mode: nil
417 * vim: shiftwidth=4 tabstop=8 expandtab