X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fdatabase.c;h=fd88d0b6bfff1064ade3ba3a3a800e689f57037e;hb=d5225315828a7592c1e91a39912f27f05d177062;hp=42b3c3aba5f43a04b3d5805584c3ccbb8abea0c6;hpb=2376c683c81f8352acb83f96d329dca7266f34c9;p=pazpar2-moved-to-github.git diff --git a/src/database.c b/src/database.c index 42b3c3a..fd88d0b 100644 --- a/src/database.c +++ b/src/database.c @@ -1,5 +1,5 @@ /* This file is part of Pazpar2. - Copyright (C) 2006-2010 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 @@ -21,32 +21,21 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #endif -#include -#include #include #include #include #include #include +#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 -#if HAVE_SYS_SOCKET_H -#include -#endif -#if HAVE_NETDB_H -#include -#endif -#if HAVE_NETINET_IN_H -#include -#endif enum pazpar2_database_criterion_type { PAZPAR2_STRING_MATCH, @@ -65,57 +54,33 @@ struct database_criterion { struct database_criterion *next; }; - 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 (targetprofiles->type != Targetprofiles_local) - { - yaz_log(YLOG_FATAL, "Only supports local type"); - return 0; - } - dir = targetprofiles->src; - urlencode(id, ide); - sprintf(path, "%s/%s", dir, ide); - 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, iochan_man_t iochan_man) +static struct host *create_host(const char *hostport) { struct host *host; + char *db_comment; host = xmalloc(sizeof(struct host)); host->hostport = xstrdup(hostport); + db_comment = strchr(host->hostport, '#'); + if (db_comment) + *db_comment = '\0'; host->connections = 0; - host->ipport = 0; host->mutex = 0; - if (host_getaddrinfo(host, iochan_man)) - { - xfree(host->hostport); - xfree(host); - return 0; - } - yaz_mutex_create(&host->mutex); + pazpar2_mutex_create(&host->mutex, "host"); + + yaz_cond_create(&host->cond_ready); return host; } -static struct host *find_host(database_hosts_t hosts, - const char *hostport, iochan_man_t iochan_man) +struct host *find_host(database_hosts_t hosts, const char *hostport) { struct host *p; yaz_mutex_enter(hosts->mutex); @@ -124,7 +89,7 @@ static struct host *find_host(database_hosts_t hosts, break; if (!p) { - p = create_host(hostport, iochan_man); + p = create_host(hostport); if (p) { p->next = hosts->hosts; @@ -135,58 +100,17 @@ static struct host *find_host(database_hosts_t hosts, return p; } -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) { struct database *db; - char hostport[256]; - char *dbname; struct setting *idset; - if (strlen(id) > 255) - return 0; - strcpy(hostport, id); - if ((dbname = strchr(hostport, '/'))) - *(dbname++) = '\0'; - else - dbname = ""; db = nmem_malloc(nmem, sizeof(*db)); - memset(db, 0, sizeof(*db)); - db->host = 0; db->url = nmem_strdup(nmem, id); - 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_NEGOTIATION_CHARSET+1; + db->num_settings = PZ_MAX_EOF; db->settings = nmem_malloc(nmem, sizeof(struct settings*) * db->num_settings); + db->next = 0; memset(db->settings, 0, sizeof(struct settings*) * db->num_settings); idset = nmem_malloc(nmem, sizeof(*idset)); idset->precedence = 0; @@ -194,7 +118,6 @@ struct database *new_database(const char *id, NMEM nmem) idset->target = idset->value = db->url; idset->next = 0; db->settings[PZ_ID] = idset; - db->next = 0; return db; } @@ -203,18 +126,9 @@ 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->next = service->databases; service->databases = db; @@ -367,7 +281,7 @@ static int database_match_criteria(struct setting **settings, // 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)) + void (*fun)(struct session *se, struct session_database *db)) { struct session_database *p; NMEM nmem = nmem_create(); @@ -410,7 +324,7 @@ database_hosts_t database_hosts_create(void) database_hosts_t p = xmalloc(sizeof(*p)); p->hosts = 0; p->mutex = 0; - yaz_mutex_create(&p->mutex); + pazpar2_mutex_create(&p->mutex, "database"); return p; } @@ -423,7 +337,7 @@ void database_hosts_destroy(database_hosts_t *pp) { struct host *p_next = p->next; yaz_mutex_destroy(&p->mutex); - xfree(p->ipport); + yaz_cond_destroy(&p->cond_ready); xfree(p->hostport); xfree(p); p = p_next;