From c9b8aa39dec714c08863e7fbb6c6a7821421f947 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 15 Oct 2009 09:59:05 +0200 Subject: [PATCH] Sessions may be added per-session. Settings system refactoring which allows settings to be defined per-session (session_database and database). --- src/database.c | 2 +- src/logic.c | 45 ++++++++------------ src/pazpar2.h | 1 + src/settings.c | 125 ++++++++++++++++++++++++++------------------------------ src/settings.h | 7 +++- 5 files changed, 80 insertions(+), 100 deletions(-) diff --git a/src/database.c b/src/database.c index fe0e1cd..3824a67 100644 --- a/src/database.c +++ b/src/database.c @@ -230,7 +230,7 @@ static int match_criterion(struct setting **settings, struct conf_service *service, struct database_criterion *c) { - int offset = settings_offset(service, c->name); + int offset = settings_lookup_offset(service, c->name); struct database_criterion_value *v; if (offset < 0) diff --git a/src/logic.c b/src/logic.c index 5399dd0..d02ced4 100644 --- a/src/logic.c +++ b/src/logic.c @@ -191,7 +191,7 @@ static void insert_settings_parameters(struct session_database *sdb, int setting; if (md->setting == Metadata_setting_parameter && - (setting = settings_offset(service, md->name)) > 0) + (setting = settings_lookup_offset(service, md->name)) >= 0) { const char *val = session_setting_oneval(sdb, setting); if (val && nparms < MAX_XSLT_ARGS) @@ -224,7 +224,7 @@ static void insert_settings_values(struct session_database *sdb, xmlDoc *doc, int offset; if (md->setting == Metadata_setting_postproc && - (offset = settings_offset(service, md->name)) > 0) + (offset = settings_lookup_offset(service, md->name)) >= 0) { const char *val = session_setting_oneval(sdb, offset); if (val) @@ -275,19 +275,20 @@ void session_settings_dump(struct session *se, { if (db->settings) { - struct conf_service *service = se->service; - int i, num = settings_num(service); + int i, num = db->num_settings; for (i = 0; i < num; i++) { struct setting *s = db->settings[i]; - for (;s; s = s->next) + for (;s ; s = s->next) { wrbuf_puts(w, "name); wrbuf_puts(w, "\" value=\""); wrbuf_xmlputs(w, s->value); - wrbuf_puts(w, "\"/>\n"); + wrbuf_puts(w, "\"/>"); } + if (db->settings[i]) + wrbuf_puts(w, "\n"); } } } @@ -557,23 +558,18 @@ enum pazpar2_error_code search(struct session *se, static void session_init_databases_fun(void *context, struct database *db) { struct session *se = (struct session *) context; - struct conf_service *service = se->service; struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new)); - int num = settings_num(service); int i; new->database = db; new->map = 0; - new->settings - = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num); - memset(new->settings, 0, sizeof(struct settings*) * num); - - if (db->settings) - { - for (i = 0; i < num; i++) - new->settings[i] = db->settings[i]; - } + assert(db->settings); + new->settings = nmem_malloc(se->session_nmem, + sizeof(struct settings *) * db->num_settings); + new->num_settings = db->num_settings; + for (i = 0; i < db->num_settings; i++) + new->settings[i] = db->settings[i]; new->next = se->databases; se->databases = new; } @@ -625,19 +621,10 @@ void session_apply_setting(struct session *se, char *dbname, char *setting, struct session_database *sdb = find_session_database(se, dbname); struct conf_service *service = se->service; struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new)); - int offset = settings_offset_cprefix(service, setting); + int offset = settings_create_offset(service, setting); - if (offset < 0) - { - yaz_log(YLOG_WARN, "Unknown setting %s", setting); - return; - } - // Jakub: This breaks the filter setting. - /*if (offset == PZ_ID) - { - yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring"); - return; - }*/ + expand_settings_array(&sdb->settings, &sdb->num_settings, offset, + se->session_nmem); new->precedence = 0; new->target = dbname; new->name = setting; diff --git a/src/pazpar2.h b/src/pazpar2.h index b84d11a..bb46b52 100644 --- a/src/pazpar2.h +++ b/src/pazpar2.h @@ -86,6 +86,7 @@ struct database_criterion { struct session_database { struct database *database; + int num_settings; struct setting **settings; normalize_record_t map; struct session_database *next; diff --git a/src/settings.c b/src/settings.c index 72a47d0..5b23e7a 100644 --- a/src/settings.c +++ b/src/settings.c @@ -88,36 +88,53 @@ int settings_num(struct conf_service *service) return service->dictionary->num; } -int settings_offset(struct conf_service *service, const char *name) +static int settings_lookup(struct conf_service *service, const char *name, + int allow_create) { + size_t maxlen; int i; + const char *p; + struct setting_dictionary *dictionary = service->dictionary; + + assert(name); - if (!name) - name = ""; - for (i = 0; i < service->dictionary->num; i++) - if (!strcmp(name, service->dictionary->dict[i])) + if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':'))) + maxlen = (p - name) + 1; + else + maxlen = strlen(name) + 1; + for (i = 0; i < dictionary->num; i++) + if (!strncmp(name, dictionary->dict[i], maxlen)) return i; - return -1; + if (!allow_create) + return -1; + if (!strncmp("pz:", name, 3)) + yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name); + if (dictionary->num + 1 > dictionary->size) + { + char **tmp = + nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*)); + memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*)); + dictionary->dict = tmp; + dictionary->size *= 2; + } + dictionary->dict[dictionary->num] = nmem_strdup(service->nmem, name); + dictionary->dict[dictionary->num][maxlen-1] = '\0'; + return dictionary->num++; } -// Ignores everything after second colon, if present -// A bit of a hack to support the pz:cclmap: scheme (and more to come?) -int settings_offset_cprefix(struct conf_service *service, const char *name) +int settings_create_offset(struct conf_service *service, const char *name) { - const char *p; - int maxlen = 100; - int i; + return settings_lookup(service, name, 1); +} - if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':'))) - maxlen = (p - name) + 1; - for (i = 0; i < service->dictionary->num; i++) - if (!strncmp(name, service->dictionary->dict[i], maxlen)) - return i; - return -1; +int settings_lookup_offset(struct conf_service *service, const char *name) +{ + return settings_lookup(service, name, 0); } char *settings_name(struct conf_service *service, int offset) { + assert(offset < service->dictionary->num); return service->dictionary->dict[offset]; } @@ -282,52 +299,29 @@ static int zurl_wildcard(const char *zurl) return SETTING_WILDCARD_NO; } -// Callback. Adds a new entry to the dictionary if necessary -// This is used in pass 1 to determine layout of dictionary -// and to load any databases mentioned -static void prepare_dictionary(struct conf_service *service, - struct setting *set) -{ - struct setting_dictionary *dictionary = service->dictionary; - - int i; - char *p; - - // Determine if we already have a dictionary entry - if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':'))) - *(p + 1) = '\0'; - for (i = 0; i < dictionary->num; i++) - if (!strcmp(dictionary->dict[i], set->name)) - return; - - if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file - { - yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name); - exit(1); - } - - // Create a new dictionary entry - // Grow dictionary if necessary - if (!dictionary->size) - dictionary->dict = - nmem_malloc(service->nmem, (dictionary->size = 50) * sizeof(char*)); - else if (dictionary->num + 1 > dictionary->size) - { - char **tmp = - nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*)); - memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*)); - dictionary->dict = tmp; - dictionary->size *= 2; - } - dictionary->dict[dictionary->num++] = nmem_strdup(service->nmem, set->name); -} - - struct update_database_context { struct setting *set; struct conf_service *service; }; +void expand_settings_array(struct setting ***set_ar, int *num, int offset, + NMEM nmem) +{ + assert(offset >= 0); + assert(*set_ar); + if (offset >= *num) + { + int i, n_num = offset + 10; + struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar)); + for (i = 0; i < *num; i++) + n_ar[i] = (*set_ar)[i]; + for (; i < n_num; i++) + n_ar[i] = 0; + *num = n_num; + *set_ar = n_ar; + } +} + // This is called from grep_databases -- adds/overrides setting for a target // This is also where the rules for precedence of settings are implemented static void update_database(void *context, struct database *db) @@ -343,8 +337,9 @@ static void update_database(void *context, struct database *db) if (!match_zurl(db->url, set->target)) return; - if ((offset = settings_offset_cprefix(service, set->name)) < 0) - return ; + offset = settings_create_offset(service, set->name); + expand_settings_array(&db->settings, &db->num_settings, offset, + service->nmem); // First we determine if this setting is overriding any existing settings // with the same name. @@ -419,18 +414,12 @@ static void initialize_soft_settings(struct conf_service *service) for (i = 0; i < service->num_metadata; i++) { - struct setting set; struct conf_metadata *md = &service->metadata[i]; if (md->setting == Metadata_setting_no) continue; - set.precedence = 0; - set.target = ""; - set.name = md->name; - set.value = ""; - set.next = 0; - prepare_dictionary(service, &set); + settings_create_offset(service, md->name); } } diff --git a/src/settings.h b/src/settings.h index eb06910..e1c575e 100644 --- a/src/settings.h +++ b/src/settings.h @@ -55,14 +55,17 @@ void settings_read_file(struct conf_service *service, const char *path, void settings_read_node(struct conf_service *service, xmlNode *n, int pass); int settings_num(struct conf_service *service); -int settings_offset(struct conf_service *service, const char *name); -int settings_offset_cprefix(struct conf_service *service, const char *name); +int settings_create_offset(struct conf_service *service, const char *name); +int settings_lookup_offset(struct conf_service *service, const char *name); void init_settings(struct conf_service *service); void resolve_databases(struct conf_service *service); void settings_read_node_x(xmlNode *n, void *client_data, void (*fun)(void *client_data, struct setting *set)); +void expand_settings_array(struct setting ***set_ar, int *num, int offset, + NMEM nmem); + #endif -- 1.7.10.4