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;
474 struct client_list *l_next = l->next;
475 client_lock(l->client);
476 client_set_session(l->client, 0);
477 client_set_database(l->client, 0);
478 client_unlock(l->client);
479 client_destroy(l->client);
485 // Associates a set of clients with a session;
486 // Note: Session-databases represent databases with per-session
488 static int select_targets(struct session *se, const char *filter)
490 return session_grep_databases(se, filter, select_targets_callback);
493 int session_active_clients(struct session *s)
495 struct client_list *l;
498 for (l = s->clients; l; l = l->next)
499 if (client_is_active(l->client))
506 enum pazpar2_error_code search(struct session *se,
508 const char *startrecs, const char *maxrecs,
510 const char **addinfo)
512 int live_channels = 0;
515 struct client_list *l;
517 yaz_log(YLOG_DEBUG, "Search");
521 session_remove_clients(se);
524 reclist_destroy(se->reclist);
526 nmem_reset(se->nmem);
527 relevance_destroy(&se->relevance);
528 se->total_records = se->total_hits = se->total_merged = 0;
529 se->num_termlists = 0;
530 live_channels = select_targets(se, filter);
534 return PAZPAR2_NO_TARGETS;
536 se->reclist = reclist_create(se->nmem);
538 for (l = se->clients; l; l = l->next)
540 struct client *cl = l->client;
543 client_set_maxrecs(cl, atoi(maxrecs));
545 client_set_startrecs(cl, atoi(startrecs));
546 if (prepare_session_database(se, client_get_database(cl)) < 0)
548 else if (client_parse_query(cl, query) < 0)
553 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
554 se->service->z3950_session_timeout,
555 se->service->server->iochan_man))
556 client_start_search(cl);
565 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
568 return PAZPAR2_NO_TARGETS;
570 return PAZPAR2_NO_ERROR;
573 // Creates a new session_database object for a database
574 static void session_init_databases_fun(void *context, struct database *db)
576 struct session *se = (struct session *) context;
577 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
583 assert(db->settings);
584 new->settings = nmem_malloc(se->session_nmem,
585 sizeof(struct settings *) * db->num_settings);
586 new->num_settings = db->num_settings;
587 for (i = 0; i < db->num_settings; i++)
589 struct setting *setting = db->settings[i];
590 new->settings[i] = setting;
592 new->next = se->databases;
596 // Doesn't free memory associated with sdb -- nmem takes care of that
597 static void session_database_destroy(struct session_database *sdb)
602 // Initialize session_database list -- this represents this session's view
603 // of the database list -- subject to modification by the settings ws command
604 void session_init_databases(struct session *se)
607 predef_grep_databases(se, se->service, session_init_databases_fun);
610 // Probably session_init_databases_fun should be refactored instead of
612 static struct session_database *load_session_database(struct session *se,
615 struct database *db = new_database(id, se->session_nmem);
617 resolve_database(se->service, db);
619 session_init_databases_fun((void*) se, db);
621 // New sdb is head of se->databases list
622 return se->databases;
625 // Find an existing session database. If not found, load it
626 static struct session_database *find_session_database(struct session *se,
629 struct session_database *sdb;
631 for (sdb = se->databases; sdb; sdb = sdb->next)
632 if (!strcmp(sdb->database->url, id))
634 return load_session_database(se, id);
637 // Apply a session override to a database
638 void session_apply_setting(struct session *se, char *dbname, char *setting,
641 struct session_database *sdb = find_session_database(se, dbname);
642 struct conf_service *service = se->service;
643 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
644 int offset = settings_create_offset(service, setting);
646 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
649 new->target = dbname;
652 new->next = sdb->settings[offset];
653 sdb->settings[offset] = new;
655 // Force later recompute of settings-driven data structures
656 // (happens when a search starts and client connections are prepared)
668 void destroy_session(struct session *s)
670 struct session_database *sdb;
672 session_remove_clients(s);
674 for (sdb = s->databases; sdb; sdb = sdb->next)
675 session_database_destroy(sdb);
676 normalize_cache_destroy(s->normalize_cache);
677 relevance_destroy(&s->relevance);
678 reclist_destroy(s->reclist);
679 nmem_destroy(s->nmem);
680 service_destroy(s->service);
681 yaz_mutex_destroy(&s->session_mutex);
682 wrbuf_destroy(s->wrbuf);
685 struct session *new_session(NMEM nmem, struct conf_service *service,
689 struct session *session = nmem_malloc(nmem, sizeof(*session));
691 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
693 session->service = service;
694 session->relevance = 0;
695 session->total_hits = 0;
696 session->total_records = 0;
697 session->number_of_warnings_unknown_elements = 0;
698 session->number_of_warnings_unknown_metadata = 0;
699 session->num_termlists = 0;
700 session->reclist = 0;
701 session->clients = 0;
702 session->session_nmem = nmem;
703 session->nmem = nmem_create();
704 session->wrbuf = wrbuf_alloc();
705 session->databases = 0;
706 for (i = 0; i <= SESSION_WATCH_MAX; i++)
708 session->watchlist[i].data = 0;
709 session->watchlist[i].fun = 0;
711 session->normalize_cache = normalize_cache_create();
712 session->session_mutex = 0;
713 pazpar2_mutex_create(&session->session_mutex, name);
718 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
720 struct hitsbytarget *res = 0;
721 struct client_list *l;
725 for (l = se->clients; l; l = l->next)
728 res = nmem_malloc(nmem, sizeof(*res) * sz);
730 for (l = se->clients; l; l = l->next)
732 struct client *cl = l->client;
733 WRBUF w = wrbuf_alloc();
734 const char *name = session_setting_oneval(client_get_database(cl),
737 res[*count].id = client_get_database(cl)->database->url;
738 res[*count].name = *name ? name : "Unknown";
739 res[*count].hits = client_get_hits(cl);
740 res[*count].records = client_get_num_records(cl);
741 res[*count].diagnostic = client_get_diagnostic(cl);
742 res[*count].state = client_get_state_str(cl);
743 res[*count].connected = client_get_connection(cl) ? 1 : 0;
744 session_settings_dump(se, client_get_database(cl), w);
745 res[*count].settings_xml = w;
752 struct termlist_score **termlist(struct session *s, const char *name, int *num)
755 struct termlist_score **tl = 0;
758 for (i = 0; i < s->num_termlists; i++)
759 if (!strcmp((const char *) s->termlists[i].name, name))
761 tl = termlist_highscore(s->termlists[i].termlist, num);
768 #ifdef MISSING_HEADERS
769 void report_nmem_stats(void)
771 size_t in_use, is_free;
773 nmem_get_memory_in_use(&in_use);
774 nmem_get_memory_free(&is_free);
776 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
777 (long) in_use, (long) is_free);
781 struct record_cluster *show_single_start(struct session *s, const char *id,
782 struct record_cluster **prev_r,
783 struct record_cluster **next_r)
785 struct record_cluster *r;
788 reclist_enter(s->reclist);
791 while ((r = reclist_read_record(s->reclist)))
793 if (!strcmp(r->recid, id))
795 *next_r = reclist_read_record(s->reclist);
800 reclist_leave(s->reclist);
806 void show_single_stop(struct session *s, struct record_cluster *rec)
811 struct record_cluster **show_range_start(struct session *s,
812 struct reclist_sortparms *sp,
813 int start, int *num, int *total, Odr_int *sumhits)
815 struct record_cluster **recs = nmem_malloc(s->nmem, *num
816 * sizeof(struct record_cluster *));
817 struct reclist_sortparms *spp;
820 yaz_timing_t t = yaz_timing_create();
833 for (spp = sp; spp; spp = spp->next)
834 if (spp->type == Metadata_sortkey_relevance)
836 relevance_prepare_read(s->relevance, s->reclist);
839 reclist_sort(s->reclist, sp);
841 reclist_enter(s->reclist);
842 *total = reclist_get_num_records(s->reclist);
843 *sumhits = s->total_hits;
845 for (i = 0; i < start; i++)
846 if (!reclist_read_record(s->reclist))
853 for (i = 0; i < *num; i++)
855 struct record_cluster *r = reclist_read_record(s->reclist);
863 reclist_leave(s->reclist);
867 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
868 yaz_timing_get_real(t), yaz_timing_get_user(t),
869 yaz_timing_get_sys(t));
870 yaz_timing_destroy(&t);
875 void show_range_stop(struct session *s, struct record_cluster **recs)
880 void statistics(struct session *se, struct statistics *stat)
882 struct client_list *l;
885 memset(stat, 0, sizeof(*stat));
886 for (l = se->clients; l; l = l->next)
888 struct client *cl = l->client;
889 if (!client_get_connection(cl))
890 stat->num_no_connection++;
891 switch (client_get_state(cl))
893 case Client_Connecting: stat->num_connecting++; break;
894 case Client_Working: stat->num_working++; break;
895 case Client_Idle: stat->num_idle++; break;
896 case Client_Failed: stat->num_failed++; break;
897 case Client_Error: stat->num_error++; break;
902 stat->num_hits = se->total_hits;
903 stat->num_records = se->total_records;
905 stat->num_clients = count;
908 static struct record_metadata *record_metadata_init(
909 NMEM nmem, const char *value, enum conf_metadata_type type,
910 struct _xmlAttr *attr)
912 struct record_metadata *rec_md = record_metadata_create(nmem);
913 struct record_metadata_attr **attrp = &rec_md->attributes;
915 for (; attr; attr = attr->next)
917 if (attr->children && attr->children->content)
919 if (strcmp((const char *) attr->name, "type"))
920 { /* skip the "type" attribute.. Its value is already part of
921 the element in output (md-%s) and so repeating it here
923 *attrp = nmem_malloc(nmem, sizeof(**attrp));
925 nmem_strdup(nmem, (const char *) attr->name);
927 nmem_strdup(nmem, (const char *) attr->children->content);
928 attrp = &(*attrp)->next;
934 if (type == Metadata_type_generic)
936 char *p = nmem_strdup(nmem, value);
938 p = normalize7bit_generic(p, " ,/.:([");
940 rec_md->data.text.disp = p;
941 rec_md->data.text.sort = 0;
943 else if (type == Metadata_type_year || type == Metadata_type_date)
948 if (type == Metadata_type_date)
950 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
953 rec_md->data.number.min = first;
954 rec_md->data.number.max = last;
961 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
962 struct conf_service *service, WRBUF norm_wr)
966 for (n = root->children; n; n = n->next)
968 if (n->type != XML_ELEMENT_NODE)
970 if (!strcmp((const char *) n->name, "metadata"))
972 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
973 if (!strcmp(name, (const char *) type))
975 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
978 const char *norm_str;
979 pp2_relevance_token_t prt =
980 pp2_relevance_tokenize(service->mergekey_pct);
982 pp2_relevance_first(prt, (const char *) value, 0);
983 if (wrbuf_len(norm_wr) > 0)
984 wrbuf_puts(norm_wr, " ");
985 wrbuf_puts(norm_wr, name);
987 pp2_relevance_token_next(prt)))
991 wrbuf_puts(norm_wr, " ");
992 wrbuf_puts(norm_wr, norm_str);
996 pp2_relevance_token_destroy(prt);
1006 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1007 struct conf_service *service, NMEM nmem)
1009 char *mergekey_norm = 0;
1010 xmlNode *root = xmlDocGetRootElement(doc);
1011 WRBUF norm_wr = wrbuf_alloc();
1013 /* consider mergekey from XSL first */
1014 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1017 const char *norm_str;
1018 pp2_relevance_token_t prt =
1019 pp2_relevance_tokenize(service->mergekey_pct);
1021 pp2_relevance_first(prt, (const char *) mergekey, 0);
1022 while ((norm_str = pp2_relevance_token_next(prt)))
1026 if (wrbuf_len(norm_wr))
1027 wrbuf_puts(norm_wr, " ");
1028 wrbuf_puts(norm_wr, norm_str);
1031 pp2_relevance_token_destroy(prt);
1036 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1038 for (field_id = 0; field_id < service->num_metadata; field_id++)
1040 struct conf_metadata *ser_md = &service->metadata[field_id];
1041 if (ser_md->mergekey != Metadata_mergekey_no)
1043 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1045 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1047 /* no mergekey on this one and it is required..
1048 Generate unique key instead */
1049 wrbuf_rewind(norm_wr);
1056 /* generate unique key if none is not generated already or is empty */
1057 if (wrbuf_len(norm_wr) == 0)
1059 wrbuf_printf(norm_wr, "%s-%d",
1060 client_get_database(cl)->database->url, record_no);
1062 if (wrbuf_len(norm_wr) > 0)
1063 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1064 wrbuf_destroy(norm_wr);
1065 return mergekey_norm;
1068 /** \brief see if metadata for pz:recordfilter exists
1069 \param root xml root element of normalized record
1070 \param sdb session database for client
1071 \retval 0 if there is no metadata for pz:recordfilter
1072 \retval 1 if there is metadata for pz:recordfilter
1074 If there is no pz:recordfilter defined, this function returns 1
1078 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1083 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1088 for (n = root->children; n; n = n->next)
1090 if (n->type != XML_ELEMENT_NODE)
1092 if (!strcmp((const char *) n->name, "metadata"))
1094 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1098 const char *eq = strchr(s, '~');
1103 if (len == strlen((const char *)type) &&
1104 !memcmp((const char *) type, s, len))
1106 xmlChar *value = xmlNodeGetContent(n);
1107 if (value && *value)
1109 if (!eq || strstr((const char *) value, eq+1))
1122 static int ingest_to_cluster(struct client *cl,
1126 const char *mergekey_norm);
1128 /** \brief ingest XML record
1129 \param cl client holds the result set for record
1130 \param rec record buffer (0 terminated)
1131 \param record_no record position (1, 2, ..)
1135 int ingest_record(struct client *cl, const char *rec,
1136 int record_no, NMEM nmem)
1138 struct session *se = client_get_session(cl);
1140 struct session_database *sdb = client_get_database(cl);
1141 struct conf_service *service = se->service;
1142 xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem);
1144 const char *mergekey_norm;
1149 root = xmlDocGetRootElement(xdoc);
1151 if (!check_record_filter(root, sdb))
1153 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1154 sdb->database->url);
1159 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1162 yaz_log(YLOG_WARN, "Got no mergekey");
1167 pazpar2_sleep(0.01);
1170 if (client_get_session(cl) == se)
1171 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1180 static int ingest_to_cluster(struct client *cl,
1184 const char *mergekey_norm)
1189 struct session_database *sdb = client_get_database(cl);
1190 struct session *se = client_get_session(cl);
1191 struct conf_service *service = se->service;
1192 struct record *record = record_create(se->nmem,
1193 service->num_metadata,
1194 service->num_sortkeys, cl,
1196 struct record_cluster *cluster = reclist_insert(se->reclist,
1203 if (global_parameters.dump_records)
1204 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1205 sdb->database->url, record_no);
1206 relevance_newrec(se->relevance, cluster);
1208 // now parsing XML record and adding data to cluster or record metadata
1209 for (n = root->children; n; n = n->next)
1211 pp2_relevance_token_t prt;
1218 if (n->type != XML_ELEMENT_NODE)
1220 if (!strcmp((const char *) n->name, "metadata"))
1222 struct conf_metadata *ser_md = 0;
1223 struct conf_sortkey *ser_sk = 0;
1224 struct record_metadata **wheretoput = 0;
1225 struct record_metadata *rec_md = 0;
1226 int md_field_id = -1;
1227 int sk_field_id = -1;
1229 type = xmlGetProp(n, (xmlChar *) "type");
1230 value = xmlNodeListGetString(xdoc, n->children, 1);
1232 if (!type || !value || !*value)
1236 = conf_service_metadata_field_id(service, (const char *) type);
1237 if (md_field_id < 0)
1239 if (se->number_of_warnings_unknown_metadata == 0)
1242 "Ignoring unknown metadata element: %s", type);
1244 se->number_of_warnings_unknown_metadata++;
1248 ser_md = &service->metadata[md_field_id];
1250 if (ser_md->sortkey_offset >= 0){
1251 sk_field_id = ser_md->sortkey_offset;
1252 ser_sk = &service->sortkeys[sk_field_id];
1255 // non-merged metadata
1256 rec_md = record_metadata_init(se->nmem, (const char *) value,
1257 ser_md->type, n->properties);
1260 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1264 wheretoput = &record->metadata[md_field_id];
1266 wheretoput = &(*wheretoput)->next;
1267 *wheretoput = rec_md;
1270 rec_md = record_metadata_init(se->nmem, (const char *) value,
1272 wheretoput = &cluster->metadata[md_field_id];
1274 // and polulate with data:
1275 // assign cluster or record based on merge action
1276 if (ser_md->merge == Metadata_merge_unique)
1278 struct record_metadata *mnode;
1279 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1280 if (!strcmp((const char *) mnode->data.text.disp,
1281 rec_md->data.text.disp))
1285 rec_md->next = *wheretoput;
1286 *wheretoput = rec_md;
1289 else if (ser_md->merge == Metadata_merge_longest)
1292 || strlen(rec_md->data.text.disp)
1293 > strlen((*wheretoput)->data.text.disp))
1295 *wheretoput = rec_md;
1298 const char *sort_str = 0;
1300 ser_sk->type == Metadata_sortkey_skiparticle;
1302 if (!cluster->sortkeys[sk_field_id])
1303 cluster->sortkeys[sk_field_id] =
1304 nmem_malloc(se->nmem,
1305 sizeof(union data_types));
1307 prt = pp2_relevance_tokenize(service->sort_pct);
1309 pp2_relevance_first(prt, rec_md->data.text.disp,
1312 pp2_relevance_token_next(prt);
1314 sort_str = pp2_get_sort(prt);
1316 cluster->sortkeys[sk_field_id]->text.disp =
1317 rec_md->data.text.disp;
1320 sort_str = rec_md->data.text.disp;
1322 "Could not make sortkey. Bug #1858");
1324 cluster->sortkeys[sk_field_id]->text.sort =
1325 nmem_strdup(se->nmem, sort_str);
1327 yaz_log(YLOG_LOG, "text disp=%s",
1328 cluster->sortkeys[sk_field_id]->text.disp);
1329 yaz_log(YLOG_LOG, "text sort=%s",
1330 cluster->sortkeys[sk_field_id]->text.sort);
1332 pp2_relevance_token_destroy(prt);
1336 else if (ser_md->merge == Metadata_merge_all)
1338 rec_md->next = *wheretoput;
1339 *wheretoput = rec_md;
1341 else if (ser_md->merge == Metadata_merge_range)
1345 *wheretoput = rec_md;
1347 cluster->sortkeys[sk_field_id]
1352 int this_min = rec_md->data.number.min;
1353 int this_max = rec_md->data.number.max;
1354 if (this_min < (*wheretoput)->data.number.min)
1355 (*wheretoput)->data.number.min = this_min;
1356 if (this_max > (*wheretoput)->data.number.max)
1357 (*wheretoput)->data.number.max = this_max;
1362 // ranking of _all_ fields enabled ...
1364 relevance_countwords(se->relevance, cluster,
1365 (char *) value, ser_md->rank,
1368 // construct facets ...
1369 if (ser_md->termlist)
1371 if (ser_md->type == Metadata_type_year)
1374 sprintf(year, "%d", rec_md->data.number.max);
1375 add_facet(se, (char *) type, year);
1376 if (rec_md->data.number.max != rec_md->data.number.min)
1378 sprintf(year, "%d", rec_md->data.number.min);
1379 add_facet(se, (char *) type, year);
1383 add_facet(se, (char *) type, (char *) value);
1393 if (se->number_of_warnings_unknown_elements == 0)
1395 "Unexpected element in internal record: %s", n->name);
1396 se->number_of_warnings_unknown_elements++;
1404 relevance_donerecord(se->relevance, cluster);
1405 se->total_records++;
1413 * c-file-style: "Stroustrup"
1414 * indent-tabs-mode: nil
1416 * vim: shiftwidth=4 tabstop=8 expandtab