1 /* $Id: logic.c,v 1.68 2007-10-02 10:08:39 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 \brief high-level logic; mostly user sessions and settings
31 #include <sys/socket.h>
37 #include <yaz/marcdisp.h>
38 #include <yaz/comstack.h>
39 #include <yaz/tcpip.h>
40 #include <yaz/proto.h>
41 #include <yaz/readconf.h>
42 #include <yaz/pquery.h>
43 #include <yaz/otherinfo.h>
44 #include <yaz/yaz-util.h>
46 #include <yaz/query-charset.h>
47 #include <yaz/querytowrbuf.h>
48 #include <yaz/oid_db.h>
49 #include <yaz/snprintf.h>
57 #include <yaz/timing.h>
60 #include <netinet/in.h>
65 #include "termlists.h"
67 #include "relevance.h"
72 #include "normalize7bit.h"
74 #define TERMLIST_HIGH_SCORE 25
78 // Note: Some things in this structure will eventually move to configuration
79 struct parameters global_parameters =
87 30, // operations timeout
91 60, // session timeout
96 180, // Z39.50 session timeout
100 // Recursively traverse query structure to extract terms.
101 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
113 pull_terms(nmem, n->u.p[0], termlist, num);
114 pull_terms(nmem, n->u.p[1], termlist, num);
117 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
118 for (i = 0; i < numwords; i++)
119 termlist[(*num)++] = words[i];
128 static void add_facet(struct session *s, const char *type, const char *value)
134 for (i = 0; i < s->num_termlists; i++)
135 if (!strcmp(s->termlists[i].name, type))
137 if (i == s->num_termlists)
139 if (i == SESSION_MAX_TERMLISTS)
141 yaz_log(YLOG_FATAL, "Too many termlists");
145 s->termlists[i].name = nmem_strdup(s->nmem, type);
146 s->termlists[i].termlist
147 = termlist_create(s->nmem, s->expected_maxrecs,
148 TERMLIST_HIGH_SCORE);
149 s->num_termlists = i + 1;
151 termlist_insert(s->termlists[i].termlist, value);
154 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
156 struct database *db = sdb->database;
158 const Odr_oid *oid = rec->direct_reference;
160 /* convert response record to XML somehow */
161 if (rec->which == Z_External_octet && oid
162 && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
165 rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
166 rec->u.octet_aligned->len);
169 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
174 else if (rec->which == Z_External_OPAC)
178 yaz_log(YLOG_WARN, "MARC decoding not configured");
183 /* OPAC gets converted to XML too */
184 WRBUF wrbuf_opac = wrbuf_alloc();
185 /* MARCXML inside the OPAC XML. Charset is in effect because we
186 use the yaz_marc handle */
187 yaz_marc_xml(sdb->yaz_marc, YAZ_MARC_MARCXML);
188 yaz_opac_decode_wrbuf(sdb->yaz_marc, rec->u.opac, wrbuf_opac);
190 rdoc = xmlParseMemory((char*) wrbuf_buf(wrbuf_opac),
191 wrbuf_len(wrbuf_opac));
194 yaz_log(YLOG_WARN, "Unable to parse OPAC XML");
195 /* Was used to debug bug #1348 */
197 FILE *f = fopen("/tmp/opac.xml.txt", "wb");
200 fwrite(wrbuf_buf(wrbuf_opac), 1, wrbuf_len(wrbuf_opac), f);
205 wrbuf_destroy(wrbuf_opac);
208 else if (oid && yaz_oid_is_iso2709(oid))
210 /* ISO2709 gets converted to MARCXML */
213 yaz_log(YLOG_WARN, "MARC decoding not configured");
222 if (rec->which != Z_External_octet)
224 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
228 buf = (char*) rec->u.octet_aligned->buf;
229 len = rec->u.octet_aligned->len;
230 if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
232 yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
236 if (yaz_marc_write_xml(sdb->yaz_marc, &res,
237 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
239 yaz_log(YLOG_WARN, "Failed to encode as XML %s",
243 rdoc = xmlNewDoc((xmlChar *) "1.0");
244 xmlDocSetRootElement(rdoc, res);
249 char oid_name_buf[OID_STR_MAX];
250 const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
252 "Unable to handle record of type %s from %s",
257 if (global_parameters.dump_records)
259 FILE *lf = yaz_log_file();
262 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
263 #if LIBXML_VERSION >= 20600
264 xmlDocFormatDump(lf, rdoc, 1);
266 xmlDocDump(lf, rdoc);
274 #define MAX_XSLT_ARGS 16
276 // Add static values from session database settings if applicable
277 static void insert_settings_parameters(struct session_database *sdb,
278 struct session *se, char **parms)
280 struct conf_service *service = global_parameters.server->service;
285 for (i = 0; i < service->num_metadata; i++)
287 struct conf_metadata *md = &service->metadata[i];
290 if (md->setting == Metadata_setting_parameter &&
291 (setting = settings_offset(md->name)) > 0)
293 const char *val = session_setting_oneval(sdb, setting);
294 if (val && nparms < MAX_XSLT_ARGS)
297 int len = strlen(val);
298 buf = nmem_malloc(se->nmem, len + 3);
300 strcpy(buf + 1, val);
303 parms[offset++] = md->name;
304 parms[offset++] = buf;
312 // Add static values from session database settings if applicable
313 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc)
315 struct conf_service *service = global_parameters.server->service;
318 for (i = 0; i < service->num_metadata; i++)
320 struct conf_metadata *md = &service->metadata[i];
323 if (md->setting == Metadata_setting_postproc &&
324 (offset = settings_offset(md->name)) > 0)
326 const char *val = session_setting_oneval(sdb, offset);
329 xmlNode *r = xmlDocGetRootElement(doc);
330 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
332 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
338 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
341 struct database_retrievalmap *m;
342 xmlDoc *rdoc = record_to_xml(sdb, rec);
345 for (m = sdb->map; m; m = m->next)
351 char *parms[MAX_XSLT_ARGS*2+1];
353 insert_settings_parameters(sdb, se, parms);
355 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
356 root= xmlDocGetRootElement(new);
357 if (!new || !root || !(root->children))
359 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
371 insert_settings_values(sdb, rdoc);
373 if (global_parameters.dump_records)
375 FILE *lf = yaz_log_file();
379 yaz_log(YLOG_LOG, "Normalized record from %s",
381 #if LIBXML_VERSION >= 20600
382 xmlDocFormatDump(lf, rdoc, 1);
384 xmlDocDump(lf, rdoc);
393 // Retrieve first defined value for 'name' for given database.
394 // Will be extended to take into account user associated with session
395 const char *session_setting_oneval(struct session_database *db, int offset)
397 if (!db->settings[offset])
399 return db->settings[offset]->value;
404 // Initialize YAZ Map structures for MARC-based targets
405 static int prepare_yazmarc(struct session_database *sdb)
411 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
414 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX))
415 && !strncmp(s, "iso2709", 7))
417 char *encoding = "marc-8s", *e;
420 // See if a native encoding is specified
421 if ((e = strchr(s, ';')))
424 sdb->yaz_marc = yaz_marc_create();
425 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
427 cm = yaz_iconv_open("utf-8", encoding);
431 "Unable to map from %s to UTF-8 for target %s",
432 encoding, sdb->database->url);
435 yaz_marc_iconv(sdb->yaz_marc, cm);
440 // Prepare XSLT stylesheets for record normalization
441 // Structures are allocated on the session_wide nmem to avoid having
442 // to recompute this for every search. This would lead
443 // to leaking if a single session was to repeatedly change the PZ_XSLT
444 // setting. However, this is not a realistic use scenario.
445 static int prepare_map(struct session *se, struct session_database *sdb)
451 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
454 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
457 struct database_retrievalmap **m = &sdb->map;
459 char auto_stylesheet[256];
461 if (!strcmp(s, "auto"))
463 const char *request_syntax = session_setting_oneval(
464 sdb, PZ_REQUESTSYNTAX);
468 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
469 "%s.xsl", request_syntax);
470 for (cp = auto_stylesheet; *cp; cp++)
472 /* deliberately only consider ASCII */
473 if (*cp > 32 && *cp < 127)
480 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
483 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
484 for (i = 0; i < num; i++)
486 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
488 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
490 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
498 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
503 // This analyzes settings and recomputes any supporting data structures
505 static int prepare_session_database(struct session *se,
506 struct session_database *sdb)
511 "No settings associated with %s", sdb->database->url);
514 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
516 if (prepare_yazmarc(sdb) < 0)
519 if (sdb->settings[PZ_XSLT] && !sdb->map)
521 if (prepare_map(se, sdb) < 0)
527 // called if watch should be removed because http_channel is to be destroyed
528 static void session_watch_cancel(void *data, struct http_channel *c)
530 struct session_watchentry *ent = data;
537 // set watch. Returns 0=OK, -1 if watch is already set
538 int session_set_watch(struct session *s, int what,
539 session_watchfun fun, void *data,
540 struct http_channel *chan)
542 if (s->watchlist[what].fun)
544 s->watchlist[what].fun = fun;
545 s->watchlist[what].data = data;
546 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
547 session_watch_cancel);
551 void session_alert_watch(struct session *s, int what)
553 if (s->watchlist[what].fun)
555 /* our watch is no longer associated with http_channel */
556 http_remove_observer(s->watchlist[what].obs);
557 session_watchfun fun = s->watchlist[what].fun;
558 void *data = s->watchlist[what].data;
560 /* reset watch before fun is invoked - in case fun wants to set
562 s->watchlist[what].fun = 0;
563 s->watchlist[what].data = 0;
564 s->watchlist[what].obs = 0;
570 //callback for grep_databases
571 static void select_targets_callback(void *context, struct session_database *db)
573 struct session *se = (struct session*) context;
574 struct client *cl = client_create();
575 client_set_database(cl, db);
576 client_set_session(cl, se);
579 // Associates a set of clients with a session;
580 // Note: Session-databases represent databases with per-session
582 int select_targets(struct session *se, struct database_criterion *crit)
585 client_destroy(se->clients);
587 return session_grep_databases(se, crit, select_targets_callback);
590 int session_active_clients(struct session *s)
595 for (c = s->clients; c; c = client_next_in_session(c))
596 if (client_is_active(c))
602 // parses crit1=val1,crit2=val2|val3,...
603 static struct database_criterion *parse_filter(NMEM m, const char *buf)
605 struct database_criterion *res = 0;
612 nmem_strsplit(m, ",", buf, &values, &num);
613 for (i = 0; i < num; i++)
618 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
619 char *eq = strchr(values[i], '=');
622 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
626 new->name = values[i];
627 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
629 for (subi = 0; subi < subnum; subi++)
631 struct database_criterion_value *newv
632 = nmem_malloc(m, sizeof(*newv));
633 newv->value = subvalues[subi];
634 newv->next = new->values;
643 enum pazpar2_error_code search(struct session *se,
644 char *query, char *filter,
645 const char **addinfo)
647 int live_channels = 0;
651 struct database_criterion *criteria;
653 yaz_log(YLOG_DEBUG, "Search");
656 nmem_reset(se->nmem);
658 se->total_records = se->total_hits = se->total_merged = 0;
660 se->num_termlists = 0;
661 criteria = parse_filter(se->nmem, filter);
663 live_channels = select_targets(se, criteria);
666 int maxrecs = live_channels * global_parameters.toget;
667 se->reclist = reclist_create(se->nmem, maxrecs);
668 se->expected_maxrecs = maxrecs;
671 return PAZPAR2_NO_TARGETS;
673 for (cl = se->clients; cl; cl = client_next_in_session(cl))
675 if (prepare_session_database(se, client_get_database(cl)) < 0)
677 *addinfo = client_get_database(cl)->database->url;
678 return PAZPAR2_CONFIG_TARGET;
680 // Parse query for target
681 if (client_parse_query(cl, query) < 0)
686 client_prep_connection(cl);
690 // If no queries could be mapped, we signal an error
694 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
696 return PAZPAR2_NO_ERROR;
699 // Creates a new session_database object for a database
700 static void session_init_databases_fun(void *context, struct database *db)
702 struct session *se = (struct session *) context;
703 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
704 int num = settings_num();
712 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
713 memset(new->settings, 0, sizeof(struct settings*) * num);
717 for (i = 0; i < num; i++)
718 new->settings[i] = db->settings[i];
720 new->next = se->databases;
724 // Doesn't free memory associated with sdb -- nmem takes care of that
725 static void session_database_destroy(struct session_database *sdb)
727 struct database_retrievalmap *m;
729 for (m = sdb->map; m; m = m->next)
730 xsltFreeStylesheet(m->stylesheet);
732 yaz_marc_destroy(sdb->yaz_marc);
735 // Initialize session_database list -- this represents this session's view
736 // of the database list -- subject to modification by the settings ws command
737 void session_init_databases(struct session *se)
740 predef_grep_databases(se, 0, session_init_databases_fun);
743 // Probably session_init_databases_fun should be refactored instead of
745 static struct session_database *load_session_database(struct session *se,
748 struct database *db = find_database(id, 0);
750 session_init_databases_fun((void*) se, db);
751 // New sdb is head of se->databases list
752 return se->databases;
755 // Find an existing session database. If not found, load it
756 static struct session_database *find_session_database(struct session *se,
759 struct session_database *sdb;
761 for (sdb = se->databases; sdb; sdb = sdb->next)
762 if (!strcmp(sdb->database->url, id))
764 return load_session_database(se, id);
767 // Apply a session override to a database
768 void session_apply_setting(struct session *se, char *dbname, char *setting,
771 struct session_database *sdb = find_session_database(se, dbname);
772 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
773 int offset = settings_offset_cprefix(setting);
777 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
780 // Jakub: This breaks the filter setting.
781 /*if (offset == PZ_ID)
783 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
787 new->target = dbname;
790 new->next = sdb->settings[offset];
791 sdb->settings[offset] = new;
793 // Force later recompute of settings-driven data structures
794 // (happens when a search starts and client connections are prepared)
797 case PZ_NATIVESYNTAX:
800 yaz_marc_destroy(sdb->yaz_marc);
807 struct database_retrievalmap *m;
808 // We don't worry about the map structure -- it's in nmem
809 for (m = sdb->map; m; m = m->next)
810 xsltFreeStylesheet(m->stylesheet);
817 void destroy_session(struct session *s)
819 struct session_database *sdb;
822 client_destroy(s->clients);
823 for (sdb = s->databases; sdb; sdb = sdb->next)
824 session_database_destroy(sdb);
825 nmem_destroy(s->nmem);
826 wrbuf_destroy(s->wrbuf);
829 struct session *new_session(NMEM nmem)
832 struct session *session = nmem_malloc(nmem, sizeof(*session));
834 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
836 session->relevance = 0;
837 session->total_hits = 0;
838 session->total_records = 0;
839 session->num_termlists = 0;
840 session->reclist = 0;
841 session->requestid = -1;
842 session->clients = 0;
843 session->expected_maxrecs = 0;
844 session->session_nmem = nmem;
845 session->nmem = nmem_create();
846 session->wrbuf = wrbuf_alloc();
847 session->databases = 0;
848 for (i = 0; i <= SESSION_WATCH_MAX; i++)
850 session->watchlist[i].data = 0;
851 session->watchlist[i].fun = 0;
856 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
858 struct hitsbytarget *res = 0;
862 for (cl = se->clients; cl; cl = client_next_in_session(cl))
865 res = nmem_malloc(nmem, sizeof(*res) * sz);
867 for (cl = se->clients; cl; cl = client_next_in_session(cl))
869 const char *name = session_setting_oneval(client_get_database(cl),
872 res[*count].id = client_get_database(cl)->database->url;
873 res[*count].name = *name ? name : "Unknown";
874 res[*count].hits = client_get_hits(cl);
875 res[*count].records = client_get_num_records(cl);
876 res[*count].diagnostic = client_get_diagnostic(cl);
877 res[*count].state = client_get_state_str(cl);
878 res[*count].connected = client_get_connection(cl) ? 1 : 0;
884 struct termlist_score **termlist(struct session *s, const char *name, int *num)
888 for (i = 0; i < s->num_termlists; i++)
889 if (!strcmp((const char *) s->termlists[i].name, name))
890 return termlist_highscore(s->termlists[i].termlist, num);
894 #ifdef MISSING_HEADERS
895 void report_nmem_stats(void)
897 size_t in_use, is_free;
899 nmem_get_memory_in_use(&in_use);
900 nmem_get_memory_free(&is_free);
902 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
903 (long) in_use, (long) is_free);
907 struct record_cluster *show_single(struct session *s, const char *id)
909 struct record_cluster *r;
911 reclist_rewind(s->reclist);
912 while ((r = reclist_read_record(s->reclist)))
913 if (!strcmp(r->recid, id))
918 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
919 int start, int *num, int *total, int *sumhits,
922 struct record_cluster **recs = nmem_malloc(nmem_show, *num
923 * sizeof(struct record_cluster *));
924 struct reclist_sortparms *spp;
927 yaz_timing_t t = yaz_timing_create();
939 for (spp = sp; spp; spp = spp->next)
940 if (spp->type == Metadata_sortkey_relevance)
942 relevance_prepare_read(s->relevance, s->reclist);
945 reclist_sort(s->reclist, sp);
947 *total = s->reclist->num_records;
948 *sumhits = s->total_hits;
950 for (i = 0; i < start; i++)
951 if (!reclist_read_record(s->reclist))
958 for (i = 0; i < *num; i++)
960 struct record_cluster *r = reclist_read_record(s->reclist);
971 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
972 yaz_timing_get_real(t), yaz_timing_get_user(t),
973 yaz_timing_get_sys(t));
974 yaz_timing_destroy(&t);
979 void statistics(struct session *se, struct statistics *stat)
984 memset(stat, 0, sizeof(*stat));
985 for (cl = se->clients; cl; cl = client_next_in_session(cl))
987 if (!client_get_connection(cl))
988 stat->num_no_connection++;
989 switch (client_get_state(cl))
991 case Client_Connecting: stat->num_connecting++; break;
992 case Client_Initializing: stat->num_initializing++; break;
993 case Client_Searching: stat->num_searching++; break;
994 case Client_Presenting: stat->num_presenting++; break;
995 case Client_Idle: stat->num_idle++; break;
996 case Client_Failed: stat->num_failed++; break;
997 case Client_Error: stat->num_error++; break;
1002 stat->num_hits = se->total_hits;
1003 stat->num_records = se->total_records;
1005 stat->num_clients = count;
1008 void start_http_listener(void)
1011 struct conf_server *ser = global_parameters.server;
1013 if (*global_parameters.listener_override)
1014 strcpy(hp, global_parameters.listener_override);
1017 strcpy(hp, ser->host ? ser->host : "");
1022 sprintf(hp + strlen(hp), "%d", ser->port);
1028 void start_proxy(void)
1031 struct conf_server *ser = global_parameters.server;
1033 if (*global_parameters.proxy_override)
1034 strcpy(hp, global_parameters.proxy_override);
1035 else if (ser->proxy_host || ser->proxy_port)
1037 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
1038 if (ser->proxy_port)
1042 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
1048 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
1052 // Master list of connections we're handling events to
1053 static IOCHAN channel_list = 0;
1054 void pazpar2_add_channel(IOCHAN chan)
1056 chan->next = channel_list;
1057 channel_list = chan;
1060 void pazpar2_event_loop()
1062 event_loop(&channel_list);
1065 static struct record_metadata *record_metadata_init(
1066 NMEM nmem, char *value, enum conf_metadata_type type)
1068 struct record_metadata *rec_md = record_metadata_create(nmem);
1069 if (type == Metadata_type_generic)
1072 p = normalize7bit_generic(p, " ,/.:([");
1074 rec_md->data.text.disp = nmem_strdup(nmem, p);
1075 rec_md->data.text.sort = 0;
1077 else if (type == Metadata_type_year)
1080 if (extract7bit_years((char *) value, &first, &last) < 0)
1082 rec_md->data.number.min = first;
1083 rec_md->data.number.max = last;
1090 struct record *ingest_record(struct client *cl, Z_External *rec,
1093 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1094 client_get_session(cl), rec);
1096 struct record *record;
1097 struct record_cluster *cluster;
1098 struct session *se = client_get_session(cl);
1099 xmlChar *mergekey, *mergekey_norm;
1102 struct conf_service *service = global_parameters.server->service;
1103 const char *norm_str = 0;
1104 pp2_relevance_token_t prt = 0;
1110 root = xmlDocGetRootElement(xdoc);
1111 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1113 yaz_log(YLOG_WARN, "No mergekey found in record");
1118 record = record_create(se->nmem,
1119 service->num_metadata, service->num_sortkeys, cl,
1122 prt = pp2_relevance_tokenize(
1123 global_parameters.server->mergekey_pct, (const char *) mergekey);
1126 norm_wr = wrbuf_alloc();
1128 while ((norm_str = pp2_relevance_token_next(prt)))
1132 if (wrbuf_len(norm_wr))
1133 wrbuf_puts(norm_wr, " ");
1134 wrbuf_puts(norm_wr, norm_str);
1138 mergekey_norm = (xmlChar *)nmem_strdup(se->nmem, wrbuf_cstr(norm_wr));
1139 wrbuf_destroy(norm_wr);
1141 pp2_relevance_token_destroy(prt);
1145 cluster = reclist_insert(se->reclist,
1146 global_parameters.server->service,
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 client_get_database(cl)->database->url, record_no);
1154 /* no room for record */
1158 relevance_newrec(se->relevance, cluster);
1161 // now parsing XML record and adding data to cluster or record metadata
1162 for (n = root->children; n; n = n->next)
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)
1192 "Ignoring unknown metadata element: %s", type);
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 rec_md->next = record->metadata[md_field_id];
1213 record->metadata[md_field_id] = rec_md;
1216 rec_md = record_metadata_init(se->nmem, (char *) value,
1218 wheretoput = &cluster->metadata[md_field_id];
1220 // and polulate with data:
1221 // assign cluster or record based on merge action
1222 if (ser_md->merge == Metadata_merge_unique)
1224 struct record_metadata *mnode;
1225 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1226 if (!strcmp((const char *) mnode->data.text.disp,
1227 rec_md->data.text.disp))
1231 rec_md->next = *wheretoput;
1232 *wheretoput = rec_md;
1235 else if (ser_md->merge == Metadata_merge_longest)
1238 || strlen(rec_md->data.text.disp)
1239 > strlen((*wheretoput)->data.text.disp))
1241 *wheretoput = rec_md;
1244 const char *sort_str = 0;
1246 ser_sk->type == Metadata_sortkey_skiparticle;
1248 if (!cluster->sortkeys[sk_field_id])
1249 cluster->sortkeys[sk_field_id] =
1250 nmem_malloc(se->nmem,
1251 sizeof(union data_types));
1253 prt = pp2_relevance_tokenize(
1254 global_parameters.server->sort_pct,
1255 rec_md->data.text.disp);
1257 pp2_relevance_token_next(prt);
1259 sort_str = pp2_get_sort(prt, skip_article);
1261 cluster->sortkeys[sk_field_id]->text.disp =
1262 rec_md->data.text.disp;
1263 cluster->sortkeys[sk_field_id]->text.sort =
1264 nmem_strdup(se->nmem, sort_str);
1266 yaz_log(YLOG_LOG, "text disp=%s",
1267 cluster->sortkeys[sk_field_id]->text.disp);
1268 yaz_log(YLOG_LOG, "text sort=%s",
1269 cluster->sortkeys[sk_field_id]->text.sort);
1271 pp2_relevance_token_destroy(prt);
1275 else if (ser_md->merge == Metadata_merge_all)
1277 rec_md->next = *wheretoput;
1278 *wheretoput = rec_md;
1280 else if (ser_md->merge == Metadata_merge_range)
1284 *wheretoput = rec_md;
1286 cluster->sortkeys[sk_field_id]
1291 int this_min = rec_md->data.number.min;
1292 int this_max = rec_md->data.number.max;
1293 if (this_min < (*wheretoput)->data.number.min)
1294 (*wheretoput)->data.number.min = this_min;
1295 if (this_max > (*wheretoput)->data.number.max)
1296 (*wheretoput)->data.number.max = this_max;
1301 union data_types *sdata
1302 = cluster->sortkeys[sk_field_id];
1303 yaz_log(YLOG_LOG, "SK range: %d-%d",
1304 sdata->number.min, sdata->number.max);
1310 // ranking of _all_ fields enabled ...
1312 relevance_countwords(se->relevance, cluster,
1313 (char *) value, ser_md->rank);
1315 // construct facets ...
1316 if (ser_md->termlist)
1318 if (ser_md->type == Metadata_type_year)
1321 sprintf(year, "%d", rec_md->data.number.max);
1322 add_facet(se, (char *) type, year);
1323 if (rec_md->data.number.max != rec_md->data.number.min)
1325 sprintf(year, "%d", rec_md->data.number.min);
1326 add_facet(se, (char *) type, year);
1330 add_facet(se, (char *) type, (char *) value);
1340 "Unexpected element %s in internal record", n->name);
1349 relevance_donerecord(se->relevance, cluster);
1350 se->total_records++;
1360 * indent-tabs-mode: nil
1362 * vim: shiftwidth=4 tabstop=8 expandtab