1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
33 #include "pazpar2_config.h"
38 #include <sys/types.h>
40 enum pazpar2_database_criterion_type {
42 PAZPAR2_SUBSTRING_MATCH
45 struct database_criterion_value {
47 struct database_criterion_value *next;
50 struct database_criterion {
52 enum pazpar2_database_criterion_type type;
53 struct database_criterion_value *values;
54 struct database_criterion *next;
57 struct database_hosts {
62 // Create a new host structure for hostport
63 static struct host *create_host(const char *hostport)
68 host = xmalloc(sizeof(struct host));
69 host->hostport = xstrdup(hostport);
70 db_comment = strchr(host->hostport, '#');
73 host->connections = 0;
76 pazpar2_mutex_create(&host->mutex, "host");
78 yaz_cond_create(&host->cond_ready);
83 static struct host *find_host(database_hosts_t hosts,
87 yaz_mutex_enter(hosts->mutex);
88 for (p = hosts->hosts; p; p = p->next)
89 if (!strcmp(p->hostport, hostport))
93 p = create_host(hostport);
96 p->next = hosts->hosts;
100 yaz_mutex_leave(hosts->mutex);
104 int resolve_database(struct conf_service *service, struct database *db,
105 const char *hostport)
110 if (!(host = find_host(service->server->database_hosts, hostport)))
117 struct database *new_database(const char *id, NMEM nmem)
120 struct setting *idset;
122 db = nmem_malloc(nmem, sizeof(*db));
123 memset(db, 0, sizeof(*db));
125 db->url = nmem_strdup(nmem, id);
129 db->num_settings = PZ_MAX_EOF;
130 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
132 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
133 idset = nmem_malloc(nmem, sizeof(*idset));
134 idset->precedence = 0;
135 idset->name = "pz:id";
136 idset->target = idset->value = db->url;
138 db->settings[PZ_ID] = idset;
144 static struct database *load_database(const char *id,
145 struct conf_service *service)
149 db = new_database(id, service->nmem);
151 db->next = service->databases;
152 service->databases = db;
157 // Return a database structure by ID. Load and add to list if necessary
158 // new==1 just means we know it's not in the list
159 struct database *find_database(const char *id, struct conf_service *service)
162 for (p = service->databases; p; p = p->next)
163 if (!strcmp(p->url, id))
165 return load_database(id, service);
168 // This whole session_grep database thing should be moved elsewhere
170 int match_zurl(const char *zurl, const char *pattern)
174 if (!strcmp(pattern, "*"))
176 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
178 char *db = strchr(zurl, '/');
181 if (!strcmp(pattern + 2, db))
186 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
188 if (!strncmp(pattern, zurl, len))
193 else if (!strcmp(pattern, zurl))
199 // This will be generalized at some point
200 static int match_criterion(struct setting **settings,
201 struct conf_service *service,
202 struct database_criterion *c)
204 int offset = settings_lookup_offset(service, c->name);
205 struct database_criterion_value *v;
209 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
212 if (!settings[offset])
214 for (v = c->values; v; v = v->next)
216 if (c->type == PAZPAR2_STRING_MATCH)
220 if (match_zurl(settings[offset]->value, v->value))
225 if (!strcmp(settings[offset]->value, v->value))
229 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
231 if (strstr(settings[offset]->value, v->value))
241 // parses crit1=val1,crit2=val2|val3,...
242 static struct database_criterion *create_database_criterion(NMEM m,
245 struct database_criterion *res = 0;
252 nmem_strsplit(m, ",", buf, &values, &num);
253 for (i = 0; i < num; i++)
258 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
260 if ((eq = strchr(values[i], '=')))
261 new->type = PAZPAR2_STRING_MATCH;
262 else if ((eq = strchr(values[i], '~')))
263 new->type = PAZPAR2_SUBSTRING_MATCH;
266 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
270 new->name = values[i];
271 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
273 for (subi = 0; subi < subnum; subi++)
275 struct database_criterion_value *newv
276 = nmem_malloc(m, sizeof(*newv));
277 newv->value = subvalues[subi];
278 newv->next = new->values;
287 static int database_match_criteria(struct setting **settings,
288 struct conf_service *service,
289 struct database_criterion *cl)
291 for (; cl; cl = cl->next)
292 if (!match_criterion(settings, service, cl))
294 if (cl) // one of the criteria failed to match -- skip this db
300 // Cycles through databases, calling a handler function on the ones for
301 // which all criteria matched.
302 int session_grep_databases(struct session *se, const char *filter,
303 void (*fun)(struct session *se, struct session_database *db))
305 struct session_database *p;
306 NMEM nmem = nmem_create();
308 struct database_criterion *cl = create_database_criterion(nmem, filter);
310 for (p = se->databases; p; p = p->next)
312 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
314 if (!p->settings[PZ_NAME])
316 if (database_match_criteria(p->settings, se->service, cl))
326 int predef_grep_databases(void *context, struct conf_service *service,
327 void (*fun)(void *context, struct database *db))
332 for (p = service->databases; p; p = p->next)
333 if (database_match_criteria(p->settings, service, 0))
341 database_hosts_t database_hosts_create(void)
343 database_hosts_t p = xmalloc(sizeof(*p));
346 pazpar2_mutex_create(&p->mutex, "database");
350 void database_hosts_destroy(database_hosts_t *pp)
354 struct host *p = (*pp)->hosts;
357 struct host *p_next = p->next;
358 yaz_mutex_destroy(&p->mutex);
359 yaz_cond_destroy(&p->cond_ready);
364 yaz_mutex_destroy(&(*pp)->mutex);
372 * c-file-style: "Stroustrup"
373 * indent-tabs-mode: nil
375 * vim: shiftwidth=4 tabstop=8 expandtab