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 struct host *find_host(database_hosts_t hosts, const char *hostport)
86 yaz_mutex_enter(hosts->mutex);
87 for (p = hosts->hosts; p; p = p->next)
88 if (!strcmp(p->hostport, hostport))
92 p = create_host(hostport);
95 p->next = hosts->hosts;
99 yaz_mutex_leave(hosts->mutex);
103 struct database *new_database(const char *id, NMEM nmem)
106 struct setting *idset;
108 db = nmem_malloc(nmem, sizeof(*db));
109 db->url = nmem_strdup(nmem, id);
110 db->num_settings = PZ_MAX_EOF;
111 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
114 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
115 idset = nmem_malloc(nmem, sizeof(*idset));
116 idset->precedence = 0;
117 idset->name = "pz:id";
118 idset->target = idset->value = db->url;
120 db->settings[PZ_ID] = idset;
125 static struct database *load_database(const char *id,
126 struct conf_service *service)
130 db = new_database(id, service->nmem);
132 db->next = service->databases;
133 service->databases = db;
138 // Return a database structure by ID. Load and add to list if necessary
139 // new==1 just means we know it's not in the list
140 struct database *find_database(const char *id, struct conf_service *service)
143 for (p = service->databases; p; p = p->next)
144 if (!strcmp(p->url, id))
146 return load_database(id, service);
149 // This whole session_grep database thing should be moved elsewhere
151 int match_zurl(const char *zurl, const char *pattern)
155 if (!strcmp(pattern, "*"))
157 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
159 char *db = strchr(zurl, '/');
162 if (!strcmp(pattern + 2, db))
167 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
169 if (!strncmp(pattern, zurl, len))
174 else if (!strcmp(pattern, zurl))
180 // This will be generalized at some point
181 static int match_criterion(struct setting **settings,
182 struct conf_service *service,
183 struct database_criterion *c)
185 int offset = settings_lookup_offset(service, c->name);
186 struct database_criterion_value *v;
190 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
193 if (!settings[offset])
195 for (v = c->values; v; v = v->next)
197 if (c->type == PAZPAR2_STRING_MATCH)
201 if (match_zurl(settings[offset]->value, v->value))
206 if (!strcmp(settings[offset]->value, v->value))
210 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
212 if (strstr(settings[offset]->value, v->value))
222 // parses crit1=val1,crit2=val2|val3,...
223 static struct database_criterion *create_database_criterion(NMEM m,
226 struct database_criterion *res = 0;
233 nmem_strsplit(m, ",", buf, &values, &num);
234 for (i = 0; i < num; i++)
239 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
241 if ((eq = strchr(values[i], '=')))
242 new->type = PAZPAR2_STRING_MATCH;
243 else if ((eq = strchr(values[i], '~')))
244 new->type = PAZPAR2_SUBSTRING_MATCH;
247 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
251 new->name = values[i];
252 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
254 for (subi = 0; subi < subnum; subi++)
256 struct database_criterion_value *newv
257 = nmem_malloc(m, sizeof(*newv));
258 newv->value = subvalues[subi];
259 newv->next = new->values;
268 static int database_match_criteria(struct setting **settings,
269 struct conf_service *service,
270 struct database_criterion *cl)
272 for (; cl; cl = cl->next)
273 if (!match_criterion(settings, service, cl))
275 if (cl) // one of the criteria failed to match -- skip this db
281 // Cycles through databases, calling a handler function on the ones for
282 // which all criteria matched.
283 int session_grep_databases(struct session *se, const char *filter,
284 void (*fun)(struct session *se, struct session_database *db))
286 struct session_database *p;
287 NMEM nmem = nmem_create();
289 struct database_criterion *cl = create_database_criterion(nmem, filter);
291 for (p = se->databases; p; p = p->next)
293 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
295 if (!p->settings[PZ_NAME])
297 if (database_match_criteria(p->settings, se->service, cl))
307 int predef_grep_databases(void *context, struct conf_service *service,
308 void (*fun)(void *context, struct database *db))
313 for (p = service->databases; p; p = p->next)
314 if (database_match_criteria(p->settings, service, 0))
322 database_hosts_t database_hosts_create(void)
324 database_hosts_t p = xmalloc(sizeof(*p));
327 pazpar2_mutex_create(&p->mutex, "database");
331 void database_hosts_destroy(database_hosts_t *pp)
335 struct host *p = (*pp)->hosts;
338 struct host *p_next = p->next;
339 yaz_mutex_destroy(&p->mutex);
340 yaz_cond_destroy(&p->cond_ready);
345 yaz_mutex_destroy(&(*pp)->mutex);
353 * c-file-style: "Stroustrup"
354 * indent-tabs-mode: nil
356 * vim: shiftwidth=4 tabstop=8 expandtab