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;
101 static int session_use(int delta)
104 if (!g_session_mutex)
105 yaz_mutex_create(&g_session_mutex);
106 yaz_mutex_enter(g_session_mutex);
107 no_sessions += delta;
108 sessions = no_sessions;
109 yaz_mutex_leave(g_session_mutex);
110 yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" :
111 (delta > 0 ? "INC" : "DEC"), no_sessions);
115 int sessions_count(void)
117 return session_use(0);
120 static void log_xml_doc(xmlDoc *doc)
122 FILE *lf = yaz_log_file();
125 #if LIBXML_VERSION >= 20600
126 xmlDocDumpFormatMemory(doc, &result, &len, 1);
128 xmlDocDumpMemory(doc, &result, &len);
132 (void) fwrite(result, 1, len, lf);
138 static void session_enter(struct session *s, const char *caller)
141 session_log(s, YLOG_DEBUG, "Session lock by %s", caller);
142 yaz_mutex_enter(s->session_mutex);
145 static void session_leave(struct session *s, const char *caller)
147 yaz_mutex_leave(s->session_mutex);
149 session_log(s, YLOG_DEBUG, "Session unlock by %s", caller);
152 static void session_normalize_facet(struct session *s,
153 const char *type, const char *value,
154 WRBUF display_wrbuf, WRBUF facet_wrbuf)
156 struct conf_service *service = s->service;
157 pp2_charset_token_t prt;
158 const char *facet_component;
160 const char *icu_chain_id = 0;
162 for (i = 0; i < service->num_metadata; i++)
163 if (!strcmp((service->metadata + i)->name, type))
164 icu_chain_id = (service->metadata + i)->facetrule;
166 icu_chain_id = "facet";
167 prt = pp2_charset_token_create(service->charsets, icu_chain_id);
170 session_log(s, YLOG_FATAL,
171 "Unknown ICU chain '%s' for facet of type '%s'",
173 wrbuf_destroy(facet_wrbuf);
174 wrbuf_destroy(display_wrbuf);
177 pp2_charset_token_first(prt, value, 0);
178 while ((facet_component = pp2_charset_token_next(prt)))
180 const char *display_component;
181 if (*facet_component)
183 if (wrbuf_len(facet_wrbuf))
184 wrbuf_puts(facet_wrbuf, " ");
185 wrbuf_puts(facet_wrbuf, facet_component);
187 display_component = pp2_get_display(prt);
188 if (display_component)
190 if (wrbuf_len(display_wrbuf))
191 wrbuf_puts(display_wrbuf, " ");
192 wrbuf_puts(display_wrbuf, display_component);
195 pp2_charset_token_destroy(prt);
198 void add_facet(struct session *s, const char *type, const char *value, int count)
200 WRBUF facet_wrbuf = wrbuf_alloc();
201 WRBUF display_wrbuf = wrbuf_alloc();
203 session_normalize_facet(s, type, value, display_wrbuf, facet_wrbuf);
205 if (wrbuf_len(facet_wrbuf))
208 for (i = 0; i < s->num_termlists; i++)
209 if (!strcmp(s->termlists[i].name, type))
211 if (i == s->num_termlists)
213 if (i == SESSION_MAX_TERMLISTS)
215 session_log(s, YLOG_FATAL, "Too many termlists");
216 wrbuf_destroy(facet_wrbuf);
217 wrbuf_destroy(display_wrbuf);
221 s->termlists[i].name = nmem_strdup(s->nmem, type);
222 s->termlists[i].termlist = termlist_create(s->nmem);
223 s->num_termlists = i + 1;
227 session_log(s, YLOG_LOG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
229 termlist_insert(s->termlists[i].termlist, wrbuf_cstr(display_wrbuf),
230 wrbuf_cstr(facet_wrbuf), count);
232 wrbuf_destroy(facet_wrbuf);
233 wrbuf_destroy(display_wrbuf);
236 static xmlDoc *record_to_xml(struct session *se,
237 struct session_database *sdb, const char *rec)
239 struct database *db = sdb->database;
242 rdoc = xmlParseMemory(rec, strlen(rec));
246 session_log(se, YLOG_WARN, "Non-wellformed XML");
250 if (global_parameters.dump_records)
252 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->id);
259 #define MAX_XSLT_ARGS 16
261 // Add static values from session database settings if applicable
262 static void insert_settings_parameters(struct session_database *sdb,
263 struct conf_service *service,
271 for (i = 0; i < service->num_metadata; i++)
273 struct conf_metadata *md = &service->metadata[i];
276 if (md->setting == Metadata_setting_parameter &&
277 (setting = settings_lookup_offset(service, md->name)) >= 0)
279 const char *val = session_setting_oneval(sdb, setting);
280 if (val && nparms < MAX_XSLT_ARGS)
283 int len = strlen(val);
284 buf = nmem_malloc(nmem, len + 3);
286 strcpy(buf + 1, val);
289 parms[offset++] = md->name;
290 parms[offset++] = buf;
298 // Add static values from session database settings if applicable
299 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
301 struct conf_service *service)
305 for (i = 0; i < service->num_metadata; i++)
307 struct conf_metadata *md = &service->metadata[i];
310 if (md->setting == Metadata_setting_postproc &&
311 (offset = settings_lookup_offset(service, md->name)) >= 0)
313 const char *val = session_setting_oneval(sdb, offset);
316 xmlNode *n = xmlNewTextChild(root, 0, (xmlChar *) "metadata",
318 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
324 static xmlDoc *normalize_record(struct session *se,
325 struct session_database *sdb,
326 struct conf_service *service,
327 const char *rec, NMEM nmem)
329 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
333 char *parms[MAX_XSLT_ARGS*2+1];
335 insert_settings_parameters(sdb, service, parms, nmem);
337 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
339 session_log(se, YLOG_WARN, "Normalize failed");
345 void session_settings_dump(struct session *se,
346 struct session_database *db,
351 int i, num = db->num_settings;
352 for (i = 0; i < num; i++)
354 struct setting *s = db->settings[i];
355 for (;s ; s = s->next)
357 wrbuf_puts(w, "<set name=\"");
358 wrbuf_xmlputs(w, s->name);
359 wrbuf_puts(w, "\" value=\"");
360 wrbuf_xmlputs(w, s->value);
361 wrbuf_puts(w, "\"/>");
369 // Retrieve first defined value for 'name' for given database.
370 // Will be extended to take into account user associated with session
371 const char *session_setting_oneval(struct session_database *db, int offset)
373 if (offset >= db->num_settings || !db->settings[offset])
375 return db->settings[offset]->value;
378 // Prepare XSLT stylesheets for record normalization
379 // Structures are allocated on the session_wide nmem to avoid having
380 // to recompute this for every search. This would lead
381 // to leaking if a single session was to repeatedly change the PZ_XSLT
382 // setting. However, this is not a realistic use scenario.
383 static int prepare_map(struct session *se, struct session_database *sdb)
385 if (sdb->settings && !sdb->map)
389 if (sdb->settings[PZ_XSLT] &&
390 (s = session_setting_oneval(sdb, PZ_XSLT)))
392 char auto_stylesheet[256];
394 if (!strcmp(s, "auto"))
396 const char *request_syntax = session_setting_oneval(
397 sdb, PZ_REQUESTSYNTAX);
401 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
402 "%s.xsl", request_syntax);
403 for (cp = auto_stylesheet; *cp; cp++)
405 /* deliberately only consider ASCII */
406 if (*cp > 32 && *cp < 127)
413 session_log(se, YLOG_WARN,
414 "No pz:requestsyntax for auto stylesheet");
417 sdb->map = normalize_cache_get(se->normalize_cache,
426 // called if watch should be removed because http_channel is to be destroyed
427 static void session_watch_cancel(void *data, struct http_channel *c,
430 struct session_watchentry *ent = data;
437 // set watch. Returns 0=OK, -1 if watch is already set
438 int session_set_watch(struct session *s, int what,
439 session_watchfun fun, void *data,
440 struct http_channel *chan)
443 session_enter(s, "session_set_watch");
444 if (s->watchlist[what].fun)
449 s->watchlist[what].fun = fun;
450 s->watchlist[what].data = data;
451 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
452 session_watch_cancel);
455 session_leave(s, "session_set_watch");
459 void session_alert_watch(struct session *s, int what)
462 session_enter(s, "session_alert_watch");
463 if (s->watchlist[what].fun)
465 /* our watch is no longer associated with http_channel */
467 session_watchfun fun;
469 http_remove_observer(s->watchlist[what].obs);
470 fun = s->watchlist[what].fun;
471 data = s->watchlist[what].data;
473 /* reset watch before fun is invoked - in case fun wants to set
475 s->watchlist[what].fun = 0;
476 s->watchlist[what].data = 0;
477 s->watchlist[what].obs = 0;
479 session_leave(s, "session_alert_watch");
480 session_log(s, YLOG_DEBUG,
481 "Alert Watch: %d calling function: %p", what, fun);
485 session_leave(s,"session_alert_watch");
488 //callback for grep_databases
489 static void select_targets_callback(struct session *se,
490 struct session_database *db)
493 struct client_list *l;
495 for (l = se->clients_cached; l; l = l->next)
496 if (client_get_database(l->client) == db)
503 cl = client_create(db->database->id);
504 client_set_database(cl, db);
506 l = xmalloc(sizeof(*l));
508 l->next = se->clients_cached;
509 se->clients_cached = l;
511 /* set session always. If may be 0 if client is not active */
512 client_set_session(cl, se);
514 l = xmalloc(sizeof(*l));
516 l->next = se->clients_active;
517 se->clients_active = l;
520 static void session_reset_active_clients(struct session *se,
521 struct client_list *new_list)
523 struct client_list *l;
525 session_enter(se, "session_reset_active_clients");
526 l = se->clients_active;
527 se->clients_active = new_list;
528 session_leave(se, "session_reset_active_clients");
532 struct client_list *l_next = l->next;
534 client_lock(l->client);
535 client_set_session(l->client, 0); /* mark client inactive */
536 client_unlock(l->client);
543 static void session_remove_cached_clients(struct session *se)
545 struct client_list *l;
547 session_reset_active_clients(se, 0);
549 session_enter(se, "session_remove_cached_clients");
550 l = se->clients_cached;
551 se->clients_cached = 0;
552 session_leave(se, "session_remove_cached_clients");
556 struct client_list *l_next = l->next;
557 client_lock(l->client);
558 client_set_session(l->client, 0);
559 client_set_database(l->client, 0);
560 client_unlock(l->client);
561 client_destroy(l->client);
567 // Associates a set of clients with a session;
568 // Note: Session-databases represent databases with per-session
570 static int select_targets(struct session *se, const char *filter)
572 return session_grep_databases(se, filter, select_targets_callback);
575 int session_active_clients(struct session *s)
577 struct client_list *l;
580 for (l = s->clients_active; l; l = l->next)
581 if (client_is_active(l->client))
587 int session_is_preferred_clients_ready(struct session *s)
589 struct client_list *l;
592 for (l = s->clients_active; l; l = l->next)
593 if (client_is_active_preferred(l->client))
595 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
599 static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
601 reclist_destroy(se->reclist);
602 if (nmem_total(se->nmem))
603 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd",
604 nmem_total(se->nmem));
605 nmem_reset(se->nmem);
606 se->total_records = se->total_merged = 0;
607 se->num_termlists = 0;
609 relevance_clear(se->relevance);
611 /* reset list of sorted results and clear to relevance search */
612 se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
613 se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
614 se->sorted_results->increasing = sp->increasing;
615 se->sorted_results->type = sp->type;
616 se->sorted_results->next = 0;
618 session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
619 sp->name, sp->increasing, sp->type);
621 se->reclist = reclist_create(se->nmem);
624 static void session_sort_unlocked(struct session *se,
625 struct reclist_sortparms *sp,
626 const char *mergekey,
629 struct client_list *l;
630 const char *field = sp->name;
631 int increasing = sp->increasing;
633 int clients_research = 0;
635 session_log(se, YLOG_DEBUG, "session_sort field=%s increasing=%d type=%d",
636 field, increasing, type);
638 if (rank && (!se->rank || strcmp(se->rank, rank)))
640 /* new rank must research/reingest anyway */
643 se->rank = *rank ? xstrdup(rank) : 0;
644 clients_research = 1;
645 session_log(se, YLOG_DEBUG, "session_sort: new rank = %s",
648 if (mergekey && (!se->mergekey || strcmp(se->mergekey, mergekey)))
650 /* new mergekey must research/reingest anyway */
653 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
654 clients_research = 1;
655 session_log(se, YLOG_DEBUG, "session_sort: new mergekey = %s",
658 if (clients_research == 0)
660 struct reclist_sortparms *sr;
661 for (sr = se->sorted_results; sr; sr = sr->next)
662 if (!reclist_sortparms_cmp(sr, sp))
666 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d already fetched",
667 field, increasing, type);
671 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d must fetch",
672 field, increasing, type);
674 // We need to reset reclist on every sort that changes the records, not just for position
675 // So if just one client requires new searching, we need to clear set.
676 // Ask each of the client if sorting requires re-search due to native sort
677 // If it does it will require us to
678 for (l = se->clients_active; l; l = l->next)
680 struct client *cl = l->client;
681 // Assume no re-search is required.
682 client_parse_init(cl, 1);
683 clients_research += client_parse_sort(cl, sp);
685 if (clients_research)
687 session_log(se, YLOG_DEBUG,
688 "session_sort: reset results due to %d clients researching",
690 session_clear_set(se, sp);
694 // A new sorting based on same record set
695 struct reclist_sortparms *sr = nmem_malloc(se->nmem, sizeof(*sr));
696 sr->name = nmem_strdup(se->nmem, field);
697 sr->increasing = increasing;
699 sr->next = se->sorted_results;
700 se->sorted_results = sr;
701 session_log(se, YLOG_DEBUG, "session_sort: no research/ingesting done");
704 session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
706 for (l = se->clients_active; l; l = l->next)
708 struct client *cl = l->client;
709 if (client_get_state(cl) == Client_Connecting ||
710 client_get_state(cl) == Client_Idle ||
711 client_get_state(cl) == Client_Working) {
712 client_start_search(cl);
716 session_log(se, YLOG_DEBUG,
717 "session_sort: %s: No re-start/ingest in show. "
718 "Wrong client state: %d",
719 client_get_id(cl), client_get_state(cl));
724 void session_sort(struct session *se, struct reclist_sortparms *sp,
725 const char *mergekey, const char *rank)
727 //session_enter(se, "session_sort");
728 session_sort_unlocked(se, sp, mergekey, rank);
729 //session_leave(se, "session_sort");
733 enum pazpar2_error_code session_search(struct session *se,
735 const char *startrecs,
739 const char **addinfo,
740 const char **addinfo2,
741 struct reclist_sortparms *sp,
742 const char *mergekey,
745 int live_channels = 0;
747 int no_failed_query = 0;
748 int no_failed_limit = 0;
749 struct client_list *l, *l0;
751 session_alert_watch(se, SESSION_WATCH_SHOW);
752 session_alert_watch(se, SESSION_WATCH_BYTARGET);
753 session_alert_watch(se, SESSION_WATCH_TERMLIST);
754 session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
756 session_log(se, YLOG_DEBUG, "Search");
760 if (se->settings_modified) {
761 session_remove_cached_clients(se);
764 session_reset_active_clients(se, 0);
766 session_enter(se, "session_search");
767 se->settings_modified = 0;
772 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
777 se->rank = *rank ? xstrdup(rank) : 0;
780 session_clear_set(se, sp);
781 relevance_destroy(&se->relevance);
783 live_channels = select_targets(se, filter);
786 session_leave(se, "session_search");
787 return PAZPAR2_NO_TARGETS;
790 facet_limits_destroy(se->facet_limits);
791 se->facet_limits = facet_limits_create(limit);
792 if (!se->facet_limits)
795 session_leave(se, "session_search");
796 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
799 l0 = se->clients_active;
800 se->clients_active = 0;
801 session_leave(se, "session_search");
803 for (l = l0; l; l = l->next)
806 struct client *cl = l->client;
807 client_parse_init(cl, 1);
808 if (prepare_map(se, client_get_database(cl)) < 0)
811 parse_ret = client_parse_query(cl, query, se->facet_limits, addinfo2);
814 else if (parse_ret == -2)
816 else if (parse_ret < 0)
817 no_working++; /* other error, such as bad CCL map */
820 client_parse_range(cl, startrecs, maxrecs);
821 client_parse_sort(cl, sp);
822 client_start_search(cl);
826 session_reset_active_clients(se, l0);
830 if (no_failed_query > 0)
833 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
835 else if (no_failed_limit > 0)
838 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
841 return PAZPAR2_NO_TARGETS;
843 return PAZPAR2_NO_ERROR;
846 // Creates a new session_database object for a database
847 static void session_init_databases_fun(void *context, struct database *db)
849 struct session *se = (struct session *) context;
850 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
856 assert(db->settings);
857 new->settings = nmem_malloc(se->session_nmem,
858 sizeof(struct settings *) * db->num_settings);
859 new->num_settings = db->num_settings;
860 for (i = 0; i < db->num_settings; i++)
862 struct setting *setting = db->settings[i];
863 new->settings[i] = setting;
865 new->next = se->databases;
869 // Doesn't free memory associated with sdb -- nmem takes care of that
870 static void session_database_destroy(struct session_database *sdb)
875 // Initialize session_database list -- this represents this session's view
876 // of the database list -- subject to modification by the settings ws command
877 void session_init_databases(struct session *se)
880 predef_grep_databases(se, se->service, session_init_databases_fun);
883 // Probably session_init_databases_fun should be refactored instead of
885 static struct session_database *load_session_database(struct session *se,
888 struct database *db = new_database_inherit_settings(id, se->session_nmem, se->service->settings);
889 session_init_databases_fun((void*) se, db);
891 // New sdb is head of se->databases list
892 return se->databases;
895 // Find an existing session database. If not found, load it
896 static struct session_database *find_session_database(struct session *se,
899 struct session_database *sdb;
901 for (sdb = se->databases; sdb; sdb = sdb->next)
902 if (!strcmp(sdb->database->id, id))
904 return load_session_database(se, id);
907 // Apply a session override to a database
908 void session_apply_setting(struct session *se, char *dbname, char *setting,
911 struct session_database *sdb = find_session_database(se, dbname);
912 struct conf_service *service = se->service;
913 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
914 int offset = settings_create_offset(service, setting);
916 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
919 new->target = dbname;
922 new->next = sdb->settings[offset];
923 sdb->settings[offset] = new;
925 se->settings_modified = 1;
927 // Force later recompute of settings-driven data structures
928 // (happens when a search starts and client connections are prepared)
940 void session_destroy(struct session *se)
942 struct session_database *sdb;
943 session_log(se, YLOG_LOG, "destroy");
945 session_remove_cached_clients(se);
947 for (sdb = se->databases; sdb; sdb = sdb->next)
948 session_database_destroy(sdb);
949 normalize_cache_destroy(se->normalize_cache);
950 relevance_destroy(&se->relevance);
951 reclist_destroy(se->reclist);
954 if (nmem_total(se->nmem))
955 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
956 if (nmem_total(se->session_nmem))
957 session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
958 facet_limits_destroy(se->facet_limits);
959 nmem_destroy(se->nmem);
960 service_destroy(se->service);
961 yaz_mutex_destroy(&se->session_mutex);
964 size_t session_get_memory_status(struct session *session) {
968 session_enter(session, "session_get_memory_status");
969 session_nmem = nmem_total(session->nmem);
970 session_leave(session, "session_get_memory_status");
975 struct session *new_session(NMEM nmem, struct conf_service *service,
979 struct session *session = nmem_malloc(nmem, sizeof(*session));
983 sprintf(tmp_str, "session#%u", session_id);
985 session->session_id = session_id;
986 session_log(session, YLOG_DEBUG, "New");
987 session->service = service;
988 session->relevance = 0;
989 session->total_records = 0;
990 session->number_of_warnings_unknown_elements = 0;
991 session->number_of_warnings_unknown_metadata = 0;
992 session->num_termlists = 0;
993 session->reclist = reclist_create(nmem);
994 session->clients_active = 0;
995 session->clients_cached = 0;
996 session->settings_modified = 0;
997 session->session_nmem = nmem;
998 session->nmem = nmem_create();
999 session->databases = 0;
1000 session->sorted_results = 0;
1001 session->facet_limits = 0;
1002 session->mergekey = 0;
1005 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1007 session->watchlist[i].data = 0;
1008 session->watchlist[i].fun = 0;
1010 session->normalize_cache = normalize_cache_create();
1011 session->session_mutex = 0;
1012 pazpar2_mutex_create(&session->session_mutex, tmp_str);
1013 session_log(session, YLOG_LOG, "create");
1019 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf);
1021 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
1022 int *count, NMEM nmem)
1024 struct hitsbytarget *res = 0;
1025 struct client_list *l;
1028 for (l = se->clients_active; l; l = l->next)
1031 res = nmem_malloc(nmem, sizeof(*res) * sz);
1033 for (l = se->clients_active; l; l = l->next)
1035 struct client *cl = l->client;
1036 WRBUF w = wrbuf_alloc();
1037 const char *name = session_setting_oneval(client_get_database(cl),
1040 res[*count].id = client_get_id(cl);
1041 res[*count].name = *name ? name : "Unknown";
1042 res[*count].hits = client_get_hits(cl);
1043 res[*count].approximation = client_get_approximation(cl);
1044 res[*count].records = client_get_num_records(cl);
1045 res[*count].filtered = client_get_num_records_filtered(cl);
1046 res[*count].diagnostic =
1047 client_get_diagnostic(cl, &res[*count].message,
1048 &res[*count].addinfo);
1049 res[*count].state = client_get_state_str(cl);
1050 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1051 session_settings_dump(se, client_get_database(cl), w);
1052 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1055 res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1062 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1064 struct hitsbytarget *p;
1065 session_enter(se, "get_hitsbytarget");
1066 p = hitsbytarget_nb(se, count, nmem);
1067 session_leave(se, "get_hitsbytarget");
1071 // Compares two hitsbytarget nodes by hitcount
1072 static int cmp_ht(const void *p1, const void *p2)
1074 const struct hitsbytarget *h1 = p1;
1075 const struct hitsbytarget *h2 = p2;
1076 return h2->hits - h1->hits;
1079 // Compares two hitsbytarget nodes by hitcount
1080 static int cmp_ht_approx(const void *p1, const void *p2)
1082 const struct hitsbytarget *h1 = p1;
1083 const struct hitsbytarget *h2 = p2;
1084 return h2->approximation - h1->approximation;
1087 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1088 NMEM nmem, int version)
1090 struct hitsbytarget *ht;
1093 ht = hitsbytarget_nb(se, &count, nmem);
1095 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1097 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1098 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1101 // do only print terms which have display names
1103 wrbuf_puts(wrbuf, "<term>\n");
1105 wrbuf_puts(wrbuf, "<id>");
1106 wrbuf_xmlputs(wrbuf, ht[i].id);
1107 wrbuf_puts(wrbuf, "</id>\n");
1109 wrbuf_puts(wrbuf, "<name>");
1110 if (!ht[i].name || !ht[i].name[0])
1111 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1113 wrbuf_xmlputs(wrbuf, ht[i].name);
1114 wrbuf_puts(wrbuf, "</name>\n");
1116 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1120 // Should not print if we know it isn't a approximation.
1121 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1122 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1123 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1126 wrbuf_puts(wrbuf, "<state>");
1127 wrbuf_xmlputs(wrbuf, ht[i].state);
1128 wrbuf_puts(wrbuf, "</state>\n");
1130 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1132 wrbuf_puts(wrbuf, "</term>\n");
1137 void perform_termlist(struct http_channel *c, struct session *se,
1138 const char *name, int num, int version)
1141 NMEM nmem_tmp = nmem_create();
1148 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1150 session_enter(se, "perform_termlist");
1152 for (j = 0; j < num_names; j++)
1155 int must_generate_empty = 1; /* bug 5350 */
1157 for (i = 0; i < se->num_termlists; i++)
1159 tname = se->termlists[i].name;
1160 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1162 struct termlist_score **p = 0;
1165 wrbuf_puts(c->wrbuf, "<list name=\"");
1166 wrbuf_xmlputs(c->wrbuf, tname);
1167 wrbuf_puts(c->wrbuf, "\">\n");
1168 must_generate_empty = 0;
1170 p = termlist_highscore(se->termlists[i].termlist, &len,
1175 for (i = 0; i < len && i < num; i++)
1177 // prevent sending empty term elements
1178 if (!p[i]->display_term || !p[i]->display_term[0])
1181 wrbuf_puts(c->wrbuf, "<term>");
1182 wrbuf_puts(c->wrbuf, "<name>");
1183 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1184 wrbuf_puts(c->wrbuf, "</name>");
1186 wrbuf_printf(c->wrbuf,
1187 "<frequency>%d</frequency>",
1189 wrbuf_puts(c->wrbuf, "</term>\n");
1192 wrbuf_puts(c->wrbuf, "</list>\n");
1196 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1198 wrbuf_puts(c->wrbuf, "<list name=\"");
1199 wrbuf_xmlputs(c->wrbuf, tname);
1200 wrbuf_puts(c->wrbuf, "\">\n");
1202 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1203 wrbuf_puts(c->wrbuf, "</list>\n");
1204 must_generate_empty = 0;
1206 if (must_generate_empty)
1208 wrbuf_puts(c->wrbuf, "<list name=\"");
1209 wrbuf_xmlputs(c->wrbuf, names[j]);
1210 wrbuf_puts(c->wrbuf, "\"/>\n");
1213 session_leave(se, "perform_termlist");
1214 nmem_destroy(nmem_tmp);
1217 #ifdef MISSING_HEADERS
1218 void report_nmem_stats(void)
1220 size_t in_use, is_free;
1222 nmem_get_memory_in_use(&in_use);
1223 nmem_get_memory_free(&is_free);
1225 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1226 (long) in_use, (long) is_free);
1230 struct record_cluster *show_single_start(struct session *se, const char *id,
1231 struct record_cluster **prev_r,
1232 struct record_cluster **next_r)
1234 struct record_cluster *r = 0;
1236 session_enter(se, "show_single_start");
1239 reclist_limit(se->reclist, se, 1);
1241 reclist_enter(se->reclist);
1242 while ((r = reclist_read_record(se->reclist)))
1244 if (!strcmp(r->recid, id))
1246 *next_r = reclist_read_record(se->reclist);
1251 reclist_leave(se->reclist);
1253 session_leave(se, "show_single_start");
1257 void show_single_stop(struct session *se, struct record_cluster *rec)
1259 session_leave(se, "show_single_stop");
1263 int session_fetch_more(struct session *se)
1265 struct client_list *l;
1268 for (l = se->clients_active; l; l = l->next)
1270 struct client *cl = l->client;
1271 if (client_get_state(cl) == Client_Idle)
1273 if (client_fetch_more(cl))
1275 session_log(se, YLOG_LOG, "%s: more to fetch",
1281 session_log(se, YLOG_LOG, "%s: no more to fetch",
1287 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1288 client_get_id(cl), client_get_state_str(cl));
1295 struct record_cluster **show_range_start(struct session *se,
1296 struct reclist_sortparms *sp,
1297 int start, int *num, int *total,
1298 Odr_int *sumhits, Odr_int *approx_hits,
1299 void (*show_records_ready)(void *data),
1300 struct http_channel *chan)
1302 struct record_cluster **recs = 0;
1303 struct reclist_sortparms *spp;
1304 struct client_list *l;
1307 yaz_timing_t t = yaz_timing_create();
1309 session_enter(se, "show_range_start");
1313 reclist_limit(se->reclist, se, 0);
1316 for (spp = sp; spp; spp = spp->next)
1317 if (spp->type == Metadata_sortkey_relevance)
1319 relevance_prepare_read(se->relevance, se->reclist);
1322 for (l = se->clients_active; l; l = l->next) {
1323 *sumhits += client_get_hits(l->client);
1324 *approx_hits += client_get_approximation(l->client);
1327 reclist_sort(se->reclist, sp);
1329 reclist_enter(se->reclist);
1330 *total = reclist_get_num_records(se->reclist);
1332 for (l = se->clients_active; l; l = l->next)
1333 client_update_show_stat(l->client, 0);
1335 for (i = 0; i < start; i++)
1337 struct record_cluster *r = reclist_read_record(se->reclist);
1345 struct record *rec = r->records;
1346 for (;rec; rec = rec->next)
1347 client_update_show_stat(rec->client, 1);
1350 recs = nmem_malloc(se->nmem, (*num > 0 ? *num : 1) * sizeof(*recs));
1351 for (i = 0; i < *num; i++)
1353 struct record_cluster *r = reclist_read_record(se->reclist);
1361 struct record *rec = r->records;
1362 for (;rec; rec = rec->next)
1363 client_update_show_stat(rec->client, 1);
1367 reclist_leave(se->reclist);
1370 session_log(se, YLOG_LOG, "show %6.5f %3.2f %3.2f",
1371 yaz_timing_get_real(t), yaz_timing_get_user(t),
1372 yaz_timing_get_sys(t));
1373 yaz_timing_destroy(&t);
1376 if (!session_fetch_more(se))
1377 session_log(se, YLOG_LOG, "can not fetch more");
1380 show_range_stop(se, recs);
1381 session_log(se, YLOG_LOG, "fetching more in progress");
1382 if (session_set_watch(se, SESSION_WATCH_SHOW,
1383 show_records_ready, chan, chan))
1385 session_log(se, YLOG_WARN, "Ignoring show block");
1386 session_enter(se, "show_range_start");
1390 session_log(se, YLOG_LOG, "session watch OK");
1397 void show_range_stop(struct session *se, struct record_cluster **recs)
1399 session_leave(se, "show_range_stop");
1402 void statistics(struct session *se, struct statistics *stat)
1404 struct client_list *l;
1407 memset(stat, 0, sizeof(*stat));
1409 for (l = se->clients_active; l; l = l->next)
1411 struct client *cl = l->client;
1412 if (!client_get_connection(cl))
1413 stat->num_no_connection++;
1414 stat->num_hits += client_get_hits(cl);
1415 switch (client_get_state(cl))
1417 case Client_Connecting: stat->num_connecting++; break;
1418 case Client_Working: stat->num_working++; break;
1419 case Client_Idle: stat->num_idle++; break;
1420 case Client_Failed: stat->num_failed++; break;
1421 case Client_Error: stat->num_error++; break;
1426 stat->num_records = se->total_records;
1428 stat->num_clients = count;
1431 static struct record_metadata *record_metadata_init(
1432 NMEM nmem, const char *value, enum conf_metadata_type type,
1433 struct _xmlAttr *attr)
1435 struct record_metadata *rec_md = record_metadata_create(nmem);
1436 struct record_metadata_attr **attrp = &rec_md->attributes;
1438 for (; attr; attr = attr->next)
1440 if (attr->children && attr->children->content)
1442 if (strcmp((const char *) attr->name, "type")
1443 && strcmp((const char *) attr->name, "empty"))
1444 { /* skip the "type" + "empty" attribute..
1445 The "Type" is already part of the element in output
1446 (md-%s) and so repeating it here is redundant */
1447 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1449 nmem_strdup(nmem, (const char *) attr->name);
1451 nmem_strdup(nmem, (const char *) attr->children->content);
1452 attrp = &(*attrp)->next;
1458 if (type == Metadata_type_generic)
1460 char *p = nmem_strdup(nmem, value);
1462 p = normalize7bit_generic(p, " ,/.:([");
1464 rec_md->data.text.disp = p;
1465 rec_md->data.text.sort = 0;
1466 rec_md->data.text.snippet = 0;
1468 else if (type == Metadata_type_year || type == Metadata_type_date)
1473 if (type == Metadata_type_date)
1475 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1478 rec_md->data.number.min = first;
1479 rec_md->data.number.max = last;
1486 static void mergekey_norm_wr(pp2_charset_fact_t charsets,
1487 WRBUF norm_wr, const char *value)
1489 const char *norm_str;
1490 pp2_charset_token_t prt =
1491 pp2_charset_token_create(charsets, "mergekey");
1493 pp2_charset_token_first(prt, value, 0);
1494 while ((norm_str = pp2_charset_token_next(prt)))
1498 if (wrbuf_len(norm_wr))
1499 wrbuf_puts(norm_wr, " ");
1500 wrbuf_puts(norm_wr, norm_str);
1503 pp2_charset_token_destroy(prt);
1506 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1507 struct conf_service *service, WRBUF norm_wr)
1511 for (n = root->children; n; n = n->next)
1513 if (n->type != XML_ELEMENT_NODE)
1515 if (!strcmp((const char *) n->name, "metadata"))
1517 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1519 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1521 else if (!strcmp(name, (const char *) type))
1523 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1524 if (value && *value)
1526 if (wrbuf_len(norm_wr) > 0)
1527 wrbuf_puts(norm_wr, " ");
1528 wrbuf_puts(norm_wr, name);
1529 mergekey_norm_wr(service->charsets, norm_wr,
1530 (const char *) value);
1542 static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
1543 struct client *cl, int record_no,
1544 struct conf_service *service, NMEM nmem,
1545 const char *session_mergekey)
1547 char *mergekey_norm = 0;
1548 WRBUF norm_wr = wrbuf_alloc();
1551 if (session_mergekey)
1555 nmem_strsplit_escape2(nmem, ",", session_mergekey, &values,
1558 for (i = 0; i < num; i++)
1559 get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
1561 else if ((mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1563 mergekey_norm_wr(service->charsets, norm_wr, (const char *) mergekey);
1568 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1570 for (field_id = 0; field_id < service->num_metadata; field_id++)
1572 struct conf_metadata *ser_md = &service->metadata[field_id];
1573 if (ser_md->mergekey != Metadata_mergekey_no)
1575 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1577 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1579 /* no mergekey on this one and it is required..
1580 Generate unique key instead */
1581 wrbuf_rewind(norm_wr);
1588 /* generate unique key if none is not generated already or is empty */
1589 if (wrbuf_len(norm_wr) == 0)
1591 wrbuf_printf(norm_wr, "position: %s-%d",
1592 client_get_id(cl), record_no);
1596 const char *lead = "content: ";
1597 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1599 if (wrbuf_len(norm_wr) > 0)
1600 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1601 wrbuf_destroy(norm_wr);
1602 return mergekey_norm;
1605 /** \brief see if metadata for pz:recordfilter exists
1606 \param root xml root element of normalized record
1607 \param sdb session database for client
1608 \retval 0 if there is no metadata for pz:recordfilter
1609 \retval 1 if there is metadata for pz:recordfilter
1611 If there is no pz:recordfilter defined, this function returns 1
1615 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1620 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1625 for (n = root->children; n; n = n->next)
1627 if (n->type != XML_ELEMENT_NODE)
1629 if (!strcmp((const char *) n->name, "metadata"))
1631 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1638 if ((eq = strchr(s, '=')))
1640 else if ((eq = strchr(s, '~')))
1646 if (len == strlen((const char *)type) &&
1647 !memcmp((const char *) type, s, len))
1649 xmlChar *value = xmlNodeGetContent(n);
1650 if (value && *value)
1653 (substring && strstr((const char *) value, eq+1)) ||
1654 (!substring && !strcmp((const char *) value, eq + 1)))
1666 static int ingest_to_cluster(struct client *cl,
1670 struct record_metadata_attr *mergekey);
1672 static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
1673 int record_no, NMEM nmem,
1674 struct session_database *sdb,
1675 struct record_metadata_attr *mergekeys)
1678 struct session *se = client_get_session(cl);
1679 struct conf_service *service = se->service;
1681 insert_settings_values(sdb, xdoc, root, service);
1683 if (!check_record_filter(root, sdb))
1685 session_log(se, YLOG_LOG,
1686 "Filtered out record no %d from %s",
1687 record_no, sdb->database->id);
1690 session_enter(se, "ingest_sub_record");
1691 if (client_get_session(cl) == se && se->relevance)
1692 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekeys);
1693 session_leave(se, "ingest_sub_record");
1698 /** \brief ingest XML record
1699 \param cl client holds the result set for record
1700 \param rec record buffer (0 terminated)
1701 \param record_no record position (1, 2, ..)
1702 \param nmem working NMEM
1707 int ingest_record(struct client *cl, const char *rec,
1708 int record_no, NMEM nmem)
1710 struct session *se = client_get_session(cl);
1711 struct session_database *sdb = client_get_database(cl);
1712 struct conf_service *service = se->service;
1713 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1720 if (global_parameters.dump_records)
1722 session_log(se, YLOG_LOG, "Normalized record from %s",
1727 root = xmlDocGetRootElement(xdoc);
1729 if (!strcmp((const char *) root->name, "cluster"))
1731 int no_merge_keys = 0;
1732 int no_merge_dups = 0;
1734 struct record_metadata_attr *mk = 0;
1736 for (sroot = root->children; sroot; sroot = sroot->next)
1737 if (sroot->type == XML_ELEMENT_NODE &&
1738 !strcmp((const char *) sroot->name, "record"))
1740 struct record_metadata_attr **mkp;
1741 const char *mergekey_norm =
1742 get_mergekey(xdoc, sroot, cl, record_no, service, nmem,
1749 for (mkp = &mk; *mkp; mkp = &(*mkp)->next)
1750 if (!strcmp((*mkp)->value, mergekey_norm))
1754 *mkp = (struct record_metadata_attr*)
1755 nmem_malloc(nmem, sizeof(**mkp));
1757 (*mkp)->value = nmem_strdup(nmem, mergekey_norm);
1764 if (no_merge_keys > 1 || no_merge_dups > 0)
1766 yaz_log(YLOG_LOG, "Got %d mergekeys, %d dups for position %d",
1767 no_merge_keys, no_merge_dups, record_no);
1769 for (sroot = root->children; !r && sroot; sroot = sroot->next)
1770 if (sroot->type == XML_ELEMENT_NODE &&
1771 !strcmp((const char *) sroot->name, "record"))
1773 r = ingest_sub_record(cl, xdoc, sroot, record_no, nmem, sdb,
1777 else if (!strcmp((const char *) root->name, "record"))
1779 const char *mergekey_norm =
1780 get_mergekey(xdoc, root, cl, record_no, service, nmem,
1784 struct record_metadata_attr *mk = (struct record_metadata_attr*)
1785 nmem_malloc(nmem, sizeof(*mk));
1787 mk->value = nmem_strdup(nmem, mergekey_norm);
1790 r = ingest_sub_record(cl, xdoc, root, record_no, nmem, sdb, mk);
1795 session_log(se, YLOG_WARN, "Bad pz root element: %s",
1796 (const char *) root->name);
1804 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1805 // struct record_metadata *rec_md = record->metadata[md_field_id];
1806 static int match_metadata_local(struct conf_service *service,
1807 struct conf_metadata *ser_md,
1808 struct record_metadata *rec_md0,
1809 char **values, int num_v)
1812 struct record_metadata *rec_md = rec_md0;
1814 WRBUF text_wr = wrbuf_alloc();
1815 for (i = 0; i < num_v; )
1819 if (ser_md->type == Metadata_type_year
1820 || ser_md->type == Metadata_type_date)
1822 int y = atoi(values[i]);
1823 if (y >= rec_md->data.number.min
1824 && y <= rec_md->data.number.max)
1831 val_wr = wrbuf_alloc();
1832 mergekey_norm_wr(service->charsets, val_wr, values[i]);
1834 wrbuf_rewind(text_wr);
1835 mergekey_norm_wr(service->charsets, text_wr,
1836 rec_md->data.text.disp);
1837 if (!strcmp(wrbuf_cstr(val_wr), wrbuf_cstr(text_wr)))
1840 rec_md = rec_md->next;
1845 wrbuf_destroy(val_wr);
1850 wrbuf_destroy(val_wr);
1851 wrbuf_destroy(text_wr);
1852 return i < num_v ? 1 : 0;
1855 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
1858 struct conf_service *service = se->service;
1862 NMEM nmem_tmp = nmem_create();
1864 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
1867 for (j = 0; j < service->num_metadata; j++)
1869 struct conf_metadata *md = service->metadata + j;
1870 if (!strcmp(md->name, name) && md->limitcluster)
1875 conf_service_metadata_field_id(service,
1878 if (md_field_id < 0)
1884 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1887 if (!match_metadata_local(service,
1888 &service->metadata[md_field_id],
1889 rec->metadata[md_field_id],
1898 nmem_destroy(nmem_tmp);
1902 // Skip record on non-zero
1903 static int check_limit_local(struct client *cl,
1904 struct record *record,
1907 int skip_record = 0;
1908 struct session *se = client_get_session(cl);
1909 struct conf_service *service = se->service;
1910 NMEM nmem_tmp = nmem_create();
1911 struct session_database *sdb = client_get_database(cl);
1913 while (!skip_record)
1919 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
1924 if (!strcmp(name, "*"))
1926 for (md_field_id = 0; md_field_id < service->num_metadata;
1929 if (match_metadata_local(
1931 &service->metadata[md_field_id],
1932 record->metadata[md_field_id],
1936 if (md_field_id == service->num_metadata)
1941 md_field_id = conf_service_metadata_field_id(service, name);
1942 if (md_field_id < 0)
1947 if (!match_metadata_local(
1949 &service->metadata[md_field_id],
1950 record->metadata[md_field_id],
1957 nmem_destroy(nmem_tmp);
1961 static int ingest_to_cluster(struct client *cl,
1965 struct record_metadata_attr *merge_keys)
1970 struct session *se = client_get_session(cl);
1971 struct conf_service *service = se->service;
1972 int term_factor = 1;
1973 struct record_cluster *cluster;
1974 struct record_metadata **metadata0;
1975 struct session_database *sdb = client_get_database(cl);
1976 NMEM ingest_nmem = 0;
1977 char **rank_values = 0;
1979 struct record *record = record_create(se->nmem,
1980 service->num_metadata,
1981 service->num_sortkeys, cl,
1984 for (n = root->children; n; n = n->next)
1992 if (n->type != XML_ELEMENT_NODE)
1994 if (!strcmp((const char *) n->name, "metadata"))
1996 struct conf_metadata *ser_md = 0;
1997 struct record_metadata **wheretoput = 0;
1998 struct record_metadata *rec_md = 0;
1999 int md_field_id = -1;
2001 type = xmlGetProp(n, (xmlChar *) "type");
2002 value = xmlNodeListGetString(xdoc, n->children, 1);
2005 if (!value || !*value)
2007 xmlChar *empty = xmlGetProp(n, (xmlChar *) "empty");
2015 = conf_service_metadata_field_id(service, (const char *) type);
2016 if (md_field_id < 0)
2018 if (se->number_of_warnings_unknown_metadata == 0)
2020 session_log(se, YLOG_WARN,
2021 "Ignoring unknown metadata element: %s", type);
2023 se->number_of_warnings_unknown_metadata++;
2027 ser_md = &service->metadata[md_field_id];
2029 // non-merged metadata
2030 rec_md = record_metadata_init(se->nmem, (const char *) value,
2031 ser_md->type, n->properties);
2034 session_log(se, YLOG_WARN, "bad metadata data '%s' "
2035 "for element '%s'", value, type);
2039 if (ser_md->type == Metadata_type_generic)
2041 WRBUF w = wrbuf_alloc();
2042 if (relevance_snippet(se->relevance,
2043 (char*) value, ser_md->name, w))
2044 rec_md->data.text.snippet = nmem_strdup(se->nmem,
2050 wheretoput = &record->metadata[md_field_id];
2052 wheretoput = &(*wheretoput)->next;
2053 *wheretoput = rec_md;
2057 if (check_limit_local(cl, record, record_no))
2059 session_log(se, YLOG_LOG, "Facet filtered out record no %d from %s",
2060 record_no, sdb->database->id);
2067 cluster = reclist_insert(se->reclist, se->relevance, service, record,
2068 merge_keys, &se->total_merged);
2070 return 0; // complete match with existing record
2073 const char *use_term_factor_str =
2074 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
2075 if (use_term_factor_str && use_term_factor_str[0] == '1')
2077 int maxrecs = client_get_maxrecs(cl);
2078 int hits = (int) client_get_hits(cl);
2079 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
2080 assert(term_factor >= 1);
2081 session_log(se, YLOG_DEBUG, "Using term factor: %d (%d / %d)",
2082 term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
2086 if (global_parameters.dump_records)
2087 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
2088 sdb->database->id, record_no);
2090 // original metadata, to check if first existence of a field
2091 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
2092 memcpy(metadata0, cluster->metadata,
2093 sizeof(*metadata0) * service->num_metadata);
2095 ingest_nmem = nmem_create();
2098 yaz_log(YLOG_LOG, "local in sort : %s", se->rank);
2099 nmem_strsplit_escape2(ingest_nmem, ",", se->rank, &rank_values,
2100 &rank_num, 1, '\\', 1);
2103 // now parsing XML record and adding data to cluster or record metadata
2104 for (n = root->children; n; n = n->next)
2106 pp2_charset_token_t prt;
2113 if (n->type != XML_ELEMENT_NODE)
2115 if (!strcmp((const char *) n->name, "metadata"))
2117 struct conf_metadata *ser_md = 0;
2118 struct conf_sortkey *ser_sk = 0;
2119 struct record_metadata **wheretoput = 0;
2120 struct record_metadata *rec_md = 0;
2121 int md_field_id = -1;
2122 int sk_field_id = -1;
2123 const char *rank = 0;
2124 xmlChar *xml_rank = 0;
2126 type = xmlGetProp(n, (xmlChar *) "type");
2127 value = xmlNodeListGetString(xdoc, n->children, 1);
2129 if (!type || !value || !*value)
2133 = conf_service_metadata_field_id(service, (const char *) type);
2134 if (md_field_id < 0)
2137 ser_md = &service->metadata[md_field_id];
2139 if (ser_md->sortkey_offset >= 0)
2141 sk_field_id = ser_md->sortkey_offset;
2142 ser_sk = &service->sortkeys[sk_field_id];
2146 rec_md = record_metadata_init(se->nmem, (const char *) value,
2149 // see if the field was not in cluster already (from beginning)
2157 for (i = 0; i < rank_num; i++)
2159 const char *val = rank_values[i];
2160 const char *cp = strchr(val, '=');
2163 if ((cp - val) == strlen((const char *) type)
2164 && !memcmp(val, type, cp - val))
2173 xml_rank = xmlGetProp(n, (xmlChar *) "rank");
2174 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
2177 wheretoput = &cluster->metadata[md_field_id];
2179 if (ser_md->merge == Metadata_merge_first)
2181 if (!metadata0[md_field_id])
2184 wheretoput = &(*wheretoput)->next;
2185 *wheretoput = rec_md;
2188 else if (ser_md->merge == Metadata_merge_unique)
2192 if (!strcmp((const char *) (*wheretoput)->data.text.disp,
2193 rec_md->data.text.disp))
2195 wheretoput = &(*wheretoput)->next;
2198 *wheretoput = rec_md;
2200 else if (ser_md->merge == Metadata_merge_longest)
2203 || strlen(rec_md->data.text.disp)
2204 > strlen((*wheretoput)->data.text.disp))
2206 *wheretoput = rec_md;
2209 const char *sort_str = 0;
2211 ser_sk->type == Metadata_sortkey_skiparticle;
2213 if (!cluster->sortkeys[sk_field_id])
2214 cluster->sortkeys[sk_field_id] =
2215 nmem_malloc(se->nmem,
2216 sizeof(union data_types));
2219 pp2_charset_token_create(service->charsets, "sort");
2221 pp2_charset_token_first(prt, rec_md->data.text.disp,
2224 pp2_charset_token_next(prt);
2226 sort_str = pp2_get_sort(prt);
2228 cluster->sortkeys[sk_field_id]->text.disp =
2229 rec_md->data.text.disp;
2232 sort_str = rec_md->data.text.disp;
2233 session_log(se, YLOG_WARN,
2234 "Could not make sortkey. Bug #1858");
2236 cluster->sortkeys[sk_field_id]->text.sort =
2237 nmem_strdup(se->nmem, sort_str);
2238 pp2_charset_token_destroy(prt);
2242 else if (ser_md->merge == Metadata_merge_all)
2245 wheretoput = &(*wheretoput)->next;
2246 *wheretoput = rec_md;
2248 else if (ser_md->merge == Metadata_merge_range)
2252 *wheretoput = rec_md;
2254 cluster->sortkeys[sk_field_id]
2259 int this_min = rec_md->data.number.min;
2260 int this_max = rec_md->data.number.max;
2261 if (this_min < (*wheretoput)->data.number.min)
2262 (*wheretoput)->data.number.min = this_min;
2263 if (this_max > (*wheretoput)->data.number.max)
2264 (*wheretoput)->data.number.max = this_max;
2268 // ranking of _all_ fields enabled ...
2271 relevance_countwords(se->relevance, cluster,
2272 (char *) value, rank, ser_md->name);
2274 // construct facets ... unless the client already has reported them
2275 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2277 if (ser_md->type == Metadata_type_year)
2280 sprintf(year, "%d", rec_md->data.number.max);
2282 add_facet(se, (char *) type, year, term_factor);
2283 if (rec_md->data.number.max != rec_md->data.number.min)
2285 sprintf(year, "%d", rec_md->data.number.min);
2286 add_facet(se, (char *) type, year, term_factor);
2290 add_facet(se, (char *) type, (char *) value, term_factor);
2302 if (se->number_of_warnings_unknown_elements == 0)
2303 session_log(se, YLOG_WARN,
2304 "Unexpected element in internal record: %s", n->name);
2305 se->number_of_warnings_unknown_elements++;
2313 nmem_destroy(ingest_nmem);
2315 relevance_donerecord(se->relevance, cluster);
2316 se->total_records++;
2321 void session_log(struct session *s, int level, const char *fmt, ...)
2327 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2328 yaz_log(level, "Session %u: %s", s ? s->session_id : 0, buf);
2336 * c-file-style: "Stroustrup"
2337 * indent-tabs-mode: nil
2339 * vim: shiftwidth=4 tabstop=8 expandtab