From 9856b661bf3d99be5b397040d3163e769a3e360b Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Fri, 20 Apr 2007 16:21:19 +0000 Subject: [PATCH] Made load of XSLT normalization stylesheet dynamic --- src/database.c | 40 +--------------------------------------- src/logic.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- src/pazpar2.c | 3 +-- src/pazpar2.h | 40 +++++++++++++++++++--------------------- 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/src/database.c b/src/database.c index 51713e6..c0c02df 100644 --- a/src/database.c +++ b/src/database.c @@ -1,4 +1,4 @@ -/* $Id: database.c,v 1.21 2007-04-20 15:36:48 quinn Exp $ +/* $Id: database.c,v 1.22 2007-04-20 16:21:19 quinn Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -165,7 +165,6 @@ static struct database *load_database(const char *id) db->explain = explain; db->settings = 0; db->next = databases; - db->map = 0; databases = db; return db; @@ -286,43 +285,6 @@ int grep_databases(void *context, struct database_criterion *cl, return i; } -// Prepare XSLT stylesheets for record normalization -static void prepare_map(void *ignore, struct database *db) -{ - struct setting *s; - - if (!db->settings) - return; - for (s = db->settings[PZ_XSLT]; s; s = s->next) - { - char **stylesheets; - struct database_retrievalmap **m = &db->map; - int num, i; - - nmem_strsplit(nmem, ",", s->value, &stylesheets, &num); - for (i = 0; i < num; i++) - { - (*m) = nmem_malloc(nmem, sizeof(**m)); - (*m)->next = 0; - if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i]))) - { - yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s", - stylesheets[i]); - exit(1); - } - m = &(*m)->next; - } - } - if (!db->map) - yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s", db->url); -} - -// Read settings for each database, and prepare support data structures -void prepare_databases(void) -{ - grep_databases(0, 0, prepare_map); -} - // This function will most likely vanish when a proper target profile mechanism is // introduced. void load_simpletargets(const char *fn) diff --git a/src/logic.c b/src/logic.c index aae6380..fa1fe6b 100644 --- a/src/logic.c +++ b/src/logic.c @@ -1,4 +1,4 @@ -/* $Id: logic.c,v 1.13 2007-04-20 15:36:48 quinn Exp $ +/* $Id: logic.c,v 1.14 2007-04-20 16:21:19 quinn Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -19,6 +19,9 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +// This file contains the primary business logic. Several parts of it should +// Eventually be factored into separate modules. + #include #include #include @@ -573,7 +576,7 @@ static xmlDoc *normalize_record(struct client *cl, Z_External *rec) #endif } - for (m = db->map; m; m = m->next){ + for (m = sdb->map; m; m = m->next){ xmlDoc *new = 0; #if 1 @@ -1283,13 +1286,53 @@ static int prepare_yazmarc(struct session_database *sdb) return 0; } +// Prepare XSLT stylesheets for record normalization +// Structures are allocated on the session_wide nmem to avoid having +// to recompute this for every search. This would lead +// to leaking if a single session was to repeatedly change the PZ_XSLT +// setting. However, this is not a realistic use scenario. +static int prepare_map(struct session *se, struct session_database *sdb) +{ + struct setting *s; + + if (!sdb->settings) + { + yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url); + return -1; + } + if ((s = sdb->settings[PZ_XSLT])) + { + char **stylesheets; + struct database_retrievalmap **m = &sdb->map; + int num, i; + + nmem_strsplit(se->session_nmem, ",", s->value, &stylesheets, &num); + for (i = 0; i < num; i++) + { + (*m) = nmem_malloc(se->session_nmem, sizeof(**m)); + (*m)->next = 0; + if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i]))) + { + yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s", + stylesheets[i]); + return -1; + } + m = &(*m)->next; + } + } + if (!sdb->map) + yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s", + sdb->database->url); + return 0; +} + // This analyzes settings and recomputes any supporting data structures // if necessary. -static int prepare_session_database(struct session_database *sdb) +static int prepare_session_database(struct session *se, struct session_database *sdb) { if (!sdb->settings) { - yaz_log(YLOG_WARN, "No settings associates with %s", sdb->database->url); + yaz_log(YLOG_WARN, "No settings associated with %s", sdb->database->url); return -1; } if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc) @@ -1299,6 +1342,8 @@ static int prepare_session_database(struct session_database *sdb) } if (sdb->settings[PZ_XSLT] && !sdb->map) { + if (prepare_map(se, sdb) < 0) + return -1; } return 0; } @@ -1529,7 +1574,7 @@ char *search(struct session *se, char *query, char *filter) for (cl = se->clients; cl; cl = cl->next) { - if (prepare_session_database(cl->database) < 0) + if (prepare_session_database(se, cl->database) < 0) return "CONFIG_ERROR"; if (client_parse_query(cl, query) < 0) // Query must parse for all targets return "QUERY"; diff --git a/src/pazpar2.c b/src/pazpar2.c index 7ded17a..d1ecf15 100644 --- a/src/pazpar2.c +++ b/src/pazpar2.c @@ -1,4 +1,4 @@ -/* $Id: pazpar2.c,v 1.80 2007-04-19 16:07:20 adam Exp $ +/* $Id: pazpar2.c,v 1.81 2007-04-20 16:21:19 quinn Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -95,7 +95,6 @@ int main(int argc, char **argv) settings_read(global_parameters.server->settings); else yaz_log(YLOG_WARN, "No settings-directory specified. Problems may well ensue!"); - prepare_databases(); global_parameters.odr_in = odr_createmem(ODR_DECODE); global_parameters.odr_out = odr_createmem(ODR_ENCODE); diff --git a/src/pazpar2.h b/src/pazpar2.h index a7047c7..c962c47 100644 --- a/src/pazpar2.h +++ b/src/pazpar2.h @@ -1,4 +1,4 @@ -/* $Id: pazpar2.h,v 1.30 2007-04-20 15:36:48 quinn Exp $ +/* $Id: pazpar2.h,v 1.31 2007-04-20 16:21:19 quinn Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -61,15 +61,6 @@ struct database { struct zr_explain *explain; struct setting **settings; struct database *next; - //yaz_marc_t yaz_marc; - struct database_retrievalmap *map; -}; - -// Normalization filter. Turns incoming record into internal representation -// Simple sequence of stylesheets run in series. -struct database_retrievalmap { - xsltStylesheet *stylesheet; - struct database_retrievalmap *next; }; struct database_criterion_value { @@ -126,17 +117,11 @@ struct client { struct client *next; }; -#define SESSION_WATCH_RECORDS 0 -#define SESSION_WATCH_MAX 0 - -#define SESSION_MAX_TERMLISTS 10 - -typedef void (*session_watchfun)(void *data); - -struct named_termlist -{ - char *name; - struct termlist *termlist; +// Normalization filter. Turns incoming record into internal representation +// Simple sequence of stylesheets run in series. +struct database_retrievalmap { + xsltStylesheet *stylesheet; + struct database_retrievalmap *next; }; // Represents a database as viewed from one session, possibly with settings overriden @@ -150,6 +135,19 @@ struct session_database struct session_database *next; }; +#define SESSION_WATCH_RECORDS 0 +#define SESSION_WATCH_MAX 0 + +#define SESSION_MAX_TERMLISTS 10 + +typedef void (*session_watchfun)(void *data); + +struct named_termlist +{ + char *name; + struct termlist *termlist; +}; + // End-user session struct session { struct session_database *databases; // All databases, settings overriden -- 1.7.10.4