+ WRBUF w = wrbuf_alloc();
+
+ struct setting *s;
+
+ for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
+ {
+ const char *p = strchr(s->name + 3, ':');
+ if (!p)
+ {
+ yaz_log(YLOG_WARN, "Malformed facetmap name: %s", s->name);
+ }
+ else if (s->value && s->value[0])
+ {
+ wrbuf_puts(w, "@attr 1=");
+ yaz_encode_pqf_term(w, s->value, strlen(s->value));
+ if (s->next)
+ wrbuf_puts(w, ",");
+ }
+ }
+ yaz_log(YLOG_DEBUG, "using facets str: %s", wrbuf_cstr(w));
+ ZOOM_connection_option_set(link, "facets",
+ wrbuf_len(w) ? wrbuf_cstr(w) : 0);
+ wrbuf_destroy(w);
+}
+
+int client_has_facet(struct client *cl, const char *name)
+{
+ struct session_database *sdb = client_get_database(cl);
+ struct setting *s;
+
+ for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
+ {
+ const char *p = strchr(s->name + 3, ':');
+ if (p && !strcmp(name, p + 1))
+ return 1;
+ }
+ return 0;
+}
+
+static const char *get_strategy_plus_sort(struct client *l, const char *field)
+{
+ struct session_database *sdb = client_get_database(l);
+ struct setting *s;
+
+ const char *strategy_plus_sort = 0;
+
+ for (s = sdb->settings[PZ_SORTMAP]; s; s = s->next)
+ {
+ char *p = strchr(s->name + 3, ':');
+ if (!p)
+ {
+ yaz_log(YLOG_WARN, "Malformed sortmap name: %s", s->name);
+ continue;
+ }
+ p++;
+ if (!strcmp(p, field))
+ {
+ strategy_plus_sort = s->value;
+ break;
+ }
+ }
+ return strategy_plus_sort;
+}
+
+void client_update_show_stat(struct client *cl, int cmd)
+{
+ if (cmd == 0)
+ cl->show_stat_no = 0;
+ else if (cmd == 1)
+ cl->show_stat_no++;
+}
+
+int client_fetch_more(struct client *cl)
+{
+ struct session_database *sdb = client_get_database(cl);
+ const char *str;
+ int extend_recs = 0;
+ int number = cl->hits - cl->record_offset;
+
+ str = session_setting_oneval(sdb, PZ_EXTENDRECS);
+ if (!str || !*str)
+ return 0;
+
+ extend_recs = atoi(str);
+
+ yaz_log(YLOG_LOG, "cl=%s show_stat_no=%d got=%d",
+ client_get_id(cl), cl->show_stat_no, cl->record_offset);
+ if (cl->show_stat_no < cl->record_offset)
+ return 0;
+ yaz_log(YLOG_LOG, "cl=%s Trying to fetch more", client_get_id(cl));
+
+ if (number > extend_recs)
+ number = extend_recs;
+ if (number > 0)
+ {
+ ZOOM_resultset set = cl->resultset;
+ struct connection *co = client_get_connection(cl);
+
+ str = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
+ ZOOM_resultset_option_set(set, "preferredRecordSyntax", str);
+ str = session_setting_oneval(sdb, PZ_ELEMENTS);
+ if (str && *str)
+ ZOOM_resultset_option_set(set, "elementSetName", str);
+
+ ZOOM_resultset_records(set, 0, cl->record_offset, number);
+ client_set_state(cl, Client_Working);
+ connection_continue(co);
+ return 1;
+ }
+ else
+ {
+ yaz_log(YLOG_LOG, "cl=%s. OK no more in total set", client_get_id(cl));
+ }
+ return 0;
+}
+
+int client_parse_init(struct client *cl, int same_search)
+{
+ cl->same_search = same_search;
+ return 0;
+}
+
+/*
+ * TODO consider how to extend the range
+ * */
+int client_parse_range(struct client *cl, const char *startrecs,
+ const char *maxrecs)
+{
+ if (maxrecs && atoi(maxrecs) != cl->maxrecs)
+ {
+ cl->same_search = 0;
+ cl->maxrecs = atoi(maxrecs);
+ }
+
+ if (startrecs && atoi(startrecs) != cl->startrecs)
+ {
+ cl->same_search = 0;
+ cl->startrecs = atoi(startrecs);
+ }
+
+ return 0;
+}
+
+int client_start_search(struct client *cl)
+{
+ struct session_database *sdb = client_get_database(cl);
+ struct connection *co = 0;
+ ZOOM_connection link = 0;
+ struct session *se = client_get_session(cl);
+ ZOOM_resultset rs;
+ const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
+ const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
+ const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
+ const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
+ const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
+ const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
+ const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
+ const char *opt_preferred = session_setting_oneval(sdb, PZ_PREFERRED);
+ const char *extra_args = session_setting_oneval(sdb, PZ_EXTRA_ARGS);
+ const char *opt_present_chunk = session_setting_oneval(sdb, PZ_PRESENT_CHUNK);
+ ZOOM_query query;
+ char maxrecs_str[24], startrecs_str[24], present_chunk_str[24];
+ struct timeval tval;
+ int present_chunk = 20; // Default chunk size
+ int rc_prep_connection;
+
+
+ yaz_gettimeofday(&tval);
+ tval.tv_sec += 5;
+
+ if (opt_present_chunk && strcmp(opt_present_chunk,"")) {
+ present_chunk = atoi(opt_present_chunk);
+ yaz_log(YLOG_DEBUG, "Present chunk set to %d", present_chunk);
+ }
+ rc_prep_connection =
+ client_prep_connection(cl, se->service->z3950_operation_timeout,
+ se->service->z3950_session_timeout,
+ se->service->server->iochan_man,
+ &tval);
+ /* Nothing has changed and we already have a result */
+ if (cl->same_search == 1 && rc_prep_connection == 2)
+ {
+ session_log(se, YLOG_LOG, "client %s resuse result", client_get_id(cl));
+ return client_reingest(cl);
+ }
+ else if (!rc_prep_connection)
+ {
+ session_log(se, YLOG_LOG, "client %s postponing search: No connection",
+ client_get_id(cl));
+ return -1;
+ }
+ co = client_get_connection(cl);
+ assert(cl);
+ link = connection_get_link(co);