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_get_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);
211 struct facet_id *next;
214 static void session_add_id_facet(struct session *s, struct client *cl,
220 struct facet_id *t = nmem_malloc(s->session_nmem, sizeof(*t));
222 t->client_id = nmem_strdup(s->session_nmem, client_get_id(cl));
223 t->type = nmem_strdup(s->session_nmem, type);
224 t->id = nmem_strdupn(s->session_nmem, id, id_len);
225 t->term = nmem_strdup(s->session_nmem, term);
226 t->next = s->facet_id_list;
227 s->facet_id_list = t;
231 // Look up a facet term, and return matching id
232 // If facet type not found, returns 0
233 // If facet type found, but no matching term, returns ""
234 const char *session_lookup_id_facet(struct session *s, struct client *cl,
239 struct facet_id *t = s->facet_id_list;
240 for (; t; t = t->next)
242 if (!strcmp(client_get_id(cl), t->client_id) && !strcmp(t->type, type) )
245 if ( !strcmp(t->term, term))
254 void add_facet(struct session *s, const char *type, const char *value, int count, struct client *cl)
256 WRBUF facet_wrbuf = wrbuf_alloc();
257 WRBUF display_wrbuf = wrbuf_alloc();
261 /* inspect pz:facetmap:split:name ?? */
262 if (!strncmp(type, "split:", 6))
264 const char *cp = strchr(value, ':');
274 session_normalize_facet(s, type, value, display_wrbuf, facet_wrbuf);
275 if (wrbuf_len(facet_wrbuf))
277 struct named_termlist **tp = &s->termlists;
278 for (; (*tp); tp = &(*tp)->next)
279 if (!strcmp((*tp)->name, type))
283 *tp = nmem_malloc(s->nmem, sizeof(**tp));
284 (*tp)->name = nmem_strdup(s->nmem, type);
285 (*tp)->termlist = termlist_create(s->nmem);
288 termlist_insert((*tp)->termlist, wrbuf_cstr(display_wrbuf),
289 wrbuf_cstr(facet_wrbuf), id, id_len, count);
291 session_add_id_facet(s, cl, type, id, id_len,
292 wrbuf_cstr(display_wrbuf));
294 wrbuf_destroy(facet_wrbuf);
295 wrbuf_destroy(display_wrbuf);
298 static xmlDoc *record_to_xml(struct session *se,
299 struct session_database *sdb, const char *rec)
301 struct database *db = sdb->database;
304 rdoc = xmlParseMemory(rec, strlen(rec));
308 session_log(se, YLOG_WARN, "Non-wellformed XML");
312 if (global_parameters.dump_records)
314 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->id);
321 #define MAX_XSLT_ARGS 16
323 // Add static values from session database settings if applicable
324 static void insert_settings_parameters(struct session_database *sdb,
325 struct conf_service *service,
333 for (i = 0; i < service->num_metadata; i++)
335 struct conf_metadata *md = &service->metadata[i];
338 if (md->setting == Metadata_setting_parameter &&
339 (setting = settings_lookup_offset(service, md->name)) >= 0)
341 const char *val = session_setting_oneval(sdb, setting);
342 if (val && nparms < MAX_XSLT_ARGS)
345 int len = strlen(val);
346 buf = nmem_malloc(nmem, len + 3);
348 strcpy(buf + 1, val);
351 parms[offset++] = md->name;
352 parms[offset++] = buf;
360 // Add static values from session database settings if applicable
361 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
363 struct conf_service *service)
367 for (i = 0; i < service->num_metadata; i++)
369 struct conf_metadata *md = &service->metadata[i];
372 if (md->setting == Metadata_setting_postproc &&
373 (offset = settings_lookup_offset(service, md->name)) >= 0)
375 const char *val = session_setting_oneval(sdb, offset);
378 xmlNode *n = xmlNewTextChild(root, 0, (xmlChar *) "metadata",
380 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
386 static xmlDoc *normalize_record(struct session *se,
387 struct session_database *sdb,
388 struct conf_service *service,
389 const char *rec, NMEM nmem)
391 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
395 char *parms[MAX_XSLT_ARGS*2+1];
397 insert_settings_parameters(sdb, service, parms, nmem);
399 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
401 session_log(se, YLOG_WARN, "Normalize failed");
407 void session_settings_dump(struct session *se,
408 struct session_database *db,
413 int i, num = db->num_settings;
414 for (i = 0; i < num; i++)
416 struct setting *s = db->settings[i];
417 for (;s ; s = s->next)
419 wrbuf_puts(w, "<set name=\"");
420 wrbuf_xmlputs(w, s->name);
421 wrbuf_puts(w, "\" value=\"");
422 wrbuf_xmlputs(w, s->value);
423 wrbuf_puts(w, "\"/>");
431 // Retrieve first defined value for 'name' for given database.
432 // Will be extended to take into account user associated with session
433 const char *session_setting_oneval(struct session_database *db, int offset)
435 if (offset >= db->num_settings || !db->settings[offset])
437 return db->settings[offset]->value;
440 // Prepare XSLT stylesheets for record normalization
441 // Structures are allocated on the session_wide nmem to avoid having
442 // to recompute this for every search. This would lead
443 // to leaking if a single session was to repeatedly change the PZ_XSLT
444 // setting. However, this is not a realistic use scenario.
445 static int prepare_map(struct session *se, struct session_database *sdb)
447 if (sdb->settings && !sdb->map)
451 if (sdb->settings[PZ_XSLT] &&
452 (s = session_setting_oneval(sdb, PZ_XSLT)))
454 char auto_stylesheet[256];
456 if (!strcmp(s, "auto"))
458 const char *request_syntax = session_setting_oneval(
459 sdb, PZ_REQUESTSYNTAX);
463 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
464 "%s.xsl", request_syntax);
465 for (cp = auto_stylesheet; *cp; cp++)
467 /* deliberately only consider ASCII */
468 if (*cp > 32 && *cp < 127)
475 session_log(se, YLOG_WARN,
476 "No pz:requestsyntax for auto stylesheet");
479 sdb->map = normalize_cache_get(se->normalize_cache,
488 // called if watch should be removed because http_channel is to be destroyed
489 static void session_watch_cancel(void *data, struct http_channel *c,
492 struct session_watchentry *ent = data;
499 // set watch. Returns 0=OK, -1 if watch is already set
500 int session_set_watch(struct session *s, int what,
501 session_watchfun fun, void *data,
502 struct http_channel *chan)
505 session_enter(s, "session_set_watch");
506 if (s->watchlist[what].fun)
511 s->watchlist[what].fun = fun;
512 s->watchlist[what].data = data;
513 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
514 session_watch_cancel);
517 session_leave(s, "session_set_watch");
521 void session_alert_watch(struct session *s, int what)
524 session_enter(s, "session_alert_watch");
525 if (s->watchlist[what].fun)
527 /* our watch is no longer associated with http_channel */
529 session_watchfun fun;
531 http_remove_observer(s->watchlist[what].obs);
532 fun = s->watchlist[what].fun;
533 data = s->watchlist[what].data;
535 /* reset watch before fun is invoked - in case fun wants to set
537 s->watchlist[what].fun = 0;
538 s->watchlist[what].data = 0;
539 s->watchlist[what].obs = 0;
541 session_leave(s, "session_alert_watch");
542 session_log(s, YLOG_DEBUG,
543 "Alert Watch: %d calling function: %p", what, fun);
547 session_leave(s,"session_alert_watch");
550 //callback for grep_databases
551 static void select_targets_callback(struct session *se,
552 struct session_database *db)
555 struct client_list *l;
557 for (l = se->clients_cached; l; l = l->next)
558 if (client_get_database(l->client) == db)
565 cl = client_create(db->database->id);
566 client_set_database(cl, db);
568 l = xmalloc(sizeof(*l));
570 l->next = se->clients_cached;
571 se->clients_cached = l;
573 client_set_session(cl, se);
575 l = xmalloc(sizeof(*l));
577 l->next = se->clients_active;
578 se->clients_active = l;
581 static void session_reset_active_clients(struct session *se,
582 struct client_list *new_list)
584 struct client_list *l;
586 session_enter(se, "session_reset_active_clients");
587 l = se->clients_active;
588 se->clients_active = new_list;
589 session_leave(se, "session_reset_active_clients");
593 struct client_list *l_next = l->next;
595 client_lock(l->client);
596 client_set_session(l->client, 0); /* mark client inactive */
597 client_unlock(l->client);
604 static void session_remove_cached_clients(struct session *se)
606 struct client_list *l;
608 session_reset_active_clients(se, 0);
610 session_enter(se, "session_remove_cached_clients");
611 l = se->clients_cached;
612 se->clients_cached = 0;
613 session_leave(se, "session_remove_cached_clients");
617 struct client_list *l_next = l->next;
618 client_lock(l->client);
619 client_set_session(l->client, 0);
620 client_set_database(l->client, 0);
621 client_mark_dead(l->client);
622 client_unlock(l->client);
623 client_destroy(l->client);
629 // Associates a set of clients with a session;
630 // Note: Session-databases represent databases with per-session
632 static int select_targets(struct session *se, const char *filter)
634 return session_grep_databases(se, filter, select_targets_callback);
637 int session_active_clients(struct session *s)
639 struct client_list *l;
642 for (l = s->clients_active; l; l = l->next)
643 if (client_is_active(l->client))
649 int session_is_preferred_clients_ready(struct session *s)
651 struct client_list *l;
654 for (l = s->clients_active; l; l = l->next)
655 if (client_is_active_preferred(l->client))
657 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
661 static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
663 reclist_destroy(se->reclist);
664 if (nmem_total(se->nmem))
665 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd",
666 nmem_total(se->nmem));
667 nmem_reset(se->nmem);
668 se->total_records = se->total_merged = 0;
670 relevance_clear(se->relevance);
672 /* reset list of sorted results and clear to relevance search */
673 se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
674 se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
675 se->sorted_results->increasing = sp->increasing;
676 se->sorted_results->type = sp->type;
677 se->sorted_results->next = 0;
679 session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
680 sp->name, sp->increasing, sp->type);
682 se->reclist = reclist_create(se->nmem);
685 void session_sort(struct session *se, struct reclist_sortparms *sp,
686 const char *mergekey, const char *rank)
688 struct client_list *l;
689 const char *field = sp->name;
690 int increasing = sp->increasing;
692 int clients_research = 0;
694 session_enter(se, "session_sort");
695 session_log(se, YLOG_DEBUG, "session_sort field=%s increasing=%d type=%d",
696 field, increasing, type);
698 if (rank && (!se->rank || strcmp(se->rank, rank)))
700 /* new rank must research/reingest anyway */
703 se->rank = *rank ? xstrdup(rank) : 0;
704 clients_research = 1;
705 session_log(se, YLOG_DEBUG, "session_sort: new rank = %s",
708 if (mergekey && (!se->mergekey || strcmp(se->mergekey, mergekey)))
710 /* new mergekey must research/reingest anyway */
713 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
714 clients_research = 1;
715 session_log(se, YLOG_DEBUG, "session_sort: new mergekey = %s",
718 if (clients_research == 0)
720 struct reclist_sortparms *sr;
721 for (sr = se->sorted_results; sr; sr = sr->next)
722 if (!reclist_sortparms_cmp(sr, sp))
726 session_log(se, YLOG_LOG, "session_sort: field=%s increasing=%d type=%d already fetched",
727 field, increasing, type);
728 session_leave(se, "session_sort");
732 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d must fetch",
733 field, increasing, type);
735 // We need to reset reclist on every sort that changes the records, not just for position
736 // So if just one client requires new searching, we need to clear set.
737 // Ask each of the client if sorting requires re-search due to native sort
738 // If it does it will require us to
739 for (l = se->clients_active; l; l = l->next)
741 struct client *cl = l->client;
742 // Assume no re-search is required.
743 client_parse_init(cl, 1);
744 clients_research += client_parse_sort(cl, sp, 0);
746 if (!clients_research || se->clients_starting)
748 // A new sorting based on same record set
749 struct reclist_sortparms *sr = nmem_malloc(se->nmem, sizeof(*sr));
750 sr->name = nmem_strdup(se->nmem, field);
751 sr->increasing = increasing;
753 sr->next = se->sorted_results;
754 se->sorted_results = sr;
755 session_log(se, YLOG_DEBUG, "session_sort: no research/ingesting done");
756 session_leave(se, "session_sort");
760 se->clients_starting = 1;
761 session_log(se, YLOG_DEBUG,
762 "session_sort: reset results due to %d clients researching",
764 session_clear_set(se, sp);
765 session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
767 session_leave(se, "session_sort");
768 for (l = se->clients_active; l; l = l->next)
770 struct client *cl = l->client;
771 if (client_get_state(cl) == Client_Connecting ||
772 client_get_state(cl) == Client_Idle ||
773 client_get_state(cl) == Client_Working) {
774 client_start_search(cl);
778 session_log(se, YLOG_DEBUG,
779 "session_sort: %s: No re-start/ingest in show. "
780 "Wrong client state: %d",
781 client_get_id(cl), client_get_state(cl));
784 session_enter(se, "session_sort");
785 se->clients_starting = 0;
786 se->force_position = 0;
787 session_leave(se, "session_sort");
791 void session_stop(struct session *se)
793 struct client_list *l;
794 session_enter(se, "session_stop1");
795 if (se->clients_starting)
797 session_leave(se, "session_stop1");
800 se->clients_starting = 1;
801 session_leave(se, "session_stop1");
803 session_alert_watch(se, SESSION_WATCH_SHOW);
804 session_alert_watch(se, SESSION_WATCH_BYTARGET);
805 session_alert_watch(se, SESSION_WATCH_TERMLIST);
806 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
808 for (l = se->clients_active; l; l = l->next)
810 struct client *cl = l->client;
813 session_enter(se, "session_stop2");
814 se->clients_starting = 0;
815 session_leave(se, "session_stop2");
818 enum pazpar2_error_code session_search(struct session *se,
820 const char *startrecs,
824 const char **addinfo,
825 const char **addinfo2,
826 struct reclist_sortparms *sp,
827 const char *mergekey,
830 int live_channels = 0;
832 int no_failed_query = 0;
833 int no_failed_limit = 0;
835 struct client_list *l;
837 session_log(se, YLOG_LOG, "search query %s", query);
839 session_log(se, YLOG_LOG, "search filter %s", filter);
841 session_log(se, YLOG_LOG, "search limit %s", limit);
845 session_enter(se, "session_search0");
846 if (se->clients_starting)
848 session_leave(se, "session_search0");
849 return PAZPAR2_NO_ERROR;
851 se->clients_starting = 1;
852 se->force_position = 0;
853 session_leave(se, "session_search0");
855 if (se->settings_modified) {
856 session_remove_cached_clients(se);
859 session_reset_active_clients(se, 0);
861 session_enter(se, "session_search");
862 se->settings_modified = 0;
867 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
872 se->rank = *rank ? xstrdup(rank) : 0;
875 session_clear_set(se, sp);
876 relevance_destroy(&se->relevance);
878 live_channels = select_targets(se, filter);
881 session_leave(se, "session_search");
882 se->clients_starting = 0;
883 return PAZPAR2_NO_TARGETS;
886 facet_limits_destroy(se->facet_limits);
887 se->facet_limits = facet_limits_create(limit);
888 if (!se->facet_limits)
891 session_leave(se, "session_search");
892 se->clients_starting = 0;
893 session_reset_active_clients(se, 0);
894 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
897 session_leave(se, "session_search");
899 session_alert_watch(se, SESSION_WATCH_SHOW);
900 session_alert_watch(se, SESSION_WATCH_BYTARGET);
901 session_alert_watch(se, SESSION_WATCH_TERMLIST);
902 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
904 for (l = se->clients_active; l; l = l->next)
907 struct client *cl = l->client;
908 client_parse_init(cl, 1);
909 if (prepare_map(se, client_get_database(cl)) < 0)
912 parse_ret = client_parse_query(cl, query, se->facet_limits, addinfo2);
915 else if (parse_ret == -2)
917 else if (parse_ret < 0)
918 no_working++; /* other error, such as bad CCL map */
921 client_parse_range(cl, startrecs, maxrecs);
922 client_parse_sort(cl, sp, &no_sortmap);
923 client_start_search(cl);
927 session_log(se, YLOG_LOG, "search "
928 "working %d sortmap %d failed-query %d failed-limit %d",
929 no_working, no_sortmap, no_failed_query, no_failed_limit);
930 session_enter(se, "session_search2");
931 if (no_working == 1 && no_sortmap == 1)
933 se->force_position = 1;
934 yaz_log(YLOG_LOG, "force_position=1");
936 se->clients_starting = 0;
937 session_leave(se, "session_search2");
940 if (no_failed_query > 0)
943 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
945 else if (no_failed_limit > 0)
948 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
951 return PAZPAR2_NO_TARGETS;
953 return PAZPAR2_NO_ERROR;
956 // Creates a new session_database object for a database
957 static void session_init_databases_fun(void *context, struct database *db)
959 struct session *se = (struct session *) context;
960 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
966 assert(db->settings);
967 new->settings = nmem_malloc(se->session_nmem,
968 sizeof(struct settings *) * db->num_settings);
969 new->num_settings = db->num_settings;
970 for (i = 0; i < db->num_settings; i++)
972 struct setting *setting = db->settings[i];
973 new->settings[i] = setting;
975 new->next = se->databases;
979 // Doesn't free memory associated with sdb -- nmem takes care of that
980 static void session_database_destroy(struct session_database *sdb)
985 // Initialize session_database list -- this represents this session's view
986 // of the database list -- subject to modification by the settings ws command
987 void session_init_databases(struct session *se)
990 predef_grep_databases(se, se->service, session_init_databases_fun);
993 // Probably session_init_databases_fun should be refactored instead of
995 static struct session_database *load_session_database(struct session *se,
998 struct database *db = new_database_inherit_settings(id, se->session_nmem, se->service->settings);
999 session_init_databases_fun((void*) se, db);
1001 // New sdb is head of se->databases list
1002 return se->databases;
1005 // Find an existing session database. If not found, load it
1006 static struct session_database *find_session_database(struct session *se,
1009 struct session_database *sdb;
1011 for (sdb = se->databases; sdb; sdb = sdb->next)
1012 if (!strcmp(sdb->database->id, id))
1014 return load_session_database(se, id);
1017 // Apply a session override to a database
1018 void session_apply_setting(struct session *se, const char *dbname,
1019 const char *name, const char *value)
1021 session_enter(se, "session_apply_setting");
1023 struct session_database *sdb = find_session_database(se, dbname);
1024 struct conf_service *service = se->service;
1026 int offset = settings_create_offset(service, name);
1028 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
1030 // Force later recompute of settings-driven data structures
1031 // (happens when a search starts and client connections are prepared)
1032 if (offset == PZ_XSLT)
1034 se->settings_modified = 1;
1035 for (s = sdb->settings[offset]; s; s = s->next)
1036 if (!strcmp(s->name, name) &&
1037 dbname && s->target && !strcmp(dbname, s->target))
1041 s = nmem_malloc(se->session_nmem, sizeof(*s));
1043 s->target = nmem_strdup(se->session_nmem, dbname);
1044 s->name = nmem_strdup(se->session_nmem, name);
1045 s->next = sdb->settings[offset];
1046 sdb->settings[offset] = s;
1048 s->value = nmem_strdup(se->session_nmem, value);
1050 session_leave(se, "session_apply_setting");
1053 void session_destroy(struct session *se)
1055 struct session_database *sdb;
1057 int sessions_total = session_use(-1);
1058 int no_facet_ids = 0;
1060 for (t = se->facet_id_list; t; t = t->next)
1062 session_log(se, YLOG_LOG, "destroy "
1063 "session-total %d nmem-op %zd nmem-ses %zd facets-ids %d",
1065 nmem_total(se->nmem), nmem_total(se->session_nmem),
1067 session_remove_cached_clients(se);
1069 for (sdb = se->databases; sdb; sdb = sdb->next)
1070 session_database_destroy(sdb);
1071 normalize_cache_destroy(se->normalize_cache);
1072 relevance_destroy(&se->relevance);
1073 reclist_destroy(se->reclist);
1074 xfree(se->mergekey);
1076 facet_limits_destroy(se->facet_limits);
1077 nmem_destroy(se->nmem);
1078 service_destroy(se->service);
1079 yaz_mutex_destroy(&se->session_mutex);
1082 size_t session_get_memory_status(struct session *session) {
1083 size_t session_nmem;
1086 session_enter(session, "session_get_memory_status");
1087 session_nmem = nmem_total(session->nmem);
1088 session_leave(session, "session_get_memory_status");
1089 return session_nmem;
1093 struct session *session_create(NMEM nmem, struct conf_service *service,
1094 unsigned session_id)
1097 struct session *session = nmem_malloc(nmem, sizeof(*session));
1101 sprintf(tmp_str, "session#%u", session_id);
1103 session->session_id = session_id;
1104 session_log(session, YLOG_DEBUG, "New");
1105 session->service = service;
1106 session->relevance = 0;
1107 session->total_records = 0;
1108 session->number_of_warnings_unknown_elements = 0;
1109 session->number_of_warnings_unknown_metadata = 0;
1110 session->termlists = 0;
1111 session->reclist = reclist_create(nmem);
1112 session->clients_active = 0;
1113 session->clients_cached = 0;
1114 session->settings_modified = 0;
1115 session->session_nmem = nmem;
1116 session->facet_id_list = 0;
1117 session->nmem = nmem_create();
1118 session->databases = 0;
1119 session->sorted_results = 0;
1120 session->facet_limits = 0;
1121 session->mergekey = 0;
1123 session->clients_starting = 0;
1124 session->force_position = 0;
1126 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1128 session->watchlist[i].data = 0;
1129 session->watchlist[i].fun = 0;
1131 session->normalize_cache = normalize_cache_create();
1132 session->session_mutex = 0;
1133 pazpar2_mutex_create(&session->session_mutex, tmp_str);
1136 session_log(session, YLOG_LOG, "create session-total %d", i);
1140 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
1141 int *count, NMEM nmem)
1143 struct hitsbytarget *res = 0;
1144 struct client_list *l;
1147 for (l = se->clients_active; l; l = l->next)
1150 res = nmem_malloc(nmem, sizeof(*res) * sz);
1152 for (l = se->clients_active; l; l = l->next)
1154 struct client *cl = l->client;
1155 WRBUF w = wrbuf_alloc();
1156 const char *name = session_setting_oneval(client_get_database(cl),
1158 res[*count].id = client_get_id(cl);
1159 res[*count].name = *name ? name : "Unknown";
1160 res[*count].hits = client_get_hits(cl);
1161 res[*count].approximation = client_get_approximation(cl);
1162 res[*count].records = client_get_num_records(cl,
1163 &res[*count].filtered,
1165 res[*count].diagnostic =
1166 client_get_diagnostic(cl, &res[*count].message,
1167 &res[*count].addinfo);
1168 res[*count].state = client_get_state_str(cl);
1169 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1170 session_settings_dump(se, client_get_database(cl), w);
1171 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1173 res[*count].suggestions_xml =
1174 nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1176 res[*count].query_data =
1177 client_get_query(cl, &res[*count].query_type, nmem);
1184 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1186 struct hitsbytarget *p;
1187 session_enter(se, "get_hitsbytarget");
1188 p = hitsbytarget_nb(se, count, nmem);
1189 session_leave(se, "get_hitsbytarget");
1193 // Compares two hitsbytarget nodes by hitcount
1194 static int cmp_ht(const void *p1, const void *p2)
1196 const struct hitsbytarget *h1 = p1;
1197 const struct hitsbytarget *h2 = p2;
1198 return h2->hits - h1->hits;
1201 // Compares two hitsbytarget nodes by hitcount
1202 static int cmp_ht_approx(const void *p1, const void *p2)
1204 const struct hitsbytarget *h1 = p1;
1205 const struct hitsbytarget *h2 = p2;
1206 return h2->approximation - h1->approximation;
1209 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1210 NMEM nmem, int version)
1212 struct hitsbytarget *ht;
1215 ht = hitsbytarget_nb(se, &count, nmem);
1217 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1219 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1220 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1223 // do only print terms which have display names
1225 wrbuf_puts(wrbuf, "<term>\n");
1227 wrbuf_puts(wrbuf, "<id>");
1228 wrbuf_xmlputs(wrbuf, ht[i].id);
1229 wrbuf_puts(wrbuf, "</id>\n");
1231 wrbuf_puts(wrbuf, "<name>");
1232 if (!ht[i].name || !ht[i].name[0])
1233 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1235 wrbuf_xmlputs(wrbuf, ht[i].name);
1236 wrbuf_puts(wrbuf, "</name>\n");
1238 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1242 // Should not print if we know it isn't a approximation.
1243 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1244 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1245 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1248 wrbuf_puts(wrbuf, "<state>");
1249 wrbuf_xmlputs(wrbuf, ht[i].state);
1250 wrbuf_puts(wrbuf, "</state>\n");
1252 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1254 wrbuf_puts(wrbuf, "</term>\n");
1259 void perform_termlist(struct http_channel *c, struct session *se,
1260 const char *name, int num, int version)
1263 NMEM nmem_tmp = nmem_create();
1270 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1272 session_enter(se, "perform_termlist");
1274 for (j = 0; j < num_names; j++)
1277 int must_generate_empty = 1; /* bug 5350 */
1279 struct named_termlist *t = se->termlists;
1280 for (; t; t = t->next)
1283 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1285 struct termlist_score **p = 0;
1288 wrbuf_puts(c->wrbuf, "<list name=\"");
1289 wrbuf_xmlputs(c->wrbuf, tname);
1290 wrbuf_puts(c->wrbuf, "\">\n");
1291 must_generate_empty = 0;
1293 p = termlist_highscore(t->termlist, &len, nmem_tmp);
1297 for (i = 0; i < len && i < num; i++)
1299 // prevent sending empty term elements
1300 if (!p[i]->display_term || !p[i]->display_term[0])
1303 wrbuf_puts(c->wrbuf, "<term>");
1304 wrbuf_puts(c->wrbuf, "<name>");
1305 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1306 wrbuf_puts(c->wrbuf, "</name>");
1307 wrbuf_printf(c->wrbuf,
1308 "<frequency>%d</frequency>",
1310 wrbuf_puts(c->wrbuf, "</term>\n");
1313 wrbuf_puts(c->wrbuf, "</list>\n");
1317 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1319 wrbuf_puts(c->wrbuf, "<list name=\"");
1320 wrbuf_xmlputs(c->wrbuf, tname);
1321 wrbuf_puts(c->wrbuf, "\">\n");
1323 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1324 wrbuf_puts(c->wrbuf, "</list>\n");
1325 must_generate_empty = 0;
1327 if (must_generate_empty)
1329 wrbuf_puts(c->wrbuf, "<list name=\"");
1330 wrbuf_xmlputs(c->wrbuf, names[j]);
1331 wrbuf_puts(c->wrbuf, "\"/>\n");
1334 session_leave(se, "perform_termlist");
1335 nmem_destroy(nmem_tmp);
1338 #ifdef MISSING_HEADERS
1339 void report_nmem_stats(void)
1341 size_t in_use, is_free;
1343 nmem_get_memory_in_use(&in_use);
1344 nmem_get_memory_free(&is_free);
1346 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1347 (long) in_use, (long) is_free);
1351 struct record_cluster *show_single_start(struct session *se, const char *id,
1352 struct record_cluster **prev_r,
1353 struct record_cluster **next_r)
1355 struct record_cluster *r = 0;
1357 session_enter(se, "show_single_start");
1360 reclist_limit(se->reclist, se, 1);
1362 reclist_enter(se->reclist);
1363 while ((r = reclist_read_record(se->reclist)))
1365 if (!strcmp(r->recid, id))
1367 *next_r = reclist_read_record(se->reclist);
1372 reclist_leave(se->reclist);
1374 session_leave(se, "show_single_start");
1378 void show_single_stop(struct session *se, struct record_cluster *rec)
1380 session_leave(se, "show_single_stop");
1384 int session_fetch_more(struct session *se)
1386 struct client_list *l;
1389 for (l = se->clients_active; l; l = l->next)
1391 struct client *cl = l->client;
1392 if (client_get_state(cl) == Client_Idle)
1394 if (client_fetch_more(cl))
1396 session_log(se, YLOG_LOG, "%s: more to fetch",
1403 int ingest_failures;
1404 int record_failures;
1405 int num = client_get_num_records(
1406 cl, &filtered, &ingest_failures, &record_failures);
1408 session_log(se, YLOG_LOG, "%s: hits=" ODR_INT_PRINTF
1409 " fetched=%d filtered=%d",
1411 client_get_hits(cl),
1413 if (ingest_failures || record_failures)
1415 session_log(se, YLOG_WARN, "%s:"
1416 " ingest failures=%d record failures=%d",
1418 ingest_failures, record_failures);
1424 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1425 client_get_id(cl), client_get_state_str(cl));
1432 struct record_cluster **show_range_start(struct session *se,
1433 struct reclist_sortparms *sp,
1434 int start, int *num, int *total,
1435 Odr_int *sumhits, Odr_int *approx_hits,
1436 void (*show_records_ready)(void *data),
1437 struct http_channel *chan)
1439 struct record_cluster **recs = 0;
1440 struct reclist_sortparms *spp;
1441 struct client_list *l;
1445 yaz_timing_t t = yaz_timing_create();
1447 session_enter(se, "show_range_start");
1451 reclist_limit(se->reclist, se, 0);
1454 for (spp = sp; spp; spp = spp->next)
1455 if (spp->type == Metadata_type_relevance)
1457 relevance_prepare_read(se->relevance, se->reclist);
1460 for (l = se->clients_active; l; l = l->next) {
1461 *sumhits += client_get_hits(l->client);
1462 *approx_hits += client_get_approximation(l->client);
1465 if (se->force_position)
1467 nmem_tmp = nmem_create();
1468 sp = reclist_parse_sortparms(nmem_tmp, "position:1", 0);
1471 reclist_sort(se->reclist, sp);
1473 nmem_destroy(nmem_tmp);
1475 reclist_enter(se->reclist);
1476 *total = reclist_get_num_records(se->reclist);
1478 for (l = se->clients_active; l; l = l->next)
1479 client_update_show_stat(l->client, 0);
1481 for (i = 0; i < start; i++)
1483 struct record_cluster *r = reclist_read_record(se->reclist);
1491 struct record *rec = r->records;
1492 for (;rec; rec = rec->next)
1493 client_update_show_stat(rec->client, 1);
1496 recs = nmem_malloc(se->nmem, (*num > 0 ? *num : 1) * sizeof(*recs));
1497 for (i = 0; i < *num; i++)
1499 struct record_cluster *r = reclist_read_record(se->reclist);
1507 struct record *rec = r->records;
1508 for (;rec; rec = rec->next)
1509 client_update_show_stat(rec->client, 1);
1513 reclist_leave(se->reclist);
1516 session_log(se, YLOG_LOG, "show %6.5f %3.2f %3.2f",
1517 yaz_timing_get_real(t), yaz_timing_get_user(t),
1518 yaz_timing_get_sys(t));
1519 yaz_timing_destroy(&t);
1522 if (!session_fetch_more(se))
1523 session_log(se, YLOG_LOG, "can not fetch more");
1526 show_range_stop(se, recs);
1527 session_log(se, YLOG_LOG, "fetching more in progress");
1528 if (session_set_watch(se, SESSION_WATCH_SHOW,
1529 show_records_ready, chan, chan))
1531 session_log(se, YLOG_WARN, "Ignoring show block");
1532 session_enter(se, "show_range_start");
1536 session_log(se, YLOG_LOG, "session watch OK");
1543 void show_range_stop(struct session *se, struct record_cluster **recs)
1545 session_leave(se, "show_range_stop");
1548 void statistics(struct session *se, struct statistics *stat)
1550 struct client_list *l;
1553 memset(stat, 0, sizeof(*stat));
1555 for (l = se->clients_active; l; l = l->next)
1557 struct client *cl = l->client;
1558 if (!client_get_connection(cl))
1559 stat->num_no_connection++;
1560 stat->num_hits += client_get_hits(cl);
1561 switch (client_get_state(cl))
1563 case Client_Connecting: stat->num_connecting++; break;
1564 case Client_Working: stat->num_working++; break;
1565 case Client_Idle: stat->num_idle++; break;
1566 case Client_Failed: stat->num_failed++; break;
1567 case Client_Error: stat->num_error++; break;
1572 stat->num_records = se->total_records;
1574 stat->num_clients = count;
1577 static struct record_metadata *record_metadata_init(
1578 NMEM nmem, const char *value, const char *norm,
1579 enum conf_metadata_type type,
1580 struct _xmlAttr *attr)
1582 struct record_metadata *rec_md = record_metadata_create(nmem);
1583 struct record_metadata_attr **attrp = &rec_md->attributes;
1585 for (; attr; attr = attr->next)
1587 if (attr->children && attr->children->content)
1589 if (strcmp((const char *) attr->name, "type")
1590 && strcmp((const char *) attr->name, "empty"))
1591 { /* skip the "type" + "empty" attribute..
1592 The "Type" is already part of the element in output
1593 (md-%s) and so repeating it here is redundant */
1594 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1596 nmem_strdup(nmem, (const char *) attr->name);
1598 nmem_strdup(nmem, (const char *) attr->children->content);
1599 attrp = &(*attrp)->next;
1607 case Metadata_type_generic:
1608 case Metadata_type_skiparticle:
1611 rec_md->data.text.disp = nmem_strdup(nmem, value);
1612 rec_md->data.text.norm = nmem_strdup(nmem, norm);
1616 if (strstr(value, "://")) /* looks like a URL */
1617 rec_md->data.text.disp = nmem_strdup(nmem, value);
1619 rec_md->data.text.disp =
1620 normalize7bit_generic(nmem_strdup(nmem, value), " ,/.:([");
1621 rec_md->data.text.norm = rec_md->data.text.disp;
1623 rec_md->data.text.sort = 0;
1624 rec_md->data.text.snippet = 0;
1626 case Metadata_type_year:
1627 case Metadata_type_date:
1632 if (type == Metadata_type_date)
1634 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1637 rec_md->data.number.min = first;
1638 rec_md->data.number.max = last;
1641 case Metadata_type_float:
1642 rec_md->data.fnumber = atof(value);
1644 case Metadata_type_relevance:
1645 case Metadata_type_position:
1646 case Metadata_type_retrieval:
1652 static void mergekey_norm_wr(pp2_charset_fact_t charsets,
1653 WRBUF norm_wr, const char *value)
1655 const char *norm_str;
1656 pp2_charset_token_t prt =
1657 pp2_charset_token_create(charsets, "mergekey");
1659 pp2_charset_token_first(prt, value, 0);
1660 while ((norm_str = pp2_charset_token_next(prt)))
1664 if (wrbuf_len(norm_wr))
1665 wrbuf_puts(norm_wr, " ");
1666 wrbuf_puts(norm_wr, norm_str);
1669 pp2_charset_token_destroy(prt);
1672 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1673 struct conf_service *service, WRBUF norm_wr)
1677 for (n = root->children; n; n = n->next)
1679 if (n->type != XML_ELEMENT_NODE)
1681 if (!strcmp((const char *) n->name, "metadata"))
1683 const char *type = yaz_xml_get_prop(n, "type");
1685 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1687 else if (!strcmp(name, (const char *) type))
1689 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1690 if (value && *value)
1692 if (wrbuf_len(norm_wr) > 0)
1693 wrbuf_puts(norm_wr, " ");
1694 wrbuf_puts(norm_wr, name);
1695 mergekey_norm_wr(service->charsets, norm_wr,
1696 (const char *) value);
1707 static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
1708 struct client *cl, int record_no,
1709 struct conf_service *service, NMEM nmem,
1710 const char *session_mergekey)
1712 char *mergekey_norm = 0;
1713 WRBUF norm_wr = wrbuf_alloc();
1714 const char *mergekey;
1716 if (session_mergekey)
1720 nmem_strsplit_escape2(nmem, ",", session_mergekey, &values,
1723 for (i = 0; i < num; i++)
1724 get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
1726 else if ((mergekey = yaz_xml_get_prop(root, "mergekey")))
1728 mergekey_norm_wr(service->charsets, norm_wr, mergekey);
1732 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1734 for (field_id = 0; field_id < service->num_metadata; field_id++)
1736 struct conf_metadata *ser_md = &service->metadata[field_id];
1737 if (ser_md->mergekey != Metadata_mergekey_no)
1739 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1741 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1743 /* no mergekey on this one and it is required..
1744 Generate unique key instead */
1745 wrbuf_rewind(norm_wr);
1752 /* generate unique key if none is not generated already or is empty */
1753 if (wrbuf_len(norm_wr) == 0)
1755 wrbuf_printf(norm_wr, "position: %s-%06d",
1756 client_get_id(cl), record_no);
1760 const char *lead = "content: ";
1761 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1763 if (wrbuf_len(norm_wr) > 0)
1764 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1765 wrbuf_destroy(norm_wr);
1766 return mergekey_norm;
1769 /** \brief see if metadata for pz:recordfilter exists
1770 \param root xml root element of normalized record
1771 \param sdb session database for client
1772 \retval 0 if there is no metadata for pz:recordfilter
1773 \retval 1 if there is metadata for pz:recordfilter
1775 If there is no pz:recordfilter defined, this function returns 1
1779 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1784 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1789 for (n = root->children; n; n = n->next)
1791 if (n->type != XML_ELEMENT_NODE)
1793 if (!strcmp((const char *) n->name, "metadata"))
1795 const char *type = yaz_xml_get_prop(n, "type");
1802 if ((eq = strchr(s, '=')))
1804 else if ((eq = strchr(s, '~')))
1810 if (len == strlen((const char *)type) &&
1811 !memcmp((const char *) type, s, len))
1813 xmlChar *value = xmlNodeGetContent(n);
1814 if (value && *value)
1817 (substring && strstr((const char *) value, eq+1)) ||
1818 (!substring && !strcmp((const char *) value, eq + 1)))
1829 static int ingest_to_cluster(struct client *cl,
1835 struct record_metadata_attr *mergekey);
1837 static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
1838 int record_no, NMEM nmem,
1839 struct session_database *sdb,
1840 struct record_metadata_attr *mergekeys)
1843 struct session *se = client_get_session(cl);
1844 WRBUF wrbuf_disp, wrbuf_norm;
1846 if (!check_record_filter(root, sdb))
1848 session_log(se, YLOG_LOG,
1849 "Filtered out record no %d from %s",
1850 record_no, sdb->database->id);
1853 wrbuf_disp = wrbuf_alloc();
1854 wrbuf_norm = wrbuf_alloc();
1855 session_enter(se, "ingest_sub_record");
1856 if (client_get_session(cl) == se && se->relevance)
1857 ret = ingest_to_cluster(cl, wrbuf_disp, wrbuf_norm,
1858 xdoc, root, record_no, mergekeys);
1859 session_leave(se, "ingest_sub_record");
1860 wrbuf_destroy(wrbuf_norm);
1861 wrbuf_destroy(wrbuf_disp);
1865 /** \brief ingest XML record
1866 \param cl client holds the result set for record
1867 \param rec record buffer (0 terminated)
1868 \param record_no record position (1, 2, ..)
1869 \param nmem working NMEM
1874 int ingest_record(struct client *cl, const char *rec,
1875 int record_no, NMEM nmem)
1877 struct session *se = client_get_session(cl);
1878 struct session_database *sdb = client_get_database(cl);
1879 struct conf_service *service = se->service;
1880 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1881 int r = ingest_xml_record(cl, xdoc, record_no, nmem, 0);
1882 client_store_xdoc(cl, record_no, xdoc);
1886 int ingest_xml_record(struct client *cl, xmlDoc *xdoc,
1887 int record_no, NMEM nmem, int cached_copy)
1889 struct session *se = client_get_session(cl);
1890 struct session_database *sdb = client_get_database(cl);
1891 struct conf_service *service = se->service;
1897 if (global_parameters.dump_records)
1899 session_log(se, YLOG_LOG, "Normalized record from %s",
1904 root = xmlDocGetRootElement(xdoc);
1906 if (!strcmp((const char *) root->name, "cluster"))
1908 int no_merge_keys = 0;
1909 int no_merge_dups = 0;
1911 struct record_metadata_attr *mk = 0;
1913 for (sroot = root->children; sroot; sroot = sroot->next)
1914 if (sroot->type == XML_ELEMENT_NODE &&
1915 !strcmp((const char *) sroot->name, "record"))
1917 struct record_metadata_attr **mkp;
1918 const char *mergekey_norm =
1919 get_mergekey(xdoc, sroot, cl, record_no, service, nmem,
1926 for (mkp = &mk; *mkp; mkp = &(*mkp)->next)
1927 if (!strcmp((*mkp)->value, mergekey_norm))
1931 *mkp = (struct record_metadata_attr*)
1932 nmem_malloc(nmem, sizeof(**mkp));
1934 (*mkp)->value = nmem_strdup(nmem, mergekey_norm);
1941 if (no_merge_keys > 1 || no_merge_dups > 0)
1943 yaz_log(YLOG_LOG, "Got %d mergekeys, %d dups for position %d",
1944 no_merge_keys, no_merge_dups, record_no);
1946 for (sroot = root->children; !r && sroot; sroot = sroot->next)
1947 if (sroot->type == XML_ELEMENT_NODE &&
1948 !strcmp((const char *) sroot->name, "record"))
1951 insert_settings_values(sdb, xdoc, root, service);
1952 r = ingest_sub_record(cl, xdoc, sroot, record_no, nmem, sdb,
1956 else if (!strcmp((const char *) root->name, "record"))
1958 const char *mergekey_norm =
1959 get_mergekey(xdoc, root, cl, record_no, service, nmem,
1963 struct record_metadata_attr *mk = (struct record_metadata_attr*)
1964 nmem_malloc(nmem, sizeof(*mk));
1966 mk->value = nmem_strdup(nmem, mergekey_norm);
1970 insert_settings_values(sdb, xdoc, root, service);
1971 r = ingest_sub_record(cl, xdoc, root, record_no, nmem, sdb, mk);
1976 session_log(se, YLOG_WARN, "Bad pz root element: %s",
1977 (const char *) root->name);
1984 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1985 // struct record_metadata *rec_md = record->metadata[md_field_id];
1986 static int match_metadata_local(struct conf_service *service,
1987 struct conf_metadata *ser_md,
1988 struct record_metadata *rec_md0,
1989 char **values, int num_v)
1992 struct record_metadata *rec_md = rec_md0;
1994 WRBUF text_wr = wrbuf_alloc();
1995 for (i = 0; i < num_v; )
1999 if (ser_md->type == Metadata_type_year
2000 || ser_md->type == Metadata_type_date)
2002 int y = atoi(values[i]);
2003 if (y >= rec_md->data.number.min
2004 && y <= rec_md->data.number.max)
2011 val_wr = wrbuf_alloc();
2012 mergekey_norm_wr(service->charsets, val_wr, values[i]);
2014 wrbuf_rewind(text_wr);
2015 mergekey_norm_wr(service->charsets, text_wr,
2016 rec_md->data.text.disp);
2017 if (!strcmp(wrbuf_cstr(val_wr), wrbuf_cstr(text_wr)))
2020 rec_md = rec_md->next;
2025 wrbuf_destroy(val_wr);
2030 wrbuf_destroy(val_wr);
2031 wrbuf_destroy(text_wr);
2032 return i < num_v ? 1 : 0;
2035 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
2038 struct conf_service *service = se->service;
2042 NMEM nmem_tmp = nmem_create();
2044 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
2047 for (j = 0; j < service->num_metadata; j++)
2049 struct conf_metadata *md = service->metadata + j;
2050 if (!strcmp(md->name, name) && md->limitcluster)
2055 conf_service_metadata_field_id(service,
2058 if (md_field_id < 0)
2064 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
2067 if (!match_metadata_local(service,
2068 &service->metadata[md_field_id],
2069 rec->metadata[md_field_id],
2078 nmem_destroy(nmem_tmp);
2082 // Skip record on non-zero
2083 static int check_limit_local(struct client *cl,
2084 struct record *record,
2087 int skip_record = 0;
2088 struct session *se = client_get_session(cl);
2089 struct conf_service *service = se->service;
2090 NMEM nmem_tmp = nmem_create();
2091 struct session_database *sdb = client_get_database(cl);
2093 while (!skip_record)
2099 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
2104 if (!strcmp(name, "*"))
2106 for (md_field_id = 0; md_field_id < service->num_metadata;
2109 if (match_metadata_local(
2111 &service->metadata[md_field_id],
2112 record->metadata[md_field_id],
2116 if (md_field_id == service->num_metadata)
2121 md_field_id = conf_service_metadata_field_id(service, name);
2122 if (md_field_id < 0)
2127 if (!match_metadata_local(
2129 &service->metadata[md_field_id],
2130 record->metadata[md_field_id],
2137 nmem_destroy(nmem_tmp);
2141 static int ingest_to_cluster(struct client *cl,
2147 struct record_metadata_attr *merge_keys)
2150 struct session *se = client_get_session(cl);
2151 struct conf_service *service = se->service;
2152 int term_factor = 1;
2153 struct record_cluster *cluster;
2154 struct record_metadata **metadata0;
2155 struct session_database *sdb = client_get_database(cl);
2156 NMEM ingest_nmem = 0;
2157 char **rank_values = 0;
2159 struct record *record = record_create(se->nmem,
2160 service->num_metadata,
2161 service->num_sortkeys, cl,
2164 for (n = root->children; n; n = n->next)
2166 if (n->type != XML_ELEMENT_NODE)
2168 if (!strcmp((const char *) n->name, "metadata"))
2170 struct conf_metadata *ser_md = 0;
2171 struct record_metadata **wheretoput = 0;
2172 struct record_metadata *rec_md = 0;
2173 int md_field_id = -1;
2175 const char *type = yaz_xml_get_prop(n, "type");
2181 = conf_service_metadata_field_id(service, (const char *) type);
2182 if (md_field_id < 0)
2184 if (se->number_of_warnings_unknown_metadata == 0)
2186 session_log(se, YLOG_WARN,
2187 "Ignoring unknown metadata element: %s", type);
2189 se->number_of_warnings_unknown_metadata++;
2193 wrbuf_rewind(wrbuf_disp);
2194 value0 = xmlNodeListGetString(xdoc, n->children, 1);
2195 if (!value0 || !*value0)
2197 const char *empty = yaz_xml_get_prop(n, "empty");
2202 wrbuf_puts(wrbuf_disp, (const char *) empty);
2206 wrbuf_puts(wrbuf_disp, (const char *) value0);
2209 ser_md = &service->metadata[md_field_id];
2211 // non-merged metadata
2212 rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp), 0,
2213 ser_md->type, n->properties);
2216 session_log(se, YLOG_WARN, "bad metadata data '%s' "
2217 "for element '%s'", wrbuf_cstr(wrbuf_disp), type);
2221 if (ser_md->type == Metadata_type_generic)
2223 WRBUF w = wrbuf_alloc();
2224 if (relevance_snippet(se->relevance,
2225 wrbuf_cstr(wrbuf_disp), ser_md->name, w))
2226 rec_md->data.text.snippet = nmem_strdup(se->nmem,
2232 wheretoput = &record->metadata[md_field_id];
2234 wheretoput = &(*wheretoput)->next;
2235 *wheretoput = rec_md;
2239 if (check_limit_local(cl, record, record_no))
2243 cluster = reclist_insert(se->reclist, se->relevance, service, record,
2244 merge_keys, &se->total_merged);
2247 return 0; // complete match with existing record
2251 const char *use_term_factor_str =
2252 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
2253 if (use_term_factor_str && use_term_factor_str[0] == '1')
2255 int maxrecs = client_get_maxrecs(cl);
2256 int hits = (int) client_get_hits(cl);
2257 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
2258 assert(term_factor >= 1);
2259 session_log(se, YLOG_DEBUG, "Using term factor: %d (%d / %d)",
2260 term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
2264 if (global_parameters.dump_records)
2265 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
2266 sdb->database->id, record_no);
2268 // original metadata, to check if first existence of a field
2269 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
2270 memcpy(metadata0, cluster->metadata,
2271 sizeof(*metadata0) * service->num_metadata);
2273 ingest_nmem = nmem_create();
2276 yaz_log(YLOG_LOG, "local in sort : %s", se->rank);
2277 nmem_strsplit_escape2(ingest_nmem, ",", se->rank, &rank_values,
2278 &rank_num, 1, '\\', 1);
2281 // now parsing XML record and adding data to cluster or record metadata
2282 for (n = root->children; n; n = n->next)
2284 if (n->type != XML_ELEMENT_NODE)
2286 if (!strcmp((const char *) n->name, "metadata"))
2288 struct conf_metadata *ser_md = 0;
2289 struct conf_sortkey *ser_sk = 0;
2290 struct record_metadata **wheretoput = 0;
2291 struct record_metadata *rec_md = 0;
2292 int md_field_id = -1;
2293 int sk_field_id = -1;
2294 const char *rank = 0;
2295 const char *xml_rank = 0;
2296 const char *type = 0;
2299 type = yaz_xml_get_prop(n, "type");
2304 = conf_service_metadata_field_id(service, (const char *) type);
2305 if (md_field_id < 0)
2308 ser_md = &service->metadata[md_field_id];
2310 if (ser_md->sortkey_offset >= 0)
2312 sk_field_id = ser_md->sortkey_offset;
2313 ser_sk = &service->sortkeys[sk_field_id];
2316 wrbuf_rewind(wrbuf_disp);
2317 wrbuf_rewind(wrbuf_norm);
2319 value0 = xmlNodeListGetString(xdoc, n->children, 1);
2320 if (!value0 || !*value0)
2327 if (ser_md->icurule)
2329 run_icu(se, ser_md->icurule, (const char *) value0,
2330 wrbuf_norm, wrbuf_disp);
2331 yaz_log(YLOG_LOG, "run_icu input=%s norm=%s disp=%s",
2332 (const char *) value0,
2333 wrbuf_cstr(wrbuf_norm), wrbuf_cstr(wrbuf_disp));
2334 rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp),
2335 wrbuf_cstr(wrbuf_norm),
2340 wrbuf_puts(wrbuf_disp, (const char *) value0);
2341 rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp),
2348 // see if the field was not in cluster already (from beginning)
2355 for (i = 0; i < rank_num; i++)
2357 const char *val = rank_values[i];
2358 const char *cp = strchr(val, '=');
2361 if ((cp - val) == strlen((const char *) type)
2362 && !memcmp(val, type, cp - val))
2371 xml_rank = yaz_xml_get_prop(n, "rank");
2372 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
2375 wheretoput = &cluster->metadata[md_field_id];
2377 if (ser_md->merge == Metadata_merge_first)
2379 if (!metadata0[md_field_id])
2382 wheretoput = &(*wheretoput)->next;
2383 *wheretoput = rec_md;
2386 else if (ser_md->merge == Metadata_merge_unique)
2390 if (!strcmp((const char *) (*wheretoput)->data.text.norm,
2391 rec_md->data.text.norm))
2393 wheretoput = &(*wheretoput)->next;
2396 *wheretoput = rec_md;
2398 else if (ser_md->merge == Metadata_merge_longest)
2401 || strlen(rec_md->data.text.norm)
2402 > strlen((*wheretoput)->data.text.norm))
2404 *wheretoput = rec_md;
2407 pp2_charset_token_t prt;
2408 const char *sort_str = 0;
2410 ser_sk->type == Metadata_type_skiparticle;
2412 if (!cluster->sortkeys[sk_field_id])
2413 cluster->sortkeys[sk_field_id] =
2414 nmem_malloc(se->nmem,
2415 sizeof(union data_types));
2418 pp2_charset_token_create(service->charsets, "sort");
2420 pp2_charset_token_first(prt, rec_md->data.text.disp,
2423 pp2_charset_token_next(prt);
2425 sort_str = pp2_get_sort(prt);
2427 cluster->sortkeys[sk_field_id]->text.disp =
2428 rec_md->data.text.disp;
2431 sort_str = rec_md->data.text.disp;
2432 session_log(se, YLOG_WARN,
2433 "Could not make sortkey. Bug #1858");
2435 cluster->sortkeys[sk_field_id]->text.sort =
2436 nmem_strdup(se->nmem, sort_str);
2437 pp2_charset_token_destroy(prt);
2441 else if (ser_md->merge == Metadata_merge_all)
2444 wheretoput = &(*wheretoput)->next;
2445 *wheretoput = rec_md;
2447 else if (ser_md->merge == Metadata_merge_range)
2451 *wheretoput = rec_md;
2453 cluster->sortkeys[sk_field_id]
2458 int this_min = rec_md->data.number.min;
2459 int this_max = rec_md->data.number.max;
2460 if (this_min < (*wheretoput)->data.number.min)
2461 (*wheretoput)->data.number.min = this_min;
2462 if (this_max > (*wheretoput)->data.number.max)
2463 (*wheretoput)->data.number.max = this_max;
2467 // ranking of _all_ fields enabled ...
2470 relevance_countwords(se->relevance, cluster,
2471 wrbuf_cstr(wrbuf_disp),
2472 rank, ser_md->name);
2474 // construct facets ... unless the client already has reported them
2475 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2477 if (ser_md->type == Metadata_type_year)
2480 sprintf(year, "%d", rec_md->data.number.max);
2482 add_facet(se, (char *) type, year, term_factor, cl);
2483 if (rec_md->data.number.max != rec_md->data.number.min)
2485 sprintf(year, "%d", rec_md->data.number.min);
2486 add_facet(se, (char *) type, year, term_factor, cl);
2490 add_facet(se, type, wrbuf_cstr(wrbuf_disp), term_factor, cl);
2495 if (se->number_of_warnings_unknown_elements == 0)
2496 session_log(se, YLOG_WARN,
2497 "Unexpected element in internal record: %s", n->name);
2498 se->number_of_warnings_unknown_elements++;
2501 nmem_destroy(ingest_nmem);
2503 relevance_donerecord(se->relevance, cluster);
2504 se->total_records++;
2509 void session_log(struct session *s, int level, const char *fmt, ...)
2515 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2516 yaz_log(level, "Session %u: %s", s ? s->session_id : 0, buf);
2524 * c-file-style: "Stroustrup"
2525 * indent-tabs-mode: nil
2527 * vim: shiftwidth=4 tabstop=8 expandtab