X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fdatabase.c;h=5ee51bb963a7f9ac4dafdf43acec2b0f4182c23e;hb=235995289021a11b33312c91a55ccfbca605348b;hp=3a987ea89b64a6bcd70e33dad5b7cc685c63a3b9;hpb=9a660575bb51d190608e5cdec416bb8a9acf3389;p=pazpar2-moved-to-github.git diff --git a/src/database.c b/src/database.c index 3a987ea..5ee51bb 100644 --- a/src/database.c +++ b/src/database.c @@ -1,4 +1,4 @@ -/* $Id: database.c,v 1.1 2007-03-15 16:55:34 quinn Exp $ */ +/* $Id: database.c,v 1.4 2007-03-23 03:26:22 quinn Exp $ */ #include #include @@ -12,6 +12,11 @@ #include "http.h" #include "zeerex.h" +#include +#include +#include +#include + static struct host *hosts = 0; // The hosts we know about static struct database *databases = 0; // The databases we know about static NMEM nmem = 0; @@ -152,7 +157,7 @@ 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; @@ -180,17 +185,47 @@ 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)) +// 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 (!strcmp(v->value, db->url)) + 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 +247,7 @@ void load_simpletargets(const char *fn) { char *url; char *name; + struct database *db; if (strncmp(line, "target ", 7)) continue; @@ -222,8 +258,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); }