Functional settings system. At this point, they control the CCL map only
[pazpar2-moved-to-github.git] / src / database.c
index 3a987ea..95703e0 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: database.c,v 1.1 2007-03-15 16:55:34 quinn Exp $ */
+/* $Id: database.c,v 1.6 2007-03-30 02:45:07 quinn Exp $ */
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
@@ -6,12 +6,19 @@
 #include <libxslt/transform.h>
 #include <libxslt/xsltutils.h>
 #include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "pazpar2.h"
 #include "config.h"
 #include "http.h"
 #include "zeerex.h"
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+
 static struct host *hosts = 0;  // The hosts we know about 
 static struct database *databases = 0; // The databases we know about
 static NMEM nmem = 0;
@@ -38,6 +45,7 @@ static struct conf_queryprofile *database_queryprofile(const char *id)
 
 static xmlDoc *get_explain_xml(const char *id)
 {
+    struct stat st;
     char *dir;
     char path[256];
     char ide[256];
@@ -54,8 +62,10 @@ static xmlDoc *get_explain_xml(const char *id)
     dir = config->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
@@ -117,9 +127,8 @@ static struct host *find_host(const char *hostport)
 static struct database *load_database(const char *id)
 {
     xmlDoc *doc = get_explain_xml(id);
-    struct zr_explain *explain;
+    struct zr_explain *explain = 0;
     struct conf_retrievalprofile *retrieval;
-    struct conf_queryprofile *query;
     struct database *db;
     struct host *host;
     char hostport[256];
@@ -133,8 +142,7 @@ static struct database *load_database(const char *id)
         if (!explain)
             return 0;
     }
-    if (!(retrieval = database_retrievalprofile(id)) ||
-            !(query = database_queryprofile(id)))
+    if (!(retrieval = database_retrievalprofile(id)))
     {
         xmlFree(doc);
         return 0;
@@ -152,15 +160,16 @@ static struct database *load_database(const char *id)
     memset(db, 0, sizeof(*db));
     db->host = host;
     db->url = nmem_strdup(nmem, id);
-    db->name = dbname;
+    db->name = 0;
     db->databases = xmalloc(2 * sizeof(char *));
     db->databases[0] = nmem_strdup(nmem, dbname);
     db->databases[1] = 0;
     db->errors = 0;
     db->explain = explain;
-    db->qprofile = query;
     db->rprofile = retrieval;
+    db->settings = 0;
     db->next = databases;
+    db->ccl_map = 0;
     databases = db;
 
     return db;
@@ -180,17 +189,67 @@ struct database *find_database(const char *id, int new)
     return load_database(id);
 }
 
-// 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))
+static int match_zurl(const char *zurl, const char *pattern)
+{
+    if (!strcmp(pattern, "*"))
+        return 1;
+    else if (!strncmp(pattern, "*/", 2))
+    {
+        char *db = strchr(zurl, '/');
+        if (!db)
+            return 0;
+        if (!strcmp(pattern + 2, db))
+            return 1;
+        else
+            return 0;
+    }
+    else if (!strcmp(pattern, zurl))
+        return 1;
+    else
+        return 0;
+}
+
+// This will be generalized at some point
+static int match_criterion(struct database *db, struct database_criterion *c)
+{
+    if (!strcmp(c->name, "id"))
+    {
+        struct database_criterion_value *v;
+        for (v = c->values; v; v = v->next)
+            if (match_zurl(db->url, v->value))
+                return 1;
+        return 0;
+    }
+    else
+        return 0;
+}
+
+int database_match_criteria(struct database *db, struct database_criterion *cl)
+{
+    for (; cl; cl = cl->next)
+        if (!match_criterion(db, 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 grep_databases(void *context, struct database_criterion *cl,
+        void (*fun)(void *context, struct database *db))
 {
     struct database *p;
     int i;
 
     for (p = databases; p; p = p->next)
     {
-        (*fun)(context, p);
-        i++;
+        if (database_match_criteria(p, cl))
+        {
+            (*fun)(context, p);
+            i++;
+        }
     }
     return i;
 }
@@ -212,6 +271,7 @@ void load_simpletargets(const char *fn)
     {
         char *url;
         char *name;
+        struct database *db;
 
         if (strncmp(line, "target ", 7))
             continue;
@@ -222,8 +282,10 @@ void load_simpletargets(const char *fn)
 
         url = line + 7;
 
-        if (!find_database(url, 0))
+        if (!(db = find_database(url, 0)))
             yaz_log(YLOG_WARN, "Unable to load database %s", url);
+        if (name && db)
+            db->name = nmem_strdup(nmem, name);
     }
     fclose(f);
 }