1 /* This file is part of Pazpar2.
2 Copyright (C) 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
42 #include <yaz/marcdisp.h>
43 #include <yaz/comstack.h>
44 #include <yaz/tcpip.h>
45 #include <yaz/proto.h>
46 #include <yaz/readconf.h>
47 #include <yaz/pquery.h>
48 #include <yaz/otherinfo.h>
49 #include <yaz/yaz-util.h>
51 #include <yaz/query-charset.h>
52 #include <yaz/querytowrbuf.h>
53 #include <yaz/oid_db.h>
54 #include <yaz/diagbib1.h>
55 #include <yaz/snprintf.h>
56 #include <yaz/rpn2cql.h>
57 #include <yaz/rpn2solr.h>
58 #include <yaz/gettimeofday.h>
62 #include <yaz/timing.h>
67 #include "parameters.h"
69 #include "connection.h"
71 #include "relevance.h"
74 #define XDOC_CACHE_SIZE 100
76 static YAZ_MUTEX g_mutex = 0;
77 static int no_clients = 0;
79 static int client_use(int delta)
83 yaz_mutex_create(&g_mutex);
84 yaz_mutex_enter(g_mutex);
87 yaz_mutex_leave(g_mutex);
88 yaz_log(YLOG_DEBUG, "%s clients=%d",
89 delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), clients);
93 int clients_count(void)
98 /** \brief Represents client state for a connection to one search target */
100 struct session_database *database;
101 struct connection *connection;
102 struct session *session;
103 char *pquery; // Current search
104 char *cqlquery; // used for SRU targets only
105 char *addinfo; // diagnostic info for most resent error
109 int filtered; /* number of records ignored for local filtering */
115 struct suggestions *suggestions;
116 enum client_state state;
117 struct show_raw *show_raw;
118 ZOOM_resultset resultset;
122 facet_limits_t facet_limits;
138 int active; // whether this request has been sent to the server
144 void (*error_handler)(void *data, const char *addinfo);
145 void (*record_handler)(void *data, const char *buf, size_t sz);
147 struct show_raw *next;
150 static const char *client_states[] = {
156 "Client_Disconnected"
159 const char *client_get_state_str(struct client *cl)
161 return client_states[cl->state];
164 enum client_state client_get_state(struct client *cl)
169 void client_set_state_nb(struct client *cl, enum client_state st)
174 void client_set_state(struct client *cl, enum client_state st)
177 if (client_is_active(cl))
180 /* If client is going from being active to inactive and all clients
181 are now idle we fire a watch for the session . The assumption is
182 that session is not mutex locked if client is already active */
183 if (was_active && !client_is_active(cl) && cl->session)
186 int no_active = session_active_clients(cl->session);
187 yaz_log(YLOG_DEBUG, "%s: releasing watches on zero active: %d",
188 client_get_id(cl), no_active);
189 if (no_active == 0) {
190 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
191 session_alert_watch(cl->session, SESSION_WATCH_BYTARGET);
192 session_alert_watch(cl->session, SESSION_WATCH_TERMLIST);
193 session_alert_watch(cl->session, SESSION_WATCH_SHOW_PREF);
198 static void client_init_xdoc(struct client *cl)
202 cl->xdoc = xmalloc(sizeof(*cl->xdoc) * XDOC_CACHE_SIZE);
203 for (i = 0; i < XDOC_CACHE_SIZE; i++)
207 static void client_destroy_xdoc(struct client *cl)
212 for (i = 0; i < XDOC_CACHE_SIZE; i++)
214 xmlFreeDoc(cl->xdoc[i]);
218 xmlDoc *client_get_xdoc(struct client *cl, int record_no)
221 if (record_no >= 0 && record_no < XDOC_CACHE_SIZE)
222 return cl->xdoc[record_no];
226 void client_store_xdoc(struct client *cl, int record_no, xmlDoc *xdoc)
229 if (record_no >= 0 && record_no < XDOC_CACHE_SIZE)
231 if (cl->xdoc[record_no])
232 xmlFreeDoc(cl->xdoc[record_no]);
233 cl->xdoc[record_no] = xdoc;
242 static void client_show_raw_error(struct client *cl, const char *addinfo);
244 struct connection *client_get_connection(struct client *cl)
246 return cl->connection;
249 struct session_database *client_get_database(struct client *cl)
254 struct session *client_get_session(struct client *cl)
259 static void client_send_raw_present(struct client *cl);
260 static int nativesyntax_to_type(const char *s, char *type, ZOOM_record rec);
262 static void client_show_immediate(
263 ZOOM_resultset resultset, struct session_database *sdb, int position,
265 void (*error_handler)(void *data, const char *addinfo),
266 void (*record_handler)(void *data, const char *buf, size_t sz),
268 const char *nativesyntax)
277 error_handler(data, "no resultset");
280 rec = ZOOM_resultset_record_immediate(resultset, position-1);
283 error_handler(data, "no record");
286 nativesyntax_to_type(nativesyntax, type, rec);
287 buf = ZOOM_record_get(rec, type, &len);
290 error_handler(data, "no record");
293 record_handler(data, buf, len);
297 int client_show_raw_begin(struct client *cl, int position,
298 const char *syntax, const char *esn,
300 void (*error_handler)(void *data, const char *addinfo),
301 void (*record_handler)(void *data, const char *buf,
304 const char *nativesyntax)
309 nativesyntax = "raw";
312 struct session_database *sdb = client_get_database(cl);
313 nativesyntax = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
317 if (syntax == 0 && esn == 0)
318 client_show_immediate(cl->resultset, client_get_database(cl),
320 error_handler, record_handler,
321 binary, nativesyntax);
324 struct show_raw *rr, **rrp;
330 rr = xmalloc(sizeof(*rr));
331 rr->position = position;
334 rr->error_handler = error_handler;
335 rr->record_handler = record_handler;
338 rr->syntax = xstrdup(syntax);
342 rr->esn = xstrdup(esn);
346 assert(nativesyntax);
347 rr->nativesyntax = xstrdup(nativesyntax);
351 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
355 if (cl->state == Client_Failed)
357 client_show_raw_error(cl, "client failed");
359 else if (cl->state == Client_Disconnected)
361 client_show_raw_error(cl, "client disconnected");
365 client_send_raw_present(cl);
371 static void client_show_raw_delete(struct show_raw *r)
375 xfree(r->nativesyntax);
379 void client_show_raw_remove(struct client *cl, void *data)
381 struct show_raw *rr = data;
382 struct show_raw **rrp = &cl->show_raw;
388 client_show_raw_delete(rr);
392 static void client_show_raw_dequeue(struct client *cl)
394 struct show_raw *rr = cl->show_raw;
396 cl->show_raw = rr->next;
397 client_show_raw_delete(rr);
400 static void client_show_raw_error(struct client *cl, const char *addinfo)
404 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
405 client_show_raw_dequeue(cl);
409 static void client_send_raw_present(struct client *cl)
411 struct session_database *sdb = client_get_database(cl);
412 struct connection *co = client_get_connection(cl);
413 ZOOM_resultset set = cl->resultset;
415 int offset = cl->show_raw->position;
416 const char *syntax = 0;
417 const char *elements = 0;
419 assert(cl->show_raw);
422 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
423 client_get_id(cl), 1, offset);
425 if (cl->show_raw->syntax)
426 syntax = cl->show_raw->syntax;
428 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
429 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
431 if (cl->show_raw->esn)
432 elements = cl->show_raw->esn;
434 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
435 if (elements && *elements)
436 ZOOM_resultset_option_set(set, "elementSetName", elements);
438 ZOOM_resultset_records(set, 0, offset-1, 1);
439 cl->show_raw->active = 1;
441 connection_continue(co);
444 static int nativesyntax_to_type(const char *s, char *type,
449 if (!strncmp(s, "iso2709", 7))
451 const char *cp = strchr(s, ';');
452 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
454 else if (!strncmp(s, "txml", 4))
456 const char *cp = strchr(s, ';');
457 yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
459 else /* pass verbatim to ZOOM - including "xml" */
463 else /* attempt to deduce structure */
465 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
468 if (!strcmp(syntax, "XML"))
473 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
475 strcpy(type, "xml; charset=marc8-s");
485 * TODO Consider thread safety!!!
488 static void client_report_facets(struct client *cl, ZOOM_resultset rs)
490 struct session_database *sdb = client_get_database(cl);
491 ZOOM_facet_field *facets = ZOOM_resultset_facets(rs);
495 struct session *se = client_get_session(cl);
496 int facet_num = ZOOM_resultset_facets_size(rs);
499 for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
501 const char *p = strchr(s->name + 3, ':');
502 if (p && p[1] && s->value && s->value[0])
505 p++; /* p now holds logical facet name */
506 for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
508 const char *native_name =
509 ZOOM_facet_field_name(facets[facet_idx]);
510 if (native_name && !strcmp(s->value, native_name))
514 ZOOM_facet_field_term_count(facets[facet_idx]);
515 for (term_idx = 0; term_idx < term_num; term_idx++ )
519 ZOOM_facet_field_get_term(facets[facet_idx],
522 add_facet(se, p, term, freq);
532 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
538 nativesyntax_to_type(cl->show_raw->nativesyntax, type, rec);
539 buf = ZOOM_record_get(rec, type, &len);
540 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
541 client_show_raw_dequeue(cl);
544 void client_check_preferred_watch(struct client *cl)
546 struct session *se = cl->session;
547 yaz_log(YLOG_DEBUG, "client_check_preferred_watch: %s ", client_get_id(cl));
551 /* TODO possible threading issue. Session can have been destroyed */
552 if (session_is_preferred_clients_ready(se)) {
553 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
556 yaz_log(YLOG_DEBUG, "client_check_preferred_watch: Still locked on preferred targets.");
561 yaz_log(YLOG_WARN, "client_check_preferred_watch: %s. No session!", client_get_id(cl));
565 struct suggestions* client_suggestions_create(const char* suggestions_string);
566 static void client_suggestions_destroy(struct client *cl);
568 void client_search_response(struct client *cl)
570 struct connection *co = cl->connection;
571 ZOOM_connection link = connection_get_link(co);
572 ZOOM_resultset resultset = cl->resultset;
573 struct session *se = client_get_session(cl);
575 const char *error, *addinfo = 0;
577 if (ZOOM_connection_error(link, &error, &addinfo))
580 client_set_state(cl, Client_Error);
581 session_log(se, YLOG_WARN, "%s: Error %s (%s)",
582 client_get_id(cl), error, addinfo);
586 client_report_facets(cl, resultset);
587 cl->record_offset = cl->startrecs;
588 cl->hits = ZOOM_resultset_size(resultset);
589 session_log(se, YLOG_LOG, "%s: hits: " ODR_INT_PRINTF,
590 client_get_id(cl), cl->hits);
592 client_suggestions_destroy(cl);
594 client_suggestions_create(ZOOM_resultset_option_get(
595 resultset, "suggestions"));
599 void client_got_records(struct client *cl)
601 struct session *se = cl->session;
604 if (reclist_get_num_records(se->reclist) > 0)
607 session_alert_watch(se, SESSION_WATCH_SHOW);
608 session_alert_watch(se, SESSION_WATCH_BYTARGET);
609 session_alert_watch(se, SESSION_WATCH_TERMLIST);
610 session_alert_watch(se, SESSION_WATCH_RECORD);
616 static void client_record_ingest(struct client *cl)
618 const char *msg, *addinfo;
620 ZOOM_resultset resultset = cl->resultset;
621 struct session *se = client_get_session(cl);
623 int offset = cl->record_offset + 1; /* 0 versus 1 numbered offsets */
625 xdoc = client_get_xdoc(cl, offset);
630 NMEM nmem = nmem_create();
631 int rc = ingest_xml_record(cl, xdoc, offset, nmem, 1);
633 session_log(se, YLOG_WARN,
634 "Failed to ingest xdoc from %s #%d",
635 client_get_id(cl), offset);
641 else if ((rec = ZOOM_resultset_record_immediate(resultset,
644 if (cl->session == 0)
646 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
648 session_log(se, YLOG_WARN, "Record error %s (%s): %s #%d",
649 msg, addinfo, client_get_id(cl), offset);
653 struct session_database *sdb = client_get_database(cl);
654 NMEM nmem = nmem_create();
658 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
659 if (nativesyntax_to_type(s, type, rec))
660 session_log(se, YLOG_WARN, "Failed to determine record type");
661 xmlrec = ZOOM_record_get(rec, type, NULL);
664 const char *rec_syn = ZOOM_record_get(rec, "syntax", NULL);
665 session_log(se, YLOG_WARN, "ZOOM_record_get failed from %s #%d",
666 client_get_id(cl), offset);
667 session_log(se, YLOG_LOG, "pz:nativesyntax=%s . "
668 "ZOOM record type=%s . Actual record syntax=%s",
669 s ? s : "null", type,
670 rec_syn ? rec_syn : "null");
674 /* OK = 0, -1 = failure, -2 = Filtered */
675 int rc = ingest_record(cl, xmlrec, offset, nmem);
678 const char *rec_syn = ZOOM_record_get(rec, "syntax", NULL);
679 session_log(se, YLOG_WARN,
680 "Failed to ingest record from %s #%d",
681 client_get_id(cl), offset);
682 session_log(se, YLOG_LOG, "pz:nativesyntax=%s . "
683 "ZOOM record type=%s . Actual record syntax=%s",
684 s ? s : "null", type,
685 rec_syn ? rec_syn : "null");
695 session_log(se, YLOG_WARN, "Got NULL record from %s #%d",
696 client_get_id(cl), offset);
701 void client_record_response(struct client *cl, int *got_records)
703 struct connection *co = cl->connection;
704 ZOOM_connection link = connection_get_link(co);
705 ZOOM_resultset resultset = cl->resultset;
706 const char *error, *addinfo;
708 if (ZOOM_connection_error(link, &error, &addinfo))
710 client_set_state(cl, Client_Error);
711 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
712 error, addinfo, client_get_id(cl));
716 if (cl->show_raw && cl->show_raw->active)
719 if ((rec = ZOOM_resultset_record_immediate(
720 resultset, cl->show_raw->position-1)))
722 cl->show_raw->active = 0;
723 ingest_raw_record(cl, rec);
727 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
728 cl->show_raw->position-1);
733 client_record_ingest(cl);
739 int client_reingest(struct client *cl)
741 int i = cl->startrecs;
742 int to = cl->record_offset;
745 cl->record_offset = i;
747 client_record_ingest(cl);
751 static void client_set_facets_request(struct client *cl, ZOOM_connection link)
753 struct session_database *sdb = client_get_database(cl);
755 WRBUF w = wrbuf_alloc();
759 for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
761 const char *p = strchr(s->name + 3, ':');
764 yaz_log(YLOG_WARN, "Malformed facetmap name: %s", s->name);
766 else if (s->value && s->value[0])
768 wrbuf_puts(w, "@attr 1=");
769 yaz_encode_pqf_term(w, s->value, strlen(s->value));
774 yaz_log(YLOG_DEBUG, "using facets str: %s", wrbuf_cstr(w));
775 ZOOM_connection_option_set(link, "facets",
776 wrbuf_len(w) ? wrbuf_cstr(w) : 0);
780 int client_has_facet(struct client *cl, const char *name)
782 struct session_database *sdb = client_get_database(cl);
785 for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
787 const char *p = strchr(s->name + 3, ':');
788 if (p && !strcmp(name, p + 1))
794 static const char *get_strategy_plus_sort(struct client *l, const char *field)
796 struct session_database *sdb = client_get_database(l);
799 const char *strategy_plus_sort = 0;
801 for (s = sdb->settings[PZ_SORTMAP]; s; s = s->next)
803 char *p = strchr(s->name + 3, ':');
806 yaz_log(YLOG_WARN, "Malformed sortmap name: %s", s->name);
810 if (!strcmp(p, field))
812 strategy_plus_sort = s->value;
816 return strategy_plus_sort;
819 void client_update_show_stat(struct client *cl, int cmd)
822 cl->show_stat_no = 0;
827 int client_fetch_more(struct client *cl)
829 struct session_database *sdb = client_get_database(cl);
832 int number = cl->hits - cl->record_offset;
834 str = session_setting_oneval(sdb, PZ_EXTENDRECS);
838 extend_recs = atoi(str);
840 yaz_log(YLOG_LOG, "cl=%s show_stat_no=%d got=%d",
841 client_get_id(cl), cl->show_stat_no, cl->record_offset);
842 if (cl->show_stat_no < cl->record_offset)
844 yaz_log(YLOG_LOG, "cl=%s Trying to fetch more", client_get_id(cl));
846 if (number > extend_recs)
847 number = extend_recs;
850 ZOOM_resultset set = cl->resultset;
851 struct connection *co = client_get_connection(cl);
853 str = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
854 ZOOM_resultset_option_set(set, "preferredRecordSyntax", str);
855 str = session_setting_oneval(sdb, PZ_ELEMENTS);
857 ZOOM_resultset_option_set(set, "elementSetName", str);
859 ZOOM_resultset_records(set, 0, cl->record_offset, number);
860 client_set_state(cl, Client_Working);
861 connection_continue(co);
866 yaz_log(YLOG_LOG, "cl=%s. OK no more in total set", client_get_id(cl));
871 int client_parse_init(struct client *cl, int same_search)
873 cl->same_search = same_search;
878 * TODO consider how to extend the range
880 int client_parse_range(struct client *cl, const char *startrecs,
883 if (maxrecs && atoi(maxrecs) != cl->maxrecs)
886 cl->maxrecs = atoi(maxrecs);
889 if (startrecs && atoi(startrecs) != cl->startrecs)
892 cl->startrecs = atoi(startrecs);
898 int client_start_search(struct client *cl)
900 struct session_database *sdb = client_get_database(cl);
901 struct connection *co = 0;
902 ZOOM_connection link = 0;
903 struct session *se = client_get_session(cl);
905 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
906 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
907 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
908 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
909 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
910 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
911 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
912 const char *opt_preferred = session_setting_oneval(sdb, PZ_PREFERRED);
913 const char *extra_args = session_setting_oneval(sdb, PZ_EXTRA_ARGS);
914 const char *opt_present_chunk = session_setting_oneval(sdb, PZ_PRESENT_CHUNK);
916 char maxrecs_str[24], startrecs_str[24], present_chunk_str[24];
918 int present_chunk = 20; // Default chunk size
919 int rc_prep_connection;
922 yaz_gettimeofday(&tval);
925 if (opt_present_chunk && strcmp(opt_present_chunk,"")) {
926 present_chunk = atoi(opt_present_chunk);
927 yaz_log(YLOG_DEBUG, "Present chunk set to %d", present_chunk);
930 client_prep_connection(cl, se->service->z3950_operation_timeout,
931 se->service->z3950_session_timeout,
932 se->service->server->iochan_man,
934 /* Nothing has changed and we already have a result */
935 if (cl->same_search == 1 && rc_prep_connection == 2)
937 session_log(se, YLOG_LOG, "%s: reuse result", client_get_id(cl));
938 client_report_facets(cl, cl->resultset);
939 return client_reingest(cl);
941 else if (!rc_prep_connection)
943 session_log(se, YLOG_LOG, "%s: postponing search: No connection",
945 client_set_state_nb(cl, Client_Working);
948 co = client_get_connection(cl);
950 link = connection_get_link(co);
953 session_log(se, YLOG_LOG, "%s: new search", client_get_id(cl));
958 client_destroy_xdoc(cl);
959 client_init_xdoc(cl);
961 if (extra_args && *extra_args)
962 ZOOM_connection_option_set(link, "extraArgs", extra_args);
965 cl->preferred = atoi(opt_preferred);
967 yaz_log(YLOG_LOG, "Target %s has preferred status: %d",
968 client_get_id(cl), cl->preferred);
972 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
974 ZOOM_connection_option_set(link, "piggyback", "1");
976 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
977 if (*opt_sru && *opt_elements)
978 ZOOM_connection_option_set(link, "schema", opt_elements);
979 else if (*opt_elements)
980 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
982 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
984 if (opt_maxrecs && *opt_maxrecs)
986 cl->maxrecs = atoi(opt_maxrecs);
989 /* convert back to string representation used in ZOOM API */
990 sprintf(maxrecs_str, "%d", cl->maxrecs);
991 ZOOM_connection_option_set(link, "count", maxrecs_str);
993 /* A present_chunk less than 1 will disable chunking. */
994 if (present_chunk > 0 && cl->maxrecs > present_chunk) {
995 sprintf(present_chunk_str, "%d", present_chunk);
996 ZOOM_connection_option_set(link, "presentChunk", present_chunk_str);
997 yaz_log(YLOG_DEBUG, "Present chunk set to %s", present_chunk_str);
1000 ZOOM_connection_option_set(link, "presentChunk", maxrecs_str);
1001 yaz_log(YLOG_DEBUG, "Present chunk set to %s (maxrecs)", maxrecs_str);
1003 sprintf(startrecs_str, "%d", cl->startrecs);
1004 ZOOM_connection_option_set(link, "start", startrecs_str);
1006 /* TODO Verify does it break something for CQL targets(non-SOLR) ? */
1007 /* facets definition is in PQF */
1008 client_set_facets_request(cl, link);
1010 query = ZOOM_query_create();
1013 session_log(se, YLOG_LOG, "%s: Search CQL: %s", client_get_id(cl),
1015 ZOOM_query_cql(query, cl->cqlquery);
1017 ZOOM_query_sortby(query, opt_sort);
1021 session_log(se, YLOG_LOG, "%s: Search PQF: %s", client_get_id(cl),
1023 ZOOM_query_prefix(query, cl->pquery);
1025 if (cl->sort_strategy && cl->sort_criteria) {
1026 yaz_log(YLOG_LOG, "Client %s: "
1027 "Set ZOOM sort strategy and criteria: %s %s",
1028 client_get_id(cl), cl->sort_strategy, cl->sort_criteria);
1029 ZOOM_query_sortby2(query, cl->sort_strategy, cl->sort_criteria);
1032 yaz_log(YLOG_DEBUG,"Client %s: Starting search", client_get_id(cl));
1033 client_set_state(cl, Client_Working);
1035 cl->record_offset = 0;
1036 rs = ZOOM_connection_search(link, query);
1037 ZOOM_query_destroy(query);
1038 ZOOM_resultset_destroy(cl->resultset);
1040 connection_continue(co);
1044 struct client *client_create(const char *id)
1046 struct client *cl = xmalloc(sizeof(*cl));
1057 cl->record_offset = 0;
1060 cl->state = Client_Disconnected;
1063 cl->suggestions = 0;
1065 pazpar2_mutex_create(&cl->mutex, "client");
1068 cl->facet_limits = 0;
1069 cl->sort_strategy = 0;
1070 cl->sort_criteria = 0;
1072 cl->id = xstrdup(id);
1073 client_init_xdoc(cl);
1076 yaz_log(YLOG_DEBUG, "client_create c=%p %s", cl, id);
1080 void client_lock(struct client *c)
1082 yaz_mutex_enter(c->mutex);
1085 void client_unlock(struct client *c)
1087 yaz_mutex_leave(c->mutex);
1090 void client_incref(struct client *c)
1092 pazpar2_incref(&c->ref_count, c->mutex);
1093 yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
1094 c, client_get_id(c), c->ref_count);
1097 int client_destroy(struct client *c)
1101 yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
1102 c, client_get_id(c), c->ref_count);
1103 if (!pazpar2_decref(&c->ref_count, c->mutex))
1114 xfree(c->sort_strategy);
1115 xfree(c->sort_criteria);
1116 assert(!c->connection);
1117 facet_limits_destroy(c->facet_limits);
1119 client_destroy_xdoc(c);
1122 ZOOM_resultset_destroy(c->resultset);
1124 yaz_mutex_destroy(&c->mutex);
1133 void client_set_connection(struct client *cl, struct connection *con)
1136 ZOOM_resultset_release(cl->resultset);
1139 assert(cl->connection == 0);
1140 cl->connection = con;
1145 cl->connection = con;
1150 void client_disconnect(struct client *cl)
1152 if (cl->state != Client_Idle)
1153 client_set_state(cl, Client_Disconnected);
1154 client_set_connection(cl, 0);
1157 // Initialize CCL map for a target
1158 static CCL_bibset prepare_cclmap(struct client *cl, CCL_bibset base_bibset)
1160 struct session_database *sdb = client_get_database(cl);
1167 res = ccl_qual_dup(base_bibset);
1169 res = ccl_qual_mk();
1170 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
1172 const char *addinfo = 0;
1173 char *p = strchr(s->name + 3, ':');
1176 WRBUF w = wrbuf_alloc();
1177 wrbuf_printf(w, "Malformed cclmap. name=%s", s->name);
1178 yaz_log(YLOG_WARN, "%s: %s", client_get_id(cl), wrbuf_cstr(w));
1179 client_set_diagnostic(cl, ZOOM_ERROR_CCL_CONFIG,
1180 ZOOM_diag_str(ZOOM_ERROR_CCL_CONFIG),
1182 client_set_state_nb(cl, Client_Error);
1188 if (ccl_qual_fitem2(res, s->value, p, &addinfo))
1190 WRBUF w = wrbuf_alloc();
1192 wrbuf_printf(w, "Malformed cclmap. name=%s: value=%s (%s)",
1193 s->name, p, addinfo);
1194 yaz_log(YLOG_WARN, "%s: %s", client_get_id(cl), wrbuf_cstr(w));
1195 client_set_diagnostic(cl, ZOOM_ERROR_CCL_CONFIG,
1196 ZOOM_diag_str(ZOOM_ERROR_CCL_CONFIG),
1198 client_set_state_nb(cl, Client_Error);
1207 // returns a xmalloced CQL query corresponding to the pquery in client
1208 static char *make_cqlquery(struct client *cl, Z_RPNQuery *zquery)
1210 cql_transform_t cqlt = cql_transform_create();
1212 WRBUF wrb = wrbuf_alloc();
1215 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
1217 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
1221 r = xstrdup(wrbuf_cstr(wrb));
1224 cql_transform_close(cqlt);
1228 // returns a xmalloced SOLR query corresponding to the pquery in client
1229 // TODO Could prob. be merge with the similar make_cqlquery
1230 static char *make_solrquery(struct client *cl, Z_RPNQuery *zquery)
1232 solr_transform_t sqlt = solr_transform_create();
1234 WRBUF wrb = wrbuf_alloc();
1237 if ((status = solr_transform_rpn2solr_wrbuf(sqlt, wrb, zquery)))
1239 yaz_log(YLOG_WARN, "Failed to generate SOLR query, code=%d", status);
1243 r = xstrdup(wrbuf_cstr(wrb));
1246 solr_transform_close(sqlt);
1250 const char *client_get_facet_limit_local(struct client *cl,
1251 struct session_database *sdb,
1253 NMEM nmem, int *num, char ***values)
1255 const char *name = 0;
1256 const char *value = 0;
1257 for (; (name = facet_limits_get(cl->facet_limits, *l, &value)); (*l)++)
1259 struct setting *s = 0;
1261 for (s = sdb->settings[PZ_LIMITMAP]; s; s = s->next)
1263 const char *p = strchr(s->name + 3, ':');
1264 if (p && !strcmp(p + 1, name) && s->value)
1268 nmem_strsplit_escape2(nmem, ",", s->value, &cvalues,
1270 for (j = 0; j < cnum; j++)
1272 const char *cvalue = cvalues[j];
1273 while (*cvalue == ' ')
1275 if (!strncmp(cvalue, "local:", 6))
1277 const char *cp = cvalue + 6;
1280 nmem_strsplit_escape2(nmem, "|", value, values,
1283 return *cp ? cp : name;
1292 static int apply_limit(struct session_database *sdb,
1293 facet_limits_t facet_limits,
1294 WRBUF w_pqf, CCL_bibset ccl_map,
1295 struct conf_service *service)
1302 NMEM nmem_tmp = nmem_create();
1303 for (i = 0; (name = facet_limits_get(facet_limits, i, &value)); i++)
1305 struct setting *s = 0;
1306 nmem_reset(nmem_tmp);
1307 /* name="pz:limitmap:author" value="rpn:@attr 1=4|local:other" */
1308 for (s = sdb->settings[PZ_LIMITMAP]; s; s = s->next)
1310 const char *p = strchr(s->name + 3, ':');
1311 if (p && !strcmp(p + 1, name) && s->value)
1317 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1320 nmem_strsplit_escape2(nmem_tmp, ",", s->value, &cvalues,
1323 for (j = 0; ret == 0 && j < cnum; j++)
1325 const char *cvalue = cvalues[j];
1326 while (*cvalue == ' ')
1328 if (!strncmp(cvalue, "rpn:", 4))
1330 const char *pqf = cvalue + 4;
1331 wrbuf_puts(w_pqf, "@and ");
1332 wrbuf_puts(w_pqf, pqf);
1333 wrbuf_puts(w_pqf, " ");
1334 for (i = 0; i < num; i++)
1337 wrbuf_puts(w_pqf, "@or ");
1338 yaz_encode_pqf_term(w_pqf, values[i],
1342 else if (!strncmp(cvalue, "ccl:", 4))
1344 const char *ccl = cvalue + 4;
1345 WRBUF ccl_w = wrbuf_alloc();
1346 for (i = 0; i < num; i++)
1349 struct ccl_rpn_node *cn;
1350 wrbuf_rewind(ccl_w);
1351 wrbuf_puts(ccl_w, ccl);
1352 wrbuf_puts(ccl_w, "=\"");
1353 wrbuf_puts(ccl_w, values[i]);
1354 wrbuf_puts(ccl_w, "\"");
1356 cn = ccl_find_str(ccl_map, wrbuf_cstr(ccl_w),
1361 wrbuf_printf(w_pqf, "@and ");
1363 /* or multiple values.. could be bad if last
1364 CCL parse fails, but this is unlikely to
1367 wrbuf_printf(w_pqf, "@or ");
1368 ccl_pquery(w_pqf, cn);
1372 wrbuf_destroy(ccl_w);
1374 else if (!strncmp(cvalue, "local:", 6)) {
1379 yaz_log(YLOG_WARN, "Target %s: Bad limitmap '%s'",
1380 sdb->database->id, cvalue);
1381 ret = -1; /* bad limitmap */
1390 for (i = 0; i < service->num_metadata; i++)
1392 struct conf_metadata *md = service->metadata + i;
1393 if (!strcmp(md->name, name) && md->limitcluster)
1395 yaz_log(YLOG_LOG, "limitcluster in use for %s",
1400 if (i == service->num_metadata)
1402 yaz_log(YLOG_WARN, "Target %s: limit %s used, but no limitmap defined",
1403 (sdb->database ? sdb->database->id : "<no id>"), name);
1407 nmem_destroy(nmem_tmp);
1411 // Parse the query given the settings specific to this client
1412 // client variable same_search is set as below as well as returned:
1413 // 0 if query is OK but different from before
1414 // 1 if query is OK but same as before
1415 // return -1 on query error
1416 // return -2 on limit error
1417 int client_parse_query(struct client *cl, const char *query,
1418 facet_limits_t facet_limits, const char **error_msg)
1420 struct session *se = client_get_session(cl);
1421 struct conf_service *service = se->service;
1422 struct session_database *sdb = client_get_database(cl);
1423 struct ccl_rpn_node *cn;
1426 CCL_bibset ccl_map = prepare_cclmap(cl, service->ccl_bibset);
1427 const char *sru = session_setting_oneval(sdb, PZ_SRU);
1428 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
1429 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
1430 const char *query_syntax = session_setting_oneval(sdb, PZ_QUERY_SYNTAX);
1438 w_ccl = wrbuf_alloc();
1439 wrbuf_puts(w_ccl, query);
1441 w_pqf = wrbuf_alloc();
1444 wrbuf_puts(w_pqf, pqf_prefix);
1445 wrbuf_puts(w_pqf, " ");
1448 if (apply_limit(sdb, facet_limits, w_pqf, ccl_map, service))
1450 ccl_qual_rm(&ccl_map);
1454 facet_limits_destroy(cl->facet_limits);
1455 cl->facet_limits = facet_limits_dup(facet_limits);
1457 yaz_log(YLOG_LOG, "Client %s: CCL query: %s limit: %s",
1458 client_get_id(cl), wrbuf_cstr(w_ccl), wrbuf_cstr(w_pqf));
1459 cn = ccl_find_str(ccl_map, wrbuf_cstr(w_ccl), &cerror, &cpos);
1460 ccl_qual_rm(&ccl_map);
1464 *error_msg = ccl_err_msg(cerror);
1465 client_set_state(cl, Client_Error);
1466 session_log(se, YLOG_WARN, "Client %s: Failed to parse CCL query '%s'",
1469 wrbuf_destroy(w_ccl);
1470 wrbuf_destroy(w_pqf);
1473 wrbuf_destroy(w_ccl);
1475 if (!pqf_strftime || !*pqf_strftime)
1476 ccl_pquery(w_pqf, cn);
1479 time_t cur_time = time(0);
1480 struct tm *tm = localtime(&cur_time);
1482 const char *cp = tmp_str;
1484 /* see man strftime(3) for things .. In particular %% gets converted
1485 to %.. And That's our original query .. */
1486 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
1490 ccl_pquery(w_pqf, cn);
1492 wrbuf_putc(w_pqf, cp[0]);
1496 /* Compares query and limit with old one. If different we need to research */
1497 if (!cl->pquery || strcmp(cl->pquery, wrbuf_cstr(w_pqf)))
1500 session_log(se, YLOG_LOG, "Client %s: "
1501 "Re-search due query/limit change: %s to %s",
1502 client_get_id(cl), cl->pquery, wrbuf_cstr(w_pqf));
1504 cl->pquery = xstrdup(wrbuf_cstr(w_pqf));
1505 // return value is no longer used.
1507 // Need to (re)search
1510 wrbuf_destroy(w_pqf);
1512 xfree(cl->cqlquery);
1515 odr_out = odr_createmem(ODR_ENCODE);
1516 zquery = p_query_rpn(odr_out, cl->pquery);
1520 session_log(se, YLOG_WARN, "Invalid PQF query for Client %s: %s",
1521 client_get_id(cl), cl->pquery);
1523 *error_msg = "Invalid PQF after CCL to PQF conversion";
1527 session_log(se, YLOG_LOG, "PQF for Client %s: %s",
1528 client_get_id(cl), cl->pquery);
1530 /* Support for PQF on SRU targets. */
1531 if (strcmp(query_syntax, "pqf") != 0 && *sru)
1533 if (!strcmp(sru, "solr"))
1534 cl->cqlquery = make_solrquery(cl, zquery);
1536 cl->cqlquery = make_cqlquery(cl, zquery);
1539 *error_msg = "Cannot convert PQF to Solr/CQL";
1543 session_log(se, YLOG_LOG, "Client %s native query: %s (%s)",
1544 client_get_id(cl), cl->cqlquery, sru);
1547 odr_destroy(odr_out);
1549 /* TODO FIX Not thread safe */
1552 // Initialize relevance structure with query terms
1553 se->relevance = relevance_create_ccl(se->service->charsets, cn,
1554 se->service->rank_cluster,
1555 se->service->rank_follow,
1556 se->service->rank_lead,
1557 se->service->rank_length);
1563 int client_parse_sort(struct client *cl, struct reclist_sortparms *sp)
1567 const char *sort_strategy_and_spec =
1568 get_strategy_plus_sort(cl, sp->name);
1569 int increasing = sp->increasing;
1570 if (!strcmp(sp->name, "relevance"))
1572 if (sort_strategy_and_spec && strlen(sort_strategy_and_spec) < 40)
1574 char strategy[50], *p;
1575 strcpy(strategy, sort_strategy_and_spec);
1576 p = strchr(strategy, ':');
1579 // Split the string in two
1587 yaz_log(YLOG_LOG, "Client %s: "
1588 "applying sorting %s %s", client_get_id(cl),
1590 if (!cl->sort_strategy || strcmp(cl->sort_strategy, strategy))
1591 cl->same_search = 0;
1592 if (!cl->sort_criteria || strcmp(cl->sort_criteria, p))
1593 cl->same_search = 0;
1594 if (cl->same_search == 0) {
1595 xfree(cl->sort_strategy);
1596 cl->sort_strategy = xstrdup(strategy);
1597 xfree(cl->sort_criteria);
1598 cl->sort_criteria = xstrdup(p);
1602 yaz_log(YLOG_LOG, "Client %s: "
1603 "Invalid sort strategy and spec found %s",
1604 client_get_id(cl), sort_strategy_and_spec);
1605 xfree(cl->sort_strategy);
1606 cl->sort_strategy = 0;
1607 xfree(cl->sort_criteria);
1608 cl->sort_criteria = 0;
1613 yaz_log(YLOG_DEBUG, "Client %s: "
1614 "No sort strategy and spec found.", client_get_id(cl));
1615 xfree(cl->sort_strategy);
1616 cl->sort_strategy = 0;
1617 xfree(cl->sort_criteria);
1618 cl->sort_criteria = 0;
1622 return !cl->same_search;
1625 void client_set_session(struct client *cl, struct session *se)
1630 int client_is_active(struct client *cl)
1632 if (cl->connection && (cl->state == Client_Connecting ||
1633 cl->state == Client_Working))
1638 int client_is_active_preferred(struct client *cl)
1640 /* only count if this is a preferred target. */
1643 /* TODO No sure this the condition that Seb wants */
1644 if (cl->connection && (cl->state == Client_Connecting ||
1645 cl->state == Client_Working))
1650 Odr_int client_get_hits(struct client *cl)
1655 Odr_int client_get_approximation(struct client *cl)
1657 if (cl->record_offset > 0)
1659 Odr_int approx = ((10 * cl->hits * (cl->record_offset - cl->filtered))
1660 / cl->record_offset + 5) /10;
1661 yaz_log(YLOG_DEBUG, "%s: Approx: %lld * %d / %d = %lld ",
1662 client_get_id(cl), cl->hits,
1663 cl->record_offset - cl->filtered, cl->record_offset, approx);
1669 int client_get_num_records(struct client *cl)
1671 return cl->record_offset;
1674 int client_get_num_records_filtered(struct client *cl)
1676 return cl->filtered;
1679 void client_set_diagnostic(struct client *cl, int diagnostic,
1680 const char *message, const char *addinfo)
1682 cl->diagnostic = diagnostic;
1684 cl->message = xstrdup(message);
1688 cl->addinfo = xstrdup(addinfo);
1691 int client_get_diagnostic(struct client *cl, const char **message,
1692 const char **addinfo)
1695 *message = cl->message;
1697 *addinfo = cl->addinfo;
1698 return cl->diagnostic;
1701 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf)
1704 struct suggestions *suggestions = cl->suggestions;
1708 if (suggestions->passthrough)
1710 yaz_log(YLOG_DEBUG, "Passthrough Suggestions: \n%s\n",
1711 suggestions->passthrough);
1712 return suggestions->passthrough;
1714 if (suggestions->num == 0)
1717 for (idx = 0; idx < suggestions->num; idx++) {
1718 wrbuf_printf(wrbuf, "<suggest term=\"%s\"", suggestions->suggest[idx]);
1719 if (suggestions->misspelled[idx] && suggestions->misspelled[idx]) {
1720 wrbuf_puts(wrbuf, suggestions->misspelled[idx]);
1721 wrbuf_puts(wrbuf, "</suggest>\n");
1724 wrbuf_puts(wrbuf, "/>\n");
1727 return wrbuf_cstr(wrbuf);
1731 void client_set_database(struct client *cl, struct session_database *db)
1736 const char *client_get_id(struct client *cl)
1741 int client_get_maxrecs(struct client *cl)
1746 void client_set_preferred(struct client *cl, int v)
1752 struct suggestions* client_suggestions_create(const char* suggestions_string)
1756 struct suggestions *suggestions;
1757 if (suggestions_string == 0 || suggestions_string[0] == 0 )
1759 nmem = nmem_create();
1760 suggestions = nmem_malloc(nmem, sizeof(*suggestions));
1761 yaz_log(YLOG_DEBUG, "client target suggestions: %s.", suggestions_string);
1763 suggestions->nmem = nmem;
1764 suggestions->num = 0;
1765 suggestions->misspelled = 0;
1766 suggestions->suggest = 0;
1767 suggestions->passthrough = nmem_strdup_null(nmem, suggestions_string);
1769 if (suggestions_string)
1770 nmem_strsplit_escape2(suggestions->nmem, "\n", suggestions_string, &suggestions->suggest,
1771 &suggestions->num, 1, '\\', 0);
1772 /* Set up misspelled array */
1773 suggestions->misspelled = (char **)
1774 nmem_malloc(nmem, suggestions->num * sizeof(*suggestions->misspelled));
1775 /* replace = with \0 .. for each item */
1776 for (i = 0; i < suggestions->num; i++)
1778 char *cp = strchr(suggestions->suggest[i], '=');
1781 suggestions->misspelled[i] = cp+1;
1787 static void client_suggestions_destroy(struct client *cl)
1789 NMEM nmem = cl->suggestions->nmem;
1790 cl->suggestions = 0;
1797 * c-file-style: "Stroustrup"
1798 * indent-tabs-mode: nil
1800 * vim: shiftwidth=4 tabstop=8 expandtab