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
40 #include <yaz/marcdisp.h>
41 #include <yaz/comstack.h>
42 #include <yaz/tcpip.h>
43 #include <yaz/proto.h>
44 #include <yaz/readconf.h>
45 #include <yaz/pquery.h>
46 #include <yaz/otherinfo.h>
47 #include <yaz/yaz-util.h>
49 #include <yaz/query-charset.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/oid_db.h>
52 #include <yaz/diagbib1.h>
53 #include <yaz/snprintf.h>
54 #include <yaz/rpn2cql.h>
58 #include <yaz/timing.h>
62 #include "parameters.h"
64 #include "connection.h"
66 #include "relevance.h"
68 /** \brief Represents client state for a connection to one search target */
70 struct session_database *database;
71 struct connection *connection;
72 struct session *session;
73 char *pquery; // Current search
74 char *cqlquery; // used for SRU targets only
80 enum client_state state;
81 struct show_raw *show_raw;
82 struct client *next; // next client in session or next in free list
83 ZOOM_resultset resultset;
87 int active; // whether this request has been sent to the server
92 void (*error_handler)(void *data, const char *addinfo);
93 void (*record_handler)(void *data, const char *buf, size_t sz);
95 struct show_raw *next;
98 static const char *client_states[] = {
104 "Client_Disconnected"
107 const char *client_get_state_str(struct client *cl)
109 return client_states[cl->state];
112 enum client_state client_get_state(struct client *cl)
117 void client_set_state(struct client *cl, enum client_state st)
120 /* no need to check for all client being non-active if this one
121 already is. Note that session_active_clients also LOCKS session */
122 if (!client_is_active(cl) && cl->session)
124 int no_active = session_active_clients(cl->session);
126 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
130 static void client_show_raw_error(struct client *cl, const char *addinfo);
132 // Close connection and set state to error
133 void client_fatal(struct client *cl)
135 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
136 connection_destroy(cl->connection);
137 client_set_state(cl, Client_Error);
140 struct connection *client_get_connection(struct client *cl)
142 return cl->connection;
145 struct session_database *client_get_database(struct client *cl)
150 struct session *client_get_session(struct client *cl)
155 const char *client_get_pquery(struct client *cl)
160 static void client_send_raw_present(struct client *cl);
161 static int nativesyntax_to_type(struct session_database *sdb, char *type,
164 static void client_show_immediate(
165 ZOOM_resultset resultset, struct session_database *sdb, int position,
167 void (*error_handler)(void *data, const char *addinfo),
168 void (*record_handler)(void *data, const char *buf, size_t sz),
178 error_handler(data, "no resultset");
181 rec = ZOOM_resultset_record(resultset, position-1);
184 error_handler(data, "no record");
190 nativesyntax_to_type(sdb, type, rec);
191 buf = ZOOM_record_get(rec, type, &len);
194 error_handler(data, "no record");
197 record_handler(data, buf, len);
201 int client_show_raw_begin(struct client *cl, int position,
202 const char *syntax, const char *esn,
204 void (*error_handler)(void *data, const char *addinfo),
205 void (*record_handler)(void *data, const char *buf,
209 if (syntax == 0 && esn == 0)
210 client_show_immediate(cl->resultset, client_get_database(cl),
212 error_handler, record_handler,
216 struct show_raw *rr, **rrp;
222 rr = xmalloc(sizeof(*rr));
223 rr->position = position;
226 rr->error_handler = error_handler;
227 rr->record_handler = record_handler;
230 rr->syntax = xstrdup(syntax);
234 rr->esn = xstrdup(esn);
239 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
243 if (cl->state == Client_Failed)
245 client_show_raw_error(cl, "client failed");
247 else if (cl->state == Client_Disconnected)
249 client_show_raw_error(cl, "client disconnected");
253 client_send_raw_present(cl);
259 static void client_show_raw_delete(struct show_raw *r)
266 void client_show_raw_remove(struct client *cl, void *data)
268 struct show_raw *rr = data;
269 struct show_raw **rrp = &cl->show_raw;
275 client_show_raw_delete(rr);
279 void client_show_raw_dequeue(struct client *cl)
281 struct show_raw *rr = cl->show_raw;
283 cl->show_raw = rr->next;
284 client_show_raw_delete(rr);
287 static void client_show_raw_error(struct client *cl, const char *addinfo)
291 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
292 client_show_raw_dequeue(cl);
296 static void client_send_raw_present(struct client *cl)
298 struct session_database *sdb = client_get_database(cl);
299 struct connection *co = client_get_connection(cl);
300 ZOOM_resultset set = cl->resultset;
302 int offset = cl->show_raw->position;
303 const char *syntax = 0;
304 const char *elements = 0;
306 assert(cl->show_raw);
309 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
310 client_get_url(cl), 1, offset);
312 if (cl->show_raw->syntax)
313 syntax = cl->show_raw->syntax;
315 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
316 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
318 if (cl->show_raw->esn)
319 elements = cl->show_raw->esn;
321 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
322 if (elements && *elements)
323 ZOOM_resultset_option_set(set, "elementSetName", elements);
325 ZOOM_resultset_records(set, 0, offset-1, 1);
326 cl->show_raw->active = 1;
328 connection_continue(co);
331 static int nativesyntax_to_type(struct session_database *sdb, char *type,
334 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
338 if (!strncmp(s, "iso2709", 7))
340 const char *cp = strchr(s, ';');
341 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
343 else if (!strncmp(s, "xml", 3))
351 else /* attempt to deduce structure */
353 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
356 if (!strcmp(syntax, "XML"))
361 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
363 strcpy(type, "xml; charset=marc8-s");
372 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
378 if (cl->show_raw->binary)
382 struct session_database *sdb = client_get_database(cl);
383 nativesyntax_to_type(sdb, type, rec);
386 buf = ZOOM_record_get(rec, type, &len);
387 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
388 client_show_raw_dequeue(cl);
391 void client_search_response(struct client *cl)
393 struct connection *co = cl->connection;
394 struct session *se = cl->session;
395 ZOOM_connection link = connection_get_link(co);
396 ZOOM_resultset resultset = cl->resultset;
397 const char *error, *addinfo;
399 if (ZOOM_connection_error(link, &error, &addinfo))
402 client_set_state(cl, Client_Error);
403 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
404 error, addinfo, client_get_url(cl));
408 cl->record_offset = cl->startrecs;
409 cl->hits = ZOOM_resultset_size(resultset);
410 se->total_hits += cl->hits;
415 void client_record_response(struct client *cl)
417 struct connection *co = cl->connection;
418 ZOOM_connection link = connection_get_link(co);
419 ZOOM_resultset resultset = cl->resultset;
420 const char *error, *addinfo;
422 if (ZOOM_connection_error(link, &error, &addinfo))
424 client_set_state(cl, Client_Error);
425 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
426 error, addinfo, client_get_url(cl));
431 const char *msg, *addinfo;
433 if (cl->show_raw && cl->show_raw->active)
435 if ((rec = ZOOM_resultset_record(resultset,
436 cl->show_raw->position-1)))
438 cl->show_raw->active = 0;
439 ingest_raw_record(cl, rec);
443 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
444 cl->show_raw->position-1);
449 int offset = cl->record_offset;
450 if ((rec = ZOOM_resultset_record(resultset, offset)))
453 if (ZOOM_record_error(rec, &msg, &addinfo, 0))
454 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
455 error, addinfo, client_get_url(cl),
459 struct session_database *sdb = client_get_database(cl);
460 NMEM nmem = nmem_create();
463 if (nativesyntax_to_type(sdb, type, rec))
464 yaz_log(YLOG_WARN, "Failed to determine record type");
465 if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
467 if (!ingest_record(cl, xmlrec, cl->record_offset, nmem))
469 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
470 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
473 yaz_log(YLOG_WARN, "Failed to ingest");
476 yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
483 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
490 void client_start_search(struct client *cl)
492 struct session_database *sdb = client_get_database(cl);
493 struct connection *co = client_get_connection(cl);
494 ZOOM_connection link = connection_get_link(co);
496 char *databaseName = sdb->database->databases[0];
497 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
498 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
499 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
500 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
501 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
502 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
503 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
504 char maxrecs_str[24], startrecs_str[24];
509 cl->record_offset = 0;
511 client_set_state(cl, Client_Working);
514 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
516 ZOOM_connection_option_set(link, "piggyback", "1");
518 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
519 if (*opt_sru && *opt_elements)
520 ZOOM_connection_option_set(link, "schema", opt_elements);
521 else if (*opt_elements)
522 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
524 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
528 sprintf(maxrecs_str, "%d", cl->maxrecs);
529 opt_maxrecs = maxrecs_str;
531 ZOOM_connection_option_set(link, "count", opt_maxrecs);
534 if (atoi(opt_maxrecs) > 20)
535 ZOOM_connection_option_set(link, "presentChunk", "20");
537 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
539 sprintf(startrecs_str, "%d", cl->startrecs);
540 ZOOM_connection_option_set(link, "start", startrecs_str);
543 ZOOM_connection_option_set(link, "databaseName", databaseName);
547 ZOOM_query q = ZOOM_query_create();
548 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
549 ZOOM_query_cql(q, cl->cqlquery);
551 ZOOM_query_sortby(q, opt_sort);
552 rs = ZOOM_connection_search(link, q);
553 ZOOM_query_destroy(q);
557 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
558 rs = ZOOM_connection_search_pqf(link, cl->pquery);
560 ZOOM_resultset_destroy(cl->resultset);
562 connection_continue(co);
565 struct client *client_create(void)
567 struct client *r = xmalloc(sizeof(*r));
576 r->record_offset = 0;
578 r->state = Client_Disconnected;
585 void client_destroy(struct client *c)
587 struct session *se = c->session;
588 if (c == se->clients)
589 se->clients = c->next;
593 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
602 connection_release(c->connection);
604 ZOOM_resultset_destroy(c->resultset);
608 void client_set_connection(struct client *cl, struct connection *con)
610 cl->connection = con;
613 void client_disconnect(struct client *cl)
615 if (cl->state != Client_Idle)
616 client_set_state(cl, Client_Disconnected);
617 client_set_connection(cl, 0);
620 // Extract terms from query into null-terminated termlist
621 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
625 pull_terms(nmem, query, termlist, &num);
629 // Initialize CCL map for a target
630 static CCL_bibset prepare_cclmap(struct client *cl)
632 struct session_database *sdb = client_get_database(cl);
639 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
641 char *p = strchr(s->name + 3, ':');
644 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
649 ccl_qual_fitem(res, s->value, p);
654 // returns a xmalloced CQL query corresponding to the pquery in client
655 static char *make_cqlquery(struct client *cl)
657 cql_transform_t cqlt = cql_transform_create();
660 WRBUF wrb = wrbuf_alloc();
662 ODR odr_out = odr_createmem(ODR_ENCODE);
664 zquery = p_query_rpn(odr_out, cl->pquery);
665 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
666 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
668 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
673 r = xstrdup(wrbuf_cstr(wrb));
676 odr_destroy(odr_out);
677 cql_transform_close(cqlt);
681 // Parse the query given the settings specific to this client
682 int client_parse_query(struct client *cl, const char *query)
684 struct session *se = client_get_session(cl);
685 struct session_database *sdb = client_get_database(cl);
686 struct ccl_rpn_node *cn;
688 CCL_bibset ccl_map = prepare_cclmap(cl);
689 const char *sru = session_setting_oneval(sdb, PZ_SRU);
690 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
691 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
696 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
697 ccl_qual_rm(&ccl_map);
700 client_set_state(cl, Client_Error);
701 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
703 client_get_database(cl)->database->url);
706 wrbuf_rewind(se->wrbuf);
709 wrbuf_puts(se->wrbuf, pqf_prefix);
710 wrbuf_puts(se->wrbuf, " ");
712 if (!pqf_strftime || !*pqf_strftime)
713 ccl_pquery(se->wrbuf, cn);
716 time_t cur_time = time(0);
717 struct tm *tm = localtime(&cur_time);
719 const char *cp = tmp_str;
721 /* see man strftime(3) for things .. In particular %% gets converted
722 to %.. And That's our original query .. */
723 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
727 ccl_pquery(se->wrbuf, cn);
729 wrbuf_putc(se->wrbuf, cp[0]);
733 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
738 if (!(cl->cqlquery = make_cqlquery(cl)))
746 // Initialize relevance structure with query terms
748 extract_terms(se->nmem, cn, p);
749 se->relevance = relevance_create(
750 se->service->relevance_pct,
751 se->nmem, (const char **) p);
758 void client_set_session(struct client *cl, struct session *se)
761 cl->next = se->clients;
765 int client_is_active(struct client *cl)
767 if (cl->connection && (cl->state == Client_Connecting ||
768 cl->state == Client_Working))
773 struct client *client_next_in_session(struct client *cl)
781 Odr_int client_get_hits(struct client *cl)
786 int client_get_num_records(struct client *cl)
788 return cl->record_offset;
791 void client_set_diagnostic(struct client *cl, int diagnostic)
793 cl->diagnostic = diagnostic;
796 int client_get_diagnostic(struct client *cl)
798 return cl->diagnostic;
801 void client_set_database(struct client *cl, struct session_database *db)
806 struct host *client_get_host(struct client *cl)
808 return client_get_database(cl)->database->host;
811 const char *client_get_url(struct client *cl)
813 return client_get_database(cl)->database->url;
816 void client_set_maxrecs(struct client *cl, int v)
821 void client_set_startrecs(struct client *cl, int v)
829 * c-file-style: "Stroustrup"
830 * indent-tabs-mode: nil
832 * vim: shiftwidth=4 tabstop=8 expandtab