Fixed record logging on Windows (-d).
[pazpar2-moved-to-github.git] / src / logic.c
index 70b3879..2096f30 100644 (file)
@@ -34,12 +34,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#if HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#if HAVE_NETDB_H
-#include <netdb.h>
-#endif
 #include <signal.h>
 #include <ctype.h>
 #include <assert.h>
@@ -63,9 +57,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/timing.h>
 #endif
 
-#if HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
 
 #include "pazpar2.h"
 #include "eventl.h"
@@ -104,6 +95,24 @@ struct parameters global_parameters =
     15   // Connect timeout
 };
 
+static void log_xml_doc(xmlDoc *doc)
+{
+    FILE *lf = yaz_log_file();
+    xmlChar *result = 0;
+    int len = 0;
+#if LIBXML_VERSION >= 20600
+    xmlDocDumpFormatMemory(doc, &result, &len, 1);
+#else
+    xmlDocDumpMemory(doc, &result, &len);
+#endif
+    if (lf && len)
+    {
+        fwrite(result, 1, len, lf);
+        fprintf(lf, "\n");
+    }
+    xmlFree(result);
+}
+
 // Recursively traverse query structure to extract terms.
 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
 {
@@ -174,17 +183,8 @@ xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
 
     if (global_parameters.dump_records)
     {
-        FILE *lf = yaz_log_file();
-        if (lf)
-        {
-            yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
-#if LIBXML_VERSION >= 20600
-            xmlDocFormatDump(lf, rdoc, 1);
-#else
-            xmlDocDump(lf, rdoc);
-#endif
-            fprintf(lf, "\n");
-        }
+        yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
+        log_xml_doc(rdoc);
     }
 
     return rdoc;
@@ -291,19 +291,9 @@ xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
 
         if (global_parameters.dump_records)
         {
-            FILE *lf = yaz_log_file();
-            
-            if (lf)
-            {
-                yaz_log(YLOG_LOG, "Normalized record from %s", 
-                        sdb->database->url);
-#if LIBXML_VERSION >= 20600
-                xmlDocFormatDump(lf, rdoc, 1);
-#else
-                xmlDocDump(lf, rdoc);
-#endif
-                fprintf(lf, "\n");
-            }
+            yaz_log(YLOG_LOG, "Normalized record from %s", 
+                    sdb->database->url);
+            log_xml_doc(rdoc);
         }
     }
     return rdoc;
@@ -539,7 +529,6 @@ enum pazpar2_error_code search(struct session *se,
     se->reclist = 0;
     se->num_termlists = 0;
     criteria = parse_filter(se->nmem, filter);
-    se->requestid++;
     live_channels = select_targets(se, criteria);
     if (live_channels)
     {
@@ -707,9 +696,9 @@ struct session *new_session(NMEM nmem)
     session->relevance = 0;
     session->total_hits = 0;
     session->total_records = 0;
+    session->number_of_warnings_unknown_elements = 0;
     session->num_termlists = 0;
     session->reclist = 0;
-    session->requestid = -1;
     session->clients = 0;
     session->expected_maxrecs = 0;
     session->session_nmem = nmem;
@@ -1086,7 +1075,6 @@ struct record *ingest_record(struct client *cl, const char *rec,
              wheretoput = &record->metadata[md_field_id];
              while (*wheretoput)
                  wheretoput = &(*wheretoput)->next;
-             rec_md->next = 0;
              *wheretoput = rec_md;
 
              // merged metadata
@@ -1096,6 +1084,12 @@ struct record *ingest_record(struct client *cl, const char *rec,
 
              // and polulate with data:
              // assign cluster or record based on merge action
+             if (ser_md->merge == Metadata_merge_no)
+             {
+                 while (*wheretoput)
+                     wheretoput = &(*wheretoput)->next;
+                 *wheretoput = rec_md;
+             }
              if (ser_md->merge == Metadata_merge_unique)
              {
                  struct record_metadata *mnode;
@@ -1209,9 +1203,13 @@ struct record *ingest_record(struct client *cl, const char *rec,
             xmlFree(value);
             type = value = 0;
         }
-        else
-            yaz_log(YLOG_WARN,
-                    "Unexpected element %s in internal record", n->name);
+         else
+         {
+             if (se->number_of_warnings_unknown_elements == 0)
+                 yaz_log(YLOG_WARN,
+                         "Unexpected element in internal record: %s", n->name);
+             se->number_of_warnings_unknown_elements++;
+         }
     }
     if (type)
         xmlFree(type);