Revise extended fetch algorithm
[pazpar2-moved-to-github.git] / src / http_command.c
index a75538f..7620e92 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2012 Index Data
+   Copyright (C) 2006-2013 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
@@ -93,6 +93,8 @@ struct http_sessions {
 static YAZ_MUTEX g_http_session_mutex = 0;
 static int g_http_sessions = 0;
 
+static void show_records_ready(void *data);
+
 int get_version(struct http_request *rq) {
     const char *version = http_argbyname(rq, "version");
     int version_no = 0;
@@ -405,7 +407,8 @@ static void cmd_exit(struct http_channel *c)
 
     response_open(c, "exit");
     response_close(c, "exit");
-    http_close_server(c->server);
+    if (global_parameters.debug_mode)
+        http_close_server(c->server);
 }
 
 static void cmd_init(struct http_channel *c)
@@ -726,6 +729,9 @@ static void bytarget_response(struct http_channel *c, struct http_session *s, co
         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
         if (ht[i].diagnostic)
         {
+            wrbuf_puts(c->wrbuf, "<message>");
+            wrbuf_xmlputs(c->wrbuf, ht[i].message);
+            wrbuf_puts(c->wrbuf, "</message>\n");
             wrbuf_puts(c->wrbuf, "<addinfo>");
             if (ht[i].addinfo)
                 wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
@@ -826,7 +832,7 @@ static void cmd_bytarget(struct http_channel *c)
 }
 
 static void write_metadata(WRBUF w, struct conf_service *service,
-        struct record_metadata **ml, int full)
+                           struct record_metadata **ml, int full, int indent)
 {
     int imeta;
 
@@ -839,7 +845,10 @@ static void write_metadata(WRBUF w, struct conf_service *service,
         for (md = ml[imeta]; md; md = md->next)
         {
             struct record_metadata_attr *attr = md->attributes;
-            wrbuf_printf(w, "\n<md-%s", cmd->name);
+            int i;
+            for (i = 0; i < indent; i++)
+                wrbuf_putc(w, ' ');
+            wrbuf_printf(w, "<md-%s", cmd->name);
 
             for (; attr; attr = attr->next)
             {
@@ -862,7 +871,7 @@ static void write_metadata(WRBUF w, struct conf_service *service,
                     wrbuf_puts(w, "[can't represent]");
                     break;
             }
-            wrbuf_printf(w, "</md-%s>", cmd->name);
+            wrbuf_printf(w, "</md-%s>\n", cmd->name);
         }
     }
 }
@@ -873,20 +882,20 @@ static void write_subrecord(struct record *r, WRBUF w,
     const char *name = session_setting_oneval(
         client_get_database(r->client), PZ_NAME);
 
-    wrbuf_puts(w, "<location id=\"");
+    wrbuf_puts(w, " <location id=\"");
     wrbuf_xmlputs(w, client_get_id(r->client));
     wrbuf_puts(w, "\"\n");
 
-    wrbuf_puts(w, " name=\"");
+    wrbuf_puts(w, "    name=\"");
     wrbuf_xmlputs(w,  *name ? name : "Unknown");
     wrbuf_puts(w, "\" ");
 
     wrbuf_puts(w, "checksum=\"");
     wrbuf_printf(w,  "%u", r->checksum);
-    wrbuf_puts(w, "\">");
+    wrbuf_puts(w, "\">\n");
 
-    write_metadata(w, service, r->metadata, show_details);
-    wrbuf_puts(w, "</location>\n");
+    write_metadata(w, service, r->metadata, show_details, 2);
+    wrbuf_puts(w, " </location>\n");
 }
 
 static void show_raw_record_error(void *data, const char *addinfo)
@@ -1024,26 +1033,26 @@ static void show_record(struct http_channel *c, struct http_session *s)
     {
         struct record *r;
         response_open_no_status(c, "record");
-        wrbuf_puts(c->wrbuf, "\n<recid>");
+        wrbuf_puts(c->wrbuf, "\n <recid>");
         wrbuf_xmlputs(c->wrbuf, rec->recid);
         wrbuf_puts(c->wrbuf, "</recid>\n");
         if (prev_r)
         {
-            wrbuf_puts(c->wrbuf, "<prevrecid>");
+            wrbuf_puts(c->wrbuf, " <prevrecid>");
             wrbuf_xmlputs(c->wrbuf, prev_r->recid);
             wrbuf_puts(c->wrbuf, "</prevrecid>\n");
         }
         if (next_r)
         {
-            wrbuf_puts(c->wrbuf, "<nextrecid>");
+            wrbuf_puts(c->wrbuf, " <nextrecid>");
             wrbuf_xmlputs(c->wrbuf, next_r->recid);
             wrbuf_puts(c->wrbuf, "</nextrecid>\n");
         }
-        wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
+        wrbuf_printf(c->wrbuf, " <activeclients>%d</activeclients>\n",
                      session_active_clients(s->psession));
-        write_metadata(c->wrbuf, service, rec->metadata, 1);
+        write_metadata(c->wrbuf, service, rec->metadata, 1, 1);
         for (r = rec->records; r; r = r->next)
-            write_subrecord(r, c->wrbuf, service, 1);
+            write_subrecord(r, c->wrbuf, service, 2);
         response_close(c, "record");
     }
     show_single_stop(s->psession, rec);
@@ -1087,7 +1096,7 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
     Odr_int total_hits;
     Odr_int approx_hits;
     int i;
-
+    struct conf_service *service = 0;
     if (!s)
         return;
 
@@ -1099,16 +1108,22 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
         startn = atoi(start);
     if (num)
         numn = atoi(num);
-    if (!sort)
-        sort = "relevance";
-    if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
+
+    service = s->psession->service;
+    if (!sort) {
+        sort = service->default_sort;
+    }
+    if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
     {
         error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
         return;
 
     }
 
-    rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits, &approx_hits);
+    rl = show_range_start(s->psession, sp, startn, &numn, &total,
+                          &total_hits, &approx_hits, show_records_ready, c);
+    if (!rl)
+        return;
 
     response_open(c, "show");
     wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>\n", active);
@@ -1128,15 +1143,23 @@ static void show_records(struct http_channel *c, struct http_session *s, int act
         struct conf_service *service = s->psession->service;
 
         wrbuf_puts(c->wrbuf, "<hit>\n");
-        write_metadata(c->wrbuf, service, rec->metadata, 0);
+        write_metadata(c->wrbuf, service, rec->metadata, 0, 1);
         for (ccount = 0, p = rl[i]->records; p;  p = p->next, ccount++)
             write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
-        if (ccount > 1)
-            wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
+        wrbuf_printf(c->wrbuf, " <count>%d</count>\n", ccount);
        if (strstr(sort, "relevance"))
-           wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
+        {
+           wrbuf_printf(c->wrbuf, " <relevance>%d</relevance>\n",
                          rec->relevance_score);
-        wrbuf_puts(c->wrbuf, "<recid>");
+            if (service->rank_debug)
+            {
+                wrbuf_printf(c->wrbuf, " <relevance_info>\n");
+                wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain1));
+                wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain2));
+                wrbuf_printf(c->wrbuf, " </relevance_info>\n");
+            }
+        }
+        wrbuf_puts(c->wrbuf, " <recid>");
         wrbuf_xmlputs(c->wrbuf, rec->recid);
         wrbuf_puts(c->wrbuf, "</recid>\n");
         wrbuf_puts(c->wrbuf, "</hit>\n");
@@ -1169,6 +1192,7 @@ static void cmd_show(struct http_channel *c)
     const char *block = http_argbyname(rq, "block");
     const char *sort = http_argbyname(rq, "sort");
     const char *block_error = http_argbyname(rq, "report");
+    struct conf_service *service = 0;
 
     struct reclist_sortparms *sp;
     int status;
@@ -1179,18 +1203,18 @@ static void cmd_show(struct http_channel *c)
     if (!s)
         return;
 
-    if (!sort)
-        sort = "relevance";
+    service = s->psession->service;
+    if (!sort) {
+        sort = service->default_sort;
+    }
 
-    if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
+    if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
     {
         error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
         release_session(c, s);
         return;
     }
-    session_sort(s->psession, sp->name, sp->increasing, sp->type == Metadata_sortkey_position);
-                 /* TODO This was too simple. Will make pazpar2 continuing reseting the session resultset and redo the search. Disable this for now
-                    sp->type == Metadata_sortkey_position */
+    session_sort(s->psession, sp);
 
     status = session_active_clients(s->psession);
 
@@ -1270,11 +1294,15 @@ static void cmd_search(struct http_channel *c)
     const char *maxrecs = http_argbyname(rq, "maxrecs");
     const char *startrecs = http_argbyname(rq, "startrecs");
     const char *limit = http_argbyname(rq, "limit");
+    const char *sort = http_argbyname(rq, "sort");
     enum pazpar2_error_code code;
     const char *addinfo = 0;
+    struct reclist_sortparms *sp;
+    struct conf_service *service = 0;
 
     if (!s)
         return;
+
     if (!query)
     {
         error(rs, PAZPAR2_MISSING_PARAMETER, "query");
@@ -1287,8 +1315,19 @@ static void cmd_search(struct http_channel *c)
         release_session(c, s);
         return;
     }
+    service = s->psession->service;
+    if (!sort) {
+        sort = service->default_sort;
+    }
+    if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
+    {
+        error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
+        release_session(c, s);
+        return;
+    }
+
     code = session_search(s->psession, query, startrecs, maxrecs, filter, limit,
-                          &addinfo, "relevance", 0);
+                          &addinfo, sp);
     if (code)
     {
         error(rs, code, addinfo);
@@ -1321,17 +1360,17 @@ static void cmd_stat(struct http_channel *c)
     }
 
     response_open_no_status(c, "stat");
-    wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
-    wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
-    wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
-    wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
-    wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
-    wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
-    wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
-    wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
-    wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
-    wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
-    wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
+    wrbuf_printf(c->wrbuf, "\n <activeclients>%d</activeclients>\n", clients);
+    wrbuf_printf(c->wrbuf, " <hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
+    wrbuf_printf(c->wrbuf, " <records>%d</records>\n", stat.num_records);
+    wrbuf_printf(c->wrbuf, " <clients>%d</clients>\n", stat.num_clients);
+    wrbuf_printf(c->wrbuf, " <unconnected>%d</unconnected>\n", stat.num_no_connection);
+    wrbuf_printf(c->wrbuf, " <connecting>%d</connecting>\n", stat.num_connecting);
+    wrbuf_printf(c->wrbuf, " <working>%d</working>\n", stat.num_working);
+    wrbuf_printf(c->wrbuf, " <idle>%d</idle>\n", stat.num_idle);
+    wrbuf_printf(c->wrbuf, " <failed>%d</failed>\n", stat.num_failed);
+    wrbuf_printf(c->wrbuf, " <error>%d</error>\n", stat.num_error);
+    wrbuf_printf(c->wrbuf, " <progress>%.2f</progress>\n", progress);
     response_close(c, "stat");
     release_session(c, s);
 }
@@ -1339,26 +1378,39 @@ static void cmd_stat(struct http_channel *c)
 static void cmd_info(struct http_channel *c)
 {
     char yaz_version_str[20];
+    char yaz_sha1_str[42];
 
     response_open_no_status(c, "info");
-    wrbuf_puts(c->wrbuf, " <version>\n");
+    wrbuf_puts(c->wrbuf, "\n <version>\n");
     wrbuf_puts(c->wrbuf, "  <pazpar2");
 #ifdef PAZPAR2_VERSION_SHA1
     wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
 #endif
     wrbuf_puts(c->wrbuf, ">");
     wrbuf_xmlputs(c->wrbuf, VERSION);
-    wrbuf_puts(c->wrbuf, "</pazpar2>");
+    wrbuf_puts(c->wrbuf, "</pazpar2>\n");
 
-    yaz_version(yaz_version_str, 0);
+    yaz_version(yaz_version_str, yaz_sha1_str);
     wrbuf_puts(c->wrbuf, "  <yaz compiled=\"");
     wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
+    wrbuf_puts(c->wrbuf, "\" sha1=\"");
+    wrbuf_xmlputs(c->wrbuf, yaz_sha1_str);
     wrbuf_puts(c->wrbuf, "\">");
     wrbuf_xmlputs(c->wrbuf, yaz_version_str);
     wrbuf_puts(c->wrbuf, "</yaz>\n");
 
     wrbuf_puts(c->wrbuf, " </version>\n");
-
+#if HAVE_UNISTD_H
+    {
+        char hostname_str[64];
+        if (gethostname(hostname_str, sizeof(hostname_str)) == 0)
+        {
+            wrbuf_puts(c->wrbuf, " <host>");
+            wrbuf_xmlputs(c->wrbuf, hostname_str);
+            wrbuf_puts(c->wrbuf, "</host>\n");
+        }
+    }
+#endif
     info_services(c->server, c->wrbuf);
 
     response_close(c, "info");