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->id = 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->id;
120 db->settings[PZ_ID] = idset;
125 // Return a database structure by ID. Load and add to list if necessary
126 // new==1 just means we know it's not in the list
127 struct database *create_database_for_service(const char *id,
128 struct conf_service *service)
131 for (p = service->databases; p; p = p->next)
132 if (!strcmp(p->id, id))
135 p = new_database(id, service->nmem);
137 p->next = service->databases;
138 service->databases = p;
143 // This whole session_grep database thing should be moved elsewhere
145 int match_zurl(const char *zurl, const char *pattern)
149 if (!strcmp(pattern, "*"))
151 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
153 char *db = strchr(zurl, '/');
156 if (!strcmp(pattern + 2, db))
161 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
163 if (!strncmp(pattern, zurl, len))
168 else if (!strcmp(pattern, zurl))
174 // This will be generalized at some point
175 static int match_criterion(struct setting **settings,
176 struct conf_service *service,
177 struct database_criterion *c)
179 int offset = settings_lookup_offset(service, c->name);
180 struct database_criterion_value *v;
184 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
187 if (!settings[offset])
189 for (v = c->values; v; v = v->next)
191 if (c->type == PAZPAR2_STRING_MATCH)
195 if (match_zurl(settings[offset]->value, v->value))
200 if (!strcmp(settings[offset]->value, v->value))
204 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
206 if (strstr(settings[offset]->value, v->value))
216 // parses crit1=val1,crit2=val2|val3,...
217 static struct database_criterion *create_database_criterion(NMEM m,
220 struct database_criterion *res = 0;
227 nmem_strsplit(m, ",", buf, &values, &num);
228 for (i = 0; i < num; i++)
233 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
235 if ((eq = strchr(values[i], '=')))
236 new->type = PAZPAR2_STRING_MATCH;
237 else if ((eq = strchr(values[i], '~')))
238 new->type = PAZPAR2_SUBSTRING_MATCH;
241 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
245 new->name = values[i];
246 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
248 for (subi = 0; subi < subnum; subi++)
250 struct database_criterion_value *newv
251 = nmem_malloc(m, sizeof(*newv));
252 newv->value = subvalues[subi];
253 newv->next = new->values;
262 static int database_match_criteria(struct setting **settings,
263 struct conf_service *service,
264 struct database_criterion *cl)
266 for (; cl; cl = cl->next)
267 if (!match_criterion(settings, service, cl))
269 if (cl) // one of the criteria failed to match -- skip this db
275 // Cycles through databases, calling a handler function on the ones for
276 // which all criteria matched.
277 int session_grep_databases(struct session *se, const char *filter,
278 void (*fun)(struct session *se, struct session_database *db))
280 struct session_database *p;
281 NMEM nmem = nmem_create();
283 struct database_criterion *cl = create_database_criterion(nmem, filter);
285 for (p = se->databases; p; p = p->next)
287 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
289 if (!p->settings[PZ_NAME])
291 if (database_match_criteria(p->settings, se->service, cl))
301 int predef_grep_databases(void *context, struct conf_service *service,
302 void (*fun)(void *context, struct database *db))
307 for (p = service->databases; p; p = p->next)
308 if (database_match_criteria(p->settings, service, 0))
316 database_hosts_t database_hosts_create(void)
318 database_hosts_t p = xmalloc(sizeof(*p));
321 pazpar2_mutex_create(&p->mutex, "database");
325 void database_hosts_destroy(database_hosts_t *pp)
329 struct host *p = (*pp)->hosts;
332 struct host *p_next = p->next;
333 yaz_mutex_destroy(&p->mutex);
334 yaz_cond_destroy(&p->cond_ready);
339 yaz_mutex_destroy(&(*pp)->mutex);
347 * c-file-style: "Stroustrup"
348 * indent-tabs-mode: nil
350 * vim: shiftwidth=4 tabstop=8 expandtab