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 memset(db, 0, sizeof(*db));
111 db->url = nmem_strdup(nmem, id);
113 db->num_settings = PZ_MAX_EOF;
114 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
116 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
117 idset = nmem_malloc(nmem, sizeof(*idset));
118 idset->precedence = 0;
119 idset->name = "pz:id";
120 idset->target = idset->value = db->url;
122 db->settings[PZ_ID] = idset;
128 static struct database *load_database(const char *id,
129 struct conf_service *service)
133 db = new_database(id, service->nmem);
135 db->next = service->databases;
136 service->databases = db;
141 // Return a database structure by ID. Load and add to list if necessary
142 // new==1 just means we know it's not in the list
143 struct database *find_database(const char *id, struct conf_service *service)
146 for (p = service->databases; p; p = p->next)
147 if (!strcmp(p->url, id))
149 return load_database(id, service);
152 // This whole session_grep database thing should be moved elsewhere
154 int match_zurl(const char *zurl, const char *pattern)
158 if (!strcmp(pattern, "*"))
160 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
162 char *db = strchr(zurl, '/');
165 if (!strcmp(pattern + 2, db))
170 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
172 if (!strncmp(pattern, zurl, len))
177 else if (!strcmp(pattern, zurl))
183 // This will be generalized at some point
184 static int match_criterion(struct setting **settings,
185 struct conf_service *service,
186 struct database_criterion *c)
188 int offset = settings_lookup_offset(service, c->name);
189 struct database_criterion_value *v;
193 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
196 if (!settings[offset])
198 for (v = c->values; v; v = v->next)
200 if (c->type == PAZPAR2_STRING_MATCH)
204 if (match_zurl(settings[offset]->value, v->value))
209 if (!strcmp(settings[offset]->value, v->value))
213 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
215 if (strstr(settings[offset]->value, v->value))
225 // parses crit1=val1,crit2=val2|val3,...
226 static struct database_criterion *create_database_criterion(NMEM m,
229 struct database_criterion *res = 0;
236 nmem_strsplit(m, ",", buf, &values, &num);
237 for (i = 0; i < num; i++)
242 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
244 if ((eq = strchr(values[i], '=')))
245 new->type = PAZPAR2_STRING_MATCH;
246 else if ((eq = strchr(values[i], '~')))
247 new->type = PAZPAR2_SUBSTRING_MATCH;
250 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
254 new->name = values[i];
255 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
257 for (subi = 0; subi < subnum; subi++)
259 struct database_criterion_value *newv
260 = nmem_malloc(m, sizeof(*newv));
261 newv->value = subvalues[subi];
262 newv->next = new->values;
271 static int database_match_criteria(struct setting **settings,
272 struct conf_service *service,
273 struct database_criterion *cl)
275 for (; cl; cl = cl->next)
276 if (!match_criterion(settings, service, cl))
278 if (cl) // one of the criteria failed to match -- skip this db
284 // Cycles through databases, calling a handler function on the ones for
285 // which all criteria matched.
286 int session_grep_databases(struct session *se, const char *filter,
287 void (*fun)(struct session *se, struct session_database *db))
289 struct session_database *p;
290 NMEM nmem = nmem_create();
292 struct database_criterion *cl = create_database_criterion(nmem, filter);
294 for (p = se->databases; p; p = p->next)
296 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
298 if (!p->settings[PZ_NAME])
300 if (database_match_criteria(p->settings, se->service, cl))
310 int predef_grep_databases(void *context, struct conf_service *service,
311 void (*fun)(void *context, struct database *db))
316 for (p = service->databases; p; p = p->next)
317 if (database_match_criteria(p->settings, service, 0))
325 database_hosts_t database_hosts_create(void)
327 database_hosts_t p = xmalloc(sizeof(*p));
330 pazpar2_mutex_create(&p->mutex, "database");
334 void database_hosts_destroy(database_hosts_t *pp)
338 struct host *p = (*pp)->hosts;
341 struct host *p_next = p->next;
342 yaz_mutex_destroy(&p->mutex);
343 yaz_cond_destroy(&p->cond_ready);
348 yaz_mutex_destroy(&(*pp)->mutex);
356 * c-file-style: "Stroustrup"
357 * indent-tabs-mode: nil
359 * vim: shiftwidth=4 tabstop=8 expandtab