1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 \brief high-level logic; mostly user sessions and settings
41 #include <yaz/marcdisp.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include <yaz/proto.h>
45 #include <yaz/readconf.h>
46 #include <yaz/pquery.h>
47 #include <yaz/otherinfo.h>
48 #include <yaz/yaz-util.h>
50 #include <yaz/query-charset.h>
51 #include <yaz/querytowrbuf.h>
52 #include <yaz/oid_db.h>
53 #include <yaz/snprintf.h>
57 #include <yaz/timing.h>
61 #include "parameters.h"
65 #include "termlists.h"
67 #include "relevance.h"
71 #include "normalize7bit.h"
73 #define TERMLIST_HIGH_SCORE 25
77 // Note: Some things in this structure will eventually move to configuration
78 struct parameters global_parameters =
85 struct client *client;
86 struct client_list *next;
89 static void log_xml_doc(xmlDoc *doc)
91 FILE *lf = yaz_log_file();
94 #if LIBXML_VERSION >= 20600
95 xmlDocDumpFormatMemory(doc, &result, &len, 1);
97 xmlDocDumpMemory(doc, &result, &len);
101 (void) fwrite(result, 1, len, lf);
107 static void session_enter(struct session *s)
109 yaz_mutex_enter(s->session_mutex);
112 static void session_leave(struct session *s)
114 yaz_mutex_leave(s->session_mutex);
117 // Recursively traverse query structure to extract terms.
118 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
130 pull_terms(nmem, n->u.p[0], termlist, num);
131 pull_terms(nmem, n->u.p[1], termlist, num);
134 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
135 for (i = 0; i < numwords; i++)
136 termlist[(*num)++] = words[i];
144 static void add_facet(struct session *s, const char *type, const char *value)
150 for (i = 0; i < s->num_termlists; i++)
151 if (!strcmp(s->termlists[i].name, type))
153 if (i == s->num_termlists)
155 if (i == SESSION_MAX_TERMLISTS)
157 yaz_log(YLOG_FATAL, "Too many termlists");
161 s->termlists[i].name = nmem_strdup(s->nmem, type);
162 s->termlists[i].termlist
163 = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
164 s->num_termlists = i + 1;
166 termlist_insert(s->termlists[i].termlist, value);
169 static xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
171 struct database *db = sdb->database;
174 rdoc = xmlParseMemory(rec, strlen(rec));
178 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
183 if (global_parameters.dump_records)
185 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
192 #define MAX_XSLT_ARGS 16
194 // Add static values from session database settings if applicable
195 static void insert_settings_parameters(struct session_database *sdb,
196 struct conf_service *service,
204 for (i = 0; i < service->num_metadata; i++)
206 struct conf_metadata *md = &service->metadata[i];
209 if (md->setting == Metadata_setting_parameter &&
210 (setting = settings_lookup_offset(service, md->name)) >= 0)
212 const char *val = session_setting_oneval(sdb, setting);
213 if (val && nparms < MAX_XSLT_ARGS)
216 int len = strlen(val);
217 buf = nmem_malloc(nmem, len + 3);
219 strcpy(buf + 1, val);
222 parms[offset++] = md->name;
223 parms[offset++] = buf;
231 // Add static values from session database settings if applicable
232 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
233 struct conf_service *service)
237 for (i = 0; i < service->num_metadata; i++)
239 struct conf_metadata *md = &service->metadata[i];
242 if (md->setting == Metadata_setting_postproc &&
243 (offset = settings_lookup_offset(service, md->name)) >= 0)
245 const char *val = session_setting_oneval(sdb, offset);
248 xmlNode *r = xmlDocGetRootElement(doc);
249 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
251 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
257 static xmlDoc *normalize_record(struct session_database *sdb,
258 struct conf_service *service,
259 const char *rec, NMEM nmem)
261 xmlDoc *rdoc = record_to_xml(sdb, rec);
265 char *parms[MAX_XSLT_ARGS*2+1];
267 insert_settings_parameters(sdb, service, parms, nmem);
269 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
271 yaz_log(YLOG_WARN, "Normalize failed from %s", sdb->database->url);
275 insert_settings_values(sdb, rdoc, service);
277 if (global_parameters.dump_records)
279 yaz_log(YLOG_LOG, "Normalized record from %s",
288 void session_settings_dump(struct session *se,
289 struct session_database *db,
294 int i, num = db->num_settings;
295 for (i = 0; i < num; i++)
297 struct setting *s = db->settings[i];
298 for (;s ; s = s->next)
300 wrbuf_puts(w, "<set name=\"");
301 wrbuf_xmlputs(w, s->name);
302 wrbuf_puts(w, "\" value=\"");
303 wrbuf_xmlputs(w, s->value);
304 wrbuf_puts(w, "\"/>");
312 // Retrieve first defined value for 'name' for given database.
313 // Will be extended to take into account user associated with session
314 const char *session_setting_oneval(struct session_database *db, int offset)
316 if (offset >= db->num_settings || !db->settings[offset])
318 return db->settings[offset]->value;
321 // Prepare XSLT stylesheets for record normalization
322 // Structures are allocated on the session_wide nmem to avoid having
323 // to recompute this for every search. This would lead
324 // to leaking if a single session was to repeatedly change the PZ_XSLT
325 // setting. However, this is not a realistic use scenario.
326 static int prepare_map(struct session *se, struct session_database *sdb)
332 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
335 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
337 char auto_stylesheet[256];
339 if (!strcmp(s, "auto"))
341 const char *request_syntax = session_setting_oneval(
342 sdb, PZ_REQUESTSYNTAX);
346 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
347 "%s.xsl", request_syntax);
348 for (cp = auto_stylesheet; *cp; cp++)
350 /* deliberately only consider ASCII */
351 if (*cp > 32 && *cp < 127)
358 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
361 sdb->map = normalize_cache_get(se->normalize_cache,
369 // This analyzes settings and recomputes any supporting data structures
371 static int prepare_session_database(struct session *se,
372 struct session_database *sdb)
377 "No settings associated with %s", sdb->database->url);
380 if (sdb->settings[PZ_XSLT] && !sdb->map)
382 if (prepare_map(se, sdb) < 0)
388 // called if watch should be removed because http_channel is to be destroyed
389 static void session_watch_cancel(void *data, struct http_channel *c,
392 struct session_watchentry *ent = data;
399 // set watch. Returns 0=OK, -1 if watch is already set
400 int session_set_watch(struct session *s, int what,
401 session_watchfun fun, void *data,
402 struct http_channel *chan)
406 if (s->watchlist[what].fun)
411 s->watchlist[what].fun = fun;
412 s->watchlist[what].data = data;
413 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
414 session_watch_cancel);
421 void session_alert_watch(struct session *s, int what)
425 if (s->watchlist[what].fun)
427 /* our watch is no longer associated with http_channel */
429 session_watchfun fun;
431 http_remove_observer(s->watchlist[what].obs);
432 fun = s->watchlist[what].fun;
433 data = s->watchlist[what].data;
435 /* reset watch before fun is invoked - in case fun wants to set
437 s->watchlist[what].fun = 0;
438 s->watchlist[what].data = 0;
439 s->watchlist[what].obs = 0;
448 //callback for grep_databases
449 static void select_targets_callback(void *context, struct session_database *db)
451 struct session *se = (struct session*) context;
452 struct client *cl = client_create();
453 struct client_list *l;
454 client_set_database(cl, db);
456 client_set_session(cl, se);
457 l = xmalloc(sizeof(*l));
459 l->next = se->clients;
463 static void session_remove_clients(struct session *se)
465 struct client_list *l = se->clients;
468 struct client_list *l_next = l->next;
469 client_set_session(l->client, 0);
470 client_destroy(l->client);
477 // Associates a set of clients with a session;
478 // Note: Session-databases represent databases with per-session
480 static int select_targets(struct session *se, const char *filter)
482 session_remove_clients(se);
483 return session_grep_databases(se, filter, select_targets_callback);
486 int session_active_clients(struct session *s)
488 struct client_list *l;
491 for (l = s->clients; l; l = l->next)
492 if (client_is_active(l->client))
499 enum pazpar2_error_code search(struct session *se,
501 const char *startrecs, const char *maxrecs,
503 const char **addinfo)
505 int live_channels = 0;
508 struct client_list *l;
510 yaz_log(YLOG_DEBUG, "Search");
515 reclist_destroy(se->reclist);
517 nmem_reset(se->nmem);
518 relevance_destroy(&se->relevance);
519 se->total_records = se->total_hits = se->total_merged = 0;
520 se->num_termlists = 0;
521 live_channels = select_targets(se, filter);
525 return PAZPAR2_NO_TARGETS;
527 se->reclist = reclist_create(se->nmem);
529 for (l = se->clients; l; l = l->next)
531 struct client *cl = l->client;
533 client_set_maxrecs(cl, atoi(maxrecs));
535 client_set_startrecs(cl, atoi(startrecs));
536 if (prepare_session_database(se, client_get_database(cl)) < 0)
538 // Parse query for target
539 if (client_parse_query(cl, query) < 0)
544 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
545 se->service->z3950_session_timeout,
546 se->service->server->iochan_man))
547 client_start_search(cl);
556 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
559 return PAZPAR2_NO_TARGETS;
561 return PAZPAR2_NO_ERROR;
564 // Creates a new session_database object for a database
565 static void session_init_databases_fun(void *context, struct database *db)
567 struct session *se = (struct session *) context;
568 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
574 assert(db->settings);
575 new->settings = nmem_malloc(se->session_nmem,
576 sizeof(struct settings *) * db->num_settings);
577 new->num_settings = db->num_settings;
578 for (i = 0; i < db->num_settings; i++)
580 struct setting *setting = db->settings[i];
581 new->settings[i] = setting;
583 new->next = se->databases;
587 // Doesn't free memory associated with sdb -- nmem takes care of that
588 static void session_database_destroy(struct session_database *sdb)
593 // Initialize session_database list -- this represents this session's view
594 // of the database list -- subject to modification by the settings ws command
595 void session_init_databases(struct session *se)
598 predef_grep_databases(se, se->service, session_init_databases_fun);
601 // Probably session_init_databases_fun should be refactored instead of
603 static struct session_database *load_session_database(struct session *se,
606 struct database *db = new_database(id, se->session_nmem);
608 resolve_database(se->service, db);
610 session_init_databases_fun((void*) se, db);
612 // New sdb is head of se->databases list
613 return se->databases;
616 // Find an existing session database. If not found, load it
617 static struct session_database *find_session_database(struct session *se,
620 struct session_database *sdb;
622 for (sdb = se->databases; sdb; sdb = sdb->next)
623 if (!strcmp(sdb->database->url, id))
625 return load_session_database(se, id);
628 // Apply a session override to a database
629 void session_apply_setting(struct session *se, char *dbname, char *setting,
632 struct session_database *sdb = find_session_database(se, dbname);
633 struct conf_service *service = se->service;
634 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
635 int offset = settings_create_offset(service, setting);
637 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
640 new->target = dbname;
643 new->next = sdb->settings[offset];
644 sdb->settings[offset] = new;
646 // Force later recompute of settings-driven data structures
647 // (happens when a search starts and client connections are prepared)
659 void destroy_session(struct session *s)
661 struct session_database *sdb;
663 session_remove_clients(s);
665 for (sdb = s->databases; sdb; sdb = sdb->next)
666 session_database_destroy(sdb);
667 normalize_cache_destroy(s->normalize_cache);
668 relevance_destroy(&s->relevance);
669 reclist_destroy(s->reclist);
670 nmem_destroy(s->nmem);
671 service_destroy(s->service);
672 yaz_mutex_destroy(&s->session_mutex);
673 wrbuf_destroy(s->wrbuf);
676 struct session *new_session(NMEM nmem, struct conf_service *service,
680 struct session *session = nmem_malloc(nmem, sizeof(*session));
682 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
684 session->service = service;
685 session->relevance = 0;
686 session->total_hits = 0;
687 session->total_records = 0;
688 session->number_of_warnings_unknown_elements = 0;
689 session->number_of_warnings_unknown_metadata = 0;
690 session->num_termlists = 0;
691 session->reclist = 0;
692 session->clients = 0;
693 session->session_nmem = nmem;
694 session->nmem = nmem_create();
695 session->wrbuf = wrbuf_alloc();
696 session->databases = 0;
697 for (i = 0; i <= SESSION_WATCH_MAX; i++)
699 session->watchlist[i].data = 0;
700 session->watchlist[i].fun = 0;
702 session->normalize_cache = normalize_cache_create();
703 session->session_mutex = 0;
704 pazpar2_mutex_create(&session->session_mutex, name);
709 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
711 struct hitsbytarget *res = 0;
712 struct client_list *l;
716 for (l = se->clients; l; l = l->next)
719 res = nmem_malloc(nmem, sizeof(*res) * sz);
721 for (l = se->clients; l; l = l->next)
723 struct client *cl = l->client;
724 WRBUF w = wrbuf_alloc();
725 const char *name = session_setting_oneval(client_get_database(cl),
728 res[*count].id = client_get_database(cl)->database->url;
729 res[*count].name = *name ? name : "Unknown";
730 res[*count].hits = client_get_hits(cl);
731 res[*count].records = client_get_num_records(cl);
732 res[*count].diagnostic = client_get_diagnostic(cl);
733 res[*count].state = client_get_state_str(cl);
734 res[*count].connected = client_get_connection(cl) ? 1 : 0;
735 session_settings_dump(se, client_get_database(cl), w);
736 res[*count].settings_xml = w;
743 struct termlist_score **termlist(struct session *s, const char *name, int *num)
746 struct termlist_score **tl = 0;
749 for (i = 0; i < s->num_termlists; i++)
750 if (!strcmp((const char *) s->termlists[i].name, name))
752 tl = termlist_highscore(s->termlists[i].termlist, num);
759 #ifdef MISSING_HEADERS
760 void report_nmem_stats(void)
762 size_t in_use, is_free;
764 nmem_get_memory_in_use(&in_use);
765 nmem_get_memory_free(&is_free);
767 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
768 (long) in_use, (long) is_free);
772 struct record_cluster *show_single_start(struct session *s, const char *id,
773 struct record_cluster **prev_r,
774 struct record_cluster **next_r)
776 struct record_cluster *r;
779 reclist_enter(s->reclist);
782 while ((r = reclist_read_record(s->reclist)))
784 if (!strcmp(r->recid, id))
786 *next_r = reclist_read_record(s->reclist);
791 reclist_leave(s->reclist);
797 void show_single_stop(struct session *s, struct record_cluster *rec)
802 struct record_cluster **show_range_start(struct session *s,
803 struct reclist_sortparms *sp,
804 int start, int *num, int *total, Odr_int *sumhits)
806 struct record_cluster **recs = nmem_malloc(s->nmem, *num
807 * sizeof(struct record_cluster *));
808 struct reclist_sortparms *spp;
811 yaz_timing_t t = yaz_timing_create();
824 for (spp = sp; spp; spp = spp->next)
825 if (spp->type == Metadata_sortkey_relevance)
827 relevance_prepare_read(s->relevance, s->reclist);
830 reclist_sort(s->reclist, sp);
832 reclist_enter(s->reclist);
833 *total = reclist_get_num_records(s->reclist);
834 *sumhits = s->total_hits;
836 for (i = 0; i < start; i++)
837 if (!reclist_read_record(s->reclist))
844 for (i = 0; i < *num; i++)
846 struct record_cluster *r = reclist_read_record(s->reclist);
854 reclist_leave(s->reclist);
858 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
859 yaz_timing_get_real(t), yaz_timing_get_user(t),
860 yaz_timing_get_sys(t));
861 yaz_timing_destroy(&t);
866 void show_range_stop(struct session *s, struct record_cluster **recs)
871 void statistics(struct session *se, struct statistics *stat)
873 struct client_list *l;
876 memset(stat, 0, sizeof(*stat));
877 for (l = se->clients; l; l = l->next)
879 struct client *cl = l->client;
880 if (!client_get_connection(cl))
881 stat->num_no_connection++;
882 switch (client_get_state(cl))
884 case Client_Connecting: stat->num_connecting++; break;
885 case Client_Working: stat->num_working++; break;
886 case Client_Idle: stat->num_idle++; break;
887 case Client_Failed: stat->num_failed++; break;
888 case Client_Error: stat->num_error++; break;
893 stat->num_hits = se->total_hits;
894 stat->num_records = se->total_records;
896 stat->num_clients = count;
899 static struct record_metadata *record_metadata_init(
900 NMEM nmem, const char *value, enum conf_metadata_type type,
901 struct _xmlAttr *attr)
903 struct record_metadata *rec_md = record_metadata_create(nmem);
904 struct record_metadata_attr **attrp = &rec_md->attributes;
906 for (; attr; attr = attr->next)
908 if (attr->children && attr->children->content)
910 if (strcmp((const char *) attr->name, "type"))
911 { /* skip the "type" attribute.. Its value is already part of
912 the element in output (md-%s) and so repeating it here
914 *attrp = nmem_malloc(nmem, sizeof(**attrp));
916 nmem_strdup(nmem, (const char *) attr->name);
918 nmem_strdup(nmem, (const char *) attr->children->content);
919 attrp = &(*attrp)->next;
925 if (type == Metadata_type_generic)
927 char *p = nmem_strdup(nmem, value);
929 p = normalize7bit_generic(p, " ,/.:([");
931 rec_md->data.text.disp = p;
932 rec_md->data.text.sort = 0;
934 else if (type == Metadata_type_year || type == Metadata_type_date)
939 if (type == Metadata_type_date)
941 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
944 rec_md->data.number.min = first;
945 rec_md->data.number.max = last;
952 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
953 struct conf_service *service, WRBUF norm_wr)
957 for (n = root->children; n; n = n->next)
959 if (n->type != XML_ELEMENT_NODE)
961 if (!strcmp((const char *) n->name, "metadata"))
963 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
964 if (!strcmp(name, (const char *) type))
966 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
969 const char *norm_str;
970 pp2_relevance_token_t prt =
971 pp2_relevance_tokenize(service->mergekey_pct);
973 pp2_relevance_first(prt, (const char *) value, 0);
974 if (wrbuf_len(norm_wr) > 0)
975 wrbuf_puts(norm_wr, " ");
976 wrbuf_puts(norm_wr, name);
978 pp2_relevance_token_next(prt)))
982 wrbuf_puts(norm_wr, " ");
983 wrbuf_puts(norm_wr, norm_str);
987 pp2_relevance_token_destroy(prt);
997 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
998 struct conf_service *service, NMEM nmem)
1000 char *mergekey_norm = 0;
1001 xmlNode *root = xmlDocGetRootElement(doc);
1002 WRBUF norm_wr = wrbuf_alloc();
1004 /* consider mergekey from XSL first */
1005 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1008 const char *norm_str;
1009 pp2_relevance_token_t prt =
1010 pp2_relevance_tokenize(service->mergekey_pct);
1012 pp2_relevance_first(prt, (const char *) mergekey, 0);
1013 while ((norm_str = pp2_relevance_token_next(prt)))
1017 if (wrbuf_len(norm_wr))
1018 wrbuf_puts(norm_wr, " ");
1019 wrbuf_puts(norm_wr, norm_str);
1022 pp2_relevance_token_destroy(prt);
1027 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1029 for (field_id = 0; field_id < service->num_metadata; field_id++)
1031 struct conf_metadata *ser_md = &service->metadata[field_id];
1032 if (ser_md->mergekey != Metadata_mergekey_no)
1034 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1036 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1038 /* no mergekey on this one and it is required..
1039 Generate unique key instead */
1040 wrbuf_rewind(norm_wr);
1047 /* generate unique key if none is not generated already or is empty */
1048 if (wrbuf_len(norm_wr) == 0)
1050 wrbuf_printf(norm_wr, "%s-%d",
1051 client_get_database(cl)->database->url, record_no);
1053 if (wrbuf_len(norm_wr) > 0)
1054 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1055 wrbuf_destroy(norm_wr);
1056 return mergekey_norm;
1059 /** \brief see if metadata for pz:recordfilter exists
1060 \param root xml root element of normalized record
1061 \param sdb session database for client
1062 \retval 0 if there is no metadata for pz:recordfilter
1063 \retval 1 if there is metadata for pz:recordfilter
1065 If there is no pz:recordfilter defined, this function returns 1
1069 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1074 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1079 for (n = root->children; n; n = n->next)
1081 if (n->type != XML_ELEMENT_NODE)
1083 if (!strcmp((const char *) n->name, "metadata"))
1085 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1089 const char *eq = strchr(s, '~');
1094 if (len == strlen((const char *)type) &&
1095 !memcmp((const char *) type, s, len))
1097 xmlChar *value = xmlNodeGetContent(n);
1098 if (value && *value)
1100 if (!eq || strstr((const char *) value, eq+1))
1113 static int ingest_to_cluster(struct client *cl,
1117 const char *mergekey_norm);
1119 /** \brief ingest XML record
1120 \param cl client holds the result set for record
1121 \param rec record buffer (0 terminated)
1122 \param record_no record position (1, 2, ..)
1126 int ingest_record(struct client *cl, const char *rec,
1127 int record_no, NMEM nmem)
1129 struct session *se = client_get_session(cl);
1131 struct session_database *sdb = client_get_database(cl);
1132 struct conf_service *service = se->service;
1133 xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem);
1135 const char *mergekey_norm;
1140 root = xmlDocGetRootElement(xdoc);
1142 if (!check_record_filter(root, sdb))
1144 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1145 sdb->database->url);
1150 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1153 yaz_log(YLOG_WARN, "Got no mergekey");
1158 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1165 static int ingest_to_cluster(struct client *cl,
1169 const char *mergekey_norm)
1174 struct session_database *sdb = client_get_database(cl);
1175 struct session *se = client_get_session(cl);
1176 struct conf_service *service = se->service;
1177 struct record *record = record_create(se->nmem,
1178 service->num_metadata,
1179 service->num_sortkeys, cl,
1181 struct record_cluster *cluster = reclist_insert(se->reclist,
1188 if (global_parameters.dump_records)
1189 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1190 sdb->database->url, record_no);
1191 relevance_newrec(se->relevance, cluster);
1193 // now parsing XML record and adding data to cluster or record metadata
1194 for (n = root->children; n; n = n->next)
1196 pp2_relevance_token_t prt;
1203 if (n->type != XML_ELEMENT_NODE)
1205 if (!strcmp((const char *) n->name, "metadata"))
1207 struct conf_metadata *ser_md = 0;
1208 struct conf_sortkey *ser_sk = 0;
1209 struct record_metadata **wheretoput = 0;
1210 struct record_metadata *rec_md = 0;
1211 int md_field_id = -1;
1212 int sk_field_id = -1;
1214 type = xmlGetProp(n, (xmlChar *) "type");
1215 value = xmlNodeListGetString(xdoc, n->children, 1);
1217 if (!type || !value || !*value)
1221 = conf_service_metadata_field_id(service, (const char *) type);
1222 if (md_field_id < 0)
1224 if (se->number_of_warnings_unknown_metadata == 0)
1227 "Ignoring unknown metadata element: %s", type);
1229 se->number_of_warnings_unknown_metadata++;
1233 ser_md = &service->metadata[md_field_id];
1235 if (ser_md->sortkey_offset >= 0){
1236 sk_field_id = ser_md->sortkey_offset;
1237 ser_sk = &service->sortkeys[sk_field_id];
1240 // non-merged metadata
1241 rec_md = record_metadata_init(se->nmem, (const char *) value,
1242 ser_md->type, n->properties);
1245 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1249 wheretoput = &record->metadata[md_field_id];
1251 wheretoput = &(*wheretoput)->next;
1252 *wheretoput = rec_md;
1255 rec_md = record_metadata_init(se->nmem, (const char *) value,
1257 wheretoput = &cluster->metadata[md_field_id];
1259 // and polulate with data:
1260 // assign cluster or record based on merge action
1261 if (ser_md->merge == Metadata_merge_unique)
1263 struct record_metadata *mnode;
1264 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1265 if (!strcmp((const char *) mnode->data.text.disp,
1266 rec_md->data.text.disp))
1270 rec_md->next = *wheretoput;
1271 *wheretoput = rec_md;
1274 else if (ser_md->merge == Metadata_merge_longest)
1277 || strlen(rec_md->data.text.disp)
1278 > strlen((*wheretoput)->data.text.disp))
1280 *wheretoput = rec_md;
1283 const char *sort_str = 0;
1285 ser_sk->type == Metadata_sortkey_skiparticle;
1287 if (!cluster->sortkeys[sk_field_id])
1288 cluster->sortkeys[sk_field_id] =
1289 nmem_malloc(se->nmem,
1290 sizeof(union data_types));
1292 prt = pp2_relevance_tokenize(service->sort_pct);
1294 pp2_relevance_first(prt, rec_md->data.text.disp,
1297 pp2_relevance_token_next(prt);
1299 sort_str = pp2_get_sort(prt);
1301 cluster->sortkeys[sk_field_id]->text.disp =
1302 rec_md->data.text.disp;
1305 sort_str = rec_md->data.text.disp;
1307 "Could not make sortkey. Bug #1858");
1309 cluster->sortkeys[sk_field_id]->text.sort =
1310 nmem_strdup(se->nmem, sort_str);
1312 yaz_log(YLOG_LOG, "text disp=%s",
1313 cluster->sortkeys[sk_field_id]->text.disp);
1314 yaz_log(YLOG_LOG, "text sort=%s",
1315 cluster->sortkeys[sk_field_id]->text.sort);
1317 pp2_relevance_token_destroy(prt);
1321 else if (ser_md->merge == Metadata_merge_all)
1323 rec_md->next = *wheretoput;
1324 *wheretoput = rec_md;
1326 else if (ser_md->merge == Metadata_merge_range)
1330 *wheretoput = rec_md;
1332 cluster->sortkeys[sk_field_id]
1337 int this_min = rec_md->data.number.min;
1338 int this_max = rec_md->data.number.max;
1339 if (this_min < (*wheretoput)->data.number.min)
1340 (*wheretoput)->data.number.min = this_min;
1341 if (this_max > (*wheretoput)->data.number.max)
1342 (*wheretoput)->data.number.max = this_max;
1347 // ranking of _all_ fields enabled ...
1349 relevance_countwords(se->relevance, cluster,
1350 (char *) value, ser_md->rank,
1353 // construct facets ...
1354 if (ser_md->termlist)
1356 if (ser_md->type == Metadata_type_year)
1359 sprintf(year, "%d", rec_md->data.number.max);
1360 add_facet(se, (char *) type, year);
1361 if (rec_md->data.number.max != rec_md->data.number.min)
1363 sprintf(year, "%d", rec_md->data.number.min);
1364 add_facet(se, (char *) type, year);
1368 add_facet(se, (char *) type, (char *) value);
1378 if (se->number_of_warnings_unknown_elements == 0)
1380 "Unexpected element in internal record: %s", n->name);
1381 se->number_of_warnings_unknown_elements++;
1389 relevance_donerecord(se->relevance, cluster);
1390 se->total_records++;
1398 * c-file-style: "Stroustrup"
1399 * indent-tabs-mode: nil
1401 * vim: shiftwidth=4 tabstop=8 expandtab