/* 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 "relevance.h"
#include "incref.h"
-/* client counting (1) , disable client counting (0) */
-#if 1
static YAZ_MUTEX g_mutex = 0;
static int no_clients = 0;
+static int no_clients_total = 0;
-static void client_use(int delta)
+static int client_use(int delta)
{
+ int clients;
if (!g_mutex)
yaz_mutex_create(&g_mutex);
yaz_mutex_enter(g_mutex);
no_clients += delta;
+ if (delta > 0)
+ no_clients_total += delta;
+ clients = no_clients;
yaz_mutex_leave(g_mutex);
- yaz_log(YLOG_DEBUG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
+ yaz_log(YLOG_DEBUG, "%s clients=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), clients);
+ return clients;
}
-#else
-#define client_use(x)
-#endif
+
+int clients_count(void) {
+ return client_use(0);
+}
+
+int clients_count_total(void) {
+ int total = 0;
+ if (!g_mutex)
+ return 0;
+ yaz_mutex_enter(g_mutex);
+ total = no_clients_total;
+ yaz_mutex_leave(g_mutex);
+ return total;
+}
+
/** \brief Represents client state for a connection to one search target */
struct client {
ZOOM_resultset resultset;
YAZ_MUTEX mutex;
int ref_count;
+ /* copy of database->url */
+ char *url;
};
struct show_raw {
that session is not mutex locked if client is already active */
if (was_active && !client_is_active(cl) && cl->session)
{
+
int no_active = session_active_clients(cl->session);
+ yaz_log(YLOG_DEBUG, "%s: releasing watches on zero active: %d", client_get_url(cl), no_active);
if (no_active == 0) {
session_alert_watch(cl->session, SESSION_WATCH_SHOW);
session_alert_watch(cl->session, SESSION_WATCH_SHOW_PREF);
client_show_raw_dequeue(cl);
}
-static void client_check_preferred_watch(struct client *cl)
+void client_check_preferred_watch(struct client *cl)
{
struct session *se = cl->session;
+ yaz_log(YLOG_DEBUG, "client_check_preferred_watch: %s ", client_get_url(cl));
if (se)
{
client_unlock(cl);
- if (session_preferred_clients_ready(se))
+ if (session_is_preferred_clients_ready(se)) {
session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
+ }
+ else
+ yaz_log(YLOG_DEBUG, "client_check_preferred_watch: Still locked on preferred targets.");
+
client_lock(cl);
}
+ else
+ yaz_log(YLOG_WARN, "client_check_preferred_watch: %s. No session!", client_get_url(cl));
+
}
void client_search_response(struct client *cl)
}
else
{
+ yaz_log(YLOG_DEBUG, "client_search_response: hits "
+ ODR_INT_PRINTF, cl->hits);
client_report_facets(cl, resultset);
cl->record_offset = cl->startrecs;
cl->hits = ZOOM_resultset_size(resultset);
- if (se)
+ if (se) {
se->total_hits += cl->hits;
- if (cl->preferred)
- client_check_preferred_watch(cl);
+ yaz_log(YLOG_DEBUG, "client_search_response: total hits "
+ ODR_INT_PRINTF, se->total_hits);
+ }
}
}
client_get_url(cl));
else
{
- if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
- yaz_log(YLOG_WARN, "Failed to ingest from %s",
- client_get_url(cl));
+ /* OK = 0, -1 = failure, -2 = Filtered */
+ if (ingest_record(cl, xmlrec, cl->record_offset, nmem) == -1)
+ yaz_log(YLOG_WARN, "Failed to ingest from %s", client_get_url(cl));
}
nmem_destroy(nmem);
}
struct session_database *sdb = client_get_database(cl);
const char *opt_facet_term_sort = session_setting_oneval(sdb, PZ_TERMLIST_TERM_SORT);
const char *opt_facet_term_count = session_setting_oneval(sdb, PZ_TERMLIST_TERM_COUNT);
+
+ /* Future record filtering on target */
+ /* const char *opt_facet_record_filter = session_setting_oneval(sdb, PZ_RECORDFILTER); */
+
/* Disable when no count is set */
/* TODO Verify: Do we need to reset the ZOOM facets if a ZOOM Connection is being reused??? */
if (opt_facet_term_count && *opt_facet_term_count)
int client_has_facet(struct client *cl, const char *name) {
ZOOM_facet_field facet_field;
if (!cl || !cl->resultset || !name) {
- yaz_log(YLOG_DEBUG, "client has facet: Missing %p %p %s", cl, (cl ? cl->resultset: 0), name);
return 0;
}
facet_field = ZOOM_resultset_get_facet_field(cl->resultset, name);
if (facet_field) {
- yaz_log(YLOG_DEBUG, "client: has facets for %s", name);
return 1;
}
- yaz_log(YLOG_DEBUG, "client: No facets for %s", name);
return 0;
}
const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
const char *opt_preferred = session_setting_oneval(sdb, PZ_PREFERRED);
+ const char *extra_args = session_setting_oneval(sdb, PZ_EXTRA_ARGS);
char maxrecs_str[24], startrecs_str[24];
assert(link);
cl->record_offset = 0;
cl->diagnostic = 0;
+ if (extra_args && *extra_args)
+ ZOOM_connection_option_set(link, "extraArgs", extra_args);
+
if (opt_preferred) {
cl->preferred = atoi(opt_preferred);
- yaz_log(YLOG_LOG, "Target %s has preferred: %d", sdb->database->url, cl->preferred);
+ if (cl->preferred)
+ yaz_log(YLOG_LOG, "Target %s has preferred status: %d", sdb->database->url, cl->preferred);
}
client_set_state(cl, Client_Working);
if (*opt_requestsyn)
ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
- if (!*opt_maxrecs)
+ if (opt_maxrecs && *opt_maxrecs)
{
- sprintf(maxrecs_str, "%d", cl->maxrecs);
- opt_maxrecs = maxrecs_str;
+ cl->maxrecs = atoi(opt_maxrecs);
}
- ZOOM_connection_option_set(link, "count", opt_maxrecs);
+ /* convert back to string representation used in ZOOM API */
+ sprintf(maxrecs_str, "%d", cl->maxrecs);
+ ZOOM_connection_option_set(link, "count", maxrecs_str);
- if (atoi(opt_maxrecs) > 20)
+ if (cl->maxrecs > 20)
ZOOM_connection_option_set(link, "presentChunk", "20");
else
- ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
+ ZOOM_connection_option_set(link, "presentChunk", maxrecs_str);
sprintf(startrecs_str, "%d", cl->startrecs);
ZOOM_connection_option_set(link, "start", startrecs_str);
struct client *client_create(void)
{
- struct client *r = xmalloc(sizeof(*r));
- r->maxrecs = 100;
- r->startrecs = 0;
- r->pquery = 0;
- r->cqlquery = 0;
- r->database = 0;
- r->connection = 0;
- r->session = 0;
- r->hits = 0;
- r->record_offset = 0;
- r->diagnostic = 0;
- r->state = Client_Disconnected;
- r->show_raw = 0;
- r->resultset = 0;
- r->mutex = 0;
- pazpar2_mutex_create(&r->mutex, "client");
- r->preferred = 0;
- r->ref_count = 1;
+ struct client *cl = xmalloc(sizeof(*cl));
+ cl->maxrecs = 100;
+ cl->startrecs = 0;
+ cl->pquery = 0;
+ cl->cqlquery = 0;
+ cl->database = 0;
+ cl->connection = 0;
+ cl->session = 0;
+ cl->hits = 0;
+ cl->record_offset = 0;
+ cl->diagnostic = 0;
+ cl->state = Client_Disconnected;
+ cl->show_raw = 0;
+ cl->resultset = 0;
+ cl->mutex = 0;
+ pazpar2_mutex_create(&cl->mutex, "client");
+ cl->preferred = 0;
+ cl->ref_count = 1;
+ cl->url = 0;
client_use(1);
- return r;
+ return cl;
}
void client_lock(struct client *c)
c->pquery = 0;
xfree(c->cqlquery);
c->cqlquery = 0;
+ xfree(c->url);
assert(!c->connection);
if (c->resultset)
struct session *se = client_get_session(cl);
struct session_database *sdb = client_get_database(cl);
struct ccl_rpn_node *cn;
+ struct ccl_rpn_node *cn_recordfilter = 0;
int cerror, cpos;
CCL_bibset ccl_map = prepare_cclmap(cl);
const char *sru = session_setting_oneval(sdb, PZ_SRU);
const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
-
+ const char *query_syntax = session_setting_oneval(sdb, PZ_QUERY_SYNTAX);
+ /* Collected, Mixed, Remote */
+ const char *option_recordfilter = session_setting_oneval(sdb, PZ_OPTION_RECORDFILTER);
+ const char *record_filter = session_setting_oneval(sdb, PZ_RECORDFILTER);
if (!ccl_map)
return -1;
+ yaz_log(YLOG_DEBUG, "query: %s", query);
cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
+ if (strcmp("remote", option_recordfilter) == 0 && record_filter != 0 && record_filter[0] != 0) {
+ int cerror, cpos;
+ yaz_log(YLOG_DEBUG, "record_filter: %s", record_filter);
+ cn_recordfilter = ccl_find_str(ccl_map, record_filter, &cerror, &cpos);
+ if (!cn_recordfilter)
+ session_log(se, YLOG_WARN, "Failed to parse CCL record filter '%s' for %s",
+ record_filter, client_get_database(cl)->database->url);
+ }
ccl_qual_rm(&ccl_map);
if (!cn)
{
client_set_state(cl, Client_Error);
- yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
+ session_log(se, YLOG_WARN, "Failed to parse CCL query '%s' for %s",
query,
client_get_database(cl)->database->url);
return -1;
wrbuf_puts(se->wrbuf, pqf_prefix);
wrbuf_puts(se->wrbuf, " ");
}
+
+ if (cn_recordfilter) {
+ wrbuf_puts(se->wrbuf, "@and ");
+ ccl_pquery(se->wrbuf, cn_recordfilter);
+ wrbuf_puts(se->wrbuf, " ");
+ }
+
if (!pqf_strftime || !*pqf_strftime)
ccl_pquery(se->wrbuf, cn);
else
xfree(cl->pquery);
cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
+ yaz_log(YLOG_DEBUG, "PQF query: %s", cl->pquery);
+
xfree(cl->cqlquery);
- if (*sru)
+
+ /* Support for PQF on SRU targets. */
+ /* TODO Refactor */
+ yaz_log(YLOG_DEBUG, "Query syntax: %s", query_syntax);
+ if (strcmp(query_syntax, "pqf") != 0 && *sru)
{
if (!strcmp(sru, "solr")) {
if (!(cl->cqlquery = make_solrquery(cl)))
void client_set_database(struct client *cl, struct session_database *db)
{
cl->database = db;
+ /* Copy the URL for safe logging even after session is gone */
+ if (db) {
+ cl->url = xstrdup(db->database->url);
+ }
}
struct host *client_get_host(struct client *cl)
const char *client_get_url(struct client *cl)
{
- if (cl->database)
- return client_get_database(cl)->database->url;
+ if (cl->url)
+ return cl->url;
else
+ /* This must not happen anymore, as the url is present until destruction of client */
return "NOURL";
}
cl->maxrecs = v;
}
+int client_get_maxrecs(struct client *cl)
+{
+ return cl->maxrecs;
+}
+
void client_set_startrecs(struct client *cl, int v)
{
cl->startrecs = v;