X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fclient.c;h=93671536adad71e5407536649f19c93d69a9f3b8;hb=9ebc11737580bcae95386cb76539650807b8f166;hp=e7bb2a0b9275d561e9398e4d58f4963c25f16aaf;hpb=0b3ccab53709ca408e31fc89dcd98319e1ae3721;p=pazpar2-moved-to-github.git diff --git a/src/client.c b/src/client.c index e7bb2a0..9367153 100644 --- a/src/client.c +++ b/src/client.c @@ -67,23 +67,39 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #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 { @@ -103,6 +119,8 @@ struct client { ZOOM_resultset resultset; YAZ_MUTEX mutex; int ref_count; + /* copy of database->url */ + char *url; }; struct show_raw { @@ -405,27 +423,48 @@ static int nativesyntax_to_type(struct session_database *sdb, char *type, * TODO Consider thread safety!!! * */ -int client_report_facets(struct client *cl, ZOOM_resultset rs) { - int facet_idx; +void client_report_facets(struct client *cl, ZOOM_resultset rs) +{ + struct session_database *sdb = client_get_database(cl); ZOOM_facet_field *facets = ZOOM_resultset_facets(rs); - int facet_num; - struct session *se = client_get_session(cl); - facet_num = ZOOM_resultset_facets_size(rs); - yaz_log(YLOG_DEBUG, "client_report_facets: %d", facet_num); - - for (facet_idx = 0; facet_idx < facet_num; facet_idx++) { - const char *name = ZOOM_facet_field_name(facets[facet_idx]); - size_t term_idx; - size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]); - for (term_idx = 0; term_idx < term_num; term_idx++ ) { - int freq; - const char *term = ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq); - if (term) - add_facet(se, name, term, freq); + + if (sdb && facets) + { + struct session *se = client_get_session(cl); + int facet_num = ZOOM_resultset_facets_size(rs); + struct setting *s; + + for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next) + { + const char *p = strchr(s->name + 3, ':'); + if (p && p[1] && s->value && s->value[0]) + { + int facet_idx; + p++; /* p now holds logical facet name */ + for (facet_idx = 0; facet_idx < facet_num; facet_idx++) + { + const char *native_name = + ZOOM_facet_field_name(facets[facet_idx]); + if (native_name && !strcmp(s->value, native_name)) + { + size_t term_idx; + size_t term_num = + ZOOM_facet_field_term_count(facets[facet_idx]); + for (term_idx = 0; term_idx < term_num; term_idx++ ) + { + int freq; + const char *term = + ZOOM_facet_field_get_term(facets[facet_idx], + term_idx, &freq); + if (term) + add_facet(se, p, term, freq); + } + break; + } + } + } } } - - return 0; } static void ingest_raw_record(struct client *cl, ZOOM_record rec) @@ -454,6 +493,7 @@ void client_check_preferred_watch(struct client *cl) if (se) { client_unlock(cl); + /* TODO possible threading issue. Session can have been destroyed */ if (session_is_preferred_clients_ready(se)) { session_alert_watch(se, SESSION_WATCH_SHOW_PREF); } @@ -571,9 +611,9 @@ void client_record_response(struct client *cl) 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); } @@ -587,63 +627,49 @@ void client_record_response(struct client *cl) } } -static int client_set_facets_request(struct client *cl, ZOOM_connection link) +static void client_set_facets_request(struct client *cl, ZOOM_connection link) { 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); - 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) + + WRBUF w = wrbuf_alloc(); + + struct setting *s; + + for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next) { - int index = 0; - struct session *session = client_get_session(cl); - struct conf_service *service = session->service; - int num = service->num_metadata; - WRBUF wrbuf = wrbuf_alloc(); - yaz_log(YLOG_DEBUG, "Facet settings, sort: %s count: %s", - opt_facet_term_sort, opt_facet_term_count); - for (index = 0; index < num; index++) + const char *p = strchr(s->name + 3, ':'); + if (!p) { - struct conf_metadata *conf_meta = &service->metadata[index]; - if (conf_meta->termlist) - { - if (wrbuf_len(wrbuf)) - wrbuf_puts(wrbuf, ", "); - wrbuf_printf(wrbuf, "@attr 1=%s", conf_meta->name); - - if (opt_facet_term_sort && *opt_facet_term_sort) - wrbuf_printf(wrbuf, " @attr 2=%s", opt_facet_term_sort); - wrbuf_printf(wrbuf, " @attr 3=%s", opt_facet_term_count); - } + yaz_log(YLOG_WARN, "Malformed facetmap name: %s", s->name); } - if (wrbuf_len(wrbuf)) + else if (s->value && s->value[0]) { - yaz_log(YLOG_LOG, "Setting ZOOM facets option: %s", wrbuf_cstr(wrbuf)); - ZOOM_connection_option_set(link, "facets", wrbuf_cstr(wrbuf)); - return 1; + wrbuf_puts(w, "@attr 1="); + yaz_encode_pqf_term(w, s->value, strlen(s->value)); + if (s->next) + wrbuf_puts(w, ","); } } - return 0; + yaz_log(YLOG_LOG, "using facets str: %s", wrbuf_cstr(w)); + ZOOM_connection_option_set(link, "facets", + wrbuf_len(w) ? wrbuf_cstr(w) : 0); + wrbuf_destroy(w); } -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; +int client_has_facet(struct client *cl, const char *name) +{ + struct session_database *sdb = client_get_database(cl); + struct setting *s; + + for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next) + { + const char *p = strchr(s->name + 3, ':'); + if (p && !strcmp(name, p + 1)) + return 1; } - yaz_log(YLOG_DEBUG, "client: No facets for %s", name); return 0; } - void client_start_search(struct client *cl) { struct session_database *sdb = client_get_database(cl); @@ -659,6 +685,7 @@ void client_start_search(struct client *cl) 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); @@ -667,6 +694,9 @@ void client_start_search(struct client *cl) 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); if (cl->preferred) @@ -687,18 +717,19 @@ void client_start_search(struct client *cl) 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); @@ -750,6 +781,7 @@ struct client *client_create(void) pazpar2_mutex_create(&cl->mutex, "client"); cl->preferred = 0; cl->ref_count = 1; + cl->url = 0; client_use(1); return cl; @@ -784,6 +816,7 @@ int client_destroy(struct client *c) c->pquery = 0; xfree(c->cqlquery); c->cqlquery = 0; + xfree(c->url); assert(!c->connection); if (c->resultset) @@ -912,8 +945,41 @@ static char *make_solrquery(struct client *cl) return r; } +static void apply_limit(struct session_database *sdb, + facet_limits_t facet_limits, + WRBUF w) +{ + int i = 0; + const char *name; + const char *value; + for (i = 0; (name = facet_limits_get(facet_limits, i, &value)); i++) + { + struct setting *s = 0; + + for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next) + { + const char *p = strchr(s->name + 3, ':'); + if (p && !strcmp(p + 1, name) && s->value && s->value[0]) + { + wrbuf_insert(w, 0, "@and ", 5); + wrbuf_puts(w, " @attr 1="); + yaz_encode_pqf_term(w, s->value, strlen(s->value)); + wrbuf_puts(w, " "); + yaz_encode_pqf_term(w, value, strlen(value)); + break; + } + } + if (!s) + { + yaz_log(YLOG_WARN, "facet %s used, but no facetmap defined", + name); + } + } +} + // Parse the query given the settings specific to this client -int client_parse_query(struct client *cl, const char *query) +int client_parse_query(struct client *cl, const char *query, + facet_limits_t facet_limits) { struct session *se = client_get_session(cl); struct session_database *sdb = client_get_database(cl); @@ -923,10 +989,12 @@ int client_parse_query(struct client *cl, const char *query) 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); + 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); ccl_qual_rm(&ccl_map); if (!cn) @@ -943,6 +1011,9 @@ int client_parse_query(struct client *cl, const char *query) wrbuf_puts(se->wrbuf, pqf_prefix); wrbuf_puts(se->wrbuf, " "); } + + apply_limit(sdb, facet_limits, se->wrbuf); + if (!pqf_strftime || !*pqf_strftime) ccl_pquery(se->wrbuf, cn); else @@ -966,8 +1037,14 @@ int client_parse_query(struct client *cl, const char *query) 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))) @@ -1045,6 +1122,10 @@ int client_get_diagnostic(struct client *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) @@ -1054,9 +1135,10 @@ 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"; }