1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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>
62 #include <yaz/timing.h>
66 #include "parameters.h"
70 #include "termlists.h"
72 #include "relevance.h"
76 #include "normalize7bit.h"
78 #include <libxml/tree.h>
82 #define MAX(a,b) ((a)>(b)?(a):(b))
84 // Note: Some things in this structure will eventually move to configuration
85 struct parameters global_parameters =
89 0, // predictable sessions
93 struct client *client;
94 struct client_list *next;
97 /* session counting (1) , disable client counting (0) */
98 static YAZ_MUTEX g_session_mutex = 0;
99 static int no_sessions = 0;
100 static int no_session_total = 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;
110 no_session_total += delta;
111 sessions = no_sessions;
112 yaz_mutex_leave(g_session_mutex);
113 yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), no_sessions);
117 int sessions_count(void)
119 return session_use(0);
122 int session_count_total(void)
125 if (!g_session_mutex)
127 yaz_mutex_enter(g_session_mutex);
128 total = no_session_total;
129 yaz_mutex_leave(g_session_mutex);
133 static void log_xml_doc(xmlDoc *doc)
135 FILE *lf = yaz_log_file();
138 #if LIBXML_VERSION >= 20600
139 xmlDocDumpFormatMemory(doc, &result, &len, 1);
141 xmlDocDumpMemory(doc, &result, &len);
145 (void) fwrite(result, 1, len, lf);
151 static void session_enter(struct session *s, const char *caller)
154 session_log(s, YLOG_DEBUG, "Session lock by %s", caller);
155 yaz_mutex_enter(s->session_mutex);
158 static void session_leave(struct session *s, const char *caller)
160 yaz_mutex_leave(s->session_mutex);
162 session_log(s, YLOG_DEBUG, "Session unlock by %s", caller);
165 static void session_normalize_facet(struct session *s, const char *type,
170 struct conf_service *service = s->service;
171 pp2_charset_token_t prt;
172 const char *facet_component;
174 const char *icu_chain_id = 0;
176 for (i = 0; i < service->num_metadata; i++)
177 if (!strcmp((service->metadata + i)->name, type))
178 icu_chain_id = (service->metadata + i)->facetrule;
180 icu_chain_id = "facet";
181 prt = pp2_charset_token_create(service->charsets, icu_chain_id);
184 yaz_log(YLOG_FATAL, "Unknown ICU chain '%s' for facet of type '%s'",
186 wrbuf_destroy(facet_wrbuf);
187 wrbuf_destroy(display_wrbuf);
190 pp2_charset_token_first(prt, value, 0);
191 while ((facet_component = pp2_charset_token_next(prt)))
193 const char *display_component;
194 if (*facet_component)
196 if (wrbuf_len(facet_wrbuf))
197 wrbuf_puts(facet_wrbuf, " ");
198 wrbuf_puts(facet_wrbuf, facet_component);
200 display_component = pp2_get_display(prt);
201 if (display_component)
203 if (wrbuf_len(display_wrbuf))
204 wrbuf_puts(display_wrbuf, " ");
205 wrbuf_puts(display_wrbuf, display_component);
208 pp2_charset_token_destroy(prt);
211 void add_facet(struct session *s, const char *type, const char *value, int count)
213 WRBUF facet_wrbuf = wrbuf_alloc();
214 WRBUF display_wrbuf = wrbuf_alloc();
216 session_normalize_facet(s, type, value, display_wrbuf, facet_wrbuf);
218 if (wrbuf_len(facet_wrbuf))
221 for (i = 0; i < s->num_termlists; i++)
222 if (!strcmp(s->termlists[i].name, type))
224 if (i == s->num_termlists)
226 if (i == SESSION_MAX_TERMLISTS)
228 session_log(s, YLOG_FATAL, "Too many termlists");
229 wrbuf_destroy(facet_wrbuf);
230 wrbuf_destroy(display_wrbuf);
234 s->termlists[i].name = nmem_strdup(s->nmem, type);
235 s->termlists[i].termlist = termlist_create(s->nmem);
236 s->num_termlists = i + 1;
240 session_log(s, YLOG_LOG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
242 termlist_insert(s->termlists[i].termlist, wrbuf_cstr(display_wrbuf),
243 wrbuf_cstr(facet_wrbuf), count);
245 wrbuf_destroy(facet_wrbuf);
246 wrbuf_destroy(display_wrbuf);
249 static xmlDoc *record_to_xml(struct session *se,
250 struct session_database *sdb, const char *rec)
252 struct database *db = sdb->database;
255 rdoc = xmlParseMemory(rec, strlen(rec));
259 session_log(se, YLOG_WARN, "Non-wellformed XML");
263 if (global_parameters.dump_records)
265 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->id);
272 #define MAX_XSLT_ARGS 16
274 // Add static values from session database settings if applicable
275 static void insert_settings_parameters(struct session_database *sdb,
276 struct conf_service *service,
284 for (i = 0; i < service->num_metadata; i++)
286 struct conf_metadata *md = &service->metadata[i];
289 if (md->setting == Metadata_setting_parameter &&
290 (setting = settings_lookup_offset(service, md->name)) >= 0)
292 const char *val = session_setting_oneval(sdb, setting);
293 if (val && nparms < MAX_XSLT_ARGS)
296 int len = strlen(val);
297 buf = nmem_malloc(nmem, len + 3);
299 strcpy(buf + 1, val);
302 parms[offset++] = md->name;
303 parms[offset++] = buf;
311 // Add static values from session database settings if applicable
312 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
313 struct conf_service *service)
317 for (i = 0; i < service->num_metadata; i++)
319 struct conf_metadata *md = &service->metadata[i];
322 if (md->setting == Metadata_setting_postproc &&
323 (offset = settings_lookup_offset(service, md->name)) >= 0)
325 const char *val = session_setting_oneval(sdb, offset);
328 xmlNode *r = xmlDocGetRootElement(doc);
329 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
331 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
337 static xmlDoc *normalize_record(struct session *se,
338 struct session_database *sdb,
339 struct conf_service *service,
340 const char *rec, NMEM nmem)
342 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
346 char *parms[MAX_XSLT_ARGS*2+1];
348 insert_settings_parameters(sdb, service, parms, nmem);
350 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
352 session_log(se, YLOG_WARN, "Normalize failed");
356 insert_settings_values(sdb, rdoc, service);
358 if (global_parameters.dump_records)
360 session_log(se, YLOG_LOG, "Normalized record from %s",
369 void session_settings_dump(struct session *se,
370 struct session_database *db,
375 int i, num = db->num_settings;
376 for (i = 0; i < num; i++)
378 struct setting *s = db->settings[i];
379 for (;s ; s = s->next)
381 wrbuf_puts(w, "<set name=\"");
382 wrbuf_xmlputs(w, s->name);
383 wrbuf_puts(w, "\" value=\"");
384 wrbuf_xmlputs(w, s->value);
385 wrbuf_puts(w, "\"/>");
393 // Retrieve first defined value for 'name' for given database.
394 // Will be extended to take into account user associated with session
395 const char *session_setting_oneval(struct session_database *db, int offset)
397 if (offset >= db->num_settings || !db->settings[offset])
399 return db->settings[offset]->value;
402 // Prepare XSLT stylesheets for record normalization
403 // Structures are allocated on the session_wide nmem to avoid having
404 // to recompute this for every search. This would lead
405 // to leaking if a single session was to repeatedly change the PZ_XSLT
406 // setting. However, this is not a realistic use scenario.
407 static int prepare_map(struct session *se, struct session_database *sdb)
409 if (sdb->settings && !sdb->map)
413 if (sdb->settings[PZ_XSLT] &&
414 (s = session_setting_oneval(sdb, PZ_XSLT)))
416 char auto_stylesheet[256];
418 if (!strcmp(s, "auto"))
420 const char *request_syntax = session_setting_oneval(
421 sdb, PZ_REQUESTSYNTAX);
425 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
426 "%s.xsl", request_syntax);
427 for (cp = auto_stylesheet; *cp; cp++)
429 /* deliberately only consider ASCII */
430 if (*cp > 32 && *cp < 127)
437 session_log(se, YLOG_WARN,
438 "No pz:requestsyntax for auto stylesheet");
441 sdb->map = normalize_cache_get(se->normalize_cache,
450 // called if watch should be removed because http_channel is to be destroyed
451 static void session_watch_cancel(void *data, struct http_channel *c,
454 struct session_watchentry *ent = data;
461 // set watch. Returns 0=OK, -1 if watch is already set
462 int session_set_watch(struct session *s, int what,
463 session_watchfun fun, void *data,
464 struct http_channel *chan)
467 session_enter(s, "session_set_watch");
468 if (s->watchlist[what].fun)
473 s->watchlist[what].fun = fun;
474 s->watchlist[what].data = data;
475 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
476 session_watch_cancel);
479 session_leave(s, "session_set_watch");
483 void session_alert_watch(struct session *s, int what)
486 session_enter(s, "session_alert_watch");
487 if (s->watchlist[what].fun)
489 /* our watch is no longer associated with http_channel */
491 session_watchfun fun;
493 http_remove_observer(s->watchlist[what].obs);
494 fun = s->watchlist[what].fun;
495 data = s->watchlist[what].data;
497 /* reset watch before fun is invoked - in case fun wants to set
499 s->watchlist[what].fun = 0;
500 s->watchlist[what].data = 0;
501 s->watchlist[what].obs = 0;
503 session_leave(s, "session_alert_watch");
504 session_log(s, YLOG_DEBUG,
505 "Alert Watch: %d calling function: %p", what, fun);
509 session_leave(s,"session_alert_watch");
512 //callback for grep_databases
513 static void select_targets_callback(struct session *se,
514 struct session_database *db)
517 struct client_list *l;
519 for (l = se->clients_cached; l; l = l->next)
520 if (client_get_database(l->client) == db)
527 cl = client_create(db->database->id);
528 client_set_database(cl, db);
530 l = xmalloc(sizeof(*l));
532 l->next = se->clients_cached;
533 se->clients_cached = l;
535 /* set session always. If may be 0 if client is not active */
536 client_set_session(cl, se);
538 l = xmalloc(sizeof(*l));
540 l->next = se->clients_active;
541 se->clients_active = l;
544 static void session_reset_active_clients(struct session *se,
545 struct client_list *new_list)
547 struct client_list *l;
549 session_enter(se, "session_reset_active_clients");
550 l = se->clients_active;
551 se->clients_active = new_list;
552 session_leave(se, "session_reset_active_clients");
556 struct client_list *l_next = l->next;
558 client_lock(l->client);
559 client_set_session(l->client, 0); /* mark client inactive */
560 client_unlock(l->client);
567 static void session_remove_cached_clients(struct session *se)
569 struct client_list *l;
571 session_reset_active_clients(se, 0);
573 session_enter(se, "session_remove_cached_clients");
574 l = se->clients_cached;
575 se->clients_cached = 0;
576 session_leave(se, "session_remove_cached_clients");
580 struct client_list *l_next = l->next;
581 client_lock(l->client);
582 client_set_session(l->client, 0);
583 client_set_database(l->client, 0);
584 client_unlock(l->client);
585 client_destroy(l->client);
591 // Associates a set of clients with a session;
592 // Note: Session-databases represent databases with per-session
594 static int select_targets(struct session *se, const char *filter)
596 return session_grep_databases(se, filter, select_targets_callback);
599 int session_active_clients(struct session *s)
601 struct client_list *l;
604 for (l = s->clients_active; l; l = l->next)
605 if (client_is_active(l->client))
611 int session_is_preferred_clients_ready(struct session *s)
613 struct client_list *l;
616 for (l = s->clients_active; l; l = l->next)
617 if (client_is_active_preferred(l->client))
619 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
623 static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
625 reclist_destroy(se->reclist);
627 if (nmem_total(se->nmem))
628 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd",
629 nmem_total(se->nmem));
630 nmem_reset(se->nmem);
631 se->total_records = se->total_merged = 0;
632 se->num_termlists = 0;
634 /* reset list of sorted results and clear to relevance search */
635 se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
636 se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
637 se->sorted_results->increasing = sp->increasing;
638 se->sorted_results->type = sp->type;
639 se->sorted_results->next = 0;
641 session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
642 sp->name, sp->increasing, sp->type);
644 se->reclist = reclist_create(se->nmem);
647 static void session_sort_unlocked(struct session *se, struct reclist_sortparms *sp)
649 struct reclist_sortparms *sr;
650 struct client_list *l;
651 const char *field = sp->name;
652 int increasing = sp->increasing;
654 int clients_research = 0;
656 yaz_log(YLOG_DEBUG, "session_sort field=%s increasing=%d type=%d", field, increasing, type);
657 /* see if we already have sorted for this criteria */
658 for (sr = se->sorted_results; sr; sr = sr->next)
660 if (!reclist_sortparms_cmp(sr,sp))
665 session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d already fetched",
666 field, increasing, type);
669 session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d must fetch",
670 field, increasing, type);
672 // We need to reset reclist on every sort that changes the records, not just for position
673 // So if just one client requires new searching, we need to clear set.
674 // Ask each of the client if sorting requires re-search due to native sort
675 // If it does it will require us to
676 for (l = se->clients_active; l; l = l->next)
678 struct client *cl = l->client;
679 // Assume no re-search is required.
680 client_parse_init(cl, 1);
681 clients_research += client_parse_sort(cl, sp);
683 if (clients_research) {
684 yaz_log(YLOG_DEBUG, "Reset results due to %d clients researching", clients_research);
685 session_clear_set(se, sp);
688 // A new sorting based on same record set
689 sr = nmem_malloc(se->nmem, sizeof(*sr));
690 sr->name = nmem_strdup(se->nmem, field);
691 sr->increasing = increasing;
693 sr->next = se->sorted_results;
694 se->sorted_results = sr;
695 session_log(se, YLOG_DEBUG, "No research/ingesting done");
698 session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
700 for (l = se->clients_active; l; l = l->next)
702 struct client *cl = l->client;
703 if (client_get_state(cl) == Client_Connecting ||
704 client_get_state(cl) == Client_Idle ||
705 client_get_state(cl) == Client_Working) {
706 client_start_search(cl);
709 yaz_log(YLOG_DEBUG, "Client %s: No re-start/ingest in show. Wrong client state: %d",
710 client_get_id(cl), client_get_state(cl));
716 void session_sort(struct session *se, struct reclist_sortparms *sp) {
717 //session_enter(se, "session_sort");
718 session_sort_unlocked(se, sp);
719 //session_leave(se, "session_sort");
723 enum pazpar2_error_code session_search(struct session *se,
725 const char *startrecs,
729 const char **addinfo,
730 struct reclist_sortparms *sp)
732 int live_channels = 0;
734 int no_failed_query = 0;
735 int no_failed_limit = 0;
736 struct client_list *l, *l0;
738 session_log(se, YLOG_DEBUG, "Search");
742 if (se->settings_modified) {
743 session_remove_cached_clients(se);
746 session_reset_active_clients(se, 0);
748 session_enter(se, "session_search");
749 se->settings_modified = 0;
751 session_clear_set(se, sp);
752 relevance_destroy(&se->relevance);
754 live_channels = select_targets(se, filter);
757 session_leave(se, "session_search");
758 return PAZPAR2_NO_TARGETS;
761 facet_limits_destroy(se->facet_limits);
762 se->facet_limits = facet_limits_create(limit);
763 if (!se->facet_limits)
766 session_leave(se, "session_search");
767 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
770 l0 = se->clients_active;
771 se->clients_active = 0;
772 session_leave(se, "session_search");
774 for (l = l0; l; l = l->next)
777 struct client *cl = l->client;
778 client_parse_init(cl, 1);
779 if (prepare_map(se, client_get_database(cl)) < 0)
782 parse_ret = client_parse_query(cl, query, se->facet_limits);
785 else if (parse_ret == -2)
787 else if (parse_ret < 0)
788 no_working++; /* other error, such as bad CCL map */
791 client_parse_range(cl, startrecs, maxrecs);
792 client_parse_sort(cl, sp);
793 client_start_search(cl);
797 session_reset_active_clients(se, l0);
801 if (no_failed_query > 0)
804 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
806 else if (no_failed_limit > 0)
809 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
812 return PAZPAR2_NO_TARGETS;
814 session_log(se, YLOG_LOG, "session_start_search done");
815 return PAZPAR2_NO_ERROR;
818 // Creates a new session_database object for a database
819 static void session_init_databases_fun(void *context, struct database *db)
821 struct session *se = (struct session *) context;
822 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
828 assert(db->settings);
829 new->settings = nmem_malloc(se->session_nmem,
830 sizeof(struct settings *) * db->num_settings);
831 new->num_settings = db->num_settings;
832 for (i = 0; i < db->num_settings; i++)
834 struct setting *setting = db->settings[i];
835 new->settings[i] = setting;
837 new->next = se->databases;
841 // Doesn't free memory associated with sdb -- nmem takes care of that
842 static void session_database_destroy(struct session_database *sdb)
847 // Initialize session_database list -- this represents this session's view
848 // of the database list -- subject to modification by the settings ws command
849 void session_init_databases(struct session *se)
852 predef_grep_databases(se, se->service, session_init_databases_fun);
855 // Probably session_init_databases_fun should be refactored instead of
857 static struct session_database *load_session_database(struct session *se,
860 struct database *db = new_database_inherit_settings(id, se->session_nmem, se->service->settings);
861 session_init_databases_fun((void*) se, db);
863 // New sdb is head of se->databases list
864 return se->databases;
867 // Find an existing session database. If not found, load it
868 static struct session_database *find_session_database(struct session *se,
871 struct session_database *sdb;
873 for (sdb = se->databases; sdb; sdb = sdb->next)
874 if (!strcmp(sdb->database->id, id))
876 return load_session_database(se, id);
879 // Apply a session override to a database
880 void session_apply_setting(struct session *se, char *dbname, char *setting,
883 struct session_database *sdb = find_session_database(se, dbname);
884 struct conf_service *service = se->service;
885 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
886 int offset = settings_create_offset(service, setting);
888 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
891 new->target = dbname;
894 new->next = sdb->settings[offset];
895 sdb->settings[offset] = new;
897 se->settings_modified = 1;
899 // Force later recompute of settings-driven data structures
900 // (happens when a search starts and client connections are prepared)
912 void session_destroy(struct session *se)
914 struct session_database *sdb;
915 session_log(se, YLOG_DEBUG, "Destroying");
917 session_remove_cached_clients(se);
919 for (sdb = se->databases; sdb; sdb = sdb->next)
920 session_database_destroy(sdb);
921 normalize_cache_destroy(se->normalize_cache);
922 relevance_destroy(&se->relevance);
923 reclist_destroy(se->reclist);
924 if (nmem_total(se->nmem))
925 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
926 if (nmem_total(se->session_nmem))
927 session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
928 facet_limits_destroy(se->facet_limits);
929 nmem_destroy(se->nmem);
930 service_destroy(se->service);
931 yaz_mutex_destroy(&se->session_mutex);
934 size_t session_get_memory_status(struct session *session) {
938 session_enter(session, "session_get_memory_status");
939 session_nmem = nmem_total(session->nmem);
940 session_leave(session, "session_get_memory_status");
945 struct session *new_session(NMEM nmem, struct conf_service *service,
949 struct session *session = nmem_malloc(nmem, sizeof(*session));
953 sprintf(tmp_str, "session#%u", session_id);
955 session->session_id = session_id;
956 session_log(session, YLOG_DEBUG, "New");
957 session->service = service;
958 session->relevance = 0;
959 session->total_records = 0;
960 session->number_of_warnings_unknown_elements = 0;
961 session->number_of_warnings_unknown_metadata = 0;
962 session->num_termlists = 0;
963 session->reclist = 0;
964 session->clients_active = 0;
965 session->clients_cached = 0;
966 session->settings_modified = 0;
967 session->session_nmem = nmem;
968 session->nmem = nmem_create();
969 session->databases = 0;
970 session->sorted_results = 0;
971 session->facet_limits = 0;
973 for (i = 0; i <= SESSION_WATCH_MAX; i++)
975 session->watchlist[i].data = 0;
976 session->watchlist[i].fun = 0;
978 session->normalize_cache = normalize_cache_create();
979 session->session_mutex = 0;
980 pazpar2_mutex_create(&session->session_mutex, tmp_str);
985 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf);
987 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
988 int *count, NMEM nmem)
990 struct hitsbytarget *res = 0;
991 struct client_list *l;
994 for (l = se->clients_active; l; l = l->next)
997 res = nmem_malloc(nmem, sizeof(*res) * sz);
999 for (l = se->clients_active; l; l = l->next)
1001 struct client *cl = l->client;
1002 WRBUF w = wrbuf_alloc();
1003 const char *name = session_setting_oneval(client_get_database(cl),
1006 res[*count].id = client_get_id(cl);
1007 res[*count].name = *name ? name : "Unknown";
1008 res[*count].hits = client_get_hits(cl);
1009 res[*count].approximation = client_get_approximation(cl);
1010 res[*count].records = client_get_num_records(cl);
1011 res[*count].filtered = client_get_num_records_filtered(cl);
1012 res[*count].diagnostic =
1013 client_get_diagnostic(cl, &res[*count].message,
1014 &res[*count].addinfo);
1015 res[*count].state = client_get_state_str(cl);
1016 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1017 session_settings_dump(se, client_get_database(cl), w);
1018 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1021 res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1028 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1030 struct hitsbytarget *p;
1031 session_enter(se, "get_hitsbytarget");
1032 p = hitsbytarget_nb(se, count, nmem);
1033 session_leave(se, "get_hitsbytarget");
1037 // Compares two hitsbytarget nodes by hitcount
1038 static int cmp_ht(const void *p1, const void *p2)
1040 const struct hitsbytarget *h1 = p1;
1041 const struct hitsbytarget *h2 = p2;
1042 return h2->hits - h1->hits;
1045 // Compares two hitsbytarget nodes by hitcount
1046 static int cmp_ht_approx(const void *p1, const void *p2)
1048 const struct hitsbytarget *h1 = p1;
1049 const struct hitsbytarget *h2 = p2;
1050 return h2->approximation - h1->approximation;
1053 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1054 NMEM nmem, int version)
1056 struct hitsbytarget *ht;
1059 ht = hitsbytarget_nb(se, &count, nmem);
1061 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1063 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1064 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1067 // do only print terms which have display names
1069 wrbuf_puts(wrbuf, "<term>\n");
1071 wrbuf_puts(wrbuf, "<id>");
1072 wrbuf_xmlputs(wrbuf, ht[i].id);
1073 wrbuf_puts(wrbuf, "</id>\n");
1075 wrbuf_puts(wrbuf, "<name>");
1076 if (!ht[i].name || !ht[i].name[0])
1077 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1079 wrbuf_xmlputs(wrbuf, ht[i].name);
1080 wrbuf_puts(wrbuf, "</name>\n");
1082 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1086 // Should not print if we know it isn't a approximation.
1087 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1088 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1089 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1092 wrbuf_puts(wrbuf, "<state>");
1093 wrbuf_xmlputs(wrbuf, ht[i].state);
1094 wrbuf_puts(wrbuf, "</state>\n");
1096 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1098 wrbuf_puts(wrbuf, "</term>\n");
1103 void perform_termlist(struct http_channel *c, struct session *se,
1104 const char *name, int num, int version)
1107 NMEM nmem_tmp = nmem_create();
1114 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1116 session_enter(se, "perform_termlist");
1118 for (j = 0; j < num_names; j++)
1121 int must_generate_empty = 1; /* bug 5350 */
1123 for (i = 0; i < se->num_termlists; i++)
1125 tname = se->termlists[i].name;
1126 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1128 struct termlist_score **p = 0;
1131 wrbuf_puts(c->wrbuf, "<list name=\"");
1132 wrbuf_xmlputs(c->wrbuf, tname);
1133 wrbuf_puts(c->wrbuf, "\">\n");
1134 must_generate_empty = 0;
1136 p = termlist_highscore(se->termlists[i].termlist, &len,
1141 for (i = 0; i < len && i < num; i++)
1143 // prevent sending empty term elements
1144 if (!p[i]->display_term || !p[i]->display_term[0])
1147 wrbuf_puts(c->wrbuf, "<term>");
1148 wrbuf_puts(c->wrbuf, "<name>");
1149 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1150 wrbuf_puts(c->wrbuf, "</name>");
1152 wrbuf_printf(c->wrbuf,
1153 "<frequency>%d</frequency>",
1155 wrbuf_puts(c->wrbuf, "</term>\n");
1158 wrbuf_puts(c->wrbuf, "</list>\n");
1162 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1164 wrbuf_puts(c->wrbuf, "<list name=\"");
1165 wrbuf_xmlputs(c->wrbuf, tname);
1166 wrbuf_puts(c->wrbuf, "\">\n");
1168 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1169 wrbuf_puts(c->wrbuf, "</list>\n");
1170 must_generate_empty = 0;
1172 if (must_generate_empty)
1174 wrbuf_puts(c->wrbuf, "<list name=\"");
1175 wrbuf_xmlputs(c->wrbuf, names[j]);
1176 wrbuf_puts(c->wrbuf, "\"/>\n");
1179 session_leave(se, "perform_termlist");
1180 nmem_destroy(nmem_tmp);
1183 #ifdef MISSING_HEADERS
1184 void report_nmem_stats(void)
1186 size_t in_use, is_free;
1188 nmem_get_memory_in_use(&in_use);
1189 nmem_get_memory_free(&is_free);
1191 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1192 (long) in_use, (long) is_free);
1196 struct record_cluster *show_single_start(struct session *se, const char *id,
1197 struct record_cluster **prev_r,
1198 struct record_cluster **next_r)
1200 struct record_cluster *r = 0;
1202 session_enter(se, "show_single_start");
1207 reclist_limit(se->reclist, se);
1209 reclist_enter(se->reclist);
1210 while ((r = reclist_read_record(se->reclist)))
1212 if (!strcmp(r->recid, id))
1214 *next_r = reclist_read_record(se->reclist);
1219 reclist_leave(se->reclist);
1222 session_leave(se, "show_single_start");
1226 void show_single_stop(struct session *se, struct record_cluster *rec)
1228 session_leave(se, "show_single_stop");
1232 int session_fetch_more(struct session *se)
1234 struct client_list *l;
1237 for (l = se->clients_active; l; l = l->next)
1239 struct client *cl = l->client;
1240 if (client_get_state(cl) == Client_Idle)
1242 if (client_fetch_more(cl))
1244 session_log(se, YLOG_LOG, "%s: more to fetch",
1250 session_log(se, YLOG_LOG, "%s: no more to fetch",
1256 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1257 client_get_id(cl), client_get_state_str(cl));
1264 struct record_cluster **show_range_start(struct session *se,
1265 struct reclist_sortparms *sp,
1266 int start, int *num, int *total,
1267 Odr_int *sumhits, Odr_int *approx_hits)
1269 struct record_cluster **recs = 0;
1270 struct reclist_sortparms *spp;
1271 struct client_list *l;
1274 yaz_timing_t t = yaz_timing_create();
1276 session_enter(se, "show_range_start");
1280 reclist_limit(se->reclist, se);
1283 for (spp = sp; spp; spp = spp->next)
1284 if (spp->type == Metadata_sortkey_relevance)
1286 relevance_prepare_read(se->relevance, se->reclist);
1289 for (l = se->clients_active; l; l = l->next) {
1290 *sumhits += client_get_hits(l->client);
1291 *approx_hits += client_get_approximation(l->client);
1294 reclist_sort(se->reclist, sp);
1296 reclist_enter(se->reclist);
1297 *total = reclist_get_num_records(se->reclist);
1299 for (i = 0; i < start; i++)
1300 if (!reclist_read_record(se->reclist))
1308 nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
1309 for (i = 0; i < *num; i++)
1311 struct record_cluster *r = reclist_read_record(se->reclist);
1319 reclist_leave(se->reclist);
1322 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1323 yaz_timing_get_real(t), yaz_timing_get_user(t),
1324 yaz_timing_get_sys(t));
1325 yaz_timing_destroy(&t);
1330 void show_range_stop(struct session *se, struct record_cluster **recs)
1332 session_leave(se, "show_range_stop");
1335 void statistics(struct session *se, struct statistics *stat)
1337 struct client_list *l;
1340 memset(stat, 0, sizeof(*stat));
1342 for (l = se->clients_active; l; l = l->next)
1344 struct client *cl = l->client;
1345 if (!client_get_connection(cl))
1346 stat->num_no_connection++;
1347 stat->num_hits += client_get_hits(cl);
1348 switch (client_get_state(cl))
1350 case Client_Connecting: stat->num_connecting++; break;
1351 case Client_Working: stat->num_working++; break;
1352 case Client_Idle: stat->num_idle++; break;
1353 case Client_Failed: stat->num_failed++; break;
1354 case Client_Error: stat->num_error++; break;
1359 stat->num_records = se->total_records;
1361 stat->num_clients = count;
1364 static struct record_metadata *record_metadata_init(
1365 NMEM nmem, const char *value, enum conf_metadata_type type,
1366 struct _xmlAttr *attr)
1368 struct record_metadata *rec_md = record_metadata_create(nmem);
1369 struct record_metadata_attr **attrp = &rec_md->attributes;
1371 for (; attr; attr = attr->next)
1373 if (attr->children && attr->children->content)
1375 if (strcmp((const char *) attr->name, "type"))
1376 { /* skip the "type" attribute.. Its value is already part of
1377 the element in output (md-%s) and so repeating it here
1379 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1381 nmem_strdup(nmem, (const char *) attr->name);
1383 nmem_strdup(nmem, (const char *) attr->children->content);
1384 attrp = &(*attrp)->next;
1390 if (type == Metadata_type_generic)
1392 char *p = nmem_strdup(nmem, value);
1394 p = normalize7bit_generic(p, " ,/.:([");
1396 rec_md->data.text.disp = p;
1397 rec_md->data.text.sort = 0;
1399 else if (type == Metadata_type_year || type == Metadata_type_date)
1404 if (type == Metadata_type_date)
1406 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1409 rec_md->data.number.min = first;
1410 rec_md->data.number.max = last;
1417 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1418 struct conf_service *service, WRBUF norm_wr)
1422 for (n = root->children; n; n = n->next)
1424 if (n->type != XML_ELEMENT_NODE)
1426 if (!strcmp((const char *) n->name, "metadata"))
1428 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1430 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1432 else if (!strcmp(name, (const char *) type))
1434 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1437 const char *norm_str;
1438 pp2_charset_token_t prt =
1439 pp2_charset_token_create(service->charsets, "mergekey");
1441 pp2_charset_token_first(prt, (const char *) value, 0);
1442 if (wrbuf_len(norm_wr) > 0)
1443 wrbuf_puts(norm_wr, " ");
1444 wrbuf_puts(norm_wr, name);
1446 pp2_charset_token_next(prt)))
1450 wrbuf_puts(norm_wr, " ");
1451 wrbuf_puts(norm_wr, norm_str);
1455 pp2_charset_token_destroy(prt);
1465 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1466 struct conf_service *service, NMEM nmem)
1468 char *mergekey_norm = 0;
1469 xmlNode *root = xmlDocGetRootElement(doc);
1470 WRBUF norm_wr = wrbuf_alloc();
1472 /* consider mergekey from XSL first */
1473 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1476 const char *norm_str;
1477 pp2_charset_token_t prt =
1478 pp2_charset_token_create(service->charsets, "mergekey");
1480 pp2_charset_token_first(prt, (const char *) mergekey, 0);
1481 while ((norm_str = pp2_charset_token_next(prt)))
1485 if (wrbuf_len(norm_wr))
1486 wrbuf_puts(norm_wr, " ");
1487 wrbuf_puts(norm_wr, norm_str);
1490 pp2_charset_token_destroy(prt);
1495 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1497 for (field_id = 0; field_id < service->num_metadata; field_id++)
1499 struct conf_metadata *ser_md = &service->metadata[field_id];
1500 if (ser_md->mergekey != Metadata_mergekey_no)
1502 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1504 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1506 /* no mergekey on this one and it is required..
1507 Generate unique key instead */
1508 wrbuf_rewind(norm_wr);
1515 /* generate unique key if none is not generated already or is empty */
1516 if (wrbuf_len(norm_wr) == 0)
1518 wrbuf_printf(norm_wr, "position: %s-%d",
1519 client_get_id(cl), record_no);
1523 const char *lead = "content: ";
1524 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1526 if (wrbuf_len(norm_wr) > 0)
1527 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1528 wrbuf_destroy(norm_wr);
1529 return mergekey_norm;
1532 /** \brief see if metadata for pz:recordfilter exists
1533 \param root xml root element of normalized record
1534 \param sdb session database for client
1535 \retval 0 if there is no metadata for pz:recordfilter
1536 \retval 1 if there is metadata for pz:recordfilter
1538 If there is no pz:recordfilter defined, this function returns 1
1542 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1547 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1552 for (n = root->children; n; n = n->next)
1554 if (n->type != XML_ELEMENT_NODE)
1556 if (!strcmp((const char *) n->name, "metadata"))
1558 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1565 if ((eq = strchr(s, '=')))
1567 else if ((eq = strchr(s, '~')))
1573 if (len == strlen((const char *)type) &&
1574 !memcmp((const char *) type, s, len))
1576 xmlChar *value = xmlNodeGetContent(n);
1577 if (value && *value)
1580 (substring && strstr((const char *) value, eq+1)) ||
1581 (!substring && !strcmp((const char *) value, eq + 1)))
1594 static int ingest_to_cluster(struct client *cl,
1598 const char *mergekey_norm);
1600 /** \brief ingest XML record
1601 \param cl client holds the result set for record
1602 \param rec record buffer (0 terminated)
1603 \param record_no record position (1, 2, ..)
1604 \param nmem working NMEM
1609 int ingest_record(struct client *cl, const char *rec,
1610 int record_no, NMEM nmem)
1612 struct session *se = client_get_session(cl);
1614 struct session_database *sdb = client_get_database(cl);
1615 struct conf_service *service = se->service;
1616 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1618 const char *mergekey_norm;
1623 root = xmlDocGetRootElement(xdoc);
1625 if (!check_record_filter(root, sdb))
1627 session_log(se, YLOG_LOG, "Filtered out record no %d from %s", record_no, sdb->database->id);
1632 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1635 session_log(se, YLOG_WARN, "Got no mergekey");
1639 session_enter(se, "ingest_record");
1640 if (client_get_session(cl) == se)
1641 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1642 session_leave(se, "ingest_record");
1648 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1649 // struct record_metadata *rec_md = record->metadata[md_field_id];
1650 static int match_metadata_local(struct conf_metadata *ser_md,
1651 struct record_metadata *rec_md0,
1652 char **values, int num_v)
1655 struct record_metadata *rec_md = rec_md0;
1656 for (i = 0; i < num_v; )
1660 if (ser_md->type == Metadata_type_year
1661 || ser_md->type == Metadata_type_date)
1663 int y = atoi(values[i]);
1664 if (y >= rec_md->data.number.min
1665 && y <= rec_md->data.number.max)
1670 yaz_log(YLOG_DEBUG, "cmp: '%s' '%s'", rec_md->data.text.disp, values[i]);
1671 if (!strcmp(rec_md->data.text.disp, values[i]))
1673 // Value equals, should not be filtered.
1677 rec_md = rec_md->next;
1685 return i < num_v ? 1 : 0;
1688 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
1691 struct conf_service *service = se->service;
1695 NMEM nmem_tmp = nmem_create();
1697 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
1700 for (j = 0; j < service->num_metadata; j++)
1702 struct conf_metadata *md = service->metadata + j;
1703 if (!strcmp(md->name, name) && md->limitcluster)
1708 conf_service_metadata_field_id(service,
1711 if (md_field_id < 0)
1717 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1720 if (!match_metadata_local(&service->metadata[md_field_id],
1721 rec->metadata[md_field_id],
1730 nmem_destroy(nmem_tmp);
1734 // Skip record on non-zero
1735 static int check_limit_local(struct client *cl,
1736 struct record *record,
1739 int skip_record = 0;
1740 struct session *se = client_get_session(cl);
1741 struct conf_service *service = se->service;
1742 NMEM nmem_tmp = nmem_create();
1743 struct session_database *sdb = client_get_database(cl);
1745 while (!skip_record)
1751 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
1756 if (!strcmp(name, "*"))
1758 for (md_field_id = 0; md_field_id < service->num_metadata;
1761 if (match_metadata_local(
1762 &service->metadata[md_field_id],
1763 record->metadata[md_field_id],
1767 if (md_field_id == service->num_metadata)
1772 md_field_id = conf_service_metadata_field_id(service, name);
1773 if (md_field_id < 0)
1778 if (!match_metadata_local(
1779 &service->metadata[md_field_id],
1780 record->metadata[md_field_id],
1787 nmem_destroy(nmem_tmp);
1791 static int ingest_to_cluster(struct client *cl,
1795 const char *mergekey_norm)
1800 struct session *se = client_get_session(cl);
1801 struct conf_service *service = se->service;
1802 int term_factor = 1;
1803 struct record_cluster *cluster;
1804 struct record_metadata **metadata0;
1805 struct session_database *sdb = client_get_database(cl);
1806 struct record *record = record_create(se->nmem,
1807 service->num_metadata,
1808 service->num_sortkeys, cl,
1811 for (n = root->children; n; n = n->next)
1819 if (n->type != XML_ELEMENT_NODE)
1821 if (!strcmp((const char *) n->name, "metadata"))
1823 struct conf_metadata *ser_md = 0;
1824 struct record_metadata **wheretoput = 0;
1825 struct record_metadata *rec_md = 0;
1826 int md_field_id = -1;
1828 type = xmlGetProp(n, (xmlChar *) "type");
1829 value = xmlNodeListGetString(xdoc, n->children, 1);
1831 if (!type || !value || !*value)
1835 = conf_service_metadata_field_id(service, (const char *) type);
1836 if (md_field_id < 0)
1838 if (se->number_of_warnings_unknown_metadata == 0)
1840 session_log(se, YLOG_WARN,
1841 "Ignoring unknown metadata element: %s", type);
1843 se->number_of_warnings_unknown_metadata++;
1847 ser_md = &service->metadata[md_field_id];
1849 // non-merged metadata
1850 rec_md = record_metadata_init(se->nmem, (const char *) value,
1851 ser_md->type, n->properties);
1854 session_log(se, YLOG_WARN, "bad metadata data '%s' "
1855 "for element '%s'", value, type);
1858 wheretoput = &record->metadata[md_field_id];
1860 wheretoput = &(*wheretoput)->next;
1861 *wheretoput = rec_md;
1865 if (check_limit_local(cl, record, record_no))
1867 session_log(se, YLOG_LOG, "Facet filtered out record no %d from %s",
1868 record_no, sdb->database->id);
1875 cluster = reclist_insert(se->reclist, service, record,
1876 mergekey_norm, &se->total_merged);
1878 return 0; // complete match with existing record
1881 const char *use_term_factor_str =
1882 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
1883 if (use_term_factor_str && use_term_factor_str[0] == '1')
1885 int maxrecs = client_get_maxrecs(cl);
1886 int hits = (int) client_get_hits(cl);
1887 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
1888 assert(term_factor >= 1);
1889 yaz_log(YLOG_DEBUG, "Using term factor: %d (%d / %d)", term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
1893 if (global_parameters.dump_records)
1894 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1895 sdb->database->id, record_no);
1898 relevance_newrec(se->relevance, cluster);
1900 // original metadata, to check if first existence of a field
1901 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
1902 memcpy(metadata0, cluster->metadata,
1903 sizeof(*metadata0) * service->num_metadata);
1905 // now parsing XML record and adding data to cluster or record metadata
1906 for (n = root->children; n; n = n->next)
1908 pp2_charset_token_t prt;
1915 if (n->type != XML_ELEMENT_NODE)
1917 if (!strcmp((const char *) n->name, "metadata"))
1919 struct conf_metadata *ser_md = 0;
1920 struct conf_sortkey *ser_sk = 0;
1921 struct record_metadata **wheretoput = 0;
1922 struct record_metadata *rec_md = 0;
1923 int md_field_id = -1;
1924 int sk_field_id = -1;
1928 type = xmlGetProp(n, (xmlChar *) "type");
1929 value = xmlNodeListGetString(xdoc, n->children, 1);
1931 if (!type || !value || !*value)
1935 = conf_service_metadata_field_id(service, (const char *) type);
1936 if (md_field_id < 0)
1939 ser_md = &service->metadata[md_field_id];
1941 if (ser_md->sortkey_offset >= 0)
1943 sk_field_id = ser_md->sortkey_offset;
1944 ser_sk = &service->sortkeys[sk_field_id];
1948 rec_md = record_metadata_init(se->nmem, (const char *) value,
1951 // see if the field was not in cluster already (from beginning)
1956 xml_rank = xmlGetProp(n, (xmlChar *) "rank");
1957 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
1959 wheretoput = &cluster->metadata[md_field_id];
1961 if (ser_md->merge == Metadata_merge_first)
1963 if (!metadata0[md_field_id])
1966 wheretoput = &(*wheretoput)->next;
1967 *wheretoput = rec_md;
1970 else if (ser_md->merge == Metadata_merge_unique)
1974 if (!strcmp((const char *) (*wheretoput)->data.text.disp,
1975 rec_md->data.text.disp))
1977 wheretoput = &(*wheretoput)->next;
1980 *wheretoput = rec_md;
1982 else if (ser_md->merge == Metadata_merge_longest)
1985 || strlen(rec_md->data.text.disp)
1986 > strlen((*wheretoput)->data.text.disp))
1988 *wheretoput = rec_md;
1991 const char *sort_str = 0;
1993 ser_sk->type == Metadata_sortkey_skiparticle;
1995 if (!cluster->sortkeys[sk_field_id])
1996 cluster->sortkeys[sk_field_id] =
1997 nmem_malloc(se->nmem,
1998 sizeof(union data_types));
2001 pp2_charset_token_create(service->charsets, "sort");
2003 pp2_charset_token_first(prt, rec_md->data.text.disp,
2006 pp2_charset_token_next(prt);
2008 sort_str = pp2_get_sort(prt);
2010 cluster->sortkeys[sk_field_id]->text.disp =
2011 rec_md->data.text.disp;
2014 sort_str = rec_md->data.text.disp;
2015 session_log(se, YLOG_WARN,
2016 "Could not make sortkey. Bug #1858");
2018 cluster->sortkeys[sk_field_id]->text.sort =
2019 nmem_strdup(se->nmem, sort_str);
2020 pp2_charset_token_destroy(prt);
2024 else if (ser_md->merge == Metadata_merge_all)
2027 wheretoput = &(*wheretoput)->next;
2028 *wheretoput = rec_md;
2030 else if (ser_md->merge == Metadata_merge_range)
2034 *wheretoput = rec_md;
2036 cluster->sortkeys[sk_field_id]
2041 int this_min = rec_md->data.number.min;
2042 int this_max = rec_md->data.number.max;
2043 if (this_min < (*wheretoput)->data.number.min)
2044 (*wheretoput)->data.number.min = this_min;
2045 if (this_max > (*wheretoput)->data.number.max)
2046 (*wheretoput)->data.number.max = this_max;
2050 // ranking of _all_ fields enabled ...
2053 relevance_countwords(se->relevance, cluster,
2054 (char *) value, rank, ser_md->name);
2057 // construct facets ... unless the client already has reported them
2058 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2060 if (ser_md->type == Metadata_type_year)
2063 sprintf(year, "%d", rec_md->data.number.max);
2065 add_facet(se, (char *) type, year, term_factor);
2066 if (rec_md->data.number.max != rec_md->data.number.min)
2068 sprintf(year, "%d", rec_md->data.number.min);
2069 add_facet(se, (char *) type, year, term_factor);
2073 add_facet(se, (char *) type, (char *) value, term_factor);
2085 if (se->number_of_warnings_unknown_elements == 0)
2086 session_log(se, YLOG_WARN,
2087 "Unexpected element in internal record: %s", n->name);
2088 se->number_of_warnings_unknown_elements++;
2097 relevance_donerecord(se->relevance, cluster);
2098 se->total_records++;
2103 void session_log(struct session *s, int level, const char *fmt, ...)
2109 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2110 yaz_log(level, "Session %u: %s", s->session_id, buf);
2118 * c-file-style: "Stroustrup"
2119 * indent-tabs-mode: nil
2121 * vim: shiftwidth=4 tabstop=8 expandtab