1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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 #define MAX(a,b) ((a)>(b)?(a):(b))
85 // Note: Some things in this structure will eventually move to configuration
86 struct parameters global_parameters =
93 struct client *client;
94 struct client_list *next;
97 static void log_xml_doc(xmlDoc *doc)
99 FILE *lf = yaz_log_file();
102 #if LIBXML_VERSION >= 20600
103 xmlDocDumpFormatMemory(doc, &result, &len, 1);
105 xmlDocDumpMemory(doc, &result, &len);
109 (void) fwrite(result, 1, len, lf);
115 static void session_enter(struct session *s)
117 yaz_mutex_enter(s->session_mutex);
120 static void session_leave(struct session *s)
122 yaz_mutex_leave(s->session_mutex);
125 // Recursively traverse query structure to extract terms.
126 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
138 pull_terms(nmem, n->u.p[0], termlist, num);
139 pull_terms(nmem, n->u.p[1], termlist, num);
142 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
143 for (i = 0; i < numwords; i++)
144 termlist[(*num)++] = words[i];
152 void add_facet(struct session *s, const char *type, const char *value, int count)
154 struct conf_service *service = s->service;
155 pp2_relevance_token_t prt;
156 const char *facet_component;
157 WRBUF facet_wrbuf = wrbuf_alloc();
158 prt = pp2_relevance_tokenize(service->facet_pct);
160 pp2_relevance_first(prt, value, 0);
161 while ((facet_component = pp2_relevance_token_next(prt)))
163 if (*facet_component)
165 if (wrbuf_len(facet_wrbuf))
166 wrbuf_puts(facet_wrbuf, " ");
167 wrbuf_puts(facet_wrbuf, facet_component);
170 pp2_relevance_token_destroy(prt);
172 if (wrbuf_len(facet_wrbuf))
175 for (i = 0; i < s->num_termlists; i++)
176 if (!strcmp(s->termlists[i].name, type))
178 if (i == s->num_termlists)
180 if (i == SESSION_MAX_TERMLISTS)
182 session_log(s, YLOG_FATAL, "Too many termlists");
183 wrbuf_destroy(facet_wrbuf);
187 s->termlists[i].name = nmem_strdup(s->nmem, type);
188 s->termlists[i].termlist
189 = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
190 s->num_termlists = i + 1;
193 session_log(s, YLOG_DEBUG, "Session: facets for %s: %s norm:%s (%d)",
194 type, value, wrbuf_cstr(facet_wrbuf), count);
195 termlist_insert(s->termlists[i].termlist, wrbuf_cstr(facet_wrbuf),
198 wrbuf_destroy(facet_wrbuf);
201 static xmlDoc *record_to_xml(struct session *se,
202 struct session_database *sdb, const char *rec)
204 struct database *db = sdb->database;
207 rdoc = xmlParseMemory(rec, strlen(rec));
211 session_log(se, YLOG_FATAL, "Non-wellformed XML received from %s",
216 if (global_parameters.dump_records)
218 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->url);
225 #define MAX_XSLT_ARGS 16
227 // Add static values from session database settings if applicable
228 static void insert_settings_parameters(struct session_database *sdb,
229 struct conf_service *service,
237 for (i = 0; i < service->num_metadata; i++)
239 struct conf_metadata *md = &service->metadata[i];
242 if (md->setting == Metadata_setting_parameter &&
243 (setting = settings_lookup_offset(service, md->name)) >= 0)
245 const char *val = session_setting_oneval(sdb, setting);
246 if (val && nparms < MAX_XSLT_ARGS)
249 int len = strlen(val);
250 buf = nmem_malloc(nmem, len + 3);
252 strcpy(buf + 1, val);
255 parms[offset++] = md->name;
256 parms[offset++] = buf;
264 // Add static values from session database settings if applicable
265 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
266 struct conf_service *service)
270 for (i = 0; i < service->num_metadata; i++)
272 struct conf_metadata *md = &service->metadata[i];
275 if (md->setting == Metadata_setting_postproc &&
276 (offset = settings_lookup_offset(service, md->name)) >= 0)
278 const char *val = session_setting_oneval(sdb, offset);
281 xmlNode *r = xmlDocGetRootElement(doc);
282 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
284 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
290 static xmlDoc *normalize_record(struct session *se,
291 struct session_database *sdb,
292 struct conf_service *service,
293 const char *rec, NMEM nmem)
295 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
299 char *parms[MAX_XSLT_ARGS*2+1];
301 insert_settings_parameters(sdb, service, parms, nmem);
303 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
305 session_log(se, YLOG_WARN, "Normalize failed from %s",
310 insert_settings_values(sdb, rdoc, service);
312 if (global_parameters.dump_records)
314 session_log(se, YLOG_LOG, "Normalized record from %s",
323 void session_settings_dump(struct session *se,
324 struct session_database *db,
329 int i, num = db->num_settings;
330 for (i = 0; i < num; i++)
332 struct setting *s = db->settings[i];
333 for (;s ; s = s->next)
335 wrbuf_puts(w, "<set name=\"");
336 wrbuf_xmlputs(w, s->name);
337 wrbuf_puts(w, "\" value=\"");
338 wrbuf_xmlputs(w, s->value);
339 wrbuf_puts(w, "\"/>");
347 // Retrieve first defined value for 'name' for given database.
348 // Will be extended to take into account user associated with session
349 const char *session_setting_oneval(struct session_database *db, int offset)
351 if (offset >= db->num_settings || !db->settings[offset])
353 return db->settings[offset]->value;
356 // Prepare XSLT stylesheets for record normalization
357 // Structures are allocated on the session_wide nmem to avoid having
358 // to recompute this for every search. This would lead
359 // to leaking if a single session was to repeatedly change the PZ_XSLT
360 // setting. However, this is not a realistic use scenario.
361 static int prepare_map(struct session *se, struct session_database *sdb)
367 session_log(se, YLOG_WARN, "No settings on %s", sdb->database->url);
370 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
372 char auto_stylesheet[256];
374 if (!strcmp(s, "auto"))
376 const char *request_syntax = session_setting_oneval(
377 sdb, PZ_REQUESTSYNTAX);
381 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
382 "%s.xsl", request_syntax);
383 for (cp = auto_stylesheet; *cp; cp++)
385 /* deliberately only consider ASCII */
386 if (*cp > 32 && *cp < 127)
393 session_log(se, YLOG_WARN,
394 "No pz:requestsyntax for auto stylesheet");
397 sdb->map = normalize_cache_get(se->normalize_cache,
398 se->service->server->config, s);
405 // This analyzes settings and recomputes any supporting data structures
407 static int prepare_session_database(struct session *se,
408 struct session_database *sdb)
412 session_log(se, YLOG_WARN,
413 "No settings associated with %s", sdb->database->url);
416 if (sdb->settings[PZ_XSLT] && !sdb->map)
418 if (prepare_map(se, sdb) < 0)
424 // called if watch should be removed because http_channel is to be destroyed
425 static void session_watch_cancel(void *data, struct http_channel *c,
428 struct session_watchentry *ent = data;
435 // set watch. Returns 0=OK, -1 if watch is already set
436 int session_set_watch(struct session *s, int what,
437 session_watchfun fun, void *data,
438 struct http_channel *chan)
442 if (s->watchlist[what].fun)
447 s->watchlist[what].fun = fun;
448 s->watchlist[what].data = data;
449 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
450 session_watch_cancel);
457 void session_alert_watch(struct session *s, int what)
461 if (s->watchlist[what].fun)
463 /* our watch is no longer associated with http_channel */
465 session_watchfun fun;
467 http_remove_observer(s->watchlist[what].obs);
468 fun = s->watchlist[what].fun;
469 data = s->watchlist[what].data;
471 /* reset watch before fun is invoked - in case fun wants to set
473 s->watchlist[what].fun = 0;
474 s->watchlist[what].data = 0;
475 s->watchlist[what].obs = 0;
478 session_log(s, YLOG_DEBUG,
479 "session_alert_watch: %d calling function: %p", what, fun);
486 //callback for grep_databases
487 static void select_targets_callback(void *context, struct session_database *db)
489 struct session *se = (struct session*) context;
490 struct client *cl = client_create();
491 struct client_list *l;
492 client_set_database(cl, db);
494 client_set_session(cl, se);
495 l = xmalloc(sizeof(*l));
497 l->next = se->clients;
501 static void session_remove_clients(struct session *se)
503 struct client_list *l;
512 struct client_list *l_next = l->next;
513 client_lock(l->client);
514 client_set_session(l->client, 0);
515 client_set_database(l->client, 0);
516 client_unlock(l->client);
517 client_destroy(l->client);
523 // Associates a set of clients with a session;
524 // Note: Session-databases represent databases with per-session
526 static int select_targets(struct session *se, const char *filter)
528 return session_grep_databases(se, filter, select_targets_callback);
531 int session_active_clients(struct session *s)
533 struct client_list *l;
536 for (l = s->clients; l; l = l->next)
537 if (client_is_active(l->client))
543 int session_is_preferred_clients_ready(struct session *s)
545 struct client_list *l;
548 for (l = s->clients; l; l = l->next)
549 if (client_is_active_preferred(l->client))
551 session_log(s, YLOG_DEBUG, "Session has %d active preferred clients.", res);
557 enum pazpar2_error_code search(struct session *se,
559 const char *startrecs, const char *maxrecs,
561 const char **addinfo)
563 int live_channels = 0;
566 struct client_list *l;
569 session_log(se, YLOG_DEBUG, "Search");
573 session_remove_clients(se);
576 reclist_destroy(se->reclist);
578 relevance_destroy(&se->relevance);
579 nmem_reset(se->nmem);
580 se->total_records = se->total_hits = se->total_merged = 0;
581 se->num_termlists = 0;
582 live_channels = select_targets(se, filter);
586 return PAZPAR2_NO_TARGETS;
588 se->reclist = reclist_create(se->nmem);
590 yaz_gettimeofday(&tval);
594 for (l = se->clients; l; l = l->next)
596 struct client *cl = l->client;
599 client_set_maxrecs(cl, atoi(maxrecs));
601 client_set_startrecs(cl, atoi(startrecs));
602 if (prepare_session_database(se, client_get_database(cl)) < 0)
604 else if (client_parse_query(cl, query) < 0)
609 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
610 se->service->z3950_session_timeout,
611 se->service->server->iochan_man,
613 client_start_search(cl);
622 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
625 return PAZPAR2_NO_TARGETS;
627 return PAZPAR2_NO_ERROR;
630 // Creates a new session_database object for a database
631 static void session_init_databases_fun(void *context, struct database *db)
633 struct session *se = (struct session *) context;
634 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
640 assert(db->settings);
641 new->settings = nmem_malloc(se->session_nmem,
642 sizeof(struct settings *) * db->num_settings);
643 new->num_settings = db->num_settings;
644 for (i = 0; i < db->num_settings; i++)
646 struct setting *setting = db->settings[i];
647 new->settings[i] = setting;
649 new->next = se->databases;
653 // Doesn't free memory associated with sdb -- nmem takes care of that
654 static void session_database_destroy(struct session_database *sdb)
659 // Initialize session_database list -- this represents this session's view
660 // of the database list -- subject to modification by the settings ws command
661 void session_init_databases(struct session *se)
664 predef_grep_databases(se, se->service, session_init_databases_fun);
667 // Probably session_init_databases_fun should be refactored instead of
669 static struct session_database *load_session_database(struct session *se,
672 struct database *db = new_database(id, se->session_nmem);
674 resolve_database(se->service, db);
676 session_init_databases_fun((void*) se, db);
678 // New sdb is head of se->databases list
679 return se->databases;
682 // Find an existing session database. If not found, load it
683 static struct session_database *find_session_database(struct session *se,
686 struct session_database *sdb;
688 for (sdb = se->databases; sdb; sdb = sdb->next)
689 if (!strcmp(sdb->database->url, id))
691 return load_session_database(se, id);
694 // Apply a session override to a database
695 void session_apply_setting(struct session *se, char *dbname, char *setting,
698 struct session_database *sdb = find_session_database(se, dbname);
699 struct conf_service *service = se->service;
700 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
701 int offset = settings_create_offset(service, setting);
703 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
706 new->target = dbname;
709 new->next = sdb->settings[offset];
710 sdb->settings[offset] = new;
712 // Force later recompute of settings-driven data structures
713 // (happens when a search starts and client connections are prepared)
725 void destroy_session(struct session *se)
727 struct session_database *sdb;
729 session_log(se, YLOG_DEBUG, "Pazpar2 session destroy");
730 session_remove_clients(se);
732 for (sdb = se->databases; sdb; sdb = sdb->next)
733 session_database_destroy(sdb);
734 normalize_cache_destroy(se->normalize_cache);
735 relevance_destroy(&se->relevance);
736 reclist_destroy(se->reclist);
737 nmem_destroy(se->nmem);
738 service_destroy(se->service);
739 yaz_mutex_destroy(&se->session_mutex);
740 wrbuf_destroy(se->wrbuf);
743 struct session *new_session(NMEM nmem, struct conf_service *service,
747 struct session *session = nmem_malloc(nmem, sizeof(*session));
751 sprintf(tmp_str, "session#%u", session_id);
753 session->session_id = session_id;
754 session_log(session, YLOG_DEBUG, "New session");
755 session->service = service;
756 session->relevance = 0;
757 session->total_hits = 0;
758 session->total_records = 0;
759 session->number_of_warnings_unknown_elements = 0;
760 session->number_of_warnings_unknown_metadata = 0;
761 session->num_termlists = 0;
762 session->reclist = 0;
763 session->clients = 0;
764 session->session_nmem = nmem;
765 session->nmem = nmem_create();
766 session->wrbuf = wrbuf_alloc();
767 session->databases = 0;
768 for (i = 0; i <= SESSION_WATCH_MAX; i++)
770 session->watchlist[i].data = 0;
771 session->watchlist[i].fun = 0;
773 session->normalize_cache = normalize_cache_create();
774 session->session_mutex = 0;
775 pazpar2_mutex_create(&session->session_mutex, tmp_str);
780 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
782 struct hitsbytarget *res = 0;
783 struct client_list *l;
787 for (l = se->clients; l; l = l->next)
790 res = nmem_malloc(nmem, sizeof(*res) * sz);
792 for (l = se->clients; l; l = l->next)
794 struct client *cl = l->client;
795 WRBUF w = wrbuf_alloc();
796 const char *name = session_setting_oneval(client_get_database(cl),
799 res[*count].id = client_get_database(cl)->database->url;
800 res[*count].name = *name ? name : "Unknown";
801 res[*count].hits = client_get_hits(cl);
802 res[*count].records = client_get_num_records(cl);
803 res[*count].diagnostic = client_get_diagnostic(cl);
804 res[*count].state = client_get_state_str(cl);
805 res[*count].connected = client_get_connection(cl) ? 1 : 0;
806 session_settings_dump(se, client_get_database(cl), w);
807 res[*count].settings_xml = w;
814 struct termlist_score **termlist(struct session *se, const char *name, int *num)
817 struct termlist_score **tl = 0;
820 for (i = 0; i < se->num_termlists; i++)
821 if (!strcmp((const char *) se->termlists[i].name, name))
823 tl = termlist_highscore(se->termlists[i].termlist, num);
830 #ifdef MISSING_HEADERS
831 void report_nmem_stats(void)
833 size_t in_use, is_free;
835 nmem_get_memory_in_use(&in_use);
836 nmem_get_memory_free(&is_free);
838 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
839 (long) in_use, (long) is_free);
843 struct record_cluster *show_single_start(struct session *se, const char *id,
844 struct record_cluster **prev_r,
845 struct record_cluster **next_r)
847 struct record_cluster *r;
850 reclist_enter(se->reclist);
853 while ((r = reclist_read_record(se->reclist)))
855 if (!strcmp(r->recid, id))
857 *next_r = reclist_read_record(se->reclist);
862 reclist_leave(se->reclist);
868 void show_single_stop(struct session *se, struct record_cluster *rec)
873 struct record_cluster **show_range_start(struct session *se,
874 struct reclist_sortparms *sp,
875 int start, int *num, int *total, Odr_int *sumhits)
877 struct record_cluster **recs;
878 struct reclist_sortparms *spp;
881 yaz_timing_t t = yaz_timing_create();
884 recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
894 for (spp = sp; spp; spp = spp->next)
895 if (spp->type == Metadata_sortkey_relevance)
897 relevance_prepare_read(se->relevance, se->reclist);
900 reclist_sort(se->reclist, sp);
902 reclist_enter(se->reclist);
903 *total = reclist_get_num_records(se->reclist);
904 *sumhits = se->total_hits;
906 for (i = 0; i < start; i++)
907 if (!reclist_read_record(se->reclist))
914 for (i = 0; i < *num; i++)
916 struct record_cluster *r = reclist_read_record(se->reclist);
924 reclist_leave(se->reclist);
928 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
929 yaz_timing_get_real(t), yaz_timing_get_user(t),
930 yaz_timing_get_sys(t));
931 yaz_timing_destroy(&t);
936 void show_range_stop(struct session *se, struct record_cluster **recs)
941 void statistics(struct session *se, struct statistics *stat)
943 struct client_list *l;
946 memset(stat, 0, sizeof(*stat));
947 for (l = se->clients; l; l = l->next)
949 struct client *cl = l->client;
950 if (!client_get_connection(cl))
951 stat->num_no_connection++;
952 switch (client_get_state(cl))
954 case Client_Connecting: stat->num_connecting++; break;
955 case Client_Working: stat->num_working++; break;
956 case Client_Idle: stat->num_idle++; break;
957 case Client_Failed: stat->num_failed++; break;
958 case Client_Error: stat->num_error++; break;
963 stat->num_hits = se->total_hits;
964 stat->num_records = se->total_records;
966 stat->num_clients = count;
969 static struct record_metadata *record_metadata_init(
970 NMEM nmem, const char *value, enum conf_metadata_type type,
971 struct _xmlAttr *attr)
973 struct record_metadata *rec_md = record_metadata_create(nmem);
974 struct record_metadata_attr **attrp = &rec_md->attributes;
976 for (; attr; attr = attr->next)
978 if (attr->children && attr->children->content)
980 if (strcmp((const char *) attr->name, "type"))
981 { /* skip the "type" attribute.. Its value is already part of
982 the element in output (md-%s) and so repeating it here
984 *attrp = nmem_malloc(nmem, sizeof(**attrp));
986 nmem_strdup(nmem, (const char *) attr->name);
988 nmem_strdup(nmem, (const char *) attr->children->content);
989 attrp = &(*attrp)->next;
995 if (type == Metadata_type_generic)
997 char *p = nmem_strdup(nmem, value);
999 p = normalize7bit_generic(p, " ,/.:([");
1001 rec_md->data.text.disp = p;
1002 rec_md->data.text.sort = 0;
1004 else if (type == Metadata_type_year || type == Metadata_type_date)
1009 if (type == Metadata_type_date)
1011 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1014 rec_md->data.number.min = first;
1015 rec_md->data.number.max = last;
1022 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1023 struct conf_service *service, WRBUF norm_wr)
1027 for (n = root->children; n; n = n->next)
1029 if (n->type != XML_ELEMENT_NODE)
1031 if (!strcmp((const char *) n->name, "metadata"))
1033 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1035 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1037 else if (!strcmp(name, (const char *) type))
1039 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1042 const char *norm_str;
1043 pp2_relevance_token_t prt =
1044 pp2_relevance_tokenize(service->mergekey_pct);
1046 pp2_relevance_first(prt, (const char *) value, 0);
1047 if (wrbuf_len(norm_wr) > 0)
1048 wrbuf_puts(norm_wr, " ");
1049 wrbuf_puts(norm_wr, name);
1051 pp2_relevance_token_next(prt)))
1055 wrbuf_puts(norm_wr, " ");
1056 wrbuf_puts(norm_wr, norm_str);
1060 pp2_relevance_token_destroy(prt);
1070 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1071 struct conf_service *service, NMEM nmem)
1073 char *mergekey_norm = 0;
1074 xmlNode *root = xmlDocGetRootElement(doc);
1075 WRBUF norm_wr = wrbuf_alloc();
1077 /* consider mergekey from XSL first */
1078 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1081 const char *norm_str;
1082 pp2_relevance_token_t prt =
1083 pp2_relevance_tokenize(service->mergekey_pct);
1085 pp2_relevance_first(prt, (const char *) mergekey, 0);
1086 while ((norm_str = pp2_relevance_token_next(prt)))
1090 if (wrbuf_len(norm_wr))
1091 wrbuf_puts(norm_wr, " ");
1092 wrbuf_puts(norm_wr, norm_str);
1095 pp2_relevance_token_destroy(prt);
1100 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1102 for (field_id = 0; field_id < service->num_metadata; field_id++)
1104 struct conf_metadata *ser_md = &service->metadata[field_id];
1105 if (ser_md->mergekey != Metadata_mergekey_no)
1107 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1109 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1111 /* no mergekey on this one and it is required..
1112 Generate unique key instead */
1113 wrbuf_rewind(norm_wr);
1120 /* generate unique key if none is not generated already or is empty */
1121 if (wrbuf_len(norm_wr) == 0)
1123 wrbuf_printf(norm_wr, "%s-%d",
1124 client_get_database(cl)->database->url, record_no);
1126 if (wrbuf_len(norm_wr) > 0)
1127 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1128 wrbuf_destroy(norm_wr);
1129 return mergekey_norm;
1132 /** \brief see if metadata for pz:recordfilter exists
1133 \param root xml root element of normalized record
1134 \param sdb session database for client
1135 \retval 0 if there is no metadata for pz:recordfilter
1136 \retval 1 if there is metadata for pz:recordfilter
1138 If there is no pz:recordfilter defined, this function returns 1
1142 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1147 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1152 for (n = root->children; n; n = n->next)
1154 if (n->type != XML_ELEMENT_NODE)
1156 if (!strcmp((const char *) n->name, "metadata"))
1158 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1165 if ((eq = strchr(s, '=')))
1167 else if ((eq = strchr(s, '~')))
1173 if (len == strlen((const char *)type) &&
1174 !memcmp((const char *) type, s, len))
1176 xmlChar *value = xmlNodeGetContent(n);
1177 if (value && *value)
1180 (substring && strstr((const char *) value, eq+1)) ||
1181 (!substring && !strcmp((const char *) value, eq + 1)))
1194 static int ingest_to_cluster(struct client *cl,
1198 const char *mergekey_norm);
1200 /** \brief ingest XML record
1201 \param cl client holds the result set for record
1202 \param rec record buffer (0 terminated)
1203 \param record_no record position (1, 2, ..)
1204 \param nmem working NMEM
1208 int ingest_record(struct client *cl, const char *rec,
1209 int record_no, NMEM nmem)
1211 struct session *se = client_get_session(cl);
1213 struct session_database *sdb = client_get_database(cl);
1214 struct conf_service *service = se->service;
1215 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1217 const char *mergekey_norm;
1222 root = xmlDocGetRootElement(xdoc);
1224 if (!check_record_filter(root, sdb))
1226 session_log(se, YLOG_WARN, "Filtered out record no %d from %s",
1227 record_no, sdb->database->url);
1232 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1235 session_log(se, YLOG_WARN, "Got no mergekey");
1240 if (client_get_session(cl) == se)
1241 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1248 static int ingest_to_cluster(struct client *cl,
1252 const char *mergekey_norm)
1257 struct session_database *sdb = client_get_database(cl);
1258 struct session *se = client_get_session(cl);
1259 struct conf_service *service = se->service;
1260 struct record *record = record_create(se->nmem,
1261 service->num_metadata,
1262 service->num_sortkeys, cl,
1264 struct record_cluster *cluster = reclist_insert(se->reclist,
1270 const char *use_term_factor_str = session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
1271 int use_term_factor = 1;
1272 int term_factor = 1;
1273 if (use_term_factor_str)
1274 use_term_factor = atoi(use_term_factor_str);
1276 if (use_term_factor) {
1277 int maxrecs = client_get_maxrecs(cl);
1278 int hits = (int) client_get_hits(cl);
1279 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
1280 assert(term_factor >= 1);
1281 yaz_log(YLOG_DEBUG, "Using term factor %d ", term_factor);
1286 if (global_parameters.dump_records)
1287 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1288 sdb->database->url, record_no);
1289 relevance_newrec(se->relevance, cluster);
1291 // now parsing XML record and adding data to cluster or record metadata
1292 for (n = root->children; n; n = n->next)
1294 pp2_relevance_token_t prt;
1301 if (n->type != XML_ELEMENT_NODE)
1303 if (!strcmp((const char *) n->name, "metadata"))
1305 struct conf_metadata *ser_md = 0;
1306 struct conf_sortkey *ser_sk = 0;
1307 struct record_metadata **wheretoput = 0;
1308 struct record_metadata *rec_md = 0;
1309 int md_field_id = -1;
1310 int sk_field_id = -1;
1312 type = xmlGetProp(n, (xmlChar *) "type");
1313 value = xmlNodeListGetString(xdoc, n->children, 1);
1315 if (!type || !value || !*value)
1319 = conf_service_metadata_field_id(service, (const char *) type);
1320 if (md_field_id < 0)
1322 if (se->number_of_warnings_unknown_metadata == 0)
1324 session_log(se, YLOG_WARN,
1325 "Ignoring unknown metadata element: %s", type);
1327 se->number_of_warnings_unknown_metadata++;
1331 ser_md = &service->metadata[md_field_id];
1333 if (ser_md->sortkey_offset >= 0){
1334 sk_field_id = ser_md->sortkey_offset;
1335 ser_sk = &service->sortkeys[sk_field_id];
1338 // non-merged metadata
1339 rec_md = record_metadata_init(se->nmem, (const char *) value,
1340 ser_md->type, n->properties);
1343 session_log(se, YLOG_WARN, "bad metadata data '%s' "
1344 "for element '%s'", value, type);
1347 wheretoput = &record->metadata[md_field_id];
1349 wheretoput = &(*wheretoput)->next;
1350 *wheretoput = rec_md;
1353 rec_md = record_metadata_init(se->nmem, (const char *) value,
1355 wheretoput = &cluster->metadata[md_field_id];
1357 // and polulate with data:
1358 // assign cluster or record based on merge action
1359 if (ser_md->merge == Metadata_merge_unique)
1361 struct record_metadata *mnode;
1362 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1363 if (!strcmp((const char *) mnode->data.text.disp,
1364 rec_md->data.text.disp))
1368 rec_md->next = *wheretoput;
1369 *wheretoput = rec_md;
1372 else if (ser_md->merge == Metadata_merge_longest)
1375 || strlen(rec_md->data.text.disp)
1376 > strlen((*wheretoput)->data.text.disp))
1378 *wheretoput = rec_md;
1381 const char *sort_str = 0;
1383 ser_sk->type == Metadata_sortkey_skiparticle;
1385 if (!cluster->sortkeys[sk_field_id])
1386 cluster->sortkeys[sk_field_id] =
1387 nmem_malloc(se->nmem,
1388 sizeof(union data_types));
1390 prt = pp2_relevance_tokenize(service->sort_pct);
1392 pp2_relevance_first(prt, rec_md->data.text.disp,
1395 pp2_relevance_token_next(prt);
1397 sort_str = pp2_get_sort(prt);
1399 cluster->sortkeys[sk_field_id]->text.disp =
1400 rec_md->data.text.disp;
1403 sort_str = rec_md->data.text.disp;
1404 session_log(se, YLOG_WARN,
1405 "Could not make sortkey. Bug #1858");
1407 cluster->sortkeys[sk_field_id]->text.sort =
1408 nmem_strdup(se->nmem, sort_str);
1409 pp2_relevance_token_destroy(prt);
1413 else if (ser_md->merge == Metadata_merge_all)
1415 rec_md->next = *wheretoput;
1416 *wheretoput = rec_md;
1418 else if (ser_md->merge == Metadata_merge_range)
1422 *wheretoput = rec_md;
1424 cluster->sortkeys[sk_field_id]
1429 int this_min = rec_md->data.number.min;
1430 int this_max = rec_md->data.number.max;
1431 if (this_min < (*wheretoput)->data.number.min)
1432 (*wheretoput)->data.number.min = this_min;
1433 if (this_max > (*wheretoput)->data.number.max)
1434 (*wheretoput)->data.number.max = this_max;
1439 // ranking of _all_ fields enabled ...
1441 relevance_countwords(se->relevance, cluster,
1442 (char *) value, ser_md->rank,
1445 // construct facets ... unless the client already has reported them
1446 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
1449 if (ser_md->type == Metadata_type_year)
1452 sprintf(year, "%d", rec_md->data.number.max);
1454 add_facet(se, (char *) type, year, term_factor);
1455 if (rec_md->data.number.max != rec_md->data.number.min)
1457 sprintf(year, "%d", rec_md->data.number.min);
1458 add_facet(se, (char *) type, year, term_factor);
1462 add_facet(se, (char *) type, (char *) value, term_factor);
1472 if (se->number_of_warnings_unknown_elements == 0)
1473 session_log(se, YLOG_WARN,
1474 "Unexpected element in internal record: %s", n->name);
1475 se->number_of_warnings_unknown_elements++;
1483 relevance_donerecord(se->relevance, cluster);
1484 se->total_records++;
1489 void session_log(struct session *s, int level, const char *fmt, ...)
1495 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
1496 yaz_log(level, "%u %s", s->session_id, buf);
1504 * c-file-style: "Stroustrup"
1505 * indent-tabs-mode: nil
1507 * vim: shiftwidth=4 tabstop=8 expandtab