1 /* $Id: logic.c,v 1.19 2007-04-23 08:15:22 marc Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 // This file contains the primary business logic. Several parts of it should
23 // Eventually be factored into separate modules.
30 #include <sys/socket.h>
36 #include <yaz/marcdisp.h>
37 #include <yaz/comstack.h>
38 #include <yaz/tcpip.h>
39 #include <yaz/proto.h>
40 #include <yaz/readconf.h>
41 #include <yaz/pquery.h>
42 #include <yaz/otherinfo.h>
43 #include <yaz/yaz-util.h>
45 #include <yaz/query-charset.h>
46 #include <yaz/querytowrbuf.h>
47 #if YAZ_VERSIONL >= 0x020163
48 #include <yaz/oid_db.h>
57 #include <yaz/timing.h>
60 #include <netinet/in.h>
65 #include "termlists.h"
67 #include "relevance.h"
74 static void client_fatal(struct client *cl);
75 static void connection_destroy(struct connection *co);
76 static int client_prep_connection(struct client *cl);
77 static void ingest_records(struct client *cl, Z_Records *r);
78 void session_alert_watch(struct session *s, int what);
80 static struct connection *connection_freelist = 0;
81 static struct client *client_freelist = 0;
83 static char *client_states[] = {
87 "Client_Initializing",
92 "Client_Disconnected",
96 // Note: Some things in this structure will eventually move to configuration
97 struct parameters global_parameters =
107 "Index Data PazPar2",
117 static int send_apdu(struct client *c, Z_APDU *a)
119 struct connection *co = c->connection;
123 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
125 odr_perror(global_parameters.odr_out, "Encoding APDU");
128 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
129 r = cs_put(co->link, buf, len);
132 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
137 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
140 odr_reset(global_parameters.odr_out); /* release the APDU structure */
141 co->state = Conn_Waiting;
145 // Set authentication token in init if one is set for the client
146 // TODO: Extend this to handle other schemes than open (should be simple)
147 static void init_authentication(struct client *cl, Z_InitRequest *req)
149 struct session_database *sdb = cl->database;
150 char *auth = session_setting_oneval(sdb, PZ_AUTHENTICATION);
154 Z_IdAuthentication *idAuth = odr_malloc(global_parameters.odr_out,
156 idAuth->which = Z_IdAuthentication_open;
157 idAuth->u.open = auth;
158 req->idAuthentication = idAuth;
162 static void send_init(IOCHAN i)
165 struct connection *co = iochan_getdata(i);
166 struct client *cl = co->client;
167 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
169 a->u.initRequest->implementationId = global_parameters.implementationId;
170 a->u.initRequest->implementationName = global_parameters.implementationName;
171 a->u.initRequest->implementationVersion =
172 global_parameters.implementationVersion;
173 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
174 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
175 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
177 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
178 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
179 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
181 init_authentication(cl, a->u.initRequest);
183 /* add virtual host if tunneling through Z39.50 proxy */
185 if (0 < strlen(global_parameters.zproxy_override)
186 && 0 < strlen(cl->database->database->url))
188 #if YAZ_VERSIONL >= 0x020163
189 yaz_oi_set_string_oid(&a->u.initRequest->otherInfo,
190 global_parameters.odr_out,
191 yaz_oid_userinfo_proxy,
192 1, cl->database->database->url);
194 yaz_oi_set_string_oidval(&a->u.initRequest->otherInfo,
195 global_parameters.odr_out, VAL_PROXY,
196 1, cl->database->database->url);
200 if (send_apdu(cl, a) >= 0)
202 iochan_setflags(i, EVENT_INPUT);
203 cl->state = Client_Initializing;
206 cl->state = Client_Error;
207 odr_reset(global_parameters.odr_out);
210 // Recursively traverse query structure to extract terms.
211 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
223 pull_terms(nmem, n->u.p[0], termlist, num);
224 pull_terms(nmem, n->u.p[1], termlist, num);
227 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
228 for (i = 0; i < numwords; i++)
229 termlist[(*num)++] = words[i];
236 // Extract terms from query into null-terminated termlist
237 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
241 pull_terms(nmem, query, termlist, &num);
245 static void send_search(IOCHAN i)
247 struct connection *co = iochan_getdata(i);
248 struct client *cl = co->client;
249 struct session *se = cl->session;
250 struct session_database *sdb = cl->database;
251 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
255 int ssub = 0, lslb = 100000, mspn = 10;
259 yaz_iconv_t iconv = 0;
261 yaz_log(YLOG_DEBUG, "Sending search to %s", cl->database->database->url);
263 // constructing RPN query
264 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
266 zquery->which = Z_Query_type_1;
267 zquery->u.type_1 = p_query_rpn(global_parameters.odr_out, cl->pquery);
269 // converting to target encoding
270 if ((queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING))){
271 iconv = yaz_iconv_open(queryenc, "UTF-8");
273 yaz_query_charset_convert_rpnquery(zquery->u.type_1,
274 global_parameters.odr_out,
276 yaz_iconv_close(iconv);
278 yaz_log(YLOG_WARN, "Query encoding failed %s %s",
279 cl->database->database->url, queryenc);
282 for (ndb = 0; sdb->database->databases[ndb]; ndb++)
284 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
285 for (ndb = 0; sdb->database->databases[ndb]; ndb++)
286 databaselist[ndb] = sdb->database->databases[ndb];
288 if (!(piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK)) || *piggyback == '1')
290 if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
292 #if YAZ_VERSIONL >= 0x020163
293 a->u.searchRequest->preferredRecordSyntax =
294 yaz_string_to_oid_odr(yaz_oid_std(),
295 CLASS_RECSYN, recsyn,
296 global_parameters.odr_out);
298 a->u.searchRequest->preferredRecordSyntax =
299 yaz_str_to_z3950oid(global_parameters.odr_out,
300 CLASS_RECSYN, recsyn);
303 a->u.searchRequest->smallSetUpperBound = &ssub;
304 a->u.searchRequest->largeSetLowerBound = &lslb;
305 a->u.searchRequest->mediumSetPresentNumber = &mspn;
307 a->u.searchRequest->resultSetName = "Default";
308 a->u.searchRequest->databaseNames = databaselist;
309 a->u.searchRequest->num_databaseNames = ndb;
312 { //scope for sending and logging queries
313 WRBUF wbquery = wrbuf_alloc();
314 yaz_query_to_wrbuf(wbquery, zquery);
317 if (send_apdu(cl, a) >= 0)
319 iochan_setflags(i, EVENT_INPUT);
320 cl->state = Client_Searching;
321 cl->requestid = se->requestid;
322 yaz_log(YLOG_LOG, "SearchRequest %s %s %s",
323 cl->database->database->url,
324 queryenc ? queryenc : "UTF-8",
325 wrbuf_cstr(wbquery));
328 cl->state = Client_Error;
329 yaz_log(YLOG_WARN, "Failed SearchRequest %s %s %s",
330 cl->database->database->url,
331 queryenc ? queryenc : "UTF-8",
332 wrbuf_cstr(wbquery));
335 wrbuf_destroy(wbquery);
338 odr_reset(global_parameters.odr_out);
341 static void send_present(IOCHAN i)
343 struct connection *co = iochan_getdata(i);
344 struct client *cl = co->client;
345 struct session_database *sdb = cl->database;
346 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
348 int start = cl->records + 1;
351 toget = global_parameters.chunk;
352 if (toget > global_parameters.toget - cl->records)
353 toget = global_parameters.toget - cl->records;
354 if (toget > cl->hits - cl->records)
355 toget = cl->hits - cl->records;
357 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
359 a->u.presentRequest->resultSetStartPoint = &start;
360 a->u.presentRequest->numberOfRecordsRequested = &toget;
362 a->u.presentRequest->resultSetId = "Default";
364 if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
366 #if YAZ_VERSIONL >= 0x020163
367 a->u.presentRequest->preferredRecordSyntax =
368 yaz_string_to_oid_odr(yaz_oid_std(),
369 CLASS_RECSYN, recsyn,
370 global_parameters.odr_out);
372 a->u.presentRequest->preferredRecordSyntax =
373 yaz_str_to_z3950oid(global_parameters.odr_out,
374 CLASS_RECSYN, recsyn);
378 if (send_apdu(cl, a) >= 0)
380 iochan_setflags(i, EVENT_INPUT);
381 cl->state = Client_Presenting;
384 cl->state = Client_Error;
385 odr_reset(global_parameters.odr_out);
388 static void do_initResponse(IOCHAN i, Z_APDU *a)
390 struct connection *co = iochan_getdata(i);
391 struct client *cl = co->client;
392 Z_InitResponse *r = a->u.initResponse;
394 yaz_log(YLOG_DEBUG, "Init response %s", cl->database->database->url);
398 cl->state = Client_Idle;
401 cl->state = Client_Failed; // FIXME need to do something to the connection
404 static void do_searchResponse(IOCHAN i, Z_APDU *a)
406 struct connection *co = iochan_getdata(i);
407 struct client *cl = co->client;
408 struct session *se = cl->session;
409 Z_SearchResponse *r = a->u.searchResponse;
411 yaz_log(YLOG_DEBUG, "Search response %s (status=%d)",
412 cl->database->database->url, *r->searchStatus);
414 if (*r->searchStatus)
416 cl->hits = *r->resultCount;
417 se->total_hits += cl->hits;
418 if (r->presentStatus && !*r->presentStatus && r->records)
420 yaz_log(YLOG_DEBUG, "Records in search response %s",
421 cl->database->database->url);
422 ingest_records(cl, r->records);
424 cl->state = Client_Idle;
429 cl->state = Client_Error;
431 Z_Records *recs = r->records;
432 if (recs->which == Z_Records_NSD)
435 "Search response: Non-surrogate diagnostic %s",
436 cl->database->database->url);
437 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
438 cl->state = Client_Error;
444 static void do_closeResponse(IOCHAN i, Z_APDU *a)
446 struct connection *co = iochan_getdata(i);
447 struct client *cl = co->client;
448 /* Z_Close *r = a->u.close; */
450 yaz_log(YLOG_WARN, "Close response %s", cl->database->database->url);
452 cl->state = Client_Failed;
453 connection_destroy(co);
457 char *normalize_mergekey(char *buf, int skiparticle)
459 char *p = buf, *pout = buf;
464 char articles[] = "the den der die des an a "; // must end in space
466 while (*p && !isalnum(*p))
469 while (*p && *p != ' ' && pout - firstword < 62)
470 *(pout++) = tolower(*(p++));
473 if (!strstr(articles, firstword))
480 while (*p && !isalnum(*p))
483 *(pout++) = tolower(*(p++));
486 while (*p && !isalnum(*p))
493 while (pout > buf && *pout == ' ');
498 static void add_facet(struct session *s, const char *type, const char *value)
504 for (i = 0; i < s->num_termlists; i++)
505 if (!strcmp(s->termlists[i].name, type))
507 if (i == s->num_termlists)
509 if (i == SESSION_MAX_TERMLISTS)
511 yaz_log(YLOG_FATAL, "Too many termlists");
515 s->termlists[i].name = nmem_strdup(s->nmem, type);
516 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
517 s->num_termlists = i + 1;
519 termlist_insert(s->termlists[i].termlist, value);
522 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
524 struct database_retrievalmap *m;
525 struct session_database *sdb = cl->database;
526 struct database *db = sdb->database;
530 // First normalize to XML
535 if (rec->which != Z_External_octet)
537 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
541 buf = (char*) rec->u.octet_aligned->buf;
542 len = rec->u.octet_aligned->len;
543 if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
545 yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
549 yaz_marc_write_using_libxml2(sdb->yaz_marc, 1);
550 if (yaz_marc_write_xml(sdb->yaz_marc, &res,
551 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
553 yaz_log(YLOG_WARN, "Failed to encode as XML %s",
557 rdoc = xmlNewDoc((xmlChar *) "1.0");
558 xmlDocSetRootElement(rdoc, res);
564 "Unknown native_syntax in normalize_record from %s",
569 if (global_parameters.dump_records){
571 "Input Record (normalized) from %s\n----------------\n",
573 #if LIBXML_VERSION >= 20600
574 xmlDocFormatDump(stderr, rdoc, 1);
576 xmlDocDump(stderr, rdoc);
580 for (m = sdb->map; m; m = m->next){
586 new = xsltApplyStylesheet(m->stylesheet, rdoc, 0);
587 root= xmlDocGetRootElement(new);
588 if (!new || !root || !(root->children))
590 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
591 cl->database->database->url);
600 // do it another way to detect transformation errors right now
601 // but does not seem to work either!
603 xsltTransformContextPtr ctxt;
604 ctxt = xsltNewTransformContext(m->stylesheet, rdoc);
605 new = xsltApplyStylesheetUser(m->stylesheet, rdoc, 0, 0, 0, ctxt);
606 if ((ctxt->state == XSLT_STATE_ERROR) ||
607 (ctxt->state == XSLT_STATE_STOPPED)){
608 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
609 cl->database->database->url);
620 if (global_parameters.dump_records)
622 fprintf(stderr, "Record from %s\n----------------\n",
623 cl->database->database->url);
624 #if LIBXML_VERSION >= 20600
625 xmlDocFormatDump(stderr, rdoc, 1);
627 xmlDocDump(stderr, rdoc);
633 // Extract what appears to be years from buf, storing highest and
635 static int extract_years(const char *buf, int *first, int *last)
644 while (*buf && !isdigit(*buf))
647 for (e = buf; *e && isdigit(*e); e++)
651 int value = atoi(buf);
652 if (*first < 0 || value < *first)
654 if (*last < 0 || value > *last)
662 static struct record *ingest_record(struct client *cl, Z_External *rec)
664 xmlDoc *xdoc = normalize_record(cl, rec);
667 struct record_cluster *cluster;
668 struct session *se = cl->session;
669 xmlChar *mergekey, *mergekey_norm;
672 struct conf_service *service = global_parameters.server->service;
677 root = xmlDocGetRootElement(xdoc);
678 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
680 yaz_log(YLOG_WARN, "No mergekey found in record");
685 res = nmem_malloc(se->nmem, sizeof(struct record));
688 res->metadata = nmem_malloc(se->nmem,
689 sizeof(struct record_metadata*) * service->num_metadata);
690 memset(res->metadata, 0, sizeof(struct record_metadata*) * service->num_metadata);
692 mergekey_norm = (xmlChar *) nmem_strdup(se->nmem, (char*) mergekey);
694 normalize_mergekey((char *) mergekey_norm, 0);
696 cluster = reclist_insert(se->reclist,
697 global_parameters.server->service,
698 res, (char *) mergekey_norm,
700 if (global_parameters.dump_records)
701 yaz_log(YLOG_LOG, "Cluster id %d from %s (#%d)", cluster->recid,
702 cl->database->database->url, cl->records);
705 /* no room for record */
709 relevance_newrec(se->relevance, cluster);
711 for (n = root->children; n; n = n->next)
719 if (n->type != XML_ELEMENT_NODE)
721 if (!strcmp((const char *) n->name, "metadata"))
723 struct conf_metadata *md = 0;
724 struct conf_sortkey *sk = 0;
725 struct record_metadata **wheretoput, *newm;
729 type = xmlGetProp(n, (xmlChar *) "type");
730 value = xmlNodeListGetString(xdoc, n->children, 0);
735 // First, find out what field we're looking at
736 for (imeta = 0; imeta < service->num_metadata; imeta++)
737 if (!strcmp((const char *) type, service->metadata[imeta].name))
739 md = &service->metadata[imeta];
740 if (md->sortkey_offset >= 0)
741 sk = &service->sortkeys[md->sortkey_offset];
746 yaz_log(YLOG_WARN, "Ignoring unknown metadata element: %s", type);
750 // Find out where we are putting it
751 if (md->merge == Metadata_merge_no)
752 wheretoput = &res->metadata[imeta];
754 wheretoput = &cluster->metadata[imeta];
757 newm = nmem_malloc(se->nmem, sizeof(struct record_metadata));
759 if (md->type == Metadata_type_generic)
762 for (p = (char *) value; *p && isspace(*p); p++)
764 for (pe = p + strlen(p) - 1;
765 pe > p && strchr(" ,/.:([", *pe); pe--)
767 newm->data.text = nmem_strdup(se->nmem, p);
770 else if (md->type == Metadata_type_year)
772 if (extract_years((char *) value, &first, &last) < 0)
777 yaz_log(YLOG_WARN, "Unknown type in metadata element %s", type);
780 if (md->type == Metadata_type_year && md->merge != Metadata_merge_range)
782 yaz_log(YLOG_WARN, "Only range merging supported for years");
785 if (md->merge == Metadata_merge_unique)
787 struct record_metadata *mnode;
788 for (mnode = *wheretoput; mnode; mnode = mnode->next)
789 if (!strcmp((const char *) mnode->data.text, newm->data.text))
793 newm->next = *wheretoput;
797 else if (md->merge == Metadata_merge_longest)
800 strlen(newm->data.text) > strlen((*wheretoput)->data.text))
805 char *s = nmem_strdup(se->nmem, newm->data.text);
806 if (!cluster->sortkeys[md->sortkey_offset])
807 cluster->sortkeys[md->sortkey_offset] =
808 nmem_malloc(se->nmem, sizeof(union data_types));
809 normalize_mergekey(s,
810 (sk->type == Metadata_sortkey_skiparticle));
811 cluster->sortkeys[md->sortkey_offset]->text = s;
815 else if (md->merge == Metadata_merge_all || md->merge == Metadata_merge_no)
817 newm->next = *wheretoput;
820 else if (md->merge == Metadata_merge_range)
822 assert(md->type == Metadata_type_year);
826 (*wheretoput)->data.number.min = first;
827 (*wheretoput)->data.number.max = last;
829 cluster->sortkeys[md->sortkey_offset] = &newm->data;
833 if (first < (*wheretoput)->data.number.min)
834 (*wheretoput)->data.number.min = first;
835 if (last > (*wheretoput)->data.number.max)
836 (*wheretoput)->data.number.max = last;
841 union data_types *sdata = cluster->sortkeys[md->sortkey_offset];
842 yaz_log(YLOG_LOG, "SK range: %d-%d", sdata->number.min, sdata->number.max);
847 yaz_log(YLOG_WARN, "Don't know how to merge on element name %s", md->name);
850 relevance_countwords(se->relevance, cluster,
851 (char *) value, md->rank);
854 if (md->type == Metadata_type_year)
857 sprintf(year, "%d", last);
858 add_facet(se, (char *) type, year);
861 sprintf(year, "%d", first);
862 add_facet(se, (char *) type, year);
866 add_facet(se, (char *) type, (char *) value);
873 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
882 relevance_donerecord(se->relevance, cluster);
888 // Retrieve first defined value for 'name' for given database.
889 // Will be extended to take into account user associated with session
890 char *session_setting_oneval(struct session_database *db, int offset)
892 if (!db->settings[offset])
894 return db->settings[offset]->value;
897 static void ingest_records(struct client *cl, Z_Records *r)
900 yaz_timing_t t = yaz_timing_create();
903 struct session *s = cl->session;
904 Z_NamePlusRecordList *rlist;
907 if (r->which != Z_Records_DBOSD)
909 rlist = r->u.databaseOrSurDiagnostics;
910 for (i = 0; i < rlist->num_records; i++)
912 Z_NamePlusRecord *npr = rlist->records[i];
915 if (npr->which != Z_NamePlusRecord_databaseRecord)
918 "Unexpected record type, probably diagnostic %s",
919 cl->database->database->url);
923 rec = ingest_record(cl, npr->u.databaseRecord);
927 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
928 session_alert_watch(s, SESSION_WATCH_RECORDS);
932 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
933 yaz_timing_get_real(t), yaz_timing_get_user(t),
934 yaz_timing_get_sys(t));
935 yaz_timing_destroy(&t);
939 static void do_presentResponse(IOCHAN i, Z_APDU *a)
941 struct connection *co = iochan_getdata(i);
942 struct client *cl = co->client;
943 Z_PresentResponse *r = a->u.presentResponse;
946 Z_Records *recs = r->records;
947 if (recs->which == Z_Records_NSD)
949 yaz_log(YLOG_WARN, "Non-surrogate diagnostic %s",
950 cl->database->database->url);
951 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
952 cl->state = Client_Error;
956 if (!*r->presentStatus && cl->state != Client_Error)
958 yaz_log(YLOG_DEBUG, "Good Present response %s",
959 cl->database->database->url);
960 ingest_records(cl, r->records);
961 cl->state = Client_Idle;
963 else if (*r->presentStatus)
965 yaz_log(YLOG_WARN, "Bad Present response %s",
966 cl->database->database->url);
967 cl->state = Client_Error;
971 void connection_handler(IOCHAN i, int event)
973 struct connection *co = iochan_getdata(i);
974 struct client *cl = co->client;
975 struct session *se = 0;
981 yaz_log(YLOG_WARN, "Destroying orphan connection");
982 connection_destroy(co);
986 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
989 socklen_t errlen = sizeof(errcode);
991 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
992 &errlen) < 0 || errcode != 0)
999 yaz_log(YLOG_DEBUG, "Connect OK");
1000 co->state = Conn_Open;
1002 cl->state = Client_Connected;
1006 else if (event & EVENT_INPUT)
1008 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
1012 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from %s",
1013 cl->database->database->url);
1014 connection_destroy(co);
1019 yaz_log(YLOG_WARN, "EOF reading from %s", cl->database->database->url);
1020 connection_destroy(co);
1023 else if (len > 1) // We discard input if we have no connection
1025 co->state = Conn_Open;
1027 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
1031 odr_reset(global_parameters.odr_in);
1032 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
1033 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
1040 case Z_APDU_initResponse:
1041 do_initResponse(i, a);
1043 case Z_APDU_searchResponse:
1044 do_searchResponse(i, a);
1046 case Z_APDU_presentResponse:
1047 do_presentResponse(i, a);
1050 do_closeResponse(i, a);
1054 "Unexpected Z39.50 response from %s",
1055 cl->database->database->url);
1059 // We aren't expecting staggered output from target
1060 // if (cs_more(t->link))
1061 // iochan_setevent(i, EVENT_INPUT);
1063 else // we throw away response and go to idle mode
1065 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
1066 cl->state = Client_Idle;
1069 /* if len==1 we do nothing but wait for more input */
1072 if (cl->state == Client_Connected) {
1076 if (cl->state == Client_Idle)
1078 if (cl->requestid != se->requestid && cl->pquery) {
1081 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
1082 cl->records < cl->hits) {
1088 // Disassociate connection from client
1089 static void connection_release(struct connection *co)
1091 struct client *cl = co->client;
1093 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
1100 // Close connection and recycle structure
1101 static void connection_destroy(struct connection *co)
1103 struct host *h = co->host;
1108 iochan_destroy(co->iochan);
1111 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
1112 if (h->connections == co)
1113 h->connections = co->next;
1116 struct connection *pco;
1117 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
1120 pco->next = co->next;
1126 if (co->client->state != Client_Idle)
1127 co->client->state = Client_Disconnected;
1128 co->client->connection = 0;
1130 co->next = connection_freelist;
1131 connection_freelist = co;
1134 static int connection_connect(struct connection *con)
1137 struct client *cl = con->client;
1138 struct host *host = con->host;
1142 assert(host->ipport);
1145 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
1147 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
1151 if (0 == strlen(global_parameters.zproxy_override)){
1152 /* no Z39.50 proxy needed - direct connect */
1153 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->database->url);
1155 if (!(addr = cs_straddr(link, host->ipport)))
1157 yaz_log(YLOG_WARN|YLOG_ERRNO,
1158 "Lookup of IP address %s failed", host->ipport);
1163 /* Z39.50 proxy connect */
1164 yaz_log(YLOG_DEBUG, "Connection create %s proxy %s",
1165 cl->database->database->url, global_parameters.zproxy_override);
1167 if (!(addr = cs_straddr(link, global_parameters.zproxy_override)))
1169 yaz_log(YLOG_WARN|YLOG_ERRNO,
1170 "Lookup of IP address %s failed",
1171 global_parameters.zproxy_override);
1176 res = cs_connect(link, addr);
1179 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->database->url);
1183 con->state = Conn_Connecting;
1184 con->iochan = iochan_create(cs_fileno(link), connection_handler, 0);
1185 iochan_setdata(con->iochan, con);
1186 pazpar2_add_channel(con->iochan);
1188 /* this fragment is bad DRY: from client_prep_connection */
1189 cl->state = Client_Connecting;
1190 iochan_setflag(con->iochan, EVENT_OUTPUT);
1194 void connect_resolver_host(struct host *host)
1196 struct connection *con = host->connections;
1200 if (con->state == Conn_Resolving)
1202 if (!host->ipport) /* unresolved */
1204 connection_destroy(con);
1205 /* start all over .. at some point it will be NULL */
1206 con = host->connections;
1208 else if (!con->client)
1210 yaz_log(YLOG_WARN, "connect_unresolved_host : ophan client");
1211 connection_destroy(con);
1212 /* start all over .. at some point it will be NULL */
1213 con = host->connections;
1217 connection_connect(con);
1225 // Creates a new connection for client, associated with the host of
1226 // client's database
1227 static struct connection *connection_create(struct client *cl)
1229 struct connection *new;
1230 struct host *host = cl->database->database->host;
1232 if ((new = connection_freelist))
1233 connection_freelist = new->next;
1236 new = xmalloc(sizeof (struct connection));
1241 new->next = new->host->connections;
1242 new->host->connections = new;
1244 cl->connection = new;
1246 new->state = Conn_Resolving;
1248 connection_connect(new);
1252 // Close connection and set state to error
1253 static void client_fatal(struct client *cl)
1255 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->database->url);
1256 connection_destroy(cl->connection);
1257 cl->state = Client_Error;
1260 // Ensure that client has a connection associated
1261 static int client_prep_connection(struct client *cl)
1263 struct connection *co;
1264 struct session *se = cl->session;
1265 struct host *host = cl->database->database->host;
1267 co = cl->connection;
1269 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->database->url);
1273 // See if someone else has an idle connection
1274 // We should look at timestamps here to select the longest-idle connection
1275 for (co = host->connections; co; co = co->next)
1276 if (co->state == Conn_Open && (!co->client || co->client->session != se))
1280 connection_release(co);
1281 cl->connection = co;
1285 co = connection_create(cl);
1289 if (co->state == Conn_Connecting)
1291 cl->state = Client_Connecting;
1292 iochan_setflag(co->iochan, EVENT_OUTPUT);
1294 else if (co->state == Conn_Open)
1296 if (cl->state == Client_Error || cl->state == Client_Disconnected)
1297 cl->state = Client_Idle;
1298 iochan_setflag(co->iochan, EVENT_OUTPUT);
1306 // Initialize YAZ Map structures for MARC-based targets
1307 static int prepare_yazmarc(struct session_database *sdb)
1313 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
1316 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX)) && !strncmp(s, "iso2709", 7))
1318 char *encoding = "marc-8s", *e;
1321 // See if a native encoding is specified
1322 if ((e = strchr(s, ';')))
1325 sdb->yaz_marc = yaz_marc_create();
1326 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
1328 cm = yaz_iconv_open("utf-8", encoding);
1332 "Unable to map from %s to UTF-8 for target %s",
1333 encoding, sdb->database->url);
1336 yaz_marc_iconv(sdb->yaz_marc, cm);
1341 // Prepare XSLT stylesheets for record normalization
1342 // Structures are allocated on the session_wide nmem to avoid having
1343 // to recompute this for every search. This would lead
1344 // to leaking if a single session was to repeatedly change the PZ_XSLT
1345 // setting. However, this is not a realistic use scenario.
1346 static int prepare_map(struct session *se, struct session_database *sdb)
1352 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
1355 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
1358 struct database_retrievalmap **m = &sdb->map;
1361 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
1362 for (i = 0; i < num; i++)
1364 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
1366 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
1368 yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s",
1376 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
1377 sdb->database->url);
1381 // This analyzes settings and recomputes any supporting data structures
1383 static int prepare_session_database(struct session *se, struct session_database *sdb)
1387 yaz_log(YLOG_WARN, "No settings associated with %s", sdb->database->url);
1390 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
1392 if (prepare_yazmarc(sdb) < 0)
1395 if (sdb->settings[PZ_XSLT] && !sdb->map)
1397 if (prepare_map(se, sdb) < 0)
1403 // Initialize CCL map for a target
1404 static CCL_bibset prepare_cclmap(struct client *cl)
1406 struct session_database *sdb = cl->database;
1412 res = ccl_qual_mk();
1413 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
1415 char *p = strchr(s->name + 3, ':');
1418 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
1423 ccl_qual_fitem(res, s->value, p);
1428 // Parse the query given the settings specific to this client
1429 static int client_parse_query(struct client *cl, const char *query)
1431 struct session *se = cl->session;
1432 struct ccl_rpn_node *cn;
1434 CCL_bibset ccl_map = prepare_cclmap(cl);
1438 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
1439 ccl_qual_rm(&ccl_map);
1442 cl->state = Client_Error;
1443 yaz_log(YLOG_WARN, "Failed to parse query for %s",
1444 cl->database->database->url);
1447 wrbuf_rewind(se->wrbuf);
1448 ccl_pquery(se->wrbuf, cn);
1449 wrbuf_putc(se->wrbuf, '\0');
1452 cl->pquery = xstrdup(wrbuf_buf(se->wrbuf));
1456 // Initialize relevance structure with query terms
1458 extract_terms(se->nmem, cn, p);
1459 se->relevance = relevance_create(se->nmem, (const char **) p,
1460 se->expected_maxrecs);
1467 static struct client *client_create(void)
1470 if (client_freelist)
1472 r = client_freelist;
1473 client_freelist = client_freelist->next;
1476 r = xmalloc(sizeof(struct client));
1486 r->state = Client_Disconnected;
1491 void client_destroy(struct client *c)
1493 struct session *se = c->session;
1494 if (c == se->clients)
1495 se->clients = c->next;
1499 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
1505 connection_release(c->connection);
1506 c->next = client_freelist;
1507 client_freelist = c;
1510 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
1512 s->watchlist[what].fun = fun;
1513 s->watchlist[what].data = data;
1516 void session_alert_watch(struct session *s, int what)
1518 if (!s->watchlist[what].fun)
1520 (*s->watchlist[what].fun)(s->watchlist[what].data);
1521 s->watchlist[what].fun = 0;
1522 s->watchlist[what].data = 0;
1525 //callback for grep_databases
1526 static void select_targets_callback(void *context, struct session_database *db)
1528 struct session *se = (struct session*) context;
1529 struct client *cl = client_create();
1532 cl->next = se->clients;
1536 // Associates a set of clients with a session;
1537 // Note: Session-databases represent databases with per-session setting overrides
1538 int select_targets(struct session *se, struct database_criterion *crit)
1541 client_destroy(se->clients);
1543 return session_grep_databases(se, crit, select_targets_callback);
1546 int session_active_clients(struct session *s)
1551 for (c = s->clients; c; c = c->next)
1552 if (c->connection && (c->state == Client_Connecting ||
1553 c->state == Client_Initializing ||
1554 c->state == Client_Searching ||
1555 c->state == Client_Presenting))
1561 // parses crit1=val1,crit2=val2|val3,...
1562 static struct database_criterion *parse_filter(NMEM m, const char *buf)
1564 struct database_criterion *res = 0;
1571 nmem_strsplit(m, ",", buf, &values, &num);
1572 for (i = 0; i < num; i++)
1577 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
1578 char *eq = strchr(values[i], '=');
1581 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
1585 new->name = values[i];
1586 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
1588 for (subi = 0; subi < subnum; subi++)
1590 struct database_criterion_value *newv = nmem_malloc(m, sizeof(*newv));
1591 newv->value = subvalues[subi];
1592 newv->next = new->values;
1601 char *search(struct session *se, char *query, char *filter)
1603 int live_channels = 0;
1605 struct database_criterion *criteria;
1607 yaz_log(YLOG_DEBUG, "Search");
1609 nmem_reset(se->nmem);
1610 criteria = parse_filter(se->nmem, filter);
1612 live_channels = select_targets(se, criteria);
1615 int maxrecs = live_channels * global_parameters.toget;
1616 se->num_termlists = 0;
1617 se->reclist = reclist_create(se->nmem, maxrecs);
1618 // This will be initialized in send_search()
1619 se->total_records = se->total_hits = se->total_merged = 0;
1620 se->expected_maxrecs = maxrecs;
1627 for (cl = se->clients; cl; cl = cl->next)
1629 if (prepare_session_database(se, cl->database) < 0)
1630 return "CONFIG_ERROR";
1631 if (client_parse_query(cl, query) < 0) // Query must parse for all targets
1635 for (cl = se->clients; cl; cl = cl->next)
1637 client_prep_connection(cl);
1643 // Creates a new session_database object for a database
1644 static void session_init_databases_fun(void *context, struct database *db)
1646 struct session *se = (struct session *) context;
1647 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
1648 int num = settings_num();
1654 new->settings = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
1655 memset(new->settings, 0, sizeof(struct settings*) * num);
1658 for (i = 0; i < num; i++)
1659 new->settings[i] = db->settings[i];
1661 new->next = se->databases;
1662 se->databases = new;
1665 // Doesn't free memory associated with sdb -- nmem takes care of that
1666 static void session_database_destroy(struct session_database *sdb)
1668 struct database_retrievalmap *m;
1670 for (m = sdb->map; m; m = m->next)
1671 xsltFreeStylesheet(m->stylesheet);
1673 yaz_marc_destroy(sdb->yaz_marc);
1676 // Initialize session_database list -- this represents this session's view
1677 // of the database list -- subject to modification by the settings ws command
1678 void session_init_databases(struct session *se)
1681 grep_databases(se, 0, session_init_databases_fun);
1684 // Probably session_init_databases_fun should be refactored instead of
1686 static struct session_database *load_session_database(struct session *se, char *id)
1688 struct database *db = find_database(id, 0);
1690 session_init_databases_fun((void*) se, db);
1691 // New sdb is head of se->databases list
1692 return se->databases;
1695 // Find an existing session database. If not found, load it
1696 static struct session_database *find_session_database(struct session *se, char *id)
1698 struct session_database *sdb;
1700 for (sdb = se->databases; sdb; sdb = sdb->next)
1701 if (!strcmp(sdb->database->url, id))
1703 return load_session_database(se, id);
1706 // Apply a session override to a database
1707 void session_apply_setting(struct session *se, char *dbname, char *setting, char *value)
1709 struct session_database *sdb = find_session_database(se, dbname);
1710 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
1711 int offset = settings_offset(setting);
1715 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
1718 new->precedence = 0;
1719 new->target = dbname;
1720 new->name = setting;
1722 new->next = sdb->settings[offset];
1723 sdb->settings[offset] = new;
1725 // Force later recompute of settings-driven data structures
1726 // (happens when a search starts and client connections are prepared)
1729 case PZ_NATIVESYNTAX:
1732 yaz_marc_destroy(sdb->yaz_marc);
1739 struct database_retrievalmap *m;
1740 // We don't worry about the map structure -- it's in nmem
1741 for (m = sdb->map; m; m = m->next)
1742 xsltFreeStylesheet(m->stylesheet);
1749 void destroy_session(struct session *s)
1751 struct session_database *sdb;
1753 yaz_log(YLOG_LOG, "Destroying session");
1755 client_destroy(s->clients);
1756 for (sdb = s->databases; sdb; sdb = sdb->next)
1757 session_database_destroy(sdb);
1758 nmem_destroy(s->nmem);
1759 wrbuf_destroy(s->wrbuf);
1762 struct session *new_session(NMEM nmem)
1765 struct session *session = nmem_malloc(nmem, sizeof(*session));
1767 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
1769 session->total_hits = 0;
1770 session->total_records = 0;
1771 session->num_termlists = 0;
1772 session->reclist = 0;
1773 session->requestid = -1;
1774 session->clients = 0;
1775 session->expected_maxrecs = 0;
1776 session->session_nmem = nmem;
1777 session->nmem = nmem_create();
1778 session->wrbuf = wrbuf_alloc();
1779 session_init_databases(session);
1780 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1782 session->watchlist[i].data = 0;
1783 session->watchlist[i].fun = 0;
1789 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1791 static struct hitsbytarget res[1000]; // FIXME MM
1795 for (cl = se->clients; cl; cl = cl->next)
1797 char *name = session_setting_oneval(cl->database, PZ_NAME);
1799 res[*count].id = cl->database->database->url;
1800 res[*count].name = *name ? name : "Unknown";
1801 res[*count].hits = cl->hits;
1802 res[*count].records = cl->records;
1803 res[*count].diagnostic = cl->diagnostic;
1804 res[*count].state = client_states[cl->state];
1805 res[*count].connected = cl->connection ? 1 : 0;
1812 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1816 for (i = 0; i < s->num_termlists; i++)
1817 if (!strcmp((const char *) s->termlists[i].name, name))
1818 return termlist_highscore(s->termlists[i].termlist, num);
1822 #ifdef MISSING_HEADERS
1823 void report_nmem_stats(void)
1825 size_t in_use, is_free;
1827 nmem_get_memory_in_use(&in_use);
1828 nmem_get_memory_free(&is_free);
1830 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1831 (long) in_use, (long) is_free);
1835 struct record_cluster *show_single(struct session *s, int id)
1837 struct record_cluster *r;
1839 reclist_rewind(s->reclist);
1840 while ((r = reclist_read_record(s->reclist)))
1846 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp, int start,
1847 int *num, int *total, int *sumhits, NMEM nmem_show)
1849 struct record_cluster **recs = nmem_malloc(nmem_show, *num
1850 * sizeof(struct record_cluster *));
1851 struct reclist_sortparms *spp;
1854 yaz_timing_t t = yaz_timing_create();
1857 for (spp = sp; spp; spp = spp->next)
1858 if (spp->type == Metadata_sortkey_relevance)
1860 relevance_prepare_read(s->relevance, s->reclist);
1863 reclist_sort(s->reclist, sp);
1865 *total = s->reclist->num_records;
1866 *sumhits = s->total_hits;
1868 for (i = 0; i < start; i++)
1869 if (!reclist_read_record(s->reclist))
1876 for (i = 0; i < *num; i++)
1878 struct record_cluster *r = reclist_read_record(s->reclist);
1888 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1889 yaz_timing_get_real(t), yaz_timing_get_user(t),
1890 yaz_timing_get_sys(t));
1891 yaz_timing_destroy(&t);
1896 void statistics(struct session *se, struct statistics *stat)
1901 memset(stat, 0, sizeof(*stat));
1902 for (cl = se->clients; cl; cl = cl->next)
1904 if (!cl->connection)
1905 stat->num_no_connection++;
1908 case Client_Connecting: stat->num_connecting++; break;
1909 case Client_Initializing: stat->num_initializing++; break;
1910 case Client_Searching: stat->num_searching++; break;
1911 case Client_Presenting: stat->num_presenting++; break;
1912 case Client_Idle: stat->num_idle++; break;
1913 case Client_Failed: stat->num_failed++; break;
1914 case Client_Error: stat->num_error++; break;
1919 stat->num_hits = se->total_hits;
1920 stat->num_records = se->total_records;
1922 stat->num_clients = count;
1925 void start_http_listener(void)
1928 struct conf_server *ser = global_parameters.server;
1930 if (*global_parameters.listener_override)
1931 strcpy(hp, global_parameters.listener_override);
1934 strcpy(hp, ser->host ? ser->host : "");
1939 sprintf(hp + strlen(hp), "%d", ser->port);
1945 void start_proxy(void)
1948 struct conf_server *ser = global_parameters.server;
1950 if (*global_parameters.proxy_override)
1951 strcpy(hp, global_parameters.proxy_override);
1952 else if (ser->proxy_host || ser->proxy_port)
1954 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
1955 if (ser->proxy_port)
1959 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
1965 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
1968 void start_zproxy(void)
1970 struct conf_server *ser = global_parameters.server;
1972 if (*global_parameters.zproxy_override){
1973 yaz_log(YLOG_LOG, "Z39.50 proxy %s",
1974 global_parameters.zproxy_override);
1978 else if (ser->zproxy_host || ser->zproxy_port)
1982 strcpy(hp, ser->zproxy_host ? ser->zproxy_host : "");
1983 if (ser->zproxy_port)
1990 sprintf(hp + strlen(hp), "%d", ser->zproxy_port);
1992 strcpy(global_parameters.zproxy_override, hp);
1993 yaz_log(YLOG_LOG, "Z39.50 proxy %s",
1994 global_parameters.zproxy_override);
2001 // Master list of connections we're handling events to
2002 static IOCHAN channel_list = 0;
2003 void pazpar2_add_channel(IOCHAN chan)
2005 chan->next = channel_list;
2006 channel_list = chan;
2009 void pazpar2_event_loop()
2011 event_loop(&channel_list);
2017 * indent-tabs-mode: nil
2019 * vim: shiftwidth=4 tabstop=8 expandtab