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 session_log(s, YLOG_FATAL,
185 "Unknown ICU chain '%s' for facet of type '%s'",
187 wrbuf_destroy(facet_wrbuf);
188 wrbuf_destroy(display_wrbuf);
191 pp2_charset_token_first(prt, value, 0);
192 while ((facet_component = pp2_charset_token_next(prt)))
194 const char *display_component;
195 if (*facet_component)
197 if (wrbuf_len(facet_wrbuf))
198 wrbuf_puts(facet_wrbuf, " ");
199 wrbuf_puts(facet_wrbuf, facet_component);
201 display_component = pp2_get_display(prt);
202 if (display_component)
204 if (wrbuf_len(display_wrbuf))
205 wrbuf_puts(display_wrbuf, " ");
206 wrbuf_puts(display_wrbuf, display_component);
209 pp2_charset_token_destroy(prt);
212 void add_facet(struct session *s, const char *type, const char *value, int count)
214 WRBUF facet_wrbuf = wrbuf_alloc();
215 WRBUF display_wrbuf = wrbuf_alloc();
217 session_normalize_facet(s, type, value, display_wrbuf, facet_wrbuf);
219 if (wrbuf_len(facet_wrbuf))
222 for (i = 0; i < s->num_termlists; i++)
223 if (!strcmp(s->termlists[i].name, type))
225 if (i == s->num_termlists)
227 if (i == SESSION_MAX_TERMLISTS)
229 session_log(s, YLOG_FATAL, "Too many termlists");
230 wrbuf_destroy(facet_wrbuf);
231 wrbuf_destroy(display_wrbuf);
235 s->termlists[i].name = nmem_strdup(s->nmem, type);
236 s->termlists[i].termlist = termlist_create(s->nmem);
237 s->num_termlists = i + 1;
241 session_log(s, YLOG_LOG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
243 termlist_insert(s->termlists[i].termlist, wrbuf_cstr(display_wrbuf),
244 wrbuf_cstr(facet_wrbuf), count);
246 wrbuf_destroy(facet_wrbuf);
247 wrbuf_destroy(display_wrbuf);
250 static xmlDoc *record_to_xml(struct session *se,
251 struct session_database *sdb, const char *rec)
253 struct database *db = sdb->database;
256 rdoc = xmlParseMemory(rec, strlen(rec));
260 session_log(se, YLOG_WARN, "Non-wellformed XML");
264 if (global_parameters.dump_records)
266 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->id);
273 #define MAX_XSLT_ARGS 16
275 // Add static values from session database settings if applicable
276 static void insert_settings_parameters(struct session_database *sdb,
277 struct conf_service *service,
285 for (i = 0; i < service->num_metadata; i++)
287 struct conf_metadata *md = &service->metadata[i];
290 if (md->setting == Metadata_setting_parameter &&
291 (setting = settings_lookup_offset(service, md->name)) >= 0)
293 const char *val = session_setting_oneval(sdb, setting);
294 if (val && nparms < MAX_XSLT_ARGS)
297 int len = strlen(val);
298 buf = nmem_malloc(nmem, len + 3);
300 strcpy(buf + 1, val);
303 parms[offset++] = md->name;
304 parms[offset++] = buf;
312 // Add static values from session database settings if applicable
313 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
314 struct conf_service *service)
318 for (i = 0; i < service->num_metadata; i++)
320 struct conf_metadata *md = &service->metadata[i];
323 if (md->setting == Metadata_setting_postproc &&
324 (offset = settings_lookup_offset(service, md->name)) >= 0)
326 const char *val = session_setting_oneval(sdb, offset);
329 xmlNode *r = xmlDocGetRootElement(doc);
330 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
332 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
338 static xmlDoc *normalize_record(struct session *se,
339 struct session_database *sdb,
340 struct conf_service *service,
341 const char *rec, NMEM nmem)
343 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
347 char *parms[MAX_XSLT_ARGS*2+1];
349 insert_settings_parameters(sdb, service, parms, nmem);
351 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
353 session_log(se, YLOG_WARN, "Normalize failed");
357 insert_settings_values(sdb, rdoc, service);
359 if (global_parameters.dump_records)
361 session_log(se, YLOG_LOG, "Normalized record from %s",
370 void session_settings_dump(struct session *se,
371 struct session_database *db,
376 int i, num = db->num_settings;
377 for (i = 0; i < num; i++)
379 struct setting *s = db->settings[i];
380 for (;s ; s = s->next)
382 wrbuf_puts(w, "<set name=\"");
383 wrbuf_xmlputs(w, s->name);
384 wrbuf_puts(w, "\" value=\"");
385 wrbuf_xmlputs(w, s->value);
386 wrbuf_puts(w, "\"/>");
394 // Retrieve first defined value for 'name' for given database.
395 // Will be extended to take into account user associated with session
396 const char *session_setting_oneval(struct session_database *db, int offset)
398 if (offset >= db->num_settings || !db->settings[offset])
400 return db->settings[offset]->value;
403 // Prepare XSLT stylesheets for record normalization
404 // Structures are allocated on the session_wide nmem to avoid having
405 // to recompute this for every search. This would lead
406 // to leaking if a single session was to repeatedly change the PZ_XSLT
407 // setting. However, this is not a realistic use scenario.
408 static int prepare_map(struct session *se, struct session_database *sdb)
410 if (sdb->settings && !sdb->map)
414 if (sdb->settings[PZ_XSLT] &&
415 (s = session_setting_oneval(sdb, PZ_XSLT)))
417 char auto_stylesheet[256];
419 if (!strcmp(s, "auto"))
421 const char *request_syntax = session_setting_oneval(
422 sdb, PZ_REQUESTSYNTAX);
426 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
427 "%s.xsl", request_syntax);
428 for (cp = auto_stylesheet; *cp; cp++)
430 /* deliberately only consider ASCII */
431 if (*cp > 32 && *cp < 127)
438 session_log(se, YLOG_WARN,
439 "No pz:requestsyntax for auto stylesheet");
442 sdb->map = normalize_cache_get(se->normalize_cache,
451 // called if watch should be removed because http_channel is to be destroyed
452 static void session_watch_cancel(void *data, struct http_channel *c,
455 struct session_watchentry *ent = data;
462 // set watch. Returns 0=OK, -1 if watch is already set
463 int session_set_watch(struct session *s, int what,
464 session_watchfun fun, void *data,
465 struct http_channel *chan)
468 session_enter(s, "session_set_watch");
469 if (s->watchlist[what].fun)
474 s->watchlist[what].fun = fun;
475 s->watchlist[what].data = data;
476 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
477 session_watch_cancel);
480 session_leave(s, "session_set_watch");
484 void session_alert_watch(struct session *s, int what)
487 session_enter(s, "session_alert_watch");
488 if (s->watchlist[what].fun)
490 /* our watch is no longer associated with http_channel */
492 session_watchfun fun;
494 http_remove_observer(s->watchlist[what].obs);
495 fun = s->watchlist[what].fun;
496 data = s->watchlist[what].data;
498 /* reset watch before fun is invoked - in case fun wants to set
500 s->watchlist[what].fun = 0;
501 s->watchlist[what].data = 0;
502 s->watchlist[what].obs = 0;
504 session_leave(s, "session_alert_watch");
505 session_log(s, YLOG_DEBUG,
506 "Alert Watch: %d calling function: %p", what, fun);
510 session_leave(s,"session_alert_watch");
513 //callback for grep_databases
514 static void select_targets_callback(struct session *se,
515 struct session_database *db)
518 struct client_list *l;
520 for (l = se->clients_cached; l; l = l->next)
521 if (client_get_database(l->client) == db)
528 cl = client_create(db->database->id);
529 client_set_database(cl, db);
531 l = xmalloc(sizeof(*l));
533 l->next = se->clients_cached;
534 se->clients_cached = l;
536 /* set session always. If may be 0 if client is not active */
537 client_set_session(cl, se);
539 l = xmalloc(sizeof(*l));
541 l->next = se->clients_active;
542 se->clients_active = l;
545 static void session_reset_active_clients(struct session *se,
546 struct client_list *new_list)
548 struct client_list *l;
550 session_enter(se, "session_reset_active_clients");
551 l = se->clients_active;
552 se->clients_active = new_list;
553 session_leave(se, "session_reset_active_clients");
557 struct client_list *l_next = l->next;
559 client_lock(l->client);
560 client_set_session(l->client, 0); /* mark client inactive */
561 client_unlock(l->client);
568 static void session_remove_cached_clients(struct session *se)
570 struct client_list *l;
572 session_reset_active_clients(se, 0);
574 session_enter(se, "session_remove_cached_clients");
575 l = se->clients_cached;
576 se->clients_cached = 0;
577 session_leave(se, "session_remove_cached_clients");
581 struct client_list *l_next = l->next;
582 client_lock(l->client);
583 client_set_session(l->client, 0);
584 client_set_database(l->client, 0);
585 client_unlock(l->client);
586 client_destroy(l->client);
592 // Associates a set of clients with a session;
593 // Note: Session-databases represent databases with per-session
595 static int select_targets(struct session *se, const char *filter)
597 return session_grep_databases(se, filter, select_targets_callback);
600 int session_active_clients(struct session *s)
602 struct client_list *l;
605 for (l = s->clients_active; l; l = l->next)
606 if (client_is_active(l->client))
612 int session_is_preferred_clients_ready(struct session *s)
614 struct client_list *l;
617 for (l = s->clients_active; l; l = l->next)
618 if (client_is_active_preferred(l->client))
620 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
624 static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
626 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 relevance_clear(se->relevance);
636 /* reset list of sorted results and clear to relevance search */
637 se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
638 se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
639 se->sorted_results->increasing = sp->increasing;
640 se->sorted_results->type = sp->type;
641 se->sorted_results->next = 0;
643 session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
644 sp->name, sp->increasing, sp->type);
646 se->reclist = reclist_create(se->nmem);
649 static void session_sort_unlocked(struct session *se,
650 struct reclist_sortparms *sp,
651 const char *mergekey)
653 struct reclist_sortparms *sr;
654 struct client_list *l;
655 const char *field = sp->name;
656 int increasing = sp->increasing;
658 int clients_research = 0;
660 session_log(se, YLOG_DEBUG, "session_sort field=%s increasing=%d type=%d",
661 field, increasing, type);
664 (se->mergekey && !strcmp(se->mergekey, mergekey)))
666 /* mergekey unchanged.. */
667 /* see if we already have sorted for this criteria */
668 for (sr = se->sorted_results; sr; sr = sr->next)
670 if (!reclist_sortparms_cmp(sr, sp))
675 session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d already fetched",
676 field, increasing, type);
679 session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d must fetch",
680 field, increasing, type);
684 /* new mergekey must research/reingest anyway */
687 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
688 clients_research = 1;
690 session_log(se, YLOG_DEBUG, "search_sort: new mergekey = %s",
694 // We need to reset reclist on every sort that changes the records, not just for position
695 // So if just one client requires new searching, we need to clear set.
696 // Ask each of the client if sorting requires re-search due to native sort
697 // If it does it will require us to
698 for (l = se->clients_active; l; l = l->next)
700 struct client *cl = l->client;
701 // Assume no re-search is required.
702 client_parse_init(cl, 1);
703 clients_research += client_parse_sort(cl, sp);
705 if (clients_research) {
706 session_log(se, YLOG_DEBUG,
707 "Reset results due to %d clients researching",
709 session_clear_set(se, sp);
712 // A new sorting based on same record set
713 sr = nmem_malloc(se->nmem, sizeof(*sr));
714 sr->name = nmem_strdup(se->nmem, field);
715 sr->increasing = increasing;
717 sr->next = se->sorted_results;
718 se->sorted_results = sr;
719 session_log(se, YLOG_DEBUG, "No research/ingesting done");
722 session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
724 for (l = se->clients_active; l; l = l->next)
726 struct client *cl = l->client;
727 if (client_get_state(cl) == Client_Connecting ||
728 client_get_state(cl) == Client_Idle ||
729 client_get_state(cl) == Client_Working) {
730 client_start_search(cl);
733 session_log(se, YLOG_DEBUG,
734 "Client %s: No re-start/ingest in show. Wrong client state: %d",
735 client_get_id(cl), client_get_state(cl));
741 void session_sort(struct session *se, struct reclist_sortparms *sp,
742 const char *mergekey)
744 //session_enter(se, "session_sort");
745 session_sort_unlocked(se, sp, mergekey);
746 //session_leave(se, "session_sort");
750 enum pazpar2_error_code session_search(struct session *se,
752 const char *startrecs,
756 const char **addinfo,
757 struct reclist_sortparms *sp,
758 const char *mergekey)
760 int live_channels = 0;
762 int no_failed_query = 0;
763 int no_failed_limit = 0;
764 struct client_list *l, *l0;
766 session_log(se, YLOG_DEBUG, "Search");
770 if (se->settings_modified) {
771 session_remove_cached_clients(se);
774 session_reset_active_clients(se, 0);
776 session_enter(se, "session_search");
777 se->settings_modified = 0;
782 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
785 session_clear_set(se, sp);
786 relevance_destroy(&se->relevance);
788 live_channels = select_targets(se, filter);
791 session_leave(se, "session_search");
792 return PAZPAR2_NO_TARGETS;
795 facet_limits_destroy(se->facet_limits);
796 se->facet_limits = facet_limits_create(limit);
797 if (!se->facet_limits)
800 session_leave(se, "session_search");
801 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
804 l0 = se->clients_active;
805 se->clients_active = 0;
806 session_leave(se, "session_search");
808 for (l = l0; l; l = l->next)
811 struct client *cl = l->client;
812 client_parse_init(cl, 1);
813 if (prepare_map(se, client_get_database(cl)) < 0)
816 parse_ret = client_parse_query(cl, query, se->facet_limits);
819 else if (parse_ret == -2)
821 else if (parse_ret < 0)
822 no_working++; /* other error, such as bad CCL map */
825 client_parse_range(cl, startrecs, maxrecs);
826 client_parse_sort(cl, sp);
827 client_start_search(cl);
831 session_reset_active_clients(se, l0);
835 if (no_failed_query > 0)
838 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
840 else if (no_failed_limit > 0)
843 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
846 return PAZPAR2_NO_TARGETS;
848 session_log(se, YLOG_LOG, "session_start_search done");
849 return PAZPAR2_NO_ERROR;
852 // Creates a new session_database object for a database
853 static void session_init_databases_fun(void *context, struct database *db)
855 struct session *se = (struct session *) context;
856 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
862 assert(db->settings);
863 new->settings = nmem_malloc(se->session_nmem,
864 sizeof(struct settings *) * db->num_settings);
865 new->num_settings = db->num_settings;
866 for (i = 0; i < db->num_settings; i++)
868 struct setting *setting = db->settings[i];
869 new->settings[i] = setting;
871 new->next = se->databases;
875 // Doesn't free memory associated with sdb -- nmem takes care of that
876 static void session_database_destroy(struct session_database *sdb)
881 // Initialize session_database list -- this represents this session's view
882 // of the database list -- subject to modification by the settings ws command
883 void session_init_databases(struct session *se)
886 predef_grep_databases(se, se->service, session_init_databases_fun);
889 // Probably session_init_databases_fun should be refactored instead of
891 static struct session_database *load_session_database(struct session *se,
894 struct database *db = new_database_inherit_settings(id, se->session_nmem, se->service->settings);
895 session_init_databases_fun((void*) se, db);
897 // New sdb is head of se->databases list
898 return se->databases;
901 // Find an existing session database. If not found, load it
902 static struct session_database *find_session_database(struct session *se,
905 struct session_database *sdb;
907 for (sdb = se->databases; sdb; sdb = sdb->next)
908 if (!strcmp(sdb->database->id, id))
910 return load_session_database(se, id);
913 // Apply a session override to a database
914 void session_apply_setting(struct session *se, char *dbname, char *setting,
917 struct session_database *sdb = find_session_database(se, dbname);
918 struct conf_service *service = se->service;
919 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
920 int offset = settings_create_offset(service, setting);
922 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
925 new->target = dbname;
928 new->next = sdb->settings[offset];
929 sdb->settings[offset] = new;
931 se->settings_modified = 1;
933 // Force later recompute of settings-driven data structures
934 // (happens when a search starts and client connections are prepared)
946 void session_destroy(struct session *se)
948 struct session_database *sdb;
949 session_log(se, YLOG_DEBUG, "Destroying");
951 session_remove_cached_clients(se);
953 for (sdb = se->databases; sdb; sdb = sdb->next)
954 session_database_destroy(sdb);
955 normalize_cache_destroy(se->normalize_cache);
956 relevance_destroy(&se->relevance);
957 reclist_destroy(se->reclist);
959 if (nmem_total(se->nmem))
960 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
961 if (nmem_total(se->session_nmem))
962 session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
963 facet_limits_destroy(se->facet_limits);
964 nmem_destroy(se->nmem);
965 service_destroy(se->service);
966 yaz_mutex_destroy(&se->session_mutex);
969 size_t session_get_memory_status(struct session *session) {
973 session_enter(session, "session_get_memory_status");
974 session_nmem = nmem_total(session->nmem);
975 session_leave(session, "session_get_memory_status");
980 struct session *new_session(NMEM nmem, struct conf_service *service,
984 struct session *session = nmem_malloc(nmem, sizeof(*session));
988 sprintf(tmp_str, "session#%u", session_id);
990 session->session_id = session_id;
991 session_log(session, YLOG_DEBUG, "New");
992 session->service = service;
993 session->relevance = 0;
994 session->total_records = 0;
995 session->number_of_warnings_unknown_elements = 0;
996 session->number_of_warnings_unknown_metadata = 0;
997 session->num_termlists = 0;
998 session->reclist = reclist_create(nmem);
999 session->clients_active = 0;
1000 session->clients_cached = 0;
1001 session->settings_modified = 0;
1002 session->session_nmem = nmem;
1003 session->nmem = nmem_create();
1004 session->databases = 0;
1005 session->sorted_results = 0;
1006 session->facet_limits = 0;
1007 session->mergekey = 0;
1009 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1011 session->watchlist[i].data = 0;
1012 session->watchlist[i].fun = 0;
1014 session->normalize_cache = normalize_cache_create();
1015 session->session_mutex = 0;
1016 pazpar2_mutex_create(&session->session_mutex, tmp_str);
1021 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf);
1023 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
1024 int *count, NMEM nmem)
1026 struct hitsbytarget *res = 0;
1027 struct client_list *l;
1030 for (l = se->clients_active; l; l = l->next)
1033 res = nmem_malloc(nmem, sizeof(*res) * sz);
1035 for (l = se->clients_active; l; l = l->next)
1037 struct client *cl = l->client;
1038 WRBUF w = wrbuf_alloc();
1039 const char *name = session_setting_oneval(client_get_database(cl),
1042 res[*count].id = client_get_id(cl);
1043 res[*count].name = *name ? name : "Unknown";
1044 res[*count].hits = client_get_hits(cl);
1045 res[*count].approximation = client_get_approximation(cl);
1046 res[*count].records = client_get_num_records(cl);
1047 res[*count].filtered = client_get_num_records_filtered(cl);
1048 res[*count].diagnostic =
1049 client_get_diagnostic(cl, &res[*count].message,
1050 &res[*count].addinfo);
1051 res[*count].state = client_get_state_str(cl);
1052 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1053 session_settings_dump(se, client_get_database(cl), w);
1054 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1057 res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1064 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1066 struct hitsbytarget *p;
1067 session_enter(se, "get_hitsbytarget");
1068 p = hitsbytarget_nb(se, count, nmem);
1069 session_leave(se, "get_hitsbytarget");
1073 // Compares two hitsbytarget nodes by hitcount
1074 static int cmp_ht(const void *p1, const void *p2)
1076 const struct hitsbytarget *h1 = p1;
1077 const struct hitsbytarget *h2 = p2;
1078 return h2->hits - h1->hits;
1081 // Compares two hitsbytarget nodes by hitcount
1082 static int cmp_ht_approx(const void *p1, const void *p2)
1084 const struct hitsbytarget *h1 = p1;
1085 const struct hitsbytarget *h2 = p2;
1086 return h2->approximation - h1->approximation;
1089 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1090 NMEM nmem, int version)
1092 struct hitsbytarget *ht;
1095 ht = hitsbytarget_nb(se, &count, nmem);
1097 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1099 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1100 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1103 // do only print terms which have display names
1105 wrbuf_puts(wrbuf, "<term>\n");
1107 wrbuf_puts(wrbuf, "<id>");
1108 wrbuf_xmlputs(wrbuf, ht[i].id);
1109 wrbuf_puts(wrbuf, "</id>\n");
1111 wrbuf_puts(wrbuf, "<name>");
1112 if (!ht[i].name || !ht[i].name[0])
1113 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1115 wrbuf_xmlputs(wrbuf, ht[i].name);
1116 wrbuf_puts(wrbuf, "</name>\n");
1118 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1122 // Should not print if we know it isn't a approximation.
1123 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1124 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1125 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1128 wrbuf_puts(wrbuf, "<state>");
1129 wrbuf_xmlputs(wrbuf, ht[i].state);
1130 wrbuf_puts(wrbuf, "</state>\n");
1132 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1134 wrbuf_puts(wrbuf, "</term>\n");
1139 void perform_termlist(struct http_channel *c, struct session *se,
1140 const char *name, int num, int version)
1143 NMEM nmem_tmp = nmem_create();
1150 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1152 session_enter(se, "perform_termlist");
1154 for (j = 0; j < num_names; j++)
1157 int must_generate_empty = 1; /* bug 5350 */
1159 for (i = 0; i < se->num_termlists; i++)
1161 tname = se->termlists[i].name;
1162 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1164 struct termlist_score **p = 0;
1167 wrbuf_puts(c->wrbuf, "<list name=\"");
1168 wrbuf_xmlputs(c->wrbuf, tname);
1169 wrbuf_puts(c->wrbuf, "\">\n");
1170 must_generate_empty = 0;
1172 p = termlist_highscore(se->termlists[i].termlist, &len,
1177 for (i = 0; i < len && i < num; i++)
1179 // prevent sending empty term elements
1180 if (!p[i]->display_term || !p[i]->display_term[0])
1183 wrbuf_puts(c->wrbuf, "<term>");
1184 wrbuf_puts(c->wrbuf, "<name>");
1185 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1186 wrbuf_puts(c->wrbuf, "</name>");
1188 wrbuf_printf(c->wrbuf,
1189 "<frequency>%d</frequency>",
1191 wrbuf_puts(c->wrbuf, "</term>\n");
1194 wrbuf_puts(c->wrbuf, "</list>\n");
1198 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1200 wrbuf_puts(c->wrbuf, "<list name=\"");
1201 wrbuf_xmlputs(c->wrbuf, tname);
1202 wrbuf_puts(c->wrbuf, "\">\n");
1204 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1205 wrbuf_puts(c->wrbuf, "</list>\n");
1206 must_generate_empty = 0;
1208 if (must_generate_empty)
1210 wrbuf_puts(c->wrbuf, "<list name=\"");
1211 wrbuf_xmlputs(c->wrbuf, names[j]);
1212 wrbuf_puts(c->wrbuf, "\"/>\n");
1215 session_leave(se, "perform_termlist");
1216 nmem_destroy(nmem_tmp);
1219 #ifdef MISSING_HEADERS
1220 void report_nmem_stats(void)
1222 size_t in_use, is_free;
1224 nmem_get_memory_in_use(&in_use);
1225 nmem_get_memory_free(&is_free);
1227 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1228 (long) in_use, (long) is_free);
1232 struct record_cluster *show_single_start(struct session *se, const char *id,
1233 struct record_cluster **prev_r,
1234 struct record_cluster **next_r)
1236 struct record_cluster *r = 0;
1238 session_enter(se, "show_single_start");
1241 reclist_limit(se->reclist, se);
1243 reclist_enter(se->reclist);
1244 while ((r = reclist_read_record(se->reclist)))
1246 if (!strcmp(r->recid, id))
1248 *next_r = reclist_read_record(se->reclist);
1253 reclist_leave(se->reclist);
1255 session_leave(se, "show_single_start");
1259 void show_single_stop(struct session *se, struct record_cluster *rec)
1261 session_leave(se, "show_single_stop");
1265 int session_fetch_more(struct session *se)
1267 struct client_list *l;
1270 for (l = se->clients_active; l; l = l->next)
1272 struct client *cl = l->client;
1273 if (client_get_state(cl) == Client_Idle)
1275 if (client_fetch_more(cl))
1277 session_log(se, YLOG_LOG, "%s: more to fetch",
1283 session_log(se, YLOG_LOG, "%s: no more to fetch",
1289 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1290 client_get_id(cl), client_get_state_str(cl));
1297 struct record_cluster **show_range_start(struct session *se,
1298 struct reclist_sortparms *sp,
1299 int start, int *num, int *total,
1300 Odr_int *sumhits, Odr_int *approx_hits,
1301 void (*show_records_ready)(void *data),
1302 struct http_channel *chan)
1304 struct record_cluster **recs = 0;
1305 struct reclist_sortparms *spp;
1306 struct client_list *l;
1309 yaz_timing_t t = yaz_timing_create();
1311 session_enter(se, "show_range_start");
1315 reclist_limit(se->reclist, se);
1318 for (spp = sp; spp; spp = spp->next)
1319 if (spp->type == Metadata_sortkey_relevance)
1321 relevance_prepare_read(se->relevance, se->reclist);
1324 for (l = se->clients_active; l; l = l->next) {
1325 *sumhits += client_get_hits(l->client);
1326 *approx_hits += client_get_approximation(l->client);
1329 reclist_sort(se->reclist, sp);
1331 reclist_enter(se->reclist);
1332 *total = reclist_get_num_records(se->reclist);
1334 for (l = se->clients_active; l; l = l->next)
1335 client_update_show_stat(l->client, 0);
1337 for (i = 0; i < start; i++)
1339 struct record_cluster *r = reclist_read_record(se->reclist);
1347 struct record *rec = r->records;
1348 for (;rec; rec = rec->next)
1349 client_update_show_stat(rec->client, 1);
1352 recs = nmem_malloc(se->nmem, (*num > 0 ? *num : 1) * sizeof(*recs));
1353 for (i = 0; i < *num; i++)
1355 struct record_cluster *r = reclist_read_record(se->reclist);
1363 struct record *rec = r->records;
1364 for (;rec; rec = rec->next)
1365 client_update_show_stat(rec->client, 1);
1369 reclist_leave(se->reclist);
1372 session_log(se, YLOG_LOG, "show %6.5f %3.2f %3.2f",
1373 yaz_timing_get_real(t), yaz_timing_get_user(t),
1374 yaz_timing_get_sys(t));
1375 yaz_timing_destroy(&t);
1378 if (!session_fetch_more(se))
1379 session_log(se, YLOG_LOG, "can not fetch more");
1382 show_range_stop(se, recs);
1383 session_log(se, YLOG_LOG, "fetching more in progress");
1384 if (session_set_watch(se, SESSION_WATCH_SHOW,
1385 show_records_ready, chan, chan))
1387 session_log(se, YLOG_WARN, "Ignoring show block");
1388 session_enter(se, "show_range_start");
1392 session_log(se, YLOG_LOG, "session watch OK");
1399 void show_range_stop(struct session *se, struct record_cluster **recs)
1401 session_leave(se, "show_range_stop");
1404 void statistics(struct session *se, struct statistics *stat)
1406 struct client_list *l;
1409 memset(stat, 0, sizeof(*stat));
1411 for (l = se->clients_active; l; l = l->next)
1413 struct client *cl = l->client;
1414 if (!client_get_connection(cl))
1415 stat->num_no_connection++;
1416 stat->num_hits += client_get_hits(cl);
1417 switch (client_get_state(cl))
1419 case Client_Connecting: stat->num_connecting++; break;
1420 case Client_Working: stat->num_working++; break;
1421 case Client_Idle: stat->num_idle++; break;
1422 case Client_Failed: stat->num_failed++; break;
1423 case Client_Error: stat->num_error++; break;
1428 stat->num_records = se->total_records;
1430 stat->num_clients = count;
1433 static struct record_metadata *record_metadata_init(
1434 NMEM nmem, const char *value, enum conf_metadata_type type,
1435 struct _xmlAttr *attr)
1437 struct record_metadata *rec_md = record_metadata_create(nmem);
1438 struct record_metadata_attr **attrp = &rec_md->attributes;
1440 for (; attr; attr = attr->next)
1442 if (attr->children && attr->children->content)
1444 if (strcmp((const char *) attr->name, "type")
1445 && strcmp((const char *) attr->name, "empty"))
1446 { /* skip the "type" + "empty" attribute..
1447 The "Type" is already part of the element in output
1448 (md-%s) and so repeating it here is redundant */
1449 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1451 nmem_strdup(nmem, (const char *) attr->name);
1453 nmem_strdup(nmem, (const char *) attr->children->content);
1454 attrp = &(*attrp)->next;
1460 if (type == Metadata_type_generic)
1462 char *p = nmem_strdup(nmem, value);
1464 p = normalize7bit_generic(p, " ,/.:([");
1466 rec_md->data.text.disp = p;
1467 rec_md->data.text.sort = 0;
1469 else if (type == Metadata_type_year || type == Metadata_type_date)
1474 if (type == Metadata_type_date)
1476 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1479 rec_md->data.number.min = first;
1480 rec_md->data.number.max = last;
1487 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1488 struct conf_service *service, WRBUF norm_wr)
1492 for (n = root->children; n; n = n->next)
1494 if (n->type != XML_ELEMENT_NODE)
1496 if (!strcmp((const char *) n->name, "metadata"))
1498 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1500 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1502 else if (!strcmp(name, (const char *) type))
1504 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1507 const char *norm_str;
1508 pp2_charset_token_t prt =
1509 pp2_charset_token_create(service->charsets, "mergekey");
1511 pp2_charset_token_first(prt, (const char *) value, 0);
1512 if (wrbuf_len(norm_wr) > 0)
1513 wrbuf_puts(norm_wr, " ");
1514 wrbuf_puts(norm_wr, name);
1516 pp2_charset_token_next(prt)))
1520 wrbuf_puts(norm_wr, " ");
1521 wrbuf_puts(norm_wr, norm_str);
1525 pp2_charset_token_destroy(prt);
1535 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1536 struct conf_service *service, NMEM nmem,
1537 const char *session_mergekey)
1539 char *mergekey_norm = 0;
1540 xmlNode *root = xmlDocGetRootElement(doc);
1541 WRBUF norm_wr = wrbuf_alloc();
1544 if (session_mergekey)
1548 nmem_strsplit_escape2(nmem, ",", session_mergekey, &values,
1551 for (i = 0; i < num; i++)
1552 get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
1554 else if ((mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1556 const char *norm_str;
1557 pp2_charset_token_t prt =
1558 pp2_charset_token_create(service->charsets, "mergekey");
1560 pp2_charset_token_first(prt, (const char *) mergekey, 0);
1561 while ((norm_str = pp2_charset_token_next(prt)))
1565 if (wrbuf_len(norm_wr))
1566 wrbuf_puts(norm_wr, " ");
1567 wrbuf_puts(norm_wr, norm_str);
1570 pp2_charset_token_destroy(prt);
1575 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1577 for (field_id = 0; field_id < service->num_metadata; field_id++)
1579 struct conf_metadata *ser_md = &service->metadata[field_id];
1580 if (ser_md->mergekey != Metadata_mergekey_no)
1582 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1584 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1586 /* no mergekey on this one and it is required..
1587 Generate unique key instead */
1588 wrbuf_rewind(norm_wr);
1595 /* generate unique key if none is not generated already or is empty */
1596 if (wrbuf_len(norm_wr) == 0)
1598 wrbuf_printf(norm_wr, "position: %s-%d",
1599 client_get_id(cl), record_no);
1603 const char *lead = "content: ";
1604 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1606 if (wrbuf_len(norm_wr) > 0)
1607 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1608 wrbuf_destroy(norm_wr);
1609 return mergekey_norm;
1612 /** \brief see if metadata for pz:recordfilter exists
1613 \param root xml root element of normalized record
1614 \param sdb session database for client
1615 \retval 0 if there is no metadata for pz:recordfilter
1616 \retval 1 if there is metadata for pz:recordfilter
1618 If there is no pz:recordfilter defined, this function returns 1
1622 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1627 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1632 for (n = root->children; n; n = n->next)
1634 if (n->type != XML_ELEMENT_NODE)
1636 if (!strcmp((const char *) n->name, "metadata"))
1638 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1645 if ((eq = strchr(s, '=')))
1647 else if ((eq = strchr(s, '~')))
1653 if (len == strlen((const char *)type) &&
1654 !memcmp((const char *) type, s, len))
1656 xmlChar *value = xmlNodeGetContent(n);
1657 if (value && *value)
1660 (substring && strstr((const char *) value, eq+1)) ||
1661 (!substring && !strcmp((const char *) value, eq + 1)))
1674 static int ingest_to_cluster(struct client *cl,
1678 const char *mergekey_norm);
1680 /** \brief ingest XML record
1681 \param cl client holds the result set for record
1682 \param rec record buffer (0 terminated)
1683 \param record_no record position (1, 2, ..)
1684 \param nmem working NMEM
1689 int ingest_record(struct client *cl, const char *rec,
1690 int record_no, NMEM nmem)
1692 struct session *se = client_get_session(cl);
1694 struct session_database *sdb = client_get_database(cl);
1695 struct conf_service *service = se->service;
1696 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1698 const char *mergekey_norm;
1703 root = xmlDocGetRootElement(xdoc);
1705 if (!check_record_filter(root, sdb))
1707 session_log(se, YLOG_LOG, "Filtered out record no %d from %s", record_no, sdb->database->id);
1712 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem,
1716 session_log(se, YLOG_WARN, "Got no mergekey");
1720 session_enter(se, "ingest_record");
1721 if (client_get_session(cl) == se && se->relevance)
1722 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1723 session_leave(se, "ingest_record");
1729 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1730 // struct record_metadata *rec_md = record->metadata[md_field_id];
1731 static int match_metadata_local(struct conf_metadata *ser_md,
1732 struct record_metadata *rec_md0,
1733 char **values, int num_v)
1736 struct record_metadata *rec_md = rec_md0;
1737 for (i = 0; i < num_v; )
1741 if (ser_md->type == Metadata_type_year
1742 || ser_md->type == Metadata_type_date)
1744 int y = atoi(values[i]);
1745 if (y >= rec_md->data.number.min
1746 && y <= rec_md->data.number.max)
1751 yaz_log(YLOG_DEBUG, "cmp: '%s' '%s'", rec_md->data.text.disp, values[i]);
1752 if (!strcmp(rec_md->data.text.disp, values[i]))
1754 // Value equals, should not be filtered.
1758 rec_md = rec_md->next;
1766 return i < num_v ? 1 : 0;
1769 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
1772 struct conf_service *service = se->service;
1776 NMEM nmem_tmp = nmem_create();
1778 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
1781 for (j = 0; j < service->num_metadata; j++)
1783 struct conf_metadata *md = service->metadata + j;
1784 if (!strcmp(md->name, name) && md->limitcluster)
1789 conf_service_metadata_field_id(service,
1792 if (md_field_id < 0)
1798 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1801 if (!match_metadata_local(&service->metadata[md_field_id],
1802 rec->metadata[md_field_id],
1811 nmem_destroy(nmem_tmp);
1815 // Skip record on non-zero
1816 static int check_limit_local(struct client *cl,
1817 struct record *record,
1820 int skip_record = 0;
1821 struct session *se = client_get_session(cl);
1822 struct conf_service *service = se->service;
1823 NMEM nmem_tmp = nmem_create();
1824 struct session_database *sdb = client_get_database(cl);
1826 while (!skip_record)
1832 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
1837 if (!strcmp(name, "*"))
1839 for (md_field_id = 0; md_field_id < service->num_metadata;
1842 if (match_metadata_local(
1843 &service->metadata[md_field_id],
1844 record->metadata[md_field_id],
1848 if (md_field_id == service->num_metadata)
1853 md_field_id = conf_service_metadata_field_id(service, name);
1854 if (md_field_id < 0)
1859 if (!match_metadata_local(
1860 &service->metadata[md_field_id],
1861 record->metadata[md_field_id],
1868 nmem_destroy(nmem_tmp);
1872 static int ingest_to_cluster(struct client *cl,
1876 const char *mergekey_norm)
1881 struct session *se = client_get_session(cl);
1882 struct conf_service *service = se->service;
1883 int term_factor = 1;
1884 struct record_cluster *cluster;
1885 struct record_metadata **metadata0;
1886 struct session_database *sdb = client_get_database(cl);
1887 struct record *record = record_create(se->nmem,
1888 service->num_metadata,
1889 service->num_sortkeys, cl,
1892 for (n = root->children; n; n = n->next)
1900 if (n->type != XML_ELEMENT_NODE)
1902 if (!strcmp((const char *) n->name, "metadata"))
1904 struct conf_metadata *ser_md = 0;
1905 struct record_metadata **wheretoput = 0;
1906 struct record_metadata *rec_md = 0;
1907 int md_field_id = -1;
1909 type = xmlGetProp(n, (xmlChar *) "type");
1910 value = xmlNodeListGetString(xdoc, n->children, 1);
1913 if (!value || !*value)
1915 xmlChar *empty = xmlGetProp(n, (xmlChar *) "empty");
1923 = conf_service_metadata_field_id(service, (const char *) type);
1924 if (md_field_id < 0)
1926 if (se->number_of_warnings_unknown_metadata == 0)
1928 session_log(se, YLOG_WARN,
1929 "Ignoring unknown metadata element: %s", type);
1931 se->number_of_warnings_unknown_metadata++;
1935 ser_md = &service->metadata[md_field_id];
1937 // non-merged metadata
1938 rec_md = record_metadata_init(se->nmem, (const char *) value,
1939 ser_md->type, n->properties);
1942 session_log(se, YLOG_WARN, "bad metadata data '%s' "
1943 "for element '%s'", value, type);
1946 wheretoput = &record->metadata[md_field_id];
1948 wheretoput = &(*wheretoput)->next;
1949 *wheretoput = rec_md;
1953 if (check_limit_local(cl, record, record_no))
1955 session_log(se, YLOG_LOG, "Facet filtered out record no %d from %s",
1956 record_no, sdb->database->id);
1963 cluster = reclist_insert(se->reclist, service, record,
1964 mergekey_norm, &se->total_merged);
1966 return 0; // complete match with existing record
1969 const char *use_term_factor_str =
1970 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
1971 if (use_term_factor_str && use_term_factor_str[0] == '1')
1973 int maxrecs = client_get_maxrecs(cl);
1974 int hits = (int) client_get_hits(cl);
1975 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
1976 assert(term_factor >= 1);
1977 session_log(se, YLOG_DEBUG, "Using term factor: %d (%d / %d)",
1978 term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
1982 if (global_parameters.dump_records)
1983 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1984 sdb->database->id, record_no);
1987 relevance_newrec(se->relevance, cluster);
1989 // original metadata, to check if first existence of a field
1990 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
1991 memcpy(metadata0, cluster->metadata,
1992 sizeof(*metadata0) * service->num_metadata);
1994 // now parsing XML record and adding data to cluster or record metadata
1995 for (n = root->children; n; n = n->next)
1997 pp2_charset_token_t prt;
2004 if (n->type != XML_ELEMENT_NODE)
2006 if (!strcmp((const char *) n->name, "metadata"))
2008 struct conf_metadata *ser_md = 0;
2009 struct conf_sortkey *ser_sk = 0;
2010 struct record_metadata **wheretoput = 0;
2011 struct record_metadata *rec_md = 0;
2012 int md_field_id = -1;
2013 int sk_field_id = -1;
2017 type = xmlGetProp(n, (xmlChar *) "type");
2018 value = xmlNodeListGetString(xdoc, n->children, 1);
2020 if (!type || !value || !*value)
2024 = conf_service_metadata_field_id(service, (const char *) type);
2025 if (md_field_id < 0)
2028 ser_md = &service->metadata[md_field_id];
2030 if (ser_md->sortkey_offset >= 0)
2032 sk_field_id = ser_md->sortkey_offset;
2033 ser_sk = &service->sortkeys[sk_field_id];
2037 rec_md = record_metadata_init(se->nmem, (const char *) value,
2040 // see if the field was not in cluster already (from beginning)
2045 xml_rank = xmlGetProp(n, (xmlChar *) "rank");
2046 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
2048 wheretoput = &cluster->metadata[md_field_id];
2050 if (ser_md->merge == Metadata_merge_first)
2052 if (!metadata0[md_field_id])
2055 wheretoput = &(*wheretoput)->next;
2056 *wheretoput = rec_md;
2059 else if (ser_md->merge == Metadata_merge_unique)
2063 if (!strcmp((const char *) (*wheretoput)->data.text.disp,
2064 rec_md->data.text.disp))
2066 wheretoput = &(*wheretoput)->next;
2069 *wheretoput = rec_md;
2071 else if (ser_md->merge == Metadata_merge_longest)
2074 || strlen(rec_md->data.text.disp)
2075 > strlen((*wheretoput)->data.text.disp))
2077 *wheretoput = rec_md;
2080 const char *sort_str = 0;
2082 ser_sk->type == Metadata_sortkey_skiparticle;
2084 if (!cluster->sortkeys[sk_field_id])
2085 cluster->sortkeys[sk_field_id] =
2086 nmem_malloc(se->nmem,
2087 sizeof(union data_types));
2090 pp2_charset_token_create(service->charsets, "sort");
2092 pp2_charset_token_first(prt, rec_md->data.text.disp,
2095 pp2_charset_token_next(prt);
2097 sort_str = pp2_get_sort(prt);
2099 cluster->sortkeys[sk_field_id]->text.disp =
2100 rec_md->data.text.disp;
2103 sort_str = rec_md->data.text.disp;
2104 session_log(se, YLOG_WARN,
2105 "Could not make sortkey. Bug #1858");
2107 cluster->sortkeys[sk_field_id]->text.sort =
2108 nmem_strdup(se->nmem, sort_str);
2109 pp2_charset_token_destroy(prt);
2113 else if (ser_md->merge == Metadata_merge_all)
2116 wheretoput = &(*wheretoput)->next;
2117 *wheretoput = rec_md;
2119 else if (ser_md->merge == Metadata_merge_range)
2123 *wheretoput = rec_md;
2125 cluster->sortkeys[sk_field_id]
2130 int this_min = rec_md->data.number.min;
2131 int this_max = rec_md->data.number.max;
2132 if (this_min < (*wheretoput)->data.number.min)
2133 (*wheretoput)->data.number.min = this_min;
2134 if (this_max > (*wheretoput)->data.number.max)
2135 (*wheretoput)->data.number.max = this_max;
2139 // ranking of _all_ fields enabled ...
2142 relevance_countwords(se->relevance, cluster,
2143 (char *) value, rank, ser_md->name);
2146 // construct facets ... unless the client already has reported them
2147 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2149 if (ser_md->type == Metadata_type_year)
2152 sprintf(year, "%d", rec_md->data.number.max);
2154 add_facet(se, (char *) type, year, term_factor);
2155 if (rec_md->data.number.max != rec_md->data.number.min)
2157 sprintf(year, "%d", rec_md->data.number.min);
2158 add_facet(se, (char *) type, year, term_factor);
2162 add_facet(se, (char *) type, (char *) value, term_factor);
2174 if (se->number_of_warnings_unknown_elements == 0)
2175 session_log(se, YLOG_WARN,
2176 "Unexpected element in internal record: %s", n->name);
2177 se->number_of_warnings_unknown_elements++;
2186 relevance_donerecord(se->relevance, cluster);
2187 se->total_records++;
2192 void session_log(struct session *s, int level, const char *fmt, ...)
2198 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2199 yaz_log(level, "Session %u: %s", s ? s->session_id : 0, buf);
2207 * c-file-style: "Stroustrup"
2208 * indent-tabs-mode: nil
2210 * vim: shiftwidth=4 tabstop=8 expandtab