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>
63 #include "parameters.h"
65 #include "connection.h"
67 #include "relevance.h"
70 /** \brief Represents client state for a connection to one search target */
72 struct session_database *database;
73 struct connection *connection;
74 struct session *session;
75 char *pquery; // Current search
76 char *cqlquery; // used for SRU targets only
82 enum client_state state;
83 struct show_raw *show_raw;
84 struct client *next; // next client in session or next in free list
85 ZOOM_resultset resultset;
91 int active; // whether this request has been sent to the server
96 void (*error_handler)(void *data, const char *addinfo);
97 void (*record_handler)(void *data, const char *buf, size_t sz);
99 struct show_raw *next;
102 static const char *client_states[] = {
108 "Client_Disconnected"
111 const char *client_get_state_str(struct client *cl)
113 return client_states[cl->state];
116 enum client_state client_get_state(struct client *cl)
121 void client_set_state(struct client *cl, enum client_state st)
124 /* no need to check for all client being non-active if this one
125 already is. Note that session_active_clients also LOCKS session */
127 if (!client_is_active(cl) && cl->session)
129 int no_active = session_active_clients(cl->session);
131 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
136 static void client_show_raw_error(struct client *cl, const char *addinfo);
138 // Close connection and set state to error
139 void client_fatal(struct client *cl)
141 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
142 connection_destroy(cl->connection);
143 client_set_state(cl, Client_Error);
146 struct connection *client_get_connection(struct client *cl)
148 return cl->connection;
151 struct session_database *client_get_database(struct client *cl)
156 struct session *client_get_session(struct client *cl)
161 const char *client_get_pquery(struct client *cl)
166 static void client_send_raw_present(struct client *cl);
167 static int nativesyntax_to_type(struct session_database *sdb, char *type,
170 static void client_show_immediate(
171 ZOOM_resultset resultset, struct session_database *sdb, int position,
173 void (*error_handler)(void *data, const char *addinfo),
174 void (*record_handler)(void *data, const char *buf, size_t sz),
184 error_handler(data, "no resultset");
187 rec = ZOOM_resultset_record(resultset, position-1);
190 error_handler(data, "no record");
196 nativesyntax_to_type(sdb, type, rec);
197 buf = ZOOM_record_get(rec, type, &len);
200 error_handler(data, "no record");
203 record_handler(data, buf, len);
207 int client_show_raw_begin(struct client *cl, int position,
208 const char *syntax, const char *esn,
210 void (*error_handler)(void *data, const char *addinfo),
211 void (*record_handler)(void *data, const char *buf,
215 if (syntax == 0 && esn == 0)
216 client_show_immediate(cl->resultset, client_get_database(cl),
218 error_handler, record_handler,
222 struct show_raw *rr, **rrp;
228 rr = xmalloc(sizeof(*rr));
229 rr->position = position;
232 rr->error_handler = error_handler;
233 rr->record_handler = record_handler;
236 rr->syntax = xstrdup(syntax);
240 rr->esn = xstrdup(esn);
245 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
249 if (cl->state == Client_Failed)
251 client_show_raw_error(cl, "client failed");
253 else if (cl->state == Client_Disconnected)
255 client_show_raw_error(cl, "client disconnected");
259 client_send_raw_present(cl);
265 static void client_show_raw_delete(struct show_raw *r)
272 void client_show_raw_remove(struct client *cl, void *data)
274 struct show_raw *rr = data;
275 struct show_raw **rrp = &cl->show_raw;
281 client_show_raw_delete(rr);
285 void client_show_raw_dequeue(struct client *cl)
287 struct show_raw *rr = cl->show_raw;
289 cl->show_raw = rr->next;
290 client_show_raw_delete(rr);
293 static void client_show_raw_error(struct client *cl, const char *addinfo)
297 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
298 client_show_raw_dequeue(cl);
302 static void client_send_raw_present(struct client *cl)
304 struct session_database *sdb = client_get_database(cl);
305 struct connection *co = client_get_connection(cl);
306 ZOOM_resultset set = cl->resultset;
308 int offset = cl->show_raw->position;
309 const char *syntax = 0;
310 const char *elements = 0;
312 assert(cl->show_raw);
315 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
316 client_get_url(cl), 1, offset);
318 if (cl->show_raw->syntax)
319 syntax = cl->show_raw->syntax;
321 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
322 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
324 if (cl->show_raw->esn)
325 elements = cl->show_raw->esn;
327 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
328 if (elements && *elements)
329 ZOOM_resultset_option_set(set, "elementSetName", elements);
331 ZOOM_resultset_records(set, 0, offset-1, 1);
332 cl->show_raw->active = 1;
334 connection_continue(co);
337 static int nativesyntax_to_type(struct session_database *sdb, char *type,
340 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
344 if (!strncmp(s, "iso2709", 7))
346 const char *cp = strchr(s, ';');
347 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
349 else if (!strncmp(s, "xml", 3))
357 else /* attempt to deduce structure */
359 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
362 if (!strcmp(syntax, "XML"))
367 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
369 strcpy(type, "xml; charset=marc8-s");
378 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
384 if (cl->show_raw->binary)
388 struct session_database *sdb = client_get_database(cl);
389 nativesyntax_to_type(sdb, type, rec);
392 buf = ZOOM_record_get(rec, type, &len);
393 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
394 client_show_raw_dequeue(cl);
397 void client_search_response(struct client *cl)
399 struct connection *co = cl->connection;
400 struct session *se = cl->session;
401 ZOOM_connection link = connection_get_link(co);
402 ZOOM_resultset resultset = cl->resultset;
403 const char *error, *addinfo = 0;
405 if (ZOOM_connection_error(link, &error, &addinfo))
408 client_set_state(cl, Client_Error);
409 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
410 error, addinfo, client_get_url(cl));
414 cl->record_offset = cl->startrecs;
415 cl->hits = ZOOM_resultset_size(resultset);
417 se->total_hits += cl->hits;
421 void client_got_records(struct client *cl)
425 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
426 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
430 void client_record_response(struct client *cl)
432 static pthread_mutex_t ingest_mutex = PTHREAD_MUTEX_INITIALIZER;
433 static int ingest_counter = 0, ingest_max = 0;
434 struct connection *co = cl->connection;
435 ZOOM_connection link = connection_get_link(co);
436 ZOOM_resultset resultset = cl->resultset;
437 const char *error, *addinfo;
439 if (ZOOM_connection_error(link, &error, &addinfo))
441 client_set_state(cl, Client_Error);
442 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
443 error, addinfo, client_get_url(cl));
448 const char *msg, *addinfo;
450 if (cl->show_raw && cl->show_raw->active)
452 if ((rec = ZOOM_resultset_record(resultset,
453 cl->show_raw->position-1)))
455 cl->show_raw->active = 0;
456 ingest_raw_record(cl, rec);
460 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
461 cl->show_raw->position-1);
466 int offset = cl->record_offset;
467 if ((rec = ZOOM_resultset_record(resultset, offset)))
470 if (cl->session == 0)
472 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
473 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
474 error, addinfo, client_get_url(cl),
478 struct session_database *sdb = client_get_database(cl);
479 NMEM nmem = nmem_create();
483 yaz_log(YLOG_LOG, "Record ingest begin client=%p session=%p", cl, cl->session);
484 pthread_mutex_lock(&ingest_mutex);
486 if (ingest_counter > ingest_max)
488 ingest_max = ingest_counter;
489 new_max = ingest_max;
491 pthread_mutex_unlock(&ingest_mutex);
493 yaz_log(YLOG_LOG, "New max client=%p new_max=%d", cl, new_max);
494 if (nativesyntax_to_type(sdb, type, rec))
495 yaz_log(YLOG_WARN, "Failed to determine record type");
496 xmlrec = ZOOM_record_get(rec, type, NULL);
498 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
502 if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
503 yaz_log(YLOG_WARN, "Failed to ingest from %s",
506 pthread_mutex_lock(&ingest_mutex);
508 pthread_mutex_unlock(&ingest_mutex);
510 yaz_log(YLOG_LOG, "Record ingest end client=%p session=%p max=%d", cl, cl->session, ingest_max);
515 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
522 void client_start_search(struct client *cl)
524 struct session_database *sdb = client_get_database(cl);
525 struct connection *co = client_get_connection(cl);
526 ZOOM_connection link = connection_get_link(co);
528 char *databaseName = sdb->database->databases[0];
529 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
530 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
531 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
532 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
533 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
534 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
535 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
536 char maxrecs_str[24], startrecs_str[24];
541 cl->record_offset = 0;
543 client_set_state(cl, Client_Working);
546 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
548 ZOOM_connection_option_set(link, "piggyback", "1");
550 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
551 if (*opt_sru && *opt_elements)
552 ZOOM_connection_option_set(link, "schema", opt_elements);
553 else if (*opt_elements)
554 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
556 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
560 sprintf(maxrecs_str, "%d", cl->maxrecs);
561 opt_maxrecs = maxrecs_str;
563 ZOOM_connection_option_set(link, "count", opt_maxrecs);
566 if (atoi(opt_maxrecs) > 20)
567 ZOOM_connection_option_set(link, "presentChunk", "20");
569 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
571 sprintf(startrecs_str, "%d", cl->startrecs);
572 ZOOM_connection_option_set(link, "start", startrecs_str);
575 ZOOM_connection_option_set(link, "databaseName", databaseName);
579 ZOOM_query q = ZOOM_query_create();
580 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
581 ZOOM_query_cql(q, cl->cqlquery);
583 ZOOM_query_sortby(q, opt_sort);
584 rs = ZOOM_connection_search(link, q);
585 ZOOM_query_destroy(q);
589 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
590 rs = ZOOM_connection_search_pqf(link, cl->pquery);
592 ZOOM_resultset_destroy(cl->resultset);
594 connection_continue(co);
597 struct client *client_create(void)
599 struct client *r = xmalloc(sizeof(*r));
608 r->record_offset = 0;
610 r->state = Client_Disconnected;
615 pazpar2_mutex_create(&r->mutex, "client");
622 void client_incref(struct client *c)
624 pazpar2_incref(&c->ref_count, c->mutex);
625 yaz_log(YLOG_DEBUG, "client_incref %s %d", client_get_url(c), c->ref_count);
628 int client_destroy(struct client *c)
632 yaz_log(YLOG_DEBUG, "client_destroy %s %d",
633 client_get_url(c), c->ref_count);
634 if (!pazpar2_decref(&c->ref_count, c->mutex))
642 ZOOM_resultset_destroy(c->resultset);
643 yaz_mutex_destroy(&c->mutex);
651 void client_set_connection(struct client *cl, struct connection *con)
655 assert(cl->connection == 0);
656 cl->connection = con;
661 cl->connection = con;
666 void client_disconnect(struct client *cl)
668 if (cl->state != Client_Idle)
669 client_set_state(cl, Client_Disconnected);
670 client_set_connection(cl, 0);
673 // Extract terms from query into null-terminated termlist
674 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
678 pull_terms(nmem, query, termlist, &num);
682 // Initialize CCL map for a target
683 static CCL_bibset prepare_cclmap(struct client *cl)
685 struct session_database *sdb = client_get_database(cl);
692 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
694 char *p = strchr(s->name + 3, ':');
697 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
702 ccl_qual_fitem(res, s->value, p);
707 // returns a xmalloced CQL query corresponding to the pquery in client
708 static char *make_cqlquery(struct client *cl)
710 cql_transform_t cqlt = cql_transform_create();
713 WRBUF wrb = wrbuf_alloc();
715 ODR odr_out = odr_createmem(ODR_ENCODE);
717 zquery = p_query_rpn(odr_out, cl->pquery);
718 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
719 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
721 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
726 r = xstrdup(wrbuf_cstr(wrb));
729 odr_destroy(odr_out);
730 cql_transform_close(cqlt);
734 // Parse the query given the settings specific to this client
735 int client_parse_query(struct client *cl, const char *query)
737 struct session *se = client_get_session(cl);
738 struct session_database *sdb = client_get_database(cl);
739 struct ccl_rpn_node *cn;
741 CCL_bibset ccl_map = prepare_cclmap(cl);
742 const char *sru = session_setting_oneval(sdb, PZ_SRU);
743 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
744 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
749 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
750 ccl_qual_rm(&ccl_map);
753 client_set_state(cl, Client_Error);
754 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
756 client_get_database(cl)->database->url);
759 wrbuf_rewind(se->wrbuf);
762 wrbuf_puts(se->wrbuf, pqf_prefix);
763 wrbuf_puts(se->wrbuf, " ");
765 if (!pqf_strftime || !*pqf_strftime)
766 ccl_pquery(se->wrbuf, cn);
769 time_t cur_time = time(0);
770 struct tm *tm = localtime(&cur_time);
772 const char *cp = tmp_str;
774 /* see man strftime(3) for things .. In particular %% gets converted
775 to %.. And That's our original query .. */
776 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
780 ccl_pquery(se->wrbuf, cn);
782 wrbuf_putc(se->wrbuf, cp[0]);
786 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
791 if (!(cl->cqlquery = make_cqlquery(cl)))
799 // Initialize relevance structure with query terms
801 extract_terms(se->nmem, cn, p);
802 se->relevance = relevance_create(
803 se->service->relevance_pct,
804 se->nmem, (const char **) p);
812 void client_remove_from_session(struct client *c)
821 struct client **ccp = &se->clients;
823 while (*ccp && *ccp != c)
835 void client_set_session(struct client *cl, struct session *se)
838 cl->next = se->clients;
842 int client_is_active(struct client *cl)
844 if (cl->connection && (cl->state == Client_Connecting ||
845 cl->state == Client_Working))
850 struct client *client_next_in_session(struct client *cl)
858 Odr_int client_get_hits(struct client *cl)
863 int client_get_num_records(struct client *cl)
865 return cl->record_offset;
868 void client_set_diagnostic(struct client *cl, int diagnostic)
870 cl->diagnostic = diagnostic;
873 int client_get_diagnostic(struct client *cl)
875 return cl->diagnostic;
878 void client_set_database(struct client *cl, struct session_database *db)
883 struct host *client_get_host(struct client *cl)
885 return client_get_database(cl)->database->host;
888 const char *client_get_url(struct client *cl)
891 return client_get_database(cl)->database->url;
896 void client_set_maxrecs(struct client *cl, int v)
901 void client_set_startrecs(struct client *cl, int v)
909 * c-file-style: "Stroustrup"
910 * indent-tabs-mode: nil
912 * vim: shiftwidth=4 tabstop=8 expandtab