1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 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>
59 #include <yaz/gettimeofday.h>
63 #include <yaz/timing.h>
67 #include "parameters.h"
71 #include "termlists.h"
73 #include "relevance.h"
77 #include "normalize7bit.h"
79 #define TERMLIST_HIGH_SCORE 25
83 inline int max(int a, int b)
85 return (a > b) ? a : b;
89 // Note: Some things in this structure will eventually move to configuration
90 struct parameters global_parameters =
97 struct client *client;
98 struct client_list *next;
101 static void log_xml_doc(xmlDoc *doc)
103 FILE *lf = yaz_log_file();
106 #if LIBXML_VERSION >= 20600
107 xmlDocDumpFormatMemory(doc, &result, &len, 1);
109 xmlDocDumpMemory(doc, &result, &len);
113 (void) fwrite(result, 1, len, lf);
119 static void session_enter(struct session *s)
121 yaz_mutex_enter(s->session_mutex);
124 static void session_leave(struct session *s)
126 yaz_mutex_leave(s->session_mutex);
129 // Recursively traverse query structure to extract terms.
130 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
142 pull_terms(nmem, n->u.p[0], termlist, num);
143 pull_terms(nmem, n->u.p[1], termlist, num);
146 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
147 for (i = 0; i < numwords; i++)
148 termlist[(*num)++] = words[i];
156 void add_facet(struct session *s, const char *type, const char *value, int count)
158 struct conf_service *service = s->service;
159 pp2_relevance_token_t prt;
160 const char *facet_component;
161 WRBUF facet_wrbuf = wrbuf_alloc();
162 prt = pp2_relevance_tokenize(service->facet_pct);
164 pp2_relevance_first(prt, value, 0);
165 while ((facet_component = pp2_relevance_token_next(prt)))
167 if (*facet_component)
169 if (wrbuf_len(facet_wrbuf))
170 wrbuf_puts(facet_wrbuf, " ");
171 wrbuf_puts(facet_wrbuf, facet_component);
174 pp2_relevance_token_destroy(prt);
176 if (wrbuf_len(facet_wrbuf))
179 for (i = 0; i < s->num_termlists; i++)
180 if (!strcmp(s->termlists[i].name, type))
182 if (i == s->num_termlists)
184 if (i == SESSION_MAX_TERMLISTS)
186 session_log(s, YLOG_FATAL, "Too many termlists");
187 wrbuf_destroy(facet_wrbuf);
191 s->termlists[i].name = nmem_strdup(s->nmem, type);
192 s->termlists[i].termlist
193 = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
194 s->num_termlists = i + 1;
197 session_log(s, YLOG_DEBUG, "Session: facets for %s: %s norm:%s (%d)",
198 type, value, wrbuf_cstr(facet_wrbuf), count);
199 termlist_insert(s->termlists[i].termlist, wrbuf_cstr(facet_wrbuf),
202 wrbuf_destroy(facet_wrbuf);
205 static xmlDoc *record_to_xml(struct session *se,
206 struct session_database *sdb, const char *rec)
208 struct database *db = sdb->database;
211 rdoc = xmlParseMemory(rec, strlen(rec));
215 session_log(se, YLOG_FATAL, "Non-wellformed XML received from %s",
220 if (global_parameters.dump_records)
222 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->url);
229 #define MAX_XSLT_ARGS 16
231 // Add static values from session database settings if applicable
232 static void insert_settings_parameters(struct session_database *sdb,
233 struct conf_service *service,
241 for (i = 0; i < service->num_metadata; i++)
243 struct conf_metadata *md = &service->metadata[i];
246 if (md->setting == Metadata_setting_parameter &&
247 (setting = settings_lookup_offset(service, md->name)) >= 0)
249 const char *val = session_setting_oneval(sdb, setting);
250 if (val && nparms < MAX_XSLT_ARGS)
253 int len = strlen(val);
254 buf = nmem_malloc(nmem, len + 3);
256 strcpy(buf + 1, val);
259 parms[offset++] = md->name;
260 parms[offset++] = buf;
268 // Add static values from session database settings if applicable
269 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
270 struct conf_service *service)
274 for (i = 0; i < service->num_metadata; i++)
276 struct conf_metadata *md = &service->metadata[i];
279 if (md->setting == Metadata_setting_postproc &&
280 (offset = settings_lookup_offset(service, md->name)) >= 0)
282 const char *val = session_setting_oneval(sdb, offset);
285 xmlNode *r = xmlDocGetRootElement(doc);
286 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
288 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
294 static xmlDoc *normalize_record(struct session *se,
295 struct session_database *sdb,
296 struct conf_service *service,
297 const char *rec, NMEM nmem)
299 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
303 char *parms[MAX_XSLT_ARGS*2+1];
305 insert_settings_parameters(sdb, service, parms, nmem);
307 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
309 session_log(se, YLOG_WARN, "Normalize failed from %s",
314 insert_settings_values(sdb, rdoc, service);
316 if (global_parameters.dump_records)
318 session_log(se, YLOG_LOG, "Normalized record from %s",
327 void session_settings_dump(struct session *se,
328 struct session_database *db,
333 int i, num = db->num_settings;
334 for (i = 0; i < num; i++)
336 struct setting *s = db->settings[i];
337 for (;s ; s = s->next)
339 wrbuf_puts(w, "<set name=\"");
340 wrbuf_xmlputs(w, s->name);
341 wrbuf_puts(w, "\" value=\"");
342 wrbuf_xmlputs(w, s->value);
343 wrbuf_puts(w, "\"/>");
351 // Retrieve first defined value for 'name' for given database.
352 // Will be extended to take into account user associated with session
353 const char *session_setting_oneval(struct session_database *db, int offset)
355 if (offset >= db->num_settings || !db->settings[offset])
357 return db->settings[offset]->value;
360 // Prepare XSLT stylesheets for record normalization
361 // Structures are allocated on the session_wide nmem to avoid having
362 // to recompute this for every search. This would lead
363 // to leaking if a single session was to repeatedly change the PZ_XSLT
364 // setting. However, this is not a realistic use scenario.
365 static int prepare_map(struct session *se, struct session_database *sdb)
371 session_log(se, YLOG_WARN, "No settings on %s", sdb->database->url);
374 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
376 char auto_stylesheet[256];
378 if (!strcmp(s, "auto"))
380 const char *request_syntax = session_setting_oneval(
381 sdb, PZ_REQUESTSYNTAX);
385 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
386 "%s.xsl", request_syntax);
387 for (cp = auto_stylesheet; *cp; cp++)
389 /* deliberately only consider ASCII */
390 if (*cp > 32 && *cp < 127)
397 session_log(se, YLOG_WARN,
398 "No pz:requestsyntax for auto stylesheet");
401 sdb->map = normalize_cache_get(se->normalize_cache,
402 se->service->server->config, s);
409 // This analyzes settings and recomputes any supporting data structures
411 static int prepare_session_database(struct session *se,
412 struct session_database *sdb)
416 session_log(se, YLOG_WARN,
417 "No settings associated with %s", sdb->database->url);
420 if (sdb->settings[PZ_XSLT] && !sdb->map)
422 if (prepare_map(se, sdb) < 0)
428 // called if watch should be removed because http_channel is to be destroyed
429 static void session_watch_cancel(void *data, struct http_channel *c,
432 struct session_watchentry *ent = data;
439 // set watch. Returns 0=OK, -1 if watch is already set
440 int session_set_watch(struct session *s, int what,
441 session_watchfun fun, void *data,
442 struct http_channel *chan)
446 if (s->watchlist[what].fun)
451 s->watchlist[what].fun = fun;
452 s->watchlist[what].data = data;
453 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
454 session_watch_cancel);
461 void session_alert_watch(struct session *s, int what)
465 if (s->watchlist[what].fun)
467 /* our watch is no longer associated with http_channel */
469 session_watchfun fun;
471 http_remove_observer(s->watchlist[what].obs);
472 fun = s->watchlist[what].fun;
473 data = s->watchlist[what].data;
475 /* reset watch before fun is invoked - in case fun wants to set
477 s->watchlist[what].fun = 0;
478 s->watchlist[what].data = 0;
479 s->watchlist[what].obs = 0;
482 session_log(s, YLOG_DEBUG,
483 "session_alert_watch: %d calling function: %p", what, fun);
490 //callback for grep_databases
491 static void select_targets_callback(void *context, struct session_database *db)
493 struct session *se = (struct session*) context;
494 struct client *cl = client_create();
495 struct client_list *l;
496 client_set_database(cl, db);
498 client_set_session(cl, se);
499 l = xmalloc(sizeof(*l));
501 l->next = se->clients;
505 static void session_remove_clients(struct session *se)
507 struct client_list *l;
516 struct client_list *l_next = l->next;
517 client_lock(l->client);
518 client_set_session(l->client, 0);
519 client_set_database(l->client, 0);
520 client_unlock(l->client);
521 client_destroy(l->client);
527 // Associates a set of clients with a session;
528 // Note: Session-databases represent databases with per-session
530 static int select_targets(struct session *se, const char *filter)
532 return session_grep_databases(se, filter, select_targets_callback);
535 int session_active_clients(struct session *s)
537 struct client_list *l;
540 for (l = s->clients; l; l = l->next)
541 if (client_is_active(l->client))
547 int session_is_preferred_clients_ready(struct session *s)
549 struct client_list *l;
552 for (l = s->clients; l; l = l->next)
553 if (client_is_active_preferred(l->client))
555 session_log(s, YLOG_DEBUG, "Session has %d active preferred clients.", res);
561 enum pazpar2_error_code search(struct session *se,
563 const char *startrecs, const char *maxrecs,
565 const char **addinfo)
567 int live_channels = 0;
570 struct client_list *l;
573 session_log(se, YLOG_DEBUG, "Search");
577 session_remove_clients(se);
580 reclist_destroy(se->reclist);
582 relevance_destroy(&se->relevance);
583 nmem_reset(se->nmem);
584 se->total_records = se->total_hits = se->total_merged = 0;
585 se->num_termlists = 0;
586 live_channels = select_targets(se, filter);
590 return PAZPAR2_NO_TARGETS;
592 se->reclist = reclist_create(se->nmem);
594 yaz_gettimeofday(&tval);
598 for (l = se->clients; l; l = l->next)
600 struct client *cl = l->client;
603 client_set_maxrecs(cl, atoi(maxrecs));
605 client_set_startrecs(cl, atoi(startrecs));
606 if (prepare_session_database(se, client_get_database(cl)) < 0)
608 else if (client_parse_query(cl, query) < 0)
613 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
614 se->service->z3950_session_timeout,
615 se->service->server->iochan_man,
617 client_start_search(cl);
626 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
629 return PAZPAR2_NO_TARGETS;
631 return PAZPAR2_NO_ERROR;
634 // Creates a new session_database object for a database
635 static void session_init_databases_fun(void *context, struct database *db)
637 struct session *se = (struct session *) context;
638 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
644 assert(db->settings);
645 new->settings = nmem_malloc(se->session_nmem,
646 sizeof(struct settings *) * db->num_settings);
647 new->num_settings = db->num_settings;
648 for (i = 0; i < db->num_settings; i++)
650 struct setting *setting = db->settings[i];
651 new->settings[i] = setting;
653 new->next = se->databases;
657 // Doesn't free memory associated with sdb -- nmem takes care of that
658 static void session_database_destroy(struct session_database *sdb)
663 // Initialize session_database list -- this represents this session's view
664 // of the database list -- subject to modification by the settings ws command
665 void session_init_databases(struct session *se)
668 predef_grep_databases(se, se->service, session_init_databases_fun);
671 // Probably session_init_databases_fun should be refactored instead of
673 static struct session_database *load_session_database(struct session *se,
676 struct database *db = new_database(id, se->session_nmem);
678 resolve_database(se->service, db);
680 session_init_databases_fun((void*) se, db);
682 // New sdb is head of se->databases list
683 return se->databases;
686 // Find an existing session database. If not found, load it
687 static struct session_database *find_session_database(struct session *se,
690 struct session_database *sdb;
692 for (sdb = se->databases; sdb; sdb = sdb->next)
693 if (!strcmp(sdb->database->url, id))
695 return load_session_database(se, id);
698 // Apply a session override to a database
699 void session_apply_setting(struct session *se, char *dbname, char *setting,
702 struct session_database *sdb = find_session_database(se, dbname);
703 struct conf_service *service = se->service;
704 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
705 int offset = settings_create_offset(service, setting);
707 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
710 new->target = dbname;
713 new->next = sdb->settings[offset];
714 sdb->settings[offset] = new;
716 // Force later recompute of settings-driven data structures
717 // (happens when a search starts and client connections are prepared)
729 void destroy_session(struct session *se)
731 struct session_database *sdb;
733 session_log(se, YLOG_DEBUG, "Pazpar2 session destroy");
734 session_remove_clients(se);
736 for (sdb = se->databases; sdb; sdb = sdb->next)
737 session_database_destroy(sdb);
738 normalize_cache_destroy(se->normalize_cache);
739 relevance_destroy(&se->relevance);
740 reclist_destroy(se->reclist);
741 nmem_destroy(se->nmem);
742 service_destroy(se->service);
743 yaz_mutex_destroy(&se->session_mutex);
744 wrbuf_destroy(se->wrbuf);
747 struct session *new_session(NMEM nmem, struct conf_service *service,
751 struct session *session = nmem_malloc(nmem, sizeof(*session));
755 sprintf(tmp_str, "session#%u", session_id);
757 session->session_id = session_id;
758 session_log(session, YLOG_DEBUG, "New session");
759 session->service = service;
760 session->relevance = 0;
761 session->total_hits = 0;
762 session->total_records = 0;
763 session->number_of_warnings_unknown_elements = 0;
764 session->number_of_warnings_unknown_metadata = 0;
765 session->num_termlists = 0;
766 session->reclist = 0;
767 session->clients = 0;
768 session->session_nmem = nmem;
769 session->nmem = nmem_create();
770 session->wrbuf = wrbuf_alloc();
771 session->databases = 0;
772 for (i = 0; i <= SESSION_WATCH_MAX; i++)
774 session->watchlist[i].data = 0;
775 session->watchlist[i].fun = 0;
777 session->normalize_cache = normalize_cache_create();
778 session->session_mutex = 0;
779 pazpar2_mutex_create(&session->session_mutex, tmp_str);
784 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
786 struct hitsbytarget *res = 0;
787 struct client_list *l;
791 for (l = se->clients; l; l = l->next)
794 res = nmem_malloc(nmem, sizeof(*res) * sz);
796 for (l = se->clients; l; l = l->next)
798 struct client *cl = l->client;
799 WRBUF w = wrbuf_alloc();
800 const char *name = session_setting_oneval(client_get_database(cl),
803 res[*count].id = client_get_database(cl)->database->url;
804 res[*count].name = *name ? name : "Unknown";
805 res[*count].hits = client_get_hits(cl);
806 res[*count].records = client_get_num_records(cl);
807 res[*count].diagnostic = client_get_diagnostic(cl);
808 res[*count].state = client_get_state_str(cl);
809 res[*count].connected = client_get_connection(cl) ? 1 : 0;
810 session_settings_dump(se, client_get_database(cl), w);
811 res[*count].settings_xml = w;
818 struct termlist_score **termlist(struct session *se, const char *name, int *num)
821 struct termlist_score **tl = 0;
824 for (i = 0; i < se->num_termlists; i++)
825 if (!strcmp((const char *) se->termlists[i].name, name))
827 tl = termlist_highscore(se->termlists[i].termlist, num);
834 #ifdef MISSING_HEADERS
835 void report_nmem_stats(void)
837 size_t in_use, is_free;
839 nmem_get_memory_in_use(&in_use);
840 nmem_get_memory_free(&is_free);
842 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
843 (long) in_use, (long) is_free);
847 struct record_cluster *show_single_start(struct session *se, const char *id,
848 struct record_cluster **prev_r,
849 struct record_cluster **next_r)
851 struct record_cluster *r;
854 reclist_enter(se->reclist);
857 while ((r = reclist_read_record(se->reclist)))
859 if (!strcmp(r->recid, id))
861 *next_r = reclist_read_record(se->reclist);
866 reclist_leave(se->reclist);
872 void show_single_stop(struct session *se, struct record_cluster *rec)
877 struct record_cluster **show_range_start(struct session *se,
878 struct reclist_sortparms *sp,
879 int start, int *num, int *total, Odr_int *sumhits)
881 struct record_cluster **recs;
882 struct reclist_sortparms *spp;
885 yaz_timing_t t = yaz_timing_create();
888 recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
898 for (spp = sp; spp; spp = spp->next)
899 if (spp->type == Metadata_sortkey_relevance)
901 relevance_prepare_read(se->relevance, se->reclist);
904 reclist_sort(se->reclist, sp);
906 reclist_enter(se->reclist);
907 *total = reclist_get_num_records(se->reclist);
908 *sumhits = se->total_hits;
910 for (i = 0; i < start; i++)
911 if (!reclist_read_record(se->reclist))
918 for (i = 0; i < *num; i++)
920 struct record_cluster *r = reclist_read_record(se->reclist);
928 reclist_leave(se->reclist);
932 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
933 yaz_timing_get_real(t), yaz_timing_get_user(t),
934 yaz_timing_get_sys(t));
935 yaz_timing_destroy(&t);
940 void show_range_stop(struct session *se, struct record_cluster **recs)
945 void statistics(struct session *se, struct statistics *stat)
947 struct client_list *l;
950 memset(stat, 0, sizeof(*stat));
951 for (l = se->clients; l; l = l->next)
953 struct client *cl = l->client;
954 if (!client_get_connection(cl))
955 stat->num_no_connection++;
956 switch (client_get_state(cl))
958 case Client_Connecting: stat->num_connecting++; break;
959 case Client_Working: stat->num_working++; break;
960 case Client_Idle: stat->num_idle++; break;
961 case Client_Failed: stat->num_failed++; break;
962 case Client_Error: stat->num_error++; break;
967 stat->num_hits = se->total_hits;
968 stat->num_records = se->total_records;
970 stat->num_clients = count;
973 static struct record_metadata *record_metadata_init(
974 NMEM nmem, const char *value, enum conf_metadata_type type,
975 struct _xmlAttr *attr)
977 struct record_metadata *rec_md = record_metadata_create(nmem);
978 struct record_metadata_attr **attrp = &rec_md->attributes;
980 for (; attr; attr = attr->next)
982 if (attr->children && attr->children->content)
984 if (strcmp((const char *) attr->name, "type"))
985 { /* skip the "type" attribute.. Its value is already part of
986 the element in output (md-%s) and so repeating it here
988 *attrp = nmem_malloc(nmem, sizeof(**attrp));
990 nmem_strdup(nmem, (const char *) attr->name);
992 nmem_strdup(nmem, (const char *) attr->children->content);
993 attrp = &(*attrp)->next;
999 if (type == Metadata_type_generic)
1001 char *p = nmem_strdup(nmem, value);
1003 p = normalize7bit_generic(p, " ,/.:([");
1005 rec_md->data.text.disp = p;
1006 rec_md->data.text.sort = 0;
1008 else if (type == Metadata_type_year || type == Metadata_type_date)
1013 if (type == Metadata_type_date)
1015 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1018 rec_md->data.number.min = first;
1019 rec_md->data.number.max = last;
1026 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1027 struct conf_service *service, WRBUF norm_wr)
1031 for (n = root->children; n; n = n->next)
1033 if (n->type != XML_ELEMENT_NODE)
1035 if (!strcmp((const char *) n->name, "metadata"))
1037 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1039 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1041 else if (!strcmp(name, (const char *) type))
1043 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1046 const char *norm_str;
1047 pp2_relevance_token_t prt =
1048 pp2_relevance_tokenize(service->mergekey_pct);
1050 pp2_relevance_first(prt, (const char *) value, 0);
1051 if (wrbuf_len(norm_wr) > 0)
1052 wrbuf_puts(norm_wr, " ");
1053 wrbuf_puts(norm_wr, name);
1055 pp2_relevance_token_next(prt)))
1059 wrbuf_puts(norm_wr, " ");
1060 wrbuf_puts(norm_wr, norm_str);
1064 pp2_relevance_token_destroy(prt);
1074 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1075 struct conf_service *service, NMEM nmem)
1077 char *mergekey_norm = 0;
1078 xmlNode *root = xmlDocGetRootElement(doc);
1079 WRBUF norm_wr = wrbuf_alloc();
1081 /* consider mergekey from XSL first */
1082 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1085 const char *norm_str;
1086 pp2_relevance_token_t prt =
1087 pp2_relevance_tokenize(service->mergekey_pct);
1089 pp2_relevance_first(prt, (const char *) mergekey, 0);
1090 while ((norm_str = pp2_relevance_token_next(prt)))
1094 if (wrbuf_len(norm_wr))
1095 wrbuf_puts(norm_wr, " ");
1096 wrbuf_puts(norm_wr, norm_str);
1099 pp2_relevance_token_destroy(prt);
1104 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1106 for (field_id = 0; field_id < service->num_metadata; field_id++)
1108 struct conf_metadata *ser_md = &service->metadata[field_id];
1109 if (ser_md->mergekey != Metadata_mergekey_no)
1111 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1113 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1115 /* no mergekey on this one and it is required..
1116 Generate unique key instead */
1117 wrbuf_rewind(norm_wr);
1124 /* generate unique key if none is not generated already or is empty */
1125 if (wrbuf_len(norm_wr) == 0)
1127 wrbuf_printf(norm_wr, "%s-%d",
1128 client_get_database(cl)->database->url, record_no);
1130 if (wrbuf_len(norm_wr) > 0)
1131 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1132 wrbuf_destroy(norm_wr);
1133 return mergekey_norm;
1136 /** \brief see if metadata for pz:recordfilter exists
1137 \param root xml root element of normalized record
1138 \param sdb session database for client
1139 \retval 0 if there is no metadata for pz:recordfilter
1140 \retval 1 if there is metadata for pz:recordfilter
1142 If there is no pz:recordfilter defined, this function returns 1
1146 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1151 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1156 for (n = root->children; n; n = n->next)
1158 if (n->type != XML_ELEMENT_NODE)
1160 if (!strcmp((const char *) n->name, "metadata"))
1162 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1169 if ((eq = strchr(s, '=')))
1171 else if ((eq = strchr(s, '~')))
1177 if (len == strlen((const char *)type) &&
1178 !memcmp((const char *) type, s, len))
1180 xmlChar *value = xmlNodeGetContent(n);
1181 if (value && *value)
1184 (substring && strstr((const char *) value, eq+1)) ||
1185 (!substring && !strcmp((const char *) value, eq + 1)))
1198 static int ingest_to_cluster(struct client *cl,
1202 const char *mergekey_norm);
1204 /** \brief ingest XML record
1205 \param cl client holds the result set for record
1206 \param rec record buffer (0 terminated)
1207 \param record_no record position (1, 2, ..)
1208 \param nmem working NMEM
1212 int ingest_record(struct client *cl, const char *rec,
1213 int record_no, NMEM nmem)
1215 struct session *se = client_get_session(cl);
1217 struct session_database *sdb = client_get_database(cl);
1218 struct conf_service *service = se->service;
1219 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1221 const char *mergekey_norm;
1226 root = xmlDocGetRootElement(xdoc);
1228 if (!check_record_filter(root, sdb))
1230 session_log(se, YLOG_WARN, "Filtered out record no %d from %s",
1231 record_no, sdb->database->url);
1236 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1239 session_log(se, YLOG_WARN, "Got no mergekey");
1244 if (client_get_session(cl) == se)
1245 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1252 static int ingest_to_cluster(struct client *cl,
1256 const char *mergekey_norm)
1261 struct session_database *sdb = client_get_database(cl);
1262 struct session *se = client_get_session(cl);
1263 struct conf_service *service = se->service;
1264 struct record *record = record_create(se->nmem,
1265 service->num_metadata,
1266 service->num_sortkeys, cl,
1268 struct record_cluster *cluster = reclist_insert(se->reclist,
1274 int use_term_factor = session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
1275 int term_factor = 1;
1277 if (use_term_factor) {
1278 int maxrecs = client_get_maxrecs(cl);
1279 int hits = (int) client_get_hits(cl);
1280 term_factor = max(hits, maxrecs) / max(1, maxrecs);
1281 assert(term_factor >= 1);
1282 yaz_log(YLOG_DEBUG, "Using term factor %d ", term_factor);
1287 if (global_parameters.dump_records)
1288 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1289 sdb->database->url, record_no);
1290 relevance_newrec(se->relevance, cluster);
1292 // now parsing XML record and adding data to cluster or record metadata
1293 for (n = root->children; n; n = n->next)
1295 pp2_relevance_token_t prt;
1302 if (n->type != XML_ELEMENT_NODE)
1304 if (!strcmp((const char *) n->name, "metadata"))
1306 struct conf_metadata *ser_md = 0;
1307 struct conf_sortkey *ser_sk = 0;
1308 struct record_metadata **wheretoput = 0;
1309 struct record_metadata *rec_md = 0;
1310 int md_field_id = -1;
1311 int sk_field_id = -1;
1313 type = xmlGetProp(n, (xmlChar *) "type");
1314 value = xmlNodeListGetString(xdoc, n->children, 1);
1316 if (!type || !value || !*value)
1320 = conf_service_metadata_field_id(service, (const char *) type);
1321 if (md_field_id < 0)
1323 if (se->number_of_warnings_unknown_metadata == 0)
1325 session_log(se, YLOG_WARN,
1326 "Ignoring unknown metadata element: %s", type);
1328 se->number_of_warnings_unknown_metadata++;
1332 ser_md = &service->metadata[md_field_id];
1334 if (ser_md->sortkey_offset >= 0){
1335 sk_field_id = ser_md->sortkey_offset;
1336 ser_sk = &service->sortkeys[sk_field_id];
1339 // non-merged metadata
1340 rec_md = record_metadata_init(se->nmem, (const char *) value,
1341 ser_md->type, n->properties);
1344 session_log(se, YLOG_WARN, "bad metadata data '%s' "
1345 "for element '%s'", value, type);
1348 wheretoput = &record->metadata[md_field_id];
1350 wheretoput = &(*wheretoput)->next;
1351 *wheretoput = rec_md;
1354 rec_md = record_metadata_init(se->nmem, (const char *) value,
1356 wheretoput = &cluster->metadata[md_field_id];
1358 // and polulate with data:
1359 // assign cluster or record based on merge action
1360 if (ser_md->merge == Metadata_merge_unique)
1362 struct record_metadata *mnode;
1363 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1364 if (!strcmp((const char *) mnode->data.text.disp,
1365 rec_md->data.text.disp))
1369 rec_md->next = *wheretoput;
1370 *wheretoput = rec_md;
1373 else if (ser_md->merge == Metadata_merge_longest)
1376 || strlen(rec_md->data.text.disp)
1377 > strlen((*wheretoput)->data.text.disp))
1379 *wheretoput = rec_md;
1382 const char *sort_str = 0;
1384 ser_sk->type == Metadata_sortkey_skiparticle;
1386 if (!cluster->sortkeys[sk_field_id])
1387 cluster->sortkeys[sk_field_id] =
1388 nmem_malloc(se->nmem,
1389 sizeof(union data_types));
1391 prt = pp2_relevance_tokenize(service->sort_pct);
1393 pp2_relevance_first(prt, rec_md->data.text.disp,
1396 pp2_relevance_token_next(prt);
1398 sort_str = pp2_get_sort(prt);
1400 cluster->sortkeys[sk_field_id]->text.disp =
1401 rec_md->data.text.disp;
1404 sort_str = rec_md->data.text.disp;
1405 session_log(se, YLOG_WARN,
1406 "Could not make sortkey. Bug #1858");
1408 cluster->sortkeys[sk_field_id]->text.sort =
1409 nmem_strdup(se->nmem, sort_str);
1410 pp2_relevance_token_destroy(prt);
1414 else if (ser_md->merge == Metadata_merge_all)
1416 rec_md->next = *wheretoput;
1417 *wheretoput = rec_md;
1419 else if (ser_md->merge == Metadata_merge_range)
1423 *wheretoput = rec_md;
1425 cluster->sortkeys[sk_field_id]
1430 int this_min = rec_md->data.number.min;
1431 int this_max = rec_md->data.number.max;
1432 if (this_min < (*wheretoput)->data.number.min)
1433 (*wheretoput)->data.number.min = this_min;
1434 if (this_max > (*wheretoput)->data.number.max)
1435 (*wheretoput)->data.number.max = this_max;
1440 // ranking of _all_ fields enabled ...
1442 relevance_countwords(se->relevance, cluster,
1443 (char *) value, ser_md->rank,
1446 // construct facets ... unless the client already has reported them
1447 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
1450 if (ser_md->type == Metadata_type_year)
1453 sprintf(year, "%d", rec_md->data.number.max);
1455 add_facet(se, (char *) type, year, term_factor);
1456 if (rec_md->data.number.max != rec_md->data.number.min)
1458 sprintf(year, "%d", rec_md->data.number.min);
1459 add_facet(se, (char *) type, year, term_factor);
1463 add_facet(se, (char *) type, (char *) value, term_factor);
1473 if (se->number_of_warnings_unknown_elements == 0)
1474 session_log(se, YLOG_WARN,
1475 "Unexpected element in internal record: %s", n->name);
1476 se->number_of_warnings_unknown_elements++;
1484 relevance_donerecord(se->relevance, cluster);
1485 se->total_records++;
1490 void session_log(struct session *s, int level, const char *fmt, ...)
1496 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
1497 yaz_log(level, "%u %s", s->session_id, buf);
1505 * c-file-style: "Stroustrup"
1506 * indent-tabs-mode: nil
1508 * vim: shiftwidth=4 tabstop=8 expandtab