Remove unmaintained zeerex/explain code
[pazpar2-moved-to-github.git] / src / pazpar2_config.c
index 60f1585..ccd7be4 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) 2006-2011 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
@@ -31,22 +31,15 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #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
 #include <unistd.h>
 #endif
+#include "ppmutex.h"
+#include "incref.h"
 #include "pazpar2_config.h"
 #include "settings.h"
 #include "eventl.h"
@@ -56,16 +49,17 @@ struct conf_config
 {
     NMEM nmem; /* for conf_config and servers memory */
     struct conf_server *servers;
+    
+    int no_threads;
     WRBUF confdir;
+    iochan_man_t iochan_man;
+    database_hosts_t database_hosts;
 };
 
 
 static char *parse_settings(struct conf_config *config,
                             NMEM nmem, xmlNode *node);
 
-static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
-                                                        xmlNode *node);
-
 static void conf_metadata_assign(NMEM nmem, 
                                  struct conf_metadata * metadata,
                                  const char *name,
@@ -119,12 +113,12 @@ static struct conf_service *service_init(struct conf_server *server,
     NMEM nmem = nmem_create();
 
     service = nmem_malloc(nmem, sizeof(struct conf_service));
+    service->mutex = 0;
     service->ref_count = 1;
     service->nmem = nmem;
     service->next = 0;
     service->settings = 0;
     service->databases = 0;
-    service->targetprofiles = 0;
     service->server = server;
     service->session_timeout = 60; /* default session timeout */
     service->z3950_session_timeout = 180;
@@ -133,6 +127,7 @@ static struct conf_service *service_init(struct conf_server *server,
     service->relevance_pct = 0;
     service->sort_pct = 0;
     service->mergekey_pct = 0;
+    service->facet_pct = 0;
 
     service->id = service_id ? nmem_strdup(nmem, service_id) : 0;
     service->num_metadata = num_metadata;
@@ -246,13 +241,13 @@ void service_destroy(struct conf_service *service)
 {
     if (service)
     {
-        assert(service->ref_count > 0);
-        service->ref_count--;
-        if (service->ref_count == 0)
+        if (!pazpar2_decref(&service->ref_count, service->mutex))
         {
             pp2_charset_destroy(service->relevance_pct);
             pp2_charset_destroy(service->sort_pct);
             pp2_charset_destroy(service->mergekey_pct);
+            pp2_charset_destroy(service->facet_pct);
+            yaz_mutex_destroy(&service->mutex);
             nmem_destroy(service->nmem);
         }
     }
@@ -260,7 +255,9 @@ void service_destroy(struct conf_service *service)
 
 void service_incref(struct conf_service *service)
 {
-    service->ref_count++;
+    yaz_log(YLOG_LOG, "service_incref. p=%p cnt=%d", service,
+            service->ref_count);
+    pazpar2_incref(&service->ref_count, service->mutex);
 }
 
 static int parse_metadata(struct conf_service *service, xmlNode *n,
@@ -409,9 +406,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;
+        }
     }
     
     
@@ -430,6 +437,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;
 }
@@ -503,17 +511,6 @@ static struct conf_service *service_create_static(struct conf_server *server,
         }
         else if (!strcmp((const char *) n->name, "settings"))
             got_settings++;
-        else if (!strcmp((const char *) n->name, (const char *) "targetprofiles"))
-        {
-            if (service->targetprofiles)
-            {
-                yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
-                return 0;
-            }
-            if (!(service->targetprofiles = 
-                  parse_targetprofiles(service->nmem, n)))
-                return 0;
-        }
         else if (!strcmp((const char *) n->name, "relevance"))
         {
             if (service->relevance_pct)
@@ -556,6 +553,20 @@ static struct conf_service *service_create_static(struct conf_server *server,
                     return 0;
             }
         }
+        else if (!strcmp((const char *) n->name, "facet"))
+        {
+            if (service->mergekey_pct)
+            {
+                yaz_log(YLOG_LOG, "facety may not repeat in service");
+                return 0;
+            }
+            else
+            {
+                service->facet_pct = pp2_charset_create_xml(n);
+                if (!service->mergekey_pct)
+                    return 0;
+            }
+        }
         else if (!strcmp((const char *) n->name, (const char *) "metadata"))
         {
             if (parse_metadata(service, n, &md_node, &sk_node))
@@ -642,7 +653,7 @@ static void inherit_server_settings(struct conf_service *s)
         }
     }
     
-    /* use relevance/sort/mergekey from server if not defined
+    /* use relevance/sort/mergekey/facet from server if not defined
        for this service.. */
     if (!s->relevance_pct)
     {
@@ -652,7 +663,7 @@ static void inherit_server_settings(struct conf_service *s)
             pp2_charset_incref(s->relevance_pct);
         }
         else
-            s->relevance_pct = pp2_charset_create(0);
+            s->relevance_pct = pp2_charset_create_a_to_z();
     }
     
     if (!s->sort_pct)
@@ -663,7 +674,7 @@ static void inherit_server_settings(struct conf_service *s)
             pp2_charset_incref(s->sort_pct);
         }
         else
-            s->sort_pct = pp2_charset_create(0);
+            s->sort_pct = pp2_charset_create_a_to_z();
     }
     
     if (!s->mergekey_pct)
@@ -674,7 +685,18 @@ static void inherit_server_settings(struct conf_service *s)
             pp2_charset_incref(s->mergekey_pct);
         }
         else
-            s->mergekey_pct = pp2_charset_create(0);
+            s->mergekey_pct = pp2_charset_create_a_to_z();
+    }
+
+    if (!s->facet_pct)
+    {
+        if (server->facet_pct)
+        {
+            s->facet_pct = server->facet_pct;
+            pp2_charset_incref(s->facet_pct);
+        }
+        else
+            s->facet_pct = pp2_charset_create(0);
     }
 }
 
@@ -687,6 +709,8 @@ struct conf_service *service_create(struct conf_server *server,
     {
         inherit_server_settings(service);
         resolve_databases(service);
+        assert(service->mutex == 0);
+        pazpar2_mutex_create(&service->mutex, "conf");
     }
     return service;
 }
@@ -696,21 +720,32 @@ 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;
     server->proxy_host = 0;
     server->proxy_port = 0;
     server->myurl = 0;
-    server->proxy_addr = 0;
     server->service = 0;
     server->config = config;
     server->next = 0;
     server->relevance_pct = 0;
     server->sort_pct = 0;
     server->mergekey_pct = 0;
+    server->facet_pct = 0;
     server->server_settings = 0;
+    server->http_server = 0;
+    server->iochan_man = 0;
+    server->database_hosts = 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)
@@ -769,6 +804,12 @@ static struct conf_server *server_create(struct conf_config *config,
             if (!server->mergekey_pct)
                 return 0;
         }
+        else if (!strcmp((const char *) n->name, "facet"))
+        {
+            server->facet_pct = pp2_charset_create_xml(n);
+            if (!server->facet_pct)
+                return 0;
+        }
         else if (!strcmp((const char *) n->name, "service"))
         {
             char *service_id = (char *)
@@ -784,8 +825,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;
                 }
 
@@ -819,64 +859,47 @@ static struct conf_server *server_create(struct conf_config *config,
     return server;
 }
 
-WRBUF conf_get_fname(struct conf_service *service, const char *fname)
+WRBUF conf_get_fname(struct conf_config *config, const char *fname)
 {
-    struct conf_config *config = service->server->config;
     WRBUF w = wrbuf_alloc();
 
     conf_dir_path(config, w, fname);
     return w;
 }
 
-static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
-                                                        xmlNode *node)
-{
-    struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
-    xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
-    xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
-
-    memset(r, 0, sizeof(*r));
-
-    if (type)
-    {
-        if (!strcmp((const char *) type, "local"))
-            r->type = Targetprofiles_local;
-        else
-        {
-            yaz_log(YLOG_FATAL, "Unknown targetprofile type");
-            return 0;
-        }
-    }
-    else
-    {
-        yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
-        return 0;
-    }
-
-    if (src)
-        r->src = nmem_strdup(nmem, (const char *) src);
-    else
-    {
-        yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
-        return 0;
-    }
-    xmlFree(type);
-    xmlFree(src);
-    return r;
-}
-
 struct conf_service *locate_service(struct conf_server *server,
                                     const char *service_id)
 {
     struct conf_service *s = server->service;
     for (; s; s = s->next)
         if (s->id && service_id && 0 == strcmp(s->id, service_id))
-            return s;
+            break;
         else if (!s->id && !service_id)
-            return s;
-    return 0;
+            break;
+    if (s)
+        service_incref(s);
+    return s;
 }
 
+void info_services(struct conf_server *server, WRBUF w)
+{
+    struct conf_service *s = server->service;
+    wrbuf_puts(w, " <services>\n");
+    for (; s; s = s->next)
+    {
+        wrbuf_puts(w, "  <service");
+        if (s->id)
+        {
+            wrbuf_puts(w, " id=\"");
+            wrbuf_xmlputs(w, s->id);
+            wrbuf_puts(w, "\"");
+        }
+        wrbuf_puts(w, "/>");
+
+        wrbuf_puts(w, "\n");
+    }
+    wrbuf_puts(w, " </services>\n");
+}
 
 static int parse_config(struct conf_config *config, xmlNode *root)
 {
@@ -894,6 +917,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((const char *) number);
+                xmlFree(number);
+            }
+        }
         else if (!strcmp((const char *) n->name, "targetprofiles"))
         {
             yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service");
@@ -909,114 +941,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);
@@ -1037,6 +961,8 @@ struct conf_config *config_create(const char *fname, int verbose)
 
     config->nmem = nmem;
     config->servers = 0;
+    config->no_threads = 0;
+    config->iochan_man = 0;
 
     config->confdir = wrbuf_alloc();
     if ((p = strrchr(fname, 
@@ -1053,14 +979,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);
     }
@@ -1086,6 +1016,9 @@ void server_destroy(struct conf_server *server)
     pp2_charset_destroy(server->relevance_pct);
     pp2_charset_destroy(server->sort_pct);
     pp2_charset_destroy(server->mergekey_pct);
+    pp2_charset_destroy(server->facet_pct);
+    yaz_log(YLOG_LOG, "server_destroy server=%p", server);
+    http_server_destroy(server->http_server);
 }
 
 void config_destroy(struct conf_config *config)
@@ -1093,12 +1026,15 @@ void config_destroy(struct conf_config *config)
     if (config)
     {
         struct conf_server *server = config->servers;
+        iochan_man_destroy(&config->iochan_man);    
         while (server)
         {
             struct conf_server *s_next = server->next;
             server_destroy(server);
             server = s_next;
         }
+        database_hosts_destroy(&config->database_hosts);
+
         wrbuf_destroy(config->confdir);
         nmem_destroy(config->nmem);
     }
@@ -1111,25 +1047,41 @@ void config_stop_listeners(struct conf_config *conf)
         http_close_server(ser);
 }
 
-void config_start_databases(struct conf_config *conf)
+void config_process_events(struct conf_config *conf)
 {
     struct conf_server *ser;
+    
+    conf->database_hosts = database_hosts_create();
     for (ser = conf->servers; ser; ser = ser->next)
     {
         struct conf_service *s = ser->service;
+
+        ser->database_hosts = conf->database_hosts;
+
         for (;s ; s = s->next)
+        {
             resolve_databases(s);
+            assert(s->mutex == 0);
+            pazpar2_mutex_create(&s->mutex, "service");
+        }
+        http_mutex_init(ser);
     }
+    iochan_man_events(conf->iochan_man);    
 }
 
 int config_start_listeners(struct conf_config *conf,
-                           const char *listener_override)
+                           const char *listener_override,
+                           const char *record_fname)
 {
     struct conf_server *ser;
+
+    conf->iochan_man = iochan_man_create(conf->no_threads);
     for (ser = conf->servers; ser; ser = ser->next)
     {
         WRBUF w = wrbuf_alloc();
         int r;
+
+        ser->iochan_man = conf->iochan_man;
         if (listener_override)
         {
             wrbuf_puts(w, listener_override);
@@ -1146,7 +1098,7 @@ int config_start_listeners(struct conf_config *conf,
                 wrbuf_printf(w, "%d", ser->port);
             }
         }
-        r = http_init(wrbuf_cstr(w), ser);
+        r = http_init(wrbuf_cstr(w), ser, record_fname);
         wrbuf_destroy(w);
         if (r)
             return -1;