/* 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
#include <yaz/querytowrbuf.h>
#include <yaz/oid_db.h>
#include <yaz/snprintf.h>
-#include <yaz/gettimeofday.h>
#define USE_TIMING 0
#if USE_TIMING
#include <libxml/tree.h>
-#define TERMLIST_HIGH_SCORE 25
-
#define MAX_CHUNK 15
#define MAX(a,b) ((a)>(b)?(a):(b))
// Note: Some things in this structure will eventually move to configuration
-struct parameters global_parameters =
+struct parameters global_parameters =
{
0, // dump_records
0, // debug_mode
xmlFree(result);
}
-static void session_enter(struct session *s)
+static void session_enter(struct session *s, const char *caller)
{
+ if (caller)
+ session_log(s, YLOG_DEBUG, "Session lock by %s", caller);
yaz_mutex_enter(s->session_mutex);
}
-static void session_leave(struct session *s)
+static void session_leave(struct session *s, const char *caller)
{
yaz_mutex_leave(s->session_mutex);
+ if (caller)
+ session_log(s, YLOG_DEBUG, "Session unlock by %s", caller);
}
static void session_normalize_facet(struct session *s, const char *type,
WRBUF display_wrbuf = wrbuf_alloc();
session_normalize_facet(s, type, value, display_wrbuf, facet_wrbuf);
-
+
if (wrbuf_len(facet_wrbuf))
{
int i;
wrbuf_destroy(display_wrbuf);
return;
}
-
+
s->termlists[i].name = nmem_strdup(s->nmem, type);
- s->termlists[i].termlist
- = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
+ s->termlists[i].termlist = termlist_create(s->nmem);
s->num_termlists = i + 1;
}
-
+
#if 0
- session_log(s, YLOG_DEBUG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
+ session_log(s, YLOG_LOG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
#endif
termlist_insert(s->termlists[i].termlist, wrbuf_cstr(display_wrbuf),
wrbuf_cstr(facet_wrbuf), count);
if (!rdoc)
{
- session_log(se, YLOG_FATAL, "Non-wellformed XML received from %s",
- db->id);
+ session_log(se, YLOG_WARN, "Non-wellformed XML");
return 0;
}
if (rdoc)
{
char *parms[MAX_XSLT_ARGS*2+1];
-
+
insert_settings_parameters(sdb, service, parms, nmem);
-
+
if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
{
- session_log(se, YLOG_WARN, "Normalize failed from %s",
- sdb->database->id);
+ session_log(se, YLOG_WARN, "Normalize failed");
}
else
{
insert_settings_values(sdb, rdoc, service);
-
+
if (global_parameters.dump_records)
{
- session_log(se, YLOG_LOG, "Normalized record from %s",
+ session_log(se, YLOG_LOG, "Normalized record from %s",
sdb->database->id);
log_xml_doc(rdoc);
}
const char *s;
if (sdb->settings[PZ_XSLT] &&
- (s = session_setting_oneval(sdb, PZ_XSLT)))
+ (s = session_setting_oneval(sdb, PZ_XSLT)))
{
char auto_stylesheet[256];
-
+
if (!strcmp(s, "auto"))
{
const char *request_syntax = session_setting_oneval(
}
// set watch. Returns 0=OK, -1 if watch is already set
-int session_set_watch(struct session *s, int what,
+int session_set_watch(struct session *s, int what,
session_watchfun fun, void *data,
struct http_channel *chan)
{
int ret;
- session_enter(s);
+ session_enter(s, "session_set_watch");
if (s->watchlist[what].fun)
ret = -1;
else
{
-
+
s->watchlist[what].fun = fun;
s->watchlist[what].data = data;
s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
session_watch_cancel);
ret = 0;
}
- session_leave(s);
+ session_leave(s, "session_set_watch");
return ret;
}
void session_alert_watch(struct session *s, int what)
{
assert(s);
- session_enter(s);
+ session_enter(s, "session_alert_watch");
if (s->watchlist[what].fun)
{
/* our watch is no longer associated with http_channel */
s->watchlist[what].data = 0;
s->watchlist[what].obs = 0;
- session_leave(s);
+ session_leave(s, "session_alert_watch");
session_log(s, YLOG_DEBUG,
"Alert Watch: %d calling function: %p", what, fun);
fun(data);
}
else
- session_leave(s);
+ session_leave(s,"session_alert_watch");
}
//callback for grep_databases
{
struct client_list *l;
- session_enter(se);
+ session_enter(se, "session_reset_active_clients");
l = se->clients_active;
se->clients_active = new_list;
- session_leave(se);
+ session_leave(se, "session_reset_active_clients");
while (l)
{
struct client_list *l_next = l->next;
- client_lock(l->client);
+ client_lock(l->client);
client_set_session(l->client, 0); /* mark client inactive */
client_unlock(l->client);
session_reset_active_clients(se, 0);
- session_enter(se);
+ session_enter(se, "session_remove_cached_clients");
l = se->clients_cached;
se->clients_cached = 0;
- session_leave(se);
+ session_leave(se, "session_remove_cached_clients");
while (l)
{
}
// Associates a set of clients with a session;
-// Note: Session-databases represent databases with per-session
+// Note: Session-databases represent databases with per-session
// setting overrides
static int select_targets(struct session *se, const char *filter)
{
return res == 0;
}
-static void session_clear_set(struct session *se,
- const char *sort_field, int increasing)
+static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
{
reclist_destroy(se->reclist);
se->reclist = 0;
nmem_reset(se->nmem);
se->total_records = se->total_merged = 0;
se->num_termlists = 0;
-
+
/* reset list of sorted results and clear to relevance search */
se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
- se->sorted_results->field = nmem_strdup(se->nmem, sort_field);
- se->sorted_results->increasing = increasing;
+ se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
+ se->sorted_results->increasing = sp->increasing;
+ se->sorted_results->type = sp->type;
se->sorted_results->next = 0;
-
+
+ session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
+ sp->name, sp->increasing, sp->type);
+
se->reclist = reclist_create(se->nmem);
}
-void session_sort(struct session *se, const char *field, int increasing,
- int clear_set)
+static void session_sort_unlocked(struct session *se, struct reclist_sortparms *sp)
{
- struct session_sorted_results *sr;
+ struct reclist_sortparms *sr;
struct client_list *l;
-
- session_enter(se);
-
- yaz_log(YLOG_LOG, "session_sort field=%s", field);
- if (clear_set)
+ const char *field = sp->name;
+ int increasing = sp->increasing;
+ int type = sp->type;
+ int clients_research = 0;
+
+ yaz_log(YLOG_LOG, "session_sort field=%s increasing=%d type=%d", field, increasing, type);
+ /* see if we already have sorted for this criteria */
+ for (sr = se->sorted_results; sr; sr = sr->next)
{
- session_clear_set(se, field, increasing);
+ if (!reclist_sortparms_cmp(sr,sp))
+ break;
}
- else
+ if (sr)
{
- /* see if we already have sorted for this critieria */
- for (sr = se->sorted_results; sr; sr = sr->next)
- {
- if (!strcmp(field, sr->field) && increasing == sr->increasing)
- break;
- }
- if (sr)
- {
- session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d already fetched",
- field, increasing);
- session_leave(se);
- return;
- }
- session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d must fetch",
- field, increasing);
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d already fetched",
+ field, increasing, type);
+ return;
+ }
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d must fetch",
+ field, increasing, type);
+
+ // We need to reset reclist on every sort that changes the records, not just for position
+ // So if just one client requires new searching, we need to clear set.
+ // Ask each of the client if sorting requires re-search due to native sort
+ // If it does it will require us to
+ for (l = se->clients_active; l; l = l->next)
+ {
+ struct client *cl = l->client;
+ // Assume no re-search is required.
+ client_parse_init(cl, 1);
+ clients_research += client_parse_sort(cl, sp);
+ }
+ if (clients_research) {
+ yaz_log(YLOG_DEBUG, "Reset results due to %d clients researching", clients_research);
+ session_clear_set(se, sp);
+ }
+ else {
+ // A new sorting based on same record set
sr = nmem_malloc(se->nmem, sizeof(*sr));
- sr->field = nmem_strdup(se->nmem, field);
+ sr->name = nmem_strdup(se->nmem, field);
sr->increasing = increasing;
+ sr->type = type;
sr->next = se->sorted_results;
se->sorted_results = sr;
+ session_log(se, YLOG_DEBUG, "No research/ingesting done");
+ return ;
}
-
+ session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
+
for (l = se->clients_active; l; l = l->next)
{
struct client *cl = l->client;
if (client_get_state(cl) == Client_Connecting ||
client_get_state(cl) == Client_Idle ||
- client_get_state(cl) == Client_Working)
+ client_get_state(cl) == Client_Working) {
client_start_search(cl);
+ }
+ else {
+ yaz_log(YLOG_DEBUG, "Client %s: No re-start/ingest in show. Wrong client state: %d",
+ client_get_id(cl), client_get_state(cl));
+ }
+
}
- session_leave(se);
}
+void session_sort(struct session *se, struct reclist_sortparms *sp) {
+ //session_enter(se, "session_sort");
+ session_sort_unlocked(se, sp);
+ //session_leave(se, "session_sort");
+}
+
+
enum pazpar2_error_code session_search(struct session *se,
const char *query,
const char *startrecs,
const char *filter,
const char *limit,
const char **addinfo,
- const char *sort_field, int increasing)
+ struct reclist_sortparms *sp)
{
int live_channels = 0;
int no_working = 0;
int no_failed_query = 0;
int no_failed_limit = 0;
struct client_list *l, *l0;
- struct timeval tval;
- facet_limits_t facet_limits;
+ int same_sort_order = 0;
session_log(se, YLOG_DEBUG, "Search");
*addinfo = 0;
- if (se->settings_modified)
+ if (se->settings_modified) {
session_remove_cached_clients(se);
+ }
else
session_reset_active_clients(se, 0);
-
- session_enter(se);
+
+ session_enter(se, "session_search");
se->settings_modified = 0;
- session_clear_set(se, sort_field, increasing);
+
+ if (se->sorted_results) {
+ if (!reclist_sortparms_cmp(se->sorted_results, sp))
+ same_sort_order = 1;
+ }
+ session_clear_set(se, sp);
relevance_destroy(&se->relevance);
live_channels = select_targets(se, filter);
if (!live_channels)
{
- session_leave(se);
+ session_leave(se, "session_search");
return PAZPAR2_NO_TARGETS;
}
- yaz_gettimeofday(&tval);
-
- tval.tv_sec += 5;
-
- facet_limits = facet_limits_create(limit);
- if (!facet_limits)
+ facet_limits_destroy(se->facet_limits);
+ se->facet_limits = facet_limits_create(limit);
+ if (!se->facet_limits)
{
*addinfo = "limit";
- session_leave(se);
+ session_leave(se, "session_search");
return PAZPAR2_MALFORMED_PARAMETER_VALUE;
}
l0 = se->clients_active;
se->clients_active = 0;
- session_leave(se);
+ session_leave(se, "session_search");
for (l = l0; l; l = l->next)
{
int parse_ret;
struct client *cl = l->client;
-
+ client_parse_init(cl, 1);
if (prepare_map(se, client_get_database(cl)) < 0)
continue;
- parse_ret = client_parse_query(cl, query, facet_limits, startrecs,
- maxrecs);
+ parse_ret = client_parse_query(cl, query, se->facet_limits);
if (parse_ret == -1)
no_failed_query++;
else if (parse_ret == -2)
no_failed_limit++;
+ else if (parse_ret < 0)
+ no_working++; /* other error, such as bad CCL map */
else
{
- int r =
- client_prep_connection(cl, se->service->z3950_operation_timeout,
- se->service->z3950_session_timeout,
- se->service->server->iochan_man,
- &tval);
- if (parse_ret == 1 && r == 2)
- {
- session_log(se, YLOG_LOG, "client %s REUSE result", client_get_id(cl));
- client_reingest(cl);
- }
- else if (r)
- {
- session_log(se, YLOG_LOG, "client %s NEW search", client_get_id(cl));
- client_start_search(cl);
- }
+ client_parse_range(cl, startrecs, maxrecs);
+ client_parse_sort(cl, sp);
+ client_start_search(cl);
no_working++;
}
}
- facet_limits_destroy(facet_limits);
session_reset_active_clients(se, l0);
if (no_working == 0)
int i;
new->database = db;
-
+
new->map = 0;
assert(db->settings);
new->settings = nmem_malloc(se->session_nmem,
// Probably session_init_databases_fun should be refactored instead of
// called here.
-static struct session_database *load_session_database(struct session *se,
+static struct session_database *load_session_database(struct session *se,
char *id)
{
struct database *db = new_database_inherit_settings(id, se->session_nmem, se->service->settings);
}
// Find an existing session database. If not found, load it
-static struct session_database *find_session_database(struct session *se,
+static struct session_database *find_session_database(struct session *se,
char *id)
{
struct session_database *sdb;
session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
if (nmem_total(se->session_nmem))
session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
+ facet_limits_destroy(se->facet_limits);
nmem_destroy(se->nmem);
service_destroy(se->service);
yaz_mutex_destroy(&se->session_mutex);
size_t session_nmem;
if (session == 0)
return 0;
- session_enter(session);
+ session_enter(session, "session_get_memory_status");
session_nmem = nmem_total(session->nmem);
- session_leave(session);
+ session_leave(session, "session_get_memory_status");
return session_nmem;
}
session->session_nmem = nmem;
session->nmem = nmem_create();
session->databases = 0;
+ session->sorted_results = 0;
+ session->facet_limits = 0;
+
for (i = 0; i <= SESSION_WATCH_MAX; i++)
{
session->watchlist[i].data = 0;
res[*count].records = client_get_num_records(cl);
res[*count].filtered = client_get_num_records_filtered(cl);
res[*count].diagnostic =
- client_get_diagnostic(cl, &res[*count].addinfo);
+ client_get_diagnostic(cl, &res[*count].message,
+ &res[*count].addinfo);
res[*count].state = client_get_state_str(cl);
res[*count].connected = client_get_connection(cl) ? 1 : 0;
session_settings_dump(se, client_get_database(cl), w);
struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
{
struct hitsbytarget *p;
- session_enter(se);
+ session_enter(se, "get_hitsbytarget");
p = hitsbytarget_nb(se, count, nmem);
- session_leave(se);
+ session_leave(se, "get_hitsbytarget");
return p;
}
-
-struct termlist_score **get_termlist_score(struct session *se,
- const char *name, int *num)
-{
- int i;
- struct termlist_score **tl = 0;
-
- session_enter(se);
- for (i = 0; i < se->num_termlists; i++)
- if (!strcmp((const char *) se->termlists[i].name, name))
- {
- tl = termlist_highscore(se->termlists[i].termlist, num);
- break;
- }
- session_leave(se);
- return tl;
-}
// Compares two hitsbytarget nodes by hitcount
static int cmp_ht(const void *p1, const void *p2)
{
// do only print terms which have display names
-
+
wrbuf_puts(wrbuf, "<term>\n");
wrbuf_puts(wrbuf, "<id>");
wrbuf_xmlputs(wrbuf, ht[i].id);
wrbuf_puts(wrbuf, "</id>\n");
-
+
wrbuf_puts(wrbuf, "<name>");
if (!ht[i].name || !ht[i].name[0])
wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
else
wrbuf_xmlputs(wrbuf, ht[i].name);
wrbuf_puts(wrbuf, "</name>\n");
-
+
wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
ht[i].hits);
wrbuf_puts(wrbuf, "<state>");
wrbuf_xmlputs(wrbuf, ht[i].state);
wrbuf_puts(wrbuf, "</state>\n");
-
- wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
+
+ wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
ht[i].diagnostic);
wrbuf_puts(wrbuf, "</term>\n");
}
nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
- session_enter(se);
+ session_enter(se, "perform_termlist");
for (j = 0; j < num_names; j++)
{
wrbuf_puts(c->wrbuf, "\">\n");
must_generate_empty = 0;
- p = termlist_highscore(se->termlists[i].termlist, &len);
+ p = termlist_highscore(se->termlists[i].termlist, &len,
+ nmem_tmp);
if (p)
{
int i;
// prevent sending empty term elements
if (!p[i]->display_term || !p[i]->display_term[0])
continue;
-
+
wrbuf_puts(c->wrbuf, "<term>");
wrbuf_puts(c->wrbuf, "<name>");
wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
wrbuf_puts(c->wrbuf, "</name>");
-
- wrbuf_printf(c->wrbuf,
- "<frequency>%d</frequency>",
+
+ wrbuf_printf(c->wrbuf,
+ "<frequency>%d</frequency>",
p[i]->frequency);
wrbuf_puts(c->wrbuf, "</term>\n");
}
wrbuf_puts(c->wrbuf, "\"/>\n");
}
}
- session_leave(se);
+ session_leave(se, "perform_termlist");
nmem_destroy(nmem_tmp);
}
nmem_get_memory_in_use(&in_use);
nmem_get_memory_free(&is_free);
- yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
+ yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
(long) in_use, (long) is_free);
}
#endif
{
struct record_cluster *r = 0;
- session_enter(se);
+ session_enter(se, "show_single_start");
*prev_r = 0;
*next_r = 0;
if (se->reclist)
{
+ reclist_limit(se->reclist, se);
+
reclist_enter(se->reclist);
while ((r = reclist_read_record(se->reclist)))
{
reclist_leave(se->reclist);
}
if (!r)
- session_leave(se);
+ session_leave(se, "show_single_start");
return r;
}
void show_single_stop(struct session *se, struct record_cluster *rec)
{
- session_leave(se);
+ session_leave(se, "show_single_stop");
}
+
struct record_cluster **show_range_start(struct session *se,
- struct reclist_sortparms *sp,
+ struct reclist_sortparms *sp,
int start, int *num, int *total, Odr_int *sumhits, Odr_int *approx_hits)
{
struct record_cluster **recs;
struct reclist_sortparms *spp;
int i;
-#if USE_TIMING
+#if USE_TIMING
yaz_timing_t t = yaz_timing_create();
#endif
- session_enter(se);
+ session_enter(se, "show_range_start");
recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
if (!se->relevance)
{
*num = 0;
*total = 0;
- *sumhits = 0;
+ *sumhits = 0;
*approx_hits = 0;
recs = 0;
}
else
{
struct client_list *l;
-
+
+ reclist_limit(se->reclist, se);
+
for (spp = sp; spp; spp = spp->next)
if (spp->type == Metadata_sortkey_relevance)
{
break;
}
reclist_sort(se->reclist, sp);
-
+
reclist_enter(se->reclist);
*total = reclist_get_num_records(se->reclist);
recs = 0;
break;
}
-
+
for (i = 0; i < *num; i++)
{
struct record_cluster *r = reclist_read_record(se->reclist);
}
#if USE_TIMING
yaz_timing_stop(t);
- yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
+ yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
yaz_timing_get_real(t), yaz_timing_get_user(t),
yaz_timing_get_sys(t));
yaz_timing_destroy(&t);
void show_range_stop(struct session *se, struct record_cluster **recs)
{
- session_leave(se);
+ session_leave(se, "show_range_stop");
}
void statistics(struct session *se, struct statistics *stat)
{
struct record_metadata *rec_md = record_metadata_create(nmem);
struct record_metadata_attr **attrp = &rec_md->attributes;
-
+
for (; attr; attr = attr->next)
{
if (attr->children && attr->children->content)
char *p = nmem_strdup(nmem, value);
p = normalize7bit_generic(p, " ,/.:([");
-
+
rec_md->data.text.disp = p;
rec_md->data.text.sort = 0;
}
const char *norm_str;
pp2_charset_token_t prt =
pp2_charset_token_create(service->charsets, "mergekey");
-
+
pp2_charset_token_first(prt, (const char *) value, 0);
if (wrbuf_len(norm_wr) > 0)
wrbuf_puts(norm_wr, " ");
service, norm_wr);
if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
{
- /* no mergekey on this one and it is required..
+ /* no mergekey on this one and it is required..
Generate unique key instead */
wrbuf_rewind(norm_wr);
break;
return mergekey_norm;
}
-/** \brief see if metadata for pz:recordfilter exists
+/** \brief see if metadata for pz:recordfilter exists
\param root xml root element of normalized record
\param sdb session database for client
\retval 0 if there is no metadata for pz:recordfilter
If there is no pz:recordfilter defined, this function returns 1
as well.
*/
-
+
static int check_record_filter(xmlNode *root, struct session_database *sdb)
{
int match = 0;
xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
xmlNode *root;
const char *mergekey_norm;
-
+
if (!xdoc)
return -1;
-
+
root = xmlDocGetRootElement(xdoc);
-
+
if (!check_record_filter(root, sdb))
{
session_log(se, YLOG_LOG, "Filtered out record no %d from %s", record_no, sdb->database->id);
xmlFreeDoc(xdoc);
return -2;
}
-
+
mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
if (!mergekey_norm)
{
xmlFreeDoc(xdoc);
return -1;
}
- session_enter(se);
+ session_enter(se, "ingest_record");
if (client_get_session(cl) == se)
ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
- session_leave(se);
-
+ session_leave(se, "ingest_record");
+
xmlFreeDoc(xdoc);
return ret;
}
+// struct conf_metadata *ser_md = &service->metadata[md_field_id];
+// struct record_metadata *rec_md = record->metadata[md_field_id];
+static int match_metadata_local(struct conf_metadata *ser_md,
+ struct record_metadata *rec_md0,
+ char **values, int num_v)
+{
+ int i;
+ struct record_metadata *rec_md = rec_md0;
+ for (i = 0; i < num_v; )
+ {
+ if (rec_md)
+ {
+ if (ser_md->type == Metadata_type_year
+ || ser_md->type == Metadata_type_date)
+ {
+ int y = atoi(values[i]);
+ if (y >= rec_md->data.number.min
+ && y <= rec_md->data.number.max)
+ break;
+ }
+ else
+ {
+ yaz_log(YLOG_DEBUG, "cmp: '%s' '%s'", rec_md->data.text.disp, values[i]);
+ if (!strcmp(rec_md->data.text.disp, values[i]))
+ {
+ // Value equals, should not be filtered.
+ break;
+ }
+ }
+ rec_md = rec_md->next;
+ }
+ else
+ {
+ rec_md = rec_md0;
+ i++;
+ }
+ }
+ return i < num_v ? 1 : 0;
+}
+
+int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
+{
+ int i;
+ struct conf_service *service = se->service;
+ int ret = 1;
+ const char *name;
+ const char *value;
+ NMEM nmem_tmp = nmem_create();
+
+ for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
+ {
+ int j;
+ for (j = 0; j < service->num_metadata; j++)
+ {
+ struct conf_metadata *md = service->metadata + j;
+ if (!strcmp(md->name, name) && md->limitcluster)
+ {
+ char **values = 0;
+ int num = 0;
+ int md_field_id =
+ conf_service_metadata_field_id(service,
+ md->limitcluster);
+
+ if (md_field_id < 0)
+ {
+ ret = 0;
+ break;
+ }
+
+ nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
+ &num, 1, '\\', 1);
+
+ if (!match_metadata_local(&service->metadata[md_field_id],
+ rec->metadata[md_field_id],
+ values, num))
+ {
+ ret = 0;
+ break;
+ }
+ }
+ }
+ }
+ nmem_destroy(nmem_tmp);
+ return ret;
+}
+
// Skip record on non-zero
static int check_limit_local(struct client *cl,
struct record *record,
int l = 0;
while (!skip_record)
{
- struct conf_metadata *ser_md = 0;
- struct record_metadata *rec_md = 0;
int md_field_id;
char **values = 0;
- int i, num_v = 0;
-
- const char *name = client_get_facet_limit_local(cl, sdb, &l, nmem_tmp, &num_v, &values);
+ int num_v = 0;
+ const char *name =
+ client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
+ &num_v, &values);
if (!name)
break;
-
- md_field_id = conf_service_metadata_field_id(service, name);
- if (md_field_id < 0)
+
+ if (!strcmp(name, "*"))
{
- skip_record = 1;
- break;
+ for (md_field_id = 0; md_field_id < service->num_metadata;
+ md_field_id++)
+ {
+ if (match_metadata_local(
+ &service->metadata[md_field_id],
+ record->metadata[md_field_id],
+ values, num_v))
+ break;
+ }
+ if (md_field_id == service->num_metadata)
+ skip_record = 1;
}
- ser_md = &service->metadata[md_field_id];
- rec_md = record->metadata[md_field_id];
- yaz_log(YLOG_DEBUG, "check limit local %s", name);
- for (i = 0; i < num_v; )
+ else
{
- if (rec_md)
+ md_field_id = conf_service_metadata_field_id(service, name);
+ if (md_field_id < 0)
{
- if (ser_md->type == Metadata_type_year
- || ser_md->type == Metadata_type_date)
- {
- int y = atoi(values[i]);
- if (y >= rec_md->data.number.min
- && y <= rec_md->data.number.max)
- break;
- }
- else
- {
- yaz_log(YLOG_DEBUG, "cmp: '%s' '%s'", rec_md->data.text.disp, values[i]);
- if (!strcmp(rec_md->data.text.disp, values[i]))
- {
- // Value equals, should not be filtered.
- break;
- }
- }
- rec_md = rec_md->next;
+ skip_record = 1;
+ break;
}
- else
+ if (!match_metadata_local(
+ &service->metadata[md_field_id],
+ record->metadata[md_field_id],
+ values, num_v))
{
- rec_md = record->metadata[md_field_id];
- i++;
+ skip_record = 1;
}
}
- // At end , not match
- if (i == num_v)
- {
- skip_record = 1;
- break;
- }
}
nmem_destroy(nmem_tmp);
return skip_record;
}
-
+
static int ingest_to_cluster(struct client *cl,
xmlDoc *xdoc,
xmlNode *root,
struct conf_service *service = se->service;
int term_factor = 1;
struct record_cluster *cluster;
+ struct record_metadata **metadata0;
struct session_database *sdb = client_get_database(cl);
- struct record *record = record_create(se->nmem,
+ struct record *record = record_create(se->nmem,
service->num_metadata,
service->num_sortkeys, cl,
record_no);
if (value)
xmlFree(value);
type = value = 0;
-
+
if (n->type != XML_ELEMENT_NODE)
continue;
if (!strcmp((const char *) n->name, "metadata"))
struct record_metadata **wheretoput = 0;
struct record_metadata *rec_md = 0;
int md_field_id = -1;
-
+
type = xmlGetProp(n, (xmlChar *) "type");
value = xmlNodeListGetString(xdoc, n->children, 1);
-
+
if (!type || !value || !*value)
continue;
-
- md_field_id
+
+ md_field_id
= conf_service_metadata_field_id(service, (const char *) type);
if (md_field_id < 0)
{
if (se->number_of_warnings_unknown_metadata == 0)
{
- session_log(se, YLOG_WARN,
+ session_log(se, YLOG_WARN,
"Ignoring unknown metadata element: %s", type);
}
se->number_of_warnings_unknown_metadata++;
continue;
}
-
+
ser_md = &service->metadata[md_field_id];
// non-merged metadata
relevance_newrec(se->relevance, cluster);
-
+
+ // original metadata, to check if first existence of a field
+ metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
+ memcpy(metadata0, cluster->metadata,
+ sizeof(*metadata0) * service->num_metadata);
+
// now parsing XML record and adding data to cluster or record metadata
for (n = root->children; n; n = n->next)
{
if (value)
xmlFree(value);
type = value = 0;
-
+
if (n->type != XML_ELEMENT_NODE)
continue;
if (!strcmp((const char *) n->name, "metadata"))
int sk_field_id = -1;
const char *rank;
xmlChar *xml_rank;
-
+
type = xmlGetProp(n, (xmlChar *) "type");
value = xmlNodeListGetString(xdoc, n->children, 1);
-
+
if (!type || !value || !*value)
continue;
-
- md_field_id
+
+ md_field_id
= conf_service_metadata_field_id(service, (const char *) type);
if (md_field_id < 0)
continue;
-
+
ser_md = &service->metadata[md_field_id];
if (ser_md->sortkey_offset >= 0)
// merged metadata
rec_md = record_metadata_init(se->nmem, (const char *) value,
ser_md->type, 0);
+
+ // see if the field was not in cluster already (from beginning)
+
if (!rec_md)
continue;
wheretoput = &cluster->metadata[md_field_id];
- // and polulate with data:
- // assign cluster or record based on merge action
- if (ser_md->merge == Metadata_merge_unique)
+ if (ser_md->merge == Metadata_merge_first)
+ {
+ if (!metadata0[md_field_id])
+ {
+ while (*wheretoput)
+ wheretoput = &(*wheretoput)->next;
+ *wheretoput = rec_md;
+ }
+ }
+ else if (ser_md->merge == Metadata_merge_unique)
{
while (*wheretoput)
{
- if (!strcmp((const char *) (*wheretoput)->data.text.disp,
+ if (!strcmp((const char *) (*wheretoput)->data.text.disp,
rec_md->data.text.disp))
break;
wheretoput = &(*wheretoput)->next;
}
else if (ser_md->merge == Metadata_merge_longest)
{
- if (!*wheretoput
- || strlen(rec_md->data.text.disp)
+ if (!*wheretoput
+ || strlen(rec_md->data.text.disp)
> strlen((*wheretoput)->data.text.disp))
{
*wheretoput = rec_md;
if (ser_sk)
{
const char *sort_str = 0;
- int skip_article =
+ int skip_article =
ser_sk->type == Metadata_sortkey_skiparticle;
if (!cluster->sortkeys[sk_field_id])
- cluster->sortkeys[sk_field_id] =
- nmem_malloc(se->nmem,
+ cluster->sortkeys[sk_field_id] =
+ nmem_malloc(se->nmem,
sizeof(union data_types));
-
+
prt =
pp2_charset_token_create(service->charsets, "sort");
skip_article);
pp2_charset_token_next(prt);
-
+
sort_str = pp2_get_sort(prt);
-
- cluster->sortkeys[sk_field_id]->text.disp =
+
+ cluster->sortkeys[sk_field_id]->text.disp =
rec_md->data.text.disp;
if (!sort_str)
{
sort_str = rec_md->data.text.disp;
- session_log(se, YLOG_WARN,
+ session_log(se, YLOG_WARN,
"Could not make sortkey. Bug #1858");
}
- cluster->sortkeys[sk_field_id]->text.sort =
+ cluster->sortkeys[sk_field_id]->text.sort =
nmem_strdup(se->nmem, sort_str);
pp2_charset_token_destroy(prt);
}
{
*wheretoput = rec_md;
if (ser_sk)
- cluster->sortkeys[sk_field_id]
+ cluster->sortkeys[sk_field_id]
= &rec_md->data;
}
else
}
}
- // ranking of _all_ fields enabled ...
+ // ranking of _all_ fields enabled ...
if (rank)
{
- relevance_countwords(se->relevance, cluster,
+ relevance_countwords(se->relevance, cluster,
(char *) value, rank, ser_md->name);
}
if (value)
xmlFree(value);
+ xfree(metadata0);
relevance_donerecord(se->relevance, cluster);
se->total_records++;