Skip Facets: Check for empty string. And reset Facet settings
[pazpar2-moved-to-github.git] / src / database.c
index b1cd969..da26ed0 100644 (file)
-/* $Id: database.c,v 1.2 2007-03-16 09:34:55 adam Exp $ */
+/* This file is part of Pazpar2.
+   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
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
-#include <libxslt/xslt.h>
-#include <libxslt/transform.h>
-#include <libxslt/xsltutils.h>
 #include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <yaz/log.h>
+#include <yaz/nmem.h>
 
-#include "pazpar2.h"
-#include "config.h"
+#include "ppmutex.h"
+#include "session.h"
+#include "host.h"
+#include "pazpar2_config.h"
+#include "settings.h"
 #include "http.h"
 #include "zeerex.h"
+#include "database.h"
 
 #include <sys/types.h>
+#if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+#if HAVE_NETDB_H
 #include <netdb.h>
+#endif
+#if HAVE_NETINET_IN_H
 #include <netinet/in.h>
+#endif
 
-static struct host *hosts = 0;  // The hosts we know about 
-static struct database *databases = 0; // The databases we know about
-static NMEM nmem = 0;
+enum pazpar2_database_criterion_type {
+    PAZPAR2_STRING_MATCH,
+    PAZPAR2_SUBSTRING_MATCH
+};
 
-// This needs to be extended with selection criteria
-static struct conf_retrievalprofile *database_retrievalprofile(const char *id)
-{
-    if (!config)
-    {
-        yaz_log(YLOG_FATAL, "Must load configuration (-f)");
-        exit(1);
-    }
-    if (!config->retrievalprofiles)
-    {
-        yaz_log(YLOG_FATAL, "No retrieval profiles defined");
-    }
-    return config->retrievalprofiles;
-}
+struct database_criterion_value {
+    char *value;
+    struct database_criterion_value *next;
+};
+
+struct database_criterion {
+    char *name;
+    enum pazpar2_database_criterion_type type;
+    struct database_criterion_value *values;
+    struct database_criterion *next;
+};
 
-static struct conf_queryprofile *database_queryprofile(const char *id)
-{
-    return (struct conf_queryprofile*) 1;
-}
 
-static xmlDoc *get_explain_xml(const char *id)
+struct database_hosts {
+    struct host *hosts;
+    YAZ_MUTEX mutex;
+};
+
+static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
+                               const char *id)
 {
+    struct stat st;
     char *dir;
     char path[256];
     char ide[256];
-    if (!config || !config->targetprofiles)
-    {
-        yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
-        return 0;
-    }
-    if (config->targetprofiles->type != Targetprofiles_local)
+    if (targetprofiles->type != Targetprofiles_local)
     {
         yaz_log(YLOG_FATAL, "Only supports local type");
         return 0;
     }
-    dir = config->targetprofiles->src;
+    dir = targetprofiles->src;
     urlencode(id, ide);
     sprintf(path, "%s/%s", dir, ide);
-    yaz_log(YLOG_LOG, "Path: %s", path);
-    return xmlParseFile(path);
+    if (!stat(path, &st))
+        return xmlParseFile(path);
+    else
+        return 0;
 }
 
 // Create a new host structure for hostport
-static struct host *create_host(const char *hostport)
+static struct host *create_host(const char *hostport, iochan_man_t iochan_man)
 {
-    struct addrinfo *addrinfo, hints;
     struct host *host;
-    char *port;
-    char ipport[128];
-    unsigned char addrbuf[4];
-    int res;
 
     host = xmalloc(sizeof(struct host));
     host->hostport = xstrdup(hostport);
     host->connections = 0;
+    host->ipport = 0;
+    host->mutex = 0;
 
-    if ((port = strchr(hostport, ':')))
-        *(port++) = '\0';
-    else
-        port = "210";
-
-    hints.ai_flags = 0;
-    hints.ai_family = PF_INET;
-    hints.ai_socktype = SOCK_STREAM;
-    hints.ai_protocol = IPPROTO_TCP;
-    hints.ai_addrlen = 0;
-    hints.ai_addr = 0;
-    hints.ai_canonname = 0;
-    hints.ai_next = 0;
-    // This is not robust code. It assumes that getaddrinfo always
-    // returns AF_INET address.
-    if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
+    if (host_getaddrinfo(host, iochan_man))
     {
-        yaz_log(YLOG_WARN, "Failed to resolve %s: %s", hostport, gai_strerror(res));
         xfree(host->hostport);
         xfree(host);
         return 0;
     }
-    assert(addrinfo->ai_family == PF_INET);
-    memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
-    sprintf(ipport, "%u.%u.%u.%u:%s",
-            addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
-    host->ipport = xstrdup(ipport);
-    freeaddrinfo(addrinfo);
-    host->next = hosts;
-    hosts = host;
+    pazpar2_mutex_create(&host->mutex, "host");
+
+    yaz_cond_create(&host->cond_ready);
+
     return host;
 }
 
-static struct host *find_host(const char *hostport)
+static struct host *find_host(database_hosts_t hosts,
+                              const char *hostport, iochan_man_t iochan_man)
 {
     struct host *p;
-    for (p = hosts; p; p = p->next)
+    yaz_mutex_enter(hosts->mutex);
+    for (p = hosts->hosts; p; p = p->next)
         if (!strcmp(p->hostport, hostport))
-            return p;
-    return create_host(hostport);
+            break;
+    if (!p)
+    {
+        p = create_host(hostport, iochan_man);
+        if (p)
+        {
+            p->next = hosts->hosts;
+            hosts->hosts = p;
+        }
+    }
+    yaz_mutex_leave(hosts->mutex);
+    return p;
 }
 
-static struct database *load_database(const char *id)
+int resolve_database(struct conf_service *service, struct database *db)
+{
+    if (db->host == 0)
+    {
+        struct host *host;
+        char *p;
+        char hostport[256];
+        strcpy(hostport, db->url);
+        if ((p = strchr(hostport, '/')))
+            *p = '\0';
+        if (!(host = find_host(service->server->database_hosts,
+                               hostport, service->server->iochan_man)))
+            return -1;
+        db->host = host;
+    }
+    return 0;
+}
+
+void resolve_databases(struct conf_service *service)
+{
+    struct database *db = service->databases;
+    for (; db; db = db->next)
+        resolve_database(service, db);
+}
+
+struct database *new_database(const char *id, NMEM nmem)
 {
-    xmlDoc *doc = get_explain_xml(id);
-    struct zr_explain *explain;
-    struct conf_retrievalprofile *retrieval;
-    struct conf_queryprofile *query;
     struct database *db;
-    struct host *host;
     char hostport[256];
     char *dbname;
+    struct setting *idset;
 
-    if (!nmem)
-        nmem = nmem_create();
-    if (doc)
-    {
-        explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
-        if (!explain)
-            return 0;
-    }
-    if (!(retrieval = database_retrievalprofile(id)) ||
-            !(query = database_queryprofile(id)))
-    {
-        xmlFree(doc);
-        return 0;
-    }
     if (strlen(id) > 255)
         return 0;
     strcpy(hostport, id);
     if ((dbname = strchr(hostport, '/')))
         *(dbname++) = '\0';
     else
-        dbname = "Default";
-    if (!(host = find_host(hostport)))
-        return 0;
+        dbname = "";
     db = nmem_malloc(nmem, sizeof(*db));
     memset(db, 0, sizeof(*db));
-    db->host = host;
+    db->host = 0;
     db->url = nmem_strdup(nmem, id);
-    db->name = dbname;
-    db->databases = xmalloc(2 * sizeof(char *));
+    db->databases = nmem_malloc(nmem, 2 * sizeof(char *));
     db->databases[0] = nmem_strdup(nmem, dbname);
     db->databases[1] = 0;
     db->errors = 0;
+    db->explain = 0;
+
+    db->num_settings = PZ_MAX_EOF;
+    db->settings = nmem_malloc(nmem, sizeof(struct settings*) * 
+                               db->num_settings);
+    memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
+    idset = nmem_malloc(nmem, sizeof(*idset));
+    idset->precedence = 0;
+    idset->name = "pz:id";
+    idset->target = idset->value = db->url;
+    idset->next = 0;
+    db->settings[PZ_ID] = idset;
+    db->next = 0;
+
+    return db;
+}
+
+static struct database *load_database(const char *id,
+                                      struct conf_service *service)
+{
+    struct database *db;
+    struct zr_explain *explain = 0;
+    xmlDoc *doc = 0;
+
+    if (service->targetprofiles 
+        && (doc = get_explain_xml(service->targetprofiles, id)))
+    {
+        explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
+        if (!explain)
+            return 0;
+    }
+    db = new_database(id, service->nmem);
     db->explain = explain;
-    db->qprofile = query;
-    db->rprofile = retrieval;
-    db->next = databases;
-    databases = db;
+    db->next = service->databases;
+    service->databases = db;
 
     return db;
 }
 
 // Return a database structure by ID. Load and add to list if necessary
 // new==1 just means we know it's not in the list
-struct database *find_database(const char *id, int new)
+struct database *find_database(const char *id, struct conf_service *service)
 {
     struct database *p;
-    if (!new)
+    for (p = service->databases; p; p = p->next)
+        if (!strcmp(p->url, id))
+            return p;
+    return load_database(id, service);
+}
+
+// This whole session_grep database thing should be moved elsewhere
+
+int match_zurl(const char *zurl, const char *pattern)
+{
+    int len;
+
+    if (!strcmp(pattern, "*"))
+        return 1;
+    else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
+    {
+        char *db = strchr(zurl, '/');
+        if (!db)
+            return 0;
+        if (!strcmp(pattern + 2, db))
+            return 1;
+        else
+            return 0;
+    }
+    else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
     {
-        for (p = databases; p; p = p->next)
-            if (!strcmp(p->url, id))
-                return p;
+        if (!strncmp(pattern, zurl, len))
+            return 1;
+        else
+            return 2;
     }
-    return load_database(id);
+    else if (!strcmp(pattern, zurl))
+        return 1;
+    else
+        return 0;
 }
 
-// Needs to be extended with criteria
-// Cycles through databases, calling a handler function on each.
-int grep_databases(void *context, void (*fun)(void *context, struct database *db))
+// This will be generalized at some point
+static int match_criterion(struct setting **settings,
+                           struct conf_service *service, 
+                           struct database_criterion *c)
 {
-    struct database *p;
-    int i;
+    int offset = settings_lookup_offset(service, c->name);
+    struct database_criterion_value *v;
 
-    for (p = databases; p; p = p->next)
+    if (offset < 0)
     {
-        (*fun)(context, p);
-        i++;
+        yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
+        return 0;
     }
-    return i;
+    if (!settings[offset])
+        return 0;
+    for (v = c->values; v; v = v->next)
+    {
+        if (c->type == PAZPAR2_STRING_MATCH)
+        {
+            if (offset == PZ_ID)
+            {
+                if (match_zurl(settings[offset]->value, v->value))
+                    break;
+            }
+            else 
+            {
+                if (!strcmp(settings[offset]->value, v->value))
+                    break;
+            }
+        }            
+        else if (c->type == PAZPAR2_SUBSTRING_MATCH)
+        {
+            if (strstr(settings[offset]->value, v->value))
+                break;
+        }
+    }
+    if (v)
+        return 1;
+    else
+        return 0;
 }
 
-// This function will most likely vanish when a proper target profile mechanism is
-// introduced.
-void load_simpletargets(const char *fn)
+// parses crit1=val1,crit2=val2|val3,...
+static struct database_criterion *create_database_criterion(NMEM m,
+                                                            const char *buf)
 {
-    FILE *f = fopen(fn, "r");
-    char line[256];
+    struct database_criterion *res = 0;
+    char **values;
+    int num;
+    int i;
 
-    if (!f)
+    if (!buf || !*buf)
+        return 0;
+    nmem_strsplit(m, ",", buf,  &values, &num);
+    for (i = 0; i < num; i++)
     {
-        yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
-        exit(1);
+        char **subvalues;
+        int subnum;
+        int subi;
+        struct database_criterion *new = nmem_malloc(m, sizeof(*new));
+        char *eq;
+        if ((eq = strchr(values[i], '=')))
+            new->type = PAZPAR2_STRING_MATCH;
+        else if ((eq = strchr(values[i], '~')))
+            new->type = PAZPAR2_SUBSTRING_MATCH;
+        else
+        {
+            yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
+            return 0;
+        }
+        *(eq++) = '\0';
+        new->name = values[i];
+        nmem_strsplit(m, "|", eq, &subvalues, &subnum);
+        new->values = 0;
+        for (subi = 0; subi < subnum; subi++)
+        {
+            struct database_criterion_value *newv
+                = nmem_malloc(m, sizeof(*newv));
+            newv->value = subvalues[subi];
+            newv->next = new->values;
+            new->values = newv;
+        }
+        new->next = res;
+        res = new;
     }
+    return res;
+}
 
-    while (fgets(line, 255, f))
-    {
-        char *url;
-        char *name;
+static int database_match_criteria(struct setting **settings,
+                                   struct conf_service *service,
+                                   struct database_criterion *cl)
+{
+    for (; cl; cl = cl->next)
+        if (!match_criterion(settings, service, cl))
+            break;
+    if (cl) // one of the criteria failed to match -- skip this db
+        return 0;
+    else
+        return 1;
+}
+
+// Cycles through databases, calling a handler function on the ones for
+// which all criteria matched.
+int session_grep_databases(struct session *se, const char *filter,
+                           void (*fun)(void *context, struct session_database *db))
+{
+    struct session_database *p;
+    NMEM nmem = nmem_create();
+    int i = 0;
+    struct database_criterion *cl = create_database_criterion(nmem, filter);
 
-        if (strncmp(line, "target ", 7))
+    for (p = se->databases; p; p = p->next)
+    {
+        if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
+            continue;
+        if (!p->settings[PZ_NAME])
             continue;
-        line[strlen(line) - 1] = '\0';
+        if (database_match_criteria(p->settings, se->service, cl))
+        {
+            (*fun)(se, p);
+            i++;
+        }
+    }
+    nmem_destroy(nmem);
+    return i;
+}
 
-        if ((name = strchr(line, ';')))
-            *(name++) = '\0';
+int predef_grep_databases(void *context, struct conf_service *service,
+                          void (*fun)(void *context, struct database *db))
+{
+    struct database *p;
+    int i = 0;
 
-        url = line + 7;
+    for (p = service->databases; p; p = p->next)
+        if (database_match_criteria(p->settings, service, 0))
+        {
+            (*fun)(context, p);
+            i++;
+        }
+    return i;
+}
 
-        if (!find_database(url, 0))
-            yaz_log(YLOG_WARN, "Unable to load database %s", url);
-    }
-    fclose(f);
+database_hosts_t database_hosts_create(void)
+{
+    database_hosts_t p = xmalloc(sizeof(*p));
+    p->hosts = 0;
+    p->mutex = 0;
+    pazpar2_mutex_create(&p->mutex, "database");
+    return p;
 }
 
+void database_hosts_destroy(database_hosts_t *pp)
+{
+    if (*pp)
+    {
+        struct host *p = (*pp)->hosts;
+        while (p)
+        {
+            struct host *p_next = p->next;
+            yaz_mutex_destroy(&p->mutex);
+            yaz_cond_destroy(&p->cond_ready);
+            xfree(p->ipport);
+            xfree(p->hostport);
+            xfree(p);
+            p = p_next;
+        }
+        yaz_mutex_destroy(&(*pp)->mutex);
+        xfree(*pp);
+    }
+}
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+