1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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
39 #include <yaz/marcdisp.h>
40 #include <yaz/comstack.h>
41 #include <yaz/tcpip.h>
42 #include <yaz/proto.h>
43 #include <yaz/readconf.h>
44 #include <yaz/pquery.h>
45 #include <yaz/otherinfo.h>
46 #include <yaz/yaz-util.h>
48 #include <yaz/query-charset.h>
49 #include <yaz/querytowrbuf.h>
50 #include <yaz/oid_db.h>
51 #include <yaz/diagbib1.h>
52 #include <yaz/snprintf.h>
53 #include <yaz/rpn2cql.h>
54 #include <yaz/rpn2solr.h>
58 #include <yaz/timing.h>
63 #include "parameters.h"
65 #include "connection.h"
67 #include "relevance.h"
70 static YAZ_MUTEX g_mutex = 0;
71 static int no_clients = 0;
72 static int no_clients_total = 0;
74 static int client_use(int delta)
78 yaz_mutex_create(&g_mutex);
79 yaz_mutex_enter(g_mutex);
82 no_clients_total += delta;
84 yaz_mutex_leave(g_mutex);
85 yaz_log(YLOG_DEBUG, "%s clients=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), clients);
89 int clients_count(void) {
93 int clients_count_total(void) {
97 yaz_mutex_enter(g_mutex);
98 total = no_clients_total;
99 yaz_mutex_leave(g_mutex);
104 /** \brief Represents client state for a connection to one search target */
106 struct session_database *database;
107 struct connection *connection;
108 struct session *session;
109 char *pquery; // Current search
110 char *cqlquery; // used for SRU targets only
117 struct suggestions *suggestions;
118 enum client_state state;
119 struct show_raw *show_raw;
120 ZOOM_resultset resultset;
134 int active; // whether this request has been sent to the server
139 void (*error_handler)(void *data, const char *addinfo);
140 void (*record_handler)(void *data, const char *buf, size_t sz);
142 struct show_raw *next;
145 static const char *client_states[] = {
151 "Client_Disconnected"
154 const char *client_get_state_str(struct client *cl)
156 return client_states[cl->state];
159 enum client_state client_get_state(struct client *cl)
164 void client_set_state(struct client *cl, enum client_state st)
167 if (client_is_active(cl))
170 /* If client is going from being active to inactive and all clients
171 are now idle we fire a watch for the session . The assumption is
172 that session is not mutex locked if client is already active */
173 if (was_active && !client_is_active(cl) && cl->session)
176 int no_active = session_active_clients(cl->session);
177 yaz_log(YLOG_DEBUG, "%s: releasing watches on zero active: %d",
178 client_get_id(cl), no_active);
179 if (no_active == 0) {
180 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
181 session_alert_watch(cl->session, SESSION_WATCH_SHOW_PREF);
186 static void client_show_raw_error(struct client *cl, const char *addinfo);
188 struct connection *client_get_connection(struct client *cl)
190 return cl->connection;
193 struct session_database *client_get_database(struct client *cl)
198 struct session *client_get_session(struct client *cl)
203 const char *client_get_pquery(struct client *cl)
208 static void client_send_raw_present(struct client *cl);
209 static int nativesyntax_to_type(struct session_database *sdb, char *type,
212 static void client_show_immediate(
213 ZOOM_resultset resultset, struct session_database *sdb, int position,
215 void (*error_handler)(void *data, const char *addinfo),
216 void (*record_handler)(void *data, const char *buf, size_t sz),
226 error_handler(data, "no resultset");
229 rec = ZOOM_resultset_record(resultset, position-1);
232 error_handler(data, "no record");
238 nativesyntax_to_type(sdb, type, rec);
239 buf = ZOOM_record_get(rec, type, &len);
242 error_handler(data, "no record");
245 record_handler(data, buf, len);
249 int client_show_raw_begin(struct client *cl, int position,
250 const char *syntax, const char *esn,
252 void (*error_handler)(void *data, const char *addinfo),
253 void (*record_handler)(void *data, const char *buf,
257 if (syntax == 0 && esn == 0)
258 client_show_immediate(cl->resultset, client_get_database(cl),
260 error_handler, record_handler,
264 struct show_raw *rr, **rrp;
270 rr = xmalloc(sizeof(*rr));
271 rr->position = position;
274 rr->error_handler = error_handler;
275 rr->record_handler = record_handler;
278 rr->syntax = xstrdup(syntax);
282 rr->esn = xstrdup(esn);
287 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
291 if (cl->state == Client_Failed)
293 client_show_raw_error(cl, "client failed");
295 else if (cl->state == Client_Disconnected)
297 client_show_raw_error(cl, "client disconnected");
301 client_send_raw_present(cl);
307 static void client_show_raw_delete(struct show_raw *r)
314 void client_show_raw_remove(struct client *cl, void *data)
316 struct show_raw *rr = data;
317 struct show_raw **rrp = &cl->show_raw;
323 client_show_raw_delete(rr);
327 void client_show_raw_dequeue(struct client *cl)
329 struct show_raw *rr = cl->show_raw;
331 cl->show_raw = rr->next;
332 client_show_raw_delete(rr);
335 static void client_show_raw_error(struct client *cl, const char *addinfo)
339 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
340 client_show_raw_dequeue(cl);
344 static void client_send_raw_present(struct client *cl)
346 struct session_database *sdb = client_get_database(cl);
347 struct connection *co = client_get_connection(cl);
348 ZOOM_resultset set = cl->resultset;
350 int offset = cl->show_raw->position;
351 const char *syntax = 0;
352 const char *elements = 0;
354 assert(cl->show_raw);
357 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
358 client_get_id(cl), 1, offset);
360 if (cl->show_raw->syntax)
361 syntax = cl->show_raw->syntax;
363 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
364 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
366 if (cl->show_raw->esn)
367 elements = cl->show_raw->esn;
369 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
370 if (elements && *elements)
371 ZOOM_resultset_option_set(set, "elementSetName", elements);
373 ZOOM_resultset_records(set, 0, offset-1, 1);
374 cl->show_raw->active = 1;
376 connection_continue(co);
379 static int nativesyntax_to_type(struct session_database *sdb, char *type,
382 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
386 if (!strncmp(s, "iso2709", 7))
388 const char *cp = strchr(s, ';');
389 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
391 else if (!strncmp(s, "xml", 3))
395 else if (!strncmp(s, "txml", 4))
397 const char *cp = strchr(s, ';');
398 yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
404 else /* attempt to deduce structure */
406 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
409 if (!strcmp(syntax, "XML"))
414 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
416 strcpy(type, "xml; charset=marc8-s");
426 * TODO Consider thread safety!!!
429 void client_report_facets(struct client *cl, ZOOM_resultset rs)
431 struct session_database *sdb = client_get_database(cl);
432 ZOOM_facet_field *facets = ZOOM_resultset_facets(rs);
436 struct session *se = client_get_session(cl);
437 int facet_num = ZOOM_resultset_facets_size(rs);
440 for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
442 const char *p = strchr(s->name + 3, ':');
443 if (p && p[1] && s->value && s->value[0])
446 p++; /* p now holds logical facet name */
447 for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
449 const char *native_name =
450 ZOOM_facet_field_name(facets[facet_idx]);
451 if (native_name && !strcmp(s->value, native_name))
455 ZOOM_facet_field_term_count(facets[facet_idx]);
456 for (term_idx = 0; term_idx < term_num; term_idx++ )
460 ZOOM_facet_field_get_term(facets[facet_idx],
463 add_facet(se, p, term, freq);
473 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
479 if (cl->show_raw->binary)
483 struct session_database *sdb = client_get_database(cl);
484 nativesyntax_to_type(sdb, type, rec);
487 buf = ZOOM_record_get(rec, type, &len);
488 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
489 client_show_raw_dequeue(cl);
492 void client_check_preferred_watch(struct client *cl)
494 struct session *se = cl->session;
495 yaz_log(YLOG_DEBUG, "client_check_preferred_watch: %s ", client_get_id(cl));
499 /* TODO possible threading issue. Session can have been destroyed */
500 if (session_is_preferred_clients_ready(se)) {
501 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
504 yaz_log(YLOG_DEBUG, "client_check_preferred_watch: Still locked on preferred targets.");
509 yaz_log(YLOG_WARN, "client_check_preferred_watch: %s. No session!", client_get_id(cl));
513 struct suggestions* client_suggestions_create(const char* suggestions_string);
514 static void client_suggestions_destroy(struct client *cl);
516 void client_search_response(struct client *cl)
518 struct connection *co = cl->connection;
519 ZOOM_connection link = connection_get_link(co);
520 ZOOM_resultset resultset = cl->resultset;
522 const char *error, *addinfo = 0;
524 if (ZOOM_connection_error(link, &error, &addinfo))
527 client_set_state(cl, Client_Error);
528 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
529 error, addinfo, client_get_id(cl));
533 yaz_log(YLOG_DEBUG, "client_search_response: hits "
534 ODR_INT_PRINTF, cl->hits);
535 client_report_facets(cl, resultset);
536 cl->record_offset = cl->startrecs;
537 cl->hits = ZOOM_resultset_size(resultset);
539 client_suggestions_destroy(cl);
540 cl->suggestions = client_suggestions_create(ZOOM_resultset_option_get(resultset, "suggestions"));
544 void client_got_records(struct client *cl)
546 struct session *se = cl->session;
550 session_alert_watch(se, SESSION_WATCH_SHOW);
551 session_alert_watch(se, SESSION_WATCH_RECORD);
556 void client_record_response(struct client *cl)
558 struct connection *co = cl->connection;
559 ZOOM_connection link = connection_get_link(co);
560 ZOOM_resultset resultset = cl->resultset;
561 const char *error, *addinfo;
563 if (ZOOM_connection_error(link, &error, &addinfo))
565 client_set_state(cl, Client_Error);
566 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
567 error, addinfo, client_get_id(cl));
572 const char *msg, *addinfo;
574 if (cl->show_raw && cl->show_raw->active)
576 if ((rec = ZOOM_resultset_record(resultset,
577 cl->show_raw->position-1)))
579 cl->show_raw->active = 0;
580 ingest_raw_record(cl, rec);
584 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
585 cl->show_raw->position-1);
590 int offset = cl->record_offset;
591 if ((rec = ZOOM_resultset_record(resultset, offset)))
594 if (cl->session == 0)
596 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
598 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
599 msg, addinfo, client_get_id(cl),
604 struct session_database *sdb = client_get_database(cl);
605 NMEM nmem = nmem_create();
609 if (nativesyntax_to_type(sdb, type, rec))
610 yaz_log(YLOG_WARN, "Failed to determine record type");
611 xmlrec = ZOOM_record_get(rec, type, NULL);
613 yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
617 /* OK = 0, -1 = failure, -2 = Filtered */
618 if (ingest_record(cl, xmlrec, cl->record_offset, nmem) == -1)
619 yaz_log(YLOG_WARN, "Failed to ingest from %s", client_get_id(cl));
626 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
633 static void client_set_facets_request(struct client *cl, ZOOM_connection link)
635 struct session_database *sdb = client_get_database(cl);
637 WRBUF w = wrbuf_alloc();
641 for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
643 const char *p = strchr(s->name + 3, ':');
646 yaz_log(YLOG_WARN, "Malformed facetmap name: %s", s->name);
648 else if (s->value && s->value[0])
650 wrbuf_puts(w, "@attr 1=");
651 yaz_encode_pqf_term(w, s->value, strlen(s->value));
656 yaz_log(YLOG_LOG, "using facets str: %s", wrbuf_cstr(w));
657 ZOOM_connection_option_set(link, "facets",
658 wrbuf_len(w) ? wrbuf_cstr(w) : 0);
662 int client_has_facet(struct client *cl, const char *name)
664 struct session_database *sdb = client_get_database(cl);
667 for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
669 const char *p = strchr(s->name + 3, ':');
670 if (p && !strcmp(name, p + 1))
676 void client_start_search(struct client *cl, const char *sort_strategy_and_spec,
679 struct session_database *sdb = client_get_database(cl);
680 struct connection *co = client_get_connection(cl);
681 ZOOM_connection link = connection_get_link(co);
683 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
684 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
685 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
686 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
687 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
688 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
689 const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
690 const char *opt_preferred = session_setting_oneval(sdb, PZ_PREFERRED);
691 const char *extra_args = session_setting_oneval(sdb, PZ_EXTRA_ARGS);
692 char maxrecs_str[24], startrecs_str[24];
697 cl->record_offset = 0;
700 if (extra_args && *extra_args)
701 ZOOM_connection_option_set(link, "extraArgs", extra_args);
704 cl->preferred = atoi(opt_preferred);
706 yaz_log(YLOG_LOG, "Target %s has preferred status: %d",
707 client_get_id(cl), cl->preferred);
709 client_set_state(cl, Client_Working);
712 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
714 ZOOM_connection_option_set(link, "piggyback", "1");
716 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
717 if (*opt_sru && *opt_elements)
718 ZOOM_connection_option_set(link, "schema", opt_elements);
719 else if (*opt_elements)
720 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
722 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
724 if (opt_maxrecs && *opt_maxrecs)
726 cl->maxrecs = atoi(opt_maxrecs);
729 /* convert back to string representation used in ZOOM API */
730 sprintf(maxrecs_str, "%d", cl->maxrecs);
731 ZOOM_connection_option_set(link, "count", maxrecs_str);
733 if (cl->maxrecs > 20)
734 ZOOM_connection_option_set(link, "presentChunk", "20");
736 ZOOM_connection_option_set(link, "presentChunk", maxrecs_str);
738 sprintf(startrecs_str, "%d", cl->startrecs);
739 ZOOM_connection_option_set(link, "start", startrecs_str);
741 /* TODO Verify does it break something for CQL targets(non-SOLR) ? */
742 /* facets definition is in PQF */
743 client_set_facets_request(cl, link);
745 q = ZOOM_query_create();
748 yaz_log(YLOG_LOG, "Search %s CQL: %s", client_get_id(cl),
750 ZOOM_query_cql(q, cl->cqlquery);
752 ZOOM_query_sortby(q, opt_sort);
756 yaz_log(YLOG_LOG, "Search %s PQF: %s", client_get_id(cl), cl->pquery);
758 ZOOM_query_prefix(q, cl->pquery);
760 if (sort_strategy_and_spec &&
761 strlen(sort_strategy_and_spec) < 40 /* spec below */)
764 strcpy(spec, sort_strategy_and_spec);
765 p = strchr(spec, ':');
768 *p++ = '\0'; /* cut the string in two */
775 yaz_log(YLOG_LOG, "applying %s %s", spec, p);
776 ZOOM_query_sortby2(q, spec, p);
779 rs = ZOOM_connection_search(link, q);
780 ZOOM_query_destroy(q);
781 ZOOM_resultset_destroy(cl->resultset);
783 connection_continue(co);
786 struct client *client_create(const char *id)
788 struct client *cl = xmalloc(sizeof(*cl));
797 cl->record_offset = 0;
799 cl->state = Client_Disconnected;
804 pazpar2_mutex_create(&cl->mutex, "client");
808 cl->id = xstrdup(id);
814 void client_lock(struct client *c)
816 yaz_mutex_enter(c->mutex);
819 void client_unlock(struct client *c)
821 yaz_mutex_leave(c->mutex);
824 void client_incref(struct client *c)
826 pazpar2_incref(&c->ref_count, c->mutex);
827 yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
828 c, client_get_id(c), c->ref_count);
831 int client_destroy(struct client *c)
835 yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
836 c, client_get_id(c), c->ref_count);
837 if (!pazpar2_decref(&c->ref_count, c->mutex))
844 assert(!c->connection);
848 ZOOM_resultset_destroy(c->resultset);
850 yaz_mutex_destroy(&c->mutex);
859 void client_set_connection(struct client *cl, struct connection *con)
862 ZOOM_resultset_release(cl->resultset);
865 assert(cl->connection == 0);
866 cl->connection = con;
871 cl->connection = con;
876 void client_disconnect(struct client *cl)
878 if (cl->state != Client_Idle)
879 client_set_state(cl, Client_Disconnected);
880 client_set_connection(cl, 0);
884 // Initialize CCL map for a target
885 static CCL_bibset prepare_cclmap(struct client *cl)
887 struct session_database *sdb = client_get_database(cl);
894 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
896 char *p = strchr(s->name + 3, ':');
899 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
904 ccl_qual_fitem(res, s->value, p);
909 // returns a xmalloced CQL query corresponding to the pquery in client
910 static char *make_cqlquery(struct client *cl)
912 cql_transform_t cqlt = cql_transform_create();
915 WRBUF wrb = wrbuf_alloc();
917 ODR odr_out = odr_createmem(ODR_ENCODE);
919 zquery = p_query_rpn(odr_out, cl->pquery);
920 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
921 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
923 yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
928 r = xstrdup(wrbuf_cstr(wrb));
931 odr_destroy(odr_out);
932 cql_transform_close(cqlt);
936 // returns a xmalloced SOLR query corresponding to the pquery in client
937 // TODO Could prob. be merge with the similar make_cqlquery
938 static char *make_solrquery(struct client *cl)
940 solr_transform_t sqlt = solr_transform_create();
943 WRBUF wrb = wrbuf_alloc();
945 ODR odr_out = odr_createmem(ODR_ENCODE);
947 zquery = p_query_rpn(odr_out, cl->pquery);
949 yaz_log(YLOG_WARN, "Failed to generate RPN from PQF: %s", cl->pquery);
952 yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
953 if ((status = solr_transform_rpn2solr_wrbuf(sqlt, wrb, zquery)))
955 yaz_log(YLOG_WARN, "Failed to generate SOLR query from PQF %s, code=%d", cl->pquery, status);
960 r = xstrdup(wrbuf_cstr(wrb));
963 odr_destroy(odr_out);
964 solr_transform_close(sqlt);
968 static void apply_limit(struct session_database *sdb,
969 facet_limits_t facet_limits,
970 WRBUF w_pqf, WRBUF w_ccl)
975 NMEM nmem_tmp = nmem_create();
976 for (i = 0; (name = facet_limits_get(facet_limits, i, &value)); i++)
978 struct setting *s = 0;
980 for (s = sdb->settings[PZ_LIMITMAP]; s; s = s->next)
982 const char *p = strchr(s->name + 3, ':');
983 if (p && !strcmp(p + 1, name) && s->value)
987 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
990 if (!strncmp(s->value, "rpn:", 4))
992 const char *pqf = s->value + 4;
994 wrbuf_puts(w_pqf, "@and ");
995 wrbuf_puts(w_pqf, pqf);
996 wrbuf_puts(w_pqf, " ");
997 for (i = 0; i < num; i++)
1000 wrbuf_puts(w_pqf, "@or ");
1001 yaz_encode_pqf_term(w_pqf, values[i],
1005 else if (!strncmp(s->value, "ccl:", 4))
1007 const char *ccl = s->value + 4;
1009 wrbuf_puts(w_ccl, " and (");
1011 for (i = 0; i < num; i++)
1014 wrbuf_puts(w_ccl, " or ");
1015 wrbuf_puts(w_ccl, ccl);
1016 wrbuf_puts(w_ccl, "=\"");
1017 wrbuf_puts(w_ccl, values[i]);
1018 wrbuf_puts(w_ccl, "\"");
1020 wrbuf_puts(w_ccl, ")");
1026 nmem_reset(nmem_tmp);
1029 yaz_log(YLOG_WARN, "Target %s: limit %s used, but no limitmap defined",
1030 (sdb->database ? sdb->database->id : "<no id>"), name);
1033 nmem_destroy(nmem_tmp);
1036 // Parse the query given the settings specific to this client
1037 int client_parse_query(struct client *cl, const char *query,
1038 facet_limits_t facet_limits)
1040 struct session *se = client_get_session(cl);
1041 struct session_database *sdb = client_get_database(cl);
1042 struct ccl_rpn_node *cn;
1044 CCL_bibset ccl_map = prepare_cclmap(cl);
1045 const char *sru = session_setting_oneval(sdb, PZ_SRU);
1046 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
1047 const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
1048 const char *query_syntax = session_setting_oneval(sdb, PZ_QUERY_SYNTAX);
1054 w_ccl = wrbuf_alloc();
1055 wrbuf_puts(w_ccl, query);
1057 w_pqf = wrbuf_alloc();
1060 wrbuf_puts(w_pqf, pqf_prefix);
1061 wrbuf_puts(w_pqf, " ");
1064 apply_limit(sdb, facet_limits, w_pqf, w_ccl);
1066 yaz_log(YLOG_LOG, "CCL query: %s", wrbuf_cstr(w_ccl));
1067 cn = ccl_find_str(ccl_map, wrbuf_cstr(w_ccl), &cerror, &cpos);
1068 ccl_qual_rm(&ccl_map);
1071 client_set_state(cl, Client_Error);
1072 session_log(se, YLOG_WARN, "Failed to parse CCL query '%s' for %s",
1075 wrbuf_destroy(w_ccl);
1076 wrbuf_destroy(w_pqf);
1079 wrbuf_destroy(w_ccl);
1081 if (!pqf_strftime || !*pqf_strftime)
1082 ccl_pquery(w_pqf, cn);
1085 time_t cur_time = time(0);
1086 struct tm *tm = localtime(&cur_time);
1088 const char *cp = tmp_str;
1090 /* see man strftime(3) for things .. In particular %% gets converted
1091 to %.. And That's our original query .. */
1092 strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
1096 ccl_pquery(w_pqf, cn);
1098 wrbuf_putc(w_pqf, cp[0]);
1102 cl->pquery = xstrdup(wrbuf_cstr(w_pqf));
1103 wrbuf_destroy(w_pqf);
1105 yaz_log(YLOG_LOG, "PQF query: %s", cl->pquery);
1107 xfree(cl->cqlquery);
1109 /* Support for PQF on SRU targets. */
1111 yaz_log(YLOG_DEBUG, "Query syntax: %s", query_syntax);
1112 if (strcmp(query_syntax, "pqf") != 0 && *sru)
1114 if (!strcmp(sru, "solr")) {
1115 if (!(cl->cqlquery = make_solrquery(cl)))
1119 if (!(cl->cqlquery = make_cqlquery(cl)))
1126 /* TODO FIX Not thread safe */
1129 // Initialize relevance structure with query terms
1130 se->relevance = relevance_create_ccl(
1131 se->service->charsets, se->nmem, cn);
1138 void client_set_session(struct client *cl, struct session *se)
1143 int client_is_active(struct client *cl)
1145 if (cl->connection && (cl->state == Client_Connecting ||
1146 cl->state == Client_Working))
1151 int client_is_active_preferred(struct client *cl)
1153 /* only count if this is a preferred target. */
1156 /* TODO No sure this the condition that Seb wants */
1157 if (cl->connection && (cl->state == Client_Connecting ||
1158 cl->state == Client_Working))
1163 Odr_int client_get_hits(struct client *cl)
1168 int client_get_num_records(struct client *cl)
1170 return cl->record_offset;
1173 void client_set_diagnostic(struct client *cl, int diagnostic)
1175 cl->diagnostic = diagnostic;
1178 int client_get_diagnostic(struct client *cl)
1180 return cl->diagnostic;
1183 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf)
1186 struct suggestions *suggestions = cl->suggestions;
1187 if (!suggestions || suggestions->num == 0) {
1190 for (idx = 0; idx < suggestions->num; idx++) {
1191 wrbuf_printf(wrbuf, "<suggest term=\"%s\"", suggestions->suggest[idx]);
1192 if (suggestions->misspelled[idx] && suggestions->misspelled[idx]) {
1193 wrbuf_puts(wrbuf, suggestions->misspelled[idx]);
1194 wrbuf_puts(wrbuf, "</suggest>\n");
1197 wrbuf_puts(wrbuf, "/>\n");
1199 return wrbuf_cstr(wrbuf);
1203 void client_set_database(struct client *cl, struct session_database *db)
1208 const char *client_get_id(struct client *cl)
1213 void client_set_maxrecs(struct client *cl, int v)
1218 int client_get_maxrecs(struct client *cl)
1223 void client_set_startrecs(struct client *cl, int v)
1228 void client_set_preferred(struct client *cl, int v)
1234 struct suggestions* client_suggestions_create(const char* suggestions_string) {
1235 if (suggestions_string == 0)
1238 NMEM nmem = nmem_create();
1239 struct suggestions *suggestions = nmem_malloc(nmem, sizeof(*suggestions));
1240 suggestions->nmem = nmem;
1241 suggestions->num = 0;
1242 suggestions->misspelled = 0;
1243 suggestions->suggest = 0;
1244 if (suggestions_string)
1245 nmem_strsplit_escape2(suggestions->nmem, "\n", suggestions_string, &suggestions->suggest,
1246 &suggestions->num, 1, '\\', 0);
1247 /* Set up misspelled array */
1248 suggestions->misspelled = (char **) nmem_malloc(nmem, suggestions->num * sizeof(**suggestions->misspelled));
1249 /* replace = with \0 .. for each item */
1250 for (i = 0; i < suggestions->num; i++)
1252 char *cp = strchr(suggestions->suggest[i], '=');
1255 suggestions->misspelled[i] = cp+1;
1261 static void client_suggestions_destroy(struct client *cl)
1263 nmem_destroy(cl->suggestions->nmem);
1264 cl->suggestions = 0;
1270 * c-file-style: "Stroustrup"
1271 * indent-tabs-mode: nil
1273 * vim: shiftwidth=4 tabstop=8 expandtab