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
29 #include <sys/socket.h>
35 #include <yaz/marcdisp.h>
36 #include <yaz/comstack.h>
37 #include <yaz/tcpip.h>
38 #include <yaz/proto.h>
39 #include <yaz/readconf.h>
40 #include <yaz/pquery.h>
41 #include <yaz/otherinfo.h>
42 #include <yaz/yaz-util.h>
44 #include <yaz/query-charset.h>
45 #include <yaz/querytowrbuf.h>
46 #include <yaz/oid_db.h>
47 #include <yaz/snprintf.h>
55 #include <yaz/timing.h>
58 #include <netinet/in.h>
63 #include "termlists.h"
65 #include "relevance.h"
70 #include "normalize7bit.h"
72 #define TERMLIST_HIGH_SCORE 25
76 // Note: Some things in this structure will eventually move to configuration
77 struct parameters global_parameters =
85 30, // operations timeout
89 60, // session timeout
94 180, // Z39.50 session timeout
98 // Recursively traverse query structure to extract terms.
99 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
111 pull_terms(nmem, n->u.p[0], termlist, num);
112 pull_terms(nmem, n->u.p[1], termlist, num);
115 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
116 for (i = 0; i < numwords; i++)
117 termlist[(*num)++] = words[i];
126 static void add_facet(struct session *s, const char *type, const char *value)
132 for (i = 0; i < s->num_termlists; i++)
133 if (!strcmp(s->termlists[i].name, type))
135 if (i == s->num_termlists)
137 if (i == SESSION_MAX_TERMLISTS)
139 yaz_log(YLOG_FATAL, "Too many termlists");
143 s->termlists[i].name = nmem_strdup(s->nmem, type);
144 s->termlists[i].termlist
145 = termlist_create(s->nmem, s->expected_maxrecs,
146 TERMLIST_HIGH_SCORE);
147 s->num_termlists = i + 1;
149 termlist_insert(s->termlists[i].termlist, value);
152 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
154 struct database *db = sdb->database;
156 const Odr_oid *oid = rec->direct_reference;
158 /* convert response record to XML somehow */
159 if (rec->which == Z_External_octet && oid
160 && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
163 rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
164 rec->u.octet_aligned->len);
167 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
172 else if (rec->which == Z_External_OPAC)
176 yaz_log(YLOG_WARN, "MARC decoding not configured");
181 /* OPAC gets converted to XML too */
182 WRBUF wrbuf_opac = wrbuf_alloc();
183 /* MARCXML inside the OPAC XML. Charset is in effect because we
184 use the yaz_marc handle */
185 yaz_marc_xml(sdb->yaz_marc, YAZ_MARC_MARCXML);
186 yaz_opac_decode_wrbuf(sdb->yaz_marc, rec->u.opac, wrbuf_opac);
188 rdoc = xmlParseMemory((char*) wrbuf_buf(wrbuf_opac),
189 wrbuf_len(wrbuf_opac));
192 yaz_log(YLOG_WARN, "Unable to parse OPAC XML");
193 /* Was used to debug bug #1348 */
195 FILE *f = fopen("/tmp/opac.xml.txt", "wb");
198 fwrite(wrbuf_buf(wrbuf_opac), 1, wrbuf_len(wrbuf_opac), f);
203 wrbuf_destroy(wrbuf_opac);
206 else if (oid && yaz_oid_is_iso2709(oid))
208 /* ISO2709 gets converted to MARCXML */
211 yaz_log(YLOG_WARN, "MARC decoding not configured");
220 if (rec->which != Z_External_octet)
222 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
226 buf = (char*) rec->u.octet_aligned->buf;
227 len = rec->u.octet_aligned->len;
228 if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
230 yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
234 if (yaz_marc_write_xml(sdb->yaz_marc, &res,
235 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
237 yaz_log(YLOG_WARN, "Failed to encode as XML %s",
241 rdoc = xmlNewDoc((xmlChar *) "1.0");
242 xmlDocSetRootElement(rdoc, res);
247 char oid_name_buf[OID_STR_MAX];
248 const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
250 "Unable to handle record of type %s from %s",
255 if (global_parameters.dump_records)
257 FILE *lf = yaz_log_file();
260 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
261 #if LIBXML_VERSION >= 20600
262 xmlDocFormatDump(lf, rdoc, 1);
264 xmlDocDump(lf, rdoc);
272 #define MAX_XSLT_ARGS 16
274 // Add static values from session database settings if applicable
275 static void insert_settings_parameters(struct session_database *sdb,
276 struct session *se, char **parms)
278 struct conf_service *service = global_parameters.server->service;
283 for (i = 0; i < service->num_metadata; i++)
285 struct conf_metadata *md = &service->metadata[i];
288 if (md->setting == Metadata_setting_parameter &&
289 (setting = settings_offset(md->name)) > 0)
291 const char *val = session_setting_oneval(sdb, setting);
292 if (val && nparms < MAX_XSLT_ARGS)
295 int len = strlen(val);
296 buf = nmem_malloc(se->nmem, len + 3);
298 strcpy(buf + 1, val);
301 parms[offset++] = md->name;
302 parms[offset++] = buf;
310 // Add static values from session database settings if applicable
311 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc)
313 struct conf_service *service = global_parameters.server->service;
316 for (i = 0; i < service->num_metadata; i++)
318 struct conf_metadata *md = &service->metadata[i];
321 if (md->setting == Metadata_setting_postproc &&
322 (offset = settings_offset(md->name)) > 0)
324 const char *val = session_setting_oneval(sdb, offset);
327 xmlNode *r = xmlDocGetRootElement(doc);
328 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
330 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
336 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
339 struct database_retrievalmap *m;
340 xmlDoc *rdoc = record_to_xml(sdb, rec);
343 for (m = sdb->map; m; m = m->next)
349 char *parms[MAX_XSLT_ARGS*2+1];
351 insert_settings_parameters(sdb, se, parms);
353 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
354 root= xmlDocGetRootElement(new);
355 if (!new || !root || !(root->children))
357 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
369 insert_settings_values(sdb, rdoc);
371 if (global_parameters.dump_records)
373 FILE *lf = yaz_log_file();
377 yaz_log(YLOG_LOG, "Normalized record from %s",
379 #if LIBXML_VERSION >= 20600
380 xmlDocFormatDump(lf, rdoc, 1);
382 xmlDocDump(lf, rdoc);
391 // Retrieve first defined value for 'name' for given database.
392 // Will be extended to take into account user associated with session
393 const char *session_setting_oneval(struct session_database *db, int offset)
395 if (!db->settings[offset])
397 return db->settings[offset]->value;
402 // Initialize YAZ Map structures for MARC-based targets
403 static int prepare_yazmarc(struct session_database *sdb)
409 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
412 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX))
413 && !strncmp(s, "iso2709", 7))
415 char *encoding = "marc-8s", *e;
418 // See if a native encoding is specified
419 if ((e = strchr(s, ';')))
422 sdb->yaz_marc = yaz_marc_create();
423 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
425 cm = yaz_iconv_open("utf-8", encoding);
429 "Unable to map from %s to UTF-8 for target %s",
430 encoding, sdb->database->url);
433 yaz_marc_iconv(sdb->yaz_marc, cm);
438 // Prepare XSLT stylesheets for record normalization
439 // Structures are allocated on the session_wide nmem to avoid having
440 // to recompute this for every search. This would lead
441 // to leaking if a single session was to repeatedly change the PZ_XSLT
442 // setting. However, this is not a realistic use scenario.
443 static int prepare_map(struct session *se, struct session_database *sdb)
449 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
452 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
455 struct database_retrievalmap **m = &sdb->map;
457 char auto_stylesheet[256];
459 if (!strcmp(s, "auto"))
461 const char *request_syntax = session_setting_oneval(
462 sdb, PZ_REQUESTSYNTAX);
466 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
467 "%s.xsl", request_syntax);
468 for (cp = auto_stylesheet; *cp; cp++)
470 /* deliberately only consider ASCII */
471 if (*cp > 32 && *cp < 127)
478 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
481 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
482 for (i = 0; i < num; i++)
484 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
486 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
488 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
496 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
501 // This analyzes settings and recomputes any supporting data structures
503 static int prepare_session_database(struct session *se,
504 struct session_database *sdb)
509 "No settings associated with %s", sdb->database->url);
512 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
514 if (prepare_yazmarc(sdb) < 0)
517 if (sdb->settings[PZ_XSLT] && !sdb->map)
519 if (prepare_map(se, sdb) < 0)
525 // called if watch should be removed because http_channel is to be destroyed
526 static void session_watch_cancel(void *data, struct http_channel *c,
529 struct session_watchentry *ent = data;
536 // set watch. Returns 0=OK, -1 if watch is already set
537 int session_set_watch(struct session *s, int what,
538 session_watchfun fun, void *data,
539 struct http_channel *chan)
541 if (s->watchlist[what].fun)
543 s->watchlist[what].fun = fun;
544 s->watchlist[what].data = data;
545 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
546 session_watch_cancel);
550 void session_alert_watch(struct session *s, int what)
552 if (s->watchlist[what].fun)
554 /* our watch is no longer associated with http_channel */
556 session_watchfun fun;
558 http_remove_observer(s->watchlist[what].obs);
559 fun = s->watchlist[what].fun;
560 data = s->watchlist[what].data;
562 /* reset watch before fun is invoked - in case fun wants to set
564 s->watchlist[what].fun = 0;
565 s->watchlist[what].data = 0;
566 s->watchlist[what].obs = 0;
572 //callback for grep_databases
573 static void select_targets_callback(void *context, struct session_database *db)
575 struct session *se = (struct session*) context;
576 struct client *cl = client_create();
577 client_set_database(cl, db);
578 client_set_session(cl, se);
581 // Associates a set of clients with a session;
582 // Note: Session-databases represent databases with per-session
584 int select_targets(struct session *se, struct database_criterion *crit)
587 client_destroy(se->clients);
589 return session_grep_databases(se, crit, select_targets_callback);
592 int session_active_clients(struct session *s)
597 for (c = s->clients; c; c = client_next_in_session(c))
598 if (client_is_active(c))
604 // parses crit1=val1,crit2=val2|val3,...
605 static struct database_criterion *parse_filter(NMEM m, const char *buf)
607 struct database_criterion *res = 0;
614 nmem_strsplit(m, ",", buf, &values, &num);
615 for (i = 0; i < num; i++)
620 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
621 char *eq = strchr(values[i], '=');
624 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
628 new->name = values[i];
629 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
631 for (subi = 0; subi < subnum; subi++)
633 struct database_criterion_value *newv
634 = nmem_malloc(m, sizeof(*newv));
635 newv->value = subvalues[subi];
636 newv->next = new->values;
645 enum pazpar2_error_code search(struct session *se,
646 char *query, char *filter,
647 const char **addinfo)
649 int live_channels = 0;
653 struct database_criterion *criteria;
655 yaz_log(YLOG_DEBUG, "Search");
658 nmem_reset(se->nmem);
660 se->total_records = se->total_hits = se->total_merged = 0;
662 se->num_termlists = 0;
663 criteria = parse_filter(se->nmem, filter);
665 live_channels = select_targets(se, criteria);
668 int maxrecs = live_channels * global_parameters.toget;
669 se->reclist = reclist_create(se->nmem, maxrecs);
670 se->expected_maxrecs = maxrecs;
673 return PAZPAR2_NO_TARGETS;
675 for (cl = se->clients; cl; cl = client_next_in_session(cl))
677 if (prepare_session_database(se, client_get_database(cl)) < 0)
679 *addinfo = client_get_database(cl)->database->url;
680 return PAZPAR2_CONFIG_TARGET;
682 // Parse query for target
683 if (client_parse_query(cl, query) < 0)
688 client_prep_connection(cl);
692 // If no queries could be mapped, we signal an error
696 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
698 return PAZPAR2_NO_ERROR;
701 // Creates a new session_database object for a database
702 static void session_init_databases_fun(void *context, struct database *db)
704 struct session *se = (struct session *) context;
705 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
706 int num = settings_num();
714 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
715 memset(new->settings, 0, sizeof(struct settings*) * num);
719 for (i = 0; i < num; i++)
720 new->settings[i] = db->settings[i];
722 new->next = se->databases;
726 // Doesn't free memory associated with sdb -- nmem takes care of that
727 static void session_database_destroy(struct session_database *sdb)
729 struct database_retrievalmap *m;
731 for (m = sdb->map; m; m = m->next)
732 xsltFreeStylesheet(m->stylesheet);
734 yaz_marc_destroy(sdb->yaz_marc);
737 // Initialize session_database list -- this represents this session's view
738 // of the database list -- subject to modification by the settings ws command
739 void session_init_databases(struct session *se)
742 predef_grep_databases(se, 0, session_init_databases_fun);
745 // Probably session_init_databases_fun should be refactored instead of
747 static struct session_database *load_session_database(struct session *se,
750 struct database *db = find_database(id, 0);
752 session_init_databases_fun((void*) se, db);
753 // New sdb is head of se->databases list
754 return se->databases;
757 // Find an existing session database. If not found, load it
758 static struct session_database *find_session_database(struct session *se,
761 struct session_database *sdb;
763 for (sdb = se->databases; sdb; sdb = sdb->next)
764 if (!strcmp(sdb->database->url, id))
766 return load_session_database(se, id);
769 // Apply a session override to a database
770 void session_apply_setting(struct session *se, char *dbname, char *setting,
773 struct session_database *sdb = find_session_database(se, dbname);
774 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
775 int offset = settings_offset_cprefix(setting);
779 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
782 // Jakub: This breaks the filter setting.
783 /*if (offset == PZ_ID)
785 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
789 new->target = dbname;
792 new->next = sdb->settings[offset];
793 sdb->settings[offset] = new;
795 // Force later recompute of settings-driven data structures
796 // (happens when a search starts and client connections are prepared)
799 case PZ_NATIVESYNTAX:
802 yaz_marc_destroy(sdb->yaz_marc);
809 struct database_retrievalmap *m;
810 // We don't worry about the map structure -- it's in nmem
811 for (m = sdb->map; m; m = m->next)
812 xsltFreeStylesheet(m->stylesheet);
819 void destroy_session(struct session *s)
821 struct session_database *sdb;
824 client_destroy(s->clients);
825 for (sdb = s->databases; sdb; sdb = sdb->next)
826 session_database_destroy(sdb);
827 nmem_destroy(s->nmem);
828 wrbuf_destroy(s->wrbuf);
831 struct session *new_session(NMEM nmem)
834 struct session *session = nmem_malloc(nmem, sizeof(*session));
836 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
838 session->relevance = 0;
839 session->total_hits = 0;
840 session->total_records = 0;
841 session->num_termlists = 0;
842 session->reclist = 0;
843 session->requestid = -1;
844 session->clients = 0;
845 session->expected_maxrecs = 0;
846 session->session_nmem = nmem;
847 session->nmem = nmem_create();
848 session->wrbuf = wrbuf_alloc();
849 session->databases = 0;
850 for (i = 0; i <= SESSION_WATCH_MAX; i++)
852 session->watchlist[i].data = 0;
853 session->watchlist[i].fun = 0;
858 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
860 struct hitsbytarget *res = 0;
864 for (cl = se->clients; cl; cl = client_next_in_session(cl))
867 res = nmem_malloc(nmem, sizeof(*res) * sz);
869 for (cl = se->clients; cl; cl = client_next_in_session(cl))
871 const char *name = session_setting_oneval(client_get_database(cl),
874 res[*count].id = client_get_database(cl)->database->url;
875 res[*count].name = *name ? name : "Unknown";
876 res[*count].hits = client_get_hits(cl);
877 res[*count].records = client_get_num_records(cl);
878 res[*count].diagnostic = client_get_diagnostic(cl);
879 res[*count].state = client_get_state_str(cl);
880 res[*count].connected = client_get_connection(cl) ? 1 : 0;
886 struct termlist_score **termlist(struct session *s, const char *name, int *num)
890 for (i = 0; i < s->num_termlists; i++)
891 if (!strcmp((const char *) s->termlists[i].name, name))
892 return termlist_highscore(s->termlists[i].termlist, num);
896 #ifdef MISSING_HEADERS
897 void report_nmem_stats(void)
899 size_t in_use, is_free;
901 nmem_get_memory_in_use(&in_use);
902 nmem_get_memory_free(&is_free);
904 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
905 (long) in_use, (long) is_free);
909 struct record_cluster *show_single(struct session *s, const char *id)
911 struct record_cluster *r;
913 reclist_rewind(s->reclist);
914 while ((r = reclist_read_record(s->reclist)))
915 if (!strcmp(r->recid, id))
920 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
921 int start, int *num, int *total, int *sumhits,
924 struct record_cluster **recs = nmem_malloc(nmem_show, *num
925 * sizeof(struct record_cluster *));
926 struct reclist_sortparms *spp;
929 yaz_timing_t t = yaz_timing_create();
941 for (spp = sp; spp; spp = spp->next)
942 if (spp->type == Metadata_sortkey_relevance)
944 relevance_prepare_read(s->relevance, s->reclist);
947 reclist_sort(s->reclist, sp);
949 *total = s->reclist->num_records;
950 *sumhits = s->total_hits;
952 for (i = 0; i < start; i++)
953 if (!reclist_read_record(s->reclist))
960 for (i = 0; i < *num; i++)
962 struct record_cluster *r = reclist_read_record(s->reclist);
973 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
974 yaz_timing_get_real(t), yaz_timing_get_user(t),
975 yaz_timing_get_sys(t));
976 yaz_timing_destroy(&t);
981 void statistics(struct session *se, struct statistics *stat)
986 memset(stat, 0, sizeof(*stat));
987 for (cl = se->clients; cl; cl = client_next_in_session(cl))
989 if (!client_get_connection(cl))
990 stat->num_no_connection++;
991 switch (client_get_state(cl))
993 case Client_Connecting: stat->num_connecting++; break;
994 case Client_Initializing: stat->num_initializing++; break;
995 case Client_Searching: stat->num_searching++; break;
996 case Client_Presenting: stat->num_presenting++; break;
997 case Client_Idle: stat->num_idle++; break;
998 case Client_Failed: stat->num_failed++; break;
999 case Client_Error: stat->num_error++; break;
1004 stat->num_hits = se->total_hits;
1005 stat->num_records = se->total_records;
1007 stat->num_clients = count;
1010 void start_http_listener(void)
1013 struct conf_server *ser = global_parameters.server;
1015 if (*global_parameters.listener_override)
1016 strcpy(hp, global_parameters.listener_override);
1019 strcpy(hp, ser->host ? ser->host : "");
1024 sprintf(hp + strlen(hp), "%d", ser->port);
1030 void start_proxy(void)
1033 struct conf_server *ser = global_parameters.server;
1035 if (*global_parameters.proxy_override)
1036 strcpy(hp, global_parameters.proxy_override);
1037 else if (ser->proxy_host || ser->proxy_port)
1039 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
1040 if (ser->proxy_port)
1044 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
1050 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
1054 // Master list of connections we're handling events to
1055 static IOCHAN channel_list = 0;
1056 void pazpar2_add_channel(IOCHAN chan)
1058 chan->next = channel_list;
1059 channel_list = chan;
1062 void pazpar2_event_loop()
1064 event_loop(&channel_list);
1067 static struct record_metadata *record_metadata_init(
1068 NMEM nmem, char *value, enum conf_metadata_type type)
1070 struct record_metadata *rec_md = record_metadata_create(nmem);
1071 if (type == Metadata_type_generic)
1074 p = normalize7bit_generic(p, " ,/.:([");
1076 rec_md->data.text.disp = nmem_strdup(nmem, p);
1077 rec_md->data.text.sort = 0;
1079 else if (type == Metadata_type_year || type == Metadata_type_date)
1084 if (type == Metadata_type_date)
1086 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1089 rec_md->data.number.min = first;
1090 rec_md->data.number.max = last;
1097 struct record *ingest_record(struct client *cl, Z_External *rec,
1100 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1101 client_get_session(cl), rec);
1103 struct record *record;
1104 struct record_cluster *cluster;
1105 struct session *se = client_get_session(cl);
1106 xmlChar *mergekey, *mergekey_norm;
1109 struct conf_service *service = global_parameters.server->service;
1110 const char *norm_str = 0;
1111 pp2_relevance_token_t prt = 0;
1117 root = xmlDocGetRootElement(xdoc);
1118 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1120 yaz_log(YLOG_WARN, "No mergekey found in record");
1125 record = record_create(se->nmem,
1126 service->num_metadata, service->num_sortkeys, cl,
1129 prt = pp2_relevance_tokenize(
1130 global_parameters.server->mergekey_pct, (const char *) mergekey);
1133 norm_wr = wrbuf_alloc();
1135 while ((norm_str = pp2_relevance_token_next(prt)))
1139 if (wrbuf_len(norm_wr))
1140 wrbuf_puts(norm_wr, " ");
1141 wrbuf_puts(norm_wr, norm_str);
1145 mergekey_norm = (xmlChar *)nmem_strdup(se->nmem, wrbuf_cstr(norm_wr));
1146 wrbuf_destroy(norm_wr);
1148 pp2_relevance_token_destroy(prt);
1152 cluster = reclist_insert(se->reclist,
1153 global_parameters.server->service,
1154 record, (char *) mergekey_norm,
1156 if (global_parameters.dump_records)
1157 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1158 client_get_database(cl)->database->url, record_no);
1161 /* no room for record */
1165 relevance_newrec(se->relevance, cluster);
1168 // now parsing XML record and adding data to cluster or record metadata
1169 for (n = root->children; n; n = n->next)
1177 if (n->type != XML_ELEMENT_NODE)
1179 if (!strcmp((const char *) n->name, "metadata"))
1181 struct conf_metadata *ser_md = 0;
1182 struct conf_sortkey *ser_sk = 0;
1183 struct record_metadata **wheretoput = 0;
1184 struct record_metadata *rec_md = 0;
1185 int md_field_id = -1;
1186 int sk_field_id = -1;
1188 type = xmlGetProp(n, (xmlChar *) "type");
1189 value = xmlNodeListGetString(xdoc, n->children, 1);
1191 if (!type || !value || !*value)
1195 = conf_service_metadata_field_id(service, (const char *) type);
1196 if (md_field_id < 0)
1199 "Ignoring unknown metadata element: %s", type);
1203 ser_md = &service->metadata[md_field_id];
1205 if (ser_md->sortkey_offset >= 0){
1206 sk_field_id = ser_md->sortkey_offset;
1207 ser_sk = &service->sortkeys[sk_field_id];
1210 // non-merged metadata
1211 rec_md = record_metadata_init(se->nmem, (char *) value,
1215 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1219 rec_md->next = record->metadata[md_field_id];
1220 record->metadata[md_field_id] = rec_md;
1223 rec_md = record_metadata_init(se->nmem, (char *) value,
1225 wheretoput = &cluster->metadata[md_field_id];
1227 // and polulate with data:
1228 // assign cluster or record based on merge action
1229 if (ser_md->merge == Metadata_merge_unique)
1231 struct record_metadata *mnode;
1232 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1233 if (!strcmp((const char *) mnode->data.text.disp,
1234 rec_md->data.text.disp))
1238 rec_md->next = *wheretoput;
1239 *wheretoput = rec_md;
1242 else if (ser_md->merge == Metadata_merge_longest)
1245 || strlen(rec_md->data.text.disp)
1246 > strlen((*wheretoput)->data.text.disp))
1248 *wheretoput = rec_md;
1251 const char *sort_str = 0;
1253 ser_sk->type == Metadata_sortkey_skiparticle;
1255 if (!cluster->sortkeys[sk_field_id])
1256 cluster->sortkeys[sk_field_id] =
1257 nmem_malloc(se->nmem,
1258 sizeof(union data_types));
1260 prt = pp2_relevance_tokenize(
1261 global_parameters.server->sort_pct,
1262 rec_md->data.text.disp);
1264 pp2_relevance_token_next(prt);
1266 sort_str = pp2_get_sort(prt, skip_article);
1268 cluster->sortkeys[sk_field_id]->text.disp =
1269 rec_md->data.text.disp;
1272 sort_str = rec_md->data.text.disp;
1274 "Could not make sortkey. Bug #1858");
1276 cluster->sortkeys[sk_field_id]->text.sort =
1277 nmem_strdup(se->nmem, sort_str);
1279 yaz_log(YLOG_LOG, "text disp=%s",
1280 cluster->sortkeys[sk_field_id]->text.disp);
1281 yaz_log(YLOG_LOG, "text sort=%s",
1282 cluster->sortkeys[sk_field_id]->text.sort);
1284 pp2_relevance_token_destroy(prt);
1288 else if (ser_md->merge == Metadata_merge_all)
1290 rec_md->next = *wheretoput;
1291 *wheretoput = rec_md;
1293 else if (ser_md->merge == Metadata_merge_range)
1297 *wheretoput = rec_md;
1299 cluster->sortkeys[sk_field_id]
1304 int this_min = rec_md->data.number.min;
1305 int this_max = rec_md->data.number.max;
1306 if (this_min < (*wheretoput)->data.number.min)
1307 (*wheretoput)->data.number.min = this_min;
1308 if (this_max > (*wheretoput)->data.number.max)
1309 (*wheretoput)->data.number.max = this_max;
1314 union data_types *sdata
1315 = cluster->sortkeys[sk_field_id];
1316 yaz_log(YLOG_LOG, "SK range: %d-%d",
1317 sdata->number.min, sdata->number.max);
1323 // ranking of _all_ fields enabled ...
1325 relevance_countwords(se->relevance, cluster,
1326 (char *) value, ser_md->rank);
1328 // construct facets ...
1329 if (ser_md->termlist)
1331 if (ser_md->type == Metadata_type_year)
1334 sprintf(year, "%d", rec_md->data.number.max);
1335 add_facet(se, (char *) type, year);
1336 if (rec_md->data.number.max != rec_md->data.number.min)
1338 sprintf(year, "%d", rec_md->data.number.min);
1339 add_facet(se, (char *) type, year);
1343 add_facet(se, (char *) type, (char *) value);
1353 "Unexpected element %s in internal record", n->name);
1362 relevance_donerecord(se->relevance, cluster);
1363 se->total_records++;
1373 * indent-tabs-mode: nil
1375 * vim: shiftwidth=4 tabstop=8 expandtab