1 /* This file is part of Pazpar2.
2 Copyright (C) Index Data
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
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
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
21 \brief high-level logic; mostly user sessions and settings
46 #include <yaz/marcdisp.h>
47 #include <yaz/comstack.h>
48 #include <yaz/tcpip.h>
49 #include <yaz/proto.h>
50 #include <yaz/readconf.h>
51 #include <yaz/pquery.h>
52 #include <yaz/otherinfo.h>
53 #include <yaz/yaz-util.h>
55 #include <yaz/query-charset.h>
56 #include <yaz/querytowrbuf.h>
57 #include <yaz/oid_db.h>
58 #include <yaz/snprintf.h>
59 #include <yaz/xml_get.h>
63 #include <yaz/timing.h>
67 #include "parameters.h"
71 #include "termlists.h"
73 #include "relevance.h"
77 #include "normalize7bit.h"
79 #include <libxml/tree.h>
83 #define MAX(a,b) ((a)>(b)?(a):(b))
85 // Note: Some things in this structure will eventually move to configuration
86 struct parameters global_parameters =
90 0, // predictable sessions
94 struct client *client;
95 struct client_list *next;
98 /* session counting (1) , disable client counting (0) */
99 static YAZ_MUTEX g_session_mutex = 0;
100 static int no_sessions = 0;
102 static int session_use(int delta)
105 if (!g_session_mutex)
106 yaz_mutex_create(&g_session_mutex);
107 yaz_mutex_enter(g_session_mutex);
108 no_sessions += delta;
109 sessions = no_sessions;
110 yaz_mutex_leave(g_session_mutex);
111 yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" :
112 (delta > 0 ? "INC" : "DEC"), no_sessions);
116 int sessions_count(void)
118 return session_use(0);
121 static void log_xml_doc(xmlDoc *doc)
123 FILE *lf = yaz_log_file();
126 #if LIBXML_VERSION >= 20600
127 xmlDocDumpFormatMemory(doc, &result, &len, 1);
129 xmlDocDumpMemory(doc, &result, &len);
133 (void) fwrite(result, 1, len, lf);
139 static void session_enter(struct session *s, const char *caller)
142 session_log(s, YLOG_DEBUG, "Session lock by %s", caller);
143 yaz_mutex_enter(s->session_mutex);
146 static void session_leave(struct session *s, const char *caller)
148 yaz_mutex_leave(s->session_mutex);
150 session_log(s, YLOG_DEBUG, "Session unlock by %s", caller);
153 static int run_icu(struct session *s, const char *icu_chain_id,
155 WRBUF norm_wr, WRBUF disp_wr)
157 const char *facet_component;
158 struct conf_service *service = s->service;
159 pp2_charset_token_t prt =
160 pp2_charset_token_create(service->charsets, icu_chain_id);
163 session_log(s, YLOG_FATAL,
164 "Unknown ICU chain '%s'", icu_chain_id);
167 pp2_charset_token_first(prt, value, 0);
168 while ((facet_component = pp2_charset_token_next(prt)))
170 const char *display_component;
171 if (*facet_component)
173 if (wrbuf_len(norm_wr))
174 wrbuf_puts(norm_wr, " ");
175 wrbuf_puts(norm_wr, facet_component);
177 display_component = pp2_get_display(prt);
178 if (display_component)
180 if (wrbuf_len(disp_wr))
181 wrbuf_puts(disp_wr, " ");
182 wrbuf_puts(disp_wr, display_component);
185 pp2_charset_token_destroy(prt);
189 static void session_normalize_facet(struct session *s,
190 const char *type, const char *value,
191 WRBUF display_wrbuf, WRBUF facet_wrbuf)
193 struct conf_service *service = s->service;
195 const char *icu_chain_id = 0;
197 for (i = 0; i < service->num_metadata; i++)
198 if (!strcmp((service->metadata + i)->name, type))
199 icu_chain_id = (service->metadata + i)->facetrule;
201 icu_chain_id = "facet";
203 run_icu(s, icu_chain_id, value, facet_wrbuf, display_wrbuf);
206 void add_facet(struct session *s, const char *type, const char *value, int count)
208 WRBUF facet_wrbuf = wrbuf_alloc();
209 WRBUF display_wrbuf = wrbuf_alloc();
211 session_normalize_facet(s, type, value, display_wrbuf, facet_wrbuf);
213 if (wrbuf_len(facet_wrbuf))
215 struct named_termlist **tp = &s->termlists;
216 for (; (*tp); tp = &(*tp)->next)
217 if (!strcmp((*tp)->name, type))
221 *tp = nmem_malloc(s->nmem, sizeof(**tp));
222 (*tp)->name = nmem_strdup(s->nmem, type);
223 (*tp)->termlist = termlist_create(s->nmem);
226 termlist_insert((*tp)->termlist, wrbuf_cstr(display_wrbuf),
227 wrbuf_cstr(facet_wrbuf), count);
229 wrbuf_destroy(facet_wrbuf);
230 wrbuf_destroy(display_wrbuf);
233 static xmlDoc *record_to_xml(struct session *se,
234 struct session_database *sdb, const char *rec)
236 struct database *db = sdb->database;
239 rdoc = xmlParseMemory(rec, strlen(rec));
243 session_log(se, YLOG_WARN, "Non-wellformed XML");
247 if (global_parameters.dump_records)
249 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->id);
256 #define MAX_XSLT_ARGS 16
258 // Add static values from session database settings if applicable
259 static void insert_settings_parameters(struct session_database *sdb,
260 struct conf_service *service,
268 for (i = 0; i < service->num_metadata; i++)
270 struct conf_metadata *md = &service->metadata[i];
273 if (md->setting == Metadata_setting_parameter &&
274 (setting = settings_lookup_offset(service, md->name)) >= 0)
276 const char *val = session_setting_oneval(sdb, setting);
277 if (val && nparms < MAX_XSLT_ARGS)
280 int len = strlen(val);
281 buf = nmem_malloc(nmem, len + 3);
283 strcpy(buf + 1, val);
286 parms[offset++] = md->name;
287 parms[offset++] = buf;
295 // Add static values from session database settings if applicable
296 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
298 struct conf_service *service)
302 for (i = 0; i < service->num_metadata; i++)
304 struct conf_metadata *md = &service->metadata[i];
307 if (md->setting == Metadata_setting_postproc &&
308 (offset = settings_lookup_offset(service, md->name)) >= 0)
310 const char *val = session_setting_oneval(sdb, offset);
313 xmlNode *n = xmlNewTextChild(root, 0, (xmlChar *) "metadata",
315 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
321 static xmlDoc *normalize_record(struct session *se,
322 struct session_database *sdb,
323 struct conf_service *service,
324 const char *rec, NMEM nmem)
326 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
330 char *parms[MAX_XSLT_ARGS*2+1];
332 insert_settings_parameters(sdb, service, parms, nmem);
334 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
336 session_log(se, YLOG_WARN, "Normalize failed");
342 void session_settings_dump(struct session *se,
343 struct session_database *db,
348 int i, num = db->num_settings;
349 for (i = 0; i < num; i++)
351 struct setting *s = db->settings[i];
352 for (;s ; s = s->next)
354 wrbuf_puts(w, "<set name=\"");
355 wrbuf_xmlputs(w, s->name);
356 wrbuf_puts(w, "\" value=\"");
357 wrbuf_xmlputs(w, s->value);
358 wrbuf_puts(w, "\"/>");
366 // Retrieve first defined value for 'name' for given database.
367 // Will be extended to take into account user associated with session
368 const char *session_setting_oneval(struct session_database *db, int offset)
370 if (offset >= db->num_settings || !db->settings[offset])
372 return db->settings[offset]->value;
375 // Prepare XSLT stylesheets for record normalization
376 // Structures are allocated on the session_wide nmem to avoid having
377 // to recompute this for every search. This would lead
378 // to leaking if a single session was to repeatedly change the PZ_XSLT
379 // setting. However, this is not a realistic use scenario.
380 static int prepare_map(struct session *se, struct session_database *sdb)
382 if (sdb->settings && !sdb->map)
386 if (sdb->settings[PZ_XSLT] &&
387 (s = session_setting_oneval(sdb, PZ_XSLT)))
389 char auto_stylesheet[256];
391 if (!strcmp(s, "auto"))
393 const char *request_syntax = session_setting_oneval(
394 sdb, PZ_REQUESTSYNTAX);
398 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
399 "%s.xsl", request_syntax);
400 for (cp = auto_stylesheet; *cp; cp++)
402 /* deliberately only consider ASCII */
403 if (*cp > 32 && *cp < 127)
410 session_log(se, YLOG_WARN,
411 "No pz:requestsyntax for auto stylesheet");
414 sdb->map = normalize_cache_get(se->normalize_cache,
423 // called if watch should be removed because http_channel is to be destroyed
424 static void session_watch_cancel(void *data, struct http_channel *c,
427 struct session_watchentry *ent = data;
434 // set watch. Returns 0=OK, -1 if watch is already set
435 int session_set_watch(struct session *s, int what,
436 session_watchfun fun, void *data,
437 struct http_channel *chan)
440 session_enter(s, "session_set_watch");
441 if (s->watchlist[what].fun)
446 s->watchlist[what].fun = fun;
447 s->watchlist[what].data = data;
448 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
449 session_watch_cancel);
452 session_leave(s, "session_set_watch");
456 void session_alert_watch(struct session *s, int what)
459 session_enter(s, "session_alert_watch");
460 if (s->watchlist[what].fun)
462 /* our watch is no longer associated with http_channel */
464 session_watchfun fun;
466 http_remove_observer(s->watchlist[what].obs);
467 fun = s->watchlist[what].fun;
468 data = s->watchlist[what].data;
470 /* reset watch before fun is invoked - in case fun wants to set
472 s->watchlist[what].fun = 0;
473 s->watchlist[what].data = 0;
474 s->watchlist[what].obs = 0;
476 session_leave(s, "session_alert_watch");
477 session_log(s, YLOG_DEBUG,
478 "Alert Watch: %d calling function: %p", what, fun);
482 session_leave(s,"session_alert_watch");
485 //callback for grep_databases
486 static void select_targets_callback(struct session *se,
487 struct session_database *db)
490 struct client_list *l;
492 for (l = se->clients_cached; l; l = l->next)
493 if (client_get_database(l->client) == db)
500 cl = client_create(db->database->id);
501 client_set_database(cl, db);
503 l = xmalloc(sizeof(*l));
505 l->next = se->clients_cached;
506 se->clients_cached = l;
508 /* set session always. If may be 0 if client is not active */
509 client_set_session(cl, se);
511 l = xmalloc(sizeof(*l));
513 l->next = se->clients_active;
514 se->clients_active = l;
517 static void session_reset_active_clients(struct session *se,
518 struct client_list *new_list)
520 struct client_list *l;
522 session_enter(se, "session_reset_active_clients");
523 l = se->clients_active;
524 se->clients_active = new_list;
525 session_leave(se, "session_reset_active_clients");
529 struct client_list *l_next = l->next;
531 client_lock(l->client);
532 client_set_session(l->client, 0); /* mark client inactive */
533 client_unlock(l->client);
540 static void session_remove_cached_clients(struct session *se)
542 struct client_list *l;
544 session_reset_active_clients(se, 0);
546 session_enter(se, "session_remove_cached_clients");
547 l = se->clients_cached;
548 se->clients_cached = 0;
549 session_leave(se, "session_remove_cached_clients");
553 struct client_list *l_next = l->next;
554 client_lock(l->client);
555 client_set_session(l->client, 0);
556 client_set_database(l->client, 0);
557 client_unlock(l->client);
558 client_destroy(l->client);
564 // Associates a set of clients with a session;
565 // Note: Session-databases represent databases with per-session
567 static int select_targets(struct session *se, const char *filter)
569 return session_grep_databases(se, filter, select_targets_callback);
572 int session_active_clients(struct session *s)
574 struct client_list *l;
577 for (l = s->clients_active; l; l = l->next)
578 if (client_is_active(l->client))
584 int session_is_preferred_clients_ready(struct session *s)
586 struct client_list *l;
589 for (l = s->clients_active; l; l = l->next)
590 if (client_is_active_preferred(l->client))
592 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
596 static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
598 reclist_destroy(se->reclist);
599 if (nmem_total(se->nmem))
600 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd",
601 nmem_total(se->nmem));
602 nmem_reset(se->nmem);
603 se->total_records = se->total_merged = 0;
605 relevance_clear(se->relevance);
607 /* reset list of sorted results and clear to relevance search */
608 se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
609 se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
610 se->sorted_results->increasing = sp->increasing;
611 se->sorted_results->type = sp->type;
612 se->sorted_results->next = 0;
614 session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
615 sp->name, sp->increasing, sp->type);
617 se->reclist = reclist_create(se->nmem);
620 void session_sort(struct session *se, struct reclist_sortparms *sp,
621 const char *mergekey, const char *rank)
623 struct client_list *l;
624 const char *field = sp->name;
625 int increasing = sp->increasing;
627 int clients_research = 0;
629 session_enter(se, "session_sort");
630 session_log(se, YLOG_DEBUG, "session_sort field=%s increasing=%d type=%d",
631 field, increasing, type);
633 if (rank && (!se->rank || strcmp(se->rank, rank)))
635 /* new rank must research/reingest anyway */
638 se->rank = *rank ? xstrdup(rank) : 0;
639 clients_research = 1;
640 session_log(se, YLOG_DEBUG, "session_sort: new rank = %s",
643 if (mergekey && (!se->mergekey || strcmp(se->mergekey, mergekey)))
645 /* new mergekey must research/reingest anyway */
648 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
649 clients_research = 1;
650 session_log(se, YLOG_DEBUG, "session_sort: new mergekey = %s",
653 if (clients_research == 0)
655 struct reclist_sortparms *sr;
656 for (sr = se->sorted_results; sr; sr = sr->next)
657 if (!reclist_sortparms_cmp(sr, sp))
661 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d already fetched",
662 field, increasing, type);
663 session_leave(se, "session_sort");
667 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d must fetch",
668 field, increasing, type);
670 // We need to reset reclist on every sort that changes the records, not just for position
671 // So if just one client requires new searching, we need to clear set.
672 // Ask each of the client if sorting requires re-search due to native sort
673 // If it does it will require us to
674 for (l = se->clients_active; l; l = l->next)
676 struct client *cl = l->client;
677 // Assume no re-search is required.
678 client_parse_init(cl, 1);
679 clients_research += client_parse_sort(cl, sp);
681 if (!clients_research || se->clients_starting)
683 // A new sorting based on same record set
684 struct reclist_sortparms *sr = nmem_malloc(se->nmem, sizeof(*sr));
685 sr->name = nmem_strdup(se->nmem, field);
686 sr->increasing = increasing;
688 sr->next = se->sorted_results;
689 se->sorted_results = sr;
690 session_log(se, YLOG_DEBUG, "session_sort: no research/ingesting done");
691 session_leave(se, "session_sort");
695 se->clients_starting = 1;
696 session_log(se, YLOG_DEBUG,
697 "session_sort: reset results due to %d clients researching",
699 session_clear_set(se, sp);
700 session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
702 session_leave(se, "session_sort");
703 for (l = se->clients_active; l; l = l->next)
705 struct client *cl = l->client;
706 if (client_get_state(cl) == Client_Connecting ||
707 client_get_state(cl) == Client_Idle ||
708 client_get_state(cl) == Client_Working) {
709 client_start_search(cl);
713 session_log(se, YLOG_DEBUG,
714 "session_sort: %s: No re-start/ingest in show. "
715 "Wrong client state: %d",
716 client_get_id(cl), client_get_state(cl));
719 session_enter(se, "session_sort");
720 se->clients_starting = 0;
721 session_leave(se, "session_sort");
725 void session_stop(struct session *se)
727 struct client_list *l;
728 session_enter(se, "session_stop1");
729 if (se->clients_starting)
731 session_leave(se, "session_stop1");
734 se->clients_starting = 1;
735 session_leave(se, "session_stop1");
737 session_alert_watch(se, SESSION_WATCH_SHOW);
738 session_alert_watch(se, SESSION_WATCH_BYTARGET);
739 session_alert_watch(se, SESSION_WATCH_TERMLIST);
740 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
742 for (l = se->clients_active; l; l = l->next)
744 struct client *cl = l->client;
747 session_enter(se, "session_stop2");
748 se->clients_starting = 0;
749 session_leave(se, "session_stop2");
752 enum pazpar2_error_code session_search(struct session *se,
754 const char *startrecs,
758 const char **addinfo,
759 const char **addinfo2,
760 struct reclist_sortparms *sp,
761 const char *mergekey,
764 int live_channels = 0;
766 int no_failed_query = 0;
767 int no_failed_limit = 0;
768 struct client_list *l;
770 session_log(se, YLOG_DEBUG, "Search");
774 session_enter(se, "session_search0");
775 if (se->clients_starting)
777 session_leave(se, "session_search0");
778 return PAZPAR2_NO_ERROR;
780 se->clients_starting = 1;
781 session_leave(se, "session_search0");
783 if (se->settings_modified) {
784 session_remove_cached_clients(se);
787 session_reset_active_clients(se, 0);
789 session_enter(se, "session_search");
790 se->settings_modified = 0;
795 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
800 se->rank = *rank ? xstrdup(rank) : 0;
803 session_clear_set(se, sp);
804 relevance_destroy(&se->relevance);
806 live_channels = select_targets(se, filter);
809 session_leave(se, "session_search");
810 se->clients_starting = 0;
811 return PAZPAR2_NO_TARGETS;
814 facet_limits_destroy(se->facet_limits);
815 se->facet_limits = facet_limits_create(limit);
816 if (!se->facet_limits)
819 session_leave(se, "session_search");
820 se->clients_starting = 0;
821 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
824 session_leave(se, "session_search");
826 session_alert_watch(se, SESSION_WATCH_SHOW);
827 session_alert_watch(se, SESSION_WATCH_BYTARGET);
828 session_alert_watch(se, SESSION_WATCH_TERMLIST);
829 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
831 for (l = se->clients_active; l; l = l->next)
834 struct client *cl = l->client;
835 client_parse_init(cl, 1);
836 if (prepare_map(se, client_get_database(cl)) < 0)
839 parse_ret = client_parse_query(cl, query, se->facet_limits, addinfo2);
842 else if (parse_ret == -2)
844 else if (parse_ret < 0)
845 no_working++; /* other error, such as bad CCL map */
848 client_parse_range(cl, startrecs, maxrecs);
849 client_parse_sort(cl, sp);
850 client_start_search(cl);
854 session_enter(se, "session_search2");
855 se->clients_starting = 0;
856 session_leave(se, "session_search2");
859 if (no_failed_query > 0)
862 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
864 else if (no_failed_limit > 0)
867 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
870 return PAZPAR2_NO_TARGETS;
872 return PAZPAR2_NO_ERROR;
875 // Creates a new session_database object for a database
876 static void session_init_databases_fun(void *context, struct database *db)
878 struct session *se = (struct session *) context;
879 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
885 assert(db->settings);
886 new->settings = nmem_malloc(se->session_nmem,
887 sizeof(struct settings *) * db->num_settings);
888 new->num_settings = db->num_settings;
889 for (i = 0; i < db->num_settings; i++)
891 struct setting *setting = db->settings[i];
892 new->settings[i] = setting;
894 new->next = se->databases;
898 // Doesn't free memory associated with sdb -- nmem takes care of that
899 static void session_database_destroy(struct session_database *sdb)
904 // Initialize session_database list -- this represents this session's view
905 // of the database list -- subject to modification by the settings ws command
906 void session_init_databases(struct session *se)
909 predef_grep_databases(se, se->service, session_init_databases_fun);
912 // Probably session_init_databases_fun should be refactored instead of
914 static struct session_database *load_session_database(struct session *se,
917 struct database *db = new_database_inherit_settings(id, se->session_nmem, se->service->settings);
918 session_init_databases_fun((void*) se, db);
920 // New sdb is head of se->databases list
921 return se->databases;
924 // Find an existing session database. If not found, load it
925 static struct session_database *find_session_database(struct session *se,
928 struct session_database *sdb;
930 for (sdb = se->databases; sdb; sdb = sdb->next)
931 if (!strcmp(sdb->database->id, id))
933 return load_session_database(se, id);
936 // Apply a session override to a database
937 void session_apply_setting(struct session *se, const char *dbname,
938 const char *name, const char *value)
940 session_enter(se, "session_apply_setting");
942 struct session_database *sdb = find_session_database(se, dbname);
943 struct conf_service *service = se->service;
945 int offset = settings_create_offset(service, name);
947 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
949 // Force later recompute of settings-driven data structures
950 // (happens when a search starts and client connections are prepared)
951 if (offset == PZ_XSLT)
953 se->settings_modified = 1;
954 for (s = sdb->settings[offset]; s; s = s->next)
955 if (!strcmp(s->name, name) &&
956 dbname && s->target && !strcmp(dbname, s->target))
960 s = nmem_malloc(se->session_nmem, sizeof(*s));
962 s->target = nmem_strdup(se->session_nmem, dbname);
963 s->name = nmem_strdup(se->session_nmem, name);
964 s->next = sdb->settings[offset];
965 sdb->settings[offset] = s;
967 s->value = nmem_strdup(se->session_nmem, value);
969 session_leave(se, "session_apply_setting");
972 void session_destroy(struct session *se)
974 struct session_database *sdb;
975 session_log(se, YLOG_LOG, "destroy");
977 session_remove_cached_clients(se);
979 for (sdb = se->databases; sdb; sdb = sdb->next)
980 session_database_destroy(sdb);
981 normalize_cache_destroy(se->normalize_cache);
982 relevance_destroy(&se->relevance);
983 reclist_destroy(se->reclist);
986 if (nmem_total(se->nmem))
987 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
988 if (nmem_total(se->session_nmem))
989 session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
990 facet_limits_destroy(se->facet_limits);
991 nmem_destroy(se->nmem);
992 service_destroy(se->service);
993 yaz_mutex_destroy(&se->session_mutex);
996 size_t session_get_memory_status(struct session *session) {
1000 session_enter(session, "session_get_memory_status");
1001 session_nmem = nmem_total(session->nmem);
1002 session_leave(session, "session_get_memory_status");
1003 return session_nmem;
1007 struct session *new_session(NMEM nmem, struct conf_service *service,
1008 unsigned session_id)
1011 struct session *session = nmem_malloc(nmem, sizeof(*session));
1015 sprintf(tmp_str, "session#%u", session_id);
1017 session->session_id = session_id;
1018 session_log(session, YLOG_DEBUG, "New");
1019 session->service = service;
1020 session->relevance = 0;
1021 session->total_records = 0;
1022 session->number_of_warnings_unknown_elements = 0;
1023 session->number_of_warnings_unknown_metadata = 0;
1024 session->termlists = 0;
1025 session->reclist = reclist_create(nmem);
1026 session->clients_active = 0;
1027 session->clients_cached = 0;
1028 session->settings_modified = 0;
1029 session->session_nmem = nmem;
1030 session->nmem = nmem_create();
1031 session->databases = 0;
1032 session->sorted_results = 0;
1033 session->facet_limits = 0;
1034 session->mergekey = 0;
1036 session->clients_starting = 0;
1038 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1040 session->watchlist[i].data = 0;
1041 session->watchlist[i].fun = 0;
1043 session->normalize_cache = normalize_cache_create();
1044 session->session_mutex = 0;
1045 pazpar2_mutex_create(&session->session_mutex, tmp_str);
1046 session_log(session, YLOG_LOG, "create");
1052 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf);
1054 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
1055 int *count, NMEM nmem)
1057 struct hitsbytarget *res = 0;
1058 struct client_list *l;
1061 for (l = se->clients_active; l; l = l->next)
1064 res = nmem_malloc(nmem, sizeof(*res) * sz);
1066 for (l = se->clients_active; l; l = l->next)
1068 struct client *cl = l->client;
1069 WRBUF w = wrbuf_alloc();
1070 const char *name = session_setting_oneval(client_get_database(cl),
1072 res[*count].id = client_get_id(cl);
1073 res[*count].name = *name ? name : "Unknown";
1074 res[*count].hits = client_get_hits(cl);
1075 res[*count].approximation = client_get_approximation(cl);
1076 res[*count].records = client_get_num_records(cl,
1077 &res[*count].filtered,
1079 res[*count].diagnostic =
1080 client_get_diagnostic(cl, &res[*count].message,
1081 &res[*count].addinfo);
1082 res[*count].state = client_get_state_str(cl);
1083 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1084 session_settings_dump(se, client_get_database(cl), w);
1085 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1088 res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1095 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1097 struct hitsbytarget *p;
1098 session_enter(se, "get_hitsbytarget");
1099 p = hitsbytarget_nb(se, count, nmem);
1100 session_leave(se, "get_hitsbytarget");
1104 // Compares two hitsbytarget nodes by hitcount
1105 static int cmp_ht(const void *p1, const void *p2)
1107 const struct hitsbytarget *h1 = p1;
1108 const struct hitsbytarget *h2 = p2;
1109 return h2->hits - h1->hits;
1112 // Compares two hitsbytarget nodes by hitcount
1113 static int cmp_ht_approx(const void *p1, const void *p2)
1115 const struct hitsbytarget *h1 = p1;
1116 const struct hitsbytarget *h2 = p2;
1117 return h2->approximation - h1->approximation;
1120 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1121 NMEM nmem, int version)
1123 struct hitsbytarget *ht;
1126 ht = hitsbytarget_nb(se, &count, nmem);
1128 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1130 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1131 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1134 // do only print terms which have display names
1136 wrbuf_puts(wrbuf, "<term>\n");
1138 wrbuf_puts(wrbuf, "<id>");
1139 wrbuf_xmlputs(wrbuf, ht[i].id);
1140 wrbuf_puts(wrbuf, "</id>\n");
1142 wrbuf_puts(wrbuf, "<name>");
1143 if (!ht[i].name || !ht[i].name[0])
1144 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1146 wrbuf_xmlputs(wrbuf, ht[i].name);
1147 wrbuf_puts(wrbuf, "</name>\n");
1149 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1153 // Should not print if we know it isn't a approximation.
1154 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1155 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1156 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1159 wrbuf_puts(wrbuf, "<state>");
1160 wrbuf_xmlputs(wrbuf, ht[i].state);
1161 wrbuf_puts(wrbuf, "</state>\n");
1163 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1165 wrbuf_puts(wrbuf, "</term>\n");
1170 void perform_termlist(struct http_channel *c, struct session *se,
1171 const char *name, int num, int version)
1174 NMEM nmem_tmp = nmem_create();
1181 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1183 session_enter(se, "perform_termlist");
1185 for (j = 0; j < num_names; j++)
1188 int must_generate_empty = 1; /* bug 5350 */
1190 struct named_termlist *t = se->termlists;
1191 for (; t; t = t->next)
1194 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1196 struct termlist_score **p = 0;
1199 wrbuf_puts(c->wrbuf, "<list name=\"");
1200 wrbuf_xmlputs(c->wrbuf, tname);
1201 wrbuf_puts(c->wrbuf, "\">\n");
1202 must_generate_empty = 0;
1204 p = termlist_highscore(t->termlist, &len, nmem_tmp);
1208 for (i = 0; i < len && i < num; i++)
1210 // prevent sending empty term elements
1211 if (!p[i]->display_term || !p[i]->display_term[0])
1214 wrbuf_puts(c->wrbuf, "<term>");
1215 wrbuf_puts(c->wrbuf, "<name>");
1216 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1217 wrbuf_puts(c->wrbuf, "</name>");
1219 wrbuf_printf(c->wrbuf,
1220 "<frequency>%d</frequency>",
1222 wrbuf_puts(c->wrbuf, "</term>\n");
1225 wrbuf_puts(c->wrbuf, "</list>\n");
1229 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1231 wrbuf_puts(c->wrbuf, "<list name=\"");
1232 wrbuf_xmlputs(c->wrbuf, tname);
1233 wrbuf_puts(c->wrbuf, "\">\n");
1235 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1236 wrbuf_puts(c->wrbuf, "</list>\n");
1237 must_generate_empty = 0;
1239 if (must_generate_empty)
1241 wrbuf_puts(c->wrbuf, "<list name=\"");
1242 wrbuf_xmlputs(c->wrbuf, names[j]);
1243 wrbuf_puts(c->wrbuf, "\"/>\n");
1246 session_leave(se, "perform_termlist");
1247 nmem_destroy(nmem_tmp);
1250 #ifdef MISSING_HEADERS
1251 void report_nmem_stats(void)
1253 size_t in_use, is_free;
1255 nmem_get_memory_in_use(&in_use);
1256 nmem_get_memory_free(&is_free);
1258 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1259 (long) in_use, (long) is_free);
1263 struct record_cluster *show_single_start(struct session *se, const char *id,
1264 struct record_cluster **prev_r,
1265 struct record_cluster **next_r)
1267 struct record_cluster *r = 0;
1269 session_enter(se, "show_single_start");
1272 reclist_limit(se->reclist, se, 1);
1274 reclist_enter(se->reclist);
1275 while ((r = reclist_read_record(se->reclist)))
1277 if (!strcmp(r->recid, id))
1279 *next_r = reclist_read_record(se->reclist);
1284 reclist_leave(se->reclist);
1286 session_leave(se, "show_single_start");
1290 void show_single_stop(struct session *se, struct record_cluster *rec)
1292 session_leave(se, "show_single_stop");
1296 int session_fetch_more(struct session *se)
1298 struct client_list *l;
1301 for (l = se->clients_active; l; l = l->next)
1303 struct client *cl = l->client;
1304 if (client_get_state(cl) == Client_Idle)
1306 if (client_fetch_more(cl))
1308 session_log(se, YLOG_LOG, "%s: more to fetch",
1315 int ingest_failures;
1316 int record_failures;
1317 int num = client_get_num_records(
1318 cl, &filtered, &ingest_failures, &record_failures);
1320 session_log(se, YLOG_LOG, "%s: hits=" ODR_INT_PRINTF
1321 " fetched=%d filtered=%d",
1323 client_get_hits(cl),
1325 if (ingest_failures || record_failures)
1327 session_log(se, YLOG_WARN, "%s:"
1328 " ingest failures=%d record failures=%d",
1330 ingest_failures, record_failures);
1336 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1337 client_get_id(cl), client_get_state_str(cl));
1344 struct record_cluster **show_range_start(struct session *se,
1345 struct reclist_sortparms *sp,
1346 int start, int *num, int *total,
1347 Odr_int *sumhits, Odr_int *approx_hits,
1348 void (*show_records_ready)(void *data),
1349 struct http_channel *chan)
1351 struct record_cluster **recs = 0;
1352 struct reclist_sortparms *spp;
1353 struct client_list *l;
1356 yaz_timing_t t = yaz_timing_create();
1358 session_enter(se, "show_range_start");
1362 reclist_limit(se->reclist, se, 0);
1365 for (spp = sp; spp; spp = spp->next)
1366 if (spp->type == Metadata_type_relevance)
1368 relevance_prepare_read(se->relevance, se->reclist);
1371 for (l = se->clients_active; l; l = l->next) {
1372 *sumhits += client_get_hits(l->client);
1373 *approx_hits += client_get_approximation(l->client);
1376 reclist_sort(se->reclist, sp);
1378 reclist_enter(se->reclist);
1379 *total = reclist_get_num_records(se->reclist);
1381 for (l = se->clients_active; l; l = l->next)
1382 client_update_show_stat(l->client, 0);
1384 for (i = 0; i < start; i++)
1386 struct record_cluster *r = reclist_read_record(se->reclist);
1394 struct record *rec = r->records;
1395 for (;rec; rec = rec->next)
1396 client_update_show_stat(rec->client, 1);
1399 recs = nmem_malloc(se->nmem, (*num > 0 ? *num : 1) * sizeof(*recs));
1400 for (i = 0; i < *num; i++)
1402 struct record_cluster *r = reclist_read_record(se->reclist);
1410 struct record *rec = r->records;
1411 for (;rec; rec = rec->next)
1412 client_update_show_stat(rec->client, 1);
1416 reclist_leave(se->reclist);
1419 session_log(se, YLOG_LOG, "show %6.5f %3.2f %3.2f",
1420 yaz_timing_get_real(t), yaz_timing_get_user(t),
1421 yaz_timing_get_sys(t));
1422 yaz_timing_destroy(&t);
1425 if (!session_fetch_more(se))
1426 session_log(se, YLOG_LOG, "can not fetch more");
1429 show_range_stop(se, recs);
1430 session_log(se, YLOG_LOG, "fetching more in progress");
1431 if (session_set_watch(se, SESSION_WATCH_SHOW,
1432 show_records_ready, chan, chan))
1434 session_log(se, YLOG_WARN, "Ignoring show block");
1435 session_enter(se, "show_range_start");
1439 session_log(se, YLOG_LOG, "session watch OK");
1446 void show_range_stop(struct session *se, struct record_cluster **recs)
1448 session_leave(se, "show_range_stop");
1451 void statistics(struct session *se, struct statistics *stat)
1453 struct client_list *l;
1456 memset(stat, 0, sizeof(*stat));
1458 for (l = se->clients_active; l; l = l->next)
1460 struct client *cl = l->client;
1461 if (!client_get_connection(cl))
1462 stat->num_no_connection++;
1463 stat->num_hits += client_get_hits(cl);
1464 switch (client_get_state(cl))
1466 case Client_Connecting: stat->num_connecting++; break;
1467 case Client_Working: stat->num_working++; break;
1468 case Client_Idle: stat->num_idle++; break;
1469 case Client_Failed: stat->num_failed++; break;
1470 case Client_Error: stat->num_error++; break;
1475 stat->num_records = se->total_records;
1477 stat->num_clients = count;
1480 static struct record_metadata *record_metadata_init(
1481 NMEM nmem, const char *value, enum conf_metadata_type type,
1482 struct _xmlAttr *attr)
1484 struct record_metadata *rec_md = record_metadata_create(nmem);
1485 struct record_metadata_attr **attrp = &rec_md->attributes;
1487 for (; attr; attr = attr->next)
1489 if (attr->children && attr->children->content)
1491 if (strcmp((const char *) attr->name, "type")
1492 && strcmp((const char *) attr->name, "empty"))
1493 { /* skip the "type" + "empty" attribute..
1494 The "Type" is already part of the element in output
1495 (md-%s) and so repeating it here is redundant */
1496 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1498 nmem_strdup(nmem, (const char *) attr->name);
1500 nmem_strdup(nmem, (const char *) attr->children->content);
1501 attrp = &(*attrp)->next;
1509 case Metadata_type_generic:
1510 case Metadata_type_skiparticle:
1511 if (strstr(value, "://")) /* looks like a URL */
1512 rec_md->data.text.disp = nmem_strdup(nmem, value);
1514 rec_md->data.text.disp =
1515 normalize7bit_generic(nmem_strdup(nmem, value), " ,/.:([");
1516 rec_md->data.text.sort = 0;
1517 rec_md->data.text.snippet = 0;
1519 case Metadata_type_year:
1520 case Metadata_type_date:
1525 if (type == Metadata_type_date)
1527 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1530 rec_md->data.number.min = first;
1531 rec_md->data.number.max = last;
1534 case Metadata_type_float:
1535 rec_md->data.fnumber = atof(value);
1537 case Metadata_type_relevance:
1538 case Metadata_type_position:
1544 static void mergekey_norm_wr(pp2_charset_fact_t charsets,
1545 WRBUF norm_wr, const char *value)
1547 const char *norm_str;
1548 pp2_charset_token_t prt =
1549 pp2_charset_token_create(charsets, "mergekey");
1551 pp2_charset_token_first(prt, value, 0);
1552 while ((norm_str = pp2_charset_token_next(prt)))
1556 if (wrbuf_len(norm_wr))
1557 wrbuf_puts(norm_wr, " ");
1558 wrbuf_puts(norm_wr, norm_str);
1561 pp2_charset_token_destroy(prt);
1564 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1565 struct conf_service *service, WRBUF norm_wr)
1569 for (n = root->children; n; n = n->next)
1571 if (n->type != XML_ELEMENT_NODE)
1573 if (!strcmp((const char *) n->name, "metadata"))
1575 const char *type = yaz_xml_get_prop(n, "type");
1577 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1579 else if (!strcmp(name, (const char *) type))
1581 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1582 if (value && *value)
1584 if (wrbuf_len(norm_wr) > 0)
1585 wrbuf_puts(norm_wr, " ");
1586 wrbuf_puts(norm_wr, name);
1587 mergekey_norm_wr(service->charsets, norm_wr,
1588 (const char *) value);
1599 static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
1600 struct client *cl, int record_no,
1601 struct conf_service *service, NMEM nmem,
1602 const char *session_mergekey)
1604 char *mergekey_norm = 0;
1605 WRBUF norm_wr = wrbuf_alloc();
1606 const char *mergekey;
1608 if (session_mergekey)
1612 nmem_strsplit_escape2(nmem, ",", session_mergekey, &values,
1615 for (i = 0; i < num; i++)
1616 get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
1618 else if ((mergekey = yaz_xml_get_prop(root, "mergekey")))
1620 mergekey_norm_wr(service->charsets, norm_wr, mergekey);
1624 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1626 for (field_id = 0; field_id < service->num_metadata; field_id++)
1628 struct conf_metadata *ser_md = &service->metadata[field_id];
1629 if (ser_md->mergekey != Metadata_mergekey_no)
1631 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1633 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1635 /* no mergekey on this one and it is required..
1636 Generate unique key instead */
1637 wrbuf_rewind(norm_wr);
1644 /* generate unique key if none is not generated already or is empty */
1645 if (wrbuf_len(norm_wr) == 0)
1647 wrbuf_printf(norm_wr, "position: %s-%d",
1648 client_get_id(cl), record_no);
1652 const char *lead = "content: ";
1653 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1655 if (wrbuf_len(norm_wr) > 0)
1656 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1657 wrbuf_destroy(norm_wr);
1658 return mergekey_norm;
1661 /** \brief see if metadata for pz:recordfilter exists
1662 \param root xml root element of normalized record
1663 \param sdb session database for client
1664 \retval 0 if there is no metadata for pz:recordfilter
1665 \retval 1 if there is metadata for pz:recordfilter
1667 If there is no pz:recordfilter defined, this function returns 1
1671 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1676 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1681 for (n = root->children; n; n = n->next)
1683 if (n->type != XML_ELEMENT_NODE)
1685 if (!strcmp((const char *) n->name, "metadata"))
1687 const char *type = yaz_xml_get_prop(n, "type");
1694 if ((eq = strchr(s, '=')))
1696 else if ((eq = strchr(s, '~')))
1702 if (len == strlen((const char *)type) &&
1703 !memcmp((const char *) type, s, len))
1705 xmlChar *value = xmlNodeGetContent(n);
1706 if (value && *value)
1709 (substring && strstr((const char *) value, eq+1)) ||
1710 (!substring && !strcmp((const char *) value, eq + 1)))
1721 static int ingest_to_cluster(struct client *cl,
1727 struct record_metadata_attr *mergekey);
1729 static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
1730 int record_no, NMEM nmem,
1731 struct session_database *sdb,
1732 struct record_metadata_attr *mergekeys)
1735 struct session *se = client_get_session(cl);
1736 WRBUF wrbuf_disp, wrbuf_norm;
1738 if (!check_record_filter(root, sdb))
1740 session_log(se, YLOG_LOG,
1741 "Filtered out record no %d from %s",
1742 record_no, sdb->database->id);
1745 wrbuf_disp = wrbuf_alloc();
1746 wrbuf_norm = wrbuf_alloc();
1747 session_enter(se, "ingest_sub_record");
1748 if (client_get_session(cl) == se && se->relevance)
1749 ret = ingest_to_cluster(cl, wrbuf_disp, wrbuf_norm,
1750 xdoc, root, record_no, mergekeys);
1751 session_leave(se, "ingest_sub_record");
1752 wrbuf_destroy(wrbuf_norm);
1753 wrbuf_destroy(wrbuf_disp);
1757 /** \brief ingest XML record
1758 \param cl client holds the result set for record
1759 \param rec record buffer (0 terminated)
1760 \param record_no record position (1, 2, ..)
1761 \param nmem working NMEM
1766 int ingest_record(struct client *cl, const char *rec,
1767 int record_no, NMEM nmem)
1769 struct session *se = client_get_session(cl);
1770 struct session_database *sdb = client_get_database(cl);
1771 struct conf_service *service = se->service;
1772 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1773 int r = ingest_xml_record(cl, xdoc, record_no, nmem, 0);
1774 client_store_xdoc(cl, record_no, xdoc);
1778 int ingest_xml_record(struct client *cl, xmlDoc *xdoc,
1779 int record_no, NMEM nmem, int cached_copy)
1781 struct session *se = client_get_session(cl);
1782 struct session_database *sdb = client_get_database(cl);
1783 struct conf_service *service = se->service;
1789 if (global_parameters.dump_records)
1791 session_log(se, YLOG_LOG, "Normalized record from %s",
1796 root = xmlDocGetRootElement(xdoc);
1798 if (!strcmp((const char *) root->name, "cluster"))
1800 int no_merge_keys = 0;
1801 int no_merge_dups = 0;
1803 struct record_metadata_attr *mk = 0;
1805 for (sroot = root->children; sroot; sroot = sroot->next)
1806 if (sroot->type == XML_ELEMENT_NODE &&
1807 !strcmp((const char *) sroot->name, "record"))
1809 struct record_metadata_attr **mkp;
1810 const char *mergekey_norm =
1811 get_mergekey(xdoc, sroot, cl, record_no, service, nmem,
1818 for (mkp = &mk; *mkp; mkp = &(*mkp)->next)
1819 if (!strcmp((*mkp)->value, mergekey_norm))
1823 *mkp = (struct record_metadata_attr*)
1824 nmem_malloc(nmem, sizeof(**mkp));
1826 (*mkp)->value = nmem_strdup(nmem, mergekey_norm);
1833 if (no_merge_keys > 1 || no_merge_dups > 0)
1835 yaz_log(YLOG_LOG, "Got %d mergekeys, %d dups for position %d",
1836 no_merge_keys, no_merge_dups, record_no);
1838 for (sroot = root->children; !r && sroot; sroot = sroot->next)
1839 if (sroot->type == XML_ELEMENT_NODE &&
1840 !strcmp((const char *) sroot->name, "record"))
1843 insert_settings_values(sdb, xdoc, root, service);
1844 r = ingest_sub_record(cl, xdoc, sroot, record_no, nmem, sdb,
1848 else if (!strcmp((const char *) root->name, "record"))
1850 const char *mergekey_norm =
1851 get_mergekey(xdoc, root, cl, record_no, service, nmem,
1855 struct record_metadata_attr *mk = (struct record_metadata_attr*)
1856 nmem_malloc(nmem, sizeof(*mk));
1858 mk->value = nmem_strdup(nmem, mergekey_norm);
1862 insert_settings_values(sdb, xdoc, root, service);
1863 r = ingest_sub_record(cl, xdoc, root, record_no, nmem, sdb, mk);
1868 session_log(se, YLOG_WARN, "Bad pz root element: %s",
1869 (const char *) root->name);
1876 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1877 // struct record_metadata *rec_md = record->metadata[md_field_id];
1878 static int match_metadata_local(struct conf_service *service,
1879 struct conf_metadata *ser_md,
1880 struct record_metadata *rec_md0,
1881 char **values, int num_v)
1884 struct record_metadata *rec_md = rec_md0;
1886 WRBUF text_wr = wrbuf_alloc();
1887 for (i = 0; i < num_v; )
1891 if (ser_md->type == Metadata_type_year
1892 || ser_md->type == Metadata_type_date)
1894 int y = atoi(values[i]);
1895 if (y >= rec_md->data.number.min
1896 && y <= rec_md->data.number.max)
1903 val_wr = wrbuf_alloc();
1904 mergekey_norm_wr(service->charsets, val_wr, values[i]);
1906 wrbuf_rewind(text_wr);
1907 mergekey_norm_wr(service->charsets, text_wr,
1908 rec_md->data.text.disp);
1909 if (!strcmp(wrbuf_cstr(val_wr), wrbuf_cstr(text_wr)))
1912 rec_md = rec_md->next;
1917 wrbuf_destroy(val_wr);
1922 wrbuf_destroy(val_wr);
1923 wrbuf_destroy(text_wr);
1924 return i < num_v ? 1 : 0;
1927 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
1930 struct conf_service *service = se->service;
1934 NMEM nmem_tmp = nmem_create();
1936 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
1939 for (j = 0; j < service->num_metadata; j++)
1941 struct conf_metadata *md = service->metadata + j;
1942 if (!strcmp(md->name, name) && md->limitcluster)
1947 conf_service_metadata_field_id(service,
1950 if (md_field_id < 0)
1956 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1959 if (!match_metadata_local(service,
1960 &service->metadata[md_field_id],
1961 rec->metadata[md_field_id],
1970 nmem_destroy(nmem_tmp);
1974 // Skip record on non-zero
1975 static int check_limit_local(struct client *cl,
1976 struct record *record,
1979 int skip_record = 0;
1980 struct session *se = client_get_session(cl);
1981 struct conf_service *service = se->service;
1982 NMEM nmem_tmp = nmem_create();
1983 struct session_database *sdb = client_get_database(cl);
1985 while (!skip_record)
1991 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
1996 if (!strcmp(name, "*"))
1998 for (md_field_id = 0; md_field_id < service->num_metadata;
2001 if (match_metadata_local(
2003 &service->metadata[md_field_id],
2004 record->metadata[md_field_id],
2008 if (md_field_id == service->num_metadata)
2013 md_field_id = conf_service_metadata_field_id(service, name);
2014 if (md_field_id < 0)
2019 if (!match_metadata_local(
2021 &service->metadata[md_field_id],
2022 record->metadata[md_field_id],
2029 nmem_destroy(nmem_tmp);
2033 static int ingest_to_cluster(struct client *cl,
2039 struct record_metadata_attr *merge_keys)
2042 struct session *se = client_get_session(cl);
2043 struct conf_service *service = se->service;
2044 int term_factor = 1;
2045 struct record_cluster *cluster;
2046 struct record_metadata **metadata0;
2047 struct session_database *sdb = client_get_database(cl);
2048 NMEM ingest_nmem = 0;
2049 char **rank_values = 0;
2051 struct record *record = record_create(se->nmem,
2052 service->num_metadata,
2053 service->num_sortkeys, cl,
2056 for (n = root->children; n; n = n->next)
2058 if (n->type != XML_ELEMENT_NODE)
2060 if (!strcmp((const char *) n->name, "metadata"))
2062 struct conf_metadata *ser_md = 0;
2063 struct record_metadata **wheretoput = 0;
2064 struct record_metadata *rec_md = 0;
2065 int md_field_id = -1;
2067 const char *type = yaz_xml_get_prop(n, "type");
2071 wrbuf_rewind(wrbuf_disp);
2072 value0 = xmlNodeListGetString(xdoc, n->children, 1);
2073 if (!value0 || !*value0)
2075 const char *empty = yaz_xml_get_prop(n, "empty");
2078 wrbuf_puts(wrbuf_disp, (const char *) empty);
2082 wrbuf_puts(wrbuf_disp, (const char *) value0);
2087 = conf_service_metadata_field_id(service, (const char *) type);
2088 if (md_field_id < 0)
2090 if (se->number_of_warnings_unknown_metadata == 0)
2092 session_log(se, YLOG_WARN,
2093 "Ignoring unknown metadata element: %s", type);
2095 se->number_of_warnings_unknown_metadata++;
2099 ser_md = &service->metadata[md_field_id];
2101 // non-merged metadata
2102 rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp),
2103 ser_md->type, n->properties);
2106 session_log(se, YLOG_WARN, "bad metadata data '%s' "
2107 "for element '%s'", wrbuf_cstr(wrbuf_disp), type);
2111 if (ser_md->type == Metadata_type_generic)
2113 WRBUF w = wrbuf_alloc();
2114 if (relevance_snippet(se->relevance,
2115 wrbuf_cstr(wrbuf_disp), ser_md->name, w))
2116 rec_md->data.text.snippet = nmem_strdup(se->nmem,
2122 wheretoput = &record->metadata[md_field_id];
2124 wheretoput = &(*wheretoput)->next;
2125 *wheretoput = rec_md;
2129 if (check_limit_local(cl, record, record_no))
2133 cluster = reclist_insert(se->reclist, se->relevance, service, record,
2134 merge_keys, &se->total_merged);
2137 return 0; // complete match with existing record
2141 const char *use_term_factor_str =
2142 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
2143 if (use_term_factor_str && use_term_factor_str[0] == '1')
2145 int maxrecs = client_get_maxrecs(cl);
2146 int hits = (int) client_get_hits(cl);
2147 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
2148 assert(term_factor >= 1);
2149 session_log(se, YLOG_DEBUG, "Using term factor: %d (%d / %d)",
2150 term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
2154 if (global_parameters.dump_records)
2155 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
2156 sdb->database->id, record_no);
2158 // original metadata, to check if first existence of a field
2159 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
2160 memcpy(metadata0, cluster->metadata,
2161 sizeof(*metadata0) * service->num_metadata);
2163 ingest_nmem = nmem_create();
2166 yaz_log(YLOG_LOG, "local in sort : %s", se->rank);
2167 nmem_strsplit_escape2(ingest_nmem, ",", se->rank, &rank_values,
2168 &rank_num, 1, '\\', 1);
2171 // now parsing XML record and adding data to cluster or record metadata
2172 for (n = root->children; n; n = n->next)
2174 if (n->type != XML_ELEMENT_NODE)
2176 if (!strcmp((const char *) n->name, "metadata"))
2178 struct conf_metadata *ser_md = 0;
2179 struct conf_sortkey *ser_sk = 0;
2180 struct record_metadata **wheretoput = 0;
2181 struct record_metadata *rec_md = 0;
2182 int md_field_id = -1;
2183 int sk_field_id = -1;
2184 const char *rank = 0;
2185 const char *xml_rank = 0;
2186 const char *type = 0;
2189 wrbuf_rewind(wrbuf_disp);
2190 type = yaz_xml_get_prop(n, "type");
2195 = conf_service_metadata_field_id(service, (const char *) type);
2196 if (md_field_id < 0)
2199 ser_md = &service->metadata[md_field_id];
2201 if (ser_md->sortkey_offset >= 0)
2203 sk_field_id = ser_md->sortkey_offset;
2204 ser_sk = &service->sortkeys[sk_field_id];
2207 value0 = xmlNodeListGetString(xdoc, n->children, 1);
2208 if (!value0 || !*value0)
2214 wrbuf_puts(wrbuf_disp, (const char *) value0);
2219 rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp),
2222 // see if the field was not in cluster already (from beginning)
2230 for (i = 0; i < rank_num; i++)
2232 const char *val = rank_values[i];
2233 const char *cp = strchr(val, '=');
2236 if ((cp - val) == strlen((const char *) type)
2237 && !memcmp(val, type, cp - val))
2246 xml_rank = yaz_xml_get_prop(n, "rank");
2247 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
2250 wheretoput = &cluster->metadata[md_field_id];
2252 if (ser_md->merge == Metadata_merge_first)
2254 if (!metadata0[md_field_id])
2257 wheretoput = &(*wheretoput)->next;
2258 *wheretoput = rec_md;
2261 else if (ser_md->merge == Metadata_merge_unique)
2265 if (!strcmp((const char *) (*wheretoput)->data.text.disp,
2266 rec_md->data.text.disp))
2268 wheretoput = &(*wheretoput)->next;
2271 *wheretoput = rec_md;
2273 else if (ser_md->merge == Metadata_merge_longest)
2276 || strlen(rec_md->data.text.disp)
2277 > strlen((*wheretoput)->data.text.disp))
2279 *wheretoput = rec_md;
2282 pp2_charset_token_t prt;
2283 const char *sort_str = 0;
2285 ser_sk->type == Metadata_type_skiparticle;
2287 if (!cluster->sortkeys[sk_field_id])
2288 cluster->sortkeys[sk_field_id] =
2289 nmem_malloc(se->nmem,
2290 sizeof(union data_types));
2293 pp2_charset_token_create(service->charsets, "sort");
2295 pp2_charset_token_first(prt, rec_md->data.text.disp,
2298 pp2_charset_token_next(prt);
2300 sort_str = pp2_get_sort(prt);
2302 cluster->sortkeys[sk_field_id]->text.disp =
2303 rec_md->data.text.disp;
2306 sort_str = rec_md->data.text.disp;
2307 session_log(se, YLOG_WARN,
2308 "Could not make sortkey. Bug #1858");
2310 cluster->sortkeys[sk_field_id]->text.sort =
2311 nmem_strdup(se->nmem, sort_str);
2312 pp2_charset_token_destroy(prt);
2316 else if (ser_md->merge == Metadata_merge_all)
2319 wheretoput = &(*wheretoput)->next;
2320 *wheretoput = rec_md;
2322 else if (ser_md->merge == Metadata_merge_range)
2326 *wheretoput = rec_md;
2328 cluster->sortkeys[sk_field_id]
2333 int this_min = rec_md->data.number.min;
2334 int this_max = rec_md->data.number.max;
2335 if (this_min < (*wheretoput)->data.number.min)
2336 (*wheretoput)->data.number.min = this_min;
2337 if (this_max > (*wheretoput)->data.number.max)
2338 (*wheretoput)->data.number.max = this_max;
2342 // ranking of _all_ fields enabled ...
2345 relevance_countwords(se->relevance, cluster,
2346 wrbuf_cstr(wrbuf_disp),
2347 rank, ser_md->name);
2349 // construct facets ... unless the client already has reported them
2350 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2352 if (ser_md->type == Metadata_type_year)
2355 sprintf(year, "%d", rec_md->data.number.max);
2357 add_facet(se, (char *) type, year, term_factor);
2358 if (rec_md->data.number.max != rec_md->data.number.min)
2360 sprintf(year, "%d", rec_md->data.number.min);
2361 add_facet(se, (char *) type, year, term_factor);
2365 add_facet(se, type, wrbuf_cstr(wrbuf_disp), term_factor);
2370 if (se->number_of_warnings_unknown_elements == 0)
2371 session_log(se, YLOG_WARN,
2372 "Unexpected element in internal record: %s", n->name);
2373 se->number_of_warnings_unknown_elements++;
2376 nmem_destroy(ingest_nmem);
2378 relevance_donerecord(se->relevance, cluster);
2379 se->total_records++;
2384 void session_log(struct session *s, int level, const char *fmt, ...)
2390 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2391 yaz_log(level, "Session %u: %s", s ? s->session_id : 0, buf);
2399 * c-file-style: "Stroustrup"
2400 * indent-tabs-mode: nil
2402 * vim: shiftwidth=4 tabstop=8 expandtab