1 /* $Id: database.c,v 1.5 2007-03-29 13:44:38 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;
131 struct conf_retrievalprofile *retrieval;
132 struct conf_queryprofile *query;
139 nmem = nmem_create();
142 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
146 if (!(retrieval = database_retrievalprofile(id)) ||
147 !(query = database_queryprofile(id)))
152 if (strlen(id) > 255)
154 strcpy(hostport, id);
155 if ((dbname = strchr(hostport, '/')))
159 if (!(host = find_host(hostport)))
161 db = nmem_malloc(nmem, sizeof(*db));
162 memset(db, 0, sizeof(*db));
164 db->url = nmem_strdup(nmem, id);
166 db->databases = xmalloc(2 * sizeof(char *));
167 db->databases[0] = nmem_strdup(nmem, dbname);
168 db->databases[1] = 0;
170 db->explain = explain;
171 db->qprofile = query;
172 db->rprofile = retrieval;
174 db->next = databases;
180 // Return a database structure by ID. Load and add to list if necessary
181 // new==1 just means we know it's not in the list
182 struct database *find_database(const char *id, int new)
187 for (p = databases; p; p = p->next)
188 if (!strcmp(p->url, id))
191 return load_database(id);
194 static int match_zurl(const char *zurl, const char *pattern)
196 if (!strcmp(pattern, "*"))
198 else if (!strncmp(pattern, "*/", 2))
200 char *db = strchr(zurl, '/');
203 if (!strcmp(pattern + 2, db))
208 else if (!strcmp(pattern, zurl))
214 // This will be generalized at some point
215 static int match_criterion(struct database *db, struct database_criterion *c)
217 if (!strcmp(c->name, "id"))
219 struct database_criterion_value *v;
220 for (v = c->values; v; v = v->next)
221 if (match_zurl(db->url, v->value))
229 int database_match_criteria(struct database *db, struct database_criterion *cl)
231 for (; cl; cl = cl->next)
232 if (!match_criterion(db, cl))
234 if (cl) // one of the criteria failed to match -- skip this db
240 // Cycles through databases, calling a handler function on the ones for
241 // which all criteria matched.
242 int grep_databases(void *context, struct database_criterion *cl,
243 void (*fun)(void *context, struct database *db))
248 for (p = databases; p; p = p->next)
250 if (database_match_criteria(p, cl))
259 // This function will most likely vanish when a proper target profile mechanism is
261 void load_simpletargets(const char *fn)
263 FILE *f = fopen(fn, "r");
268 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
272 while (fgets(line, 255, f))
278 if (strncmp(line, "target ", 7))
280 line[strlen(line) - 1] = '\0';
282 if ((name = strchr(line, ';')))
287 if (!(db = find_database(url, 0)))
288 yaz_log(YLOG_WARN, "Unable to load database %s", url);
290 db->name = nmem_strdup(nmem, name);
299 * indent-tabs-mode: nil
301 * vim: shiftwidth=4 tabstop=8 expandtab