1 /* This file is part of Pazpar2.
2 Copyright (C) Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 \brief high-level logic; mostly user sessions and settings
46 #include <yaz/marcdisp.h>
47 #include <yaz/comstack.h>
48 #include <yaz/tcpip.h>
49 #include <yaz/proto.h>
50 #include <yaz/readconf.h>
51 #include <yaz/pquery.h>
52 #include <yaz/otherinfo.h>
53 #include <yaz/yaz-util.h>
55 #include <yaz/query-charset.h>
56 #include <yaz/querytowrbuf.h>
57 #include <yaz/oid_db.h>
58 #include <yaz/snprintf.h>
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, const char *dbname,
909 const char *name, const char *value)
911 session_enter(se, "session_apply_setting");
913 struct session_database *sdb = find_session_database(se, dbname);
914 struct conf_service *service = se->service;
916 int offset = settings_create_offset(service, name);
918 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
920 // Force later recompute of settings-driven data structures
921 // (happens when a search starts and client connections are prepared)
922 if (offset == PZ_XSLT)
924 se->settings_modified = 1;
925 for (s = sdb->settings[offset]; s; s = s->next)
926 if (!strcmp(s->name, name) &&
927 dbname && s->target && !strcmp(dbname, s->target))
931 s = nmem_malloc(se->session_nmem, sizeof(*s));
933 s->target = nmem_strdup(se->session_nmem, dbname);
934 s->name = nmem_strdup(se->session_nmem, name);
935 s->next = sdb->settings[offset];
936 sdb->settings[offset] = s;
938 s->value = nmem_strdup(se->session_nmem, value);
940 session_leave(se, "session_apply_setting");
943 void session_destroy(struct session *se)
945 struct session_database *sdb;
946 session_log(se, YLOG_LOG, "destroy");
948 session_remove_cached_clients(se);
950 for (sdb = se->databases; sdb; sdb = sdb->next)
951 session_database_destroy(sdb);
952 normalize_cache_destroy(se->normalize_cache);
953 relevance_destroy(&se->relevance);
954 reclist_destroy(se->reclist);
957 if (nmem_total(se->nmem))
958 session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
959 if (nmem_total(se->session_nmem))
960 session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
961 facet_limits_destroy(se->facet_limits);
962 nmem_destroy(se->nmem);
963 service_destroy(se->service);
964 yaz_mutex_destroy(&se->session_mutex);
967 size_t session_get_memory_status(struct session *session) {
971 session_enter(session, "session_get_memory_status");
972 session_nmem = nmem_total(session->nmem);
973 session_leave(session, "session_get_memory_status");
978 struct session *new_session(NMEM nmem, struct conf_service *service,
982 struct session *session = nmem_malloc(nmem, sizeof(*session));
986 sprintf(tmp_str, "session#%u", session_id);
988 session->session_id = session_id;
989 session_log(session, YLOG_DEBUG, "New");
990 session->service = service;
991 session->relevance = 0;
992 session->total_records = 0;
993 session->number_of_warnings_unknown_elements = 0;
994 session->number_of_warnings_unknown_metadata = 0;
995 session->num_termlists = 0;
996 session->reclist = reclist_create(nmem);
997 session->clients_active = 0;
998 session->clients_cached = 0;
999 session->settings_modified = 0;
1000 session->session_nmem = nmem;
1001 session->nmem = nmem_create();
1002 session->databases = 0;
1003 session->sorted_results = 0;
1004 session->facet_limits = 0;
1005 session->mergekey = 0;
1008 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1010 session->watchlist[i].data = 0;
1011 session->watchlist[i].fun = 0;
1013 session->normalize_cache = normalize_cache_create();
1014 session->session_mutex = 0;
1015 pazpar2_mutex_create(&session->session_mutex, tmp_str);
1016 session_log(session, YLOG_LOG, "create");
1022 const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf);
1024 static struct hitsbytarget *hitsbytarget_nb(struct session *se,
1025 int *count, NMEM nmem)
1027 struct hitsbytarget *res = 0;
1028 struct client_list *l;
1031 for (l = se->clients_active; l; l = l->next)
1034 res = nmem_malloc(nmem, sizeof(*res) * sz);
1036 for (l = se->clients_active; l; l = l->next)
1038 struct client *cl = l->client;
1039 WRBUF w = wrbuf_alloc();
1040 const char *name = session_setting_oneval(client_get_database(cl),
1042 res[*count].id = client_get_id(cl);
1043 res[*count].name = *name ? name : "Unknown";
1044 res[*count].hits = client_get_hits(cl);
1045 res[*count].approximation = client_get_approximation(cl);
1046 res[*count].records = client_get_num_records(cl,
1047 &res[*count].filtered,
1049 res[*count].diagnostic =
1050 client_get_diagnostic(cl, &res[*count].message,
1051 &res[*count].addinfo);
1052 res[*count].state = client_get_state_str(cl);
1053 res[*count].connected = client_get_connection(cl) ? 1 : 0;
1054 session_settings_dump(se, client_get_database(cl), w);
1055 res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
1058 res[*count].suggestions_xml = nmem_strdup(nmem, client_get_suggestions_xml(cl, w));
1065 struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
1067 struct hitsbytarget *p;
1068 session_enter(se, "get_hitsbytarget");
1069 p = hitsbytarget_nb(se, count, nmem);
1070 session_leave(se, "get_hitsbytarget");
1074 // Compares two hitsbytarget nodes by hitcount
1075 static int cmp_ht(const void *p1, const void *p2)
1077 const struct hitsbytarget *h1 = p1;
1078 const struct hitsbytarget *h2 = p2;
1079 return h2->hits - h1->hits;
1082 // Compares two hitsbytarget nodes by hitcount
1083 static int cmp_ht_approx(const void *p1, const void *p2)
1085 const struct hitsbytarget *h1 = p1;
1086 const struct hitsbytarget *h2 = p2;
1087 return h2->approximation - h1->approximation;
1090 static int targets_termlist_nb(WRBUF wrbuf, struct session *se, int num,
1091 NMEM nmem, int version)
1093 struct hitsbytarget *ht;
1096 ht = hitsbytarget_nb(se, &count, nmem);
1098 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht_approx);
1100 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
1101 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
1104 // do only print terms which have display names
1106 wrbuf_puts(wrbuf, "<term>\n");
1108 wrbuf_puts(wrbuf, "<id>");
1109 wrbuf_xmlputs(wrbuf, ht[i].id);
1110 wrbuf_puts(wrbuf, "</id>\n");
1112 wrbuf_puts(wrbuf, "<name>");
1113 if (!ht[i].name || !ht[i].name[0])
1114 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
1116 wrbuf_xmlputs(wrbuf, ht[i].name);
1117 wrbuf_puts(wrbuf, "</name>\n");
1119 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
1123 // Should not print if we know it isn't a approximation.
1124 wrbuf_printf(wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
1125 wrbuf_printf(wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
1126 wrbuf_printf(wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
1129 wrbuf_puts(wrbuf, "<state>");
1130 wrbuf_xmlputs(wrbuf, ht[i].state);
1131 wrbuf_puts(wrbuf, "</state>\n");
1133 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
1135 wrbuf_puts(wrbuf, "</term>\n");
1140 void perform_termlist(struct http_channel *c, struct session *se,
1141 const char *name, int num, int version)
1144 NMEM nmem_tmp = nmem_create();
1151 nmem_strsplit(nmem_tmp, ",", name, &names, &num_names);
1153 session_enter(se, "perform_termlist");
1155 for (j = 0; j < num_names; j++)
1158 int must_generate_empty = 1; /* bug 5350 */
1160 for (i = 0; i < se->num_termlists; i++)
1162 tname = se->termlists[i].name;
1163 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1165 struct termlist_score **p = 0;
1168 wrbuf_puts(c->wrbuf, "<list name=\"");
1169 wrbuf_xmlputs(c->wrbuf, tname);
1170 wrbuf_puts(c->wrbuf, "\">\n");
1171 must_generate_empty = 0;
1173 p = termlist_highscore(se->termlists[i].termlist, &len,
1178 for (i = 0; i < len && i < num; i++)
1180 // prevent sending empty term elements
1181 if (!p[i]->display_term || !p[i]->display_term[0])
1184 wrbuf_puts(c->wrbuf, "<term>");
1185 wrbuf_puts(c->wrbuf, "<name>");
1186 wrbuf_xmlputs(c->wrbuf, p[i]->display_term);
1187 wrbuf_puts(c->wrbuf, "</name>");
1189 wrbuf_printf(c->wrbuf,
1190 "<frequency>%d</frequency>",
1192 wrbuf_puts(c->wrbuf, "</term>\n");
1195 wrbuf_puts(c->wrbuf, "</list>\n");
1199 if (!strcmp(names[j], tname) || !strcmp(names[j], "*"))
1201 wrbuf_puts(c->wrbuf, "<list name=\"");
1202 wrbuf_xmlputs(c->wrbuf, tname);
1203 wrbuf_puts(c->wrbuf, "\">\n");
1205 targets_termlist_nb(c->wrbuf, se, num, c->nmem, version);
1206 wrbuf_puts(c->wrbuf, "</list>\n");
1207 must_generate_empty = 0;
1209 if (must_generate_empty)
1211 wrbuf_puts(c->wrbuf, "<list name=\"");
1212 wrbuf_xmlputs(c->wrbuf, names[j]);
1213 wrbuf_puts(c->wrbuf, "\"/>\n");
1216 session_leave(se, "perform_termlist");
1217 nmem_destroy(nmem_tmp);
1220 #ifdef MISSING_HEADERS
1221 void report_nmem_stats(void)
1223 size_t in_use, is_free;
1225 nmem_get_memory_in_use(&in_use);
1226 nmem_get_memory_free(&is_free);
1228 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1229 (long) in_use, (long) is_free);
1233 struct record_cluster *show_single_start(struct session *se, const char *id,
1234 struct record_cluster **prev_r,
1235 struct record_cluster **next_r)
1237 struct record_cluster *r = 0;
1239 session_enter(se, "show_single_start");
1242 reclist_limit(se->reclist, se, 1);
1244 reclist_enter(se->reclist);
1245 while ((r = reclist_read_record(se->reclist)))
1247 if (!strcmp(r->recid, id))
1249 *next_r = reclist_read_record(se->reclist);
1254 reclist_leave(se->reclist);
1256 session_leave(se, "show_single_start");
1260 void show_single_stop(struct session *se, struct record_cluster *rec)
1262 session_leave(se, "show_single_stop");
1266 int session_fetch_more(struct session *se)
1268 struct client_list *l;
1271 for (l = se->clients_active; l; l = l->next)
1273 struct client *cl = l->client;
1274 if (client_get_state(cl) == Client_Idle)
1276 if (client_fetch_more(cl))
1278 session_log(se, YLOG_LOG, "%s: more to fetch",
1285 int ingest_failures;
1286 int record_failures;
1287 int num = client_get_num_records(
1288 cl, &filtered, &ingest_failures, &record_failures);
1290 session_log(se, YLOG_LOG, "%s: hits=" ODR_INT_PRINTF
1291 " fetched=%d filtered=%d",
1293 client_get_hits(cl),
1295 if (ingest_failures || record_failures)
1297 session_log(se, YLOG_WARN, "%s:"
1298 " ingest failures=%d record failures=%d",
1300 ingest_failures, record_failures);
1306 session_log(se, YLOG_LOG, "%s: no fetch due to state=%s",
1307 client_get_id(cl), client_get_state_str(cl));
1314 struct record_cluster **show_range_start(struct session *se,
1315 struct reclist_sortparms *sp,
1316 int start, int *num, int *total,
1317 Odr_int *sumhits, Odr_int *approx_hits,
1318 void (*show_records_ready)(void *data),
1319 struct http_channel *chan)
1321 struct record_cluster **recs = 0;
1322 struct reclist_sortparms *spp;
1323 struct client_list *l;
1326 yaz_timing_t t = yaz_timing_create();
1328 session_enter(se, "show_range_start");
1332 reclist_limit(se->reclist, se, 0);
1335 for (spp = sp; spp; spp = spp->next)
1336 if (spp->type == Metadata_type_relevance)
1338 relevance_prepare_read(se->relevance, se->reclist);
1341 for (l = se->clients_active; l; l = l->next) {
1342 *sumhits += client_get_hits(l->client);
1343 *approx_hits += client_get_approximation(l->client);
1346 reclist_sort(se->reclist, sp);
1348 reclist_enter(se->reclist);
1349 *total = reclist_get_num_records(se->reclist);
1351 for (l = se->clients_active; l; l = l->next)
1352 client_update_show_stat(l->client, 0);
1354 for (i = 0; i < start; i++)
1356 struct record_cluster *r = reclist_read_record(se->reclist);
1364 struct record *rec = r->records;
1365 for (;rec; rec = rec->next)
1366 client_update_show_stat(rec->client, 1);
1369 recs = nmem_malloc(se->nmem, (*num > 0 ? *num : 1) * sizeof(*recs));
1370 for (i = 0; i < *num; i++)
1372 struct record_cluster *r = reclist_read_record(se->reclist);
1380 struct record *rec = r->records;
1381 for (;rec; rec = rec->next)
1382 client_update_show_stat(rec->client, 1);
1386 reclist_leave(se->reclist);
1389 session_log(se, YLOG_LOG, "show %6.5f %3.2f %3.2f",
1390 yaz_timing_get_real(t), yaz_timing_get_user(t),
1391 yaz_timing_get_sys(t));
1392 yaz_timing_destroy(&t);
1395 if (!session_fetch_more(se))
1396 session_log(se, YLOG_LOG, "can not fetch more");
1399 show_range_stop(se, recs);
1400 session_log(se, YLOG_LOG, "fetching more in progress");
1401 if (session_set_watch(se, SESSION_WATCH_SHOW,
1402 show_records_ready, chan, chan))
1404 session_log(se, YLOG_WARN, "Ignoring show block");
1405 session_enter(se, "show_range_start");
1409 session_log(se, YLOG_LOG, "session watch OK");
1416 void show_range_stop(struct session *se, struct record_cluster **recs)
1418 session_leave(se, "show_range_stop");
1421 void statistics(struct session *se, struct statistics *stat)
1423 struct client_list *l;
1426 memset(stat, 0, sizeof(*stat));
1428 for (l = se->clients_active; l; l = l->next)
1430 struct client *cl = l->client;
1431 if (!client_get_connection(cl))
1432 stat->num_no_connection++;
1433 stat->num_hits += client_get_hits(cl);
1434 switch (client_get_state(cl))
1436 case Client_Connecting: stat->num_connecting++; break;
1437 case Client_Working: stat->num_working++; break;
1438 case Client_Idle: stat->num_idle++; break;
1439 case Client_Failed: stat->num_failed++; break;
1440 case Client_Error: stat->num_error++; break;
1445 stat->num_records = se->total_records;
1447 stat->num_clients = count;
1450 static struct record_metadata *record_metadata_init(
1451 NMEM nmem, const char *value, enum conf_metadata_type type,
1452 struct _xmlAttr *attr)
1454 struct record_metadata *rec_md = record_metadata_create(nmem);
1455 struct record_metadata_attr **attrp = &rec_md->attributes;
1457 for (; attr; attr = attr->next)
1459 if (attr->children && attr->children->content)
1461 if (strcmp((const char *) attr->name, "type")
1462 && strcmp((const char *) attr->name, "empty"))
1463 { /* skip the "type" + "empty" attribute..
1464 The "Type" is already part of the element in output
1465 (md-%s) and so repeating it here is redundant */
1466 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1468 nmem_strdup(nmem, (const char *) attr->name);
1470 nmem_strdup(nmem, (const char *) attr->children->content);
1471 attrp = &(*attrp)->next;
1479 case Metadata_type_generic:
1480 case Metadata_type_skiparticle:
1481 if (strstr(value, "://")) /* looks like a URL */
1482 rec_md->data.text.disp = nmem_strdup(nmem, value);
1484 rec_md->data.text.disp =
1485 normalize7bit_generic(nmem_strdup(nmem, value), " ,/.:([");
1486 rec_md->data.text.sort = 0;
1487 rec_md->data.text.snippet = 0;
1489 case Metadata_type_year:
1490 case Metadata_type_date:
1495 if (type == Metadata_type_date)
1497 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1500 rec_md->data.number.min = first;
1501 rec_md->data.number.max = last;
1504 case Metadata_type_float:
1505 rec_md->data.fnumber = atof(value);
1507 case Metadata_type_relevance:
1508 case Metadata_type_position:
1514 static void mergekey_norm_wr(pp2_charset_fact_t charsets,
1515 WRBUF norm_wr, const char *value)
1517 const char *norm_str;
1518 pp2_charset_token_t prt =
1519 pp2_charset_token_create(charsets, "mergekey");
1521 pp2_charset_token_first(prt, value, 0);
1522 while ((norm_str = pp2_charset_token_next(prt)))
1526 if (wrbuf_len(norm_wr))
1527 wrbuf_puts(norm_wr, " ");
1528 wrbuf_puts(norm_wr, norm_str);
1531 pp2_charset_token_destroy(prt);
1534 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1535 struct conf_service *service, WRBUF norm_wr)
1539 for (n = root->children; n; n = n->next)
1541 if (n->type != XML_ELEMENT_NODE)
1543 if (!strcmp((const char *) n->name, "metadata"))
1545 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1547 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1549 else if (!strcmp(name, (const char *) type))
1551 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1552 if (value && *value)
1554 if (wrbuf_len(norm_wr) > 0)
1555 wrbuf_puts(norm_wr, " ");
1556 wrbuf_puts(norm_wr, name);
1557 mergekey_norm_wr(service->charsets, norm_wr,
1558 (const char *) value);
1570 static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
1571 struct client *cl, int record_no,
1572 struct conf_service *service, NMEM nmem,
1573 const char *session_mergekey)
1575 char *mergekey_norm = 0;
1576 WRBUF norm_wr = wrbuf_alloc();
1579 if (session_mergekey)
1583 nmem_strsplit_escape2(nmem, ",", session_mergekey, &values,
1586 for (i = 0; i < num; i++)
1587 get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
1589 else if ((mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1591 mergekey_norm_wr(service->charsets, norm_wr, (const char *) mergekey);
1596 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1598 for (field_id = 0; field_id < service->num_metadata; field_id++)
1600 struct conf_metadata *ser_md = &service->metadata[field_id];
1601 if (ser_md->mergekey != Metadata_mergekey_no)
1603 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1605 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1607 /* no mergekey on this one and it is required..
1608 Generate unique key instead */
1609 wrbuf_rewind(norm_wr);
1616 /* generate unique key if none is not generated already or is empty */
1617 if (wrbuf_len(norm_wr) == 0)
1619 wrbuf_printf(norm_wr, "position: %s-%d",
1620 client_get_id(cl), record_no);
1624 const char *lead = "content: ";
1625 wrbuf_insert(norm_wr, 0, lead, strlen(lead));
1627 if (wrbuf_len(norm_wr) > 0)
1628 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1629 wrbuf_destroy(norm_wr);
1630 return mergekey_norm;
1633 /** \brief see if metadata for pz:recordfilter exists
1634 \param root xml root element of normalized record
1635 \param sdb session database for client
1636 \retval 0 if there is no metadata for pz:recordfilter
1637 \retval 1 if there is metadata for pz:recordfilter
1639 If there is no pz:recordfilter defined, this function returns 1
1643 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1648 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1653 for (n = root->children; n; n = n->next)
1655 if (n->type != XML_ELEMENT_NODE)
1657 if (!strcmp((const char *) n->name, "metadata"))
1659 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1666 if ((eq = strchr(s, '=')))
1668 else if ((eq = strchr(s, '~')))
1674 if (len == strlen((const char *)type) &&
1675 !memcmp((const char *) type, s, len))
1677 xmlChar *value = xmlNodeGetContent(n);
1678 if (value && *value)
1681 (substring && strstr((const char *) value, eq+1)) ||
1682 (!substring && !strcmp((const char *) value, eq + 1)))
1694 static int ingest_to_cluster(struct client *cl,
1698 struct record_metadata_attr *mergekey);
1700 static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
1701 int record_no, NMEM nmem,
1702 struct session_database *sdb,
1703 struct record_metadata_attr *mergekeys)
1706 struct session *se = client_get_session(cl);
1708 if (!check_record_filter(root, sdb))
1710 session_log(se, YLOG_LOG,
1711 "Filtered out record no %d from %s",
1712 record_no, sdb->database->id);
1715 session_enter(se, "ingest_sub_record");
1716 if (client_get_session(cl) == se && se->relevance)
1717 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekeys);
1718 session_leave(se, "ingest_sub_record");
1723 /** \brief ingest XML record
1724 \param cl client holds the result set for record
1725 \param rec record buffer (0 terminated)
1726 \param record_no record position (1, 2, ..)
1727 \param nmem working NMEM
1732 int ingest_record(struct client *cl, const char *rec,
1733 int record_no, NMEM nmem)
1735 struct session *se = client_get_session(cl);
1736 struct session_database *sdb = client_get_database(cl);
1737 struct conf_service *service = se->service;
1738 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1739 int r = ingest_xml_record(cl, xdoc, record_no, nmem, 0);
1740 client_store_xdoc(cl, record_no, xdoc);
1744 int ingest_xml_record(struct client *cl, xmlDoc *xdoc,
1745 int record_no, NMEM nmem, int cached_copy)
1747 struct session *se = client_get_session(cl);
1748 struct session_database *sdb = client_get_database(cl);
1749 struct conf_service *service = se->service;
1755 if (global_parameters.dump_records)
1757 session_log(se, YLOG_LOG, "Normalized record from %s",
1762 root = xmlDocGetRootElement(xdoc);
1764 if (!strcmp((const char *) root->name, "cluster"))
1766 int no_merge_keys = 0;
1767 int no_merge_dups = 0;
1769 struct record_metadata_attr *mk = 0;
1771 for (sroot = root->children; sroot; sroot = sroot->next)
1772 if (sroot->type == XML_ELEMENT_NODE &&
1773 !strcmp((const char *) sroot->name, "record"))
1775 struct record_metadata_attr **mkp;
1776 const char *mergekey_norm =
1777 get_mergekey(xdoc, sroot, cl, record_no, service, nmem,
1784 for (mkp = &mk; *mkp; mkp = &(*mkp)->next)
1785 if (!strcmp((*mkp)->value, mergekey_norm))
1789 *mkp = (struct record_metadata_attr*)
1790 nmem_malloc(nmem, sizeof(**mkp));
1792 (*mkp)->value = nmem_strdup(nmem, mergekey_norm);
1799 if (no_merge_keys > 1 || no_merge_dups > 0)
1801 yaz_log(YLOG_LOG, "Got %d mergekeys, %d dups for position %d",
1802 no_merge_keys, no_merge_dups, record_no);
1804 for (sroot = root->children; !r && sroot; sroot = sroot->next)
1805 if (sroot->type == XML_ELEMENT_NODE &&
1806 !strcmp((const char *) sroot->name, "record"))
1809 insert_settings_values(sdb, xdoc, root, service);
1810 r = ingest_sub_record(cl, xdoc, sroot, record_no, nmem, sdb,
1814 else if (!strcmp((const char *) root->name, "record"))
1816 const char *mergekey_norm =
1817 get_mergekey(xdoc, root, cl, record_no, service, nmem,
1821 struct record_metadata_attr *mk = (struct record_metadata_attr*)
1822 nmem_malloc(nmem, sizeof(*mk));
1824 mk->value = nmem_strdup(nmem, mergekey_norm);
1828 insert_settings_values(sdb, xdoc, root, service);
1829 r = ingest_sub_record(cl, xdoc, root, record_no, nmem, sdb, mk);
1834 session_log(se, YLOG_WARN, "Bad pz root element: %s",
1835 (const char *) root->name);
1842 // struct conf_metadata *ser_md = &service->metadata[md_field_id];
1843 // struct record_metadata *rec_md = record->metadata[md_field_id];
1844 static int match_metadata_local(struct conf_service *service,
1845 struct conf_metadata *ser_md,
1846 struct record_metadata *rec_md0,
1847 char **values, int num_v)
1850 struct record_metadata *rec_md = rec_md0;
1852 WRBUF text_wr = wrbuf_alloc();
1853 for (i = 0; i < num_v; )
1857 if (ser_md->type == Metadata_type_year
1858 || ser_md->type == Metadata_type_date)
1860 int y = atoi(values[i]);
1861 if (y >= rec_md->data.number.min
1862 && y <= rec_md->data.number.max)
1869 val_wr = wrbuf_alloc();
1870 mergekey_norm_wr(service->charsets, val_wr, values[i]);
1872 wrbuf_rewind(text_wr);
1873 mergekey_norm_wr(service->charsets, text_wr,
1874 rec_md->data.text.disp);
1875 if (!strcmp(wrbuf_cstr(val_wr), wrbuf_cstr(text_wr)))
1878 rec_md = rec_md->next;
1883 wrbuf_destroy(val_wr);
1888 wrbuf_destroy(val_wr);
1889 wrbuf_destroy(text_wr);
1890 return i < num_v ? 1 : 0;
1893 int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
1896 struct conf_service *service = se->service;
1900 NMEM nmem_tmp = nmem_create();
1902 for (i = 0; (name = facet_limits_get(se->facet_limits, i, &value)); i++)
1905 for (j = 0; j < service->num_metadata; j++)
1907 struct conf_metadata *md = service->metadata + j;
1908 if (!strcmp(md->name, name) && md->limitcluster)
1913 conf_service_metadata_field_id(service,
1916 if (md_field_id < 0)
1922 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
1925 if (!match_metadata_local(service,
1926 &service->metadata[md_field_id],
1927 rec->metadata[md_field_id],
1936 nmem_destroy(nmem_tmp);
1940 // Skip record on non-zero
1941 static int check_limit_local(struct client *cl,
1942 struct record *record,
1945 int skip_record = 0;
1946 struct session *se = client_get_session(cl);
1947 struct conf_service *service = se->service;
1948 NMEM nmem_tmp = nmem_create();
1949 struct session_database *sdb = client_get_database(cl);
1951 while (!skip_record)
1957 client_get_facet_limit_local(cl, sdb, &l, nmem_tmp,
1962 if (!strcmp(name, "*"))
1964 for (md_field_id = 0; md_field_id < service->num_metadata;
1967 if (match_metadata_local(
1969 &service->metadata[md_field_id],
1970 record->metadata[md_field_id],
1974 if (md_field_id == service->num_metadata)
1979 md_field_id = conf_service_metadata_field_id(service, name);
1980 if (md_field_id < 0)
1985 if (!match_metadata_local(
1987 &service->metadata[md_field_id],
1988 record->metadata[md_field_id],
1995 nmem_destroy(nmem_tmp);
1999 static int ingest_to_cluster(struct client *cl,
2003 struct record_metadata_attr *merge_keys)
2008 struct session *se = client_get_session(cl);
2009 struct conf_service *service = se->service;
2010 int term_factor = 1;
2011 struct record_cluster *cluster;
2012 struct record_metadata **metadata0;
2013 struct session_database *sdb = client_get_database(cl);
2014 NMEM ingest_nmem = 0;
2015 char **rank_values = 0;
2017 struct record *record = record_create(se->nmem,
2018 service->num_metadata,
2019 service->num_sortkeys, cl,
2022 for (n = root->children; n; n = n->next)
2030 if (n->type != XML_ELEMENT_NODE)
2032 if (!strcmp((const char *) n->name, "metadata"))
2034 struct conf_metadata *ser_md = 0;
2035 struct record_metadata **wheretoput = 0;
2036 struct record_metadata *rec_md = 0;
2037 int md_field_id = -1;
2039 type = xmlGetProp(n, (xmlChar *) "type");
2040 value = xmlNodeListGetString(xdoc, n->children, 1);
2043 if (!value || !*value)
2045 xmlChar *empty = xmlGetProp(n, (xmlChar *) "empty");
2053 = conf_service_metadata_field_id(service, (const char *) type);
2054 if (md_field_id < 0)
2056 if (se->number_of_warnings_unknown_metadata == 0)
2058 session_log(se, YLOG_WARN,
2059 "Ignoring unknown metadata element: %s", type);
2061 se->number_of_warnings_unknown_metadata++;
2065 ser_md = &service->metadata[md_field_id];
2067 // non-merged metadata
2068 rec_md = record_metadata_init(se->nmem, (const char *) value,
2069 ser_md->type, n->properties);
2072 session_log(se, YLOG_WARN, "bad metadata data '%s' "
2073 "for element '%s'", value, type);
2077 if (ser_md->type == Metadata_type_generic)
2079 WRBUF w = wrbuf_alloc();
2080 if (relevance_snippet(se->relevance,
2081 (char*) value, ser_md->name, w))
2082 rec_md->data.text.snippet = nmem_strdup(se->nmem,
2088 wheretoput = &record->metadata[md_field_id];
2090 wheretoput = &(*wheretoput)->next;
2091 *wheretoput = rec_md;
2095 if (check_limit_local(cl, record, record_no))
2103 cluster = reclist_insert(se->reclist, se->relevance, service, record,
2104 merge_keys, &se->total_merged);
2111 return 0; // complete match with existing record
2115 const char *use_term_factor_str =
2116 session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
2117 if (use_term_factor_str && use_term_factor_str[0] == '1')
2119 int maxrecs = client_get_maxrecs(cl);
2120 int hits = (int) client_get_hits(cl);
2121 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
2122 assert(term_factor >= 1);
2123 session_log(se, YLOG_DEBUG, "Using term factor: %d (%d / %d)",
2124 term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
2128 if (global_parameters.dump_records)
2129 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
2130 sdb->database->id, record_no);
2132 // original metadata, to check if first existence of a field
2133 metadata0 = xmalloc(sizeof(*metadata0) * service->num_metadata);
2134 memcpy(metadata0, cluster->metadata,
2135 sizeof(*metadata0) * service->num_metadata);
2137 ingest_nmem = nmem_create();
2140 yaz_log(YLOG_LOG, "local in sort : %s", se->rank);
2141 nmem_strsplit_escape2(ingest_nmem, ",", se->rank, &rank_values,
2142 &rank_num, 1, '\\', 1);
2145 // now parsing XML record and adding data to cluster or record metadata
2146 for (n = root->children; n; n = n->next)
2148 pp2_charset_token_t prt;
2155 if (n->type != XML_ELEMENT_NODE)
2157 if (!strcmp((const char *) n->name, "metadata"))
2159 struct conf_metadata *ser_md = 0;
2160 struct conf_sortkey *ser_sk = 0;
2161 struct record_metadata **wheretoput = 0;
2162 struct record_metadata *rec_md = 0;
2163 int md_field_id = -1;
2164 int sk_field_id = -1;
2165 const char *rank = 0;
2166 xmlChar *xml_rank = 0;
2168 type = xmlGetProp(n, (xmlChar *) "type");
2169 value = xmlNodeListGetString(xdoc, n->children, 1);
2171 if (!type || !value || !*value)
2175 = conf_service_metadata_field_id(service, (const char *) type);
2176 if (md_field_id < 0)
2179 ser_md = &service->metadata[md_field_id];
2181 if (ser_md->sortkey_offset >= 0)
2183 sk_field_id = ser_md->sortkey_offset;
2184 ser_sk = &service->sortkeys[sk_field_id];
2188 rec_md = record_metadata_init(se->nmem, (const char *) value,
2191 // see if the field was not in cluster already (from beginning)
2199 for (i = 0; i < rank_num; i++)
2201 const char *val = rank_values[i];
2202 const char *cp = strchr(val, '=');
2205 if ((cp - val) == strlen((const char *) type)
2206 && !memcmp(val, type, cp - val))
2215 xml_rank = xmlGetProp(n, (xmlChar *) "rank");
2216 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
2219 wheretoput = &cluster->metadata[md_field_id];
2221 if (ser_md->merge == Metadata_merge_first)
2223 if (!metadata0[md_field_id])
2226 wheretoput = &(*wheretoput)->next;
2227 *wheretoput = rec_md;
2230 else if (ser_md->merge == Metadata_merge_unique)
2234 if (!strcmp((const char *) (*wheretoput)->data.text.disp,
2235 rec_md->data.text.disp))
2237 wheretoput = &(*wheretoput)->next;
2240 *wheretoput = rec_md;
2242 else if (ser_md->merge == Metadata_merge_longest)
2245 || strlen(rec_md->data.text.disp)
2246 > strlen((*wheretoput)->data.text.disp))
2248 *wheretoput = rec_md;
2251 const char *sort_str = 0;
2253 ser_sk->type == Metadata_type_skiparticle;
2255 if (!cluster->sortkeys[sk_field_id])
2256 cluster->sortkeys[sk_field_id] =
2257 nmem_malloc(se->nmem,
2258 sizeof(union data_types));
2261 pp2_charset_token_create(service->charsets, "sort");
2263 pp2_charset_token_first(prt, rec_md->data.text.disp,
2266 pp2_charset_token_next(prt);
2268 sort_str = pp2_get_sort(prt);
2270 cluster->sortkeys[sk_field_id]->text.disp =
2271 rec_md->data.text.disp;
2274 sort_str = rec_md->data.text.disp;
2275 session_log(se, YLOG_WARN,
2276 "Could not make sortkey. Bug #1858");
2278 cluster->sortkeys[sk_field_id]->text.sort =
2279 nmem_strdup(se->nmem, sort_str);
2280 pp2_charset_token_destroy(prt);
2284 else if (ser_md->merge == Metadata_merge_all)
2287 wheretoput = &(*wheretoput)->next;
2288 *wheretoput = rec_md;
2290 else if (ser_md->merge == Metadata_merge_range)
2294 *wheretoput = rec_md;
2296 cluster->sortkeys[sk_field_id]
2301 int this_min = rec_md->data.number.min;
2302 int this_max = rec_md->data.number.max;
2303 if (this_min < (*wheretoput)->data.number.min)
2304 (*wheretoput)->data.number.min = this_min;
2305 if (this_max > (*wheretoput)->data.number.max)
2306 (*wheretoput)->data.number.max = this_max;
2310 // ranking of _all_ fields enabled ...
2313 relevance_countwords(se->relevance, cluster,
2314 (char *) value, rank, ser_md->name);
2316 // construct facets ... unless the client already has reported them
2317 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
2319 if (ser_md->type == Metadata_type_year)
2322 sprintf(year, "%d", rec_md->data.number.max);
2324 add_facet(se, (char *) type, year, term_factor);
2325 if (rec_md->data.number.max != rec_md->data.number.min)
2327 sprintf(year, "%d", rec_md->data.number.min);
2328 add_facet(se, (char *) type, year, term_factor);
2332 add_facet(se, (char *) type, (char *) value, term_factor);
2344 if (se->number_of_warnings_unknown_elements == 0)
2345 session_log(se, YLOG_WARN,
2346 "Unexpected element in internal record: %s", n->name);
2347 se->number_of_warnings_unknown_elements++;
2355 nmem_destroy(ingest_nmem);
2357 relevance_donerecord(se->relevance, cluster);
2358 se->total_records++;
2363 void session_log(struct session *s, int level, const char *fmt, ...)
2369 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
2370 yaz_log(level, "Session %u: %s", s ? s->session_id : 0, buf);
2378 * c-file-style: "Stroustrup"
2379 * indent-tabs-mode: nil
2381 * vim: shiftwidth=4 tabstop=8 expandtab