/* This file is part of Pazpar2.
- Copyright (C) 2006-2010 Index Data
+ Copyright (C) 2006-2011 Index Data
Pazpar2 is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
#include <yaz/yaz-util.h>
-#include "pazpar2.h"
+#include "ppmutex.h"
+#include "session.h"
#include "reclists.h"
#include "jenkins_hash.h"
YAZ_MUTEX mutex;
};
-static struct reclist_sortparms *qsort_sortparms = 0; /* thread pr */
-
struct reclist_bucket
{
struct record_cluster *record;
struct reclist_bucket *hnext;
struct reclist_bucket *snext;
+ struct reclist_sortparms *sort_parms;
};
struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
if (!strcmp(parm, "relevance"))
{
type = Metadata_sortkey_relevance;
+ }
+ else if (!strcmp(parm, "position"))
+ {
+ type = Metadata_sortkey_position;
}
else
{
new->offset = offset;
new->type = type;
new->increasing = increasing;
+ new->name = nmem_strdup(nmem, parm);
rp = &new->next;
if (*(parms = cpp))
parms++;
static int reclist_cmp(const void *p1, const void *p2)
{
+ struct reclist_sortparms *sortparms =
+ (*(struct reclist_bucket **) p1)->sort_parms;
struct record_cluster *r1 = (*(struct reclist_bucket**) p1)->record;
struct record_cluster *r2 = (*(struct reclist_bucket**) p2)->record;
struct reclist_sortparms *s;
int res = 0;
- for (s = qsort_sortparms; s && res == 0; s = s->next)
+ for (s = sortparms; s && res == 0; s = s->next)
{
union data_types *ut1 = r1->sortkeys[s->offset];
union data_types *ut2 = r2->sortkeys[s->offset];
else
res = 0;
break;
+ case Metadata_sortkey_position:
+ if (r1->records && r2->records)
+ {
+ int pos1 = 0, pos2 = 0;
+ struct record *rec;
+ for (rec = r1->records; rec; rec = rec->next)
+ if (pos1 == 0 || rec->position < pos1)
+ pos1 = rec->position;
+ for (rec = r2->records; rec; rec = rec->next)
+ if (pos2 == 0 || rec->position < pos2)
+ pos2 = rec->position;
+ if (s->increasing)
+ res = pos1 - pos2;
+ else
+ res = pos2 - pos1;
+ }
+ break;
default:
yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
res = 0;
void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
{
-
struct reclist_bucket **flatlist = xmalloc(sizeof(*flatlist) * l->num_records);
struct reclist_bucket *ptr;
struct reclist_bucket **prev;
prev = &l->sorted_list;
while (ptr)
{
+ ptr->sort_parms = parms;
flatlist[i] = ptr;
ptr = ptr->snext;
i++;
}
assert(i == l->num_records);
- qsort_sortparms = parms;
qsort(flatlist, l->num_records, sizeof(*flatlist), reclist_cmp);
for (i = 0; i < l->num_records; i++)
{
res->num_records = 0;
res->mutex = 0;
- yaz_mutex_create(&res->mutex);
+ pazpar2_mutex_create(&res->mutex, "reclist");
return res;
}
void reclist_destroy(struct reclist *l)
{
- yaz_mutex_destroy(&l->mutex);
+ if (l)
+ yaz_mutex_destroy(&l->mutex);
}
int reclist_get_num_records(struct reclist *l)
}
// Insert a record. Return record cluster (newly formed or pre-existing)
-struct record_cluster *reclist_insert( struct reclist *l,
- struct conf_service *service,
- struct record *record,
- char *merge_key, int *total)
+struct record_cluster *reclist_insert(struct reclist *l,
+ struct conf_service *service,
+ struct record *record,
+ const char *merge_key, int *total)
{
unsigned int bucket;
struct reclist_bucket **p;
if (!strcmp(merge_key, (*p)->record->merge_key))
{
struct record_cluster *existing = (*p)->record;
+ struct record *re = existing->records;
+
+ for (; re; re = re->next)
+ {
+ if (re->client == record->client &&
+ record_compare(record, re, service))
+ {
+ yaz_mutex_leave(l->mutex);
+ return 0;
+ }
+ }
record->next = existing->records;
existing->records = record;
cluster = existing;
new->record = cluster;
new->hnext = 0;
cluster->records = record;
- cluster->merge_key = merge_key;
+ cluster->merge_key = nmem_strdup(l->nmem, merge_key);
cluster->relevance_score = 0;
cluster->term_frequency_vec = 0;
- cluster->recid = merge_key;
+ cluster->recid = nmem_strdup(l->nmem, merge_key);
(*total)++;
cluster->metadata =
nmem_malloc(l->nmem,