Add version to termlist response method. Print records and filtered in target respons...
[pazpar2-moved-to-github.git] / src / session.c
index 19ac45c..fde1e78 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2011 Index Data
+   Copyright (C) 2006-2012 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -76,6 +76,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "settings.h"
 #include "normalize7bit.h"
 
+#include <libxml/tree.h>
+
 #define TERMLIST_HIGH_SCORE 25
 
 #define MAX_CHUNK 15
@@ -523,13 +525,14 @@ static void select_targets_callback(struct session *se,
     {
         cl = client_create(db->database->id);
         client_set_database(cl, db);
-        client_set_session(cl, se);
 
         l = xmalloc(sizeof(*l));
         l->client = cl;
         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));
     l->client = cl;
@@ -550,6 +553,11 @@ static void session_reset_active_clients(struct session *se,
     while (l)
     {
         struct client_list *l_next = l->next;
+
+        client_lock(l->client); 
+        client_set_session(l->client, 0); /* mark client inactive */
+        client_unlock(l->client);
+
         xfree(l);
         l = l_next;
     }
@@ -642,11 +650,9 @@ void session_sort(struct session *se, const char *field, int increasing)
     for (l = se->clients_active; l; l = l->next)
     {
         struct client *cl = l->client;
-        struct timeval tval;
-        if (client_prep_connection(cl, se->service->z3950_operation_timeout,
-                                   se->service->z3950_session_timeout,
-                                   se->service->server->iochan_man,
-                                   &tval))
+        if (client_get_state(cl) == Client_Connecting ||
+            client_get_state(cl) == Client_Idle ||
+            client_get_state(cl) == Client_Working)
             client_start_search(cl);
     }
     session_leave(se);
@@ -731,27 +737,24 @@ enum pazpar2_error_code session_search(struct session *se,
             no_failed_query++;
         else if (parse_ret == -2)
             no_failed_limit++;
-        else if (parse_ret == 0)
-        {
-            session_log(se, YLOG_LOG, "client NEW %s", client_get_id(cl));
-            no_working++;
-            if (client_prep_connection(cl, se->service->z3950_operation_timeout,
-                                       se->service->z3950_session_timeout,
-                                       se->service->server->iochan_man,
-                                       &tval))
-                client_start_search(cl);
-        }
         else
         {
-            session_log(se, YLOG_LOG, "client REUSE %s", client_get_id(cl));
-            no_working++;
-            if (client_prep_connection(cl, se->service->z3950_operation_timeout,
+            int r =
+                client_prep_connection(cl, se->service->z3950_operation_timeout,
                                        se->service->z3950_session_timeout,
                                        se->service->server->iochan_man,
-                                       &tval))
+                                       &tval);
+            if (parse_ret == 1 && r == 2)
             {
+                session_log(se, YLOG_LOG, "client REUSE %s", client_get_id(cl));
                 client_reingest(cl);
             }
+            else if (r)
+            {
+                session_log(se, YLOG_LOG, "client NEW %s", client_get_id(cl));
+                client_start_search(cl);
+            }
+            no_working++;
         }
     }
     facet_limits_destroy(facet_limits);
@@ -961,6 +964,7 @@ static struct hitsbytarget *hitsbytarget_nb(struct session *se,
         res[*count].name = *name ? name : "Unknown";
         res[*count].hits = client_get_hits(cl);
         res[*count].records = client_get_num_records(cl);
+        res[*count].filtered = client_get_num_records_filtered(cl);
         res[*count].diagnostic =
             client_get_diagnostic(cl, &res[*count].addinfo);
         res[*count].state = client_get_state_str(cl);
@@ -1011,7 +1015,7 @@ static int cmp_ht(const void *p1, const void *p2)
 }
 
 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
-                               NMEM nmem)
+                               NMEM nmem, int version)
 {
     struct hitsbytarget *ht;
     int count, i;
@@ -1038,7 +1042,12 @@ static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
         
         wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
                      ht[i].hits);
-        
+
+        if (version >= 2) {
+            wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records);
+            wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
+        }
+
         wrbuf_puts(wrbuf, "<state>");
         wrbuf_xmlputs(wrbuf, ht[i].state);
         wrbuf_puts(wrbuf, "</state>\n");
@@ -1051,7 +1060,7 @@ static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
 }
 
 void perform_termlist(struct http_channel *c, struct session *se,
-                      const char *name, int num)
+                      const char *name, int num, int version)
 {
     int i, j;
     NMEM nmem_tmp = nmem_create();
@@ -1068,7 +1077,8 @@ void perform_termlist(struct http_channel *c, struct session *se,
     for (j = 0; j < num_names; j++)
     {
         const char *tname;
-        
+        int must_generate_empty = 1; /* bug 5350 */
+
         for (i = 0; i < se->num_termlists; i++)
         {
             tname = se->termlists[i].name;
@@ -1080,6 +1090,7 @@ void perform_termlist(struct http_channel *c, struct session *se,
                 wrbuf_puts(c->wrbuf, "<list name=\"");
                 wrbuf_xmlputs(c->wrbuf, tname);
                 wrbuf_puts(c->wrbuf, "\">\n");
+                must_generate_empty = 0;
 
                 p = termlist_highscore(se->termlists[i].termlist, &len);
                 if (p)
@@ -1112,8 +1123,15 @@ void perform_termlist(struct http_channel *c, struct session *se,
             wrbuf_xmlputs(c->wrbuf, tname);
             wrbuf_puts(c->wrbuf, "\">\n");
 
-            targets_termlist_nb(c->wrbuf, se, num, c->nmem);
+            targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
             wrbuf_puts(c->wrbuf, "</list>\n");
+            must_generate_empty = 0;
+        }
+        if (must_generate_empty)
+        {
+            wrbuf_puts(c->wrbuf, "<list name=\"");
+            wrbuf_xmlputs(c->wrbuf, names[j]);
+            wrbuf_puts(c->wrbuf, "\"/>\n");
         }
     }
     session_leave(se);
@@ -1531,8 +1549,7 @@ int ingest_record(struct client *cl, const char *rec,
     
     if (!check_record_filter(root, sdb))
     {
-        session_log(se, YLOG_LOG, "Filtered out record no %d from %s",
-                    record_no, sdb->database->id);
+        session_log(se, YLOG_LOG, "Filtered out record no %d from %s", record_no, sdb->database->id);
         xmlFreeDoc(xdoc);
         return -2;
     }