1 /* $Id: logic.c,v 1.66 2007-09-10 16:25:50 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 =
101 // Recursively traverse query structure to extract terms.
102 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
114 pull_terms(nmem, n->u.p[0], termlist, num);
115 pull_terms(nmem, n->u.p[1], termlist, num);
118 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
119 for (i = 0; i < numwords; i++)
120 termlist[(*num)++] = words[i];
129 static void add_facet(struct session *s, const char *type, const char *value)
135 for (i = 0; i < s->num_termlists; i++)
136 if (!strcmp(s->termlists[i].name, type))
138 if (i == s->num_termlists)
140 if (i == SESSION_MAX_TERMLISTS)
142 yaz_log(YLOG_FATAL, "Too many termlists");
146 s->termlists[i].name = nmem_strdup(s->nmem, type);
147 s->termlists[i].termlist
148 = termlist_create(s->nmem, s->expected_maxrecs,
149 TERMLIST_HIGH_SCORE);
150 s->num_termlists = i + 1;
152 termlist_insert(s->termlists[i].termlist, value);
155 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
157 struct database *db = sdb->database;
159 const Odr_oid *oid = rec->direct_reference;
161 /* convert response record to XML somehow */
162 if (rec->which == Z_External_octet && oid
163 && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
166 rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
167 rec->u.octet_aligned->len);
170 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
175 else if (rec->which == Z_External_OPAC)
179 yaz_log(YLOG_WARN, "MARC decoding not configured");
184 /* OPAC gets converted to XML too */
185 WRBUF wrbuf_opac = wrbuf_alloc();
186 /* MARCXML inside the OPAC XML. Charset is in effect because we
187 use the yaz_marc handle */
188 yaz_marc_xml(sdb->yaz_marc, YAZ_MARC_MARCXML);
189 yaz_opac_decode_wrbuf(sdb->yaz_marc, rec->u.opac, wrbuf_opac);
191 rdoc = xmlParseMemory((char*) wrbuf_buf(wrbuf_opac),
192 wrbuf_len(wrbuf_opac));
195 yaz_log(YLOG_WARN, "Unable to parse OPAC XML");
196 /* Was used to debug bug #1348 */
198 FILE *f = fopen("/tmp/opac.xml.txt", "wb");
201 fwrite(wrbuf_buf(wrbuf_opac), 1, wrbuf_len(wrbuf_opac), f);
206 wrbuf_destroy(wrbuf_opac);
209 else if (oid && yaz_oid_is_iso2709(oid))
211 /* ISO2709 gets converted to MARCXML */
214 yaz_log(YLOG_WARN, "MARC decoding not configured");
223 if (rec->which != Z_External_octet)
225 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
229 buf = (char*) rec->u.octet_aligned->buf;
230 len = rec->u.octet_aligned->len;
231 if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
233 yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
237 if (yaz_marc_write_xml(sdb->yaz_marc, &res,
238 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
240 yaz_log(YLOG_WARN, "Failed to encode as XML %s",
244 rdoc = xmlNewDoc((xmlChar *) "1.0");
245 xmlDocSetRootElement(rdoc, res);
250 char oid_name_buf[OID_STR_MAX];
251 const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
253 "Unable to handle record of type %s from %s",
258 if (global_parameters.dump_records)
260 FILE *lf = yaz_log_file();
263 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
264 #if LIBXML_VERSION >= 20600
265 xmlDocFormatDump(lf, rdoc, 1);
267 xmlDocDump(lf, rdoc);
275 #define MAX_XSLT_ARGS 16
277 // Add static values from session database settings if applicable
278 static void insert_settings_parameters(struct session_database *sdb,
279 struct session *se, char **parms)
281 struct conf_service *service = global_parameters.server->service;
286 for (i = 0; i < service->num_metadata; i++)
288 struct conf_metadata *md = &service->metadata[i];
291 if (md->setting == Metadata_setting_parameter &&
292 (setting = settings_offset(md->name)) > 0)
294 const char *val = session_setting_oneval(sdb, setting);
295 if (val && nparms < MAX_XSLT_ARGS)
298 int len = strlen(val);
299 buf = nmem_malloc(se->nmem, len + 3);
301 strcpy(buf + 1, val);
304 parms[offset++] = md->name;
305 parms[offset++] = buf;
313 // Add static values from session database settings if applicable
314 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc)
316 struct conf_service *service = global_parameters.server->service;
319 for (i = 0; i < service->num_metadata; i++)
321 struct conf_metadata *md = &service->metadata[i];
324 if (md->setting == Metadata_setting_postproc &&
325 (offset = settings_offset(md->name)) > 0)
327 const char *val = session_setting_oneval(sdb, offset);
330 xmlNode *r = xmlDocGetRootElement(doc);
331 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
333 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
339 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
342 struct database_retrievalmap *m;
343 xmlDoc *rdoc = record_to_xml(sdb, rec);
346 for (m = sdb->map; m; m = m->next)
352 char *parms[MAX_XSLT_ARGS*2+1];
354 insert_settings_parameters(sdb, se, parms);
356 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
357 root= xmlDocGetRootElement(new);
358 if (!new || !root || !(root->children))
360 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
372 insert_settings_values(sdb, rdoc);
374 if (global_parameters.dump_records)
376 FILE *lf = yaz_log_file();
380 yaz_log(YLOG_LOG, "Normalized record from %s",
382 #if LIBXML_VERSION >= 20600
383 xmlDocFormatDump(lf, rdoc, 1);
385 xmlDocDump(lf, rdoc);
394 // Retrieve first defined value for 'name' for given database.
395 // Will be extended to take into account user associated with session
396 const char *session_setting_oneval(struct session_database *db, int offset)
398 if (!db->settings[offset])
400 return db->settings[offset]->value;
405 // Initialize YAZ Map structures for MARC-based targets
406 static int prepare_yazmarc(struct session_database *sdb)
412 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
415 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX))
416 && !strncmp(s, "iso2709", 7))
418 char *encoding = "marc-8s", *e;
421 // See if a native encoding is specified
422 if ((e = strchr(s, ';')))
425 sdb->yaz_marc = yaz_marc_create();
426 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
428 cm = yaz_iconv_open("utf-8", encoding);
432 "Unable to map from %s to UTF-8 for target %s",
433 encoding, sdb->database->url);
436 yaz_marc_iconv(sdb->yaz_marc, cm);
441 // Prepare XSLT stylesheets for record normalization
442 // Structures are allocated on the session_wide nmem to avoid having
443 // to recompute this for every search. This would lead
444 // to leaking if a single session was to repeatedly change the PZ_XSLT
445 // setting. However, this is not a realistic use scenario.
446 static int prepare_map(struct session *se, struct session_database *sdb)
452 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
455 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
458 struct database_retrievalmap **m = &sdb->map;
460 char auto_stylesheet[256];
462 if (!strcmp(s, "auto"))
464 const char *request_syntax = session_setting_oneval(
465 sdb, PZ_REQUESTSYNTAX);
469 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
470 "%s.xsl", request_syntax);
471 for (cp = auto_stylesheet; *cp; cp++)
473 /* deliberately only consider ASCII */
474 if (*cp > 32 && *cp < 127)
481 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
484 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
485 for (i = 0; i < num; i++)
487 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
489 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
491 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
499 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
504 // This analyzes settings and recomputes any supporting data structures
506 static int prepare_session_database(struct session *se,
507 struct session_database *sdb)
512 "No settings associated with %s", sdb->database->url);
515 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
517 if (prepare_yazmarc(sdb) < 0)
520 if (sdb->settings[PZ_XSLT] && !sdb->map)
522 if (prepare_map(se, sdb) < 0)
528 // called if watch should be removed because http_channel is to be destroyed
529 static void session_watch_cancel(void *data, struct http_channel *c)
531 struct session_watchentry *ent = data;
538 // set watch. Returns 0=OK, -1 if watch is already set
539 int session_set_watch(struct session *s, int what,
540 session_watchfun fun, void *data,
541 struct http_channel *chan)
543 if (s->watchlist[what].fun)
545 s->watchlist[what].fun = fun;
546 s->watchlist[what].data = data;
547 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
548 session_watch_cancel);
552 void session_alert_watch(struct session *s, int what)
554 if (s->watchlist[what].fun)
556 /* our watch is no longer associated with http_channel */
557 http_remove_observer(s->watchlist[what].obs);
558 session_watchfun fun = s->watchlist[what].fun;
559 void *data = s->watchlist[what].data;
561 /* reset watch before fun is invoked - in case fun wants to set
563 s->watchlist[what].fun = 0;
564 s->watchlist[what].data = 0;
565 s->watchlist[what].obs = 0;
571 //callback for grep_databases
572 static void select_targets_callback(void *context, struct session_database *db)
574 struct session *se = (struct session*) context;
575 struct client *cl = client_create();
576 client_set_database(cl, db);
577 client_set_session(cl, se);
580 // Associates a set of clients with a session;
581 // Note: Session-databases represent databases with per-session
583 int select_targets(struct session *se, struct database_criterion *crit)
586 client_destroy(se->clients);
588 return session_grep_databases(se, crit, select_targets_callback);
591 int session_active_clients(struct session *s)
596 for (c = s->clients; c; c = client_next_in_session(c))
597 if (client_is_active(c))
603 // parses crit1=val1,crit2=val2|val3,...
604 static struct database_criterion *parse_filter(NMEM m, const char *buf)
606 struct database_criterion *res = 0;
613 nmem_strsplit(m, ",", buf, &values, &num);
614 for (i = 0; i < num; i++)
619 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
620 char *eq = strchr(values[i], '=');
623 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
627 new->name = values[i];
628 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
630 for (subi = 0; subi < subnum; subi++)
632 struct database_criterion_value *newv
633 = nmem_malloc(m, sizeof(*newv));
634 newv->value = subvalues[subi];
635 newv->next = new->values;
644 enum pazpar2_error_code search(struct session *se,
645 char *query, char *filter,
646 const char **addinfo)
648 int live_channels = 0;
652 struct database_criterion *criteria;
654 yaz_log(YLOG_DEBUG, "Search");
657 nmem_reset(se->nmem);
659 se->total_records = se->total_hits = se->total_merged = 0;
661 se->num_termlists = 0;
662 criteria = parse_filter(se->nmem, filter);
664 live_channels = select_targets(se, criteria);
667 int maxrecs = live_channels * global_parameters.toget;
668 se->reclist = reclist_create(se->nmem, maxrecs);
669 se->expected_maxrecs = maxrecs;
672 return PAZPAR2_NO_TARGETS;
674 for (cl = se->clients; cl; cl = client_next_in_session(cl))
676 if (prepare_session_database(se, client_get_database(cl)) < 0)
678 *addinfo = client_get_database(cl)->database->url;
679 return PAZPAR2_CONFIG_TARGET;
681 // Parse query for target
682 if (client_parse_query(cl, query) < 0)
687 client_prep_connection(cl);
691 // If no queries could be mapped, we signal an error
695 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
697 return PAZPAR2_NO_ERROR;
700 // Creates a new session_database object for a database
701 static void session_init_databases_fun(void *context, struct database *db)
703 struct session *se = (struct session *) context;
704 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
705 int num = settings_num();
713 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
714 memset(new->settings, 0, sizeof(struct settings*) * num);
718 for (i = 0; i < num; i++)
719 new->settings[i] = db->settings[i];
721 new->next = se->databases;
725 // Doesn't free memory associated with sdb -- nmem takes care of that
726 static void session_database_destroy(struct session_database *sdb)
728 struct database_retrievalmap *m;
730 for (m = sdb->map; m; m = m->next)
731 xsltFreeStylesheet(m->stylesheet);
733 yaz_marc_destroy(sdb->yaz_marc);
736 // Initialize session_database list -- this represents this session's view
737 // of the database list -- subject to modification by the settings ws command
738 void session_init_databases(struct session *se)
741 predef_grep_databases(se, 0, session_init_databases_fun);
744 // Probably session_init_databases_fun should be refactored instead of
746 static struct session_database *load_session_database(struct session *se,
749 struct database *db = find_database(id, 0);
751 session_init_databases_fun((void*) se, db);
752 // New sdb is head of se->databases list
753 return se->databases;
756 // Find an existing session database. If not found, load it
757 static struct session_database *find_session_database(struct session *se,
760 struct session_database *sdb;
762 for (sdb = se->databases; sdb; sdb = sdb->next)
763 if (!strcmp(sdb->database->url, id))
765 return load_session_database(se, id);
768 // Apply a session override to a database
769 void session_apply_setting(struct session *se, char *dbname, char *setting,
772 struct session_database *sdb = find_session_database(se, dbname);
773 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
774 int offset = settings_offset_cprefix(setting);
778 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
781 // Jakub: This breaks the filter setting.
782 /*if (offset == PZ_ID)
784 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
788 new->target = dbname;
791 new->next = sdb->settings[offset];
792 sdb->settings[offset] = new;
794 // Force later recompute of settings-driven data structures
795 // (happens when a search starts and client connections are prepared)
798 case PZ_NATIVESYNTAX:
801 yaz_marc_destroy(sdb->yaz_marc);
808 struct database_retrievalmap *m;
809 // We don't worry about the map structure -- it's in nmem
810 for (m = sdb->map; m; m = m->next)
811 xsltFreeStylesheet(m->stylesheet);
818 void destroy_session(struct session *s)
820 struct session_database *sdb;
823 client_destroy(s->clients);
824 for (sdb = s->databases; sdb; sdb = sdb->next)
825 session_database_destroy(sdb);
826 nmem_destroy(s->nmem);
827 wrbuf_destroy(s->wrbuf);
830 struct session *new_session(NMEM nmem)
833 struct session *session = nmem_malloc(nmem, sizeof(*session));
835 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
837 session->relevance = 0;
838 session->total_hits = 0;
839 session->total_records = 0;
840 session->num_termlists = 0;
841 session->reclist = 0;
842 session->requestid = -1;
843 session->clients = 0;
844 session->expected_maxrecs = 0;
845 session->session_nmem = nmem;
846 session->nmem = nmem_create();
847 session->wrbuf = wrbuf_alloc();
848 session->databases = 0;
849 for (i = 0; i <= SESSION_WATCH_MAX; i++)
851 session->watchlist[i].data = 0;
852 session->watchlist[i].fun = 0;
857 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
859 struct hitsbytarget *res = 0;
863 for (cl = se->clients; cl; cl = client_next_in_session(cl))
866 res = nmem_malloc(nmem, sizeof(*res) * sz);
868 for (cl = se->clients; cl; cl = client_next_in_session(cl))
870 const char *name = session_setting_oneval(client_get_database(cl),
873 res[*count].id = client_get_database(cl)->database->url;
874 res[*count].name = *name ? name : "Unknown";
875 res[*count].hits = client_get_hits(cl);
876 res[*count].records = client_get_num_records(cl);
877 res[*count].diagnostic = client_get_diagnostic(cl);
878 res[*count].state = client_get_state_str(cl);
879 res[*count].connected = client_get_connection(cl) ? 1 : 0;
885 struct termlist_score **termlist(struct session *s, const char *name, int *num)
889 for (i = 0; i < s->num_termlists; i++)
890 if (!strcmp((const char *) s->termlists[i].name, name))
891 return termlist_highscore(s->termlists[i].termlist, num);
895 #ifdef MISSING_HEADERS
896 void report_nmem_stats(void)
898 size_t in_use, is_free;
900 nmem_get_memory_in_use(&in_use);
901 nmem_get_memory_free(&is_free);
903 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
904 (long) in_use, (long) is_free);
908 struct record_cluster *show_single(struct session *s, const char *id)
910 struct record_cluster *r;
912 reclist_rewind(s->reclist);
913 while ((r = reclist_read_record(s->reclist)))
914 if (!strcmp(r->recid, id))
919 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
920 int start, int *num, int *total, int *sumhits,
923 struct record_cluster **recs = nmem_malloc(nmem_show, *num
924 * sizeof(struct record_cluster *));
925 struct reclist_sortparms *spp;
928 yaz_timing_t t = yaz_timing_create();
940 for (spp = sp; spp; spp = spp->next)
941 if (spp->type == Metadata_sortkey_relevance)
943 relevance_prepare_read(s->relevance, s->reclist);
946 reclist_sort(s->reclist, sp);
948 *total = s->reclist->num_records;
949 *sumhits = s->total_hits;
951 for (i = 0; i < start; i++)
952 if (!reclist_read_record(s->reclist))
959 for (i = 0; i < *num; i++)
961 struct record_cluster *r = reclist_read_record(s->reclist);
972 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
973 yaz_timing_get_real(t), yaz_timing_get_user(t),
974 yaz_timing_get_sys(t));
975 yaz_timing_destroy(&t);
980 void statistics(struct session *se, struct statistics *stat)
985 memset(stat, 0, sizeof(*stat));
986 for (cl = se->clients; cl; cl = client_next_in_session(cl))
988 if (!client_get_connection(cl))
989 stat->num_no_connection++;
990 switch (client_get_state(cl))
992 case Client_Connecting: stat->num_connecting++; break;
993 case Client_Initializing: stat->num_initializing++; break;
994 case Client_Searching: stat->num_searching++; break;
995 case Client_Presenting: stat->num_presenting++; break;
996 case Client_Idle: stat->num_idle++; break;
997 case Client_Failed: stat->num_failed++; break;
998 case Client_Error: stat->num_error++; break;
1003 stat->num_hits = se->total_hits;
1004 stat->num_records = se->total_records;
1006 stat->num_clients = count;
1009 void start_http_listener(void)
1012 struct conf_server *ser = global_parameters.server;
1014 if (*global_parameters.listener_override)
1015 strcpy(hp, global_parameters.listener_override);
1018 strcpy(hp, ser->host ? ser->host : "");
1023 sprintf(hp + strlen(hp), "%d", ser->port);
1029 void start_proxy(void)
1032 struct conf_server *ser = global_parameters.server;
1034 if (*global_parameters.proxy_override)
1035 strcpy(hp, global_parameters.proxy_override);
1036 else if (ser->proxy_host || ser->proxy_port)
1038 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
1039 if (ser->proxy_port)
1043 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
1049 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
1053 // Master list of connections we're handling events to
1054 static IOCHAN channel_list = 0;
1055 void pazpar2_add_channel(IOCHAN chan)
1057 chan->next = channel_list;
1058 channel_list = chan;
1061 void pazpar2_event_loop()
1063 event_loop(&channel_list);
1066 static struct record_metadata *record_metadata_init(
1067 NMEM nmem, char *value, enum conf_metadata_type type)
1069 struct record_metadata *rec_md = record_metadata_create(nmem);
1070 if (type == Metadata_type_generic)
1073 p = normalize7bit_generic(p, " ,/.:([");
1075 rec_md->data.text.disp = nmem_strdup(nmem, p);
1076 rec_md->data.text.sort = 0;
1078 else if (type == Metadata_type_year)
1081 if (extract7bit_years((char *) value, &first, &last) < 0)
1083 rec_md->data.number.min = first;
1084 rec_md->data.number.max = last;
1091 struct record *ingest_record(struct client *cl, Z_External *rec,
1094 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1095 client_get_session(cl), rec);
1097 struct record *record;
1098 struct record_cluster *cluster;
1099 struct session *se = client_get_session(cl);
1100 xmlChar *mergekey, *mergekey_norm;
1103 struct conf_service *service = global_parameters.server->service;
1104 const char *norm_str = 0;
1105 pp2_relevance_token_t prt = 0;
1111 root = xmlDocGetRootElement(xdoc);
1112 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1114 yaz_log(YLOG_WARN, "No mergekey found in record");
1119 record = record_create(se->nmem,
1120 service->num_metadata, service->num_sortkeys, cl,
1123 prt = pp2_relevance_tokenize(
1124 global_parameters.server->mergekey_pct, (const char *) mergekey);
1127 norm_wr = wrbuf_alloc();
1129 while ((norm_str = pp2_relevance_token_next(prt)))
1133 if (wrbuf_len(norm_wr))
1134 wrbuf_puts(norm_wr, " ");
1135 wrbuf_puts(norm_wr, norm_str);
1139 mergekey_norm = (xmlChar *)nmem_strdup(se->nmem, wrbuf_cstr(norm_wr));
1140 wrbuf_destroy(norm_wr);
1142 pp2_relevance_token_destroy(prt);
1146 cluster = reclist_insert(se->reclist,
1147 global_parameters.server->service,
1148 record, (char *) mergekey_norm,
1150 if (global_parameters.dump_records)
1151 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1152 client_get_database(cl)->database->url, record_no);
1155 /* no room for record */
1159 relevance_newrec(se->relevance, cluster);
1162 // now parsing XML record and adding data to cluster or record metadata
1163 for (n = root->children; n; n = n->next)
1171 if (n->type != XML_ELEMENT_NODE)
1173 if (!strcmp((const char *) n->name, "metadata"))
1175 struct conf_metadata *ser_md = 0;
1176 struct conf_sortkey *ser_sk = 0;
1177 struct record_metadata **wheretoput = 0;
1178 struct record_metadata *rec_md = 0;
1179 int md_field_id = -1;
1180 int sk_field_id = -1;
1182 type = xmlGetProp(n, (xmlChar *) "type");
1183 value = xmlNodeListGetString(xdoc, n->children, 1);
1185 if (!type || !value || !*value)
1189 = conf_service_metadata_field_id(service, (const char *) type);
1190 if (md_field_id < 0)
1193 "Ignoring unknown metadata element: %s", type);
1197 ser_md = &service->metadata[md_field_id];
1199 if (ser_md->sortkey_offset >= 0){
1200 sk_field_id = ser_md->sortkey_offset;
1201 ser_sk = &service->sortkeys[sk_field_id];
1204 // non-merged metadata
1205 rec_md = record_metadata_init(se->nmem, (char *) value,
1209 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1213 rec_md->next = record->metadata[md_field_id];
1214 record->metadata[md_field_id] = rec_md;
1217 rec_md = record_metadata_init(se->nmem, (char *) value,
1219 wheretoput = &cluster->metadata[md_field_id];
1221 // and polulate with data:
1222 // assign cluster or record based on merge action
1223 if (ser_md->merge == Metadata_merge_unique)
1225 struct record_metadata *mnode;
1226 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1227 if (!strcmp((const char *) mnode->data.text.disp,
1228 rec_md->data.text.disp))
1232 rec_md->next = *wheretoput;
1233 *wheretoput = rec_md;
1236 else if (ser_md->merge == Metadata_merge_longest)
1239 || strlen(rec_md->data.text.disp)
1240 > strlen((*wheretoput)->data.text.disp))
1242 *wheretoput = rec_md;
1245 const char *sort_str = 0;
1247 ser_sk->type == Metadata_sortkey_skiparticle;
1249 if (!cluster->sortkeys[sk_field_id])
1250 cluster->sortkeys[sk_field_id] =
1251 nmem_malloc(se->nmem,
1252 sizeof(union data_types));
1254 prt = pp2_relevance_tokenize(
1255 global_parameters.server->sort_pct,
1256 rec_md->data.text.disp);
1258 pp2_relevance_token_next(prt);
1260 sort_str = pp2_get_sort(prt, skip_article);
1262 cluster->sortkeys[sk_field_id]->text.disp =
1263 rec_md->data.text.disp;
1264 cluster->sortkeys[sk_field_id]->text.sort =
1265 nmem_strdup(se->nmem, sort_str);
1267 yaz_log(YLOG_LOG, "text disp=%s",
1268 cluster->sortkeys[sk_field_id]->text.disp);
1269 yaz_log(YLOG_LOG, "text sort=%s",
1270 cluster->sortkeys[sk_field_id]->text.sort);
1272 pp2_relevance_token_destroy(prt);
1276 else if (ser_md->merge == Metadata_merge_all)
1278 rec_md->next = *wheretoput;
1279 *wheretoput = rec_md;
1281 else if (ser_md->merge == Metadata_merge_range)
1285 *wheretoput = rec_md;
1287 cluster->sortkeys[sk_field_id]
1292 int this_min = rec_md->data.number.min;
1293 int this_max = rec_md->data.number.max;
1294 if (this_min < (*wheretoput)->data.number.min)
1295 (*wheretoput)->data.number.min = this_min;
1296 if (this_max > (*wheretoput)->data.number.max)
1297 (*wheretoput)->data.number.max = this_max;
1302 union data_types *sdata
1303 = cluster->sortkeys[sk_field_id];
1304 yaz_log(YLOG_LOG, "SK range: %d-%d",
1305 sdata->number.min, sdata->number.max);
1311 // ranking of _all_ fields enabled ...
1313 relevance_countwords(se->relevance, cluster,
1314 (char *) value, ser_md->rank);
1316 // construct facets ...
1317 if (ser_md->termlist)
1319 if (ser_md->type == Metadata_type_year)
1322 sprintf(year, "%d", rec_md->data.number.max);
1323 add_facet(se, (char *) type, year);
1324 if (rec_md->data.number.max != rec_md->data.number.min)
1326 sprintf(year, "%d", rec_md->data.number.min);
1327 add_facet(se, (char *) type, year);
1331 add_facet(se, (char *) type, (char *) value);
1341 "Unexpected element %s in internal record", n->name);
1350 relevance_donerecord(se->relevance, cluster);
1351 se->total_records++;
1361 * indent-tabs-mode: nil
1363 * vim: shiftwidth=4 tabstop=8 expandtab