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"
69 /** \brief Represents client state for a connection to one search target */
71 struct session_database *database;
72 struct connection *connection;
73 struct session *session;
74 char *pquery; // Current search
75 char *cqlquery; // used for SRU targets only
81 enum client_state state;
82 struct show_raw *show_raw;
83 struct client *next; // next client in session or next in free list
84 ZOOM_resultset resultset;
90 int active; // whether this request has been sent to the server
95 void (*error_handler)(void *data, const char *addinfo);
96 void (*record_handler)(void *data, const char *buf, size_t sz);
98 struct show_raw *next;
101 static const char *client_states[] = {
107 "Client_Disconnected"
110 const char *client_get_state_str(struct client *cl)
112 return client_states[cl->state];
115 enum client_state client_get_state(struct client *cl)
120 void client_set_state(struct client *cl, enum client_state st)
123 /* no need to check for all client being non-active if this one
124 already is. Note that session_active_clients also LOCKS session */
126 if (!client_is_active(cl) && cl->session)
128 int no_active = session_active_clients(cl->session);
130 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
135 static void client_show_raw_error(struct client *cl, const char *addinfo);
137 // Close connection and set state to error
138 void client_fatal(struct client *cl)
140 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
141 connection_destroy(cl->connection);
142 client_set_state(cl, Client_Error);
145 struct connection *client_get_connection(struct client *cl)
147 return cl->connection;
150 struct session_database *client_get_database(struct client *cl)
155 struct session *client_get_session(struct client *cl)
160 const char *client_get_pquery(struct client *cl)
165 static void client_send_raw_present(struct client *cl);
166 static int nativesyntax_to_type(struct session_database *sdb, char *type,
169 static void client_show_immediate(
170 ZOOM_resultset resultset, struct session_database *sdb, int position,
172 void (*error_handler)(void *data, const char *addinfo),
173 void (*record_handler)(void *data, const char *buf, size_t sz),
183 error_handler(data, "no resultset");
186 rec = ZOOM_resultset_record(resultset, position-1);
189 error_handler(data, "no record");
195 nativesyntax_to_type(sdb, type, rec);
196 buf = ZOOM_record_get(rec, type, &len);
199 error_handler(data, "no record");
202 record_handler(data, buf, len);
206 int client_show_raw_begin(struct client *cl, int position,
207 const char *syntax, const char *esn,
209 void (*error_handler)(void *data, const char *addinfo),
210 void (*record_handler)(void *data, const char *buf,
214 if (syntax == 0 && esn == 0)
215 client_show_immediate(cl->resultset, client_get_database(cl),
217 error_handler, record_handler,
221 struct show_raw *rr, **rrp;
227 rr = xmalloc(sizeof(*rr));
228 rr->position = position;
231 rr->error_handler = error_handler;
232 rr->record_handler = record_handler;
235 rr->syntax = xstrdup(syntax);
239 rr->esn = xstrdup(esn);
244 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
248 if (cl->state == Client_Failed)
250 client_show_raw_error(cl, "client failed");
252 else if (cl->state == Client_Disconnected)
254 client_show_raw_error(cl, "client disconnected");
258 client_send_raw_present(cl);
264 static void client_show_raw_delete(struct show_raw *r)
271 void client_show_raw_remove(struct client *cl, void *data)
273 struct show_raw *rr = data;
274 struct show_raw **rrp = &cl->show_raw;
280 client_show_raw_delete(rr);
284 void client_show_raw_dequeue(struct client *cl)
286 struct show_raw *rr = cl->show_raw;
288 cl->show_raw = rr->next;
289 client_show_raw_delete(rr);
292 static void client_show_raw_error(struct client *cl, const char *addinfo)
296 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
297 client_show_raw_dequeue(cl);
301 static void client_send_raw_present(struct client *cl)
303 struct session_database *sdb = client_get_database(cl);
304 struct connection *co = client_get_connection(cl);
305 ZOOM_resultset set = cl->resultset;
307 int offset = cl->show_raw->position;
308 const char *syntax = 0;
309 const char *elements = 0;
311 assert(cl->show_raw);
314 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
315 client_get_url(cl), 1, offset);
317 if (cl->show_raw->syntax)
318 syntax = cl->show_raw->syntax;
320 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
321 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
323 if (cl->show_raw->esn)
324 elements = cl->show_raw->esn;
326 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
327 if (elements && *elements)
328 ZOOM_resultset_option_set(set, "elementSetName", elements);
330 ZOOM_resultset_records(set, 0, offset-1, 1);
331 cl->show_raw->active = 1;
333 connection_continue(co);
336 static int nativesyntax_to_type(struct session_database *sdb, char *type,
339 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
343 if (!strncmp(s, "iso2709", 7))
345 const char *cp = strchr(s, ';');
346 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
348 else if (!strncmp(s, "xml", 3))
356 else /* attempt to deduce structure */
358 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
361 if (!strcmp(syntax, "XML"))
366 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
368 strcpy(type, "xml; charset=marc8-s");
377 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
383 if (cl->show_raw->binary)
387 struct session_database *sdb = client_get_database(cl);
388 nativesyntax_to_type(sdb, type, rec);
391 buf = ZOOM_record_get(rec, type, &len);
392 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
393 client_show_raw_dequeue(cl);
396 void client_search_response(struct client *cl)
398 struct connection *co = cl->connection;
399 struct session *se = cl->session;
400 ZOOM_connection link = connection_get_link(co);
401 ZOOM_resultset resultset = cl->resultset;
402 const char *error, *addinfo = 0;
404 if (ZOOM_connection_error(link, &error, &addinfo))
407 client_set_state(cl, Client_Error);
408 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
409 error, addinfo, client_get_url(cl));
413 cl->record_offset = cl->startrecs;
414 cl->hits = ZOOM_resultset_size(resultset);
416 se->total_hits += cl->hits;
420 void client_got_records(struct client *cl)
424 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
425 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
429 void client_record_response(struct client *cl)
431 struct connection *co = cl->connection;
432 ZOOM_connection link = connection_get_link(co);
433 ZOOM_resultset resultset = cl->resultset;
434 const char *error, *addinfo;
436 if (ZOOM_connection_error(link, &error, &addinfo))
438 client_set_state(cl, Client_Error);
439 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
440 error, addinfo, client_get_url(cl));
445 const char *msg, *addinfo;
447 if (cl->show_raw && cl->show_raw->active)
449 if ((rec = ZOOM_resultset_record(resultset,
450 cl->show_raw->position-1)))
452 cl->show_raw->active = 0;
453 ingest_raw_record(cl, rec);
457 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
458 cl->show_raw->position-1);
463 int offset = cl->record_offset;
464 if ((rec = ZOOM_resultset_record(resultset, offset)))
467 if (cl->session == 0)
469 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
470 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
471 error, addinfo, client_get_url(cl),
475 struct session_database *sdb = client_get_database(cl);
476 NMEM nmem = nmem_create();
479 if (nativesyntax_to_type(sdb, type, rec))
480 yaz_log(YLOG_WARN, "Failed to determine record type");
481 xmlrec = ZOOM_record_get(rec, type, NULL);
483 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
487 if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
488 yaz_log(YLOG_WARN, "Failed to ingest from %s",
496 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
503 void client_start_search(struct client *cl)
505 struct session_database *sdb = client_get_database(cl);
506 struct connection *co = client_get_connection(cl);
507 ZOOM_connection link = connection_get_link(co);
509 char *databaseName = sdb->database->databases[0];
510 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
511 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
512 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
513 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
514 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
515 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
516 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
517 char maxrecs_str[24], startrecs_str[24];
522 cl->record_offset = 0;
524 client_set_state(cl, Client_Working);
527 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
529 ZOOM_connection_option_set(link, "piggyback", "1");
531 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
532 if (*opt_sru && *opt_elements)
533 ZOOM_connection_option_set(link, "schema", opt_elements);
534 else if (*opt_elements)
535 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
537 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
541 sprintf(maxrecs_str, "%d", cl->maxrecs);
542 opt_maxrecs = maxrecs_str;
544 ZOOM_connection_option_set(link, "count", opt_maxrecs);
547 if (atoi(opt_maxrecs) > 20)
548 ZOOM_connection_option_set(link, "presentChunk", "20");
550 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
552 sprintf(startrecs_str, "%d", cl->startrecs);
553 ZOOM_connection_option_set(link, "start", startrecs_str);
556 ZOOM_connection_option_set(link, "databaseName", databaseName);
560 ZOOM_query q = ZOOM_query_create();
561 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
562 ZOOM_query_cql(q, cl->cqlquery);
564 ZOOM_query_sortby(q, opt_sort);
565 rs = ZOOM_connection_search(link, q);
566 ZOOM_query_destroy(q);
570 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
571 rs = ZOOM_connection_search_pqf(link, cl->pquery);
573 ZOOM_resultset_destroy(cl->resultset);
575 connection_continue(co);
578 struct client *client_create(void)
580 struct client *r = xmalloc(sizeof(*r));
589 r->record_offset = 0;
591 r->state = Client_Disconnected;
596 yaz_mutex_create(&r->mutex);
602 void client_incref(struct client *c)
604 pazpar2_incref(&c->ref_count, c->mutex);
605 yaz_log(YLOG_LOG, "client_incref %s %d", client_get_url(c), c->ref_count);
608 int client_destroy(struct client *c)
612 yaz_log(YLOG_LOG, "client_destroy %s %d",
613 client_get_url(c), c->ref_count);
614 if (!pazpar2_decref(&c->ref_count, c->mutex))
622 ZOOM_resultset_destroy(c->resultset);
623 yaz_mutex_destroy(&c->mutex);
631 void client_set_connection(struct client *cl, struct connection *con)
635 assert(cl->connection == 0);
636 cl->connection = con;
641 cl->connection = con;
646 void client_disconnect(struct client *cl)
648 if (cl->state != Client_Idle)
649 client_set_state(cl, Client_Disconnected);
650 client_set_connection(cl, 0);
653 // Extract terms from query into null-terminated termlist
654 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
658 pull_terms(nmem, query, termlist, &num);
662 // Initialize CCL map for a target
663 static CCL_bibset prepare_cclmap(struct client *cl)
665 struct session_database *sdb = client_get_database(cl);
672 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
674 char *p = strchr(s->name + 3, ':');
677 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
682 ccl_qual_fitem(res, s->value, p);
687 // returns a xmalloced CQL query corresponding to the pquery in client
688 static char *make_cqlquery(struct client *cl)
690 cql_transform_t cqlt = cql_transform_create();
693 WRBUF wrb = wrbuf_alloc();
695 ODR odr_out = odr_createmem(ODR_ENCODE);
697 zquery = p_query_rpn(odr_out, cl->pquery);
698 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
699 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
701 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
706 r = xstrdup(wrbuf_cstr(wrb));
709 odr_destroy(odr_out);
710 cql_transform_close(cqlt);
714 // Parse the query given the settings specific to this client
715 int client_parse_query(struct client *cl, const char *query)
717 struct session *se = client_get_session(cl);
718 struct session_database *sdb = client_get_database(cl);
719 struct ccl_rpn_node *cn;
721 CCL_bibset ccl_map = prepare_cclmap(cl);
722 const char *sru = session_setting_oneval(sdb, PZ_SRU);
723 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
724 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
729 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
730 ccl_qual_rm(&ccl_map);
733 client_set_state(cl, Client_Error);
734 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
736 client_get_database(cl)->database->url);
739 wrbuf_rewind(se->wrbuf);
742 wrbuf_puts(se->wrbuf, pqf_prefix);
743 wrbuf_puts(se->wrbuf, " ");
745 if (!pqf_strftime || !*pqf_strftime)
746 ccl_pquery(se->wrbuf, cn);
749 time_t cur_time = time(0);
750 struct tm *tm = localtime(&cur_time);
752 const char *cp = tmp_str;
754 /* see man strftime(3) for things .. In particular %% gets converted
755 to %.. And That's our original query .. */
756 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
760 ccl_pquery(se->wrbuf, cn);
762 wrbuf_putc(se->wrbuf, cp[0]);
766 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
771 if (!(cl->cqlquery = make_cqlquery(cl)))
779 // Initialize relevance structure with query terms
781 extract_terms(se->nmem, cn, p);
782 se->relevance = relevance_create(
783 se->service->relevance_pct,
784 se->nmem, (const char **) p);
792 void client_remove_from_session(struct client *c)
801 struct client **ccp = &se->clients;
803 while (*ccp && *ccp != c)
814 void client_set_session(struct client *cl, struct session *se)
817 cl->next = se->clients;
821 int client_is_active(struct client *cl)
823 if (cl->connection && (cl->state == Client_Connecting ||
824 cl->state == Client_Working))
829 struct client *client_next_in_session(struct client *cl)
837 Odr_int client_get_hits(struct client *cl)
842 int client_get_num_records(struct client *cl)
844 return cl->record_offset;
847 void client_set_diagnostic(struct client *cl, int diagnostic)
849 cl->diagnostic = diagnostic;
852 int client_get_diagnostic(struct client *cl)
854 return cl->diagnostic;
857 void client_set_database(struct client *cl, struct session_database *db)
862 struct host *client_get_host(struct client *cl)
864 return client_get_database(cl)->database->host;
867 const char *client_get_url(struct client *cl)
869 return client_get_database(cl)->database->url;
872 void client_set_maxrecs(struct client *cl, int v)
877 void client_set_startrecs(struct client *cl, int v)
885 * c-file-style: "Stroustrup"
886 * indent-tabs-mode: nil
888 * vim: shiftwidth=4 tabstop=8 expandtab