refactor the search/show to command to use common code path
[pazpar2-moved-to-github.git] / src / session.c
index e2eda61..fa4b4a6 100644 (file)
@@ -56,7 +56,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/querytowrbuf.h>
 #include <yaz/oid_db.h>
 #include <yaz/snprintf.h>
-#include <yaz/gettimeofday.h>
 
 #define USE_TIMING 0
 #if USE_TIMING
@@ -619,8 +618,7 @@ int session_is_preferred_clients_ready(struct session *s)
     return res == 0;
 }
 
-static void session_clear_set(struct session *se,
-                              const char *sort_field, int increasing, int position)
+static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
 {
     reclist_destroy(se->reclist);
     se->reclist = 0;
@@ -633,55 +631,69 @@ static void session_clear_set(struct session *se,
 
     /* 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->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 position=%d configured",
-                sort_field, increasing, position);
+    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);
 }
 
-void session_sort(struct session *se, const char *field, int increasing,
-                  int position)
+void session_sort(struct session *se, struct reclist_sortparms *sp)
 {
-    struct session_sorted_results *sr;
+    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;
     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 */
+    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 (!strcmp(field, sr->field) && increasing == sr->increasing && sr->position == position)
+        if (!reclist_sortparms_cmp(sr,sp))
             break;
     }
     if (sr)
     {
-        session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d position=%d already fetched",
-                    field, increasing, position);
+        session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d already fetched",
+                    field, increasing, type);
         session_leave(se);
         return;
     }
-    session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d position=%d must fetch",
-                field, increasing, position);
-    if (position)
+    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)
     {
-        yaz_log(YLOG_DEBUG, "Reset results due to position");
-        session_clear_set(se, field, increasing, position);
+        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->field = nmem_strdup(se->nmem, field);
+        sr->name = nmem_strdup(se->nmem, field);
         sr->increasing = increasing;
-        sr->position = position;
+        sr->type = type;
         sr->next = se->sorted_results;
         se->sorted_results = sr;
     }
-    yaz_log(YLOG_DEBUG, "Restarting search for clients due to change in sort order");
+    // 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)
     {
@@ -689,9 +701,13 @@ void session_sort(struct session *se, const char *field, int increasing,
         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);
         }
+        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));
+        }
+
     }
     session_leave(se);
 }
@@ -710,8 +726,8 @@ enum pazpar2_error_code session_search(struct session *se,
     int no_failed_query = 0;
     int no_failed_limit = 0;
     struct client_list *l, *l0;
-    struct timeval tval;
     facet_limits_t facet_limits;
+    int same_sort_order = 0;
 
     session_log(se, YLOG_DEBUG, "Search");
 
@@ -724,7 +740,12 @@ enum pazpar2_error_code session_search(struct session *se,
 
     session_enter(se);
     se->settings_modified = 0;
-    session_clear_set(se, sp->name, sp->increasing, sp->type == Metadata_sortkey_position);
+
+    if (se->sorted_results) {
+        if (!reclist_sortparms_cmp(se->sorted_results, sp))
+            same_sort_order = 1;
+    }
+    session_clear_set(se, sp);
     relevance_destroy(&se->relevance);
 
     live_channels = select_targets(se, filter);
@@ -734,10 +755,6 @@ enum pazpar2_error_code session_search(struct session *se,
         return PAZPAR2_NO_TARGETS;
     }
 
-    yaz_gettimeofday(&tval);
-
-    tval.tv_sec += 5;
-
     facet_limits = facet_limits_create(limit);
     if (!facet_limits)
     {
@@ -754,35 +771,22 @@ enum pazpar2_error_code session_search(struct session *se,
     {
         int parse_ret;
         struct client *cl = l->client;
-
+        client_parse_init(cl, 1);
         if (prepare_map(se, client_get_database(cl)) < 0)
             continue;
 
-        parse_ret = client_parse_query(cl, query, facet_limits, startrecs,
-                                       maxrecs, se->service->ccl_bibset);
+        parse_ret = client_parse_query(cl, query, facet_limits, se->service->ccl_bibset);
         if (parse_ret == -1)
             no_failed_query++;
         else if (parse_ret == -2)
             no_failed_limit++;
-        else if (parse_ret)
+        else if (parse_ret < 0)
             no_working++; /* other error, such as bad CCL map */
         else
         {
-            int r =
-                client_prep_connection(cl, se->service->z3950_operation_timeout,
-                                       se->service->z3950_session_timeout,
-                                       se->service->server->iochan_man,
-                                       &tval);
-            if (parse_ret == 1 && r == 2)
-            {
-                session_log(se, YLOG_LOG, "client %s REUSE result", client_get_id(cl));
-                client_reingest(cl);
-            }
-            else if (r)
-            {
-                session_log(se, YLOG_LOG, "client %s NEW search", client_get_id(cl));
-                client_start_search(cl);
-            }
+            client_parse_range(cl, startrecs, maxrecs);
+            client_parse_sort(cl, sp);
+            client_start_search(cl);
             no_working++;
         }
     }
@@ -959,6 +963,8 @@ struct session *new_session(NMEM nmem, struct conf_service *service,
     session->session_nmem = nmem;
     session->nmem = nmem_create();
     session->databases = 0;
+    session->sorted_results = 0;
+
     for (i = 0; i <= SESSION_WATCH_MAX; i++)
     {
         session->watchlist[i].data = 0;