+int session_is_preferred_clients_ready(struct session *s)
+{
+ struct client_list *l;
+ int res = 0;
+
+ for (l = s->clients_active; l; l = l->next)
+ if (client_is_active_preferred(l->client))
+ res++;
+ session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
+ return res == 0;
+}
+
+static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
+{
+ reclist_destroy(se->reclist);
+ se->reclist = 0;
+ if (nmem_total(se->nmem))
+ session_log(se, YLOG_DEBUG, "NMEN operation usage %zd",
+ nmem_total(se->nmem));
+ nmem_reset(se->nmem);
+ se->total_records = se->total_merged = 0;
+ se->num_termlists = 0;
+
+ /* reset list of sorted results and clear to relevance search */
+ se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
+ se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
+ se->sorted_results->increasing = sp->increasing;
+ se->sorted_results->type = sp->type;
+ se->sorted_results->next = 0;
+
+ session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
+ sp->name, sp->increasing, sp->type);
+
+ se->reclist = reclist_create(se->nmem);
+}
+
+static void session_sort_unlocked(struct session *se, struct reclist_sortparms *sp)
+{
+ struct reclist_sortparms *sr;
+ struct client_list *l;
+ const char *field = sp->name;
+ int increasing = sp->increasing;
+ int type = sp->type;
+ int clients_research = 0;
+
+ yaz_log(YLOG_LOG, "session_sort field=%s increasing=%d type=%d", field, increasing, type);
+ /* see if we already have sorted for this criteria */
+ for (sr = se->sorted_results; sr; sr = sr->next)
+ {
+ if (!reclist_sortparms_cmp(sr,sp))
+ break;
+ }
+ if (sr)
+ {
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d already fetched",
+ field, increasing, type);
+ return;
+ }
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d must fetch",
+ field, increasing, type);
+
+ // We need to reset reclist on every sort that changes the records, not just for position
+ // So if just one client requires new searching, we need to clear set.
+ // Ask each of the client if sorting requires re-search due to native sort
+ // If it does it will require us to
+ for (l = se->clients_active; l; l = l->next)
+ {
+ struct client *cl = l->client;
+ // Assume no re-search is required.
+ client_parse_init(cl, 1);
+ clients_research += client_parse_sort(cl, sp);
+ }
+ if (clients_research) {
+ yaz_log(YLOG_DEBUG, "Reset results due to %d clients researching", clients_research);
+ session_clear_set(se, sp);
+ }
+ else {
+ // A new sorting based on same record set
+ sr = nmem_malloc(se->nmem, sizeof(*sr));
+ sr->name = nmem_strdup(se->nmem, field);
+ sr->increasing = increasing;
+ sr->type = type;
+ sr->next = se->sorted_results;
+ se->sorted_results = sr;
+ session_log(se, YLOG_DEBUG, "No research/ingesting done");
+ return ;
+ }
+ session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
+
+ for (l = se->clients_active; l; l = l->next)
+ {
+ struct client *cl = l->client;
+ if (client_get_state(cl) == Client_Connecting ||
+ client_get_state(cl) == Client_Idle ||
+ client_get_state(cl) == Client_Working) {
+ client_start_search(cl);
+ }
+ else {
+ yaz_log(YLOG_DEBUG, "Client %s: No re-start/ingest in show. Wrong client state: %d",
+ client_get_id(cl), client_get_state(cl));
+ }
+
+ }
+}
+
+void session_sort(struct session *se, struct reclist_sortparms *sp) {
+ //session_enter(se, "session_sort");
+ session_sort_unlocked(se, sp);
+ //session_leave(se, "session_sort");
+}