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
45 #include <yaz/marcdisp.h>
46 #include <yaz/comstack.h>
47 #include <yaz/tcpip.h>
48 #include <yaz/proto.h>
49 #include <yaz/readconf.h>
50 #include <yaz/pquery.h>
51 #include <yaz/otherinfo.h>
52 #include <yaz/yaz-util.h>
54 #include <yaz/query-charset.h>
55 #include <yaz/querytowrbuf.h>
56 #include <yaz/oid_db.h>
57 #include <yaz/snprintf.h>
58 #include <yaz/gettimeofday.h>
62 #include <yaz/timing.h>
66 #include "parameters.h"
70 #include "termlists.h"
72 #include "relevance.h"
76 #include "normalize7bit.h"
78 #define TERMLIST_HIGH_SCORE 25
82 // Note: Some things in this structure will eventually move to configuration
83 struct parameters global_parameters =
90 struct client *client;
91 struct client_list *next;
94 static void log_xml_doc(xmlDoc *doc)
96 FILE *lf = yaz_log_file();
99 #if LIBXML_VERSION >= 20600
100 xmlDocDumpFormatMemory(doc, &result, &len, 1);
102 xmlDocDumpMemory(doc, &result, &len);
106 (void) fwrite(result, 1, len, lf);
112 static void session_enter(struct session *s)
114 yaz_mutex_enter(s->session_mutex);
117 static void session_leave(struct session *s)
119 yaz_mutex_leave(s->session_mutex);
122 // Recursively traverse query structure to extract terms.
123 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
135 pull_terms(nmem, n->u.p[0], termlist, num);
136 pull_terms(nmem, n->u.p[1], termlist, num);
139 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
140 for (i = 0; i < numwords; i++)
141 termlist[(*num)++] = words[i];
149 void add_facet(struct session *s, const char *type, const char *value, int count)
155 for (i = 0; i < s->num_termlists; i++)
156 if (!strcmp(s->termlists[i].name, type))
158 if (i == s->num_termlists)
160 if (i == SESSION_MAX_TERMLISTS)
162 yaz_log(YLOG_FATAL, "Too many termlists");
166 s->termlists[i].name = nmem_strdup(s->nmem, type);
167 s->termlists[i].termlist
168 = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
169 s->num_termlists = i + 1;
171 yaz_log(YLOG_DEBUG, "Session: facets for %s: %s (%d)", type, value, count);
173 termlist_insert(s->termlists[i].termlist, value, count);
176 static xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
178 struct database *db = sdb->database;
181 rdoc = xmlParseMemory(rec, strlen(rec));
185 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
190 if (global_parameters.dump_records)
192 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
199 #define MAX_XSLT_ARGS 16
201 // Add static values from session database settings if applicable
202 static void insert_settings_parameters(struct session_database *sdb,
203 struct conf_service *service,
211 for (i = 0; i < service->num_metadata; i++)
213 struct conf_metadata *md = &service->metadata[i];
216 if (md->setting == Metadata_setting_parameter &&
217 (setting = settings_lookup_offset(service, md->name)) >= 0)
219 const char *val = session_setting_oneval(sdb, setting);
220 if (val && nparms < MAX_XSLT_ARGS)
223 int len = strlen(val);
224 buf = nmem_malloc(nmem, len + 3);
226 strcpy(buf + 1, val);
229 parms[offset++] = md->name;
230 parms[offset++] = buf;
238 // Add static values from session database settings if applicable
239 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
240 struct conf_service *service)
244 for (i = 0; i < service->num_metadata; i++)
246 struct conf_metadata *md = &service->metadata[i];
249 if (md->setting == Metadata_setting_postproc &&
250 (offset = settings_lookup_offset(service, md->name)) >= 0)
252 const char *val = session_setting_oneval(sdb, offset);
255 xmlNode *r = xmlDocGetRootElement(doc);
256 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
258 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
264 static xmlDoc *normalize_record(struct session_database *sdb,
265 struct conf_service *service,
266 const char *rec, NMEM nmem)
268 xmlDoc *rdoc = record_to_xml(sdb, rec);
272 char *parms[MAX_XSLT_ARGS*2+1];
274 insert_settings_parameters(sdb, service, parms, nmem);
276 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
278 yaz_log(YLOG_WARN, "Normalize failed from %s", sdb->database->url);
282 insert_settings_values(sdb, rdoc, service);
284 if (global_parameters.dump_records)
286 yaz_log(YLOG_LOG, "Normalized record from %s",
295 void session_settings_dump(struct session *se,
296 struct session_database *db,
301 int i, num = db->num_settings;
302 for (i = 0; i < num; i++)
304 struct setting *s = db->settings[i];
305 for (;s ; s = s->next)
307 wrbuf_puts(w, "<set name=\"");
308 wrbuf_xmlputs(w, s->name);
309 wrbuf_puts(w, "\" value=\"");
310 wrbuf_xmlputs(w, s->value);
311 wrbuf_puts(w, "\"/>");
319 // Retrieve first defined value for 'name' for given database.
320 // Will be extended to take into account user associated with session
321 const char *session_setting_oneval(struct session_database *db, int offset)
323 if (offset >= db->num_settings || !db->settings[offset])
325 return db->settings[offset]->value;
328 // Prepare XSLT stylesheets for record normalization
329 // Structures are allocated on the session_wide nmem to avoid having
330 // to recompute this for every search. This would lead
331 // to leaking if a single session was to repeatedly change the PZ_XSLT
332 // setting. However, this is not a realistic use scenario.
333 static int prepare_map(struct session *se, struct session_database *sdb)
339 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
342 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
344 char auto_stylesheet[256];
346 if (!strcmp(s, "auto"))
348 const char *request_syntax = session_setting_oneval(
349 sdb, PZ_REQUESTSYNTAX);
353 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
354 "%s.xsl", request_syntax);
355 for (cp = auto_stylesheet; *cp; cp++)
357 /* deliberately only consider ASCII */
358 if (*cp > 32 && *cp < 127)
365 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
368 sdb->map = normalize_cache_get(se->normalize_cache,
369 se->service->server->config, s);
376 // This analyzes settings and recomputes any supporting data structures
378 static int prepare_session_database(struct session *se,
379 struct session_database *sdb)
384 "No settings associated with %s", sdb->database->url);
387 if (sdb->settings[PZ_XSLT] && !sdb->map)
389 if (prepare_map(se, sdb) < 0)
395 // called if watch should be removed because http_channel is to be destroyed
396 static void session_watch_cancel(void *data, struct http_channel *c,
399 struct session_watchentry *ent = data;
406 // set watch. Returns 0=OK, -1 if watch is already set
407 int session_set_watch(struct session *s, int what,
408 session_watchfun fun, void *data,
409 struct http_channel *chan)
413 if (s->watchlist[what].fun)
418 s->watchlist[what].fun = fun;
419 s->watchlist[what].data = data;
420 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
421 session_watch_cancel);
428 void session_alert_watch(struct session *s, int what)
432 if (s->watchlist[what].fun)
434 /* our watch is no longer associated with http_channel */
436 session_watchfun fun;
438 http_remove_observer(s->watchlist[what].obs);
439 fun = s->watchlist[what].fun;
440 data = s->watchlist[what].data;
442 /* reset watch before fun is invoked - in case fun wants to set
444 s->watchlist[what].fun = 0;
445 s->watchlist[what].data = 0;
446 s->watchlist[what].obs = 0;
449 yaz_log(YLOG_DEBUG, "session_alert_watch: %d calling function: %p", what, fun);
456 //callback for grep_databases
457 static void select_targets_callback(void *context, struct session_database *db)
459 struct session *se = (struct session*) context;
460 struct client *cl = client_create();
461 struct client_list *l;
462 client_set_database(cl, db);
464 client_set_session(cl, se);
465 l = xmalloc(sizeof(*l));
467 l->next = se->clients;
471 static void session_remove_clients(struct session *se)
473 struct client_list *l;
482 struct client_list *l_next = l->next;
483 client_lock(l->client);
484 client_set_session(l->client, 0);
485 client_set_database(l->client, 0);
486 client_unlock(l->client);
487 client_destroy(l->client);
493 // Associates a set of clients with a session;
494 // Note: Session-databases represent databases with per-session
496 static int select_targets(struct session *se, const char *filter)
498 return session_grep_databases(se, filter, select_targets_callback);
501 int session_active_clients(struct session *s)
503 struct client_list *l;
506 for (l = s->clients; l; l = l->next)
507 if (client_is_active(l->client))
513 int session_is_preferred_clients_ready(struct session *s)
515 struct client_list *l;
518 for (l = s->clients; l; l = l->next)
519 if (client_is_active_preferred(l->client))
521 yaz_log(YLOG_DEBUG, "%p Session has %d active preferred clients.", s, res);
527 enum pazpar2_error_code search(struct session *se,
529 const char *startrecs, const char *maxrecs,
531 const char **addinfo)
533 int live_channels = 0;
536 struct client_list *l;
539 yaz_log(YLOG_DEBUG, "Search");
543 session_remove_clients(se);
546 reclist_destroy(se->reclist);
548 relevance_destroy(&se->relevance);
549 nmem_reset(se->nmem);
550 se->total_records = se->total_hits = se->total_merged = 0;
551 se->num_termlists = 0;
552 live_channels = select_targets(se, filter);
556 return PAZPAR2_NO_TARGETS;
558 se->reclist = reclist_create(se->nmem);
560 yaz_gettimeofday(&tval);
564 for (l = se->clients; l; l = l->next)
566 struct client *cl = l->client;
569 client_set_maxrecs(cl, atoi(maxrecs));
571 client_set_startrecs(cl, atoi(startrecs));
572 if (prepare_session_database(se, client_get_database(cl)) < 0)
574 else if (client_parse_query(cl, query) < 0)
579 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
580 se->service->z3950_session_timeout,
581 se->service->server->iochan_man,
583 client_start_search(cl);
592 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
595 return PAZPAR2_NO_TARGETS;
597 return PAZPAR2_NO_ERROR;
600 // Creates a new session_database object for a database
601 static void session_init_databases_fun(void *context, struct database *db)
603 struct session *se = (struct session *) context;
604 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
610 assert(db->settings);
611 new->settings = nmem_malloc(se->session_nmem,
612 sizeof(struct settings *) * db->num_settings);
613 new->num_settings = db->num_settings;
614 for (i = 0; i < db->num_settings; i++)
616 struct setting *setting = db->settings[i];
617 new->settings[i] = setting;
619 new->next = se->databases;
623 // Doesn't free memory associated with sdb -- nmem takes care of that
624 static void session_database_destroy(struct session_database *sdb)
629 // Initialize session_database list -- this represents this session's view
630 // of the database list -- subject to modification by the settings ws command
631 void session_init_databases(struct session *se)
634 predef_grep_databases(se, se->service, session_init_databases_fun);
637 // Probably session_init_databases_fun should be refactored instead of
639 static struct session_database *load_session_database(struct session *se,
642 struct database *db = new_database(id, se->session_nmem);
644 resolve_database(se->service, db);
646 session_init_databases_fun((void*) se, db);
648 // New sdb is head of se->databases list
649 return se->databases;
652 // Find an existing session database. If not found, load it
653 static struct session_database *find_session_database(struct session *se,
656 struct session_database *sdb;
658 for (sdb = se->databases; sdb; sdb = sdb->next)
659 if (!strcmp(sdb->database->url, id))
661 return load_session_database(se, id);
664 // Apply a session override to a database
665 void session_apply_setting(struct session *se, char *dbname, char *setting,
668 struct session_database *sdb = find_session_database(se, dbname);
669 struct conf_service *service = se->service;
670 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
671 int offset = settings_create_offset(service, setting);
673 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
676 new->target = dbname;
679 new->next = sdb->settings[offset];
680 sdb->settings[offset] = new;
682 // Force later recompute of settings-driven data structures
683 // (happens when a search starts and client connections are prepared)
695 void destroy_session(struct session *se)
697 struct session_database *sdb;
699 yaz_log(YLOG_DEBUG, "%p Pazpar2 session destroy", se);
700 session_remove_clients(se);
702 for (sdb = se->databases; sdb; sdb = sdb->next)
703 session_database_destroy(sdb);
704 normalize_cache_destroy(se->normalize_cache);
705 relevance_destroy(&se->relevance);
706 reclist_destroy(se->reclist);
707 nmem_destroy(se->nmem);
708 service_destroy(se->service);
709 yaz_mutex_destroy(&se->session_mutex);
710 wrbuf_destroy(se->wrbuf);
713 struct session *new_session(NMEM nmem, struct conf_service *service,
717 struct session *session = nmem_malloc(nmem, sizeof(*session));
719 yaz_log(YLOG_DEBUG, "%p New Pazpar2 session", session);
721 session->service = service;
722 session->relevance = 0;
723 session->total_hits = 0;
724 session->total_records = 0;
725 session->number_of_warnings_unknown_elements = 0;
726 session->number_of_warnings_unknown_metadata = 0;
727 session->num_termlists = 0;
728 session->reclist = 0;
729 session->clients = 0;
730 session->session_nmem = nmem;
731 session->nmem = nmem_create();
732 session->wrbuf = wrbuf_alloc();
733 session->databases = 0;
734 for (i = 0; i <= SESSION_WATCH_MAX; i++)
736 session->watchlist[i].data = 0;
737 session->watchlist[i].fun = 0;
739 session->normalize_cache = normalize_cache_create();
740 session->session_mutex = 0;
741 pazpar2_mutex_create(&session->session_mutex, name);
746 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
748 struct hitsbytarget *res = 0;
749 struct client_list *l;
753 for (l = se->clients; l; l = l->next)
756 res = nmem_malloc(nmem, sizeof(*res) * sz);
758 for (l = se->clients; l; l = l->next)
760 struct client *cl = l->client;
761 WRBUF w = wrbuf_alloc();
762 const char *name = session_setting_oneval(client_get_database(cl),
765 res[*count].id = client_get_database(cl)->database->url;
766 res[*count].name = *name ? name : "Unknown";
767 res[*count].hits = client_get_hits(cl);
768 res[*count].records = client_get_num_records(cl);
769 res[*count].diagnostic = client_get_diagnostic(cl);
770 res[*count].state = client_get_state_str(cl);
771 res[*count].connected = client_get_connection(cl) ? 1 : 0;
772 session_settings_dump(se, client_get_database(cl), w);
773 res[*count].settings_xml = w;
780 struct termlist_score **termlist(struct session *se, const char *name, int *num)
783 struct termlist_score **tl = 0;
786 for (i = 0; i < se->num_termlists; i++)
787 if (!strcmp((const char *) se->termlists[i].name, name))
789 tl = termlist_highscore(se->termlists[i].termlist, num);
796 #ifdef MISSING_HEADERS
797 void report_nmem_stats(void)
799 size_t in_use, is_free;
801 nmem_get_memory_in_use(&in_use);
802 nmem_get_memory_free(&is_free);
804 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
805 (long) in_use, (long) is_free);
809 struct record_cluster *show_single_start(struct session *se, const char *id,
810 struct record_cluster **prev_r,
811 struct record_cluster **next_r)
813 struct record_cluster *r;
816 reclist_enter(se->reclist);
819 while ((r = reclist_read_record(se->reclist)))
821 if (!strcmp(r->recid, id))
823 *next_r = reclist_read_record(se->reclist);
828 reclist_leave(se->reclist);
834 void show_single_stop(struct session *se, struct record_cluster *rec)
839 struct record_cluster **show_range_start(struct session *se,
840 struct reclist_sortparms *sp,
841 int start, int *num, int *total, Odr_int *sumhits)
843 struct record_cluster **recs;
844 struct reclist_sortparms *spp;
847 yaz_timing_t t = yaz_timing_create();
850 recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
860 for (spp = sp; spp; spp = spp->next)
861 if (spp->type == Metadata_sortkey_relevance)
863 relevance_prepare_read(se->relevance, se->reclist);
866 reclist_sort(se->reclist, sp);
868 reclist_enter(se->reclist);
869 *total = reclist_get_num_records(se->reclist);
870 *sumhits = se->total_hits;
872 for (i = 0; i < start; i++)
873 if (!reclist_read_record(se->reclist))
880 for (i = 0; i < *num; i++)
882 struct record_cluster *r = reclist_read_record(se->reclist);
890 reclist_leave(se->reclist);
894 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
895 yaz_timing_get_real(t), yaz_timing_get_user(t),
896 yaz_timing_get_sys(t));
897 yaz_timing_destroy(&t);
902 void show_range_stop(struct session *se, struct record_cluster **recs)
907 void statistics(struct session *se, struct statistics *stat)
909 struct client_list *l;
912 memset(stat, 0, sizeof(*stat));
913 for (l = se->clients; l; l = l->next)
915 struct client *cl = l->client;
916 if (!client_get_connection(cl))
917 stat->num_no_connection++;
918 switch (client_get_state(cl))
920 case Client_Connecting: stat->num_connecting++; break;
921 case Client_Working: stat->num_working++; break;
922 case Client_Idle: stat->num_idle++; break;
923 case Client_Failed: stat->num_failed++; break;
924 case Client_Error: stat->num_error++; break;
929 stat->num_hits = se->total_hits;
930 stat->num_records = se->total_records;
932 stat->num_clients = count;
935 static struct record_metadata *record_metadata_init(
936 NMEM nmem, const char *value, enum conf_metadata_type type,
937 struct _xmlAttr *attr)
939 struct record_metadata *rec_md = record_metadata_create(nmem);
940 struct record_metadata_attr **attrp = &rec_md->attributes;
942 for (; attr; attr = attr->next)
944 if (attr->children && attr->children->content)
946 if (strcmp((const char *) attr->name, "type"))
947 { /* skip the "type" attribute.. Its value is already part of
948 the element in output (md-%s) and so repeating it here
950 *attrp = nmem_malloc(nmem, sizeof(**attrp));
952 nmem_strdup(nmem, (const char *) attr->name);
954 nmem_strdup(nmem, (const char *) attr->children->content);
955 attrp = &(*attrp)->next;
961 if (type == Metadata_type_generic)
963 char *p = nmem_strdup(nmem, value);
965 p = normalize7bit_generic(p, " ,/.:([");
967 rec_md->data.text.disp = p;
968 rec_md->data.text.sort = 0;
970 else if (type == Metadata_type_year || type == Metadata_type_date)
975 if (type == Metadata_type_date)
977 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
980 rec_md->data.number.min = first;
981 rec_md->data.number.max = last;
988 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
989 struct conf_service *service, WRBUF norm_wr)
993 for (n = root->children; n; n = n->next)
995 if (n->type != XML_ELEMENT_NODE)
997 if (!strcmp((const char *) n->name, "metadata"))
999 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1001 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
1003 else if (!strcmp(name, (const char *) type))
1005 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
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 *) value, 0);
1013 if (wrbuf_len(norm_wr) > 0)
1014 wrbuf_puts(norm_wr, " ");
1015 wrbuf_puts(norm_wr, name);
1017 pp2_relevance_token_next(prt)))
1021 wrbuf_puts(norm_wr, " ");
1022 wrbuf_puts(norm_wr, norm_str);
1026 pp2_relevance_token_destroy(prt);
1036 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1037 struct conf_service *service, NMEM nmem)
1039 char *mergekey_norm = 0;
1040 xmlNode *root = xmlDocGetRootElement(doc);
1041 WRBUF norm_wr = wrbuf_alloc();
1043 /* consider mergekey from XSL first */
1044 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1047 const char *norm_str;
1048 pp2_relevance_token_t prt =
1049 pp2_relevance_tokenize(service->mergekey_pct);
1051 pp2_relevance_first(prt, (const char *) mergekey, 0);
1052 while ((norm_str = pp2_relevance_token_next(prt)))
1056 if (wrbuf_len(norm_wr))
1057 wrbuf_puts(norm_wr, " ");
1058 wrbuf_puts(norm_wr, norm_str);
1061 pp2_relevance_token_destroy(prt);
1066 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1068 for (field_id = 0; field_id < service->num_metadata; field_id++)
1070 struct conf_metadata *ser_md = &service->metadata[field_id];
1071 if (ser_md->mergekey != Metadata_mergekey_no)
1073 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1075 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1077 /* no mergekey on this one and it is required..
1078 Generate unique key instead */
1079 wrbuf_rewind(norm_wr);
1086 /* generate unique key if none is not generated already or is empty */
1087 if (wrbuf_len(norm_wr) == 0)
1089 wrbuf_printf(norm_wr, "%s-%d",
1090 client_get_database(cl)->database->url, record_no);
1092 if (wrbuf_len(norm_wr) > 0)
1093 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1094 wrbuf_destroy(norm_wr);
1095 return mergekey_norm;
1098 /** \brief see if metadata for pz:recordfilter exists
1099 \param root xml root element of normalized record
1100 \param sdb session database for client
1101 \retval 0 if there is no metadata for pz:recordfilter
1102 \retval 1 if there is metadata for pz:recordfilter
1104 If there is no pz:recordfilter defined, this function returns 1
1108 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1113 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1118 for (n = root->children; n; n = n->next)
1120 if (n->type != XML_ELEMENT_NODE)
1122 if (!strcmp((const char *) n->name, "metadata"))
1124 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1131 if ((eq = strchr(s, '=')))
1133 else if ((eq = strchr(s, '~')))
1139 if (len == strlen((const char *)type) &&
1140 !memcmp((const char *) type, s, len))
1142 xmlChar *value = xmlNodeGetContent(n);
1143 if (value && *value)
1146 (substring && strstr((const char *) value, eq+1)) ||
1147 (!substring && !strcmp((const char *) value, eq + 1)))
1160 static int ingest_to_cluster(struct client *cl,
1164 const char *mergekey_norm);
1166 /** \brief ingest XML record
1167 \param cl client holds the result set for record
1168 \param rec record buffer (0 terminated)
1169 \param record_no record position (1, 2, ..)
1170 \param nmem working NMEM
1174 int ingest_record(struct client *cl, const char *rec,
1175 int record_no, NMEM nmem)
1177 struct session *se = client_get_session(cl);
1179 struct session_database *sdb = client_get_database(cl);
1180 struct conf_service *service = se->service;
1181 xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem);
1183 const char *mergekey_norm;
1188 root = xmlDocGetRootElement(xdoc);
1190 if (!check_record_filter(root, sdb))
1192 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1193 sdb->database->url);
1198 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1201 yaz_log(YLOG_WARN, "Got no mergekey");
1206 if (client_get_session(cl) == se)
1207 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1214 static int ingest_to_cluster(struct client *cl,
1218 const char *mergekey_norm)
1223 struct session_database *sdb = client_get_database(cl);
1224 struct session *se = client_get_session(cl);
1225 struct conf_service *service = se->service;
1226 struct record *record = record_create(se->nmem,
1227 service->num_metadata,
1228 service->num_sortkeys, cl,
1230 struct record_cluster *cluster = reclist_insert(se->reclist,
1237 if (global_parameters.dump_records)
1238 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1239 sdb->database->url, record_no);
1240 relevance_newrec(se->relevance, cluster);
1242 // now parsing XML record and adding data to cluster or record metadata
1243 for (n = root->children; n; n = n->next)
1245 pp2_relevance_token_t prt;
1252 if (n->type != XML_ELEMENT_NODE)
1254 if (!strcmp((const char *) n->name, "metadata"))
1256 struct conf_metadata *ser_md = 0;
1257 struct conf_sortkey *ser_sk = 0;
1258 struct record_metadata **wheretoput = 0;
1259 struct record_metadata *rec_md = 0;
1260 int md_field_id = -1;
1261 int sk_field_id = -1;
1263 type = xmlGetProp(n, (xmlChar *) "type");
1264 value = xmlNodeListGetString(xdoc, n->children, 1);
1266 if (!type || !value || !*value)
1270 = conf_service_metadata_field_id(service, (const char *) type);
1271 if (md_field_id < 0)
1273 if (se->number_of_warnings_unknown_metadata == 0)
1276 "Ignoring unknown metadata element: %s", type);
1278 se->number_of_warnings_unknown_metadata++;
1282 ser_md = &service->metadata[md_field_id];
1284 if (ser_md->sortkey_offset >= 0){
1285 sk_field_id = ser_md->sortkey_offset;
1286 ser_sk = &service->sortkeys[sk_field_id];
1289 // non-merged metadata
1290 rec_md = record_metadata_init(se->nmem, (const char *) value,
1291 ser_md->type, n->properties);
1294 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1298 wheretoput = &record->metadata[md_field_id];
1300 wheretoput = &(*wheretoput)->next;
1301 *wheretoput = rec_md;
1304 rec_md = record_metadata_init(se->nmem, (const char *) value,
1306 wheretoput = &cluster->metadata[md_field_id];
1308 // and polulate with data:
1309 // assign cluster or record based on merge action
1310 if (ser_md->merge == Metadata_merge_unique)
1312 struct record_metadata *mnode;
1313 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1314 if (!strcmp((const char *) mnode->data.text.disp,
1315 rec_md->data.text.disp))
1319 rec_md->next = *wheretoput;
1320 *wheretoput = rec_md;
1323 else if (ser_md->merge == Metadata_merge_longest)
1326 || strlen(rec_md->data.text.disp)
1327 > strlen((*wheretoput)->data.text.disp))
1329 *wheretoput = rec_md;
1332 const char *sort_str = 0;
1334 ser_sk->type == Metadata_sortkey_skiparticle;
1336 if (!cluster->sortkeys[sk_field_id])
1337 cluster->sortkeys[sk_field_id] =
1338 nmem_malloc(se->nmem,
1339 sizeof(union data_types));
1341 prt = pp2_relevance_tokenize(service->sort_pct);
1343 pp2_relevance_first(prt, rec_md->data.text.disp,
1346 pp2_relevance_token_next(prt);
1348 sort_str = pp2_get_sort(prt);
1350 cluster->sortkeys[sk_field_id]->text.disp =
1351 rec_md->data.text.disp;
1354 sort_str = rec_md->data.text.disp;
1356 "Could not make sortkey. Bug #1858");
1358 cluster->sortkeys[sk_field_id]->text.sort =
1359 nmem_strdup(se->nmem, sort_str);
1361 yaz_log(YLOG_LOG, "text disp=%s",
1362 cluster->sortkeys[sk_field_id]->text.disp);
1363 yaz_log(YLOG_LOG, "text sort=%s",
1364 cluster->sortkeys[sk_field_id]->text.sort);
1366 pp2_relevance_token_destroy(prt);
1370 else if (ser_md->merge == Metadata_merge_all)
1372 rec_md->next = *wheretoput;
1373 *wheretoput = rec_md;
1375 else if (ser_md->merge == Metadata_merge_range)
1379 *wheretoput = rec_md;
1381 cluster->sortkeys[sk_field_id]
1386 int this_min = rec_md->data.number.min;
1387 int this_max = rec_md->data.number.max;
1388 if (this_min < (*wheretoput)->data.number.min)
1389 (*wheretoput)->data.number.min = this_min;
1390 if (this_max > (*wheretoput)->data.number.max)
1391 (*wheretoput)->data.number.max = this_max;
1396 // ranking of _all_ fields enabled ...
1398 relevance_countwords(se->relevance, cluster,
1399 (char *) value, ser_md->rank,
1402 // construct facets ... unless the client already has reported them
1403 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
1406 if (ser_md->type == Metadata_type_year)
1409 sprintf(year, "%d", rec_md->data.number.max);
1410 add_facet(se, (char *) type, year, 1);
1411 if (rec_md->data.number.max != rec_md->data.number.min)
1413 sprintf(year, "%d", rec_md->data.number.min);
1414 add_facet(se, (char *) type, year, 1);
1418 add_facet(se, (char *) type, (char *) value, 1);
1428 if (se->number_of_warnings_unknown_elements == 0)
1430 "Unexpected element in internal record: %s", n->name);
1431 se->number_of_warnings_unknown_elements++;
1439 relevance_donerecord(se->relevance, cluster);
1440 se->total_records++;
1448 * c-file-style: "Stroustrup"
1449 * indent-tabs-mode: nil
1451 * vim: shiftwidth=4 tabstop=8 expandtab