-/* $Id: logic.c,v 1.26 2007-04-27 12:17:04 marc Exp $
+/* $Id: logic.c,v 1.45 2007-06-15 19:35:17 adam Exp $
Copyright (c) 2006-2007, Index Data.
This file is part of Pazpar2.
#include <yaz/nmem.h>
#include <yaz/query-charset.h>
#include <yaz/querytowrbuf.h>
-#if YAZ_VERSIONL >= 0x020163
#include <yaz/oid_db.h>
-#endif
#if HAVE_CONFIG_H
#include "cconfig.h"
#include "settings.h"
#include "normalize7bit.h"
+#define TERMLIST_HIGH_SCORE 25
+
#define MAX_CHUNK 15
// Note: Some things in this structure will eventually move to configuration
{
"",
"",
- "",
- "",
- 0,
+ "",
0,
+ 0, /* dump_records */
+ 0, /* debug_mode */
30,
"81",
"Index Data PazPar2",
s->termlists[i].name = nmem_strdup(s->nmem, type);
s->termlists[i].termlist
- = termlist_create(s->nmem, s->expected_maxrecs, 15);
+ = termlist_create(s->nmem, s->expected_maxrecs,
+ TERMLIST_HIGH_SCORE);
s->num_termlists = i + 1;
}
termlist_insert(s->termlists[i].termlist, value);
}
-xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
+xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
{
- struct database_retrievalmap *m;
struct database *db = sdb->database;
- xmlNode *res;
- xmlDoc *rdoc;
+ xmlDoc *rdoc = 0;
+ const Odr_oid *oid = rec->direct_reference;
- // First normalize to XML
- if (sdb->yaz_marc)
+ /* convert response record to XML somehow */
+ if (rec->which == Z_External_octet && oid
+ && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
{
- char *buf;
- int len;
- if (rec->which != Z_External_octet)
+ /* xml already */
+ rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len);
+ if (!rdoc)
{
- yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
+ yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
db->url);
return 0;
}
- buf = (char*) rec->u.octet_aligned->buf;
- len = rec->u.octet_aligned->len;
- if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
+ }
+ else if (oid && yaz_oid_is_iso2709(oid))
+ {
+ /* ISO2709 gets converted to MARCXML */
+ if (!sdb->yaz_marc)
{
- yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
+ yaz_log(YLOG_FATAL, "Unable to handle ISO2709 record");
return 0;
}
-
- yaz_marc_write_using_libxml2(sdb->yaz_marc, 1);
- if (yaz_marc_write_xml(sdb->yaz_marc, &res,
- "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
+ else
{
- yaz_log(YLOG_WARN, "Failed to encode as XML %s",
- db->url);
- return 0;
+ xmlNode *res;
+ char *buf;
+ int len;
+
+ if (rec->which != Z_External_octet)
+ {
+ yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
+ db->url);
+ return 0;
+ }
+ buf = (char*) rec->u.octet_aligned->buf;
+ len = rec->u.octet_aligned->len;
+ if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
+ {
+ yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
+ return 0;
+ }
+
+ yaz_marc_write_using_libxml2(sdb->yaz_marc, 1);
+ if (yaz_marc_write_xml(sdb->yaz_marc, &res,
+ "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
+ {
+ yaz_log(YLOG_WARN, "Failed to encode as XML %s",
+ db->url);
+ return 0;
+ }
+ rdoc = xmlNewDoc((xmlChar *) "1.0");
+ xmlDocSetRootElement(rdoc, res);
}
- rdoc = xmlNewDoc((xmlChar *) "1.0");
- xmlDocSetRootElement(rdoc, res);
-
}
else
{
+ char oid_name_buf[OID_STR_MAX];
+ const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
yaz_log(YLOG_FATAL,
- "Unknown native_syntax in normalize_record from %s",
- db->url);
+ "Unable to handle record of type %s from %s",
+ oid_name, db->url);
return 0;
}
-
+
if (global_parameters.dump_records){
fprintf(stderr,
"Input Record (normalized) from %s\n----------------\n",
xmlDocDump(stderr, rdoc);
#endif
}
+ return rdoc;
+}
- for (m = sdb->map; m; m = m->next){
- xmlDoc *new = 0;
-
-#if 1
- {
- xmlNodePtr root = 0;
- new = xsltApplyStylesheet(m->stylesheet, rdoc, 0);
- root= xmlDocGetRootElement(new);
- if (!new || !root || !(root->children))
- {
- yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
- sdb->database->url);
- xmlFreeDoc(new);
+xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
+{
+ struct database_retrievalmap *m;
+ xmlDoc *rdoc = record_to_xml(sdb, rec);
+ if (rdoc)
+ {
+ for (m = sdb->map; m; m = m->next){
+ xmlDoc *new = 0;
+
+ {
+ xmlNodePtr root = 0;
+ new = xsltApplyStylesheet(m->stylesheet, rdoc, 0);
+ root= xmlDocGetRootElement(new);
+ if (!new || !root || !(root->children))
+ {
+ yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
+ sdb->database->url);
+ xmlFreeDoc(new);
+ xmlFreeDoc(rdoc);
+ return 0;
+ }
+ }
+
xmlFreeDoc(rdoc);
- return 0;
- }
+ rdoc = new;
}
-#else
- // do it another way to detect transformation errors right now
- // but does not seem to work either!
+ if (global_parameters.dump_records)
{
- xsltTransformContextPtr ctxt;
- ctxt = xsltNewTransformContext(m->stylesheet, rdoc);
- new = xsltApplyStylesheetUser(m->stylesheet, rdoc, 0, 0, 0, ctxt);
- if ((ctxt->state == XSLT_STATE_ERROR) ||
- (ctxt->state == XSLT_STATE_STOPPED)){
- yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
- cl->database->database->url);
- xmlFreeDoc(new);
- xmlFreeDoc(rdoc);
- return 0;
- }
- }
-#endif
-
- xmlFreeDoc(rdoc);
- rdoc = new;
- }
- if (global_parameters.dump_records)
- {
- fprintf(stderr, "Record from %s\n----------------\n",
- sdb->database->url);
+ fprintf(stderr, "Record from %s\n----------------\n",
+ sdb->database->url);
#if LIBXML_VERSION >= 20600
- xmlDocFormatDump(stderr, rdoc, 1);
+ xmlDocFormatDump(stderr, rdoc, 1);
#else
- xmlDocDump(stderr, rdoc);
+ xmlDocDump(stderr, rdoc);
#endif
+ }
}
return rdoc;
}
return 0;
}
+// called if watch should be removed because http_channel is to be destroyed
+static void session_watch_cancel(void *data, struct http_channel *c)
+{
+ struct session_watchentry *ent = data;
-void session_set_watch(struct session *s, int what,
- session_watchfun fun, void *data)
+ ent->fun = 0;
+ ent->data = 0;
+ ent->obs = 0;
+}
+
+// set watch. Returns 0=OK, -1 if watch is already set
+int session_set_watch(struct session *s, int what,
+ session_watchfun fun, void *data,
+ struct http_channel *chan)
{
+ if (s->watchlist[what].fun)
+ return -1;
s->watchlist[what].fun = fun;
s->watchlist[what].data = data;
+ s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
+ session_watch_cancel);
+ return 0;
}
void session_alert_watch(struct session *s, int what)
{
if (!s->watchlist[what].fun)
return;
+ http_remove_observer(s->watchlist[what].obs);
(*s->watchlist[what].fun)(s->watchlist[what].data);
s->watchlist[what].fun = 0;
s->watchlist[what].data = 0;
+ s->watchlist[what].obs = 0;
}
//callback for grep_databases
return res;
}
-char *search(struct session *se, char *query, char *filter)
+enum pazpar2_error_code search(struct session *se,
+ char *query, char *filter,
+ const char **addinfo)
{
int live_channels = 0;
struct client *cl;
yaz_log(YLOG_DEBUG, "Search");
+ *addinfo = 0;
nmem_reset(se->nmem);
criteria = parse_filter(se->nmem, filter);
se->requestid++;
se->expected_maxrecs = maxrecs;
}
else
- return "NOTARGETS";
+ return PAZPAR2_NO_TARGETS;
se->relevance = 0;
for (cl = se->clients; cl; cl = client_next_in_session(cl))
{
if (prepare_session_database(se, client_get_database(cl)) < 0)
- return "CONFIG_ERROR";
+ {
+ *addinfo = client_get_database(cl)->database->url;
+ return PAZPAR2_CONFIG_TARGET;
+ }
// Query must parse for all targets
- if (client_parse_query(cl, query) < 0)
- return "QUERY";
+ if (client_parse_query(cl, query) < 0)
+ {
+ *addinfo = "query";
+ return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+ }
}
for (cl = se->clients; cl; cl = client_next_in_session(cl))
- {
client_prep_connection(cl);
- }
- return 0;
+ return PAZPAR2_NO_ERROR;
}
// Creates a new session_database object for a database
new->database = db;
new->yaz_marc = 0;
+
+#ifdef HAVE_ICU
+ if (global_parameters.server && global_parameters.server->icu_chn)
+ new->pct = pp2_charset_create(global_parameters.server->icu_chn);
+ else
+ new->pct = pp2_charset_create(0);
+#else // HAVE_ICU
+ new->pct = pp2_charset_create(0);
+#endif // HAVE_ICU
+
new->map = 0;
new->settings
= nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
xsltFreeStylesheet(m->stylesheet);
if (sdb->yaz_marc)
yaz_marc_destroy(sdb->yaz_marc);
+ if (sdb->pct)
+ pp2_charset_destroy(sdb->pct);
}
// Initialize session_database list -- this represents this session's view
{
struct session_database *sdb = find_session_database(se, dbname);
struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
- int offset = settings_offset(setting);
+ int offset = settings_offset_cprefix(setting);
if (offset < 0)
{
yaz_log(YLOG_WARN, "Unknown setting %s", setting);
return;
}
+ // Jakub: This breaks the filter setting.
+ /*if (offset == PZ_ID)
+ {
+ yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
+ return;
+ }*/
new->precedence = 0;
new->target = dbname;
new->name = setting;
{
struct session_database *sdb;
- yaz_log(YLOG_LOG, "Destroying session");
while (s->clients)
client_destroy(s->clients);
for (sdb = s->databases; sdb; sdb = sdb->next)
yaz_log(YLOG_DEBUG, "New Pazpar2 session");
+ session->relevance = 0;
session->total_hits = 0;
session->total_records = 0;
session->num_termlists = 0;
yaz_timing_t t = yaz_timing_create();
#endif
- for (spp = sp; spp; spp = spp->next)
- if (spp->type == Metadata_sortkey_relevance)
- {
- relevance_prepare_read(s->relevance, s->reclist);
- break;
- }
- reclist_sort(s->reclist, sp);
-
- *total = s->reclist->num_records;
- *sumhits = s->total_hits;
-
- for (i = 0; i < start; i++)
- if (!reclist_read_record(s->reclist))
- {
- *num = 0;
- recs = 0;
- break;
- }
-
- for (i = 0; i < *num; i++)
+ if (!s->relevance)
{
- struct record_cluster *r = reclist_read_record(s->reclist);
- if (!r)
+ *num = 0;
+ *total = 0;
+ *sumhits = 0;
+ recs = 0;
+ }
+ else
+ {
+ for (spp = sp; spp; spp = spp->next)
+ if (spp->type == Metadata_sortkey_relevance)
+ {
+ relevance_prepare_read(s->relevance, s->reclist);
+ break;
+ }
+ reclist_sort(s->reclist, sp);
+
+ *total = s->reclist->num_records;
+ *sumhits = s->total_hits;
+
+ for (i = 0; i < start; i++)
+ if (!reclist_read_record(s->reclist))
+ {
+ *num = 0;
+ recs = 0;
+ break;
+ }
+
+ for (i = 0; i < *num; i++)
{
- *num = i;
- break;
+ struct record_cluster *r = reclist_read_record(s->reclist);
+ if (!r)
+ {
+ *num = i;
+ break;
+ }
+ recs[i] = r;
}
- recs[i] = r;
}
#if USE_TIMING
yaz_timing_stop(t);
http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
}
-void start_zproxy(void)
-{
- struct conf_server *ser = global_parameters.server;
-
- if (*global_parameters.zproxy_override){
- yaz_log(YLOG_LOG, "Z39.50 proxy %s",
- global_parameters.zproxy_override);
- return;
- }
-
- else if (ser->zproxy_host || ser->zproxy_port)
- {
- char hp[128] = "";
-
- strcpy(hp, ser->zproxy_host ? ser->zproxy_host : "");
- if (ser->zproxy_port)
- {
- if (*hp)
- strcat(hp, ":");
- else
- strcat(hp, "@:");
-
- sprintf(hp + strlen(hp), "%d", ser->zproxy_port);
- }
- strcpy(global_parameters.zproxy_override, hp);
- yaz_log(YLOG_LOG, "Z39.50 proxy %s",
- global_parameters.zproxy_override);
-
- }
- else
- return;
-}
// Master list of connections we're handling events to
static IOCHAN channel_list = 0;
return 0;
}
-#if 0
- record = nmem_malloc(se->nmem, sizeof(struct record));
- record->next = 0;
- record->client = cl;
- record->metadata = nmem_malloc(se->nmem,
- sizeof(struct record_metadata*) * service->num_metadata);
- memset(record->metadata, 0,
- sizeof(struct record_metadata*) * service->num_metadata);
-
-#else
record = record_create(se->nmem,
- service->num_metadata, service->num_sortkeys);
- record_assign_client(record, cl);
-#endif
+ service->num_metadata, service->num_sortkeys, cl,
+ record_no);
mergekey_norm = (xmlChar *) nmem_strdup(se->nmem, (char*) mergekey);
xmlFree(mergekey);
int first, last;
type = xmlGetProp(n, (xmlChar *) "type");
- value = xmlNodeListGetString(xdoc, n->children, 0);
+ value = xmlNodeListGetString(xdoc, n->children, 1);
if (!type || !value)
continue;
-#if 0
- // First, find out what field we're looking at
- for (md_field_id = 0; md_field_id < service->num_metadata;
- md_field_id++)
- if (!strcmp((const char *) type,
- service->metadata[md_field_id].name))
- {
- ser_md = &service->metadata[md_field_id];
- if (ser_md->sortkey_offset >= 0){
- sk_field_id = ser_md->sortkey_offset;
- ser_sk = &service->sortkeys[sk_field_id];
- }
- break;
- }
-
- if (!ser_md)
- {
- yaz_log(YLOG_WARN,
- "Ignoring unknown metadata element: %s", type);
- continue;
- }
-
-#else
md_field_id
= conf_service_metadata_field_id(service, (const char *) type);
if (md_field_id < 0)
sk_field_id = ser_md->sortkey_offset;
ser_sk = &service->sortkeys[sk_field_id];
}
-#endif
// Find out where we are putting it - based on merge or not
if (ser_md->merge == Metadata_merge_no)
wheretoput = &cluster->metadata[md_field_id];
// create new record_metadata
-#if 0
- rec_md = nmem_malloc(se->nmem, sizeof(struct record_metadata));
- rec_md->next = 0;
-#else
rec_md = record_metadata_create(se->nmem);
-#endif
// and polulate with data:
// type based charmapping decisions follow here
if (ser_md->type == Metadata_type_generic)
{
-#if 0
- char *p, *pe;
- for (p = (char *) value; *p && isspace(*p); p++)
- ;
- for (pe = p + strlen(p) - 1;
- pe > p && strchr(" ,/.:([", *pe); pe--)
- *pe = '\0';
-#else
char * p = (char *) value;
p = normalize7bit_generic(p, " ,/.:([");
-#endif
rec_md->data.text = nmem_strdup(se->nmem, p);
continue;
}
-#if 0 // this test does not belong here.
- // the condition is enforced in the constructor
- // inside conf_metadata_assign()
- // and will _never_ occur
- if (ser_md->type == Metadata_type_year
- && ser_md->merge != Metadata_merge_range)
- {
- yaz_log(YLOG_WARN, "Only range merging supported for years");
- continue;
- }
-# endif
-
-
// and polulate with data:
// assign cluster or record based on merge action
if (ser_md->merge == Metadata_merge_unique)
}
else if (ser_md->merge == Metadata_merge_range)
{
-
-#if 0 // this assert does not belong here.
- // the condition is enforced in the constructor
- // inside conf_metadata_assign()
- // and will _never_ occur
- assert(ser_md->type == Metadata_type_year);
-#endif
-
if (!*wheretoput)
{
*wheretoput = rec_md;
#endif
}
-#if 0 // this else is only entered when Metadata_merge_no
- // but I believe then we should _not_ pollute with log messages
- else
-
- yaz_log(YLOG_WARN,
- "Don't know how to merge on element name %s",
- ser_md->name);
-#endif
-
// ranking of _all_ fields enabled ...
if (ser_md->rank)