X-Git-Url: http://lists.indexdata.com/cgi-bin?a=blobdiff_plain;f=src%2Fsession.c;h=a48f69bd8037d830ad8bbc3a443ba6976ec757fd;hb=7087f7cc9945cfacfbe7341a1099eef773914772;hp=0e5427ea9950094aee04019090247f933a46004f;hpb=e07a5d5dfcf5be4cc9eb3ab91a60866ff0aee09c;p=pazpar2-moved-to-github.git diff --git a/src/session.c b/src/session.c index 0e5427e..a48f69b 100644 --- a/src/session.c +++ b/src/session.c @@ -405,12 +405,8 @@ static int prepare_map(struct session *se, struct session_database *sdb) { const char *s; - if (!sdb->settings) - { - session_log(se, YLOG_WARN, "No settings on %s", sdb->database->id); - return -1; - } - if ((s = session_setting_oneval(sdb, PZ_XSLT))) + if (sdb->settings && sdb->settings[PZ_XSLT] && !sdb->map && + (s = session_setting_oneval(sdb, PZ_XSLT))) { char auto_stylesheet[256]; @@ -438,32 +434,13 @@ static int prepare_map(struct session *se, struct session_database *sdb) } } sdb->map = normalize_cache_get(se->normalize_cache, - se->service->server->config, s); + se->service, s); if (!sdb->map) return -1; } return 0; } -// This analyzes settings and recomputes any supporting data structures -// if necessary. -static int prepare_session_database(struct session *se, - struct session_database *sdb) -{ - if (!sdb->settings) - { - session_log(se, YLOG_WARN, - "No settings associated with %s", sdb->database->id); - return -1; - } - if (sdb->settings[PZ_XSLT] && !sdb->map) - { - if (prepare_map(se, sdb) < 0) - return -1; - } - return 0; -} - // called if watch should be removed because http_channel is to be destroyed static void session_watch_cancel(void *data, struct http_channel *c, void *data2) @@ -530,26 +507,60 @@ void session_alert_watch(struct session *s, int what) static void select_targets_callback(struct session *se, struct session_database *db) { - struct client *cl = client_create(db->database->id); + struct client *cl; struct client_list *l; - client_set_database(cl, db); + for (l = se->clients_cached; l; l = l->next) + if (client_get_database(l->client) == db) + break; - client_set_session(cl, se); + if (l) + cl = l->client; + else + { + cl = client_create(db->database->id); + client_set_database(cl, db); + client_set_session(cl, se); + + l = xmalloc(sizeof(*l)); + l->client = cl; + l->next = se->clients_cached; + se->clients_cached = l; + } l = xmalloc(sizeof(*l)); l->client = cl; - l->next = se->clients; - se->clients = l; + l->next = se->clients_active; + se->clients_active = l; } -static void session_remove_clients(struct session *se) +static void session_reset_active_clients(struct session *se, + struct client_list *new_list) { struct client_list *l; session_enter(se); - l = se->clients; - se->clients = 0; + l = se->clients_active; + se->clients_active = new_list; + session_leave(se); + + while (l) + { + struct client_list *l_next = l->next; + xfree(l); + l = l_next; + } +} + +static void session_remove_cached_clients(struct session *se) +{ + struct client_list *l; + + session_reset_active_clients(se, 0); + + session_enter(se); + l = se->clients_cached; + se->clients_cached = 0; session_leave(se); while (l) @@ -578,7 +589,7 @@ int session_active_clients(struct session *s) struct client_list *l; int res = 0; - for (l = s->clients; l; l = l->next) + for (l = s->clients_active; l; l = l->next) if (client_is_active(l->client)) res++; @@ -590,7 +601,7 @@ int session_is_preferred_clients_ready(struct session *s) struct client_list *l; int res = 0; - for (l = s->clients; l; l = l->next) + for (l = s->clients_active; l; l = l->next) if (client_is_active_preferred(l->client)) res++; session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res); @@ -650,7 +661,7 @@ void session_sort(struct session *se, const char *field, int increasing) sr->next = se->sorted_results; se->sorted_results = sr; - for (l = se->clients; l; l = l->next) + for (l = se->clients_active; l; l = l->next) { struct client *cl = l->client; const char *strategy_plus_sort = get_strategy_plus_sort(cl, field); @@ -679,7 +690,7 @@ enum pazpar2_error_code session_search(struct session *se, int live_channels = 0; int no_working = 0; int no_failed = 0; - struct client_list *l; + struct client_list *l, *l0; struct timeval tval; facet_limits_t facet_limits; @@ -687,11 +698,15 @@ enum pazpar2_error_code session_search(struct session *se, *addinfo = 0; - session_remove_clients(se); + if (se->settings_modified) + session_remove_cached_clients(se); + else + session_reset_active_clients(se, 0); session_enter(se); reclist_destroy(se->reclist); se->reclist = 0; + se->settings_modified = 0; relevance_destroy(&se->relevance); nmem_reset(se->nmem); se->total_records = se->total_merged = 0; @@ -722,21 +737,27 @@ enum pazpar2_error_code session_search(struct session *se, session_leave(se); return PAZPAR2_MALFORMED_PARAMETER_VALUE; } - for (l = se->clients; l; l = l->next) + + l0 = se->clients_active; + se->clients_active = 0; + session_leave(se); + + for (l = l0; l; l = l->next) { + int parse_ret; struct client *cl = l->client; const char *strategy_plus_sort = get_strategy_plus_sort(cl, sort_field); - if (maxrecs) - client_set_maxrecs(cl, atoi(maxrecs)); - if (startrecs) - client_set_startrecs(cl, atoi(startrecs)); - if (prepare_session_database(se, client_get_database(cl)) < 0) - ; - else if (client_parse_query(cl, query, facet_limits) < 0) + if (prepare_map(se, client_get_database(cl)) < 0) + continue; + + parse_ret = client_parse_query(cl, query, facet_limits, startrecs, + maxrecs); + if (parse_ret < 0) no_failed++; - else + else if (parse_ret == 0) { + session_log(se, YLOG_LOG, "client NEW %s", client_get_id(cl)); no_working++; if (client_prep_connection(cl, se->service->z3950_operation_timeout, se->service->z3950_session_timeout, @@ -744,9 +765,22 @@ enum pazpar2_error_code session_search(struct session *se, &tval)) client_start_search(cl, strategy_plus_sort, increasing); } + else + { + session_log(se, YLOG_LOG, "client REUSE %s", client_get_id(cl)); + no_working++; + if (client_prep_connection(cl, se->service->z3950_operation_timeout, + se->service->z3950_session_timeout, + se->service->server->iochan_man, + &tval)) + { + client_reingest(cl); + } + } } facet_limits_destroy(facet_limits); - session_leave(se); + session_reset_active_clients(se, l0); + if (no_working == 0) { if (no_failed > 0) @@ -757,6 +791,7 @@ enum pazpar2_error_code session_search(struct session *se, else return PAZPAR2_NO_TARGETS; } + yaz_log(YLOG_LOG, "session_start_search done"); return PAZPAR2_NO_ERROR; } @@ -840,6 +875,8 @@ void session_apply_setting(struct session *se, char *dbname, char *setting, new->next = sdb->settings[offset]; sdb->settings[offset] = new; + se->settings_modified = 1; + // Force later recompute of settings-driven data structures // (happens when a search starts and client connections are prepared) switch (offset) @@ -853,11 +890,12 @@ void session_apply_setting(struct session *se, char *dbname, char *setting, } } -void session_destroy(struct session *se) { +void session_destroy(struct session *se) +{ struct session_database *sdb; session_log(se, YLOG_DEBUG, "Destroying"); session_use(-1); - session_remove_clients(se); + session_remove_cached_clients(se); for (sdb = se->databases; sdb; sdb = sdb->next) session_database_destroy(sdb); @@ -869,12 +907,6 @@ void session_destroy(struct session *se) { yaz_mutex_destroy(&se->session_mutex); } -/* Depreciated: use session_destroy */ -void destroy_session(struct session *se) -{ - session_destroy(se); -} - size_t session_get_memory_status(struct session *session) { size_t session_nmem; if (session == 0) @@ -905,7 +937,9 @@ struct session *new_session(NMEM nmem, struct conf_service *service, session->number_of_warnings_unknown_metadata = 0; session->num_termlists = 0; session->reclist = 0; - session->clients = 0; + session->clients_active = 0; + session->clients_cached = 0; + session->settings_modified = 0; session->session_nmem = nmem; session->nmem = nmem_create(); session->databases = 0; @@ -921,6 +955,8 @@ struct session *new_session(NMEM nmem, struct conf_service *service, return session; } +const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf); + static struct hitsbytarget *hitsbytarget_nb(struct session *se, int *count, NMEM nmem) { @@ -928,12 +964,12 @@ static struct hitsbytarget *hitsbytarget_nb(struct session *se, struct client_list *l; size_t sz = 0; - for (l = se->clients; l; l = l->next) + for (l = se->clients_active; l; l = l->next) sz++; res = nmem_malloc(nmem, sizeof(*res) * sz); *count = 0; - for (l = se->clients; l; l = l->next) + for (l = se->clients_active; l; l = l->next) { struct client *cl = l->client; WRBUF w = wrbuf_alloc(); @@ -949,6 +985,9 @@ static struct hitsbytarget *hitsbytarget_nb(struct session *se, res[*count].connected = client_get_connection(cl) ? 1 : 0; session_settings_dump(se, client_get_database(cl), w); res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w)); + wrbuf_rewind(w); + wrbuf_puts(w, ""); + res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w)); wrbuf_destroy(w); (*count)++; } @@ -1045,6 +1084,11 @@ void perform_termlist(struct http_channel *c, struct session *se, for (j = 0; j < num_names; j++) { const char *tname; + + wrbuf_puts(c->wrbuf, "wrbuf, names[j]); + wrbuf_puts(c->wrbuf, "\">\n"); + for (i = 0; i < se->num_termlists; i++) { tname = se->termlists[i].name; @@ -1056,9 +1100,6 @@ void perform_termlist(struct http_channel *c, struct session *se, if (p) { int i; - wrbuf_puts(c->wrbuf, "wrbuf, tname); - wrbuf_puts(c->wrbuf, "\">\n"); for (i = 0; i < len && i < num; i++) { // prevent sending empty term elements @@ -1075,19 +1116,15 @@ void perform_termlist(struct http_channel *c, struct session *se, p[i]->frequency); wrbuf_puts(c->wrbuf, "\n"); } - wrbuf_puts(c->wrbuf, "\n"); } } } tname = "xtargets"; if (num_names > 0 && !strcmp(names[j], tname)) { - wrbuf_puts(c->wrbuf, "wrbuf, tname); - wrbuf_puts(c->wrbuf, "\">\n"); targets_termlist_nb(c->wrbuf, se, num, c->nmem); - wrbuf_puts(c->wrbuf, "\n"); } + wrbuf_puts(c->wrbuf, "\n"); } session_leave(se); nmem_destroy(nmem_tmp); @@ -1174,7 +1211,7 @@ struct record_cluster **show_range_start(struct session *se, *total = reclist_get_num_records(se->reclist); *sumhits = 0; - for (l = se->clients; l; l = l->next) + for (l = se->clients_active; l; l = l->next) *sumhits += client_get_hits(l->client); for (i = 0; i < start; i++) @@ -1219,7 +1256,7 @@ void statistics(struct session *se, struct statistics *stat) memset(stat, 0, sizeof(*stat)); stat->num_hits = 0; - for (l = se->clients; l; l = l->next) + for (l = se->clients_active; l; l = l->next) { struct client *cl = l->client; if (!client_get_connection(cl)) @@ -1395,9 +1432,14 @@ static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no, /* generate unique key if none is not generated already or is empty */ if (wrbuf_len(norm_wr) == 0) { - wrbuf_printf(norm_wr, "%s-%d", + wrbuf_printf(norm_wr, "position: %s-%d", client_get_id(cl), record_no); } + else + { + const char *lead = "content: "; + wrbuf_insert(norm_wr, 0, lead, strlen(lead)); + } if (wrbuf_len(norm_wr) > 0) mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr)); wrbuf_destroy(norm_wr); @@ -1662,6 +1704,9 @@ static int ingest_to_cluster(struct client *cl, // merged metadata rec_md = record_metadata_init(se->nmem, (const char *) value, ser_md->type, 0); + if (!rec_md) + continue; + wheretoput = &cluster->metadata[md_field_id]; // and polulate with data: