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 /* client counting (1) , disable client counting (0) */
72 static YAZ_MUTEX g_mutex = 0;
73 static int no_clients = 0;
75 static void client_use(int delta)
78 yaz_mutex_create(&g_mutex);
79 yaz_mutex_enter(g_mutex);
81 yaz_mutex_leave(g_mutex);
82 yaz_log(YLOG_LOG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
88 /** \brief Represents client state for a connection to one search target */
90 struct session_database *database;
91 struct connection *connection;
92 struct session *session;
93 char *pquery; // Current search
94 char *cqlquery; // used for SRU targets only
100 enum client_state state;
101 struct show_raw *show_raw;
102 ZOOM_resultset resultset;
108 int active; // whether this request has been sent to the server
113 void (*error_handler)(void *data, const char *addinfo);
114 void (*record_handler)(void *data, const char *buf, size_t sz);
116 struct show_raw *next;
119 static const char *client_states[] = {
125 "Client_Disconnected"
128 const char *client_get_state_str(struct client *cl)
130 return client_states[cl->state];
133 enum client_state client_get_state(struct client *cl)
138 void client_set_state(struct client *cl, enum client_state st)
141 /* no need to check for all client being non-active if this one
142 already is. Note that session_active_clients also LOCKS session */
144 if (!client_is_active(cl) && cl->session)
146 int no_active = session_active_clients(cl->session);
148 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
153 static void client_show_raw_error(struct client *cl, const char *addinfo);
155 struct connection *client_get_connection(struct client *cl)
157 return cl->connection;
160 struct session_database *client_get_database(struct client *cl)
165 struct session *client_get_session(struct client *cl)
170 const char *client_get_pquery(struct client *cl)
175 static void client_send_raw_present(struct client *cl);
176 static int nativesyntax_to_type(struct session_database *sdb, char *type,
179 static void client_show_immediate(
180 ZOOM_resultset resultset, struct session_database *sdb, int position,
182 void (*error_handler)(void *data, const char *addinfo),
183 void (*record_handler)(void *data, const char *buf, size_t sz),
193 error_handler(data, "no resultset");
196 rec = ZOOM_resultset_record(resultset, position-1);
199 error_handler(data, "no record");
205 nativesyntax_to_type(sdb, type, rec);
206 buf = ZOOM_record_get(rec, type, &len);
209 error_handler(data, "no record");
212 record_handler(data, buf, len);
216 int client_show_raw_begin(struct client *cl, int position,
217 const char *syntax, const char *esn,
219 void (*error_handler)(void *data, const char *addinfo),
220 void (*record_handler)(void *data, const char *buf,
224 if (syntax == 0 && esn == 0)
225 client_show_immediate(cl->resultset, client_get_database(cl),
227 error_handler, record_handler,
231 struct show_raw *rr, **rrp;
237 rr = xmalloc(sizeof(*rr));
238 rr->position = position;
241 rr->error_handler = error_handler;
242 rr->record_handler = record_handler;
245 rr->syntax = xstrdup(syntax);
249 rr->esn = xstrdup(esn);
254 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
258 if (cl->state == Client_Failed)
260 client_show_raw_error(cl, "client failed");
262 else if (cl->state == Client_Disconnected)
264 client_show_raw_error(cl, "client disconnected");
268 client_send_raw_present(cl);
274 static void client_show_raw_delete(struct show_raw *r)
281 void client_show_raw_remove(struct client *cl, void *data)
283 struct show_raw *rr = data;
284 struct show_raw **rrp = &cl->show_raw;
290 client_show_raw_delete(rr);
294 void client_show_raw_dequeue(struct client *cl)
296 struct show_raw *rr = cl->show_raw;
298 cl->show_raw = rr->next;
299 client_show_raw_delete(rr);
302 static void client_show_raw_error(struct client *cl, const char *addinfo)
306 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
307 client_show_raw_dequeue(cl);
311 static void client_send_raw_present(struct client *cl)
313 struct session_database *sdb = client_get_database(cl);
314 struct connection *co = client_get_connection(cl);
315 ZOOM_resultset set = cl->resultset;
317 int offset = cl->show_raw->position;
318 const char *syntax = 0;
319 const char *elements = 0;
321 assert(cl->show_raw);
324 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
325 client_get_url(cl), 1, offset);
327 if (cl->show_raw->syntax)
328 syntax = cl->show_raw->syntax;
330 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
331 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
333 if (cl->show_raw->esn)
334 elements = cl->show_raw->esn;
336 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
337 if (elements && *elements)
338 ZOOM_resultset_option_set(set, "elementSetName", elements);
340 ZOOM_resultset_records(set, 0, offset-1, 1);
341 cl->show_raw->active = 1;
343 connection_continue(co);
346 static int nativesyntax_to_type(struct session_database *sdb, char *type,
349 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
353 if (!strncmp(s, "iso2709", 7))
355 const char *cp = strchr(s, ';');
356 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
358 else if (!strncmp(s, "xml", 3))
362 else if (!strncmp(s, "txml", 4))
364 const char *cp = strchr(s, ';');
365 yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
371 else /* attempt to deduce structure */
373 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
376 if (!strcmp(syntax, "XML"))
381 else if (!strcmp(syntax, "TXML"))
383 strcpy(type, "txml");
386 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
388 strcpy(type, "xml; charset=marc8-s");
397 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
403 if (cl->show_raw->binary)
407 struct session_database *sdb = client_get_database(cl);
408 nativesyntax_to_type(sdb, type, rec);
411 buf = ZOOM_record_get(rec, type, &len);
412 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
413 client_show_raw_dequeue(cl);
416 void client_search_response(struct client *cl)
418 struct connection *co = cl->connection;
419 struct session *se = cl->session;
420 ZOOM_connection link = connection_get_link(co);
421 ZOOM_resultset resultset = cl->resultset;
422 const char *error, *addinfo = 0;
424 if (ZOOM_connection_error(link, &error, &addinfo))
427 client_set_state(cl, Client_Error);
428 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
429 error, addinfo, client_get_url(cl));
433 cl->record_offset = cl->startrecs;
434 cl->hits = ZOOM_resultset_size(resultset);
436 se->total_hits += cl->hits;
440 void client_got_records(struct client *cl)
444 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
445 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
449 void client_record_response(struct client *cl)
451 struct connection *co = cl->connection;
452 ZOOM_connection link = connection_get_link(co);
453 ZOOM_resultset resultset = cl->resultset;
454 const char *error, *addinfo;
456 if (ZOOM_connection_error(link, &error, &addinfo))
458 client_set_state(cl, Client_Error);
459 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
460 error, addinfo, client_get_url(cl));
465 const char *msg, *addinfo;
467 if (cl->show_raw && cl->show_raw->active)
469 if ((rec = ZOOM_resultset_record(resultset,
470 cl->show_raw->position-1)))
472 cl->show_raw->active = 0;
473 ingest_raw_record(cl, rec);
477 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
478 cl->show_raw->position-1);
483 int offset = cl->record_offset;
484 if ((rec = ZOOM_resultset_record(resultset, offset)))
487 if (cl->session == 0)
489 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
491 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
492 msg, addinfo, client_get_url(cl),
497 struct session_database *sdb = client_get_database(cl);
498 NMEM nmem = nmem_create();
502 if (nativesyntax_to_type(sdb, type, rec))
503 yaz_log(YLOG_WARN, "Failed to determine record type");
504 xmlrec = ZOOM_record_get(rec, type, NULL);
506 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
510 if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
511 yaz_log(YLOG_WARN, "Failed to ingest from %s",
519 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
526 void client_start_search(struct client *cl)
528 struct session_database *sdb = client_get_database(cl);
529 struct connection *co = client_get_connection(cl);
530 ZOOM_connection link = connection_get_link(co);
532 char *databaseName = sdb->database->databases[0];
533 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
534 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
535 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
536 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
537 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
538 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
539 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
540 char maxrecs_str[24], startrecs_str[24];
545 cl->record_offset = 0;
547 client_set_state(cl, Client_Working);
550 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
552 ZOOM_connection_option_set(link, "piggyback", "1");
554 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
555 if (*opt_sru && *opt_elements)
556 ZOOM_connection_option_set(link, "schema", opt_elements);
557 else if (*opt_elements)
558 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
560 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
564 sprintf(maxrecs_str, "%d", cl->maxrecs);
565 opt_maxrecs = maxrecs_str;
567 ZOOM_connection_option_set(link, "count", opt_maxrecs);
570 if (atoi(opt_maxrecs) > 20)
571 ZOOM_connection_option_set(link, "presentChunk", "20");
573 ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
575 sprintf(startrecs_str, "%d", cl->startrecs);
576 ZOOM_connection_option_set(link, "start", startrecs_str);
579 ZOOM_connection_option_set(link, "databaseName", databaseName);
583 ZOOM_query q = ZOOM_query_create();
584 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
585 ZOOM_query_cql(q, cl->cqlquery);
587 ZOOM_query_sortby(q, opt_sort);
588 rs = ZOOM_connection_search(link, q);
589 ZOOM_query_destroy(q);
593 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
594 rs = ZOOM_connection_search_pqf(link, cl->pquery);
596 ZOOM_resultset_destroy(cl->resultset);
598 connection_continue(co);
601 struct client *client_create(void)
603 struct client *r = xmalloc(sizeof(*r));
612 r->record_offset = 0;
614 r->state = Client_Disconnected;
618 pazpar2_mutex_create(&r->mutex, "client");
626 void client_incref(struct client *c)
628 pazpar2_incref(&c->ref_count, c->mutex);
629 yaz_log(YLOG_LOG, "client_incref c=%p %s cnt=%d",
630 c, client_get_url(c), c->ref_count);
633 int client_destroy(struct client *c)
637 yaz_log(YLOG_LOG, "client_destroy c=%p %s cnt=%d",
638 c, client_get_url(c), c->ref_count);
639 if (!pazpar2_decref(&c->ref_count, c->mutex))
645 assert(!c->connection);
646 assert(!c->resultset);
648 yaz_mutex_destroy(&c->mutex);
657 void client_set_connection(struct client *cl, struct connection *con)
661 ZOOM_resultset_destroy(cl->resultset);
666 assert(cl->connection == 0);
667 cl->connection = con;
672 cl->connection = con;
677 void client_disconnect(struct client *cl)
679 if (cl->state != Client_Idle)
680 client_set_state(cl, Client_Disconnected);
681 client_set_connection(cl, 0);
684 // Extract terms from query into null-terminated termlist
685 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
689 pull_terms(nmem, query, termlist, &num);
693 // Initialize CCL map for a target
694 static CCL_bibset prepare_cclmap(struct client *cl)
696 struct session_database *sdb = client_get_database(cl);
703 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
705 char *p = strchr(s->name + 3, ':');
708 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
713 ccl_qual_fitem(res, s->value, p);
718 // returns a xmalloced CQL query corresponding to the pquery in client
719 static char *make_cqlquery(struct client *cl)
721 cql_transform_t cqlt = cql_transform_create();
724 WRBUF wrb = wrbuf_alloc();
726 ODR odr_out = odr_createmem(ODR_ENCODE);
728 zquery = p_query_rpn(odr_out, cl->pquery);
729 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
730 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
732 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
737 r = xstrdup(wrbuf_cstr(wrb));
740 odr_destroy(odr_out);
741 cql_transform_close(cqlt);
745 // Parse the query given the settings specific to this client
746 int client_parse_query(struct client *cl, const char *query)
748 struct session *se = client_get_session(cl);
749 struct session_database *sdb = client_get_database(cl);
750 struct ccl_rpn_node *cn;
752 CCL_bibset ccl_map = prepare_cclmap(cl);
753 const char *sru = session_setting_oneval(sdb, PZ_SRU);
754 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
755 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
760 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
761 ccl_qual_rm(&ccl_map);
764 client_set_state(cl, Client_Error);
765 yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
767 client_get_database(cl)->database->url);
770 wrbuf_rewind(se->wrbuf);
773 wrbuf_puts(se->wrbuf, pqf_prefix);
774 wrbuf_puts(se->wrbuf, " ");
776 if (!pqf_strftime || !*pqf_strftime)
777 ccl_pquery(se->wrbuf, cn);
780 time_t cur_time = time(0);
781 struct tm *tm = localtime(&cur_time);
783 const char *cp = tmp_str;
785 /* see man strftime(3) for things .. In particular %% gets converted
786 to %.. And That's our original query .. */
787 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
791 ccl_pquery(se->wrbuf, cn);
793 wrbuf_putc(se->wrbuf, cp[0]);
797 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
802 if (!(cl->cqlquery = make_cqlquery(cl)))
810 // Initialize relevance structure with query terms
812 extract_terms(se->nmem, cn, p);
813 se->relevance = relevance_create(
814 se->service->relevance_pct,
815 se->nmem, (const char **) p);
822 void client_set_session(struct client *cl, struct session *se)
827 int client_is_active(struct client *cl)
829 if (cl->connection && (cl->state == Client_Connecting ||
830 cl->state == Client_Working))
835 Odr_int client_get_hits(struct client *cl)
840 int client_get_num_records(struct client *cl)
842 return cl->record_offset;
845 void client_set_diagnostic(struct client *cl, int diagnostic)
847 cl->diagnostic = diagnostic;
850 int client_get_diagnostic(struct client *cl)
852 return cl->diagnostic;
855 void client_set_database(struct client *cl, struct session_database *db)
860 struct host *client_get_host(struct client *cl)
862 return client_get_database(cl)->database->host;
865 const char *client_get_url(struct client *cl)
868 return client_get_database(cl)->database->url;
873 void client_set_maxrecs(struct client *cl, int v)
878 void client_set_startrecs(struct client *cl, int v)
886 * c-file-style: "Stroustrup"
887 * indent-tabs-mode: nil
889 * vim: shiftwidth=4 tabstop=8 expandtab