Automaked pazpar2. Bug #799. Preprocessor defines are put in cconfig.h and
[pazpar2-moved-to-github.git] / src / pazpar2.c
index 4c3ab58..18d0f0a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: pazpar2.c,v 1.10 2007-01-04 03:16:14 quinn Exp $ */;
+/* $Id: pazpar2.c,v 1.18 2007-01-08 12:43:41 adam Exp $ */;
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include <assert.h>
 
+#include <yaz/marcdisp.h>
 #include <yaz/comstack.h>
 #include <yaz/tcpip.h>
 #include <yaz/proto.h>
 #include <yaz/yaz-util.h>
 #include <yaz/nmem.h>
 
+#if HAVE_CONFIG_H
+#include "cconfig.h"
+#endif
+
+#define USE_TIMING 0
+#if USE_TIMING
+#include <yaz/timing.h>
+#endif
+
 #include "pazpar2.h"
 #include "eventl.h"
-#include "command.h"
 #include "http.h"
 #include "termlists.h"
 #include "reclists.h"
 #include "relevance.h"
 #include "config.h"
 
-#define PAZPAR2_VERSION "0.1"
 #define MAX_CHUNK 15
 
 static void client_fatal(struct client *cl);
@@ -65,7 +73,7 @@ struct parameters global_parameters =
     30,
     "81",
     "Index Data PazPar2 (MasterKey)",
-    PAZPAR2_VERSION,
+    VERSION,
     600, // 10 minutes
     60,
     100,
@@ -76,7 +84,6 @@ struct parameters global_parameters =
     0
 };
 
-
 static int send_apdu(struct client *c, Z_APDU *a)
 {
     struct connection *co = c->connection;
@@ -194,6 +201,8 @@ static void send_present(IOCHAN i)
     int start = cl->records + 1;
 
     toget = global_parameters.chunk;
+    if (toget > global_parameters.toget - cl->records)
+        toget = global_parameters.toget - cl->records;
     if (toget > cl->hits - cl->records)
        toget = cl->hits - cl->records;
 
@@ -426,6 +435,7 @@ static struct record *ingest_record(struct client *cl, Z_External *rec)
     if (!(mergekey = xmlGetProp(root, "mergekey")))
     {
         yaz_log(YLOG_WARN, "No mergekey found in record");
+        xmlFreeDoc(xdoc);
         return 0;
     }
 
@@ -441,6 +451,12 @@ static struct record *ingest_record(struct client *cl, Z_External *rec)
     res->merge_key = normalize_mergekey(mergekey_norm);
 
     head = reclist_insert(se->reclist, res);
+    if (!head)
+    {
+        /* no room for record */
+        xmlFreeDoc(xdoc);
+        return 0;
+    }
     relevance_newrec(se->relevance, head);
 
     for (n = root->children; n; n = n->next)
@@ -451,21 +467,28 @@ static struct record *ingest_record(struct client *cl, Z_External *rec)
         {
             xmlChar *type = xmlGetProp(n, "type");
             xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
-            add_facet(se, type, value);
-            relevance_countwords(se->relevance, head, value, 1);
+            if (type && value)
+            {
+                add_facet(se, type, value);
+                relevance_countwords(se->relevance, head, value, 1);
+            }
             xmlFree(type);
             xmlFree(value);
         }
         else if (!strcmp(n->name, "metadata"))
         {
-            xmlChar *type = xmlGetProp(n, "type"), *value;
-            if (!strcmp(type, "title"))
-                res->title = nmem_strdup(se->nmem,
-                        value = xmlNodeListGetString(xdoc, n->children, 0));
-
-            relevance_countwords(se->relevance, head, value, 4);
+            xmlChar *type = xmlGetProp(n, "type");
+            if (type && !strcmp(type, "title"))
+            {
+                xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
+                if (value)
+                {
+                    res->title = nmem_strdup(se->nmem, value);
+                    relevance_countwords(se->relevance, head, value, 4);
+                    xmlFree(value);
+                }
+            }
             xmlFree(type);
-            xmlFree(value);
         }
         else
             yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
@@ -481,6 +504,9 @@ static struct record *ingest_record(struct client *cl, Z_External *rec)
 
 static void ingest_records(struct client *cl, Z_Records *r)
 {
+#if USE_TIMING
+    yaz_timing_t t = yaz_timing_create();
+#endif
     struct record *rec;
     struct session *s = cl->session;
     Z_NamePlusRecordList *rlist;
@@ -505,6 +531,14 @@ static void ingest_records(struct client *cl, Z_Records *r)
     }
     if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
         session_alert_watch(s, SESSION_WATCH_RECORDS);
+
+#if USE_TIMING
+    yaz_timing_stop(t);
+    yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f", 
+            yaz_timing_get_real(t), yaz_timing_get_user(t),
+            yaz_timing_get_sys(t));
+    yaz_timing_destroy(&t);
+#endif
 }
 
 static void do_presentResponse(IOCHAN i, Z_APDU *a)
@@ -1137,9 +1171,7 @@ struct termlist_score **termlist(struct session *s, const char *name, int *num)
     return 0;
 }
 
-#ifdef REPORT_NMEM
-// conditional compilation by SH: This lead to a warning with currently installed
-// YAZ header files on us1
+#ifdef MISSING_HEADERS
 void report_nmem_stats(void)
 {
     size_t in_use, is_free;
@@ -1158,7 +1190,9 @@ struct record **show(struct session *s, int start, int *num, int *total,
     struct record **recs = nmem_malloc(nmem_show, *num 
                                        * sizeof(struct record *));
     int i;
-
+#if USE_TIMING    
+    yaz_timing_t t = yaz_timing_create();
+#endif
     relevance_prepare_read(s->relevance, s->reclist);
 
     *total = s->reclist->num_records;
@@ -1168,7 +1202,8 @@ struct record **show(struct session *s, int start, int *num, int *total,
         if (!reclist_read_record(s->reclist))
         {
             *num = 0;
-            return 0;
+            recs = 0;
+            break;
         }
 
     for (i = 0; i < *num; i++)
@@ -1181,6 +1216,13 @@ struct record **show(struct session *s, int start, int *num, int *total,
         }
         recs[i] = r;
     }
+#if USE_TIMING
+    yaz_timing_stop(t);
+    yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f", 
+            yaz_timing_get_real(t), yaz_timing_get_user(t),
+            yaz_timing_get_sys(t));
+    yaz_timing_destroy(&t);
+#endif
     return recs;
 }
 
@@ -1235,17 +1277,13 @@ int main(int argc, char **argv)
 
     yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
 
-    while ((ret = options("f:x:c:h:p:C:s:d", argv, argc, &arg)) != -2)
+    while ((ret = options("f:x:h:p:C:s:d", argv, argc, &arg)) != -2)
     {
        switch (ret) {
             case 'f':
                 if (!read_config(arg))
                     exit(1);
                 break;
-           case 'c':
-               command_init(atoi(arg));
-                setport++;
-               break;
             case 'h':
                 http_init(arg);
                 setport++;
@@ -1266,7 +1304,6 @@ int main(int argc, char **argv)
                fprintf(stderr, "Usage: pazpar2\n"
                         "    -f configfile\n"
                         "    -h [host:]port          (REST protocol listener)\n"
-                        "    -c cmdport              (telnet-style)\n"
                         "    -C cclconfig\n"
                         "    -s simpletargetfile\n"
                         "    -p hostname[:portno]    (HTTP proxy)\n");
@@ -1276,7 +1313,7 @@ int main(int argc, char **argv)
 
     if (!setport)
     {
-        fprintf(stderr, "Set command port with -h or -c\n");
+        fprintf(stderr, "Set command port with -h\n");
         exit(1);
     }