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 =
90 0, // predictable sessions
94 struct client *client;
95 struct client_list *next;
98 /* session counting (1) , disable client counting (0) */
99 static YAZ_MUTEX g_session_mutex = 0;
100 static int no_sessions = 0;
102 static int session_use(int delta)
105 if (!g_session_mutex)
106 yaz_mutex_create(&g_session_mutex);
107 yaz_mutex_enter(g_session_mutex);
108 no_sessions += delta;
109 sessions = no_sessions;
110 yaz_mutex_leave(g_session_mutex);
111 yaz_log(YLOG_DEBUG, "%s sesions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), no_sessions);
115 int sessions_count(void) {
116 return session_use(0);
119 static void log_xml_doc(xmlDoc *doc)
121 FILE *lf = yaz_log_file();
124 #if LIBXML_VERSION >= 20600
125 xmlDocDumpFormatMemory(doc, &result, &len, 1);
127 xmlDocDumpMemory(doc, &result, &len);
131 (void) fwrite(result, 1, len, lf);
137 static void session_enter(struct session *s)
139 yaz_mutex_enter(s->session_mutex);
142 static void session_leave(struct session *s)
144 yaz_mutex_leave(s->session_mutex);
147 // Recursively traverse query structure to extract terms.
148 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
160 pull_terms(nmem, n->u.p[0], termlist, num);
161 pull_terms(nmem, n->u.p[1], termlist, num);
164 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
165 for (i = 0; i < numwords; i++)
166 termlist[(*num)++] = words[i];
174 void add_facet(struct session *s, const char *type, const char *value, int count)
176 struct conf_service *service = s->service;
177 pp2_relevance_token_t prt;
178 const char *facet_component;
179 WRBUF facet_wrbuf = wrbuf_alloc();
180 prt = pp2_relevance_tokenize(service->facet_pct);
182 pp2_relevance_first(prt, value, 0);
183 while ((facet_component = pp2_relevance_token_next(prt)))
185 if (*facet_component)
187 if (wrbuf_len(facet_wrbuf))
188 wrbuf_puts(facet_wrbuf, " ");
189 wrbuf_puts(facet_wrbuf, facet_component);
192 pp2_relevance_token_destroy(prt);
194 if (wrbuf_len(facet_wrbuf))
197 for (i = 0; i < s->num_termlists; i++)
198 if (!strcmp(s->termlists[i].name, type))
200 if (i == s->num_termlists)
202 if (i == SESSION_MAX_TERMLISTS)
204 session_log(s, YLOG_FATAL, "Too many termlists");
205 wrbuf_destroy(facet_wrbuf);
209 s->termlists[i].name = nmem_strdup(s->nmem, type);
210 s->termlists[i].termlist
211 = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
212 s->num_termlists = i + 1;
216 session_log(s, YLOG_DEBUG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
218 termlist_insert(s->termlists[i].termlist, wrbuf_cstr(facet_wrbuf),
221 wrbuf_destroy(facet_wrbuf);
224 static xmlDoc *record_to_xml(struct session *se,
225 struct session_database *sdb, const char *rec)
227 struct database *db = sdb->database;
230 rdoc = xmlParseMemory(rec, strlen(rec));
234 session_log(se, YLOG_FATAL, "Non-wellformed XML received from %s",
239 if (global_parameters.dump_records)
241 session_log(se, YLOG_LOG, "Un-normalized record from %s", db->url);
248 #define MAX_XSLT_ARGS 16
250 // Add static values from session database settings if applicable
251 static void insert_settings_parameters(struct session_database *sdb,
252 struct conf_service *service,
260 for (i = 0; i < service->num_metadata; i++)
262 struct conf_metadata *md = &service->metadata[i];
265 if (md->setting == Metadata_setting_parameter &&
266 (setting = settings_lookup_offset(service, md->name)) >= 0)
268 const char *val = session_setting_oneval(sdb, setting);
269 if (val && nparms < MAX_XSLT_ARGS)
272 int len = strlen(val);
273 buf = nmem_malloc(nmem, len + 3);
275 strcpy(buf + 1, val);
278 parms[offset++] = md->name;
279 parms[offset++] = buf;
287 // Add static values from session database settings if applicable
288 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
289 struct conf_service *service)
293 for (i = 0; i < service->num_metadata; i++)
295 struct conf_metadata *md = &service->metadata[i];
298 if (md->setting == Metadata_setting_postproc &&
299 (offset = settings_lookup_offset(service, md->name)) >= 0)
301 const char *val = session_setting_oneval(sdb, offset);
304 xmlNode *r = xmlDocGetRootElement(doc);
305 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
307 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
313 static xmlDoc *normalize_record(struct session *se,
314 struct session_database *sdb,
315 struct conf_service *service,
316 const char *rec, NMEM nmem)
318 xmlDoc *rdoc = record_to_xml(se, sdb, rec);
322 char *parms[MAX_XSLT_ARGS*2+1];
324 insert_settings_parameters(sdb, service, parms, nmem);
326 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
328 session_log(se, YLOG_WARN, "Normalize failed from %s",
333 insert_settings_values(sdb, rdoc, service);
335 if (global_parameters.dump_records)
337 session_log(se, YLOG_LOG, "Normalized record from %s",
346 void session_settings_dump(struct session *se,
347 struct session_database *db,
352 int i, num = db->num_settings;
353 for (i = 0; i < num; i++)
355 struct setting *s = db->settings[i];
356 for (;s ; s = s->next)
358 wrbuf_puts(w, "<set name=\"");
359 wrbuf_xmlputs(w, s->name);
360 wrbuf_puts(w, "\" value=\"");
361 wrbuf_xmlputs(w, s->value);
362 wrbuf_puts(w, "\"/>");
370 // Retrieve first defined value for 'name' for given database.
371 // Will be extended to take into account user associated with session
372 const char *session_setting_oneval(struct session_database *db, int offset)
374 if (offset >= db->num_settings || !db->settings[offset])
376 return db->settings[offset]->value;
379 // Prepare XSLT stylesheets for record normalization
380 // Structures are allocated on the session_wide nmem to avoid having
381 // to recompute this for every search. This would lead
382 // to leaking if a single session was to repeatedly change the PZ_XSLT
383 // setting. However, this is not a realistic use scenario.
384 static int prepare_map(struct session *se, struct session_database *sdb)
390 session_log(se, YLOG_WARN, "No settings on %s", sdb->database->url);
393 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
395 char auto_stylesheet[256];
397 if (!strcmp(s, "auto"))
399 const char *request_syntax = session_setting_oneval(
400 sdb, PZ_REQUESTSYNTAX);
404 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
405 "%s.xsl", request_syntax);
406 for (cp = auto_stylesheet; *cp; cp++)
408 /* deliberately only consider ASCII */
409 if (*cp > 32 && *cp < 127)
416 session_log(se, YLOG_WARN,
417 "No pz:requestsyntax for auto stylesheet");
420 sdb->map = normalize_cache_get(se->normalize_cache,
421 se->service->server->config, s);
428 // This analyzes settings and recomputes any supporting data structures
430 static int prepare_session_database(struct session *se,
431 struct session_database *sdb)
435 session_log(se, YLOG_WARN,
436 "No settings associated with %s", sdb->database->url);
439 if (sdb->settings[PZ_XSLT] && !sdb->map)
441 if (prepare_map(se, sdb) < 0)
447 // called if watch should be removed because http_channel is to be destroyed
448 static void session_watch_cancel(void *data, struct http_channel *c,
451 struct session_watchentry *ent = data;
458 // set watch. Returns 0=OK, -1 if watch is already set
459 int session_set_watch(struct session *s, int what,
460 session_watchfun fun, void *data,
461 struct http_channel *chan)
465 if (s->watchlist[what].fun)
470 s->watchlist[what].fun = fun;
471 s->watchlist[what].data = data;
472 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
473 session_watch_cancel);
480 void session_alert_watch(struct session *s, int what)
484 if (s->watchlist[what].fun)
486 /* our watch is no longer associated with http_channel */
488 session_watchfun fun;
490 http_remove_observer(s->watchlist[what].obs);
491 fun = s->watchlist[what].fun;
492 data = s->watchlist[what].data;
494 /* reset watch before fun is invoked - in case fun wants to set
496 s->watchlist[what].fun = 0;
497 s->watchlist[what].data = 0;
498 s->watchlist[what].obs = 0;
501 session_log(s, YLOG_DEBUG,
502 "Alert Watch: %d calling function: %p", what, fun);
509 //callback for grep_databases
510 static void select_targets_callback(void *context, struct session_database *db)
512 struct session *se = (struct session*) context;
513 struct client *cl = client_create();
514 struct client_list *l;
515 client_set_database(cl, db);
517 client_set_session(cl, se);
519 l = xmalloc(sizeof(*l));
521 l->next = se->clients;
525 static void session_remove_clients(struct session *se)
527 struct client_list *l;
536 struct client_list *l_next = l->next;
537 client_lock(l->client);
538 client_set_session(l->client, 0);
539 client_set_database(l->client, 0);
540 client_unlock(l->client);
541 client_destroy(l->client);
547 // Associates a set of clients with a session;
548 // Note: Session-databases represent databases with per-session
550 static int select_targets(struct session *se, const char *filter)
552 return session_grep_databases(se, filter, select_targets_callback);
555 int session_active_clients(struct session *s)
557 struct client_list *l;
560 for (l = s->clients; l; l = l->next)
561 if (client_is_active(l->client))
567 int session_is_preferred_clients_ready(struct session *s)
569 struct client_list *l;
572 for (l = s->clients; l; l = l->next)
573 if (client_is_active_preferred(l->client))
575 session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
581 enum pazpar2_error_code search(struct session *se,
583 const char *startrecs, const char *maxrecs,
585 const char **addinfo)
587 int live_channels = 0;
590 struct client_list *l;
593 session_log(se, YLOG_DEBUG, "Search");
597 session_remove_clients(se);
600 reclist_destroy(se->reclist);
602 relevance_destroy(&se->relevance);
603 nmem_reset(se->nmem);
604 se->total_records = se->total_hits = se->total_merged = 0;
605 se->num_termlists = 0;
606 live_channels = select_targets(se, filter);
610 return PAZPAR2_NO_TARGETS;
612 se->reclist = reclist_create(se->nmem);
614 yaz_gettimeofday(&tval);
618 for (l = se->clients; l; l = l->next)
620 struct client *cl = l->client;
623 client_set_maxrecs(cl, atoi(maxrecs));
625 client_set_startrecs(cl, atoi(startrecs));
626 if (prepare_session_database(se, client_get_database(cl)) < 0)
628 else if (client_parse_query(cl, query) < 0)
633 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
634 se->service->z3950_session_timeout,
635 se->service->server->iochan_man,
637 client_start_search(cl);
646 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
649 return PAZPAR2_NO_TARGETS;
651 return PAZPAR2_NO_ERROR;
654 // Creates a new session_database object for a database
655 static void session_init_databases_fun(void *context, struct database *db)
657 struct session *se = (struct session *) context;
658 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
664 assert(db->settings);
665 new->settings = nmem_malloc(se->session_nmem,
666 sizeof(struct settings *) * db->num_settings);
667 new->num_settings = db->num_settings;
668 for (i = 0; i < db->num_settings; i++)
670 struct setting *setting = db->settings[i];
671 new->settings[i] = setting;
673 new->next = se->databases;
677 // Doesn't free memory associated with sdb -- nmem takes care of that
678 static void session_database_destroy(struct session_database *sdb)
683 // Initialize session_database list -- this represents this session's view
684 // of the database list -- subject to modification by the settings ws command
685 void session_init_databases(struct session *se)
688 predef_grep_databases(se, se->service, session_init_databases_fun);
691 // Probably session_init_databases_fun should be refactored instead of
693 static struct session_database *load_session_database(struct session *se,
696 struct database *db = new_database(id, se->session_nmem);
698 resolve_database(se->service, db);
700 session_init_databases_fun((void*) se, db);
702 // New sdb is head of se->databases list
703 return se->databases;
706 // Find an existing session database. If not found, load it
707 static struct session_database *find_session_database(struct session *se,
710 struct session_database *sdb;
712 for (sdb = se->databases; sdb; sdb = sdb->next)
713 if (!strcmp(sdb->database->url, id))
715 return load_session_database(se, id);
718 // Apply a session override to a database
719 void session_apply_setting(struct session *se, char *dbname, char *setting,
722 struct session_database *sdb = find_session_database(se, dbname);
723 struct conf_service *service = se->service;
724 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
725 int offset = settings_create_offset(service, setting);
727 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
730 new->target = dbname;
733 new->next = sdb->settings[offset];
734 sdb->settings[offset] = new;
736 // Force later recompute of settings-driven data structures
737 // (happens when a search starts and client connections are prepared)
749 void destroy_session(struct session *se)
751 struct session_database *sdb;
752 session_log(se, YLOG_DEBUG, "Destroying");
754 session_remove_clients(se);
756 for (sdb = se->databases; sdb; sdb = sdb->next)
757 session_database_destroy(sdb);
758 normalize_cache_destroy(se->normalize_cache);
759 relevance_destroy(&se->relevance);
760 reclist_destroy(se->reclist);
761 nmem_destroy(se->nmem);
762 service_destroy(se->service);
763 yaz_mutex_destroy(&se->session_mutex);
764 wrbuf_destroy(se->wrbuf);
767 size_t session_get_memory_status(struct session *session) {
771 session_enter(session);
772 session_nmem = nmem_total(session->nmem);
773 session_leave(session);
778 struct session *new_session(NMEM nmem, struct conf_service *service,
782 struct session *session = nmem_malloc(nmem, sizeof(*session));
786 sprintf(tmp_str, "session#%u", session_id);
788 session->session_id = session_id;
789 session_log(session, YLOG_DEBUG, "New");
790 session->service = service;
791 session->relevance = 0;
792 session->total_hits = 0;
793 session->total_records = 0;
794 session->number_of_warnings_unknown_elements = 0;
795 session->number_of_warnings_unknown_metadata = 0;
796 session->num_termlists = 0;
797 session->reclist = 0;
798 session->clients = 0;
799 session->session_nmem = nmem;
800 session->nmem = nmem_create();
801 session->wrbuf = wrbuf_alloc();
802 session->databases = 0;
803 for (i = 0; i <= SESSION_WATCH_MAX; i++)
805 session->watchlist[i].data = 0;
806 session->watchlist[i].fun = 0;
808 session->normalize_cache = normalize_cache_create();
809 session->session_mutex = 0;
810 pazpar2_mutex_create(&session->session_mutex, tmp_str);
815 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
817 struct hitsbytarget *res = 0;
818 struct client_list *l;
822 for (l = se->clients; l; l = l->next)
825 res = nmem_malloc(nmem, sizeof(*res) * sz);
827 for (l = se->clients; l; l = l->next)
829 struct client *cl = l->client;
830 WRBUF w = wrbuf_alloc();
831 const char *name = session_setting_oneval(client_get_database(cl),
834 res[*count].id = client_get_database(cl)->database->url;
835 res[*count].name = *name ? name : "Unknown";
836 res[*count].hits = client_get_hits(cl);
837 res[*count].records = client_get_num_records(cl);
838 res[*count].diagnostic = client_get_diagnostic(cl);
839 res[*count].state = client_get_state_str(cl);
840 res[*count].connected = client_get_connection(cl) ? 1 : 0;
841 session_settings_dump(se, client_get_database(cl), w);
842 res[*count].settings_xml = w;
849 struct termlist_score **termlist(struct session *se, const char *name, int *num)
852 struct termlist_score **tl = 0;
855 for (i = 0; i < se->num_termlists; i++)
856 if (!strcmp((const char *) se->termlists[i].name, name))
858 tl = termlist_highscore(se->termlists[i].termlist, num);
865 #ifdef MISSING_HEADERS
866 void report_nmem_stats(void)
868 size_t in_use, is_free;
870 nmem_get_memory_in_use(&in_use);
871 nmem_get_memory_free(&is_free);
873 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
874 (long) in_use, (long) is_free);
878 struct record_cluster *show_single_start(struct session *se, const char *id,
879 struct record_cluster **prev_r,
880 struct record_cluster **next_r)
882 struct record_cluster *r = 0;
889 reclist_enter(se->reclist);
890 while ((r = reclist_read_record(se->reclist)))
892 if (!strcmp(r->recid, id))
894 *next_r = reclist_read_record(se->reclist);
899 reclist_leave(se->reclist);
906 void show_single_stop(struct session *se, struct record_cluster *rec)
911 struct record_cluster **show_range_start(struct session *se,
912 struct reclist_sortparms *sp,
913 int start, int *num, int *total, Odr_int *sumhits)
915 struct record_cluster **recs;
916 struct reclist_sortparms *spp;
919 yaz_timing_t t = yaz_timing_create();
922 recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
932 for (spp = sp; spp; spp = spp->next)
933 if (spp->type == Metadata_sortkey_relevance)
935 relevance_prepare_read(se->relevance, se->reclist);
938 reclist_sort(se->reclist, sp);
940 reclist_enter(se->reclist);
941 *total = reclist_get_num_records(se->reclist);
942 *sumhits = se->total_hits;
944 for (i = 0; i < start; i++)
945 if (!reclist_read_record(se->reclist))
952 for (i = 0; i < *num; i++)
954 struct record_cluster *r = reclist_read_record(se->reclist);
962 reclist_leave(se->reclist);
966 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
967 yaz_timing_get_real(t), yaz_timing_get_user(t),
968 yaz_timing_get_sys(t));
969 yaz_timing_destroy(&t);
974 void show_range_stop(struct session *se, struct record_cluster **recs)
979 void statistics(struct session *se, struct statistics *stat)
981 struct client_list *l;
984 memset(stat, 0, sizeof(*stat));
985 for (l = se->clients; l; l = l->next)
987 struct client *cl = l->client;
988 if (!client_get_connection(cl))
989 stat->num_no_connection++;
990 switch (client_get_state(cl))
992 case Client_Connecting: stat->num_connecting++; break;
993 case Client_Working: stat->num_working++; break;
994 case Client_Idle: stat->num_idle++; break;
995 case Client_Failed: stat->num_failed++; break;
996 case Client_Error: stat->num_error++; break;
1001 stat->num_hits = se->total_hits;
1002 stat->num_records = se->total_records;
1004 stat->num_clients = count;
1007 static struct record_metadata *record_metadata_init(
1008 NMEM nmem, const char *value, enum conf_metadata_type type,
1009 struct _xmlAttr *attr)
1011 struct record_metadata *rec_md = record_metadata_create(nmem);
1012 struct record_metadata_attr **attrp = &rec_md->attributes;
1014 for (; attr; attr = attr->next)
1016 if (attr->children && attr->children->content)
1018 if (strcmp((const char *) attr->name, "type"))
1019 { /* skip the "type" attribute.. Its value is already part of
1020 the element in output (md-%s) and so repeating it here
1022 *attrp = nmem_malloc(nmem, sizeof(**attrp));
1024 nmem_strdup(nmem, (const char *) attr->name);
1026 nmem_strdup(nmem, (const char *) attr->children->content);
1027 attrp = &(*attrp)->next;
1033 if (type == Metadata_type_generic)
1035 char *p = nmem_strdup(nmem, value);
1037 p = normalize7bit_generic(p, " ,/.:([");
1039 rec_md->data.text.disp = p;
1040 rec_md->data.text.sort = 0;
1042 else if (type == Metadata_type_year || type == Metadata_type_date)
1047 if (type == Metadata_type_date)
1049 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1052 rec_md->data.number.min = first;
1053 rec_md->data.number.max = last;
1060 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
1061 struct conf_service *service, WRBUF norm_wr)
1065 for (n = root->children; n; n = n->next)
1067 if (n->type != XML_ELEMENT_NODE)
1069 if (!strcmp((const char *) n->name, "metadata"))
1071 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1073 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1075 else if (!strcmp(name, (const char *) type))
1077 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1080 const char *norm_str;
1081 pp2_relevance_token_t prt =
1082 pp2_relevance_tokenize(service->mergekey_pct);
1084 pp2_relevance_first(prt, (const char *) value, 0);
1085 if (wrbuf_len(norm_wr) > 0)
1086 wrbuf_puts(norm_wr, " ");
1087 wrbuf_puts(norm_wr, name);
1089 pp2_relevance_token_next(prt)))
1093 wrbuf_puts(norm_wr, " ");
1094 wrbuf_puts(norm_wr, norm_str);
1098 pp2_relevance_token_destroy(prt);
1108 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1109 struct conf_service *service, NMEM nmem)
1111 char *mergekey_norm = 0;
1112 xmlNode *root = xmlDocGetRootElement(doc);
1113 WRBUF norm_wr = wrbuf_alloc();
1115 /* consider mergekey from XSL first */
1116 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1119 const char *norm_str;
1120 pp2_relevance_token_t prt =
1121 pp2_relevance_tokenize(service->mergekey_pct);
1123 pp2_relevance_first(prt, (const char *) mergekey, 0);
1124 while ((norm_str = pp2_relevance_token_next(prt)))
1128 if (wrbuf_len(norm_wr))
1129 wrbuf_puts(norm_wr, " ");
1130 wrbuf_puts(norm_wr, norm_str);
1133 pp2_relevance_token_destroy(prt);
1138 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1140 for (field_id = 0; field_id < service->num_metadata; field_id++)
1142 struct conf_metadata *ser_md = &service->metadata[field_id];
1143 if (ser_md->mergekey != Metadata_mergekey_no)
1145 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1147 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1149 /* no mergekey on this one and it is required..
1150 Generate unique key instead */
1151 wrbuf_rewind(norm_wr);
1158 /* generate unique key if none is not generated already or is empty */
1159 if (wrbuf_len(norm_wr) == 0)
1161 wrbuf_printf(norm_wr, "%s-%d",
1162 client_get_database(cl)->database->url, record_no);
1164 if (wrbuf_len(norm_wr) > 0)
1165 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1166 wrbuf_destroy(norm_wr);
1167 return mergekey_norm;
1170 /** \brief see if metadata for pz:recordfilter exists
1171 \param root xml root element of normalized record
1172 \param sdb session database for client
1173 \retval 0 if there is no metadata for pz:recordfilter
1174 \retval 1 if there is metadata for pz:recordfilter
1176 If there is no pz:recordfilter defined, this function returns 1
1180 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1185 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1190 for (n = root->children; n; n = n->next)
1192 if (n->type != XML_ELEMENT_NODE)
1194 if (!strcmp((const char *) n->name, "metadata"))
1196 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1203 if ((eq = strchr(s, '=')))
1205 else if ((eq = strchr(s, '~')))
1211 if (len == strlen((const char *)type) &&
1212 !memcmp((const char *) type, s, len))
1214 xmlChar *value = xmlNodeGetContent(n);
1215 if (value && *value)
1218 (substring && strstr((const char *) value, eq+1)) ||
1219 (!substring && !strcmp((const char *) value, eq + 1)))
1232 static int ingest_to_cluster(struct client *cl,
1236 const char *mergekey_norm);
1238 /** \brief ingest XML record
1239 \param cl client holds the result set for record
1240 \param rec record buffer (0 terminated)
1241 \param record_no record position (1, 2, ..)
1242 \param nmem working NMEM
1246 int ingest_record(struct client *cl, const char *rec,
1247 int record_no, NMEM nmem)
1249 struct session *se = client_get_session(cl);
1251 struct session_database *sdb = client_get_database(cl);
1252 struct conf_service *service = se->service;
1253 xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
1255 const char *mergekey_norm;
1260 root = xmlDocGetRootElement(xdoc);
1262 if (!check_record_filter(root, sdb))
1264 session_log(se, YLOG_WARN, "Filtered out record no %d from %s",
1265 record_no, sdb->database->url);
1270 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1273 session_log(se, YLOG_WARN, "Got no mergekey");
1278 if (client_get_session(cl) == se)
1279 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1286 static int ingest_to_cluster(struct client *cl,
1290 const char *mergekey_norm)
1295 struct session_database *sdb = client_get_database(cl);
1296 struct session *se = client_get_session(cl);
1297 struct conf_service *service = se->service;
1298 struct record *record = record_create(se->nmem,
1299 service->num_metadata,
1300 service->num_sortkeys, cl,
1302 struct record_cluster *cluster = reclist_insert(se->reclist,
1308 const char *use_term_factor_str = session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
1309 int use_term_factor = 0;
1310 int term_factor = 1;
1311 if (use_term_factor_str && use_term_factor_str[0] != 0)
1312 use_term_factor = atoi(use_term_factor_str);
1313 if (use_term_factor) {
1314 int maxrecs = client_get_maxrecs(cl);
1315 int hits = (int) client_get_hits(cl);
1316 term_factor = MAX(hits, maxrecs) / MAX(1, maxrecs);
1317 assert(term_factor >= 1);
1318 yaz_log(YLOG_DEBUG, "Using term factor: %d (%d / %d)", term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
1323 if (global_parameters.dump_records)
1324 session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1325 sdb->database->url, record_no);
1326 relevance_newrec(se->relevance, cluster);
1328 // now parsing XML record and adding data to cluster or record metadata
1329 for (n = root->children; n; n = n->next)
1331 pp2_relevance_token_t prt;
1338 if (n->type != XML_ELEMENT_NODE)
1340 if (!strcmp((const char *) n->name, "metadata"))
1342 struct conf_metadata *ser_md = 0;
1343 struct conf_sortkey *ser_sk = 0;
1344 struct record_metadata **wheretoput = 0;
1345 struct record_metadata *rec_md = 0;
1346 int md_field_id = -1;
1347 int sk_field_id = -1;
1349 type = xmlGetProp(n, (xmlChar *) "type");
1350 value = xmlNodeListGetString(xdoc, n->children, 1);
1352 if (!type || !value || !*value)
1356 = conf_service_metadata_field_id(service, (const char *) type);
1357 if (md_field_id < 0)
1359 if (se->number_of_warnings_unknown_metadata == 0)
1361 session_log(se, YLOG_WARN,
1362 "Ignoring unknown metadata element: %s", type);
1364 se->number_of_warnings_unknown_metadata++;
1368 ser_md = &service->metadata[md_field_id];
1370 if (ser_md->sortkey_offset >= 0){
1371 sk_field_id = ser_md->sortkey_offset;
1372 ser_sk = &service->sortkeys[sk_field_id];
1375 // non-merged metadata
1376 rec_md = record_metadata_init(se->nmem, (const char *) value,
1377 ser_md->type, n->properties);
1380 session_log(se, YLOG_WARN, "bad metadata data '%s' "
1381 "for element '%s'", value, type);
1384 wheretoput = &record->metadata[md_field_id];
1386 wheretoput = &(*wheretoput)->next;
1387 *wheretoput = rec_md;
1390 rec_md = record_metadata_init(se->nmem, (const char *) value,
1392 wheretoput = &cluster->metadata[md_field_id];
1394 // and polulate with data:
1395 // assign cluster or record based on merge action
1396 if (ser_md->merge == Metadata_merge_unique)
1398 struct record_metadata *mnode;
1399 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1400 if (!strcmp((const char *) mnode->data.text.disp,
1401 rec_md->data.text.disp))
1405 rec_md->next = *wheretoput;
1406 *wheretoput = rec_md;
1409 else if (ser_md->merge == Metadata_merge_longest)
1412 || strlen(rec_md->data.text.disp)
1413 > strlen((*wheretoput)->data.text.disp))
1415 *wheretoput = rec_md;
1418 const char *sort_str = 0;
1420 ser_sk->type == Metadata_sortkey_skiparticle;
1422 if (!cluster->sortkeys[sk_field_id])
1423 cluster->sortkeys[sk_field_id] =
1424 nmem_malloc(se->nmem,
1425 sizeof(union data_types));
1427 prt = pp2_relevance_tokenize(service->sort_pct);
1429 pp2_relevance_first(prt, rec_md->data.text.disp,
1432 pp2_relevance_token_next(prt);
1434 sort_str = pp2_get_sort(prt);
1436 cluster->sortkeys[sk_field_id]->text.disp =
1437 rec_md->data.text.disp;
1440 sort_str = rec_md->data.text.disp;
1441 session_log(se, YLOG_WARN,
1442 "Could not make sortkey. Bug #1858");
1444 cluster->sortkeys[sk_field_id]->text.sort =
1445 nmem_strdup(se->nmem, sort_str);
1446 pp2_relevance_token_destroy(prt);
1450 else if (ser_md->merge == Metadata_merge_all)
1452 rec_md->next = *wheretoput;
1453 *wheretoput = rec_md;
1455 else if (ser_md->merge == Metadata_merge_range)
1459 *wheretoput = rec_md;
1461 cluster->sortkeys[sk_field_id]
1466 int this_min = rec_md->data.number.min;
1467 int this_max = rec_md->data.number.max;
1468 if (this_min < (*wheretoput)->data.number.min)
1469 (*wheretoput)->data.number.min = this_min;
1470 if (this_max > (*wheretoput)->data.number.max)
1471 (*wheretoput)->data.number.max = this_max;
1476 // ranking of _all_ fields enabled ...
1478 relevance_countwords(se->relevance, cluster,
1479 (char *) value, ser_md->rank,
1482 // construct facets ... unless the client already has reported them
1483 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
1486 if (ser_md->type == Metadata_type_year)
1489 sprintf(year, "%d", rec_md->data.number.max);
1491 add_facet(se, (char *) type, year, term_factor);
1492 if (rec_md->data.number.max != rec_md->data.number.min)
1494 sprintf(year, "%d", rec_md->data.number.min);
1495 add_facet(se, (char *) type, year, term_factor);
1499 add_facet(se, (char *) type, (char *) value, term_factor);
1509 if (se->number_of_warnings_unknown_elements == 0)
1510 session_log(se, YLOG_WARN,
1511 "Unexpected element in internal record: %s", n->name);
1512 se->number_of_warnings_unknown_elements++;
1520 relevance_donerecord(se->relevance, cluster);
1521 se->total_records++;
1526 void session_log(struct session *s, int level, const char *fmt, ...)
1532 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
1533 yaz_log(level, "Session (%u): %s", s->session_id, buf);
1541 * c-file-style: "Stroustrup"
1542 * indent-tabs-mode: nil
1544 * vim: shiftwidth=4 tabstop=8 expandtab