1 /* $Id: database.c,v 1.6 2007-03-30 02:45:07 quinn Exp $ */
3 #include <libxml/parser.h>
4 #include <libxml/tree.h>
5 #include <libxslt/xslt.h>
6 #include <libxslt/transform.h>
7 #include <libxslt/xsltutils.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
22 static struct host *hosts = 0; // The hosts we know about
23 static struct database *databases = 0; // The databases we know about
26 // This needs to be extended with selection criteria
27 static struct conf_retrievalprofile *database_retrievalprofile(const char *id)
31 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
34 if (!config->retrievalprofiles)
36 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
38 return config->retrievalprofiles;
41 static struct conf_queryprofile *database_queryprofile(const char *id)
43 return (struct conf_queryprofile*) 1;
46 static xmlDoc *get_explain_xml(const char *id)
52 if (!config || !config->targetprofiles)
54 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
57 if (config->targetprofiles->type != Targetprofiles_local)
59 yaz_log(YLOG_FATAL, "Only supports local type");
62 dir = config->targetprofiles->src;
64 sprintf(path, "%s/%s", dir, ide);
66 return xmlParseFile(path);
71 // Create a new host structure for hostport
72 static struct host *create_host(const char *hostport)
74 struct addrinfo *addrinfo, hints;
78 unsigned char addrbuf[4];
81 host = xmalloc(sizeof(struct host));
82 host->hostport = xstrdup(hostport);
83 host->connections = 0;
85 if ((port = strchr(hostport, ':')))
91 hints.ai_family = PF_INET;
92 hints.ai_socktype = SOCK_STREAM;
93 hints.ai_protocol = IPPROTO_TCP;
96 hints.ai_canonname = 0;
98 // This is not robust code. It assumes that getaddrinfo always
99 // returns AF_INET address.
100 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
102 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", hostport, gai_strerror(res));
103 xfree(host->hostport);
107 assert(addrinfo->ai_family == PF_INET);
108 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
109 sprintf(ipport, "%u.%u.%u.%u:%s",
110 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
111 host->ipport = xstrdup(ipport);
112 freeaddrinfo(addrinfo);
118 static struct host *find_host(const char *hostport)
121 for (p = hosts; p; p = p->next)
122 if (!strcmp(p->hostport, hostport))
124 return create_host(hostport);
127 static struct database *load_database(const char *id)
129 xmlDoc *doc = get_explain_xml(id);
130 struct zr_explain *explain = 0;
131 struct conf_retrievalprofile *retrieval;
138 nmem = nmem_create();
141 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
145 if (!(retrieval = database_retrievalprofile(id)))
150 if (strlen(id) > 255)
152 strcpy(hostport, id);
153 if ((dbname = strchr(hostport, '/')))
157 if (!(host = find_host(hostport)))
159 db = nmem_malloc(nmem, sizeof(*db));
160 memset(db, 0, sizeof(*db));
162 db->url = nmem_strdup(nmem, id);
164 db->databases = xmalloc(2 * sizeof(char *));
165 db->databases[0] = nmem_strdup(nmem, dbname);
166 db->databases[1] = 0;
168 db->explain = explain;
169 db->rprofile = retrieval;
171 db->next = databases;
178 // Return a database structure by ID. Load and add to list if necessary
179 // new==1 just means we know it's not in the list
180 struct database *find_database(const char *id, int new)
185 for (p = databases; p; p = p->next)
186 if (!strcmp(p->url, id))
189 return load_database(id);
192 static int match_zurl(const char *zurl, const char *pattern)
194 if (!strcmp(pattern, "*"))
196 else if (!strncmp(pattern, "*/", 2))
198 char *db = strchr(zurl, '/');
201 if (!strcmp(pattern + 2, db))
206 else if (!strcmp(pattern, zurl))
212 // This will be generalized at some point
213 static int match_criterion(struct database *db, struct database_criterion *c)
215 if (!strcmp(c->name, "id"))
217 struct database_criterion_value *v;
218 for (v = c->values; v; v = v->next)
219 if (match_zurl(db->url, v->value))
227 int database_match_criteria(struct database *db, struct database_criterion *cl)
229 for (; cl; cl = cl->next)
230 if (!match_criterion(db, cl))
232 if (cl) // one of the criteria failed to match -- skip this db
238 // Cycles through databases, calling a handler function on the ones for
239 // which all criteria matched.
240 int grep_databases(void *context, struct database_criterion *cl,
241 void (*fun)(void *context, struct database *db))
246 for (p = databases; p; p = p->next)
248 if (database_match_criteria(p, cl))
257 // This function will most likely vanish when a proper target profile mechanism is
259 void load_simpletargets(const char *fn)
261 FILE *f = fopen(fn, "r");
266 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
270 while (fgets(line, 255, f))
276 if (strncmp(line, "target ", 7))
278 line[strlen(line) - 1] = '\0';
280 if ((name = strchr(line, ';')))
285 if (!(db = find_database(url, 0)))
286 yaz_log(YLOG_WARN, "Unable to load database %s", url);
288 db->name = nmem_strdup(nmem, name);
297 * indent-tabs-mode: nil
299 * vim: shiftwidth=4 tabstop=8 expandtab