log level for http is http (not HTTP)
[pazpar2-moved-to-github.git] / src / session.c
index e76da0a..41447c6 100644 (file)
@@ -228,18 +228,27 @@ static void session_add_id_facet(struct session *s, struct client *cl,
 }
 
 
+// Look up a facet term, and return matching id
+// If facet type not found, returns 0
+// If facet type found, but no matching term, returns ""
 const char *session_lookup_id_facet(struct session *s, struct client *cl,
                                     const char *type,
                                     const char *term)
 {
+    char *retval = 0;
     struct facet_id *t = s->facet_id_list;
-    for (; t; t = t->next)
-        if (!strcmp(client_get_id(cl), t->client_id) &&
-            !strcmp(t->type, type) && !strcmp(t->term, term))
+    for (; t; t = t->next) 
+    {
+        if (!strcmp(client_get_id(cl), t->client_id) &&  !strcmp(t->type, type) )
         {
-            return t->id;
+            retval = "";
+            if ( !strcmp(t->term, term))
+            {
+                return t->id;
+            }
         }
-    return 0;
+    }
+    return retval;
 }
 
 void add_facet(struct session *s, const char *type, const char *value, int count, struct client *cl)
@@ -561,7 +570,6 @@ static void select_targets_callback(struct session *se,
         l->next = se->clients_cached;
         se->clients_cached = l;
     }
-    /* set session always. If may be 0 if client is not active */
     client_set_session(cl, se);
 
     l = xmalloc(sizeof(*l));
@@ -610,6 +618,7 @@ static void session_remove_cached_clients(struct session *se)
         client_lock(l->client);
         client_set_session(l->client, 0);
         client_set_database(l->client, 0);
+        client_mark_dead(l->client);
         client_unlock(l->client);
         client_destroy(l->client);
         xfree(l);
@@ -714,7 +723,7 @@ void session_sort(struct session *se, struct reclist_sortparms *sp,
                 break;
         if (sr)
         {
-            session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d already fetched",
+            session_log(se, YLOG_LOG, "session_sort: field=%s increasing=%d type=%d already fetched",
                         field, increasing, type);
             session_leave(se, "session_sort");
             return;
@@ -732,7 +741,7 @@ void session_sort(struct session *se, struct reclist_sortparms *sp,
         struct client *cl = l->client;
         // Assume no re-search is required.
         client_parse_init(cl, 1);
-        clients_research += client_parse_sort(cl, sp);
+        clients_research += client_parse_sort(cl, sp, 0);
     }
     if (!clients_research || se->clients_starting)
     {
@@ -774,6 +783,7 @@ void session_sort(struct session *se, struct reclist_sortparms *sp,
         }
         session_enter(se, "session_sort");
         se->clients_starting = 0;
+        se->force_position = 0;
         session_leave(se, "session_sort");
     }
 }
@@ -821,6 +831,7 @@ enum pazpar2_error_code session_search(struct session *se,
     int no_working = 0;
     int no_failed_query = 0;
     int no_failed_limit = 0;
+    int no_sortmap = 0;
     struct client_list *l;
 
     session_log(se, YLOG_DEBUG, "Search");
@@ -834,6 +845,7 @@ enum pazpar2_error_code session_search(struct session *se,
         return PAZPAR2_NO_ERROR;
     }
     se->clients_starting = 1;
+    se->force_position = 0;
     session_leave(se, "session_search0");
 
     if (se->settings_modified) {
@@ -874,6 +886,7 @@ enum pazpar2_error_code session_search(struct session *se,
         *addinfo = "limit";
         session_leave(se, "session_search");
         se->clients_starting = 0;
+        session_reset_active_clients(se, 0);
         return PAZPAR2_MALFORMED_PARAMETER_VALUE;
     }
 
@@ -902,12 +915,19 @@ enum pazpar2_error_code session_search(struct session *se,
         else
         {
             client_parse_range(cl, startrecs, maxrecs);
-            client_parse_sort(cl, sp);
+            client_parse_sort(cl, sp, &no_sortmap);
             client_start_search(cl);
             no_working++;
         }
     }
+    yaz_log(YLOG_LOG, "session_search: no_working=%d no_sortmap=%d",
+            no_working, no_sortmap);
     session_enter(se, "session_search2");
+    if (no_working == 1 && no_sortmap == 1)
+    {
+        se->force_position = 1;
+        yaz_log(YLOG_LOG, "force_position=1");
+    }
     se->clients_starting = 0;
     session_leave(se, "session_search2");
     if (no_working == 0)
@@ -1091,6 +1111,7 @@ struct session *new_session(NMEM nmem, struct conf_service *service,
     session->mergekey = 0;
     session->rank = 0;
     session->clients_starting = 0;
+    session->force_position = 0;
 
     for (i = 0; i <= SESSION_WATCH_MAX; i++)
     {
@@ -1409,6 +1430,7 @@ struct record_cluster **show_range_start(struct session *se,
     struct reclist_sortparms *spp;
     struct client_list *l;
     int i;
+    NMEM nmem_tmp = 0;
 #if USE_TIMING
     yaz_timing_t t = yaz_timing_create();
 #endif
@@ -1430,7 +1452,15 @@ struct record_cluster **show_range_start(struct session *se,
             *approx_hits += client_get_approximation(l->client);
         }
     }
+    if (se->force_position)
+    {
+        nmem_tmp = nmem_create();
+        sp = reclist_parse_sortparms(nmem_tmp, "position:1", 0);
+        assert(sp);
+    }
     reclist_sort(se->reclist, sp);
+    if (nmem_tmp)
+        nmem_destroy(nmem_tmp);
 
     reclist_enter(se->reclist);
     *total = reclist_get_num_records(se->reclist);
@@ -1712,7 +1742,7 @@ static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
     /* generate unique key if none is not generated already or is empty */
     if (wrbuf_len(norm_wr) == 0)
     {
-        wrbuf_printf(norm_wr, "position: %s-%d",
+        wrbuf_printf(norm_wr, "position: %s-%06d",
                      client_get_id(cl), record_no);
     }
     else