+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;
+ }
+ // yaz_log(YLOG_DEBUG, "Restarting search or re-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: Not 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");
+}
+
+
+enum pazpar2_error_code session_search(struct session *se,
+ const char *query,
+ const char *startrecs,
+ const char *maxrecs,
+ const char *filter,
+ const char *limit,
+ const char **addinfo,
+ struct reclist_sortparms *sp)