1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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
38 #include <sys/socket.h>
47 #include <yaz/marcdisp.h>
48 #include <yaz/comstack.h>
49 #include <yaz/tcpip.h>
50 #include <yaz/proto.h>
51 #include <yaz/readconf.h>
52 #include <yaz/pquery.h>
53 #include <yaz/otherinfo.h>
54 #include <yaz/yaz-util.h>
56 #include <yaz/query-charset.h>
57 #include <yaz/querytowrbuf.h>
58 #include <yaz/oid_db.h>
59 #include <yaz/snprintf.h>
63 #include <yaz/timing.h>
67 #include <netinet/in.h>
73 #include "termlists.h"
75 #include "relevance.h"
79 #include "normalize7bit.h"
81 #define TERMLIST_HIGH_SCORE 25
85 // Note: Some things in this structure will eventually move to configuration
86 struct parameters global_parameters =
94 30, // operations timeout
98 60, // session timeout
103 180, // Z39.50 session timeout
104 15 // Connect timeout
107 // Recursively traverse query structure to extract terms.
108 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
120 pull_terms(nmem, n->u.p[0], termlist, num);
121 pull_terms(nmem, n->u.p[1], termlist, num);
124 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
125 for (i = 0; i < numwords; i++)
126 termlist[(*num)++] = words[i];
135 static void add_facet(struct session *s, const char *type, const char *value)
141 for (i = 0; i < s->num_termlists; i++)
142 if (!strcmp(s->termlists[i].name, type))
144 if (i == s->num_termlists)
146 if (i == SESSION_MAX_TERMLISTS)
148 yaz_log(YLOG_FATAL, "Too many termlists");
152 s->termlists[i].name = nmem_strdup(s->nmem, type);
153 s->termlists[i].termlist
154 = termlist_create(s->nmem, s->expected_maxrecs,
155 TERMLIST_HIGH_SCORE);
156 s->num_termlists = i + 1;
158 termlist_insert(s->termlists[i].termlist, value);
161 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
163 struct database *db = sdb->database;
165 const Odr_oid *oid = rec->direct_reference;
167 /* convert response record to XML somehow */
168 if (rec->which == Z_External_octet && oid
169 && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
172 rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
173 rec->u.octet_aligned->len);
176 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
181 else if (rec->which == Z_External_OPAC)
185 yaz_log(YLOG_WARN, "MARC decoding not configured");
190 /* OPAC gets converted to XML too */
191 WRBUF wrbuf_opac = wrbuf_alloc();
192 /* MARCXML inside the OPAC XML. Charset is in effect because we
193 use the yaz_marc handle */
194 yaz_marc_xml(sdb->yaz_marc, YAZ_MARC_MARCXML);
195 yaz_opac_decode_wrbuf(sdb->yaz_marc, rec->u.opac, wrbuf_opac);
197 rdoc = xmlParseMemory((char*) wrbuf_buf(wrbuf_opac),
198 wrbuf_len(wrbuf_opac));
201 yaz_log(YLOG_WARN, "Unable to parse OPAC XML");
202 /* Was used to debug bug #1348 */
204 FILE *f = fopen("/tmp/opac.xml.txt", "wb");
207 fwrite(wrbuf_buf(wrbuf_opac), 1, wrbuf_len(wrbuf_opac), f);
212 wrbuf_destroy(wrbuf_opac);
215 else if (oid && yaz_oid_is_iso2709(oid))
217 /* ISO2709 gets converted to MARCXML */
220 yaz_log(YLOG_WARN, "MARC decoding not configured");
229 if (rec->which != Z_External_octet)
231 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
235 buf = (char*) rec->u.octet_aligned->buf;
236 len = rec->u.octet_aligned->len;
237 if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
239 yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
243 if (yaz_marc_write_xml(sdb->yaz_marc, &res,
244 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
246 yaz_log(YLOG_WARN, "Failed to encode as XML %s",
250 rdoc = xmlNewDoc((xmlChar *) "1.0");
251 xmlDocSetRootElement(rdoc, res);
256 char oid_name_buf[OID_STR_MAX];
257 const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
259 "Unable to handle record of type %s from %s",
264 if (global_parameters.dump_records)
266 FILE *lf = yaz_log_file();
269 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
270 #if LIBXML_VERSION >= 20600
271 xmlDocFormatDump(lf, rdoc, 1);
273 xmlDocDump(lf, rdoc);
281 #define MAX_XSLT_ARGS 16
283 // Add static values from session database settings if applicable
284 static void insert_settings_parameters(struct session_database *sdb,
285 struct session *se, char **parms)
287 struct conf_service *service = global_parameters.server->service;
292 for (i = 0; i < service->num_metadata; i++)
294 struct conf_metadata *md = &service->metadata[i];
297 if (md->setting == Metadata_setting_parameter &&
298 (setting = settings_offset(md->name)) > 0)
300 const char *val = session_setting_oneval(sdb, setting);
301 if (val && nparms < MAX_XSLT_ARGS)
304 int len = strlen(val);
305 buf = nmem_malloc(se->nmem, len + 3);
307 strcpy(buf + 1, val);
310 parms[offset++] = md->name;
311 parms[offset++] = buf;
319 // Add static values from session database settings if applicable
320 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc)
322 struct conf_service *service = global_parameters.server->service;
325 for (i = 0; i < service->num_metadata; i++)
327 struct conf_metadata *md = &service->metadata[i];
330 if (md->setting == Metadata_setting_postproc &&
331 (offset = settings_offset(md->name)) > 0)
333 const char *val = session_setting_oneval(sdb, offset);
336 xmlNode *r = xmlDocGetRootElement(doc);
337 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
339 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
345 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
348 struct database_retrievalmap *m;
349 xmlDoc *rdoc = record_to_xml(sdb, rec);
352 for (m = sdb->map; m; m = m->next)
358 char *parms[MAX_XSLT_ARGS*2+1];
360 insert_settings_parameters(sdb, se, parms);
362 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
363 root= xmlDocGetRootElement(new);
364 if (!new || !root || !(root->children))
366 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
378 insert_settings_values(sdb, rdoc);
380 if (global_parameters.dump_records)
382 FILE *lf = yaz_log_file();
386 yaz_log(YLOG_LOG, "Normalized record from %s",
388 #if LIBXML_VERSION >= 20600
389 xmlDocFormatDump(lf, rdoc, 1);
391 xmlDocDump(lf, rdoc);
400 // Retrieve first defined value for 'name' for given database.
401 // Will be extended to take into account user associated with session
402 const char *session_setting_oneval(struct session_database *db, int offset)
404 if (!db->settings[offset])
406 return db->settings[offset]->value;
411 // Initialize YAZ Map structures for MARC-based targets
412 static int prepare_yazmarc(struct session_database *sdb)
418 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
421 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX))
422 && !strncmp(s, "iso2709", 7))
424 char *encoding = "marc-8s", *e;
427 // See if a native encoding is specified
428 if ((e = strchr(s, ';')))
431 sdb->yaz_marc = yaz_marc_create();
432 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
434 cm = yaz_iconv_open("utf-8", encoding);
438 "Unable to map from %s to UTF-8 for target %s",
439 encoding, sdb->database->url);
442 yaz_marc_iconv(sdb->yaz_marc, cm);
447 // Prepare XSLT stylesheets for record normalization
448 // Structures are allocated on the session_wide nmem to avoid having
449 // to recompute this for every search. This would lead
450 // to leaking if a single session was to repeatedly change the PZ_XSLT
451 // setting. However, this is not a realistic use scenario.
452 static int prepare_map(struct session *se, struct session_database *sdb)
458 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
461 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
464 struct database_retrievalmap **m = &sdb->map;
466 char auto_stylesheet[256];
468 if (!strcmp(s, "auto"))
470 const char *request_syntax = session_setting_oneval(
471 sdb, PZ_REQUESTSYNTAX);
475 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
476 "%s.xsl", request_syntax);
477 for (cp = auto_stylesheet; *cp; cp++)
479 /* deliberately only consider ASCII */
480 if (*cp > 32 && *cp < 127)
487 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
490 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
491 for (i = 0; i < num; i++)
493 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
495 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
497 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
505 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
510 // This analyzes settings and recomputes any supporting data structures
512 static int prepare_session_database(struct session *se,
513 struct session_database *sdb)
518 "No settings associated with %s", sdb->database->url);
521 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
523 if (prepare_yazmarc(sdb) < 0)
526 if (sdb->settings[PZ_XSLT] && !sdb->map)
528 if (prepare_map(se, sdb) < 0)
534 // called if watch should be removed because http_channel is to be destroyed
535 static void session_watch_cancel(void *data, struct http_channel *c,
538 struct session_watchentry *ent = data;
545 // set watch. Returns 0=OK, -1 if watch is already set
546 int session_set_watch(struct session *s, int what,
547 session_watchfun fun, void *data,
548 struct http_channel *chan)
550 if (s->watchlist[what].fun)
552 s->watchlist[what].fun = fun;
553 s->watchlist[what].data = data;
554 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
555 session_watch_cancel);
559 void session_alert_watch(struct session *s, int what)
561 if (s->watchlist[what].fun)
563 /* our watch is no longer associated with http_channel */
565 session_watchfun fun;
567 http_remove_observer(s->watchlist[what].obs);
568 fun = s->watchlist[what].fun;
569 data = s->watchlist[what].data;
571 /* reset watch before fun is invoked - in case fun wants to set
573 s->watchlist[what].fun = 0;
574 s->watchlist[what].data = 0;
575 s->watchlist[what].obs = 0;
581 //callback for grep_databases
582 static void select_targets_callback(void *context, struct session_database *db)
584 struct session *se = (struct session*) context;
585 struct client *cl = client_create();
586 client_set_database(cl, db);
587 client_set_session(cl, se);
590 // Associates a set of clients with a session;
591 // Note: Session-databases represent databases with per-session
593 int select_targets(struct session *se, struct database_criterion *crit)
596 client_destroy(se->clients);
598 return session_grep_databases(se, crit, select_targets_callback);
601 int session_active_clients(struct session *s)
606 for (c = s->clients; c; c = client_next_in_session(c))
607 if (client_is_active(c))
613 // parses crit1=val1,crit2=val2|val3,...
614 static struct database_criterion *parse_filter(NMEM m, const char *buf)
616 struct database_criterion *res = 0;
623 nmem_strsplit(m, ",", buf, &values, &num);
624 for (i = 0; i < num; i++)
629 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
630 char *eq = strchr(values[i], '=');
633 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
637 new->name = values[i];
638 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
640 for (subi = 0; subi < subnum; subi++)
642 struct database_criterion_value *newv
643 = nmem_malloc(m, sizeof(*newv));
644 newv->value = subvalues[subi];
645 newv->next = new->values;
654 enum pazpar2_error_code search(struct session *se,
655 char *query, char *filter,
656 const char **addinfo)
658 int live_channels = 0;
662 struct database_criterion *criteria;
664 yaz_log(YLOG_DEBUG, "Search");
667 nmem_reset(se->nmem);
669 se->total_records = se->total_hits = se->total_merged = 0;
671 se->num_termlists = 0;
672 criteria = parse_filter(se->nmem, filter);
674 live_channels = select_targets(se, criteria);
677 int maxrecs = live_channels * global_parameters.toget;
678 se->reclist = reclist_create(se->nmem, maxrecs);
679 se->expected_maxrecs = maxrecs;
682 return PAZPAR2_NO_TARGETS;
684 for (cl = se->clients; cl; cl = client_next_in_session(cl))
686 if (prepare_session_database(se, client_get_database(cl)) < 0)
688 *addinfo = client_get_database(cl)->database->url;
689 return PAZPAR2_CONFIG_TARGET;
691 // Parse query for target
692 if (client_parse_query(cl, query) < 0)
697 client_prep_connection(cl);
701 // If no queries could be mapped, we signal an error
705 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
707 return PAZPAR2_NO_ERROR;
710 // Creates a new session_database object for a database
711 static void session_init_databases_fun(void *context, struct database *db)
713 struct session *se = (struct session *) context;
714 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
715 int num = settings_num();
723 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
724 memset(new->settings, 0, sizeof(struct settings*) * num);
728 for (i = 0; i < num; i++)
729 new->settings[i] = db->settings[i];
731 new->next = se->databases;
735 // Doesn't free memory associated with sdb -- nmem takes care of that
736 static void session_database_destroy(struct session_database *sdb)
738 struct database_retrievalmap *m;
740 for (m = sdb->map; m; m = m->next)
741 xsltFreeStylesheet(m->stylesheet);
743 yaz_marc_destroy(sdb->yaz_marc);
746 // Initialize session_database list -- this represents this session's view
747 // of the database list -- subject to modification by the settings ws command
748 void session_init_databases(struct session *se)
751 predef_grep_databases(se, 0, session_init_databases_fun);
754 // Probably session_init_databases_fun should be refactored instead of
756 static struct session_database *load_session_database(struct session *se,
759 struct database *db = find_database(id, 0);
761 session_init_databases_fun((void*) se, db);
762 // New sdb is head of se->databases list
763 return se->databases;
766 // Find an existing session database. If not found, load it
767 static struct session_database *find_session_database(struct session *se,
770 struct session_database *sdb;
772 for (sdb = se->databases; sdb; sdb = sdb->next)
773 if (!strcmp(sdb->database->url, id))
775 return load_session_database(se, id);
778 // Apply a session override to a database
779 void session_apply_setting(struct session *se, char *dbname, char *setting,
782 struct session_database *sdb = find_session_database(se, dbname);
783 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
784 int offset = settings_offset_cprefix(setting);
788 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
791 // Jakub: This breaks the filter setting.
792 /*if (offset == PZ_ID)
794 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
798 new->target = dbname;
801 new->next = sdb->settings[offset];
802 sdb->settings[offset] = new;
804 // Force later recompute of settings-driven data structures
805 // (happens when a search starts and client connections are prepared)
808 case PZ_NATIVESYNTAX:
811 yaz_marc_destroy(sdb->yaz_marc);
818 struct database_retrievalmap *m;
819 // We don't worry about the map structure -- it's in nmem
820 for (m = sdb->map; m; m = m->next)
821 xsltFreeStylesheet(m->stylesheet);
828 void destroy_session(struct session *s)
830 struct session_database *sdb;
833 client_destroy(s->clients);
834 for (sdb = s->databases; sdb; sdb = sdb->next)
835 session_database_destroy(sdb);
836 nmem_destroy(s->nmem);
837 wrbuf_destroy(s->wrbuf);
840 struct session *new_session(NMEM nmem)
843 struct session *session = nmem_malloc(nmem, sizeof(*session));
845 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
847 session->relevance = 0;
848 session->total_hits = 0;
849 session->total_records = 0;
850 session->num_termlists = 0;
851 session->reclist = 0;
852 session->requestid = -1;
853 session->clients = 0;
854 session->expected_maxrecs = 0;
855 session->session_nmem = nmem;
856 session->nmem = nmem_create();
857 session->wrbuf = wrbuf_alloc();
858 session->databases = 0;
859 for (i = 0; i <= SESSION_WATCH_MAX; i++)
861 session->watchlist[i].data = 0;
862 session->watchlist[i].fun = 0;
867 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
869 struct hitsbytarget *res = 0;
873 for (cl = se->clients; cl; cl = client_next_in_session(cl))
876 res = nmem_malloc(nmem, sizeof(*res) * sz);
878 for (cl = se->clients; cl; cl = client_next_in_session(cl))
880 const char *name = session_setting_oneval(client_get_database(cl),
883 res[*count].id = client_get_database(cl)->database->url;
884 res[*count].name = *name ? name : "Unknown";
885 res[*count].hits = client_get_hits(cl);
886 res[*count].records = client_get_num_records(cl);
887 res[*count].diagnostic = client_get_diagnostic(cl);
888 res[*count].state = client_get_state_str(cl);
889 res[*count].connected = client_get_connection(cl) ? 1 : 0;
895 struct termlist_score **termlist(struct session *s, const char *name, int *num)
899 for (i = 0; i < s->num_termlists; i++)
900 if (!strcmp((const char *) s->termlists[i].name, name))
901 return termlist_highscore(s->termlists[i].termlist, num);
905 #ifdef MISSING_HEADERS
906 void report_nmem_stats(void)
908 size_t in_use, is_free;
910 nmem_get_memory_in_use(&in_use);
911 nmem_get_memory_free(&is_free);
913 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
914 (long) in_use, (long) is_free);
918 struct record_cluster *show_single(struct session *s, const char *id)
920 struct record_cluster *r;
922 reclist_rewind(s->reclist);
923 while ((r = reclist_read_record(s->reclist)))
924 if (!strcmp(r->recid, id))
929 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
930 int start, int *num, int *total, int *sumhits,
933 struct record_cluster **recs = nmem_malloc(nmem_show, *num
934 * sizeof(struct record_cluster *));
935 struct reclist_sortparms *spp;
938 yaz_timing_t t = yaz_timing_create();
950 for (spp = sp; spp; spp = spp->next)
951 if (spp->type == Metadata_sortkey_relevance)
953 relevance_prepare_read(s->relevance, s->reclist);
956 reclist_sort(s->reclist, sp);
958 *total = s->reclist->num_records;
959 *sumhits = s->total_hits;
961 for (i = 0; i < start; i++)
962 if (!reclist_read_record(s->reclist))
969 for (i = 0; i < *num; i++)
971 struct record_cluster *r = reclist_read_record(s->reclist);
982 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
983 yaz_timing_get_real(t), yaz_timing_get_user(t),
984 yaz_timing_get_sys(t));
985 yaz_timing_destroy(&t);
990 void statistics(struct session *se, struct statistics *stat)
995 memset(stat, 0, sizeof(*stat));
996 for (cl = se->clients; cl; cl = client_next_in_session(cl))
998 if (!client_get_connection(cl))
999 stat->num_no_connection++;
1000 switch (client_get_state(cl))
1002 case Client_Connecting: stat->num_connecting++; break;
1003 case Client_Initializing: stat->num_initializing++; break;
1004 case Client_Searching: stat->num_searching++; break;
1005 case Client_Presenting: stat->num_presenting++; break;
1006 case Client_Idle: stat->num_idle++; break;
1007 case Client_Failed: stat->num_failed++; break;
1008 case Client_Error: stat->num_error++; break;
1013 stat->num_hits = se->total_hits;
1014 stat->num_records = se->total_records;
1016 stat->num_clients = count;
1019 int start_http_listener(void)
1022 struct conf_server *ser = global_parameters.server;
1024 if (*global_parameters.listener_override)
1025 strcpy(hp, global_parameters.listener_override);
1028 strcpy(hp, ser->host ? ser->host : "");
1033 sprintf(hp + strlen(hp), "%d", ser->port);
1036 return http_init(hp);
1039 void start_proxy(void)
1042 struct conf_server *ser = global_parameters.server;
1044 if (*global_parameters.proxy_override)
1045 strcpy(hp, global_parameters.proxy_override);
1046 else if (ser->proxy_host || ser->proxy_port)
1048 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
1049 if (ser->proxy_port)
1053 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
1059 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
1063 // Master list of connections we're handling events to
1064 static IOCHAN channel_list = 0;
1065 void pazpar2_add_channel(IOCHAN chan)
1067 chan->next = channel_list;
1068 channel_list = chan;
1071 void pazpar2_event_loop()
1073 event_loop(&channel_list);
1076 static struct record_metadata *record_metadata_init(
1077 NMEM nmem, char *value, enum conf_metadata_type type)
1079 struct record_metadata *rec_md = record_metadata_create(nmem);
1080 if (type == Metadata_type_generic)
1083 p = normalize7bit_generic(p, " ,/.:([");
1085 rec_md->data.text.disp = nmem_strdup(nmem, p);
1086 rec_md->data.text.sort = 0;
1088 else if (type == Metadata_type_year || type == Metadata_type_date)
1093 if (type == Metadata_type_date)
1095 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1098 rec_md->data.number.min = first;
1099 rec_md->data.number.max = last;
1106 struct record *ingest_record(struct client *cl, Z_External *rec,
1109 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1110 client_get_session(cl), rec);
1112 struct record *record;
1113 struct record_cluster *cluster;
1114 struct session *se = client_get_session(cl);
1115 xmlChar *mergekey, *mergekey_norm;
1118 struct conf_service *service = global_parameters.server->service;
1119 const char *norm_str = 0;
1120 pp2_relevance_token_t prt = 0;
1126 root = xmlDocGetRootElement(xdoc);
1127 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1129 yaz_log(YLOG_WARN, "No mergekey found in record");
1134 record = record_create(se->nmem,
1135 service->num_metadata, service->num_sortkeys, cl,
1138 prt = pp2_relevance_tokenize(
1139 global_parameters.server->mergekey_pct, (const char *) mergekey);
1142 norm_wr = wrbuf_alloc();
1144 while ((norm_str = pp2_relevance_token_next(prt)))
1148 if (wrbuf_len(norm_wr))
1149 wrbuf_puts(norm_wr, " ");
1150 wrbuf_puts(norm_wr, norm_str);
1154 mergekey_norm = (xmlChar *)nmem_strdup(se->nmem, wrbuf_cstr(norm_wr));
1155 wrbuf_destroy(norm_wr);
1157 pp2_relevance_token_destroy(prt);
1161 cluster = reclist_insert(se->reclist,
1162 global_parameters.server->service,
1163 record, (char *) mergekey_norm,
1165 if (global_parameters.dump_records)
1166 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1167 client_get_database(cl)->database->url, record_no);
1170 /* no room for record */
1174 relevance_newrec(se->relevance, cluster);
1177 // now parsing XML record and adding data to cluster or record metadata
1178 for (n = root->children; n; n = n->next)
1186 if (n->type != XML_ELEMENT_NODE)
1188 if (!strcmp((const char *) n->name, "metadata"))
1190 struct conf_metadata *ser_md = 0;
1191 struct conf_sortkey *ser_sk = 0;
1192 struct record_metadata **wheretoput = 0;
1193 struct record_metadata *rec_md = 0;
1194 int md_field_id = -1;
1195 int sk_field_id = -1;
1197 type = xmlGetProp(n, (xmlChar *) "type");
1198 value = xmlNodeListGetString(xdoc, n->children, 1);
1200 if (!type || !value || !*value)
1204 = conf_service_metadata_field_id(service, (const char *) type);
1205 if (md_field_id < 0)
1208 "Ignoring unknown metadata element: %s", type);
1212 ser_md = &service->metadata[md_field_id];
1214 if (ser_md->sortkey_offset >= 0){
1215 sk_field_id = ser_md->sortkey_offset;
1216 ser_sk = &service->sortkeys[sk_field_id];
1219 // non-merged metadata
1220 rec_md = record_metadata_init(se->nmem, (char *) value,
1224 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1228 wheretoput = &record->metadata[md_field_id];
1230 wheretoput = &(*wheretoput)->next;
1231 *wheretoput = rec_md;
1234 rec_md = record_metadata_init(se->nmem, (char *) value,
1236 wheretoput = &cluster->metadata[md_field_id];
1238 // and polulate with data:
1239 // assign cluster or record based on merge action
1240 if (ser_md->merge == Metadata_merge_no)
1243 wheretoput = &(*wheretoput)->next;
1244 *wheretoput = rec_md;
1246 if (ser_md->merge == Metadata_merge_unique)
1248 struct record_metadata *mnode;
1249 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1250 if (!strcmp((const char *) mnode->data.text.disp,
1251 rec_md->data.text.disp))
1255 rec_md->next = *wheretoput;
1256 *wheretoput = rec_md;
1259 else if (ser_md->merge == Metadata_merge_longest)
1262 || strlen(rec_md->data.text.disp)
1263 > strlen((*wheretoput)->data.text.disp))
1265 *wheretoput = rec_md;
1268 const char *sort_str = 0;
1270 ser_sk->type == Metadata_sortkey_skiparticle;
1272 if (!cluster->sortkeys[sk_field_id])
1273 cluster->sortkeys[sk_field_id] =
1274 nmem_malloc(se->nmem,
1275 sizeof(union data_types));
1277 prt = pp2_relevance_tokenize(
1278 global_parameters.server->sort_pct,
1279 rec_md->data.text.disp);
1281 pp2_relevance_token_next(prt);
1283 sort_str = pp2_get_sort(prt, skip_article);
1285 cluster->sortkeys[sk_field_id]->text.disp =
1286 rec_md->data.text.disp;
1289 sort_str = rec_md->data.text.disp;
1291 "Could not make sortkey. Bug #1858");
1293 cluster->sortkeys[sk_field_id]->text.sort =
1294 nmem_strdup(se->nmem, sort_str);
1296 yaz_log(YLOG_LOG, "text disp=%s",
1297 cluster->sortkeys[sk_field_id]->text.disp);
1298 yaz_log(YLOG_LOG, "text sort=%s",
1299 cluster->sortkeys[sk_field_id]->text.sort);
1301 pp2_relevance_token_destroy(prt);
1305 else if (ser_md->merge == Metadata_merge_all)
1307 rec_md->next = *wheretoput;
1308 *wheretoput = rec_md;
1310 else if (ser_md->merge == Metadata_merge_range)
1314 *wheretoput = rec_md;
1316 cluster->sortkeys[sk_field_id]
1321 int this_min = rec_md->data.number.min;
1322 int this_max = rec_md->data.number.max;
1323 if (this_min < (*wheretoput)->data.number.min)
1324 (*wheretoput)->data.number.min = this_min;
1325 if (this_max > (*wheretoput)->data.number.max)
1326 (*wheretoput)->data.number.max = this_max;
1331 // ranking of _all_ fields enabled ...
1333 relevance_countwords(se->relevance, cluster,
1334 (char *) value, ser_md->rank);
1336 // construct facets ...
1337 if (ser_md->termlist)
1339 if (ser_md->type == Metadata_type_year)
1342 sprintf(year, "%d", rec_md->data.number.max);
1343 add_facet(se, (char *) type, year);
1344 if (rec_md->data.number.max != rec_md->data.number.min)
1346 sprintf(year, "%d", rec_md->data.number.min);
1347 add_facet(se, (char *) type, year);
1351 add_facet(se, (char *) type, (char *) value);
1361 "Unexpected element %s in internal record", n->name);
1370 relevance_donerecord(se->relevance, cluster);
1371 se->total_records++;
1381 * indent-tabs-mode: nil
1383 * vim: shiftwidth=4 tabstop=8 expandtab