pazpar2  1.14.1
database.c
Go to the documentation of this file.
1 /* This file is part of Pazpar2.
2  Copyright (C) Index Data
3 
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
7 version.
8 
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
12 for more details.
13 
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
17 
18 */
19 
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <assert.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <yaz/log.h>
28 #include <yaz/nmem.h>
29 
30 #include "ppmutex.h"
31 #include "session.h"
32 #include "pazpar2_config.h"
33 #include "settings.h"
34 #include "http.h"
35 #include "database.h"
36 
37 #include <sys/types.h>
38 
42 };
43 
45  char *value;
47 };
48 
51  char *name;
55 };
56 
57 struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings)
58 {
59  struct database *db;
60  struct setting *idset;
61 
62  db = nmem_malloc(nmem, sizeof(*db));
63  db->id = nmem_strdup(nmem, id);
64  db->next = 0;
65 
66  if (service_settings && service_settings->num_settings > 0) {
67  yaz_log(YLOG_DEBUG, "copying settings from service to database %s settings", db->id);
68  db->num_settings = service_settings->num_settings;
69  db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings);
70  // Initialize database settings with service settings
71  memcpy(db->settings, service_settings->settings, sizeof(*db->settings) * db->num_settings);
72  }
73  else {
74  yaz_log(YLOG_DEBUG, "No service settings to database %s ", db->id);
76  db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings);
77  memset(db->settings, 0, sizeof(*db->settings) * db->num_settings);
78  }
79  idset = nmem_malloc(nmem, sizeof(*idset));
80  idset->precedence = 0;
81  idset->name = "pz:id";
82  idset->target = idset->value = db->id;
83  idset->next = db->settings[PZ_ID];
84  db->settings[PZ_ID] = idset;
85 
86  return db;
87 }
88 
89 // Return a database structure by ID. Load and add to list if necessary
90 // new==1 just means we know it's not in the list
91 struct database *create_database_for_service(const char *id,
92  struct conf_service *service)
93 {
94  struct database *p;
95  for (p = service->databases; p; p = p->next)
96  if (!strcmp(p->id, id))
97  return p;
98 
99  yaz_log(YLOG_DEBUG, "new database %s under service %s", id,
100  service->id ? service->id : "null");
101  p = new_database_inherit_settings(id, service->nmem, service->settings);
102  p->next = service->databases;
103  service->databases = p;
104 
105  return p;
106 }
107 
108 // This whole session_grep database thing should be moved elsewhere
109 
110 int match_zurl(const char *zurl, const char *pattern)
111 {
112  int len;
113 
114  if (!strcmp(pattern, "*"))
115  return 1;
116  else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
117  {
118  char *db = strchr(zurl, '/');
119  if (!db)
120  return 0;
121  if (!strcmp(pattern + 2, db))
122  return 1;
123  else
124  return 0;
125  }
126  else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
127  {
128  if (!strncmp(pattern, zurl, len))
129  return 1;
130  else
131  return 0;
132  }
133  else if (!strcmp(pattern, zurl))
134  return 1;
135  else
136  return 0;
137 }
138 
139 // This will be generalized at some point
140 static int match_criterion(struct setting **settings,
141  int num_settings,
142  struct conf_service *service,
143  struct database_criterion *c)
144 {
145  int offset = settings_lookup_offset(service, c->name);
146  struct database_criterion_value *v;
147 
148  if (offset < 0 || offset >= num_settings)
149  {
150  yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
151  return 0;
152  }
153  if (!settings[offset])
154  return 0;
155  for (v = c->values; v; v = v->next)
156  {
157  if (c->type == PAZPAR2_STRING_MATCH)
158  {
159  if (offset == PZ_ID)
160  {
161  if (match_zurl(settings[offset]->value, v->value))
162  break;
163  }
164  else
165  {
166  if (!strcmp(settings[offset]->value, v->value))
167  break;
168  }
169  }
170  else if (c->type == PAZPAR2_SUBSTRING_MATCH)
171  {
172  if (strstr(settings[offset]->value, v->value))
173  break;
174  }
175  }
176  if (v)
177  return 1;
178  else
179  return 0;
180 }
181 
182 // parses crit1=val1,crit2=val2|val3,...
184  const char *buf)
185 {
186  struct database_criterion *res = 0;
187  char **values;
188  int num;
189  int i;
190  int union_filter = 0;
191 
192  if (!buf || !*buf)
193  return 0;
194  if (*buf == '|')
195  {
196  union_filter = 1;
197  buf++;
198  }
199  nmem_strsplit(m, ",", buf, &values, &num);
200  for (i = 0; i < num; i++)
201  {
202  char **subvalues;
203  int subnum;
204  int subi;
205  struct database_criterion *new = nmem_malloc(m, sizeof(*new));
206  char *eq;
207 
208  new->union_filter = union_filter;
209  for (eq = values[i]; *eq; eq++)
210  if (*eq == '=')
211  {
212  new->type = PAZPAR2_STRING_MATCH;
213  break;
214  }
215  else if (*eq == '~')
216  {
217  new->type = PAZPAR2_SUBSTRING_MATCH;
218  break;
219  }
220  if (!*eq)
221  {
222  yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
223  return 0;
224  }
225  *(eq++) = '\0';
226  new->name = values[i];
227  nmem_strsplit(m, "|", eq, &subvalues, &subnum);
228  new->values = 0;
229  for (subi = 0; subi < subnum; subi++)
230  {
231  struct database_criterion_value *newv
232  = nmem_malloc(m, sizeof(*newv));
233  newv->value = subvalues[subi];
234  newv->next = new->values;
235  new->values = newv;
236  }
237  new->next = res;
238  res = new;
239  }
240  return res;
241 }
242 
243 static int database_match_criteria(struct setting **settings,
244  int num_settings,
245  struct conf_service *service,
246  struct database_criterion *cl)
247 {
248  if (cl && cl->union_filter)
249  {
250  for (; cl; cl = cl->next)
251  if (match_criterion(settings, num_settings, service, cl))
252  return 1;
253  return 0;
254  }
255  else
256  {
257  for (; cl; cl = cl->next)
258  if (!match_criterion(settings, num_settings, service, cl))
259  return 0;
260  }
261  return 1;
262 }
263 
264 // Cycles through databases, calling a handler function on the ones for
265 // which all criteria matched.
266 int session_grep_databases(struct session *se, const char *filter,
267  void (*fun)(struct session *se, struct session_database *db))
268 {
269  struct session_database *p;
270  NMEM nmem = nmem_create();
271  int i = 0;
272  struct database_criterion *cl = create_database_criterion(nmem, filter);
273 
274  for (p = se->databases; p; p = p->next)
275  {
276  if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
277  continue;
278  if (!p->settings[PZ_NAME])
279  continue;
281  se->service, cl))
282  {
283  (*fun)(se, p);
284  i++;
285  }
286  }
287  nmem_destroy(nmem);
288  return i;
289 }
290 
291 int predef_grep_databases(void *context, struct conf_service *service,
292  void (*fun)(void *context, struct database *db))
293 {
294  struct database *p;
295  int i = 0;
296 
297  for (p = service->databases; p; p = p->next)
298  if (database_match_criteria(p->settings, p->num_settings, service, 0))
299  {
300  (*fun)(context, p);
301  i++;
302  }
303  return i;
304 }
305 
306 /*
307  * Local variables:
308  * c-basic-offset: 4
309  * c-file-style: "Stroustrup"
310  * indent-tabs-mode: nil
311  * End:
312  * vim: shiftwidth=4 tabstop=8 expandtab
313  */
314 
pazpar2_database_criterion_type
Definition: database.c:39
@ PAZPAR2_SUBSTRING_MATCH
Definition: database.c:41
@ PAZPAR2_STRING_MATCH
Definition: database.c:40
int session_grep_databases(struct session *se, const char *filter, void(*fun)(struct session *se, struct session_database *db))
Definition: database.c:266
static int match_criterion(struct setting **settings, int num_settings, struct conf_service *service, struct database_criterion *c)
Definition: database.c:140
static struct database_criterion * create_database_criterion(NMEM m, const char *buf)
Definition: database.c:183
struct database * create_database_for_service(const char *id, struct conf_service *service)
Definition: database.c:91
int predef_grep_databases(void *context, struct conf_service *service, void(*fun)(void *context, struct database *db))
Definition: database.c:291
int match_zurl(const char *zurl, const char *pattern)
Definition: database.c:110
struct database * new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings)
Definition: database.c:57
static int database_match_criteria(struct setting **settings, int num_settings, struct conf_service *service, struct database_criterion *cl)
Definition: database.c:243
void(* fun)(struct http_channel *c)
control MUTEX debugging
int settings_lookup_offset(struct conf_service *service, const char *name)
Definition: settings.c:150
#define PZ_ID
Definition: settings.h:32
#define PZ_ALLOW
Definition: settings.h:30
#define PZ_NAME
Definition: settings.h:33
#define PZ_MAX_EOF
Definition: settings.h:63
struct database * databases
struct settings_array * settings
struct database_criterion_value * next
Definition: database.c:46
struct database_criterion * next
Definition: database.c:54
struct database_criterion_value * values
Definition: database.c:53
enum pazpar2_database_criterion_type type
Definition: database.c:52
char * id
Definition: settings.h:76
struct database * next
Definition: settings.h:79
int num_settings
Definition: settings.h:77
struct setting ** settings
Definition: settings.h:78
struct session_database * next
Definition: session.h:64
struct setting ** settings
Definition: session.h:62
int num_settings
Definition: session.h:61
struct session_database * databases
Definition: session.h:94
struct conf_service * service
Definition: session.h:93
const char * name
Definition: settings.h:69
const char * value
Definition: settings.h:70
const char * target
Definition: settings.h:68
struct setting * next
Definition: settings.h:71
int precedence
Definition: settings.h:67
struct setting ** settings
Definition: settings.h:85
int num_settings
Definition: settings.h:86