1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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
41 #include <yaz/marcdisp.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include <yaz/proto.h>
45 #include <yaz/readconf.h>
46 #include <yaz/pquery.h>
47 #include <yaz/otherinfo.h>
48 #include <yaz/yaz-util.h>
50 #include <yaz/query-charset.h>
51 #include <yaz/querytowrbuf.h>
52 #include <yaz/oid_db.h>
53 #include <yaz/snprintf.h>
57 #include <yaz/timing.h>
60 #include "parameters.h"
64 #include "termlists.h"
66 #include "relevance.h"
70 #include "normalize7bit.h"
73 #define TERMLIST_HIGH_SCORE 25
77 // Note: Some things in this structure will eventually move to configuration
78 struct parameters global_parameters =
85 static void log_xml_doc(xmlDoc *doc)
87 FILE *lf = yaz_log_file();
90 #if LIBXML_VERSION >= 20600
91 xmlDocDumpFormatMemory(doc, &result, &len, 1);
93 xmlDocDumpMemory(doc, &result, &len);
97 fwrite(result, 1, len, lf);
103 // Recursively traverse query structure to extract terms.
104 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
116 pull_terms(nmem, n->u.p[0], termlist, num);
117 pull_terms(nmem, n->u.p[1], termlist, num);
120 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
121 for (i = 0; i < numwords; i++)
122 termlist[(*num)++] = words[i];
131 static void add_facet(struct session *s, const char *type, const char *value)
137 for (i = 0; i < s->num_termlists; i++)
138 if (!strcmp(s->termlists[i].name, type))
140 if (i == s->num_termlists)
142 if (i == SESSION_MAX_TERMLISTS)
144 yaz_log(YLOG_FATAL, "Too many termlists");
148 s->termlists[i].name = nmem_strdup(s->nmem, type);
149 s->termlists[i].termlist
150 = termlist_create(s->nmem, s->expected_maxrecs,
151 TERMLIST_HIGH_SCORE);
152 s->num_termlists = i + 1;
154 termlist_insert(s->termlists[i].termlist, value);
157 xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
159 struct database *db = sdb->database;
162 rdoc = xmlParseMemory(rec, strlen(rec));
166 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
171 if (global_parameters.dump_records)
173 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
180 #define MAX_XSLT_ARGS 16
182 // Add static values from session database settings if applicable
183 static void insert_settings_parameters(struct session_database *sdb,
184 struct session *se, char **parms)
186 struct conf_service *service = se->service;
191 for (i = 0; i < service->num_metadata; i++)
193 struct conf_metadata *md = &service->metadata[i];
196 if (md->setting == Metadata_setting_parameter &&
197 (setting = settings_offset(service, md->name)) > 0)
199 const char *val = session_setting_oneval(sdb, setting);
200 if (val && nparms < MAX_XSLT_ARGS)
203 int len = strlen(val);
204 buf = nmem_malloc(se->nmem, len + 3);
206 strcpy(buf + 1, val);
209 parms[offset++] = md->name;
210 parms[offset++] = buf;
218 // Add static values from session database settings if applicable
219 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
220 struct conf_service *service)
224 for (i = 0; i < service->num_metadata; i++)
226 struct conf_metadata *md = &service->metadata[i];
229 if (md->setting == Metadata_setting_postproc &&
230 (offset = settings_offset(service, md->name)) > 0)
232 const char *val = session_setting_oneval(sdb, offset);
235 xmlNode *r = xmlDocGetRootElement(doc);
236 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
238 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
244 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
247 struct database_retrievalmap *m;
248 xmlDoc *rdoc = record_to_xml(sdb, rec);
251 for (m = sdb->map; m; m = m->next)
257 char *parms[MAX_XSLT_ARGS*2+1];
259 insert_settings_parameters(sdb, se, parms);
263 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
267 new = marcmap_apply(m->marcmap, rdoc);
270 root = xmlDocGetRootElement(new);
272 if (!new || !root || !(root->children))
274 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
286 insert_settings_values(sdb, rdoc, se->service);
288 if (global_parameters.dump_records)
290 yaz_log(YLOG_LOG, "Normalized record from %s",
298 // Retrieve first defined value for 'name' for given database.
299 // Will be extended to take into account user associated with session
300 const char *session_setting_oneval(struct session_database *db, int offset)
302 if (!db->settings[offset])
304 return db->settings[offset]->value;
307 // Prepare XSLT stylesheets for record normalization
308 // Structures are allocated on the session_wide nmem to avoid having
309 // to recompute this for every search. This would lead
310 // to leaking if a single session was to repeatedly change the PZ_XSLT
311 // setting. However, this is not a realistic use scenario.
312 static int prepare_map(struct session *se, struct session_database *sdb)
318 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
321 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
324 struct database_retrievalmap **m = &sdb->map;
326 char auto_stylesheet[256];
328 if (!strcmp(s, "auto"))
330 const char *request_syntax = session_setting_oneval(
331 sdb, PZ_REQUESTSYNTAX);
335 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
336 "%s.xsl", request_syntax);
337 for (cp = auto_stylesheet; *cp; cp++)
339 /* deliberately only consider ASCII */
340 if (*cp > 32 && *cp < 127)
347 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
350 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
351 for (i = 0; i < num; i++)
353 WRBUF fname = conf_get_fname(se->service, stylesheets[i]);
355 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
359 if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
361 (*m)->marcmap = NULL;
362 if (!((*m)->stylesheet =
363 xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
365 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
367 wrbuf_destroy(fname);
372 else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
374 (*m)->stylesheet = NULL;
375 if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), se->session_nmem)))
377 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
379 wrbuf_destroy(fname);
383 wrbuf_destroy(fname);
388 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
393 // This analyzes settings and recomputes any supporting data structures
395 static int prepare_session_database(struct session *se,
396 struct session_database *sdb)
401 "No settings associated with %s", sdb->database->url);
404 if (sdb->settings[PZ_XSLT] && !sdb->map)
406 if (prepare_map(se, sdb) < 0)
412 // called if watch should be removed because http_channel is to be destroyed
413 static void session_watch_cancel(void *data, struct http_channel *c,
416 struct session_watchentry *ent = data;
423 // set watch. Returns 0=OK, -1 if watch is already set
424 int session_set_watch(struct session *s, int what,
425 session_watchfun fun, void *data,
426 struct http_channel *chan)
428 if (s->watchlist[what].fun)
430 s->watchlist[what].fun = fun;
431 s->watchlist[what].data = data;
432 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
433 session_watch_cancel);
437 void session_alert_watch(struct session *s, int what)
439 if (s->watchlist[what].fun)
441 /* our watch is no longer associated with http_channel */
443 session_watchfun fun;
445 http_remove_observer(s->watchlist[what].obs);
446 fun = s->watchlist[what].fun;
447 data = s->watchlist[what].data;
449 /* reset watch before fun is invoked - in case fun wants to set
451 s->watchlist[what].fun = 0;
452 s->watchlist[what].data = 0;
453 s->watchlist[what].obs = 0;
459 //callback for grep_databases
460 static void select_targets_callback(void *context, struct session_database *db)
462 struct session *se = (struct session*) context;
463 struct client *cl = client_create();
464 client_set_database(cl, db);
465 client_set_session(cl, se);
468 // Associates a set of clients with a session;
469 // Note: Session-databases represent databases with per-session
471 int select_targets(struct session *se, struct database_criterion *crit)
474 client_destroy(se->clients);
476 return session_grep_databases(se, crit, select_targets_callback);
479 int session_active_clients(struct session *s)
484 for (c = s->clients; c; c = client_next_in_session(c))
485 if (client_is_active(c))
491 // parses crit1=val1,crit2=val2|val3,...
492 static struct database_criterion *parse_filter(NMEM m, const char *buf)
494 struct database_criterion *res = 0;
501 nmem_strsplit(m, ",", buf, &values, &num);
502 for (i = 0; i < num; i++)
507 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
508 char *eq = strchr(values[i], '=');
511 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
515 new->name = values[i];
516 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
518 for (subi = 0; subi < subnum; subi++)
520 struct database_criterion_value *newv
521 = nmem_malloc(m, sizeof(*newv));
522 newv->value = subvalues[subi];
523 newv->next = new->values;
532 enum pazpar2_error_code search(struct session *se,
533 const char *query, const char *filter,
534 const char **addinfo)
536 int live_channels = 0;
540 struct database_criterion *criteria;
542 yaz_log(YLOG_DEBUG, "Search");
545 nmem_reset(se->nmem);
547 se->total_records = se->total_hits = se->total_merged = 0;
549 se->num_termlists = 0;
550 criteria = parse_filter(se->nmem, filter);
551 live_channels = select_targets(se, criteria);
554 int maxrecs = live_channels * global_parameters.toget; // This is buggy!!!
555 se->reclist = reclist_create(se->nmem, maxrecs);
556 se->expected_maxrecs = maxrecs;
559 return PAZPAR2_NO_TARGETS;
561 for (cl = se->clients; cl; cl = client_next_in_session(cl))
563 if (prepare_session_database(se, client_get_database(cl)) < 0)
565 *addinfo = client_get_database(cl)->database->url;
566 return PAZPAR2_CONFIG_TARGET;
568 // Parse query for target
569 if (client_parse_query(cl, query) < 0)
574 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
575 se->service->z3950_session_timeout))
576 client_start_search(cl);
580 // If no queries could be mapped, we signal an error
584 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
586 return PAZPAR2_NO_ERROR;
589 // Creates a new session_database object for a database
590 static void session_init_databases_fun(void *context, struct database *db)
592 struct session *se = (struct session *) context;
593 struct conf_service *service = se->service;
594 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
595 int num = settings_num(service);
602 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
603 memset(new->settings, 0, sizeof(struct settings*) * num);
607 for (i = 0; i < num; i++)
608 new->settings[i] = db->settings[i];
610 new->next = se->databases;
614 // Doesn't free memory associated with sdb -- nmem takes care of that
615 static void session_database_destroy(struct session_database *sdb)
617 struct database_retrievalmap *m;
619 for (m = sdb->map; m; m = m->next)
620 xsltFreeStylesheet(m->stylesheet);
623 // Initialize session_database list -- this represents this session's view
624 // of the database list -- subject to modification by the settings ws command
625 void session_init_databases(struct session *se)
628 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
631 // Probably session_init_databases_fun should be refactored instead of
633 static struct session_database *load_session_database(struct session *se,
636 struct database *db = find_database(id, 0, se->service);
638 resolve_database(db);
640 session_init_databases_fun((void*) se, db);
641 // New sdb is head of se->databases list
642 return se->databases;
645 // Find an existing session database. If not found, load it
646 static struct session_database *find_session_database(struct session *se,
649 struct session_database *sdb;
651 for (sdb = se->databases; sdb; sdb = sdb->next)
652 if (!strcmp(sdb->database->url, id))
654 return load_session_database(se, id);
657 // Apply a session override to a database
658 void session_apply_setting(struct session *se, char *dbname, char *setting,
661 struct session_database *sdb = find_session_database(se, dbname);
662 struct conf_service *service = se->service;
663 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
664 int offset = settings_offset_cprefix(service, setting);
668 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
671 // Jakub: This breaks the filter setting.
672 /*if (offset == PZ_ID)
674 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
678 new->target = dbname;
681 new->next = sdb->settings[offset];
682 sdb->settings[offset] = new;
684 // Force later recompute of settings-driven data structures
685 // (happens when a search starts and client connections are prepared)
691 struct database_retrievalmap *m;
692 // We don't worry about the map structure -- it's in nmem
693 for (m = sdb->map; m; m = m->next)
694 xsltFreeStylesheet(m->stylesheet);
701 void destroy_session(struct session *s)
703 struct session_database *sdb;
706 client_destroy(s->clients);
707 for (sdb = s->databases; sdb; sdb = sdb->next)
708 session_database_destroy(sdb);
709 nmem_destroy(s->nmem);
710 service_destroy(s->service);
711 wrbuf_destroy(s->wrbuf);
714 struct session *new_session(NMEM nmem, struct conf_service *service)
717 struct session *session = nmem_malloc(nmem, sizeof(*session));
719 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
721 session->service = service;
722 session->relevance = 0;
723 session->total_hits = 0;
724 session->total_records = 0;
725 session->number_of_warnings_unknown_elements = 0;
726 session->number_of_warnings_unknown_metadata = 0;
727 session->num_termlists = 0;
728 session->reclist = 0;
729 session->clients = 0;
730 session->expected_maxrecs = 0;
731 session->session_nmem = nmem;
732 session->nmem = nmem_create();
733 session->wrbuf = wrbuf_alloc();
734 session->databases = 0;
735 for (i = 0; i <= SESSION_WATCH_MAX; i++)
737 session->watchlist[i].data = 0;
738 session->watchlist[i].fun = 0;
743 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
745 struct hitsbytarget *res = 0;
749 for (cl = se->clients; cl; cl = client_next_in_session(cl))
752 res = nmem_malloc(nmem, sizeof(*res) * sz);
754 for (cl = se->clients; cl; cl = client_next_in_session(cl))
756 const char *name = session_setting_oneval(client_get_database(cl),
759 res[*count].id = client_get_database(cl)->database->url;
760 res[*count].name = *name ? name : "Unknown";
761 res[*count].hits = client_get_hits(cl);
762 res[*count].records = client_get_num_records(cl);
763 res[*count].diagnostic = client_get_diagnostic(cl);
764 res[*count].state = client_get_state_str(cl);
765 res[*count].connected = client_get_connection(cl) ? 1 : 0;
771 struct termlist_score **termlist(struct session *s, const char *name, int *num)
775 for (i = 0; i < s->num_termlists; i++)
776 if (!strcmp((const char *) s->termlists[i].name, name))
777 return termlist_highscore(s->termlists[i].termlist, num);
781 #ifdef MISSING_HEADERS
782 void report_nmem_stats(void)
784 size_t in_use, is_free;
786 nmem_get_memory_in_use(&in_use);
787 nmem_get_memory_free(&is_free);
789 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
790 (long) in_use, (long) is_free);
794 struct record_cluster *show_single(struct session *s, const char *id,
795 struct record_cluster **prev_r,
796 struct record_cluster **next_r)
798 struct record_cluster *r;
800 reclist_rewind(s->reclist);
803 while ((r = reclist_read_record(s->reclist)))
805 if (!strcmp(r->recid, id))
807 *next_r = reclist_read_record(s->reclist);
815 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
816 int start, int *num, int *total, int *sumhits,
819 struct record_cluster **recs = nmem_malloc(nmem_show, *num
820 * sizeof(struct record_cluster *));
821 struct reclist_sortparms *spp;
824 yaz_timing_t t = yaz_timing_create();
836 for (spp = sp; spp; spp = spp->next)
837 if (spp->type == Metadata_sortkey_relevance)
839 relevance_prepare_read(s->relevance, s->reclist);
842 reclist_sort(s->reclist, sp);
844 *total = s->reclist->num_records;
845 *sumhits = s->total_hits;
847 for (i = 0; i < start; i++)
848 if (!reclist_read_record(s->reclist))
855 for (i = 0; i < *num; i++)
857 struct record_cluster *r = reclist_read_record(s->reclist);
868 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
869 yaz_timing_get_real(t), yaz_timing_get_user(t),
870 yaz_timing_get_sys(t));
871 yaz_timing_destroy(&t);
876 void statistics(struct session *se, struct statistics *stat)
881 memset(stat, 0, sizeof(*stat));
882 for (cl = se->clients; cl; cl = client_next_in_session(cl))
884 if (!client_get_connection(cl))
885 stat->num_no_connection++;
886 switch (client_get_state(cl))
888 case Client_Connecting: stat->num_connecting++; break;
889 case Client_Working: stat->num_working++; break;
890 case Client_Idle: stat->num_idle++; break;
891 case Client_Failed: stat->num_failed++; break;
892 case Client_Error: stat->num_error++; break;
897 stat->num_hits = se->total_hits;
898 stat->num_records = se->total_records;
900 stat->num_clients = count;
904 // Master list of connections we're handling events to
905 static IOCHAN channel_list = 0; /* thread pr */
907 void pazpar2_add_channel(IOCHAN chan)
909 chan->next = channel_list;
913 void pazpar2_event_loop()
915 event_loop(&channel_list);
918 static struct record_metadata *record_metadata_init(
919 NMEM nmem, char *value, enum conf_metadata_type type)
921 struct record_metadata *rec_md = record_metadata_create(nmem);
922 if (type == Metadata_type_generic)
925 p = normalize7bit_generic(p, " ,/.:([");
927 rec_md->data.text.disp = nmem_strdup(nmem, p);
928 rec_md->data.text.sort = 0;
930 else if (type == Metadata_type_year || type == Metadata_type_date)
935 if (type == Metadata_type_date)
937 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
940 rec_md->data.number.min = first;
941 rec_md->data.number.max = last;
948 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
949 struct conf_service *service, NMEM nmem)
951 char *mergekey_norm = 0;
952 xmlNode *root = xmlDocGetRootElement(doc);
953 WRBUF norm_wr = wrbuf_alloc();
956 /* consider mergekey from XSL first */
957 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
960 const char *norm_str;
961 pp2_relevance_token_t prt =
962 pp2_relevance_tokenize(
963 service->mergekey_pct,
964 (const char *) mergekey);
966 while ((norm_str = pp2_relevance_token_next(prt)))
970 if (wrbuf_len(norm_wr))
971 wrbuf_puts(norm_wr, " ");
972 wrbuf_puts(norm_wr, norm_str);
975 pp2_relevance_token_destroy(prt);
980 /* no mergekey defined in XSL. Look for mergekey metadata instead */
981 for (n = root->children; n; n = n->next)
983 if (n->type != XML_ELEMENT_NODE)
985 if (!strcmp((const char *) n->name, "metadata"))
987 struct conf_metadata *ser_md = 0;
988 int md_field_id = -1;
990 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
996 = conf_service_metadata_field_id(service,
997 (const char *) type);
998 if (md_field_id >= 0)
1000 ser_md = &service->metadata[md_field_id];
1001 if (ser_md->mergekey == Metadata_mergekey_yes)
1003 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1006 const char *norm_str;
1007 pp2_relevance_token_t prt =
1008 pp2_relevance_tokenize(
1009 service->mergekey_pct,
1010 (const char *) value);
1012 while ((norm_str = pp2_relevance_token_next(prt)))
1016 if (wrbuf_len(norm_wr))
1017 wrbuf_puts(norm_wr, " ");
1018 wrbuf_puts(norm_wr, norm_str);
1022 pp2_relevance_token_destroy(prt);
1031 /* generate unique key if none is not generated already or is empty */
1032 if (wrbuf_len(norm_wr) == 0)
1034 wrbuf_printf(norm_wr, "%s-%d",
1035 client_get_database(cl)->database->url, record_no);
1037 if (wrbuf_len(norm_wr) > 0)
1038 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1039 wrbuf_destroy(norm_wr);
1040 return mergekey_norm;
1043 /** \brief see if metadata for pz:recordfilter exists
1044 \param root xml root element of normalized record
1045 \param sdb session database for client
1046 \retval 0 if there is no metadata for pz:recordfilter
1047 \retval 1 if there is metadata for pz:recordfilter
1049 If there is no pz:recordfilter defined, this function returns 1
1053 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1058 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1063 for (n = root->children; n; n = n->next)
1065 if (n->type != XML_ELEMENT_NODE)
1067 if (!strcmp((const char *) n->name, "metadata"))
1069 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1073 const char *eq = strchr(s, '=');
1078 if (len == strlen((const char *)type) &&
1079 !memcmp((const char *) type, s, len))
1081 xmlChar *value = xmlNodeGetContent(n);
1082 if (value && *value)
1084 if (!eq || strstr((const char *) value, eq+1))
1097 /** \brief ingest XML record
1098 \param cl client holds the result set for record
1099 \param rec record buffer (0 terminated)
1100 \param record_no record position (1, 2, ..)
1101 \returns resulting record or NULL on failure
1103 struct record *ingest_record(struct client *cl, const char *rec,
1106 struct session_database *sdb = client_get_database(cl);
1107 struct session *se = client_get_session(cl);
1108 xmlDoc *xdoc = normalize_record(sdb, se, rec);
1110 struct record *record;
1111 struct record_cluster *cluster;
1112 const char *mergekey_norm;
1115 struct conf_service *service = se->service;
1120 root = xmlDocGetRootElement(xdoc);
1122 if (!check_record_filter(root, sdb))
1124 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1125 sdb->database->url);
1130 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1133 yaz_log(YLOG_WARN, "Got no mergekey");
1137 record = record_create(se->nmem,
1138 service->num_metadata, service->num_sortkeys, cl,
1141 cluster = reclist_insert(se->reclist,
1143 record, (char *) mergekey_norm,
1145 if (global_parameters.dump_records)
1146 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1147 sdb->database->url, record_no);
1150 /* no room for record */
1154 relevance_newrec(se->relevance, cluster);
1156 // now parsing XML record and adding data to cluster or record metadata
1157 for (n = root->children; n; n = n->next)
1159 pp2_relevance_token_t prt;
1166 if (n->type != XML_ELEMENT_NODE)
1168 if (!strcmp((const char *) n->name, "metadata"))
1170 struct conf_metadata *ser_md = 0;
1171 struct conf_sortkey *ser_sk = 0;
1172 struct record_metadata **wheretoput = 0;
1173 struct record_metadata *rec_md = 0;
1174 int md_field_id = -1;
1175 int sk_field_id = -1;
1177 type = xmlGetProp(n, (xmlChar *) "type");
1178 value = xmlNodeListGetString(xdoc, n->children, 1);
1180 if (!type || !value || !*value)
1184 = conf_service_metadata_field_id(service, (const char *) type);
1185 if (md_field_id < 0)
1187 if (se->number_of_warnings_unknown_metadata == 0)
1190 "Ignoring unknown metadata element: %s", type);
1192 se->number_of_warnings_unknown_metadata++;
1196 ser_md = &service->metadata[md_field_id];
1198 if (ser_md->sortkey_offset >= 0){
1199 sk_field_id = ser_md->sortkey_offset;
1200 ser_sk = &service->sortkeys[sk_field_id];
1203 // non-merged metadata
1204 rec_md = record_metadata_init(se->nmem, (char *) value,
1208 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1212 wheretoput = &record->metadata[md_field_id];
1214 wheretoput = &(*wheretoput)->next;
1215 *wheretoput = rec_md;
1218 rec_md = record_metadata_init(se->nmem, (char *) value,
1220 wheretoput = &cluster->metadata[md_field_id];
1222 // and polulate with data:
1223 // assign cluster or record based on merge action
1224 if (ser_md->merge == Metadata_merge_unique)
1226 struct record_metadata *mnode;
1227 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1228 if (!strcmp((const char *) mnode->data.text.disp,
1229 rec_md->data.text.disp))
1233 rec_md->next = *wheretoput;
1234 *wheretoput = rec_md;
1237 else if (ser_md->merge == Metadata_merge_longest)
1240 || strlen(rec_md->data.text.disp)
1241 > strlen((*wheretoput)->data.text.disp))
1243 *wheretoput = rec_md;
1246 const char *sort_str = 0;
1248 ser_sk->type == Metadata_sortkey_skiparticle;
1250 if (!cluster->sortkeys[sk_field_id])
1251 cluster->sortkeys[sk_field_id] =
1252 nmem_malloc(se->nmem,
1253 sizeof(union data_types));
1255 prt = pp2_relevance_tokenize(
1257 rec_md->data.text.disp);
1259 pp2_relevance_token_next(prt);
1261 sort_str = pp2_get_sort(prt, skip_article);
1263 cluster->sortkeys[sk_field_id]->text.disp =
1264 rec_md->data.text.disp;
1267 sort_str = rec_md->data.text.disp;
1269 "Could not make sortkey. Bug #1858");
1271 cluster->sortkeys[sk_field_id]->text.sort =
1272 nmem_strdup(se->nmem, sort_str);
1274 yaz_log(YLOG_LOG, "text disp=%s",
1275 cluster->sortkeys[sk_field_id]->text.disp);
1276 yaz_log(YLOG_LOG, "text sort=%s",
1277 cluster->sortkeys[sk_field_id]->text.sort);
1279 pp2_relevance_token_destroy(prt);
1283 else if (ser_md->merge == Metadata_merge_all)
1285 rec_md->next = *wheretoput;
1286 *wheretoput = rec_md;
1288 else if (ser_md->merge == Metadata_merge_range)
1292 *wheretoput = rec_md;
1294 cluster->sortkeys[sk_field_id]
1299 int this_min = rec_md->data.number.min;
1300 int this_max = rec_md->data.number.max;
1301 if (this_min < (*wheretoput)->data.number.min)
1302 (*wheretoput)->data.number.min = this_min;
1303 if (this_max > (*wheretoput)->data.number.max)
1304 (*wheretoput)->data.number.max = this_max;
1309 // ranking of _all_ fields enabled ...
1311 relevance_countwords(se->relevance, cluster,
1312 (char *) value, ser_md->rank);
1314 // construct facets ...
1315 if (ser_md->termlist)
1317 if (ser_md->type == Metadata_type_year)
1320 sprintf(year, "%d", rec_md->data.number.max);
1321 add_facet(se, (char *) type, year);
1322 if (rec_md->data.number.max != rec_md->data.number.min)
1324 sprintf(year, "%d", rec_md->data.number.min);
1325 add_facet(se, (char *) type, year);
1329 add_facet(se, (char *) type, (char *) value);
1339 if (se->number_of_warnings_unknown_elements == 0)
1341 "Unexpected element in internal record: %s", n->name);
1342 se->number_of_warnings_unknown_elements++;
1352 relevance_donerecord(se->relevance, cluster);
1353 se->total_records++;
1363 * c-file-style: "Stroustrup"
1364 * indent-tabs-mode: nil
1366 * vim: shiftwidth=4 tabstop=8 expandtab