Number of worker-threads controlled by main config
[pazpar2-moved-to-github.git] / src / pazpar2_config.c
index 959bfcb..fb62e99 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) 2006-2010 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
@@ -26,25 +26,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
-#include <libxslt/xslt.h>
-#include <libxslt/transform.h>
-#include <libxslt/xsltutils.h>
 
 #include <yaz/yaz-util.h>
 #include <yaz/nmem.h>
 #include <yaz/snprintf.h>
 #include <yaz/tpath.h>
+#include <yaz/xml_include.h>
 
-#if HAVE_GLOB_H
-#define USE_POSIX_GLOB 1
-#else
-#define USE_POSIX_GLOB 0
-#endif
-
-
-#if USE_POSIX_GLOB
-#include <glob.h>
-#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #if HAVE_UNISTD_H
@@ -59,6 +47,7 @@ struct conf_config
 {
     NMEM nmem; /* for conf_config and servers memory */
     struct conf_server *servers;
+    int no_threads;
     WRBUF confdir;
 };
 
@@ -131,7 +120,7 @@ static struct conf_service *service_init(struct conf_server *server,
     service->server = server;
     service->session_timeout = 60; /* default session timeout */
     service->z3950_session_timeout = 180;
-    service->z3950_connect_timeout = 15;
+    service->z3950_operation_timeout = 30;
 
     service->relevance_pct = 0;
     service->sort_pct = 0;
@@ -412,9 +401,19 @@ static int parse_metadata(struct conf_service *service, xmlNode *n,
     else
         sortkey_offset = -1;
     
-    if (xml_mergekey && strcmp((const char *) xml_mergekey, "no"))
+    if (xml_mergekey)
     {
-        mergekey_type = Metadata_mergekey_yes;
+        if (!strcmp((const char *) xml_mergekey, "required"))
+            mergekey_type = Metadata_mergekey_required;
+        else if (!strcmp((const char *) xml_mergekey, "optional"))
+            mergekey_type = Metadata_mergekey_optional;
+        else if (!strcmp((const char *) xml_mergekey, "no"))
+            mergekey_type = Metadata_mergekey_no;
+        else
+        {
+            yaz_log(YLOG_FATAL, "Unknown value for mergekey: %s", xml_mergekey);
+            return -1;
+        }
     }
     
     
@@ -433,6 +432,7 @@ static int parse_metadata(struct conf_service *service, xmlNode *n,
     xmlFree(xml_termlist);
     xmlFree(xml_rank);
     xmlFree(xml_setting);
+    xmlFree(xml_mergekey);
     (*md_node)++;
     return 0;
 }
@@ -481,14 +481,14 @@ static struct conf_service *service_create_static(struct conf_server *server,
                     return 0;
                 }
             }
-            src = xmlGetProp(n, (xmlChar *) "z3950_connect");
+            src = xmlGetProp(n, (xmlChar *) "z3950_operation");
             if (src)
             {
-                service->z3950_connect_timeout = atoi((const char *) src);
+                service->z3950_operation_timeout = atoi((const char *) src);
                 xmlFree(src);
                 if (service->z3950_session_timeout < 9)
                 {
-                    yaz_log(YLOG_FATAL, "Z39.50 connect timeout out of range");
+                    yaz_log(YLOG_FATAL, "Z39.50 operation timeout out of range");
                     return 0;
                 }
             }
@@ -699,6 +699,7 @@ static struct conf_server *server_create(struct conf_config *config,
 {
     xmlNode *n;
     struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
+    xmlChar *server_id = xmlGetProp(node, (xmlChar *) "id");
 
     server->host = 0;
     server->port = 0;
@@ -714,6 +715,13 @@ static struct conf_server *server_create(struct conf_config *config,
     server->mergekey_pct = 0;
     server->server_settings = 0;
 
+    if (server_id)
+    {
+        server->server_id = nmem_strdup(nmem, (const char *)server_id);
+        xmlFree(server_id);
+    }
+    else
+        server->server_id = 0;
     for (n = node->children; n; n = n->next)
     {
         if (n->type != XML_ELEMENT_NODE)
@@ -787,8 +795,7 @@ static struct conf_server *server_create(struct conf_config *config,
                 }
                 else if (!(*sp)->id && !service_id)
                 {
-                    yaz_log(YLOG_FATAL, "Duplicate unnamed service '%s'",
-                        service_id);
+                    yaz_log(YLOG_FATAL, "Duplicate unnamed service");
                     break;
                 }
 
@@ -822,17 +829,13 @@ static struct conf_server *server_create(struct conf_config *config,
     return server;
 }
 
-xsltStylesheet *conf_load_stylesheet(struct conf_service *service,
-                                     const char *fname)
+WRBUF conf_get_fname(struct conf_service *service, const char *fname)
 {
     struct conf_config *config = service->server->config;
     WRBUF w = wrbuf_alloc();
-    xsltStylesheet *s;
 
     conf_dir_path(config, w, fname);
-    s = xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(w));
-    wrbuf_destroy(w);
-    return s;
+    return w;
 }
 
 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
@@ -901,6 +904,15 @@ static int parse_config(struct conf_config *config, xmlNode *root)
             tmp->next = config->servers;
             config->servers = tmp;
         }
+        else if (!strcmp((const char *) n->name, "threads"))
+        {
+            xmlChar *number = xmlGetProp(n, (xmlChar *) "number");
+            if (number)
+            {
+                config->no_threads = atoi(number);
+                xmlFree(number);
+            }
+        }
         else if (!strcmp((const char *) n->name, "targetprofiles"))
         {
             yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service");
@@ -916,114 +928,6 @@ static int parse_config(struct conf_config *config, xmlNode *root)
     return 0;
 }
 
-static int process_config_includes(struct conf_config *config, xmlNode *n);
-
-static int config_include_one(struct conf_config *config, xmlNode **sib,
-    const char *path)
-{
-    struct stat st;
-    if (stat(path, &st) < 0)
-    {
-        yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
-        return -1;
-    }
-    else
-    {
-        if ((st.st_mode & S_IFMT) == S_IFREG)
-        {
-            xmlDoc *doc = xmlParseFile(path);
-            if (doc)
-            {
-                xmlNodePtr t = xmlDocGetRootElement(doc);
-                int ret = process_config_includes(config, t);
-                *sib = xmlAddNextSibling(*sib, xmlCopyNode(t, 1));
-                xmlFreeDoc(doc);
-                if (ret)
-                    return -1;
-            }
-            else
-            {
-                yaz_log(YLOG_FATAL, "Could not parse %s", path);
-                return -1;
-            }
-        }
-    }
-    return 0;
-}
-
-static int config_include_src(struct conf_config *config, xmlNode **np,
-                              const char *src)
-{
-    int ret = 0; /* return code. OK so far */
-    WRBUF w = wrbuf_alloc();
-    xmlNodePtr sib; /* our sibling that we append */
-    xmlNodePtr c; /* tmp node */
-
-    wrbuf_printf(w, " begin include src=\"%s\" ", src);
-
-    /* replace include element with a 'begin' comment */
-    sib = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
-    xmlReplaceNode(*np, sib);
-
-    xmlFreeNode(*np);
-
-    wrbuf_rewind(w);
-    conf_dir_path(config, w, src);
-#if USE_POSIX_GLOB
-    {
-        size_t i;
-        glob_t glob_res;
-        glob(wrbuf_cstr(w), 0 /* flags */, 0 /* errfunc */, &glob_res);
-        
-        for (i = 0; ret == 0 && i < glob_res.gl_pathc; i++)
-        {
-            const char *path = glob_res.gl_pathv[i];
-            ret = config_include_one(config, &sib, path);
-        }
-        globfree(&glob_res);
-    }
-#else
-    ret = config_include_one(config, &sib, wrbuf_cstr(w));
-#endif
-    wrbuf_rewind(w);
-    wrbuf_printf(w, " end include src=\"%s\" ", src);
-    c = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
-    sib = xmlAddNextSibling(sib, c);
-    
-    *np = sib;
-    wrbuf_destroy(w);
-    return ret;
-}
-
-static int process_config_includes(struct conf_config *config, xmlNode *n)
-{
-    for (; n; n = n->next)
-    {
-        if (n->type == XML_ELEMENT_NODE)
-        {
-            if (!strcmp((const char *) n->name, "include"))
-            {
-                xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
-                if (src)
-                {
-                    int ret = config_include_src(config, &n,
-                                                 (const char *) src);
-                    xmlFree(src);
-                    if (ret)
-                        return ret;
-                        
-                }
-            }
-            else
-            {
-                if (process_config_includes(config, n->children))
-                    return -1;
-            }
-        }
-    }
-    return 0;
-}
-
 struct conf_config *config_create(const char *fname, int verbose)
 {
     xmlDoc *doc = xmlParseFile(fname);
@@ -1044,6 +948,7 @@ struct conf_config *config_create(const char *fname, int verbose)
 
     config->nmem = nmem;
     config->servers = 0;
+    config->no_threads = 0;
 
     config->confdir = wrbuf_alloc();
     if ((p = strrchr(fname, 
@@ -1060,14 +965,18 @@ struct conf_config *config_create(const char *fname, int verbose)
     wrbuf_puts(config->confdir, "");
     
     n = xmlDocGetRootElement(doc);
-    r = process_config_includes(config, n);
+    r = yaz_xml_include_simple(n, wrbuf_cstr(config->confdir));
     if (r == 0) /* OK */
     {
         if (verbose)
         {
             yaz_log(YLOG_LOG, "Configuration %s after include processing",
                     fname);
+#if LIBXML_VERSION >= 20600
             xmlDocFormatDump(yaz_log_file(), doc, 0);
+#else
+            xmlDocDump(yaz_log_file(), doc);
+#endif
         }
         r = parse_config(config, n);
     }
@@ -1133,6 +1042,7 @@ int config_start_listeners(struct conf_config *conf,
                            const char *listener_override)
 {
     struct conf_server *ser;
+    pazpar2_chan_man_start(conf->no_threads);
     for (ser = conf->servers; ser; ser = ser->next)
     {
         WRBUF w = wrbuf_alloc();