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));
509 if ((eq = strchr(values[i], '=')))
510 new->type = PAZPAR2_STRING_MATCH;
511 if ((eq = strchr(values[i], '~')))
512 new->type = PAZPAR2_SUBSTRING_MATCH;
515 yaz_log(YLOG_WARN, "Missing equal-signi/tilde in filter");
519 new->name = values[i];
520 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
522 for (subi = 0; subi < subnum; subi++)
524 struct database_criterion_value *newv
525 = nmem_malloc(m, sizeof(*newv));
526 newv->value = subvalues[subi];
527 newv->next = new->values;
536 enum pazpar2_error_code search(struct session *se,
537 const char *query, const char *filter,
538 const char **addinfo)
540 int live_channels = 0;
544 struct database_criterion *criteria;
546 yaz_log(YLOG_DEBUG, "Search");
549 nmem_reset(se->nmem);
551 se->total_records = se->total_hits = se->total_merged = 0;
553 se->num_termlists = 0;
554 criteria = parse_filter(se->nmem, filter);
555 live_channels = select_targets(se, criteria);
558 int maxrecs = live_channels * global_parameters.toget; // This is buggy!!!
559 se->reclist = reclist_create(se->nmem, maxrecs);
560 se->expected_maxrecs = maxrecs;
563 return PAZPAR2_NO_TARGETS;
565 for (cl = se->clients; cl; cl = client_next_in_session(cl))
567 if (prepare_session_database(se, client_get_database(cl)) < 0)
569 *addinfo = client_get_database(cl)->database->url;
570 return PAZPAR2_CONFIG_TARGET;
572 // Parse query for target
573 if (client_parse_query(cl, query) < 0)
578 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
579 se->service->z3950_session_timeout))
580 client_start_search(cl);
584 // If no queries could be mapped, we signal an error
588 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
590 return PAZPAR2_NO_ERROR;
593 // Creates a new session_database object for a database
594 static void session_init_databases_fun(void *context, struct database *db)
596 struct session *se = (struct session *) context;
597 struct conf_service *service = se->service;
598 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
599 int num = settings_num(service);
606 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
607 memset(new->settings, 0, sizeof(struct settings*) * num);
611 for (i = 0; i < num; i++)
612 new->settings[i] = db->settings[i];
614 new->next = se->databases;
618 // Doesn't free memory associated with sdb -- nmem takes care of that
619 static void session_database_destroy(struct session_database *sdb)
621 struct database_retrievalmap *m;
623 for (m = sdb->map; m; m = m->next)
624 xsltFreeStylesheet(m->stylesheet);
627 // Initialize session_database list -- this represents this session's view
628 // of the database list -- subject to modification by the settings ws command
629 void session_init_databases(struct session *se)
632 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
635 // Probably session_init_databases_fun should be refactored instead of
637 static struct session_database *load_session_database(struct session *se,
640 struct database *db = find_database(id, 0, se->service);
642 resolve_database(db);
644 session_init_databases_fun((void*) se, db);
645 // New sdb is head of se->databases list
646 return se->databases;
649 // Find an existing session database. If not found, load it
650 static struct session_database *find_session_database(struct session *se,
653 struct session_database *sdb;
655 for (sdb = se->databases; sdb; sdb = sdb->next)
656 if (!strcmp(sdb->database->url, id))
658 return load_session_database(se, id);
661 // Apply a session override to a database
662 void session_apply_setting(struct session *se, char *dbname, char *setting,
665 struct session_database *sdb = find_session_database(se, dbname);
666 struct conf_service *service = se->service;
667 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
668 int offset = settings_offset_cprefix(service, setting);
672 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
675 // Jakub: This breaks the filter setting.
676 /*if (offset == PZ_ID)
678 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
682 new->target = dbname;
685 new->next = sdb->settings[offset];
686 sdb->settings[offset] = new;
688 // Force later recompute of settings-driven data structures
689 // (happens when a search starts and client connections are prepared)
695 struct database_retrievalmap *m;
696 // We don't worry about the map structure -- it's in nmem
697 for (m = sdb->map; m; m = m->next)
698 xsltFreeStylesheet(m->stylesheet);
705 void destroy_session(struct session *s)
707 struct session_database *sdb;
710 client_destroy(s->clients);
711 for (sdb = s->databases; sdb; sdb = sdb->next)
712 session_database_destroy(sdb);
713 nmem_destroy(s->nmem);
714 service_destroy(s->service);
715 wrbuf_destroy(s->wrbuf);
718 struct session *new_session(NMEM nmem, struct conf_service *service)
721 struct session *session = nmem_malloc(nmem, sizeof(*session));
723 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
725 session->service = service;
726 session->relevance = 0;
727 session->total_hits = 0;
728 session->total_records = 0;
729 session->number_of_warnings_unknown_elements = 0;
730 session->number_of_warnings_unknown_metadata = 0;
731 session->num_termlists = 0;
732 session->reclist = 0;
733 session->clients = 0;
734 session->expected_maxrecs = 0;
735 session->session_nmem = nmem;
736 session->nmem = nmem_create();
737 session->wrbuf = wrbuf_alloc();
738 session->databases = 0;
739 for (i = 0; i <= SESSION_WATCH_MAX; i++)
741 session->watchlist[i].data = 0;
742 session->watchlist[i].fun = 0;
747 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
749 struct hitsbytarget *res = 0;
753 for (cl = se->clients; cl; cl = client_next_in_session(cl))
756 res = nmem_malloc(nmem, sizeof(*res) * sz);
758 for (cl = se->clients; cl; cl = client_next_in_session(cl))
760 const char *name = session_setting_oneval(client_get_database(cl),
763 res[*count].id = client_get_database(cl)->database->url;
764 res[*count].name = *name ? name : "Unknown";
765 res[*count].hits = client_get_hits(cl);
766 res[*count].records = client_get_num_records(cl);
767 res[*count].diagnostic = client_get_diagnostic(cl);
768 res[*count].state = client_get_state_str(cl);
769 res[*count].connected = client_get_connection(cl) ? 1 : 0;
775 struct termlist_score **termlist(struct session *s, const char *name, int *num)
779 for (i = 0; i < s->num_termlists; i++)
780 if (!strcmp((const char *) s->termlists[i].name, name))
781 return termlist_highscore(s->termlists[i].termlist, num);
785 #ifdef MISSING_HEADERS
786 void report_nmem_stats(void)
788 size_t in_use, is_free;
790 nmem_get_memory_in_use(&in_use);
791 nmem_get_memory_free(&is_free);
793 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
794 (long) in_use, (long) is_free);
798 struct record_cluster *show_single(struct session *s, const char *id,
799 struct record_cluster **prev_r,
800 struct record_cluster **next_r)
802 struct record_cluster *r;
804 reclist_rewind(s->reclist);
807 while ((r = reclist_read_record(s->reclist)))
809 if (!strcmp(r->recid, id))
811 *next_r = reclist_read_record(s->reclist);
819 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
820 int start, int *num, int *total, int *sumhits,
823 struct record_cluster **recs = nmem_malloc(nmem_show, *num
824 * sizeof(struct record_cluster *));
825 struct reclist_sortparms *spp;
828 yaz_timing_t t = yaz_timing_create();
840 for (spp = sp; spp; spp = spp->next)
841 if (spp->type == Metadata_sortkey_relevance)
843 relevance_prepare_read(s->relevance, s->reclist);
846 reclist_sort(s->reclist, sp);
848 *total = s->reclist->num_records;
849 *sumhits = s->total_hits;
851 for (i = 0; i < start; i++)
852 if (!reclist_read_record(s->reclist))
859 for (i = 0; i < *num; i++)
861 struct record_cluster *r = reclist_read_record(s->reclist);
872 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
873 yaz_timing_get_real(t), yaz_timing_get_user(t),
874 yaz_timing_get_sys(t));
875 yaz_timing_destroy(&t);
880 void statistics(struct session *se, struct statistics *stat)
885 memset(stat, 0, sizeof(*stat));
886 for (cl = se->clients; cl; cl = client_next_in_session(cl))
888 if (!client_get_connection(cl))
889 stat->num_no_connection++;
890 switch (client_get_state(cl))
892 case Client_Connecting: stat->num_connecting++; break;
893 case Client_Working: stat->num_working++; break;
894 case Client_Idle: stat->num_idle++; break;
895 case Client_Failed: stat->num_failed++; break;
896 case Client_Error: stat->num_error++; break;
901 stat->num_hits = se->total_hits;
902 stat->num_records = se->total_records;
904 stat->num_clients = count;
908 // Master list of connections we're handling events to
909 static IOCHAN channel_list = 0; /* thread pr */
911 void pazpar2_add_channel(IOCHAN chan)
913 chan->next = channel_list;
917 void pazpar2_event_loop()
919 event_loop(&channel_list);
922 static struct record_metadata *record_metadata_init(
923 NMEM nmem, char *value, enum conf_metadata_type type)
925 struct record_metadata *rec_md = record_metadata_create(nmem);
926 if (type == Metadata_type_generic)
929 p = normalize7bit_generic(p, " ,/.:([");
931 rec_md->data.text.disp = nmem_strdup(nmem, p);
932 rec_md->data.text.sort = 0;
934 else if (type == Metadata_type_year || type == Metadata_type_date)
939 if (type == Metadata_type_date)
941 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
944 rec_md->data.number.min = first;
945 rec_md->data.number.max = last;
952 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
953 struct conf_service *service, NMEM nmem)
955 char *mergekey_norm = 0;
956 xmlNode *root = xmlDocGetRootElement(doc);
957 WRBUF norm_wr = wrbuf_alloc();
960 /* consider mergekey from XSL first */
961 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
964 const char *norm_str;
965 pp2_relevance_token_t prt =
966 pp2_relevance_tokenize(
967 service->mergekey_pct,
968 (const char *) mergekey);
970 while ((norm_str = pp2_relevance_token_next(prt)))
974 if (wrbuf_len(norm_wr))
975 wrbuf_puts(norm_wr, " ");
976 wrbuf_puts(norm_wr, norm_str);
979 pp2_relevance_token_destroy(prt);
984 /* no mergekey defined in XSL. Look for mergekey metadata instead */
985 for (n = root->children; n; n = n->next)
987 if (n->type != XML_ELEMENT_NODE)
989 if (!strcmp((const char *) n->name, "metadata"))
991 struct conf_metadata *ser_md = 0;
992 int md_field_id = -1;
994 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1000 = conf_service_metadata_field_id(service,
1001 (const char *) type);
1002 if (md_field_id >= 0)
1004 ser_md = &service->metadata[md_field_id];
1005 if (ser_md->mergekey == Metadata_mergekey_yes)
1007 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1010 const char *norm_str;
1011 pp2_relevance_token_t prt =
1012 pp2_relevance_tokenize(
1013 service->mergekey_pct,
1014 (const char *) value);
1016 while ((norm_str = pp2_relevance_token_next(prt)))
1020 if (wrbuf_len(norm_wr))
1021 wrbuf_puts(norm_wr, " ");
1022 wrbuf_puts(norm_wr, norm_str);
1026 pp2_relevance_token_destroy(prt);
1035 /* generate unique key if none is not generated already or is empty */
1036 if (wrbuf_len(norm_wr) == 0)
1038 wrbuf_printf(norm_wr, "%s-%d",
1039 client_get_database(cl)->database->url, record_no);
1041 if (wrbuf_len(norm_wr) > 0)
1042 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1043 wrbuf_destroy(norm_wr);
1044 return mergekey_norm;
1047 /** \brief see if metadata for pz:recordfilter exists
1048 \param root xml root element of normalized record
1049 \param sdb session database for client
1050 \retval 0 if there is no metadata for pz:recordfilter
1051 \retval 1 if there is metadata for pz:recordfilter
1053 If there is no pz:recordfilter defined, this function returns 1
1057 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1062 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1067 for (n = root->children; n; n = n->next)
1069 if (n->type != XML_ELEMENT_NODE)
1071 if (!strcmp((const char *) n->name, "metadata"))
1073 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1077 const char *eq = strchr(s, '=');
1082 if (len == strlen((const char *)type) &&
1083 !memcmp((const char *) type, s, len))
1085 xmlChar *value = xmlNodeGetContent(n);
1086 if (value && *value)
1088 if (!eq || strstr((const char *) value, eq+1))
1101 /** \brief ingest XML record
1102 \param cl client holds the result set for record
1103 \param rec record buffer (0 terminated)
1104 \param record_no record position (1, 2, ..)
1105 \returns resulting record or NULL on failure
1107 struct record *ingest_record(struct client *cl, const char *rec,
1110 struct session_database *sdb = client_get_database(cl);
1111 struct session *se = client_get_session(cl);
1112 xmlDoc *xdoc = normalize_record(sdb, se, rec);
1114 struct record *record;
1115 struct record_cluster *cluster;
1116 const char *mergekey_norm;
1119 struct conf_service *service = se->service;
1124 root = xmlDocGetRootElement(xdoc);
1126 if (!check_record_filter(root, sdb))
1128 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1129 sdb->database->url);
1134 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1137 yaz_log(YLOG_WARN, "Got no mergekey");
1141 record = record_create(se->nmem,
1142 service->num_metadata, service->num_sortkeys, cl,
1145 cluster = reclist_insert(se->reclist,
1147 record, (char *) mergekey_norm,
1149 if (global_parameters.dump_records)
1150 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1151 sdb->database->url, record_no);
1154 /* no room for record */
1158 relevance_newrec(se->relevance, cluster);
1160 // now parsing XML record and adding data to cluster or record metadata
1161 for (n = root->children; n; n = n->next)
1163 pp2_relevance_token_t prt;
1170 if (n->type != XML_ELEMENT_NODE)
1172 if (!strcmp((const char *) n->name, "metadata"))
1174 struct conf_metadata *ser_md = 0;
1175 struct conf_sortkey *ser_sk = 0;
1176 struct record_metadata **wheretoput = 0;
1177 struct record_metadata *rec_md = 0;
1178 int md_field_id = -1;
1179 int sk_field_id = -1;
1181 type = xmlGetProp(n, (xmlChar *) "type");
1182 value = xmlNodeListGetString(xdoc, n->children, 1);
1184 if (!type || !value || !*value)
1188 = conf_service_metadata_field_id(service, (const char *) type);
1189 if (md_field_id < 0)
1191 if (se->number_of_warnings_unknown_metadata == 0)
1194 "Ignoring unknown metadata element: %s", type);
1196 se->number_of_warnings_unknown_metadata++;
1200 ser_md = &service->metadata[md_field_id];
1202 if (ser_md->sortkey_offset >= 0){
1203 sk_field_id = ser_md->sortkey_offset;
1204 ser_sk = &service->sortkeys[sk_field_id];
1207 // non-merged metadata
1208 rec_md = record_metadata_init(se->nmem, (char *) value,
1212 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1216 wheretoput = &record->metadata[md_field_id];
1218 wheretoput = &(*wheretoput)->next;
1219 *wheretoput = rec_md;
1222 rec_md = record_metadata_init(se->nmem, (char *) value,
1224 wheretoput = &cluster->metadata[md_field_id];
1226 // and polulate with data:
1227 // assign cluster or record based on merge action
1228 if (ser_md->merge == Metadata_merge_unique)
1230 struct record_metadata *mnode;
1231 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1232 if (!strcmp((const char *) mnode->data.text.disp,
1233 rec_md->data.text.disp))
1237 rec_md->next = *wheretoput;
1238 *wheretoput = rec_md;
1241 else if (ser_md->merge == Metadata_merge_longest)
1244 || strlen(rec_md->data.text.disp)
1245 > strlen((*wheretoput)->data.text.disp))
1247 *wheretoput = rec_md;
1250 const char *sort_str = 0;
1252 ser_sk->type == Metadata_sortkey_skiparticle;
1254 if (!cluster->sortkeys[sk_field_id])
1255 cluster->sortkeys[sk_field_id] =
1256 nmem_malloc(se->nmem,
1257 sizeof(union data_types));
1259 prt = pp2_relevance_tokenize(
1261 rec_md->data.text.disp);
1263 pp2_relevance_token_next(prt);
1265 sort_str = pp2_get_sort(prt, skip_article);
1267 cluster->sortkeys[sk_field_id]->text.disp =
1268 rec_md->data.text.disp;
1271 sort_str = rec_md->data.text.disp;
1273 "Could not make sortkey. Bug #1858");
1275 cluster->sortkeys[sk_field_id]->text.sort =
1276 nmem_strdup(se->nmem, sort_str);
1278 yaz_log(YLOG_LOG, "text disp=%s",
1279 cluster->sortkeys[sk_field_id]->text.disp);
1280 yaz_log(YLOG_LOG, "text sort=%s",
1281 cluster->sortkeys[sk_field_id]->text.sort);
1283 pp2_relevance_token_destroy(prt);
1287 else if (ser_md->merge == Metadata_merge_all)
1289 rec_md->next = *wheretoput;
1290 *wheretoput = rec_md;
1292 else if (ser_md->merge == Metadata_merge_range)
1296 *wheretoput = rec_md;
1298 cluster->sortkeys[sk_field_id]
1303 int this_min = rec_md->data.number.min;
1304 int this_max = rec_md->data.number.max;
1305 if (this_min < (*wheretoput)->data.number.min)
1306 (*wheretoput)->data.number.min = this_min;
1307 if (this_max > (*wheretoput)->data.number.max)
1308 (*wheretoput)->data.number.max = this_max;
1313 // ranking of _all_ fields enabled ...
1315 relevance_countwords(se->relevance, cluster,
1316 (char *) value, ser_md->rank);
1318 // construct facets ...
1319 if (ser_md->termlist)
1321 if (ser_md->type == Metadata_type_year)
1324 sprintf(year, "%d", rec_md->data.number.max);
1325 add_facet(se, (char *) type, year);
1326 if (rec_md->data.number.max != rec_md->data.number.min)
1328 sprintf(year, "%d", rec_md->data.number.min);
1329 add_facet(se, (char *) type, year);
1333 add_facet(se, (char *) type, (char *) value);
1343 if (se->number_of_warnings_unknown_elements == 0)
1345 "Unexpected element in internal record: %s", n->name);
1346 se->number_of_warnings_unknown_elements++;
1356 relevance_donerecord(se->relevance, cluster);
1357 se->total_records++;
1367 * c-file-style: "Stroustrup"
1368 * indent-tabs-mode: nil
1370 * vim: shiftwidth=4 tabstop=8 expandtab