1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 #include <yaz/marcdisp.h>
40 #include <yaz/comstack.h>
41 #include <yaz/tcpip.h>
42 #include <yaz/proto.h>
43 #include <yaz/readconf.h>
44 #include <yaz/pquery.h>
45 #include <yaz/otherinfo.h>
46 #include <yaz/yaz-util.h>
48 #include <yaz/query-charset.h>
49 #include <yaz/querytowrbuf.h>
50 #include <yaz/oid_db.h>
51 #include <yaz/diagbib1.h>
52 #include <yaz/snprintf.h>
53 #include <yaz/rpn2cql.h>
54 #include <yaz/rpn2solr.h>
58 #include <yaz/timing.h>
63 #include "parameters.h"
65 #include "connection.h"
67 #include "relevance.h"
70 /* client counting (1) , disable client counting (0) */
72 static YAZ_MUTEX g_mutex = 0;
73 static int no_clients = 0;
75 static void client_use(int delta)
78 yaz_mutex_create(&g_mutex);
79 yaz_mutex_enter(g_mutex);
81 yaz_mutex_leave(g_mutex);
82 yaz_log(YLOG_DEBUG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
88 /** \brief Represents client state for a connection to one search target */
90 struct session_database *database;
91 struct connection *connection;
92 struct session *session;
93 char *pquery; // Current search
94 char *cqlquery; // used for SRU targets only
101 enum client_state state;
102 struct show_raw *show_raw;
103 ZOOM_resultset resultset;
109 int active; // whether this request has been sent to the server
114 void (*error_handler)(void *data, const char *addinfo);
115 void (*record_handler)(void *data, const char *buf, size_t sz);
117 struct show_raw *next;
120 static const char *client_states[] = {
126 "Client_Disconnected"
129 const char *client_get_state_str(struct client *cl)
131 return client_states[cl->state];
134 enum client_state client_get_state(struct client *cl)
139 void client_set_state(struct client *cl, enum client_state st)
142 if (client_is_active(cl))
145 /* If client is going from being active to inactive and all clients
146 are now idle we fire a watch for the session . The assumption is
147 that session is not mutex locked if client is already active */
148 if (was_active && !client_is_active(cl) && cl->session)
150 int no_active = session_active_clients(cl->session);
151 if (no_active == 0) {
152 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
153 session_alert_watch(cl->session, SESSION_WATCH_SHOW_PREF);
158 static void client_show_raw_error(struct client *cl, const char *addinfo);
160 struct connection *client_get_connection(struct client *cl)
162 return cl->connection;
165 struct session_database *client_get_database(struct client *cl)
170 struct session *client_get_session(struct client *cl)
175 const char *client_get_pquery(struct client *cl)
180 static void client_send_raw_present(struct client *cl);
181 static int nativesyntax_to_type(struct session_database *sdb, char *type,
184 static void client_show_immediate(
185 ZOOM_resultset resultset, struct session_database *sdb, int position,
187 void (*error_handler)(void *data, const char *addinfo),
188 void (*record_handler)(void *data, const char *buf, size_t sz),
198 error_handler(data, "no resultset");
201 rec = ZOOM_resultset_record(resultset, position-1);
204 error_handler(data, "no record");
210 nativesyntax_to_type(sdb, type, rec);
211 buf = ZOOM_record_get(rec, type, &len);
214 error_handler(data, "no record");
217 record_handler(data, buf, len);
221 int client_show_raw_begin(struct client *cl, int position,
222 const char *syntax, const char *esn,
224 void (*error_handler)(void *data, const char *addinfo),
225 void (*record_handler)(void *data, const char *buf,
229 if (syntax == 0 && esn == 0)
230 client_show_immediate(cl->resultset, client_get_database(cl),
232 error_handler, record_handler,
236 struct show_raw *rr, **rrp;
242 rr = xmalloc(sizeof(*rr));
243 rr->position = position;
246 rr->error_handler = error_handler;
247 rr->record_handler = record_handler;
250 rr->syntax = xstrdup(syntax);
254 rr->esn = xstrdup(esn);
259 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
263 if (cl->state == Client_Failed)
265 client_show_raw_error(cl, "client failed");
267 else if (cl->state == Client_Disconnected)
269 client_show_raw_error(cl, "client disconnected");
273 client_send_raw_present(cl);
279 static void client_show_raw_delete(struct show_raw *r)
286 void client_show_raw_remove(struct client *cl, void *data)
288 struct show_raw *rr = data;
289 struct show_raw **rrp = &cl->show_raw;
295 client_show_raw_delete(rr);
299 void client_show_raw_dequeue(struct client *cl)
301 struct show_raw *rr = cl->show_raw;
303 cl->show_raw = rr->next;
304 client_show_raw_delete(rr);
307 static void client_show_raw_error(struct client *cl, const char *addinfo)
311 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
312 client_show_raw_dequeue(cl);
316 static void client_send_raw_present(struct client *cl)
318 struct session_database *sdb = client_get_database(cl);
319 struct connection *co = client_get_connection(cl);
320 ZOOM_resultset set = cl->resultset;
322 int offset = cl->show_raw->position;
323 const char *syntax = 0;
324 const char *elements = 0;
326 assert(cl->show_raw);
329 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
330 client_get_url(cl), 1, offset);
332 if (cl->show_raw->syntax)
333 syntax = cl->show_raw->syntax;
335 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
336 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
338 if (cl->show_raw->esn)
339 elements = cl->show_raw->esn;
341 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
342 if (elements && *elements)
343 ZOOM_resultset_option_set(set, "elementSetName", elements);
345 ZOOM_resultset_records(set, 0, offset-1, 1);
346 cl->show_raw->active = 1;
348 connection_continue(co);
351 static int nativesyntax_to_type(struct session_database *sdb, char *type,
354 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
358 if (!strncmp(s, "iso2709", 7))
360 const char *cp = strchr(s, ';');
361 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
363 else if (!strncmp(s, "xml", 3))
367 else if (!strncmp(s, "txml", 4))
369 const char *cp = strchr(s, ';');
370 yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
376 else /* attempt to deduce structure */
378 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
381 if (!strcmp(syntax, "XML"))
386 else if (!strcmp(syntax, "TXML"))
388 strcpy(type, "txml");
391 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
393 strcpy(type, "xml; charset=marc8-s");
403 * TODO Consider thread safety!!!
406 int client_report_facets(struct client *cl, ZOOM_resultset rs) {
408 ZOOM_facet_field *facets = ZOOM_resultset_facets(rs);
410 struct session *se = client_get_session(cl);
411 facet_num = ZOOM_resultset_facets_size(rs);
412 yaz_log(YLOG_DEBUG, "client_report_facets: %d", facet_num);
414 for (facet_idx = 0; facet_idx < facet_num; facet_idx++) {
415 const char *name = ZOOM_facet_field_name(facets[facet_idx]);
417 size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
418 for (term_idx = 0; term_idx < term_num; term_idx++ ) {
420 const char *term = ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
422 add_facet(se, name, term, freq);
429 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
435 if (cl->show_raw->binary)
439 struct session_database *sdb = client_get_database(cl);
440 nativesyntax_to_type(sdb, type, rec);
443 buf = ZOOM_record_get(rec, type, &len);
444 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
445 client_show_raw_dequeue(cl);
448 static void client_check_preferred_watch(struct client *cl)
450 struct session *se = cl->session;
454 if (session_preferred_clients_ready(se))
455 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
460 void client_search_response(struct client *cl)
462 struct connection *co = cl->connection;
463 struct session *se = cl->session;
464 ZOOM_connection link = connection_get_link(co);
465 ZOOM_resultset resultset = cl->resultset;
467 const char *error, *addinfo = 0;
469 if (ZOOM_connection_error(link, &error, &addinfo))
472 client_set_state(cl, Client_Error);
473 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
474 error, addinfo, client_get_url(cl));
478 client_report_facets(cl, resultset);
479 cl->record_offset = cl->startrecs;
480 cl->hits = ZOOM_resultset_size(resultset);
482 se->total_hits += cl->hits;
484 client_check_preferred_watch(cl);
488 void client_got_records(struct client *cl)
490 struct session *se = cl->session;
494 session_alert_watch(se, SESSION_WATCH_SHOW);
495 session_alert_watch(se, SESSION_WATCH_RECORD);
500 void client_record_response(struct client *cl)
502 struct connection *co = cl->connection;
503 ZOOM_connection link = connection_get_link(co);
504 ZOOM_resultset resultset = cl->resultset;
505 const char *error, *addinfo;
507 if (ZOOM_connection_error(link, &error, &addinfo))
509 client_set_state(cl, Client_Error);
510 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
511 error, addinfo, client_get_url(cl));
516 const char *msg, *addinfo;
518 if (cl->show_raw && cl->show_raw->active)
520 if ((rec = ZOOM_resultset_record(resultset,
521 cl->show_raw->position-1)))
523 cl->show_raw->active = 0;
524 ingest_raw_record(cl, rec);
528 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
529 cl->show_raw->position-1);
534 int offset = cl->record_offset;
535 if ((rec = ZOOM_resultset_record(resultset, offset)))
538 if (cl->session == 0)
540 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
542 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
543 msg, addinfo, client_get_url(cl),
548 struct session_database *sdb = client_get_database(cl);
549 NMEM nmem = nmem_create();
553 if (nativesyntax_to_type(sdb, type, rec))
554 yaz_log(YLOG_WARN, "Failed to determine record type");
555 xmlrec = ZOOM_record_get(rec, type, NULL);
557 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
561 if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
562 yaz_log(YLOG_WARN, "Failed to ingest from %s",
570 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
577 static int client_set_facets_request(struct client *cl, ZOOM_connection link)
579 struct session_database *sdb = client_get_database(cl);
580 const char *opt_facet_term_sort = session_setting_oneval(sdb, PZ_TERMLIST_TERM_SORT);
581 const char *opt_facet_term_count = session_setting_oneval(sdb, PZ_TERMLIST_TERM_COUNT);
582 /* Disable when no count is set */
583 /* TODO Verify: Do we need to reset the ZOOM facets if a ZOOM Connection is being reused??? */
584 if (opt_facet_term_count && *opt_facet_term_count)
587 struct session *session = client_get_session(cl);
588 struct conf_service *service = session->service;
589 int num = service->num_metadata;
590 WRBUF wrbuf = wrbuf_alloc();
591 yaz_log(YLOG_DEBUG, "Facet settings, sort: %s count: %s",
592 opt_facet_term_sort, opt_facet_term_count);
593 for (index = 0; index < num; index++)
595 struct conf_metadata *conf_meta = &service->metadata[index];
596 if (conf_meta->termlist)
598 if (wrbuf_len(wrbuf))
599 wrbuf_puts(wrbuf, ", ");
600 wrbuf_printf(wrbuf, "@attr 1=%s", conf_meta->name);
602 if (opt_facet_term_sort && *opt_facet_term_sort)
603 wrbuf_printf(wrbuf, " @attr 2=%s", opt_facet_term_sort);
604 wrbuf_printf(wrbuf, " @attr 3=%s", opt_facet_term_count);
607 if (wrbuf_len(wrbuf))
609 yaz_log(YLOG_LOG, "Setting ZOOM facets option: %s", wrbuf_cstr(wrbuf));
610 ZOOM_connection_option_set(link, "facets", wrbuf_cstr(wrbuf));
617 int client_has_facet(struct client *cl, const char *name) {
618 ZOOM_facet_field facet_field;
619 if (!cl || !cl->resultset || !name) {
620 yaz_log(YLOG_DEBUG, "client has facet: Missing %p %p %s", cl, (cl ? cl->resultset: 0), name);
623 facet_field = ZOOM_resultset_get_facet_field(cl->resultset, name);
625 yaz_log(YLOG_DEBUG, "client: has facets for %s", name);
628 yaz_log(YLOG_DEBUG, "client: No facets for %s", name);
633 void client_start_search(struct client *cl)
635 struct session_database *sdb = client_get_database(cl);
636 struct connection *co = client_get_connection(cl);
637 ZOOM_connection link = connection_get_link(co);
639 char *databaseName = sdb->database->databases[0];
640 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
641 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
642 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
643 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
644 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
645 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
646 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
647 const char *opt_preferred = session_setting_oneval(sdb, PZ_PREFERRED);
648 char maxrecs_str[24], startrecs_str[24];
653 cl->record_offset = 0;
657 cl->preferred = atoi(opt_preferred);
658 yaz_log(YLOG_LOG, "Target %s has preferred: %d", sdb->database->url, cl->preferred);
660 client_set_state(cl, Client_Working);
663 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
665 ZOOM_connection_option_set(link, "piggyback", "1");
667 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
668 if (*opt_sru && *opt_elements)
669 ZOOM_connection_option_set(link, "schema", opt_elements);
670 else if (*opt_elements)
671 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
673 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
677 sprintf(maxrecs_str, "%d", cl->maxrecs);
678 opt_maxrecs = maxrecs_str;
680 ZOOM_connection_option_set(link, "count", opt_maxrecs);
683 if (atoi(opt_maxrecs) > 20)
684 ZOOM_connection_option_set(link, "presentChunk", "20");
686 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
688 sprintf(startrecs_str, "%d", cl->startrecs);
689 ZOOM_connection_option_set(link, "start", startrecs_str);
692 ZOOM_connection_option_set(link, "databaseName", databaseName);
694 /* TODO Verify does it break something for CQL targets(non-SOLR) ? */
695 /* facets definition is in PQF */
696 client_set_facets_request(cl, link);
700 ZOOM_query q = ZOOM_query_create();
701 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
702 ZOOM_query_cql(q, cl->cqlquery);
704 ZOOM_query_sortby(q, opt_sort);
705 rs = ZOOM_connection_search(link, q);
706 ZOOM_query_destroy(q);
710 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
711 rs = ZOOM_connection_search_pqf(link, cl->pquery);
713 ZOOM_resultset_destroy(cl->resultset);
715 connection_continue(co);
718 struct client *client_create(void)
720 struct client *r = xmalloc(sizeof(*r));
729 r->record_offset = 0;
731 r->state = Client_Disconnected;
735 pazpar2_mutex_create(&r->mutex, "client");
743 void client_lock(struct client *c)
745 yaz_mutex_enter(c->mutex);
748 void client_unlock(struct client *c)
750 yaz_mutex_leave(c->mutex);
753 void client_incref(struct client *c)
755 pazpar2_incref(&c->ref_count, c->mutex);
756 yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
757 c, client_get_url(c), c->ref_count);
760 int client_destroy(struct client *c)
764 yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
765 c, client_get_url(c), c->ref_count);
766 if (!pazpar2_decref(&c->ref_count, c->mutex))
772 assert(!c->connection);
776 ZOOM_resultset_destroy(c->resultset);
778 yaz_mutex_destroy(&c->mutex);
787 void client_set_connection(struct client *cl, struct connection *con)
790 ZOOM_resultset_release(cl->resultset);
793 assert(cl->connection == 0);
794 cl->connection = con;
799 cl->connection = con;
804 void client_disconnect(struct client *cl)
806 if (cl->state != Client_Idle)
807 client_set_state(cl, Client_Disconnected);
808 client_set_connection(cl, 0);
811 // Extract terms from query into null-terminated termlist
812 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
816 pull_terms(nmem, query, termlist, &num);
820 // Initialize CCL map for a target
821 static CCL_bibset prepare_cclmap(struct client *cl)
823 struct session_database *sdb = client_get_database(cl);
830 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
832 char *p = strchr(s->name + 3, ':');
835 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
840 ccl_qual_fitem(res, s->value, p);
845 // returns a xmalloced CQL query corresponding to the pquery in client
846 static char *make_cqlquery(struct client *cl)
848 cql_transform_t cqlt = cql_transform_create();
851 WRBUF wrb = wrbuf_alloc();
853 ODR odr_out = odr_createmem(ODR_ENCODE);
855 zquery = p_query_rpn(odr_out, cl->pquery);
856 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
857 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
859 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
864 r = xstrdup(wrbuf_cstr(wrb));
867 odr_destroy(odr_out);
868 cql_transform_close(cqlt);
872 // returns a xmalloced SOLR query corresponding to the pquery in client
873 // TODO Could prob. be merge with the similar make_cqlquery
874 static char *make_solrquery(struct client *cl)
876 solr_transform_t sqlt = solr_transform_create();
879 WRBUF wrb = wrbuf_alloc();
881 ODR odr_out = odr_createmem(ODR_ENCODE);
883 zquery = p_query_rpn(odr_out, cl->pquery);
884 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
885 if ((status = solr_transform_rpn2solr_wrbuf(sqlt, wrb, zquery)))
887 yaz_log(YLOG_WARN, "Failed to generate SOLR query, code=%d", status);
892 r = xstrdup(wrbuf_cstr(wrb));
895 odr_destroy(odr_out);
896 solr_transform_close(sqlt);
900 // Parse the query given the settings specific to this client
901 int client_parse_query(struct client *cl, const char *query)
903 struct session *se = client_get_session(cl);
904 struct session_database *sdb = client_get_database(cl);
905 struct ccl_rpn_node *cn;
907 CCL_bibset ccl_map = prepare_cclmap(cl);
908 const char *sru = session_setting_oneval(sdb, PZ_SRU);
909 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
910 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
915 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
916 ccl_qual_rm(&ccl_map);
919 client_set_state(cl, Client_Error);
920 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
922 client_get_database(cl)->database->url);
925 wrbuf_rewind(se->wrbuf);
928 wrbuf_puts(se->wrbuf, pqf_prefix);
929 wrbuf_puts(se->wrbuf, " ");
931 if (!pqf_strftime || !*pqf_strftime)
932 ccl_pquery(se->wrbuf, cn);
935 time_t cur_time = time(0);
936 struct tm *tm = localtime(&cur_time);
938 const char *cp = tmp_str;
940 /* see man strftime(3) for things .. In particular %% gets converted
941 to %.. And That's our original query .. */
942 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
946 ccl_pquery(se->wrbuf, cn);
948 wrbuf_putc(se->wrbuf, cp[0]);
952 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
957 if (!strcmp(sru, "solr")) {
958 if (!(cl->cqlquery = make_solrquery(cl)))
962 if (!(cl->cqlquery = make_cqlquery(cl)))
969 /* TODO FIX Not thread safe */
972 // Initialize relevance structure with query terms
974 extract_terms(se->nmem, cn, p);
975 se->relevance = relevance_create(
976 se->service->relevance_pct,
977 se->nmem, (const char **) p);
984 void client_set_session(struct client *cl, struct session *se)
989 int client_is_active(struct client *cl)
991 if (cl->connection && (cl->state == Client_Connecting ||
992 cl->state == Client_Working))
997 int client_is_active_preferred(struct client *cl)
999 /* only count if this is a preferred target. */
1002 /* TODO No sure this the condition that Seb wants */
1003 if (cl->connection && (cl->state == Client_Connecting ||
1004 cl->state == Client_Working))
1010 Odr_int client_get_hits(struct client *cl)
1015 int client_get_num_records(struct client *cl)
1017 return cl->record_offset;
1020 void client_set_diagnostic(struct client *cl, int diagnostic)
1022 cl->diagnostic = diagnostic;
1025 int client_get_diagnostic(struct client *cl)
1027 return cl->diagnostic;
1030 void client_set_database(struct client *cl, struct session_database *db)
1035 struct host *client_get_host(struct client *cl)
1037 return client_get_database(cl)->database->host;
1040 const char *client_get_url(struct client *cl)
1043 return client_get_database(cl)->database->url;
1048 void client_set_maxrecs(struct client *cl, int v)
1053 void client_set_startrecs(struct client *cl, int v)
1058 void client_set_preferred(struct client *cl, int v)
1067 * c-file-style: "Stroustrup"
1068 * indent-tabs-mode: nil
1070 * vim: shiftwidth=4 tabstop=8 expandtab