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;
47 struct record_cluster *record;
48 struct reclist_bucket *hash_next;
49 struct reclist_bucket *sorted_next;
50 struct reclist_sortparms *sort_parms;
53 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
54 struct conf_service *service)
56 struct reclist_sortparms *res = 0;
57 struct reclist_sortparms **rp = &res;
59 if (strlen(parms) > 256)
69 enum conf_sortkey_type type = Metadata_sortkey_string;
70 struct reclist_sortparms *new;
72 if (!(cpp = strchr(parms, ',')))
73 cpp = parms + strlen(parms);
74 strncpy(parm, parms, cpp - parms);
75 parm[cpp-parms] = '\0';
77 if ((pp = strchr(parm, ':')))
81 else if (pp[1] == '0')
85 yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
92 type = Metadata_sortkey_position;
94 yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
98 if (type != Metadata_sortkey_position)
100 if (!strcmp(parm, "relevance"))
102 type = Metadata_sortkey_relevance;
104 else if (!strcmp(parm, "position"))
106 type = Metadata_sortkey_position;
110 for (i = 0; i < service->num_sortkeys; i++)
112 struct conf_sortkey *sk = &service->sortkeys[i];
113 if (!strcmp(sk->name, parm))
116 if (type == Metadata_sortkey_skiparticle)
117 type = Metadata_sortkey_string;
121 if (i >= service->num_sortkeys)
123 yaz_log(YLOG_FATAL, "Sortkey not defined in service: %s",
130 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
132 new->offset = offset;
134 new->increasing = increasing;
135 new->name = nmem_strdup(nmem, parm);
143 static int reclist_cmp(const void *p1, const void *p2)
145 struct reclist_sortparms *sortparms =
146 (*(struct reclist_bucket **) p1)->sort_parms;
147 struct record_cluster *r1 = (*(struct reclist_bucket**) p1)->record;
148 struct record_cluster *r2 = (*(struct reclist_bucket**) p2)->record;
149 struct reclist_sortparms *s;
152 for (s = sortparms; s && res == 0; s = s->next)
154 union data_types *ut1 = r1->sortkeys[s->offset];
155 union data_types *ut2 = r2->sortkeys[s->offset];
159 case Metadata_sortkey_relevance:
160 res = r2->relevance_score - r1->relevance_score;
162 case Metadata_sortkey_string:
163 s1 = ut1 ? ut1->text.sort : "";
164 s2 = ut2 ? ut2->text.sort : "";
165 res = strcmp(s2, s1);
172 case Metadata_sortkey_numeric:
176 res = ut1->number.min - ut2->number.min;
178 res = ut2->number.max - ut1->number.max;
180 else if (ut1 && !ut2)
182 else if (!ut1 && ut2)
187 case Metadata_sortkey_position:
188 if (r1->records && r2->records)
190 int pos1 = 0, pos2 = 0;
192 for (rec = r1->records; rec; rec = rec->next)
193 if (pos1 == 0 || rec->position < pos1)
194 pos1 = rec->position;
195 for (rec = r2->records; rec; rec = rec->next)
196 if (pos2 == 0 || rec->position < pos2)
197 pos2 = rec->position;
202 yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
208 res = strcmp(r1->recid, r2->recid);
212 void reclist_limit(struct reclist *l, struct session *se)
216 struct reclist_bucket **pp = &l->sorted_list;
219 for (i = 0; i < l->hash_size; i++)
221 struct reclist_bucket *p;
222 for (p = l->hashtable[i]; p; p = p->hash_next)
224 if (session_check_cluster_limit(se, p->record))
227 pp = &p->sorted_next;
232 yaz_log(YLOG_LOG, "session_check_cluster returned false");
237 l->num_records = num;
241 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
243 struct reclist_bucket **flatlist = xmalloc(sizeof(*flatlist) * l->num_records);
244 struct reclist_bucket *ptr;
245 struct reclist_bucket **prev;
250 ptr = l->sorted_list;
251 prev = &l->sorted_list;
254 ptr->sort_parms = parms;
256 ptr = ptr->sorted_next;
259 assert(i == l->num_records);
261 qsort(flatlist, l->num_records, sizeof(*flatlist), reclist_cmp);
262 for (i = 0; i < l->num_records; i++)
265 prev = &flatlist[i]->sorted_next;
274 struct record_cluster *reclist_read_record(struct reclist *l)
276 if (l && l->sorted_ptr)
278 struct record_cluster *t = l->sorted_ptr->record;
279 l->sorted_ptr = l->sorted_ptr->sorted_next;
286 void reclist_enter(struct reclist *l)
288 yaz_mutex_enter(l->mutex);
290 l->sorted_ptr = l->sorted_list;
294 void reclist_leave(struct reclist *l)
296 yaz_mutex_leave(l->mutex);
298 l->sorted_ptr = l->sorted_list;
302 struct reclist *reclist_create(NMEM nmem)
304 struct reclist *res = nmem_malloc(nmem, sizeof(struct reclist));
305 res->hash_size = 399;
307 = nmem_malloc(nmem, res->hash_size * sizeof(struct reclist_bucket*));
308 memset(res->hashtable, 0, res->hash_size * sizeof(struct reclist_bucket*));
312 res->sorted_list = 0;
314 res->num_records = 0;
316 pazpar2_mutex_create(&res->mutex, "reclist");
320 void reclist_destroy(struct reclist *l)
325 for (i = 0; i < l->hash_size; i++)
327 struct reclist_bucket *p;
328 for (p = l->hashtable[i]; p; p = p->hash_next)
330 wrbuf_destroy(p->record->relevance_explain1);
331 wrbuf_destroy(p->record->relevance_explain2);
334 yaz_mutex_destroy(&l->mutex);
338 int reclist_get_num_records(struct reclist *l)
341 return l->num_records;
345 // Insert a record. Return record cluster (newly formed or pre-existing)
346 struct record_cluster *reclist_insert(struct reclist *l,
347 struct conf_service *service,
348 struct record *record,
349 const char *merge_key, int *total)
352 struct reclist_bucket **p;
353 struct record_cluster *cluster = 0;
361 bucket = jenkins_hash((unsigned char*) merge_key) % l->hash_size;
363 yaz_mutex_enter(l->mutex);
364 for (p = &l->hashtable[bucket]; *p; p = &(*p)->hash_next)
366 // We found a matching record. Merge them
367 if (!strcmp(merge_key, (*p)->record->merge_key))
369 struct record_cluster *existing = (*p)->record;
370 struct record *re = existing->records;
372 for (; re; re = re->next)
374 if (re->client == record->client &&
375 record_compare(record, re, service))
377 yaz_mutex_leave(l->mutex);
381 record->next = existing->records;
382 existing->records = record;
389 struct reclist_bucket *new =
390 nmem_malloc(l->nmem, sizeof(*new));
392 cluster = nmem_malloc(l->nmem, sizeof(*cluster));
395 new->record = cluster;
397 cluster->records = record;
398 cluster->merge_key = nmem_strdup(l->nmem, merge_key);
399 cluster->relevance_score = 0;
400 cluster->term_frequency_vec = 0;
401 cluster->recid = nmem_strdup(l->nmem, merge_key);
405 sizeof(struct record_metadata*) * service->num_metadata);
406 memset(cluster->metadata, 0,
407 sizeof(struct record_metadata*) * service->num_metadata);
409 nmem_malloc(l->nmem, sizeof(struct record_metadata*) * service->num_sortkeys);
410 memset(cluster->sortkeys, 0,
411 sizeof(union data_types*) * service->num_sortkeys);
413 cluster->relevance_explain1 = wrbuf_alloc();
414 cluster->relevance_explain2 = wrbuf_alloc();
415 /* attach to hash list */
419 yaz_mutex_leave(l->mutex);
423 int reclist_sortparms_cmp(struct reclist_sortparms *sort1, struct reclist_sortparms *sort2)
428 if (sort1 == 0 || sort2 == 0)
430 rc = strcmp(sort1->name, sort2->name) || sort1->increasing != sort2->increasing || sort1->type != sort2->type;
436 * c-file-style: "Stroustrup"
437 * indent-tabs-mode: nil
439 * vim: shiftwidth=4 tabstop=8 expandtab