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;
455 //callback for grep_databases
456 static void select_targets_callback(void *context, struct session_database *db)
458 struct session *se = (struct session*) context;
459 struct client *cl = client_create();
460 struct client_list *l;
461 client_set_database(cl, db);
463 client_set_session(cl, se);
464 l = xmalloc(sizeof(*l));
466 l->next = se->clients;
470 static void session_remove_clients(struct session *se)
472 struct client_list *l;
481 struct client_list *l_next = l->next;
482 client_lock(l->client);
483 client_set_session(l->client, 0);
484 client_set_database(l->client, 0);
485 client_unlock(l->client);
486 client_destroy(l->client);
492 // Associates a set of clients with a session;
493 // Note: Session-databases represent databases with per-session
495 static int select_targets(struct session *se, const char *filter)
497 return session_grep_databases(se, filter, select_targets_callback);
500 int session_active_clients(struct session *s)
502 struct client_list *l;
505 for (l = s->clients; l; l = l->next)
506 if (client_is_active(l->client))
513 enum pazpar2_error_code search(struct session *se,
515 const char *startrecs, const char *maxrecs,
517 const char **addinfo)
519 int live_channels = 0;
522 struct client_list *l;
525 yaz_log(YLOG_DEBUG, "Search");
529 session_remove_clients(se);
532 reclist_destroy(se->reclist);
534 relevance_destroy(&se->relevance);
535 nmem_reset(se->nmem);
536 se->total_records = se->total_hits = se->total_merged = 0;
537 se->num_termlists = 0;
538 live_channels = select_targets(se, filter);
542 return PAZPAR2_NO_TARGETS;
544 se->reclist = reclist_create(se->nmem);
546 yaz_gettimeofday(&tval);
550 for (l = se->clients; l; l = l->next)
552 struct client *cl = l->client;
555 client_set_maxrecs(cl, atoi(maxrecs));
557 client_set_startrecs(cl, atoi(startrecs));
558 if (prepare_session_database(se, client_get_database(cl)) < 0)
560 else if (client_parse_query(cl, query) < 0)
565 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
566 se->service->z3950_session_timeout,
567 se->service->server->iochan_man,
569 client_start_search(cl);
578 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
581 return PAZPAR2_NO_TARGETS;
583 return PAZPAR2_NO_ERROR;
586 // Creates a new session_database object for a database
587 static void session_init_databases_fun(void *context, struct database *db)
589 struct session *se = (struct session *) context;
590 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
596 assert(db->settings);
597 new->settings = nmem_malloc(se->session_nmem,
598 sizeof(struct settings *) * db->num_settings);
599 new->num_settings = db->num_settings;
600 for (i = 0; i < db->num_settings; i++)
602 struct setting *setting = db->settings[i];
603 new->settings[i] = setting;
605 new->next = se->databases;
609 // Doesn't free memory associated with sdb -- nmem takes care of that
610 static void session_database_destroy(struct session_database *sdb)
615 // Initialize session_database list -- this represents this session's view
616 // of the database list -- subject to modification by the settings ws command
617 void session_init_databases(struct session *se)
620 predef_grep_databases(se, se->service, session_init_databases_fun);
623 // Probably session_init_databases_fun should be refactored instead of
625 static struct session_database *load_session_database(struct session *se,
628 struct database *db = new_database(id, se->session_nmem);
630 resolve_database(se->service, db);
632 session_init_databases_fun((void*) se, db);
634 // New sdb is head of se->databases list
635 return se->databases;
638 // Find an existing session database. If not found, load it
639 static struct session_database *find_session_database(struct session *se,
642 struct session_database *sdb;
644 for (sdb = se->databases; sdb; sdb = sdb->next)
645 if (!strcmp(sdb->database->url, id))
647 return load_session_database(se, id);
650 // Apply a session override to a database
651 void session_apply_setting(struct session *se, char *dbname, char *setting,
654 struct session_database *sdb = find_session_database(se, dbname);
655 struct conf_service *service = se->service;
656 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
657 int offset = settings_create_offset(service, setting);
659 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
662 new->target = dbname;
665 new->next = sdb->settings[offset];
666 sdb->settings[offset] = new;
668 // Force later recompute of settings-driven data structures
669 // (happens when a search starts and client connections are prepared)
681 void destroy_session(struct session *se)
683 struct session_database *sdb;
685 yaz_log(YLOG_DEBUG, "%p Pazpar2 session destroy", se);
686 session_remove_clients(se);
688 for (sdb = se->databases; sdb; sdb = sdb->next)
689 session_database_destroy(sdb);
690 normalize_cache_destroy(se->normalize_cache);
691 relevance_destroy(&se->relevance);
692 reclist_destroy(se->reclist);
693 nmem_destroy(se->nmem);
694 service_destroy(se->service);
695 yaz_mutex_destroy(&se->session_mutex);
696 wrbuf_destroy(se->wrbuf);
699 struct session *new_session(NMEM nmem, struct conf_service *service,
703 struct session *session = nmem_malloc(nmem, sizeof(*session));
705 yaz_log(YLOG_DEBUG, "%p New Pazpar2 session", session);
707 session->service = service;
708 session->relevance = 0;
709 session->total_hits = 0;
710 session->total_records = 0;
711 session->number_of_warnings_unknown_elements = 0;
712 session->number_of_warnings_unknown_metadata = 0;
713 session->num_termlists = 0;
714 session->reclist = 0;
715 session->clients = 0;
716 session->session_nmem = nmem;
717 session->nmem = nmem_create();
718 session->wrbuf = wrbuf_alloc();
719 session->databases = 0;
720 for (i = 0; i <= SESSION_WATCH_MAX; i++)
722 session->watchlist[i].data = 0;
723 session->watchlist[i].fun = 0;
725 session->normalize_cache = normalize_cache_create();
726 session->session_mutex = 0;
727 pazpar2_mutex_create(&session->session_mutex, name);
732 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
734 struct hitsbytarget *res = 0;
735 struct client_list *l;
739 for (l = se->clients; l; l = l->next)
742 res = nmem_malloc(nmem, sizeof(*res) * sz);
744 for (l = se->clients; l; l = l->next)
746 struct client *cl = l->client;
747 WRBUF w = wrbuf_alloc();
748 const char *name = session_setting_oneval(client_get_database(cl),
751 res[*count].id = client_get_database(cl)->database->url;
752 res[*count].name = *name ? name : "Unknown";
753 res[*count].hits = client_get_hits(cl);
754 res[*count].records = client_get_num_records(cl);
755 res[*count].diagnostic = client_get_diagnostic(cl);
756 res[*count].state = client_get_state_str(cl);
757 res[*count].connected = client_get_connection(cl) ? 1 : 0;
758 session_settings_dump(se, client_get_database(cl), w);
759 res[*count].settings_xml = w;
766 struct termlist_score **termlist(struct session *se, const char *name, int *num)
769 struct termlist_score **tl = 0;
772 for (i = 0; i < se->num_termlists; i++)
773 if (!strcmp((const char *) se->termlists[i].name, name))
775 tl = termlist_highscore(se->termlists[i].termlist, num);
782 #ifdef MISSING_HEADERS
783 void report_nmem_stats(void)
785 size_t in_use, is_free;
787 nmem_get_memory_in_use(&in_use);
788 nmem_get_memory_free(&is_free);
790 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
791 (long) in_use, (long) is_free);
795 struct record_cluster *show_single_start(struct session *se, const char *id,
796 struct record_cluster **prev_r,
797 struct record_cluster **next_r)
799 struct record_cluster *r;
802 reclist_enter(se->reclist);
805 while ((r = reclist_read_record(se->reclist)))
807 if (!strcmp(r->recid, id))
809 *next_r = reclist_read_record(se->reclist);
814 reclist_leave(se->reclist);
820 void show_single_stop(struct session *se, struct record_cluster *rec)
825 struct record_cluster **show_range_start(struct session *se,
826 struct reclist_sortparms *sp,
827 int start, int *num, int *total, Odr_int *sumhits)
829 struct record_cluster **recs;
830 struct reclist_sortparms *spp;
833 yaz_timing_t t = yaz_timing_create();
836 recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
846 for (spp = sp; spp; spp = spp->next)
847 if (spp->type == Metadata_sortkey_relevance)
849 relevance_prepare_read(se->relevance, se->reclist);
852 reclist_sort(se->reclist, sp);
854 reclist_enter(se->reclist);
855 *total = reclist_get_num_records(se->reclist);
856 *sumhits = se->total_hits;
858 for (i = 0; i < start; i++)
859 if (!reclist_read_record(se->reclist))
866 for (i = 0; i < *num; i++)
868 struct record_cluster *r = reclist_read_record(se->reclist);
876 reclist_leave(se->reclist);
880 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
881 yaz_timing_get_real(t), yaz_timing_get_user(t),
882 yaz_timing_get_sys(t));
883 yaz_timing_destroy(&t);
888 void show_range_stop(struct session *se, struct record_cluster **recs)
893 void statistics(struct session *se, struct statistics *stat)
895 struct client_list *l;
898 memset(stat, 0, sizeof(*stat));
899 for (l = se->clients; l; l = l->next)
901 struct client *cl = l->client;
902 if (!client_get_connection(cl))
903 stat->num_no_connection++;
904 switch (client_get_state(cl))
906 case Client_Connecting: stat->num_connecting++; break;
907 case Client_Working: stat->num_working++; break;
908 case Client_Idle: stat->num_idle++; break;
909 case Client_Failed: stat->num_failed++; break;
910 case Client_Error: stat->num_error++; break;
915 stat->num_hits = se->total_hits;
916 stat->num_records = se->total_records;
918 stat->num_clients = count;
921 static struct record_metadata *record_metadata_init(
922 NMEM nmem, const char *value, enum conf_metadata_type type,
923 struct _xmlAttr *attr)
925 struct record_metadata *rec_md = record_metadata_create(nmem);
926 struct record_metadata_attr **attrp = &rec_md->attributes;
928 for (; attr; attr = attr->next)
930 if (attr->children && attr->children->content)
932 if (strcmp((const char *) attr->name, "type"))
933 { /* skip the "type" attribute.. Its value is already part of
934 the element in output (md-%s) and so repeating it here
936 *attrp = nmem_malloc(nmem, sizeof(**attrp));
938 nmem_strdup(nmem, (const char *) attr->name);
940 nmem_strdup(nmem, (const char *) attr->children->content);
941 attrp = &(*attrp)->next;
947 if (type == Metadata_type_generic)
949 char *p = nmem_strdup(nmem, value);
951 p = normalize7bit_generic(p, " ,/.:([");
953 rec_md->data.text.disp = p;
954 rec_md->data.text.sort = 0;
956 else if (type == Metadata_type_year || type == Metadata_type_date)
961 if (type == Metadata_type_date)
963 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
966 rec_md->data.number.min = first;
967 rec_md->data.number.max = last;
974 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
975 struct conf_service *service, WRBUF norm_wr)
979 for (n = root->children; n; n = n->next)
981 if (n->type != XML_ELEMENT_NODE)
983 if (!strcmp((const char *) n->name, "metadata"))
985 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
987 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
989 else if (!strcmp(name, (const char *) type))
991 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
994 const char *norm_str;
995 pp2_relevance_token_t prt =
996 pp2_relevance_tokenize(service->mergekey_pct);
998 pp2_relevance_first(prt, (const char *) value, 0);
999 if (wrbuf_len(norm_wr) > 0)
1000 wrbuf_puts(norm_wr, " ");
1001 wrbuf_puts(norm_wr, name);
1003 pp2_relevance_token_next(prt)))
1007 wrbuf_puts(norm_wr, " ");
1008 wrbuf_puts(norm_wr, norm_str);
1012 pp2_relevance_token_destroy(prt);
1022 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
1023 struct conf_service *service, NMEM nmem)
1025 char *mergekey_norm = 0;
1026 xmlNode *root = xmlDocGetRootElement(doc);
1027 WRBUF norm_wr = wrbuf_alloc();
1029 /* consider mergekey from XSL first */
1030 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
1033 const char *norm_str;
1034 pp2_relevance_token_t prt =
1035 pp2_relevance_tokenize(service->mergekey_pct);
1037 pp2_relevance_first(prt, (const char *) mergekey, 0);
1038 while ((norm_str = pp2_relevance_token_next(prt)))
1042 if (wrbuf_len(norm_wr))
1043 wrbuf_puts(norm_wr, " ");
1044 wrbuf_puts(norm_wr, norm_str);
1047 pp2_relevance_token_destroy(prt);
1052 /* no mergekey defined in XSL. Look for mergekey metadata instead */
1054 for (field_id = 0; field_id < service->num_metadata; field_id++)
1056 struct conf_metadata *ser_md = &service->metadata[field_id];
1057 if (ser_md->mergekey != Metadata_mergekey_no)
1059 int r = get_mergekey_from_doc(doc, root, ser_md->name,
1061 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
1063 /* no mergekey on this one and it is required..
1064 Generate unique key instead */
1065 wrbuf_rewind(norm_wr);
1072 /* generate unique key if none is not generated already or is empty */
1073 if (wrbuf_len(norm_wr) == 0)
1075 wrbuf_printf(norm_wr, "%s-%d",
1076 client_get_database(cl)->database->url, record_no);
1078 if (wrbuf_len(norm_wr) > 0)
1079 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1080 wrbuf_destroy(norm_wr);
1081 return mergekey_norm;
1084 /** \brief see if metadata for pz:recordfilter exists
1085 \param root xml root element of normalized record
1086 \param sdb session database for client
1087 \retval 0 if there is no metadata for pz:recordfilter
1088 \retval 1 if there is metadata for pz:recordfilter
1090 If there is no pz:recordfilter defined, this function returns 1
1094 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1099 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1104 for (n = root->children; n; n = n->next)
1106 if (n->type != XML_ELEMENT_NODE)
1108 if (!strcmp((const char *) n->name, "metadata"))
1110 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1114 const char *eq = strchr(s, '~');
1119 if (len == strlen((const char *)type) &&
1120 !memcmp((const char *) type, s, len))
1122 xmlChar *value = xmlNodeGetContent(n);
1123 if (value && *value)
1125 if (!eq || strstr((const char *) value, eq+1))
1138 static int ingest_to_cluster(struct client *cl,
1142 const char *mergekey_norm);
1144 /** \brief ingest XML record
1145 \param cl client holds the result set for record
1146 \param rec record buffer (0 terminated)
1147 \param record_no record position (1, 2, ..)
1148 \param nmem working NMEM
1152 int ingest_record(struct client *cl, const char *rec,
1153 int record_no, NMEM nmem)
1155 struct session *se = client_get_session(cl);
1157 struct session_database *sdb = client_get_database(cl);
1158 struct conf_service *service = se->service;
1159 xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem);
1161 const char *mergekey_norm;
1166 root = xmlDocGetRootElement(xdoc);
1168 if (!check_record_filter(root, sdb))
1170 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1171 sdb->database->url);
1176 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
1179 yaz_log(YLOG_WARN, "Got no mergekey");
1184 if (client_get_session(cl) == se)
1185 ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
1192 static int ingest_to_cluster(struct client *cl,
1196 const char *mergekey_norm)
1201 struct session_database *sdb = client_get_database(cl);
1202 struct session *se = client_get_session(cl);
1203 struct conf_service *service = se->service;
1204 struct record *record = record_create(se->nmem,
1205 service->num_metadata,
1206 service->num_sortkeys, cl,
1208 struct record_cluster *cluster = reclist_insert(se->reclist,
1215 if (global_parameters.dump_records)
1216 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1217 sdb->database->url, record_no);
1218 relevance_newrec(se->relevance, cluster);
1220 // now parsing XML record and adding data to cluster or record metadata
1221 for (n = root->children; n; n = n->next)
1223 pp2_relevance_token_t prt;
1230 if (n->type != XML_ELEMENT_NODE)
1232 if (!strcmp((const char *) n->name, "metadata"))
1234 struct conf_metadata *ser_md = 0;
1235 struct conf_sortkey *ser_sk = 0;
1236 struct record_metadata **wheretoput = 0;
1237 struct record_metadata *rec_md = 0;
1238 int md_field_id = -1;
1239 int sk_field_id = -1;
1241 type = xmlGetProp(n, (xmlChar *) "type");
1242 value = xmlNodeListGetString(xdoc, n->children, 1);
1244 if (!type || !value || !*value)
1248 = conf_service_metadata_field_id(service, (const char *) type);
1249 if (md_field_id < 0)
1251 if (se->number_of_warnings_unknown_metadata == 0)
1254 "Ignoring unknown metadata element: %s", type);
1256 se->number_of_warnings_unknown_metadata++;
1260 ser_md = &service->metadata[md_field_id];
1262 if (ser_md->sortkey_offset >= 0){
1263 sk_field_id = ser_md->sortkey_offset;
1264 ser_sk = &service->sortkeys[sk_field_id];
1267 // non-merged metadata
1268 rec_md = record_metadata_init(se->nmem, (const char *) value,
1269 ser_md->type, n->properties);
1272 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1276 wheretoput = &record->metadata[md_field_id];
1278 wheretoput = &(*wheretoput)->next;
1279 *wheretoput = rec_md;
1282 rec_md = record_metadata_init(se->nmem, (const char *) value,
1284 wheretoput = &cluster->metadata[md_field_id];
1286 // and polulate with data:
1287 // assign cluster or record based on merge action
1288 if (ser_md->merge == Metadata_merge_unique)
1290 struct record_metadata *mnode;
1291 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1292 if (!strcmp((const char *) mnode->data.text.disp,
1293 rec_md->data.text.disp))
1297 rec_md->next = *wheretoput;
1298 *wheretoput = rec_md;
1301 else if (ser_md->merge == Metadata_merge_longest)
1304 || strlen(rec_md->data.text.disp)
1305 > strlen((*wheretoput)->data.text.disp))
1307 *wheretoput = rec_md;
1310 const char *sort_str = 0;
1312 ser_sk->type == Metadata_sortkey_skiparticle;
1314 if (!cluster->sortkeys[sk_field_id])
1315 cluster->sortkeys[sk_field_id] =
1316 nmem_malloc(se->nmem,
1317 sizeof(union data_types));
1319 prt = pp2_relevance_tokenize(service->sort_pct);
1321 pp2_relevance_first(prt, rec_md->data.text.disp,
1324 pp2_relevance_token_next(prt);
1326 sort_str = pp2_get_sort(prt);
1328 cluster->sortkeys[sk_field_id]->text.disp =
1329 rec_md->data.text.disp;
1332 sort_str = rec_md->data.text.disp;
1334 "Could not make sortkey. Bug #1858");
1336 cluster->sortkeys[sk_field_id]->text.sort =
1337 nmem_strdup(se->nmem, sort_str);
1339 yaz_log(YLOG_LOG, "text disp=%s",
1340 cluster->sortkeys[sk_field_id]->text.disp);
1341 yaz_log(YLOG_LOG, "text sort=%s",
1342 cluster->sortkeys[sk_field_id]->text.sort);
1344 pp2_relevance_token_destroy(prt);
1348 else if (ser_md->merge == Metadata_merge_all)
1350 rec_md->next = *wheretoput;
1351 *wheretoput = rec_md;
1353 else if (ser_md->merge == Metadata_merge_range)
1357 *wheretoput = rec_md;
1359 cluster->sortkeys[sk_field_id]
1364 int this_min = rec_md->data.number.min;
1365 int this_max = rec_md->data.number.max;
1366 if (this_min < (*wheretoput)->data.number.min)
1367 (*wheretoput)->data.number.min = this_min;
1368 if (this_max > (*wheretoput)->data.number.max)
1369 (*wheretoput)->data.number.max = this_max;
1374 // ranking of _all_ fields enabled ...
1376 relevance_countwords(se->relevance, cluster,
1377 (char *) value, ser_md->rank,
1380 // construct facets ... unless the client already has reported them
1381 if (ser_md->termlist && !client_has_facet(cl, (char *) type))
1384 if (ser_md->type == Metadata_type_year)
1387 sprintf(year, "%d", rec_md->data.number.max);
1388 add_facet(se, (char *) type, year, 1);
1389 if (rec_md->data.number.max != rec_md->data.number.min)
1391 sprintf(year, "%d", rec_md->data.number.min);
1392 add_facet(se, (char *) type, year, 1);
1396 add_facet(se, (char *) type, (char *) value, 1);
1406 if (se->number_of_warnings_unknown_elements == 0)
1408 "Unexpected element in internal record: %s", n->name);
1409 se->number_of_warnings_unknown_elements++;
1417 relevance_donerecord(se->relevance, cluster);
1418 se->total_records++;
1426 * c-file-style: "Stroustrup"
1427 * indent-tabs-mode: nil
1429 * vim: shiftwidth=4 tabstop=8 expandtab