/* This file is part of Pazpar2.
- Copyright (C) 2006-2012 Index Data
+ Copyright (C) 2006-2013 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
int num_records;
struct reclist_bucket *sorted_list;
struct reclist_bucket *sorted_ptr;
- struct reclist_bucket **last;
NMEM nmem;
YAZ_MUTEX mutex;
};
struct reclist_bucket
{
struct record_cluster *record;
- struct reclist_bucket *hnext;
- struct reclist_bucket *snext;
+ struct reclist_bucket *hash_next;
+ struct reclist_bucket *sorted_next;
struct reclist_sortparms *sort_parms;
};
+static void append_merge_keys(struct record_metadata_attr **p,
+ struct record_metadata_attr *a,
+ NMEM nmem)
+{
+ while (*p)
+ p = &(*p)->next;
+ for (; a; a = a->next)
+ {
+ *p = (struct record_metadata_attr *) nmem_malloc(nmem, sizeof(**p));
+ (*p)->name = nmem_strdup_null(nmem, a->name);
+ (*p)->value = nmem_strdup_null(nmem, a->value);
+ p = &(*p)->next;
+ }
+ *p = 0;
+}
+
struct reclist_sortparms *reclist_parse_sortparms(NMEM nmem, const char *parms,
struct conf_service *service)
{
if (!(cpp = strchr(parms, ',')))
cpp = parms + strlen(parms);
- strncpy(parm, parms, cpp - parms);
+ strncpy(parm, parms, cpp - parms);
parm[cpp-parms] = '\0';
if ((pp = strchr(parm, ':')))
yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
return 0;
}
-
+
if (pp[2])
{
if (pp[2] == 'p')
if (!strcmp(parm, "relevance"))
{
type = Metadata_sortkey_relevance;
- }
+ }
else if (!strcmp(parm, "position"))
{
type = Metadata_sortkey_position;
static int reclist_cmp(const void *p1, const void *p2)
{
- struct reclist_sortparms *sortparms =
+ 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;
return res;
}
+void reclist_limit(struct reclist *l, struct session *se, int lazy)
+{
+ unsigned i;
+ int num = 0;
+ struct reclist_bucket **pp = &l->sorted_list;
+
+ reclist_enter(l);
+
+ if (!lazy || !*pp)
+ {
+ for (i = 0; i < l->hash_size; i++)
+ {
+ struct reclist_bucket *p;
+ for (p = l->hashtable[i]; p; p = p->hash_next)
+ {
+ if (session_check_cluster_limit(se, p->record))
+ {
+ *pp = p;
+ pp = &p->sorted_next;
+ num++;
+ }
+ }
+ }
+ *pp = 0;
+ }
+ l->num_records = num;
+ reclist_leave(l);
+}
+
void reclist_sort(struct reclist *l, struct reclist_sortparms *parms)
{
struct reclist_bucket **flatlist = xmalloc(sizeof(*flatlist) * l->num_records);
{
ptr->sort_parms = parms;
flatlist[i] = ptr;
- ptr = ptr->snext;
+ ptr = ptr->sorted_next;
i++;
}
assert(i == l->num_records);
for (i = 0; i < l->num_records; i++)
{
*prev = flatlist[i];
- prev = &flatlist[i]->snext;
+ prev = &flatlist[i]->sorted_next;
}
*prev = 0;
- l->last = prev;
xfree(flatlist);
if (l && l->sorted_ptr)
{
struct record_cluster *t = l->sorted_ptr->record;
- l->sorted_ptr = l->sorted_ptr->snext;
+ l->sorted_ptr = l->sorted_ptr->sorted_next;
return t;
}
else
{
struct reclist *res = nmem_malloc(nmem, sizeof(struct reclist));
res->hash_size = 399;
- res->hashtable
+ res->hashtable
= nmem_malloc(nmem, res->hash_size * sizeof(struct reclist_bucket*));
memset(res->hashtable, 0, res->hash_size * sizeof(struct reclist_bucket*));
res->nmem = nmem;
res->sorted_ptr = 0;
res->sorted_list = 0;
- res->last = &res->sorted_list;
res->num_records = 0;
res->mutex = 0;
void reclist_destroy(struct reclist *l)
{
if (l)
+ {
+ unsigned i;
+ for (i = 0; i < l->hash_size; i++)
+ {
+ struct reclist_bucket *p;
+ for (p = l->hashtable[i]; p; p = p->hash_next)
+ {
+ wrbuf_destroy(p->record->relevance_explain1);
+ wrbuf_destroy(p->record->relevance_explain2);
+ }
+ }
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 conf_service *service,
struct record *record,
- const char *merge_key, int *total)
+ struct record_metadata_attr *merge_keys,
+ int *total)
{
- unsigned int bucket;
- struct reclist_bucket **p;
struct record_cluster *cluster = 0;
-
+ struct record_metadata_attr *mkl = merge_keys;
+ struct reclist_bucket **p;
+
assert(service);
assert(l);
assert(record);
- assert(merge_key);
+ assert(merge_keys);
assert(total);
- bucket = jenkins_hash((unsigned char*) merge_key) % l->hash_size;
-
yaz_mutex_enter(l->mutex);
- for (p = &l->hashtable[bucket]; *p; p = &(*p)->hnext)
+
+ for (; mkl; mkl = mkl->next)
{
- // We found a matching record. Merge them
- if (!strcmp(merge_key, (*p)->record->merge_key))
- {
- struct record_cluster *existing = (*p)->record;
- struct record *re = existing->records;
+ const char *merge_key = mkl->value;
+ unsigned int bucket =
+ jenkins_hash((unsigned char*) merge_key) % l->hash_size;
- for (; re; re = re->next)
+ for (p = &l->hashtable[bucket]; *p; p = &(*p)->hash_next)
+ {
+ struct record_metadata_attr *mkr = (*p)->record->merge_keys;
+ for (; mkr; mkr = mkr->next)
{
- if (re->client == record->client &&
- record_compare(record, re, service))
- {
- yaz_mutex_leave(l->mutex);
- return 0;
+ // We found a matching record. Merge them
+ if (!strcmp(merge_key, mkr->value))
+ {
+ struct record **re;
+
+ cluster = (*p)->record;
+ for (re = &cluster->records; *re; re = &(*re)->next)
+ {
+ if ((*re)->client == record->client &&
+ record_compare(record, *re, service))
+ {
+ yaz_mutex_leave(l->mutex);
+ return 0;
+ }
+ }
+ *re = record;
+ record->next = 0;
+ goto out;
}
}
- record->next = existing->records;
- existing->records = record;
- cluster = existing;
- break;
}
}
+out:
if (!cluster)
{
struct reclist_bucket *new =
nmem_malloc(l->nmem, sizeof(*new));
cluster = nmem_malloc(l->nmem, sizeof(*cluster));
-
+
record->next = 0;
new->record = cluster;
- new->hnext = 0;
+ new->hash_next = 0;
cluster->records = record;
- cluster->merge_key = nmem_strdup(l->nmem, merge_key);
+
+ cluster->merge_keys = 0;
+ append_merge_keys(&cluster->merge_keys, merge_keys, l->nmem);
+
cluster->relevance_score = 0;
cluster->term_frequency_vec = 0;
- cluster->recid = nmem_strdup(l->nmem, merge_key);
+ cluster->recid = cluster->merge_keys->value;
(*total)++;
cluster->metadata =
nmem_malloc(l->nmem,
sizeof(struct record_metadata*) * service->num_metadata);
- memset(cluster->metadata, 0,
+ memset(cluster->metadata, 0,
sizeof(struct record_metadata*) * service->num_metadata);
- cluster->sortkeys =
+ cluster->sortkeys =
nmem_malloc(l->nmem, sizeof(struct record_metadata*) * service->num_sortkeys);
memset(cluster->sortkeys, 0,
sizeof(union data_types*) * service->num_sortkeys);
-
- /* attach to hash list */
- *p = new;
-
- /* append to sorted_list */
- *l->last = new;
- l->last = &new->snext;
- l->sorted_ptr = l->sorted_list;
- new->snext = 0;
+ cluster->relevance_explain1 = wrbuf_alloc();
+ cluster->relevance_explain2 = wrbuf_alloc();
+ /* attach to hash list */
+ *p = new;
l->num_records++;
+ l->sorted_list = l->sorted_ptr = 0;
}
yaz_mutex_leave(l->mutex);
return cluster;
}
+int reclist_sortparms_cmp(struct reclist_sortparms *sort1, struct reclist_sortparms *sort2)
+{
+ int rc;
+ if (sort1 == sort2)
+ return 0;
+ if (sort1 == 0 || sort2 == 0)
+ return 1;
+ rc = strcmp(sort1->name, sort2->name) || sort1->increasing != sort2->increasing || sort1->type != sort2->type;
+ return rc;
+}
/*
* Local variables:
* c-basic-offset: 4