+static void session_clear_set(struct session *se,
+ const char *sort_field, int increasing, int position)
+{
+ 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->field = nmem_strdup(se->nmem, sort_field);
+ se->sorted_results->increasing = increasing;
+ se->sorted_results->position = position;
+ se->sorted_results->next = 0;
+
+ session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d position=%d configured",
+ sort_field, increasing, position);
+
+ se->reclist = reclist_create(se->nmem);
+}
+
+void session_sort(struct session *se, const char *field, int increasing,
+ int position)
+{
+ struct session_sorted_results *sr;
+ struct client_list *l;
+
+ session_enter(se);
+
+ yaz_log(YLOG_LOG, "session_sort field=%s increasing=%d position=%d", field, increasing, position);
+ /* see if we already have sorted for this critieria */
+ for (sr = se->sorted_results; sr; sr = sr->next)
+ {
+ if (!strcmp(field, sr->field) && increasing == sr->increasing && sr->position == position)
+ break;
+ }
+ if (sr)
+ {
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d position=%d already fetched",
+ field, increasing, position);
+ session_leave(se);
+ return;
+ }
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d position=%d must fetch",
+ field, increasing, position);
+ if (position)
+ {
+ yaz_log(YLOG_DEBUG, "Reset results due to position");
+ session_clear_set(se, field, increasing, position);
+ }
+ else {
+ sr = nmem_malloc(se->nmem, sizeof(*sr));
+ sr->field = nmem_strdup(se->nmem, field);
+ sr->increasing = increasing;
+ sr->position = position;
+ sr->next = se->sorted_results;
+ se->sorted_results = sr;
+ }
+ yaz_log(YLOG_DEBUG, "Restarting search 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) {
+ yaz_log(YLOG_DEBUG, "Client %s: Restarting search due to change in sort order", client_get_id(cl));
+ client_start_search(cl);
+ }
+ }
+ session_leave(se);
+}
+
+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)