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,
300 struct conf_service *service)
304 for (i = 0; i < service->num_metadata; i++)
306 struct conf_metadata *md = &service->metadata[i];
309 if (md->setting == Metadata_setting_postproc &&
310 (offset = settings_lookup_offset(service, md->name)) >= 0)
312 const char *val = session_setting_oneval(sdb, offset);
315 xmlNode *r = xmlDocGetRootElement(doc);
316 xmlNode *n = xmlNewTextChild(r, 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");
343 insert_settings_values(sdb, rdoc, service);
345 if (global_parameters.dump_records)
347 session_log(se, YLOG_LOG, "Normalized record from %s",
356 void session_settings_dump(struct session *se,
357 struct session_database *db,
362 int i, num = db->num_settings;
363 for (i = 0; i < num; i++)
365 struct setting *s = db->settings[i];
366 for (;s ; s = s->next)
368 wrbuf_puts(w, "<set name=\"");
369 wrbuf_xmlputs(w, s->name);
370 wrbuf_puts(w, "\" value=\"");
371 wrbuf_xmlputs(w, s->value);
372 wrbuf_puts(w, "\"/>");
380 // Retrieve first defined value for 'name' for given database.
381 // Will be extended to take into account user associated with session
382 const char *session_setting_oneval(struct session_database *db, int offset)
384 if (offset >= db->num_settings || !db->settings[offset])
386 return db->settings[offset]->value;
389 // Prepare XSLT stylesheets for record normalization
390 // Structures are allocated on the session_wide nmem to avoid having
391 // to recompute this for every search. This would lead
392 // to leaking if a single session was to repeatedly change the PZ_XSLT
393 // setting. However, this is not a realistic use scenario.
394 static int prepare_map(struct session *se, struct session_database *sdb)
396 if (sdb->settings && !sdb->map)
400 if (sdb->settings[PZ_XSLT] &&
401 (s = session_setting_oneval(sdb, PZ_XSLT)))
403 char auto_stylesheet[256];
405 if (!strcmp(s, "auto"))
407 const char *request_syntax = session_setting_oneval(
408 sdb, PZ_REQUESTSYNTAX);
412 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
413 "%s.xsl", request_syntax);
414 for (cp = auto_stylesheet; *cp; cp++)
416 /* deliberately only consider ASCII */
417 if (*cp > 32 && *cp < 127)
424 session_log(se, YLOG_WARN,
425 "No pz:requestsyntax for auto stylesheet");
428 sdb->map = normalize_cache_get(se->normalize_cache,
437 // called if watch should be removed because http_channel is to be destroyed
438 static void session_watch_cancel(void *data, struct http_channel *c,
441 struct session_watchentry *ent = data;
448 // set watch. Returns 0=OK, -1 if watch is already set
449 int session_set_watch(struct session *s, int what,
450 session_watchfun fun, void *data,
451 struct http_channel *chan)
454 session_enter(s, "session_set_watch");
455 if (s->watchlist[what].fun)
460 s->watchlist[what].fun = fun;
461 s->watchlist[what].data = data;
462 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
463 session_watch_cancel);
466 session_leave(s, "session_set_watch");
470 void session_alert_watch(struct session *s, int what)
473 session_enter(s, "session_alert_watch");
474 if (s->watchlist[what].fun)
476 /* our watch is no longer associated with http_channel */
478 session_watchfun fun;
480 http_remove_observer(s->watchlist[what].obs);
481 fun = s->watchlist[what].fun;
482 data = s->watchlist[what].data;
484 /* reset watch before fun is invoked - in case fun wants to set
486 s->watchlist[what].fun = 0;
487 s->watchlist[what].data = 0;
488 s->watchlist[what].obs = 0;
490 session_leave(s, "session_alert_watch");
491 session_log(s, YLOG_DEBUG,
492 "Alert Watch: %d calling function: %p", what, fun);
496 session_leave(s,"session_alert_watch");
499 //callback for grep_databases
500 static void select_targets_callback(struct session *se,
501 struct session_database *db)
504 struct client_list *l;
506 for (l = se->clients_cached; l; l = l->next)
507 if (client_get_database(l->client) == db)
514 cl = client_create(db->database->id);
515 client_set_database(cl, db);
517 l = xmalloc(sizeof(*l));
519 l->next = se->clients_cached;
520 se->clients_cached = l;
522 /* set session always. If may be 0 if client is not active */
523 client_set_session(cl, se);
525 l = xmalloc(sizeof(*l));
527 l->next = se->clients_active;
528 se->clients_active = l;
531 static void session_reset_active_clients(struct session *se,
532 struct client_list *new_list)
534 struct client_list *l;
536 session_enter(se, "session_reset_active_clients");
537 l = se->clients_active;
538 se->clients_active = new_list;
539 session_leave(se, "session_reset_active_clients");
543 struct client_list *l_next = l->next;
545 client_lock(l->client);
546 client_set_session(l->client, 0); /* mark client inactive */
547 client_unlock(l->client);
554 static void session_remove_cached_clients(struct session *se)
556 struct client_list *l;
558 session_reset_active_clients(se, 0);
560 session_enter(se, "session_remove_cached_clients");
561 l = se->clients_cached;
562 se->clients_cached = 0;
563 session_leave(se, "session_remove_cached_clients");
567 struct client_list *l_next = l->next;
568 client_lock(l->client);
569 client_set_session(l->client, 0);
570 client_set_database(l->client, 0);
571 client_unlock(l->client);
572 client_destroy(l->client);
578 // Associates a set of clients with a session;
579 // Note: Session-databases represent databases with per-session
581 static int select_targets(struct session *se, const char *filter)
583 return session_grep_databases(se, filter, select_targets_callback);
586 int session_active_clients(struct session *s)
588 struct client_list *l;
591 for (l = s->clients_active; l; l = l->next)
592 if (client_is_active(l->client))
598 int session_is_preferred_clients_ready(struct session *s)
600 struct client_list *l;
603 for (l = s->clients_active; l; l = l->next)
604 if (client_is_active_preferred(l->client))
606 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
610 static void session_clear_set(struct session *se, struct reclist_sortparms *sp)
612 reclist_destroy(se->reclist);
613 if (nmem_total(se->nmem))
614 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd",
615 nmem_total(se->nmem));
616 nmem_reset(se->nmem);
617 se->total_records = se->total_merged = 0;
618 se->num_termlists = 0;
620 relevance_clear(se->relevance);
622 /* reset list of sorted results and clear to relevance search */
623 se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
624 se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
625 se->sorted_results->increasing = sp->increasing;
626 se->sorted_results->type = sp->type;
627 se->sorted_results->next = 0;
629 session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
630 sp->name, sp->increasing, sp->type);
632 se->reclist = reclist_create(se->nmem);
635 static void session_sort_unlocked(struct session *se,
636 struct reclist_sortparms *sp,
637 const char *mergekey,
640 struct client_list *l;
641 const char *field = sp->name;
642 int increasing = sp->increasing;
644 int clients_research = 0;
646 session_log(se, YLOG_DEBUG, "session_sort field=%s increasing=%d type=%d",
647 field, increasing, type);
649 if (rank && (!se->rank || strcmp(se->rank, rank)))
651 /* new rank must research/reingest anyway */
654 se->rank = *rank ? xstrdup(rank) : 0;
655 clients_research = 1;
656 session_log(se, YLOG_DEBUG, "session_sort: new rank = %s",
659 if (mergekey && (!se->mergekey || strcmp(se->mergekey, mergekey)))
661 /* new mergekey must research/reingest anyway */
664 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
665 clients_research = 1;
666 session_log(se, YLOG_DEBUG, "session_sort: new mergekey = %s",
669 if (clients_research == 0)
671 struct reclist_sortparms *sr;
672 for (sr = se->sorted_results; sr; sr = sr->next)
673 if (!reclist_sortparms_cmp(sr, sp))
677 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d already fetched",
678 field, increasing, type);
682 session_log(se, YLOG_DEBUG, "session_sort: field=%s increasing=%d type=%d must fetch",
683 field, increasing, type);
685 // We need to reset reclist on every sort that changes the records, not just for position
686 // So if just one client requires new searching, we need to clear set.
687 // Ask each of the client if sorting requires re-search due to native sort
688 // If it does it will require us to
689 for (l = se->clients_active; l; l = l->next)
691 struct client *cl = l->client;
692 // Assume no re-search is required.
693 client_parse_init(cl, 1);
694 clients_research += client_parse_sort(cl, sp);
696 if (clients_research)
698 session_log(se, YLOG_DEBUG,
699 "session_sort: reset results due to %d clients researching",
701 session_clear_set(se, sp);
705 // A new sorting based on same record set
706 struct reclist_sortparms *sr = nmem_malloc(se->nmem, sizeof(*sr));
707 sr->name = nmem_strdup(se->nmem, field);
708 sr->increasing = increasing;
710 sr->next = se->sorted_results;
711 se->sorted_results = sr;
712 session_log(se, YLOG_DEBUG, "session_sort: no research/ingesting done");
715 session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
717 for (l = se->clients_active; l; l = l->next)
719 struct client *cl = l->client;
720 if (client_get_state(cl) == Client_Connecting ||
721 client_get_state(cl) == Client_Idle ||
722 client_get_state(cl) == Client_Working) {
723 client_start_search(cl);
727 session_log(se, YLOG_DEBUG,
728 "session_sort: %s: No re-start/ingest in show. "
729 "Wrong client state: %d",
730 client_get_id(cl), client_get_state(cl));
735 void session_sort(struct session *se, struct reclist_sortparms *sp,
736 const char *mergekey, const char *rank)
738 //session_enter(se, "session_sort");
739 session_sort_unlocked(se, sp, mergekey, rank);
740 //session_leave(se, "session_sort");
744 enum pazpar2_error_code session_search(struct session *se,
746 const char *startrecs,
750 const char **addinfo,
751 struct reclist_sortparms *sp,
752 const char *mergekey,
755 int live_channels = 0;
757 int no_failed_query = 0;
758 int no_failed_limit = 0;
759 struct client_list *l, *l0;
761 session_log(se, YLOG_DEBUG, "Search");
765 if (se->settings_modified) {
766 session_remove_cached_clients(se);
769 session_reset_active_clients(se, 0);
771 session_enter(se, "session_search");
772 se->settings_modified = 0;
777 se->mergekey = *mergekey ? xstrdup(mergekey) : 0;
782 se->rank = *rank ? xstrdup(rank) : 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);
960 if (nmem_total(se->nmem))
961 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
962 if (nmem_total(se->session_nmem))
963 session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
964 facet_limits_destroy(se->facet_limits);
965 nmem_destroy(se->nmem);
966 service_destroy(se->service);
967 yaz_mutex_destroy(&se->session_mutex);
970 size_t session_get_memory_status(struct session *session) {
974 session_enter(session, "session_get_memory_status");
975 session_nmem = nmem_total(session->nmem);
976 session_leave(session, "session_get_memory_status");
981 struct session *new_session(NMEM nmem, struct conf_service *service,
985 struct session *session = nmem_malloc(nmem, sizeof(*session));
989 sprintf(tmp_str, "session#%u", session_id);
991 session->session_id = session_id;
992 session_log(session, YLOG_DEBUG, "New");
993 session->service = service;
994 session->relevance = 0;
995 session->total_records = 0;
996 session->number_of_warnings_unknown_elements = 0;
997 session->number_of_warnings_unknown_metadata = 0;
998 session->num_termlists = 0;
999 session->reclist = reclist_create(nmem);
1000 session->clients_active = 0;
1001 session->clients_cached = 0;
1002 session->settings_modified = 0;
1003 session->session_nmem = nmem;
1004 session->nmem = nmem_create();
1005 session->databases = 0;
1006 session->sorted_results = 0;
1007 session->facet_limits = 0;
1008 session->mergekey = 0;
1011 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1013 session->watchlist[i].data = 0;
1014 session->watchlist[i].fun = 0;
1016 session->normalize_cache = normalize_cache_create();
1017 session->session_mutex = 0;
1018 pazpar2_mutex_create(&session->session_mutex, tmp_str);
1023 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf);
1025 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
1026 int *count, NMEM nmem)
1028 struct hitsbytarget *res = 0;
1029 struct client_list *l;
1032 for (l = se->clients_active; l; l = l->next)
1035 res = nmem_malloc(nmem, sizeof(*res) * sz);
1037 for (l = se->clients_active; l; l = l->next)
1039 struct client *cl = l->client;
1040 WRBUF w = wrbuf_alloc();
1041 const char *name = session_setting_oneval(client_get_database(cl),
1044 res[*count].id = client_get_id(cl);
1045 res[*count].name = *name ? name : "Unknown";
1046 res[*count].hits = client_get_hits(cl);
1047 res[*count].approximation = client_get_approximation(cl);
1048 res[*count].records = client_get_num_records(cl);
1049 res[*count].filtered = client_get_num_records_filtered(cl);
1050 res[*count].diagnostic =
1051 client_get_diagnostic(cl, &res[*count].message,
1052 &res[*count].addinfo);
1053 res[*count].state = client_get_state_str(cl);
1054 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1055 session_settings_dump(se, client_get_database(cl), w);
1056 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1059 res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1066 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1068 struct hitsbytarget *p;
1069 session_enter(se, "get_hitsbytarget");
1070 p = hitsbytarget_nb(se, count, nmem);
1071 session_leave(se, "get_hitsbytarget");
1075 // Compares two hitsbytarget nodes by hitcount
1076 static int cmp_ht(const void *p1, const void *p2)
1078 const struct hitsbytarget *h1 = p1;
1079 const struct hitsbytarget *h2 = p2;
1080 return h2->hits - h1->hits;
1083 // Compares two hitsbytarget nodes by hitcount
1084 static int cmp_ht_approx(const void *p1, const void *p2)
1086 const struct hitsbytarget *h1 = p1;
1087 const struct hitsbytarget *h2 = p2;
1088 return h2->approximation - h1->approximation;
1091 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1092 NMEM nmem, int version)
1094 struct hitsbytarget *ht;
1097 ht = hitsbytarget_nb(se, &count, nmem);
1099 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1101 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1102 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1105 // do only print terms which have display names
1107 wrbuf_puts(wrbuf, "<term>\n");
1109 wrbuf_puts(wrbuf, "<id>");
1110 wrbuf_xmlputs(wrbuf, ht[i].id);
1111 wrbuf_puts(wrbuf, "</id>\n");
1113 wrbuf_puts(wrbuf, "<name>");
1114 if (!ht[i].name || !ht[i].name[0])
1115 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1117 wrbuf_xmlputs(wrbuf, ht[i].name);
1118 wrbuf_puts(wrbuf, "</name>\n");
1120 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1124 // Should not print if we know it isn't a approximation.
1125 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1126 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1127 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1130 wrbuf_puts(wrbuf, "<state>");
1131 wrbuf_xmlputs(wrbuf, ht[i].state);
1132 wrbuf_puts(wrbuf, "</state>\n");
1134 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1136 wrbuf_puts(wrbuf, "</term>\n");
1141 void perform_termlist(struct http_channel *c, struct session *se,
1142 const char *name, int num, int version)
1145 NMEM nmem_tmp = nmem_create();
1152 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1154 session_enter(se, "perform_termlist");
1156 for (j = 0; j < num_names; j++)
1159 int must_generate_empty = 1; /* bug 5350 */
1161 for (i = 0; i < se->num_termlists; i++)
1163 tname = se->termlists[i].name;
1164 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1166 struct termlist_score **p = 0;
1169 wrbuf_puts(c->wrbuf, "<list name=\"");
1170 wrbuf_xmlputs(c->wrbuf, tname);
1171 wrbuf_puts(c->wrbuf, "\">\n");
1172 must_generate_empty = 0;
1174 p = termlist_highscore(se->termlists[i].termlist, &len,
1179 for (i = 0; i < len && i < num; i++)
1181 // prevent sending empty term elements
1182 if (!p[i]->display_term || !p[i]->display_term[0])
1185 wrbuf_puts(c->wrbuf, "<term>");
1186 wrbuf_puts(c->wrbuf, "<name>");
1187 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1188 wrbuf_puts(c->wrbuf, "</name>");
1190 wrbuf_printf(c->wrbuf,
1191 "<frequency>%d</frequency>",
1193 wrbuf_puts(c->wrbuf, "</term>\n");
1196 wrbuf_puts(c->wrbuf, "</list>\n");
1200 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1202 wrbuf_puts(c->wrbuf, "<list name=\"");
1203 wrbuf_xmlputs(c->wrbuf, tname);
1204 wrbuf_puts(c->wrbuf, "\">\n");
1206 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1207 wrbuf_puts(c->wrbuf, "</list>\n");
1208 must_generate_empty = 0;
1210 if (must_generate_empty)
1212 wrbuf_puts(c->wrbuf, "<list name=\"");
1213 wrbuf_xmlputs(c->wrbuf, names[j]);
1214 wrbuf_puts(c->wrbuf, "\"/>\n");
1217 session_leave(se, "perform_termlist");
1218 nmem_destroy(nmem_tmp);
1221 #ifdef MISSING_HEADERS
1222 void report_nmem_stats(void)
1224 size_t in_use, is_free;
1226 nmem_get_memory_in_use(&in_use);
1227 nmem_get_memory_free(&is_free);
1229 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1230 (long) in_use, (long) is_free);
1234 struct record_cluster *show_single_start(struct session *se, const char *id,
1235 struct record_cluster **prev_r,
1236 struct record_cluster **next_r)
1238 struct record_cluster *r = 0;
1240 session_enter(se, "show_single_start");
1243 reclist_limit(se->reclist, se);
1245 reclist_enter(se->reclist);
1246 while ((r = reclist_read_record(se->reclist)))
1248 if (!strcmp(r->recid, id))
1250 *next_r = reclist_read_record(se->reclist);
1255 reclist_leave(se->reclist);
1257 session_leave(se, "show_single_start");
1261 void show_single_stop(struct session *se, struct record_cluster *rec)
1263 session_leave(se, "show_single_stop");
1267 int session_fetch_more(struct session *se)
1269 struct client_list *l;
1272 for (l = se->clients_active; l; l = l->next)
1274 struct client *cl = l->client;
1275 if (client_get_state(cl) == Client_Idle)
1277 if (client_fetch_more(cl))
1279 session_log(se, YLOG_LOG, "%s: more to fetch",
1285 session_log(se, YLOG_LOG, "%s: no more to fetch",
1291 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1292 client_get_id(cl), client_get_state_str(cl));
1299 struct record_cluster **show_range_start(struct session *se,
1300 struct reclist_sortparms *sp,
1301 int start, int *num, int *total,
1302 Odr_int *sumhits, Odr_int *approx_hits,
1303 void (*show_records_ready)(void *data),
1304 struct http_channel *chan)
1306 struct record_cluster **recs = 0;
1307 struct reclist_sortparms *spp;
1308 struct client_list *l;
1311 yaz_timing_t t = yaz_timing_create();
1313 session_enter(se, "show_range_start");
1317 reclist_limit(se->reclist, se);
1320 for (spp = sp; spp; spp = spp->next)
1321 if (spp->type == Metadata_sortkey_relevance)
1323 relevance_prepare_read(se->relevance, se->reclist);
1326 for (l = se->clients_active; l; l = l->next) {
1327 *sumhits += client_get_hits(l->client);
1328 *approx_hits += client_get_approximation(l->client);
1331 reclist_sort(se->reclist, sp);
1333 reclist_enter(se->reclist);
1334 *total = reclist_get_num_records(se->reclist);
1336 for (l = se->clients_active; l; l = l->next)
1337 client_update_show_stat(l->client, 0);
1339 for (i = 0; i < start; i++)
1341 struct record_cluster *r = reclist_read_record(se->reclist);
1349 struct record *rec = r->records;
1350 for (;rec; rec = rec->next)
1351 client_update_show_stat(rec->client, 1);
1354 recs = nmem_malloc(se->nmem, (*num > 0 ? *num : 1) * sizeof(*recs));
1355 for (i = 0; i < *num; i++)
1357 struct record_cluster *r = reclist_read_record(se->reclist);
1365 struct record *rec = r->records;
1366 for (;rec; rec = rec->next)
1367 client_update_show_stat(rec->client, 1);
1371 reclist_leave(se->reclist);
1374 session_log(se, YLOG_LOG, "show %6.5f %3.2f %3.2f",
1375 yaz_timing_get_real(t), yaz_timing_get_user(t),
1376 yaz_timing_get_sys(t));
1377 yaz_timing_destroy(&t);
1380 if (!session_fetch_more(se))
1381 session_log(se, YLOG_LOG, "can not fetch more");
1384 show_range_stop(se, recs);
1385 session_log(se, YLOG_LOG, "fetching more in progress");
1386 if (session_set_watch(se, SESSION_WATCH_SHOW,
1387 show_records_ready, chan, chan))
1389 session_log(se, YLOG_WARN, "Ignoring show block");
1390 session_enter(se, "show_range_start");
1394 session_log(se, YLOG_LOG, "session watch OK");
1401 void show_range_stop(struct session *se, struct record_cluster **recs)
1403 session_leave(se, "show_range_stop");
1406 void statistics(struct session *se, struct statistics *stat)
1408 struct client_list *l;
1411 memset(stat, 0, sizeof(*stat));
1413 for (l = se->clients_active; l; l = l->next)
1415 struct client *cl = l->client;
1416 if (!client_get_connection(cl))
1417 stat->num_no_connection++;
1418 stat->num_hits += client_get_hits(cl);
1419 switch (client_get_state(cl))
1421 case Client_Connecting: stat->num_connecting++; break;
1422 case Client_Working: stat->num_working++; break;
1423 case Client_Idle: stat->num_idle++; break;
1424 case Client_Failed: stat->num_failed++; break;
1425 case Client_Error: stat->num_error++; break;
1430 stat->num_records = se->total_records;
1432 stat->num_clients = count;
1435 static struct record_metadata *record_metadata_init(
1436 NMEM nmem, const char *value, enum conf_metadata_type type,
1437 struct _xmlAttr *attr)
1439 struct record_metadata *rec_md = record_metadata_create(nmem);
1440 struct record_metadata_attr **attrp = &rec_md->attributes;
1442 for (; attr; attr = attr->next)
1444 if (attr->children && attr->children->content)
1446 if (strcmp((const char *) attr->name, "type")
1447 && strcmp((const char *) attr->name, "empty"))
1448 { /* skip the "type" + "empty" attribute..
1449 The "Type" is already part of the element in output
1450 (md-%s) and so repeating it here is redundant */
1451 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1453 nmem_strdup(nmem, (const char *) attr->name);
1455 nmem_strdup(nmem, (const char *) attr->children->content);
1456 attrp = &(*attrp)->next;
1462 if (type == Metadata_type_generic)
1464 char *p = nmem_strdup(nmem, value);
1466 p = normalize7bit_generic(p, " ,/.:([");
1468 rec_md->data.text.disp = p;
1469 rec_md->data.text.sort = 0;
1471 else if (type == Metadata_type_year || type == Metadata_type_date)
1476 if (type == Metadata_type_date)
1478 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1481 rec_md->data.number.min = first;
1482 rec_md->data.number.max = last;
1489 static void mergekey_norm_wr(pp2_charset_fact_t charsets,
1490 WRBUF norm_wr, const char *value)
1492 const char *norm_str;
1493 pp2_charset_token_t prt =
1494 pp2_charset_token_create(charsets, "mergekey");
1496 pp2_charset_token_first(prt, value, 0);
1497 while ((norm_str = pp2_charset_token_next(prt)))
1501 if (wrbuf_len(norm_wr))
1502 wrbuf_puts(norm_wr, " ");
1503 wrbuf_puts(norm_wr, norm_str);
1506 pp2_charset_token_destroy(prt);
1509 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1510 struct conf_service *service, WRBUF norm_wr)
1514 for (n = root->children; n; n = n->next)
1516 if (n->type != XML_ELEMENT_NODE)
1518 if (!strcmp((const char *) n->name, "metadata"))
1520 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1522 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1524 else if (!strcmp(name, (const char *) type))
1526 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1527 if (value && *value)
1529 if (wrbuf_len(norm_wr) > 0)
1530 wrbuf_puts(norm_wr, " ");
1531 wrbuf_puts(norm_wr, name);
1532 mergekey_norm_wr(service->charsets, norm_wr,
1533 (const char *) value);
1545 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1546 struct conf_service *service, NMEM nmem,
1547 const char *session_mergekey)
1549 char *mergekey_norm = 0;
1550 xmlNode *root = xmlDocGetRootElement(doc);
1551 WRBUF norm_wr = wrbuf_alloc();
1554 if (session_mergekey)
1558 nmem_strsplit_escape2(nmem, ",", session_mergekey, &values,
1561 for (i = 0; i < num; i++)
1562 get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
1564 else if ((mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1566 mergekey_norm_wr(service->charsets, norm_wr, (const char *) mergekey);
1571 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1573 for (field_id = 0; field_id < service->num_metadata; field_id++)
1575 struct conf_metadata *ser_md = &service->metadata[field_id];
1576 if (ser_md->mergekey != Metadata_mergekey_no)
1578 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1580 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1582 /* no mergekey on this one and it is required..
1583 Generate unique key instead */
1584 wrbuf_rewind(norm_wr);
1591 /* generate unique key if none is not generated already or is empty */
1592 if (wrbuf_len(norm_wr) == 0)
1594 wrbuf_printf(norm_wr, "position: %s-%d",
1595 client_get_id(cl), record_no);
1599 const char *lead = "content: ";
1600 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1602 if (wrbuf_len(norm_wr) > 0)
1603 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1604 wrbuf_destroy(norm_wr);
1605 return mergekey_norm;
1608 /** \brief see if metadata for pz:recordfilter exists
1609 \param root xml root element of normalized record
1610 \param sdb session database for client
1611 \retval 0 if there is no metadata for pz:recordfilter
1612 \retval 1 if there is metadata for pz:recordfilter
1614 If there is no pz:recordfilter defined, this function returns 1
1618 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1623 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1628 for (n = root->children; n; n = n->next)
1630 if (n->type != XML_ELEMENT_NODE)
1632 if (!strcmp((const char *) n->name, "metadata"))
1634 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1641 if ((eq = strchr(s, '=')))
1643 else if ((eq = strchr(s, '~')))
1649 if (len == strlen((const char *)type) &&
1650 !memcmp((const char *) type, s, len))
1652 xmlChar *value = xmlNodeGetContent(n);
1653 if (value && *value)
1656 (substring && strstr((const char *) value, eq+1)) ||
1657 (!substring && !strcmp((const char *) value, eq + 1)))
1670 static int ingest_to_cluster(struct client *cl,
1674 const char *mergekey_norm);
1676 /** \brief ingest XML record
1677 \param cl client holds the result set for record
1678 \param rec record buffer (0 terminated)
1679 \param record_no record position (1, 2, ..)
1680 \param nmem working NMEM
1685 int ingest_record(struct client *cl, const char *rec,
1686 int record_no, NMEM nmem)
1688 struct session *se = client_get_session(cl);
1690 struct session_database *sdb = client_get_database(cl);
1691 struct conf_service *service = se->service;
1692 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1694 const char *mergekey_norm;
1699 root = xmlDocGetRootElement(xdoc);
1701 if (!check_record_filter(root, sdb))
1703 session_log(se, YLOG_LOG, "Filtered out record no %d from %s", record_no, sdb->database->id);
1708 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem,
1712 session_log(se, YLOG_WARN, "Got no mergekey");
1716 session_enter(se, "ingest_record");
1717 if (client_get_session(cl) == se && se->relevance)
1718 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1719 session_leave(se, "ingest_record");
1725 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1726 // struct record_metadata *rec_md = record->metadata[md_field_id];
1727 static int match_metadata_local(struct conf_service *service,
1728 struct conf_metadata *ser_md,
1729 struct record_metadata *rec_md0,
1730 char **values, int num_v)
1733 struct record_metadata *rec_md = rec_md0;
1735 WRBUF text_wr = wrbuf_alloc();
1736 for (i = 0; i < num_v; )
1740 if (ser_md->type == Metadata_type_year
1741 || ser_md->type == Metadata_type_date)
1743 int y = atoi(values[i]);
1744 if (y >= rec_md->data.number.min
1745 && y <= rec_md->data.number.max)
1752 val_wr = wrbuf_alloc();
1753 mergekey_norm_wr(service->charsets, val_wr, values[i]);
1755 wrbuf_rewind(text_wr);
1756 mergekey_norm_wr(service->charsets, text_wr,
1757 rec_md->data.text.disp);
1758 if (!strcmp(wrbuf_cstr(val_wr), wrbuf_cstr(text_wr)))
1761 rec_md = rec_md->next;
1766 wrbuf_destroy(val_wr);
1771 wrbuf_destroy(val_wr);
1772 wrbuf_destroy(text_wr);
1773 return i < num_v ? 1 : 0;
1776 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
1779 struct conf_service *service = se->service;
1783 NMEM nmem_tmp = nmem_create();
1785 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
1788 for (j = 0; j < service->num_metadata; j++)
1790 struct conf_metadata *md = service->metadata + j;
1791 if (!strcmp(md->name, name) && md->limitcluster)
1796 conf_service_metadata_field_id(service,
1799 if (md_field_id < 0)
1805 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1808 if (!match_metadata_local(service,
1809 &service->metadata[md_field_id],
1810 rec->metadata[md_field_id],
1819 nmem_destroy(nmem_tmp);
1823 // Skip record on non-zero
1824 static int check_limit_local(struct client *cl,
1825 struct record *record,
1828 int skip_record = 0;
1829 struct session *se = client_get_session(cl);
1830 struct conf_service *service = se->service;
1831 NMEM nmem_tmp = nmem_create();
1832 struct session_database *sdb = client_get_database(cl);
1834 while (!skip_record)
1840 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
1845 if (!strcmp(name, "*"))
1847 for (md_field_id = 0; md_field_id < service->num_metadata;
1850 if (match_metadata_local(
1852 &service->metadata[md_field_id],
1853 record->metadata[md_field_id],
1857 if (md_field_id == service->num_metadata)
1862 md_field_id = conf_service_metadata_field_id(service, name);
1863 if (md_field_id < 0)
1868 if (!match_metadata_local(
1870 &service->metadata[md_field_id],
1871 record->metadata[md_field_id],
1878 nmem_destroy(nmem_tmp);
1882 static int ingest_to_cluster(struct client *cl,
1886 const char *mergekey_norm)
1891 struct session *se = client_get_session(cl);
1892 struct conf_service *service = se->service;
1893 int term_factor = 1;
1894 struct record_cluster *cluster;
1895 struct record_metadata **metadata0;
1896 struct session_database *sdb = client_get_database(cl);
1897 NMEM ingest_nmem = 0;
1898 char **rank_values = 0;
1900 struct record *record = record_create(se->nmem,
1901 service->num_metadata,
1902 service->num_sortkeys, cl,
1905 for (n = root->children; n; n = n->next)
1913 if (n->type != XML_ELEMENT_NODE)
1915 if (!strcmp((const char *) n->name, "metadata"))
1917 struct conf_metadata *ser_md = 0;
1918 struct record_metadata **wheretoput = 0;
1919 struct record_metadata *rec_md = 0;
1920 int md_field_id = -1;
1922 type = xmlGetProp(n, (xmlChar *) "type");
1923 value = xmlNodeListGetString(xdoc, n->children, 1);
1926 if (!value || !*value)
1928 xmlChar *empty = xmlGetProp(n, (xmlChar *) "empty");
1936 = conf_service_metadata_field_id(service, (const char *) type);
1937 if (md_field_id < 0)
1939 if (se->number_of_warnings_unknown_metadata == 0)
1941 session_log(se, YLOG_WARN,
1942 "Ignoring unknown metadata element: %s", type);
1944 se->number_of_warnings_unknown_metadata++;
1948 ser_md = &service->metadata[md_field_id];
1950 // non-merged metadata
1951 rec_md = record_metadata_init(se->nmem, (const char *) value,
1952 ser_md->type, n->properties);
1955 session_log(se, YLOG_WARN, "bad metadata data '%s' "
1956 "for element '%s'", value, type);
1959 wheretoput = &record->metadata[md_field_id];
1961 wheretoput = &(*wheretoput)->next;
1962 *wheretoput = rec_md;
1966 if (check_limit_local(cl, record, record_no))
1968 session_log(se, YLOG_LOG, "Facet filtered out record no %d from %s",
1969 record_no, sdb->database->id);
1976 cluster = reclist_insert(se->reclist, service, record,
1977 mergekey_norm, &se->total_merged);
1979 return 0; // complete match with existing record
1982 const char *use_term_factor_str =
1983 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
1984 if (use_term_factor_str && use_term_factor_str[0] == '1')
1986 int maxrecs = client_get_maxrecs(cl);
1987 int hits = (int) client_get_hits(cl);
1988 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
1989 assert(term_factor >= 1);
1990 session_log(se, YLOG_DEBUG, "Using term factor: %d (%d / %d)",
1991 term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
1995 if (global_parameters.dump_records)
1996 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1997 sdb->database->id, record_no);
2000 relevance_newrec(se->relevance, cluster);
2002 // original metadata, to check if first existence of a field
2003 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
2004 memcpy(metadata0, cluster->metadata,
2005 sizeof(*metadata0) * service->num_metadata);
2007 ingest_nmem = nmem_create();
2010 yaz_log(YLOG_LOG, "local in sort : %s", se->rank);
2011 nmem_strsplit_escape2(ingest_nmem, ",", se->rank, &rank_values,
2012 &rank_num, 1, '\\', 1);
2015 // now parsing XML record and adding data to cluster or record metadata
2016 for (n = root->children; n; n = n->next)
2018 pp2_charset_token_t prt;
2025 if (n->type != XML_ELEMENT_NODE)
2027 if (!strcmp((const char *) n->name, "metadata"))
2029 struct conf_metadata *ser_md = 0;
2030 struct conf_sortkey *ser_sk = 0;
2031 struct record_metadata **wheretoput = 0;
2032 struct record_metadata *rec_md = 0;
2033 int md_field_id = -1;
2034 int sk_field_id = -1;
2035 const char *rank = 0;
2036 xmlChar *xml_rank = 0;
2038 type = xmlGetProp(n, (xmlChar *) "type");
2039 value = xmlNodeListGetString(xdoc, n->children, 1);
2041 if (!type || !value || !*value)
2045 = conf_service_metadata_field_id(service, (const char *) type);
2046 if (md_field_id < 0)
2049 ser_md = &service->metadata[md_field_id];
2051 if (ser_md->sortkey_offset >= 0)
2053 sk_field_id = ser_md->sortkey_offset;
2054 ser_sk = &service->sortkeys[sk_field_id];
2058 rec_md = record_metadata_init(se->nmem, (const char *) value,
2061 // see if the field was not in cluster already (from beginning)
2069 for (i = 0; i < rank_num; i++)
2071 const char *val = rank_values[i];
2072 const char *cp = strchr(val, '=');
2075 if ((cp - val) == strlen((const char *) type)
2076 && !memcmp(val, type, cp - val))
2085 xml_rank = xmlGetProp(n, (xmlChar *) "rank");
2086 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
2089 wheretoput = &cluster->metadata[md_field_id];
2091 if (ser_md->merge == Metadata_merge_first)
2093 if (!metadata0[md_field_id])
2096 wheretoput = &(*wheretoput)->next;
2097 *wheretoput = rec_md;
2100 else if (ser_md->merge == Metadata_merge_unique)
2104 if (!strcmp((const char *) (*wheretoput)->data.text.disp,
2105 rec_md->data.text.disp))
2107 wheretoput = &(*wheretoput)->next;
2110 *wheretoput = rec_md;
2112 else if (ser_md->merge == Metadata_merge_longest)
2115 || strlen(rec_md->data.text.disp)
2116 > strlen((*wheretoput)->data.text.disp))
2118 *wheretoput = rec_md;
2121 const char *sort_str = 0;
2123 ser_sk->type == Metadata_sortkey_skiparticle;
2125 if (!cluster->sortkeys[sk_field_id])
2126 cluster->sortkeys[sk_field_id] =
2127 nmem_malloc(se->nmem,
2128 sizeof(union data_types));
2131 pp2_charset_token_create(service->charsets, "sort");
2133 pp2_charset_token_first(prt, rec_md->data.text.disp,
2136 pp2_charset_token_next(prt);
2138 sort_str = pp2_get_sort(prt);
2140 cluster->sortkeys[sk_field_id]->text.disp =
2141 rec_md->data.text.disp;
2144 sort_str = rec_md->data.text.disp;
2145 session_log(se, YLOG_WARN,
2146 "Could not make sortkey. Bug #1858");
2148 cluster->sortkeys[sk_field_id]->text.sort =
2149 nmem_strdup(se->nmem, sort_str);
2150 pp2_charset_token_destroy(prt);
2154 else if (ser_md->merge == Metadata_merge_all)
2157 wheretoput = &(*wheretoput)->next;
2158 *wheretoput = rec_md;
2160 else if (ser_md->merge == Metadata_merge_range)
2164 *wheretoput = rec_md;
2166 cluster->sortkeys[sk_field_id]
2171 int this_min = rec_md->data.number.min;
2172 int this_max = rec_md->data.number.max;
2173 if (this_min < (*wheretoput)->data.number.min)
2174 (*wheretoput)->data.number.min = this_min;
2175 if (this_max > (*wheretoput)->data.number.max)
2176 (*wheretoput)->data.number.max = this_max;
2180 // ranking of _all_ fields enabled ...
2183 relevance_countwords(se->relevance, cluster,
2184 (char *) value, rank, ser_md->name);
2187 // construct facets ... unless the client already has reported them
2188 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2190 if (ser_md->type == Metadata_type_year)
2193 sprintf(year, "%d", rec_md->data.number.max);
2195 add_facet(se, (char *) type, year, term_factor);
2196 if (rec_md->data.number.max != rec_md->data.number.min)
2198 sprintf(year, "%d", rec_md->data.number.min);
2199 add_facet(se, (char *) type, year, term_factor);
2203 add_facet(se, (char *) type, (char *) value, term_factor);
2215 if (se->number_of_warnings_unknown_elements == 0)
2216 session_log(se, YLOG_WARN,
2217 "Unexpected element in internal record: %s", n->name);
2218 se->number_of_warnings_unknown_elements++;
2226 nmem_destroy(ingest_nmem);
2228 relevance_donerecord(se->relevance, cluster);
2229 se->total_records++;
2234 void session_log(struct session *s, int level, const char *fmt, ...)
2240 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2241 yaz_log(level, "Session %u: %s", s ? s->session_id : 0, buf);
2249 * c-file-style: "Stroustrup"
2250 * indent-tabs-mode: nil
2252 * vim: shiftwidth=4 tabstop=8 expandtab