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