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>
57 #include <yaz/timing.h>
62 #include "parameters.h"
64 #include "connection.h"
66 #include "relevance.h"
69 /* client counting (1) , disable client counting (0) */
71 static YAZ_MUTEX g_mutex = 0;
72 static int no_clients = 0;
74 static void client_use(int delta)
77 yaz_mutex_create(&g_mutex);
78 yaz_mutex_enter(g_mutex);
80 yaz_mutex_leave(g_mutex);
81 yaz_log(YLOG_DEBUG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
87 /** \brief Represents client state for a connection to one search target */
89 struct session_database *database;
90 struct connection *connection;
91 struct session *session;
92 char *pquery; // Current search
93 char *cqlquery; // used for SRU targets only
99 enum client_state state;
100 struct show_raw *show_raw;
101 ZOOM_resultset resultset;
107 int active; // whether this request has been sent to the server
112 void (*error_handler)(void *data, const char *addinfo);
113 void (*record_handler)(void *data, const char *buf, size_t sz);
115 struct show_raw *next;
118 static const char *client_states[] = {
124 "Client_Disconnected"
127 const char *client_get_state_str(struct client *cl)
129 return client_states[cl->state];
132 enum client_state client_get_state(struct client *cl)
137 void client_set_state(struct client *cl, enum client_state st)
140 if (client_is_active(cl))
143 /* If client is going from being active to inactive and all clients
144 are now idle we fire a watch for the session . The assumption is
145 that session is not mutex locked if client is already active */
146 if (was_active && !client_is_active(cl) && cl->session)
148 int no_active = session_active_clients(cl->session);
150 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
154 static void client_show_raw_error(struct client *cl, const char *addinfo);
156 struct connection *client_get_connection(struct client *cl)
158 return cl->connection;
161 struct session_database *client_get_database(struct client *cl)
166 struct session *client_get_session(struct client *cl)
171 const char *client_get_pquery(struct client *cl)
176 static void client_send_raw_present(struct client *cl);
177 static int nativesyntax_to_type(struct session_database *sdb, char *type,
180 static void client_show_immediate(
181 ZOOM_resultset resultset, struct session_database *sdb, int position,
183 void (*error_handler)(void *data, const char *addinfo),
184 void (*record_handler)(void *data, const char *buf, size_t sz),
194 error_handler(data, "no resultset");
197 rec = ZOOM_resultset_record(resultset, position-1);
200 error_handler(data, "no record");
206 nativesyntax_to_type(sdb, type, rec);
207 buf = ZOOM_record_get(rec, type, &len);
210 error_handler(data, "no record");
213 record_handler(data, buf, len);
217 int client_show_raw_begin(struct client *cl, int position,
218 const char *syntax, const char *esn,
220 void (*error_handler)(void *data, const char *addinfo),
221 void (*record_handler)(void *data, const char *buf,
225 if (syntax == 0 && esn == 0)
226 client_show_immediate(cl->resultset, client_get_database(cl),
228 error_handler, record_handler,
232 struct show_raw *rr, **rrp;
238 rr = xmalloc(sizeof(*rr));
239 rr->position = position;
242 rr->error_handler = error_handler;
243 rr->record_handler = record_handler;
246 rr->syntax = xstrdup(syntax);
250 rr->esn = xstrdup(esn);
255 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
259 if (cl->state == Client_Failed)
261 client_show_raw_error(cl, "client failed");
263 else if (cl->state == Client_Disconnected)
265 client_show_raw_error(cl, "client disconnected");
269 client_send_raw_present(cl);
275 static void client_show_raw_delete(struct show_raw *r)
282 void client_show_raw_remove(struct client *cl, void *data)
284 struct show_raw *rr = data;
285 struct show_raw **rrp = &cl->show_raw;
291 client_show_raw_delete(rr);
295 void client_show_raw_dequeue(struct client *cl)
297 struct show_raw *rr = cl->show_raw;
299 cl->show_raw = rr->next;
300 client_show_raw_delete(rr);
303 static void client_show_raw_error(struct client *cl, const char *addinfo)
307 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
308 client_show_raw_dequeue(cl);
312 static void client_send_raw_present(struct client *cl)
314 struct session_database *sdb = client_get_database(cl);
315 struct connection *co = client_get_connection(cl);
316 ZOOM_resultset set = cl->resultset;
318 int offset = cl->show_raw->position;
319 const char *syntax = 0;
320 const char *elements = 0;
322 assert(cl->show_raw);
325 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
326 client_get_url(cl), 1, offset);
328 if (cl->show_raw->syntax)
329 syntax = cl->show_raw->syntax;
331 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
332 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
334 if (cl->show_raw->esn)
335 elements = cl->show_raw->esn;
337 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
338 if (elements && *elements)
339 ZOOM_resultset_option_set(set, "elementSetName", elements);
341 ZOOM_resultset_records(set, 0, offset-1, 1);
342 cl->show_raw->active = 1;
344 connection_continue(co);
347 static int nativesyntax_to_type(struct session_database *sdb, char *type,
350 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
354 if (!strncmp(s, "iso2709", 7))
356 const char *cp = strchr(s, ';');
357 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
359 else if (!strncmp(s, "xml", 3))
363 else if (!strncmp(s, "txml", 4))
365 const char *cp = strchr(s, ';');
366 yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
372 else /* attempt to deduce structure */
374 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
377 if (!strcmp(syntax, "XML"))
382 else if (!strcmp(syntax, "TXML"))
384 strcpy(type, "txml");
387 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
389 strcpy(type, "xml; charset=marc8-s");
399 * TODO Consider thread safety!!!
402 int client_report_facets(struct client *cl, ZOOM_resultset rs) {
404 ZOOM_facet_field *facets = ZOOM_resultset_facets(rs);
406 struct session *se = client_get_session(cl);
407 facet_num = ZOOM_resultset_facets_size(rs);
408 yaz_log(YLOG_DEBUG, "client_report_facets: %d", facet_num);
410 for (facet_idx = 0; facet_idx < facet_num; facet_idx++) {
411 const char *name = ZOOM_facet_field_name(facets[facet_idx]);
413 size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
414 for (term_idx = 0; term_idx < term_num; term_idx++ ) {
416 const char *term = ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
418 add_facet(se, name, term, freq);
425 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
431 if (cl->show_raw->binary)
435 struct session_database *sdb = client_get_database(cl);
436 nativesyntax_to_type(sdb, type, rec);
439 buf = ZOOM_record_get(rec, type, &len);
440 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
441 client_show_raw_dequeue(cl);
444 void client_search_response(struct client *cl)
446 struct connection *co = cl->connection;
447 struct session *se = cl->session;
448 ZOOM_connection link = connection_get_link(co);
449 ZOOM_resultset resultset = cl->resultset;
451 const char *error, *addinfo = 0;
453 if (ZOOM_connection_error(link, &error, &addinfo))
456 client_set_state(cl, Client_Error);
457 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
458 error, addinfo, client_get_url(cl));
462 client_report_facets(cl, resultset);
463 cl->record_offset = cl->startrecs;
464 cl->hits = ZOOM_resultset_size(resultset);
466 se->total_hits += cl->hits;
470 void client_got_records(struct client *cl)
472 struct session *se = cl->session;
476 session_alert_watch(se, SESSION_WATCH_SHOW);
477 session_alert_watch(se, SESSION_WATCH_RECORD);
482 void client_record_response(struct client *cl)
484 struct connection *co = cl->connection;
485 ZOOM_connection link = connection_get_link(co);
486 ZOOM_resultset resultset = cl->resultset;
487 const char *error, *addinfo;
489 if (ZOOM_connection_error(link, &error, &addinfo))
491 client_set_state(cl, Client_Error);
492 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
493 error, addinfo, client_get_url(cl));
498 const char *msg, *addinfo;
500 if (cl->show_raw && cl->show_raw->active)
502 if ((rec = ZOOM_resultset_record(resultset,
503 cl->show_raw->position-1)))
505 cl->show_raw->active = 0;
506 ingest_raw_record(cl, rec);
510 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
511 cl->show_raw->position-1);
516 int offset = cl->record_offset;
517 if ((rec = ZOOM_resultset_record(resultset, offset)))
520 if (cl->session == 0)
522 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
524 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
525 msg, addinfo, client_get_url(cl),
530 struct session_database *sdb = client_get_database(cl);
531 NMEM nmem = nmem_create();
535 if (nativesyntax_to_type(sdb, type, rec))
536 yaz_log(YLOG_WARN, "Failed to determine record type");
537 xmlrec = ZOOM_record_get(rec, type, NULL);
539 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
543 if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
544 yaz_log(YLOG_WARN, "Failed to ingest from %s",
552 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
559 static int client_set_facets_request(struct client *cl, ZOOM_connection link) {
561 struct session_database *sdb = client_get_database(cl);
562 const char *opt_facet_term_sort = session_setting_oneval(sdb, PZ_TERMLIST_TERM_SORT);
563 const char *opt_facet_term_count = session_setting_oneval(sdb, PZ_TERMLIST_TERM_COUNT);
564 struct session *session = client_get_session(cl);
565 struct conf_service *service = session->service;
566 int num = service->num_metadata;
567 WRBUF wrbuf = wrbuf_alloc();
569 yaz_log(YLOG_DEBUG, "Facet settings, sort: %s count: %s", opt_facet_term_sort, opt_facet_term_count);
570 for (index = 0; index < num; index++) {
571 struct conf_metadata *conf_meta = &service->metadata[index];
572 if (conf_meta->termlist) {
576 wrbuf_puts(wrbuf, ",");
577 wrbuf_printf(wrbuf, "@attr 1=%s ", conf_meta->name);
579 if (opt_facet_term_sort && opt_facet_term_sort[0] != '\0') {
580 wrbuf_printf(wrbuf, " @attr 2=%s ", opt_facet_term_sort);
582 if (opt_facet_term_count && opt_facet_term_count[0] != '\0') {
583 wrbuf_printf(wrbuf, " @attr 3=%s ", opt_facet_term_count);
587 if (wrbuf_len(wrbuf)) {
588 yaz_log(YLOG_LOG, "Setting ZOOM facets option: %s", wrbuf_cstr(wrbuf));
589 ZOOM_connection_option_set(link, "facets", wrbuf_cstr(wrbuf));
595 int client_has_facet(struct client *cl, const char *name) {
596 ZOOM_facet_field facet_field;
597 if (!cl || !cl->resultset || !name) {
598 yaz_log(YLOG_DEBUG, "client has facet: Missing %p %p %s", cl, (cl ? cl->resultset: 0), name);
601 facet_field = ZOOM_resultset_get_facet_field(cl->resultset, name);
603 yaz_log(YLOG_DEBUG, "client: has facets for %s", name);
606 yaz_log(YLOG_DEBUG, "client: No facets for %s", name);
611 void client_start_search(struct client *cl)
613 struct session_database *sdb = client_get_database(cl);
614 struct connection *co = client_get_connection(cl);
615 ZOOM_connection link = connection_get_link(co);
617 char *databaseName = sdb->database->databases[0];
618 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
619 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
620 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
621 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
622 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
623 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
624 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
625 char maxrecs_str[24], startrecs_str[24];
630 cl->record_offset = 0;
632 client_set_state(cl, Client_Working);
635 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
637 ZOOM_connection_option_set(link, "piggyback", "1");
639 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
640 if (*opt_sru && *opt_elements)
641 ZOOM_connection_option_set(link, "schema", opt_elements);
642 else if (*opt_elements)
643 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
645 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
649 sprintf(maxrecs_str, "%d", cl->maxrecs);
650 opt_maxrecs = maxrecs_str;
652 ZOOM_connection_option_set(link, "count", opt_maxrecs);
655 if (atoi(opt_maxrecs) > 20)
656 ZOOM_connection_option_set(link, "presentChunk", "20");
658 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
660 sprintf(startrecs_str, "%d", cl->startrecs);
661 ZOOM_connection_option_set(link, "start", startrecs_str);
664 ZOOM_connection_option_set(link, "databaseName", databaseName);
668 ZOOM_query q = ZOOM_query_create();
669 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
670 ZOOM_query_cql(q, cl->cqlquery);
672 ZOOM_query_sortby(q, opt_sort);
673 rs = ZOOM_connection_search(link, q);
674 ZOOM_query_destroy(q);
678 client_set_facets_request(cl, link);
679 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
680 rs = ZOOM_connection_search_pqf(link, cl->pquery);
682 ZOOM_resultset_destroy(cl->resultset);
684 connection_continue(co);
687 struct client *client_create(void)
689 struct client *r = xmalloc(sizeof(*r));
698 r->record_offset = 0;
700 r->state = Client_Disconnected;
704 pazpar2_mutex_create(&r->mutex, "client");
712 void client_lock(struct client *c)
714 yaz_mutex_enter(c->mutex);
717 void client_unlock(struct client *c)
719 yaz_mutex_leave(c->mutex);
722 void client_incref(struct client *c)
724 pazpar2_incref(&c->ref_count, c->mutex);
725 yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
726 c, client_get_url(c), c->ref_count);
729 int client_destroy(struct client *c)
733 yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
734 c, client_get_url(c), c->ref_count);
735 if (!pazpar2_decref(&c->ref_count, c->mutex))
741 assert(!c->connection);
745 ZOOM_resultset_destroy(c->resultset);
747 yaz_mutex_destroy(&c->mutex);
756 void client_set_connection(struct client *cl, struct connection *con)
759 ZOOM_resultset_release(cl->resultset);
762 assert(cl->connection == 0);
763 cl->connection = con;
768 cl->connection = con;
773 void client_disconnect(struct client *cl)
775 if (cl->state != Client_Idle)
776 client_set_state(cl, Client_Disconnected);
777 client_set_connection(cl, 0);
780 // Extract terms from query into null-terminated termlist
781 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
785 pull_terms(nmem, query, termlist, &num);
789 // Initialize CCL map for a target
790 static CCL_bibset prepare_cclmap(struct client *cl)
792 struct session_database *sdb = client_get_database(cl);
799 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
801 char *p = strchr(s->name + 3, ':');
804 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
809 ccl_qual_fitem(res, s->value, p);
814 // returns a xmalloced CQL query corresponding to the pquery in client
815 static char *make_cqlquery(struct client *cl)
817 cql_transform_t cqlt = cql_transform_create();
820 WRBUF wrb = wrbuf_alloc();
822 ODR odr_out = odr_createmem(ODR_ENCODE);
824 zquery = p_query_rpn(odr_out, cl->pquery);
825 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
826 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
828 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
833 r = xstrdup(wrbuf_cstr(wrb));
836 odr_destroy(odr_out);
837 cql_transform_close(cqlt);
841 // Parse the query given the settings specific to this client
842 int client_parse_query(struct client *cl, const char *query)
844 struct session *se = client_get_session(cl);
845 struct session_database *sdb = client_get_database(cl);
846 struct ccl_rpn_node *cn;
848 CCL_bibset ccl_map = prepare_cclmap(cl);
849 const char *sru = session_setting_oneval(sdb, PZ_SRU);
850 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
851 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
856 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
857 ccl_qual_rm(&ccl_map);
860 client_set_state(cl, Client_Error);
861 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
863 client_get_database(cl)->database->url);
866 wrbuf_rewind(se->wrbuf);
869 wrbuf_puts(se->wrbuf, pqf_prefix);
870 wrbuf_puts(se->wrbuf, " ");
872 if (!pqf_strftime || !*pqf_strftime)
873 ccl_pquery(se->wrbuf, cn);
876 time_t cur_time = time(0);
877 struct tm *tm = localtime(&cur_time);
879 const char *cp = tmp_str;
881 /* see man strftime(3) for things .. In particular %% gets converted
882 to %.. And That's our original query .. */
883 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
887 ccl_pquery(se->wrbuf, cn);
889 wrbuf_putc(se->wrbuf, cp[0]);
893 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
898 if (!(cl->cqlquery = make_cqlquery(cl)))
904 /* TODO FIX Not thread safe */
907 // Initialize relevance structure with query terms
909 extract_terms(se->nmem, cn, p);
910 se->relevance = relevance_create(
911 se->service->relevance_pct,
912 se->nmem, (const char **) p);
919 void client_set_session(struct client *cl, struct session *se)
924 int client_is_active(struct client *cl)
926 if (cl->connection && (cl->state == Client_Connecting ||
927 cl->state == Client_Working))
932 Odr_int client_get_hits(struct client *cl)
937 int client_get_num_records(struct client *cl)
939 return cl->record_offset;
942 void client_set_diagnostic(struct client *cl, int diagnostic)
944 cl->diagnostic = diagnostic;
947 int client_get_diagnostic(struct client *cl)
949 return cl->diagnostic;
952 void client_set_database(struct client *cl, struct session_database *db)
957 struct host *client_get_host(struct client *cl)
959 return client_get_database(cl)->database->host;
962 const char *client_get_url(struct client *cl)
965 return client_get_database(cl)->database->url;
970 void client_set_maxrecs(struct client *cl, int v)
975 void client_set_startrecs(struct client *cl, int v)
983 * c-file-style: "Stroustrup"
984 * indent-tabs-mode: nil
986 * vim: shiftwidth=4 tabstop=8 expandtab