1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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 record_cluster *sorted_list;
40 struct record_cluster *sorted_ptr;
47 struct record_cluster *record;
48 struct reclist_bucket *hash_next;
51 static void append_merge_keys(struct record_metadata_attr **p,
52 const struct record_metadata_attr *a,
55 for (; a; a = a->next)
57 struct record_metadata_attr **pi = p;
58 for (; *pi; pi = &(*pi)->next)
59 if (!strcmp((*pi)->value, a->value))
63 *pi = (struct record_metadata_attr *) nmem_malloc(nmem, sizeof(**p));
64 (*pi)->name = nmem_strdup_null(nmem, a->name);
65 (*pi)->value = nmem_strdup_null(nmem, a->value);
71 struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
72 struct conf_service *service)
74 struct reclist_sortparms *res = 0;
75 struct reclist_sortparms **rp = &res;
77 if (strlen(parms) > 256)
87 enum conf_metadata_type type = Metadata_type_generic;
88 struct reclist_sortparms *new;
90 if (!(cpp = strchr(parms, ',')))
91 cpp = parms + strlen(parms);
92 strncpy(parm, parms, cpp - parms);
93 parm[cpp-parms] = '\0';
95 if ((pp = strchr(parm, ':')))
99 else if (pp[1] == '0')
103 yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
110 type = Metadata_type_position;
112 yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
116 if (type != Metadata_type_position)
118 if (!strcmp(parm, "relevance"))
120 type = Metadata_type_relevance;
122 else if (!strcmp(parm, "position"))
124 type = Metadata_type_position;
128 for (i = 0; i < service->num_sortkeys; i++)
130 struct conf_sortkey *sk = &service->sortkeys[i];
131 if (!strcmp(sk->name, parm))
137 if (i >= service->num_sortkeys)
139 yaz_log(YLOG_FATAL, "Sortkey not defined in service: %s",
146 new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
148 new->offset = offset;
150 new->increasing = increasing;
151 new->name = nmem_strdup(nmem, parm);
159 static int reclist_cmp(const void *p1, const void *p2)
161 struct record_cluster *r1 = (*(struct record_cluster**) p1);
162 struct record_cluster *r2 = (*(struct record_cluster**) p2);
163 struct reclist_sortparms *sortparms = r1->sort_parms;
164 struct reclist_sortparms *s;
167 for (s = sortparms; s && res == 0; s = s->next)
169 union data_types *ut1 = r1->sortkeys[s->offset];
170 union data_types *ut2 = r2->sortkeys[s->offset];
174 case Metadata_type_relevance:
175 res = r1->relevance_score - r2->relevance_score;
177 case Metadata_type_generic:
178 case Metadata_type_skiparticle:
179 s1 = ut1 ? ut1->text.sort : "";
180 s2 = ut2 ? ut2->text.sort : "";
181 res = strcmp(s1, s2);
183 case Metadata_type_year:
184 case Metadata_type_date:
188 res = ut1->number.min - ut2->number.min;
190 res = ut1->number.max - ut2->number.max;
192 else if (ut1 && !ut2)
194 res = -1; /* without date/year: last! */
197 else if (!ut1 && ut2)
199 res = 1; /* without date/year: last! */
205 case Metadata_type_position:
206 if (r1->records && r2->records)
208 int pos1 = 0, pos2 = 0;
210 for (rec = r1->records; rec; rec = rec->next)
211 if (pos1 == 0 || rec->position < pos1)
212 pos1 = rec->position;
213 for (rec = r2->records; rec; rec = rec->next)
214 if (pos2 == 0 || rec->position < pos2)
215 pos2 = rec->position;
220 case Metadata_type_float:
223 if (ut1->fnumber == ut2->fnumber)
225 else if (ut1->fnumber > ut2->fnumber)
238 if (res && !s->increasing)
242 res = strcmp(r1->recid, r2->recid);
246 void reclist_limit(struct reclist *l, struct session *se, int lazy)
250 struct record_cluster **pp = &l->sorted_list;
256 for (i = 0; i < l->hash_size; i++)
258 struct reclist_bucket *p;
259 for (p = l->hashtable[i]; p; p = p->hash_next)
260 p->record->sorted_next = 0;
262 for (i = 0; i < l->hash_size; i++)
264 struct reclist_bucket *p;
265 for (p = l->hashtable[i]; p; p = p->hash_next)
267 if (session_check_cluster_limit(se, p->record))
269 if (!p->record->sorted_next)
272 pp = &p->record->sorted_next;
273 *pp = p->record; /* signal already in use */
281 l->num_records = num;
285 void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
287 struct record_cluster **flatlist = xmalloc(sizeof(*flatlist) * l->num_records);
288 struct record_cluster *ptr;
289 struct record_cluster **prev;
294 ptr = l->sorted_list;
295 prev = &l->sorted_list;
298 ptr->sort_parms = parms;
300 ptr = ptr->sorted_next;
303 assert(i == l->num_records);
305 qsort(flatlist, l->num_records, sizeof(*flatlist), reclist_cmp);
306 for (i = 0; i < l->num_records; i++)
309 prev = &flatlist[i]->sorted_next;
318 struct record_cluster *reclist_read_record(struct reclist *l)
320 if (l && l->sorted_ptr)
322 struct record_cluster *t = l->sorted_ptr;
323 l->sorted_ptr = l->sorted_ptr->sorted_next;
330 void reclist_enter(struct reclist *l)
332 yaz_mutex_enter(l->mutex);
334 l->sorted_ptr = l->sorted_list;
338 void reclist_leave(struct reclist *l)
340 yaz_mutex_leave(l->mutex);
342 l->sorted_ptr = l->sorted_list;
346 struct reclist *reclist_create(NMEM nmem)
348 struct reclist *res = nmem_malloc(nmem, sizeof(struct reclist));
349 res->hash_size = 399;
351 = nmem_malloc(nmem, res->hash_size * sizeof(struct reclist_bucket*));
352 memset(res->hashtable, 0, res->hash_size * sizeof(struct reclist_bucket*));
356 res->sorted_list = 0;
358 res->num_records = 0;
360 pazpar2_mutex_create(&res->mutex, "reclist");
364 void reclist_destroy(struct reclist *l)
369 for (i = 0; i < l->hash_size; i++)
371 struct reclist_bucket *p;
372 for (p = l->hashtable[i]; p; p = p->hash_next)
374 wrbuf_destroy(p->record->relevance_explain1);
375 wrbuf_destroy(p->record->relevance_explain2);
376 p->record->relevance_explain1 = 0;
377 p->record->relevance_explain2 = 0;
380 yaz_mutex_destroy(&l->mutex);
384 int reclist_get_num_records(struct reclist *l)
387 return l->num_records;
391 static void merge_cluster(struct reclist *l,
393 struct record_cluster *dst,
394 struct record_cluster *src)
396 struct record_metadata_attr *mkl;
397 struct record **rp = &dst->records;
398 for (; *rp; rp = &(*rp)->next)
402 for (mkl = src->merge_keys; mkl; mkl = mkl->next)
404 const char *merge_key = mkl->value;
405 unsigned int bucket =
406 jenkins_hash((unsigned char*) merge_key) % l->hash_size;
407 struct reclist_bucket *p;
409 for (p = l->hashtable[bucket]; p; p = p->hash_next)
410 if (p->record == src)
414 /* not merging metadata and sortkeys yet */
416 relevance_mergerec(r, dst, src);
418 wrbuf_puts(dst->relevance_explain1, wrbuf_cstr(src->relevance_explain1));
419 wrbuf_puts(dst->relevance_explain2, wrbuf_cstr(src->relevance_explain2));
421 wrbuf_destroy(src->relevance_explain1);
422 src->relevance_explain1 = 0;
423 wrbuf_destroy(src->relevance_explain2);
424 src->relevance_explain2 = 0;
426 append_merge_keys(&dst->merge_keys, src->merge_keys, l->nmem);
429 static struct record_cluster *new_cluster(
432 struct conf_service *service,
433 struct record *record,
434 struct record_metadata_attr *merge_keys
437 struct record_cluster *cluster;
438 cluster = nmem_malloc(l->nmem, sizeof(*cluster));
441 cluster->records = record;
442 cluster->merge_keys = 0;
443 append_merge_keys(&cluster->merge_keys, merge_keys, l->nmem);
444 cluster->relevance_score = 0;
445 cluster->recid = cluster->merge_keys->value;
448 sizeof(struct record_metadata*) * service->num_metadata);
449 memset(cluster->metadata, 0,
450 sizeof(struct record_metadata*) * service->num_metadata);
452 nmem_malloc(l->nmem, sizeof(struct record_metadata*) * service->num_sortkeys);
453 memset(cluster->sortkeys, 0,
454 sizeof(union data_types*) * service->num_sortkeys);
455 relevance_newrec(r, cluster);
456 cluster->relevance_explain1 = wrbuf_alloc();
457 cluster->relevance_explain2 = wrbuf_alloc();
458 /* attach to hash list */
460 l->sorted_list = l->sorted_ptr = 0;
464 // Insert a record. Return record cluster (newly formed or pre-existing)
465 struct record_cluster *reclist_insert(struct reclist *l,
467 struct conf_service *service,
468 struct record *record,
469 struct record_metadata_attr *merge_keys,
472 struct record_cluster *cluster = 0;
473 struct record_metadata_attr *mkl = merge_keys;
481 yaz_mutex_enter(l->mutex);
483 for (; mkl; mkl = mkl->next)
485 const char *merge_key = mkl->value;
486 unsigned int bucket =
487 jenkins_hash((unsigned char*) merge_key) % l->hash_size;
488 struct reclist_bucket **p;
489 struct reclist_bucket *rb = 0;
491 for (p = &l->hashtable[bucket]; *p; p = &(*p)->hash_next)
493 struct record_metadata_attr *mkr = (*p)->record->merge_keys;
494 for (; mkr; mkr = mkr->next)
496 // We found a matching record. Merge them
497 if (!strcmp(merge_key, mkr->value))
503 for (re = &rb->record->records; *re; re = &(*re)->next)
505 if ((*re)->client == record->client &&
506 record_compare(record, *re, service))
508 yaz_mutex_leave(l->mutex);
512 cluster = rb->record;
518 if (cluster != rb->record)
520 assert(rb->record->relevance_explain1);
521 merge_cluster(l, r, cluster, rb->record);
531 cluster = new_cluster(l, r, service, record, merge_keys);
536 rb = nmem_malloc(l->nmem, sizeof(*rb));
537 rb->record = cluster;
543 yaz_mutex_leave(l->mutex);
547 int reclist_sortparms_cmp(struct reclist_sortparms *sort1, struct reclist_sortparms *sort2)
552 if (sort1 == 0 || sort2 == 0)
554 rc = strcmp(sort1->name, sort2->name) || sort1->increasing != sort2->increasing || sort1->type != sort2->type;
560 * c-file-style: "Stroustrup"
561 * indent-tabs-mode: nil
563 * vim: shiftwidth=4 tabstop=8 expandtab