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 /* no need to check for all client being non-active if this one
141 already is. Note that session_active_clients also LOCKS session */
142 if (!client_is_active(cl) && cl->session)
144 int no_active = session_active_clients(cl->session);
146 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
150 static void client_show_raw_error(struct client *cl, const char *addinfo);
152 struct connection *client_get_connection(struct client *cl)
154 return cl->connection;
157 struct session_database *client_get_database(struct client *cl)
162 struct session *client_get_session(struct client *cl)
167 const char *client_get_pquery(struct client *cl)
172 static void client_send_raw_present(struct client *cl);
173 static int nativesyntax_to_type(struct session_database *sdb, char *type,
176 static void client_show_immediate(
177 ZOOM_resultset resultset, struct session_database *sdb, int position,
179 void (*error_handler)(void *data, const char *addinfo),
180 void (*record_handler)(void *data, const char *buf, size_t sz),
190 error_handler(data, "no resultset");
193 rec = ZOOM_resultset_record(resultset, position-1);
196 error_handler(data, "no record");
202 nativesyntax_to_type(sdb, type, rec);
203 buf = ZOOM_record_get(rec, type, &len);
206 error_handler(data, "no record");
209 record_handler(data, buf, len);
213 int client_show_raw_begin(struct client *cl, int position,
214 const char *syntax, const char *esn,
216 void (*error_handler)(void *data, const char *addinfo),
217 void (*record_handler)(void *data, const char *buf,
221 if (syntax == 0 && esn == 0)
222 client_show_immediate(cl->resultset, client_get_database(cl),
224 error_handler, record_handler,
228 struct show_raw *rr, **rrp;
234 rr = xmalloc(sizeof(*rr));
235 rr->position = position;
238 rr->error_handler = error_handler;
239 rr->record_handler = record_handler;
242 rr->syntax = xstrdup(syntax);
246 rr->esn = xstrdup(esn);
251 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
255 if (cl->state == Client_Failed)
257 client_show_raw_error(cl, "client failed");
259 else if (cl->state == Client_Disconnected)
261 client_show_raw_error(cl, "client disconnected");
265 client_send_raw_present(cl);
271 static void client_show_raw_delete(struct show_raw *r)
278 void client_show_raw_remove(struct client *cl, void *data)
280 struct show_raw *rr = data;
281 struct show_raw **rrp = &cl->show_raw;
287 client_show_raw_delete(rr);
291 void client_show_raw_dequeue(struct client *cl)
293 struct show_raw *rr = cl->show_raw;
295 cl->show_raw = rr->next;
296 client_show_raw_delete(rr);
299 static void client_show_raw_error(struct client *cl, const char *addinfo)
303 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
304 client_show_raw_dequeue(cl);
308 static void client_send_raw_present(struct client *cl)
310 struct session_database *sdb = client_get_database(cl);
311 struct connection *co = client_get_connection(cl);
312 ZOOM_resultset set = cl->resultset;
314 int offset = cl->show_raw->position;
315 const char *syntax = 0;
316 const char *elements = 0;
318 assert(cl->show_raw);
321 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
322 client_get_url(cl), 1, offset);
324 if (cl->show_raw->syntax)
325 syntax = cl->show_raw->syntax;
327 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
328 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
330 if (cl->show_raw->esn)
331 elements = cl->show_raw->esn;
333 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
334 if (elements && *elements)
335 ZOOM_resultset_option_set(set, "elementSetName", elements);
337 ZOOM_resultset_records(set, 0, offset-1, 1);
338 cl->show_raw->active = 1;
340 connection_continue(co);
343 static int nativesyntax_to_type(struct session_database *sdb, char *type,
346 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
350 if (!strncmp(s, "iso2709", 7))
352 const char *cp = strchr(s, ';');
353 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
355 else if (!strncmp(s, "xml", 3))
359 else if (!strncmp(s, "txml", 4))
361 const char *cp = strchr(s, ';');
362 yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
368 else /* attempt to deduce structure */
370 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
373 if (!strcmp(syntax, "XML"))
378 else if (!strcmp(syntax, "TXML"))
380 strcpy(type, "txml");
383 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
385 strcpy(type, "xml; charset=marc8-s");
395 * TODO Consider thread safety!!!
398 int client_report_facets(struct client *cl, ZOOM_resultset rs) {
400 ZOOM_facet_field *facets = ZOOM_resultset_facets(rs);
402 struct session *se = client_get_session(cl);
403 facet_num = ZOOM_resultset_facets_size(rs);
404 yaz_log(YLOG_DEBUG, "client_report_facets: %d", facet_num);
406 for (facet_idx = 0; facet_idx < facet_num; facet_idx++) {
407 const char *name = ZOOM_facet_field_name(facets[facet_idx]);
409 size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
410 for (term_idx = 0; term_idx < term_num; term_idx++ ) {
412 const char *term = ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
414 add_facet(se, name, term, freq);
421 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
427 if (cl->show_raw->binary)
431 struct session_database *sdb = client_get_database(cl);
432 nativesyntax_to_type(sdb, type, rec);
435 buf = ZOOM_record_get(rec, type, &len);
436 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
437 client_show_raw_dequeue(cl);
440 void client_search_response(struct client *cl)
442 struct connection *co = cl->connection;
443 struct session *se = cl->session;
444 ZOOM_connection link = connection_get_link(co);
445 ZOOM_resultset resultset = cl->resultset;
447 const char *error, *addinfo = 0;
449 if (ZOOM_connection_error(link, &error, &addinfo))
452 client_set_state(cl, Client_Error);
453 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
454 error, addinfo, client_get_url(cl));
458 client_report_facets(cl, resultset);
459 cl->record_offset = cl->startrecs;
460 cl->hits = ZOOM_resultset_size(resultset);
462 se->total_hits += cl->hits;
466 void client_got_records(struct client *cl)
468 struct session *se = cl->session;
472 session_alert_watch(se, SESSION_WATCH_SHOW);
473 session_alert_watch(se, SESSION_WATCH_RECORD);
478 void client_record_response(struct client *cl)
480 struct connection *co = cl->connection;
481 ZOOM_connection link = connection_get_link(co);
482 ZOOM_resultset resultset = cl->resultset;
483 const char *error, *addinfo;
485 if (ZOOM_connection_error(link, &error, &addinfo))
487 client_set_state(cl, Client_Error);
488 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
489 error, addinfo, client_get_url(cl));
494 const char *msg, *addinfo;
496 if (cl->show_raw && cl->show_raw->active)
498 if ((rec = ZOOM_resultset_record(resultset,
499 cl->show_raw->position-1)))
501 cl->show_raw->active = 0;
502 ingest_raw_record(cl, rec);
506 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
507 cl->show_raw->position-1);
512 int offset = cl->record_offset;
513 if ((rec = ZOOM_resultset_record(resultset, offset)))
516 if (cl->session == 0)
518 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
520 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
521 msg, addinfo, client_get_url(cl),
526 struct session_database *sdb = client_get_database(cl);
527 NMEM nmem = nmem_create();
531 if (nativesyntax_to_type(sdb, type, rec))
532 yaz_log(YLOG_WARN, "Failed to determine record type");
533 xmlrec = ZOOM_record_get(rec, type, NULL);
535 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
539 if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
540 yaz_log(YLOG_WARN, "Failed to ingest from %s",
548 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
555 static int client_set_facets_request(struct client *cl, ZOOM_connection link) {
557 struct session_database *sdb = client_get_database(cl);
558 const char *opt_facet_term_sort = session_setting_oneval(sdb, PZ_TERMLIST_TERM_SORT);
559 const char *opt_facet_term_count = session_setting_oneval(sdb, PZ_TERMLIST_TERM_COUNT);
560 struct session *session = client_get_session(cl);
561 struct conf_service *service = session->service;
562 int num = service->num_metadata;
563 WRBUF wrbuf = wrbuf_alloc();
565 yaz_log(YLOG_DEBUG, "Facet settings, sort: %s count: %s", opt_facet_term_sort, opt_facet_term_count);
566 for (index = 0; index < num; index++) {
567 struct conf_metadata *conf_meta = &service->metadata[index];
568 if (conf_meta->termlist) {
572 wrbuf_puts(wrbuf, ",");
573 wrbuf_printf(wrbuf, "@attr 1=%s ", conf_meta->name);
575 if (opt_facet_term_sort && opt_facet_term_sort[0] != '\0') {
576 wrbuf_printf(wrbuf, " @attr 2=%s ", opt_facet_term_sort);
578 if (opt_facet_term_count && opt_facet_term_count[0] != '\0') {
579 wrbuf_printf(wrbuf, " @attr 3=%s ", opt_facet_term_count);
583 if (wrbuf_len(wrbuf)) {
584 yaz_log(YLOG_LOG, "Setting ZOOM facets option: %s", wrbuf_cstr(wrbuf));
585 ZOOM_connection_option_set(link, "facets", wrbuf_cstr(wrbuf));
591 int client_has_facet(struct client *cl, const char *name) {
592 ZOOM_facet_field facet_field;
593 if (!cl || !cl->resultset || !name) {
594 yaz_log(YLOG_DEBUG, "client has facet: Missing %p %p %s", cl, (cl ? cl->resultset: 0), name);
597 facet_field = ZOOM_resultset_get_facet_field(cl->resultset, name);
599 yaz_log(YLOG_DEBUG, "client: has facets for %s", name);
602 yaz_log(YLOG_DEBUG, "client: No facets for %s", name);
607 void client_start_search(struct client *cl)
609 struct session_database *sdb = client_get_database(cl);
610 struct connection *co = client_get_connection(cl);
611 ZOOM_connection link = connection_get_link(co);
613 char *databaseName = sdb->database->databases[0];
614 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
615 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
616 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
617 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
618 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
619 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
620 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
621 char maxrecs_str[24], startrecs_str[24];
626 cl->record_offset = 0;
628 client_set_state(cl, Client_Working);
631 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
633 ZOOM_connection_option_set(link, "piggyback", "1");
635 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
636 if (*opt_sru && *opt_elements)
637 ZOOM_connection_option_set(link, "schema", opt_elements);
638 else if (*opt_elements)
639 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
641 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
645 sprintf(maxrecs_str, "%d", cl->maxrecs);
646 opt_maxrecs = maxrecs_str;
648 ZOOM_connection_option_set(link, "count", opt_maxrecs);
651 if (atoi(opt_maxrecs) > 20)
652 ZOOM_connection_option_set(link, "presentChunk", "20");
654 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
656 sprintf(startrecs_str, "%d", cl->startrecs);
657 ZOOM_connection_option_set(link, "start", startrecs_str);
660 ZOOM_connection_option_set(link, "databaseName", databaseName);
664 ZOOM_query q = ZOOM_query_create();
665 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
666 ZOOM_query_cql(q, cl->cqlquery);
668 ZOOM_query_sortby(q, opt_sort);
669 rs = ZOOM_connection_search(link, q);
670 ZOOM_query_destroy(q);
674 client_set_facets_request(cl, link);
675 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
676 rs = ZOOM_connection_search_pqf(link, cl->pquery);
678 ZOOM_resultset_destroy(cl->resultset);
680 connection_continue(co);
683 struct client *client_create(void)
685 struct client *r = xmalloc(sizeof(*r));
694 r->record_offset = 0;
696 r->state = Client_Disconnected;
700 pazpar2_mutex_create(&r->mutex, "client");
708 void client_lock(struct client *c)
710 yaz_mutex_enter(c->mutex);
713 void client_unlock(struct client *c)
715 yaz_mutex_leave(c->mutex);
718 void client_incref(struct client *c)
720 pazpar2_incref(&c->ref_count, c->mutex);
721 yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
722 c, client_get_url(c), c->ref_count);
725 int client_destroy(struct client *c)
729 yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
730 c, client_get_url(c), c->ref_count);
731 if (!pazpar2_decref(&c->ref_count, c->mutex))
737 assert(!c->connection);
741 ZOOM_resultset_destroy(c->resultset);
743 yaz_mutex_destroy(&c->mutex);
752 void client_set_connection(struct client *cl, struct connection *con)
755 ZOOM_resultset_release(cl->resultset);
758 assert(cl->connection == 0);
759 cl->connection = con;
764 cl->connection = con;
769 void client_disconnect(struct client *cl)
771 if (cl->state != Client_Idle)
772 client_set_state(cl, Client_Disconnected);
773 client_set_connection(cl, 0);
776 // Extract terms from query into null-terminated termlist
777 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
781 pull_terms(nmem, query, termlist, &num);
785 // Initialize CCL map for a target
786 static CCL_bibset prepare_cclmap(struct client *cl)
788 struct session_database *sdb = client_get_database(cl);
795 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
797 char *p = strchr(s->name + 3, ':');
800 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
805 ccl_qual_fitem(res, s->value, p);
810 // returns a xmalloced CQL query corresponding to the pquery in client
811 static char *make_cqlquery(struct client *cl)
813 cql_transform_t cqlt = cql_transform_create();
816 WRBUF wrb = wrbuf_alloc();
818 ODR odr_out = odr_createmem(ODR_ENCODE);
820 zquery = p_query_rpn(odr_out, cl->pquery);
821 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
822 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
824 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
829 r = xstrdup(wrbuf_cstr(wrb));
832 odr_destroy(odr_out);
833 cql_transform_close(cqlt);
837 // Parse the query given the settings specific to this client
838 int client_parse_query(struct client *cl, const char *query)
840 struct session *se = client_get_session(cl);
841 struct session_database *sdb = client_get_database(cl);
842 struct ccl_rpn_node *cn;
844 CCL_bibset ccl_map = prepare_cclmap(cl);
845 const char *sru = session_setting_oneval(sdb, PZ_SRU);
846 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
847 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
852 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
853 ccl_qual_rm(&ccl_map);
856 client_set_state(cl, Client_Error);
857 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
859 client_get_database(cl)->database->url);
862 wrbuf_rewind(se->wrbuf);
865 wrbuf_puts(se->wrbuf, pqf_prefix);
866 wrbuf_puts(se->wrbuf, " ");
868 if (!pqf_strftime || !*pqf_strftime)
869 ccl_pquery(se->wrbuf, cn);
872 time_t cur_time = time(0);
873 struct tm *tm = localtime(&cur_time);
875 const char *cp = tmp_str;
877 /* see man strftime(3) for things .. In particular %% gets converted
878 to %.. And That's our original query .. */
879 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
883 ccl_pquery(se->wrbuf, cn);
885 wrbuf_putc(se->wrbuf, cp[0]);
889 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
894 if (!(cl->cqlquery = make_cqlquery(cl)))
900 /* TODO FIX Not thread safe */
903 // Initialize relevance structure with query terms
905 extract_terms(se->nmem, cn, p);
906 se->relevance = relevance_create(
907 se->service->relevance_pct,
908 se->nmem, (const char **) p);
915 void client_set_session(struct client *cl, struct session *se)
920 int client_is_active(struct client *cl)
922 if (cl->connection && (cl->state == Client_Connecting ||
923 cl->state == Client_Working))
928 Odr_int client_get_hits(struct client *cl)
933 int client_get_num_records(struct client *cl)
935 return cl->record_offset;
938 void client_set_diagnostic(struct client *cl, int diagnostic)
940 cl->diagnostic = diagnostic;
943 int client_get_diagnostic(struct client *cl)
945 return cl->diagnostic;
948 void client_set_database(struct client *cl, struct session_database *db)
953 struct host *client_get_host(struct client *cl)
955 return client_get_database(cl)->database->host;
958 const char *client_get_url(struct client *cl)
961 return client_get_database(cl)->database->url;
966 void client_set_maxrecs(struct client *cl, int v)
971 void client_set_startrecs(struct client *cl, int v)
979 * c-file-style: "Stroustrup"
980 * indent-tabs-mode: nil
982 * vim: shiftwidth=4 tabstop=8 expandtab