1 /* $Id: pazpar2.c,v 1.13 2007-01-04 20:00:58 quinn Exp $ */;
8 #include <sys/socket.h>
14 #include <yaz/comstack.h>
15 #include <yaz/tcpip.h>
16 #include <yaz/proto.h>
17 #include <yaz/readconf.h>
18 #include <yaz/pquery.h>
19 #include <yaz/yaz-util.h>
24 #include <yaz/timing.h>
31 #include "termlists.h"
33 #include "relevance.h"
36 #define PAZPAR2_VERSION "0.1"
39 static void client_fatal(struct client *cl);
40 static void connection_destroy(struct connection *co);
41 static int client_prep_connection(struct client *cl);
42 static void ingest_records(struct client *cl, Z_Records *r);
43 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db);
44 void session_alert_watch(struct session *s, int what);
46 IOCHAN channel_list = 0; // Master list of connections we're handling events to
48 static struct connection *connection_freelist = 0;
49 static struct client *client_freelist = 0;
51 static struct host *hosts = 0; // The hosts we know about
52 static struct database *databases = 0; // The databases we know about
54 static char *client_states[] = {
58 "Client_Initializing",
63 "Client_Disconnected",
67 struct parameters global_parameters =
72 "Index Data PazPar2 (MasterKey)",
84 static int send_apdu(struct client *c, Z_APDU *a)
86 struct connection *co = c->connection;
90 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
92 odr_perror(global_parameters.odr_out, "Encoding APDU");
95 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
96 r = cs_put(co->link, buf, len);
99 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
104 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
107 odr_reset(global_parameters.odr_out); /* release the APDU structure */
108 co->state = Conn_Waiting;
113 static void send_init(IOCHAN i)
115 struct connection *co = iochan_getdata(i);
116 struct client *cl = co->client;
117 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
119 a->u.initRequest->implementationId = global_parameters.implementationId;
120 a->u.initRequest->implementationName = global_parameters.implementationName;
121 a->u.initRequest->implementationVersion =
122 global_parameters.implementationVersion;
123 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
124 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
125 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
127 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
128 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
129 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
130 if (send_apdu(cl, a) >= 0)
132 iochan_setflags(i, EVENT_INPUT);
133 cl->state = Client_Initializing;
136 cl->state = Client_Error;
137 odr_reset(global_parameters.odr_out);
140 static void send_search(IOCHAN i)
142 struct connection *co = iochan_getdata(i);
143 struct client *cl = co->client;
144 struct session *se = cl->session;
145 struct database *db = cl->database;
146 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
147 int ndb, cerror, cpos;
150 struct ccl_rpn_node *cn;
151 int ssub = 0, lslb = 100000, mspn = 10;
153 yaz_log(YLOG_DEBUG, "Sending search");
155 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
158 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
160 zquery->which = Z_Query_type_1;
161 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
164 for (ndb = 0; db->databases[ndb]; ndb++)
166 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
167 for (ndb = 0; db->databases[ndb]; ndb++)
168 databaselist[ndb] = db->databases[ndb];
170 a->u.presentRequest->preferredRecordSyntax =
171 yaz_oidval_to_z3950oid(global_parameters.odr_out,
172 CLASS_RECSYN, VAL_USMARC);
173 a->u.searchRequest->smallSetUpperBound = &ssub;
174 a->u.searchRequest->largeSetLowerBound = &lslb;
175 a->u.searchRequest->mediumSetPresentNumber = &mspn;
176 a->u.searchRequest->resultSetName = "Default";
177 a->u.searchRequest->databaseNames = databaselist;
178 a->u.searchRequest->num_databaseNames = ndb;
180 if (send_apdu(cl, a) >= 0)
182 iochan_setflags(i, EVENT_INPUT);
183 cl->state = Client_Searching;
184 cl->requestid = se->requestid;
187 cl->state = Client_Error;
189 odr_reset(global_parameters.odr_out);
192 static void send_present(IOCHAN i)
194 struct connection *co = iochan_getdata(i);
195 struct client *cl = co->client;
196 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
198 int start = cl->records + 1;
200 toget = global_parameters.chunk;
201 if (toget > cl->hits - cl->records)
202 toget = cl->hits - cl->records;
204 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
206 a->u.presentRequest->resultSetStartPoint = &start;
207 a->u.presentRequest->numberOfRecordsRequested = &toget;
209 a->u.presentRequest->resultSetId = "Default";
211 a->u.presentRequest->preferredRecordSyntax =
212 yaz_oidval_to_z3950oid(global_parameters.odr_out,
213 CLASS_RECSYN, VAL_USMARC);
215 if (send_apdu(cl, a) >= 0)
217 iochan_setflags(i, EVENT_INPUT);
218 cl->state = Client_Presenting;
221 cl->state = Client_Error;
222 odr_reset(global_parameters.odr_out);
225 static void do_initResponse(IOCHAN i, Z_APDU *a)
227 struct connection *co = iochan_getdata(i);
228 struct client *cl = co->client;
229 Z_InitResponse *r = a->u.initResponse;
231 yaz_log(YLOG_DEBUG, "Received init response");
235 cl->state = Client_Idle;
238 cl->state = Client_Failed; // FIXME need to do something to the connection
241 static void do_searchResponse(IOCHAN i, Z_APDU *a)
243 struct connection *co = iochan_getdata(i);
244 struct client *cl = co->client;
245 struct session *se = cl->session;
246 Z_SearchResponse *r = a->u.searchResponse;
248 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
250 if (*r->searchStatus)
252 cl->hits = *r->resultCount;
253 se->total_hits += cl->hits;
254 if (r->presentStatus && !*r->presentStatus && r->records)
256 yaz_log(YLOG_DEBUG, "Records in search response");
257 cl->records += *r->numberOfRecordsReturned;
258 ingest_records(cl, r->records);
260 cl->state = Client_Idle;
265 cl->state = Client_Error;
267 Z_Records *recs = r->records;
268 if (recs->which == Z_Records_NSD)
270 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
271 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
272 cl->state = Client_Error;
278 char *normalize_mergekey(char *buf)
280 char *p = buf, *pout = buf;
284 while (*p && !isalnum(*p))
287 *(pout++) = tolower(*(p++));
290 while (*p && !isalnum(*p))
301 // FIXME needs to be generalized. Should flexibly generate X lists per search
302 static void extract_subject(struct session *s, const char *rec)
304 const char *field, *subfield;
306 while ((field = find_field(rec, "650")))
309 if ((subfield = find_subfield(field, 'a')))
315 ef = index(subfield, '\n');
318 if ((e = index(subfield, '\t')) && e < ef)
320 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
324 memcpy(buf, subfield, len);
328 termlist_insert(s->termlist, buf);
335 static void add_facet(struct session *s, const char *type, const char *value)
339 for (i = 0; i < s->num_termlists; i++)
340 if (!strcmp(s->termlists[i].name, type))
342 if (i == s->num_termlists)
344 if (i == SESSION_MAX_TERMLISTS)
346 yaz_log(YLOG_FATAL, "Too many termlists");
349 s->termlists[i].name = nmem_strdup(s->nmem, type);
350 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
351 s->num_termlists = i + 1;
353 termlist_insert(s->termlists[i].termlist, value);
356 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
358 struct conf_retrievalprofile *rprofile = cl->database->rprofile;
359 struct conf_retrievalmap *m;
363 // First normalize to XML
364 if (rprofile->native_syntax == Nativesyn_iso2709)
368 if (rec->which != Z_External_octet)
370 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
373 buf = (char*) rec->u.octet_aligned->buf;
374 len = rec->u.octet_aligned->len;
375 if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
377 yaz_log(YLOG_WARN, "Failed to decode MARC");
380 if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
381 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
383 yaz_log(YLOG_WARN, "Failed to encode as XML");
386 rdoc = xmlNewDoc("1.0");
387 xmlDocSetRootElement(rdoc, res);
391 yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
394 for (m = rprofile->maplist; m; m = m->next)
397 if (m->type != Map_xslt)
399 yaz_log(YLOG_WARN, "Unknown map type");
402 if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
404 yaz_log(YLOG_WARN, "XSLT transformation failed");
410 if (global_parameters.dump_records)
412 fprintf(stderr, "Record:\n----------------\n");
413 xmlDocFormatDump(stderr, rdoc, 1);
418 static struct record *ingest_record(struct client *cl, Z_External *rec)
420 xmlDoc *xdoc = normalize_record(cl, rec);
422 struct record *res, *head;
423 struct session *se = cl->session;
424 xmlChar *mergekey, *mergekey_norm;
429 root = xmlDocGetRootElement(xdoc);
430 if (!(mergekey = xmlGetProp(root, "mergekey")))
432 yaz_log(YLOG_WARN, "No mergekey found in record");
436 res = nmem_malloc(se->nmem, sizeof(struct record));
437 res->next_cluster = 0;
438 res->target_offset = -1;
439 res->term_frequency_vec = 0;
440 res->title = "Unknown";
443 mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
445 res->merge_key = normalize_mergekey(mergekey_norm);
447 head = reclist_insert(se->reclist, res);
448 relevance_newrec(se->relevance, head);
450 for (n = root->children; n; n = n->next)
452 if (n->type != XML_ELEMENT_NODE)
454 if (!strcmp(n->name, "facet"))
456 xmlChar *type = xmlGetProp(n, "type");
457 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
460 add_facet(se, type, value);
461 relevance_countwords(se->relevance, head, value, 1);
466 else if (!strcmp(n->name, "metadata"))
468 xmlChar *type = xmlGetProp(n, "type");
469 if (type && !strcmp(type, "title"))
471 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
474 res->title = nmem_strdup(se->nmem, value);
475 relevance_countwords(se->relevance, head, value, 4);
482 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
487 relevance_donerecord(se->relevance, head);
493 static void ingest_records(struct client *cl, Z_Records *r)
496 yaz_timing_t t = yaz_timing_create();
499 struct session *s = cl->session;
500 Z_NamePlusRecordList *rlist;
503 if (r->which != Z_Records_DBOSD)
505 rlist = r->u.databaseOrSurDiagnostics;
506 for (i = 0; i < rlist->num_records; i++)
508 Z_NamePlusRecord *npr = rlist->records[i];
510 if (npr->which != Z_NamePlusRecord_databaseRecord)
512 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
516 rec = ingest_record(cl, npr->u.databaseRecord);
520 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
521 session_alert_watch(s, SESSION_WATCH_RECORDS);
525 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
526 yaz_timing_get_real(t), yaz_timing_get_user(t),
527 yaz_timing_get_sys(t));
528 yaz_timing_destroy(&t);
532 static void do_presentResponse(IOCHAN i, Z_APDU *a)
534 struct connection *co = iochan_getdata(i);
535 struct client *cl = co->client;
536 Z_PresentResponse *r = a->u.presentResponse;
539 Z_Records *recs = r->records;
540 if (recs->which == Z_Records_NSD)
542 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
543 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
544 cl->state = Client_Error;
548 if (!*r->presentStatus && cl->state != Client_Error)
550 yaz_log(YLOG_DEBUG, "Good Present response");
551 cl->records += *r->numberOfRecordsReturned;
552 ingest_records(cl, r->records);
553 cl->state = Client_Idle;
555 else if (*r->presentStatus)
557 yaz_log(YLOG_WARN, "Bad Present response");
558 cl->state = Client_Error;
562 static void handler(IOCHAN i, int event)
564 struct connection *co = iochan_getdata(i);
565 struct client *cl = co->client;
566 struct session *se = 0;
572 yaz_log(YLOG_WARN, "Destroying orphan connection");
573 connection_destroy(co);
577 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
580 socklen_t errlen = sizeof(errcode);
582 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
583 &errlen) < 0 || errcode != 0)
590 yaz_log(YLOG_DEBUG, "Connect OK");
591 co->state = Conn_Open;
593 cl->state = Client_Connected;
597 else if (event & EVENT_INPUT)
599 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
603 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
604 connection_destroy(co);
609 yaz_log(YLOG_WARN, "EOF reading from Z server");
610 connection_destroy(co);
613 else if (len > 1) // We discard input if we have no connection
615 co->state = Conn_Open;
617 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
621 odr_reset(global_parameters.odr_in);
622 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
623 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
630 case Z_APDU_initResponse:
631 do_initResponse(i, a);
633 case Z_APDU_searchResponse:
634 do_searchResponse(i, a);
636 case Z_APDU_presentResponse:
637 do_presentResponse(i, a);
640 yaz_log(YLOG_WARN, "Unexpected result from server");
644 // We aren't expecting staggered output from target
645 // if (cs_more(t->link))
646 // iochan_setevent(i, EVENT_INPUT);
648 else // we throw away response and go to idle mode
650 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
651 cl->state = Client_Idle;
654 /* if len==1 we do nothing but wait for more input */
657 if (cl->state == Client_Connected) {
661 if (cl->state == Client_Idle)
663 if (cl->requestid != se->requestid && *se->query) {
666 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
667 cl->records < cl->hits) {
673 // Disassociate connection from client
674 static void connection_release(struct connection *co)
676 struct client *cl = co->client;
678 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
685 // Close connection and recycle structure
686 static void connection_destroy(struct connection *co)
688 struct host *h = co->host;
690 iochan_destroy(co->iochan);
692 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
693 if (h->connections == co)
694 h->connections = co->next;
697 struct connection *pco;
698 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
701 pco->next = co->next;
707 if (co->client->state != Client_Idle)
708 co->client->state = Client_Disconnected;
709 co->client->connection = 0;
711 co->next = connection_freelist;
712 connection_freelist = co;
715 // Creates a new connection for client, associated with the host of
717 static struct connection *connection_create(struct client *cl)
719 struct connection *new;
724 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
725 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
727 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
731 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
733 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
737 res = cs_connect(link, addr);
740 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
744 if ((new = connection_freelist))
745 connection_freelist = new->next;
748 new = xmalloc(sizeof (struct connection));
752 new->state = Conn_Connecting;
753 new->host = cl->database->host;
754 new->next = new->host->connections;
755 new->host->connections = new;
757 cl->connection = new;
760 new->iochan = iochan_create(cs_fileno(link), handler, 0);
761 iochan_setdata(new->iochan, new);
762 new->iochan->next = channel_list;
763 channel_list = new->iochan;
767 // Close connection and set state to error
768 static void client_fatal(struct client *cl)
770 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
771 connection_destroy(cl->connection);
772 cl->state = Client_Error;
775 // Ensure that client has a connection associated
776 static int client_prep_connection(struct client *cl)
778 struct connection *co;
779 struct session *se = cl->session;
780 struct host *host = cl->database->host;
784 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
788 // See if someone else has an idle connection
789 // We should look at timestamps here to select the longest-idle connection
790 for (co = host->connections; co; co = co->next)
791 if (co->state == Conn_Open && (!co->client || co->client->session != se))
795 connection_release(co);
800 co = connection_create(cl);
804 if (co->state == Conn_Connecting)
806 cl->state = Client_Connecting;
807 iochan_setflag(co->iochan, EVENT_OUTPUT);
809 else if (co->state == Conn_Open)
811 if (cl->state == Client_Error || cl->state == Client_Disconnected)
812 cl->state = Client_Idle;
813 iochan_setflag(co->iochan, EVENT_OUTPUT);
821 // This function will most likely vanish when a proper target profile mechanism is
823 void load_simpletargets(const char *fn)
825 FILE *f = fopen(fn, "r");
830 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
834 while (fgets(line, 255, f))
838 struct database *database;
840 if (strncmp(line, "target ", 7))
843 url[strlen(url) - 1] = '\0';
844 yaz_log(YLOG_DEBUG, "Target: %s", url);
845 if ((db = strchr(url, '/')))
850 for (host = hosts; host; host = host->next)
851 if (!strcmp(url, host->hostport))
855 struct addrinfo *addrinfo, hints;
858 unsigned char addrbuf[4];
861 host = xmalloc(sizeof(struct host));
862 host->hostport = xstrdup(url);
863 host->connections = 0;
865 if ((port = strchr(url, ':')))
871 hints.ai_family = PF_INET;
872 hints.ai_socktype = SOCK_STREAM;
873 hints.ai_protocol = IPPROTO_TCP;
874 hints.ai_addrlen = 0;
876 hints.ai_canonname = 0;
878 // This is not robust code. It assumes that getaddrinfo returns AF_INET
880 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
882 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
883 xfree(host->hostport);
887 assert(addrinfo->ai_family == PF_INET);
888 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
889 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
890 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
891 host->ipport = xstrdup(ipport);
892 freeaddrinfo(addrinfo);
896 database = xmalloc(sizeof(struct database));
897 database->host = host;
898 database->url = xmalloc(strlen(url) + strlen(db) + 2);
899 strcpy(database->url, url);
900 strcat(database->url, "/");
901 strcat(database->url, db);
903 database->databases = xmalloc(2 * sizeof(char *));
904 database->databases[0] = xstrdup(db);
905 database->databases[1] = 0;
906 database->errors = 0;
907 database->qprofile = 0;
908 database->rprofile = database_retrieval_profile(database);
909 database->next = databases;
910 databases = database;
916 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
924 pull_terms(nmem, n->u.p[0], termlist, num);
925 pull_terms(nmem, n->u.p[1], termlist, num);
928 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
935 // Extract terms from query into null-terminated termlist
936 static int extract_terms(NMEM nmem, char *query, char **termlist)
939 struct ccl_rpn_node *n;
942 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
945 pull_terms(nmem, n, termlist, &num);
951 static struct client *client_create(void)
957 client_freelist = client_freelist->next;
960 r = xmalloc(sizeof(struct client));
969 r->state = Client_Disconnected;
974 void client_destroy(struct client *c)
976 struct session *se = c->session;
977 if (c == se->clients)
978 se->clients = c->next;
982 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
988 connection_release(c->connection);
989 c->next = client_freelist;
993 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
995 s->watchlist[what].fun = fun;
996 s->watchlist[what].data = data;
999 void session_alert_watch(struct session *s, int what)
1001 if (!s->watchlist[what].fun)
1003 (*s->watchlist[what].fun)(s->watchlist[what].data);
1004 s->watchlist[what].fun = 0;
1005 s->watchlist[what].data = 0;
1008 // This needs to be extended with selection criteria
1009 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
1013 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
1016 if (!config->retrievalprofiles)
1018 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
1020 return config->retrievalprofiles;
1023 // This should be extended with parameters to control selection criteria
1024 // Associates a set of clients with a session;
1025 int select_targets(struct session *se)
1027 struct database *db;
1031 client_destroy(se->clients);
1032 for (db = databases; db; db = db->next)
1034 struct client *cl = client_create();
1037 cl->next = se->clients;
1044 int session_active_clients(struct session *s)
1049 for (c = s->clients; c; c = c->next)
1050 if (c->connection && (c->state == Client_Connecting ||
1051 c->state == Client_Initializing ||
1052 c->state == Client_Searching ||
1053 c->state == Client_Presenting))
1059 char *search(struct session *se, char *query)
1061 int live_channels = 0;
1064 yaz_log(YLOG_DEBUG, "Search");
1066 strcpy(se->query, query);
1068 nmem_reset(se->nmem);
1069 for (cl = se->clients; cl; cl = cl->next)
1075 if (client_prep_connection(cl))
1081 int maxrecs = live_channels * global_parameters.toget;
1082 se->num_termlists = 0;
1083 se->reclist = reclist_create(se->nmem, maxrecs);
1084 extract_terms(se->nmem, query, p);
1085 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1086 se->total_records = se->total_hits = 0;
1087 se->expected_maxrecs = maxrecs;
1095 void destroy_session(struct session *s)
1097 yaz_log(YLOG_LOG, "Destroying session");
1099 client_destroy(s->clients);
1100 nmem_destroy(s->nmem);
1101 wrbuf_free(s->wrbuf, 1);
1104 struct session *new_session()
1107 struct session *session = xmalloc(sizeof(*session));
1109 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1111 session->total_hits = 0;
1112 session->total_records = 0;
1113 session->num_termlists = 0;
1114 session->reclist = 0;
1115 session->requestid = -1;
1116 session->clients = 0;
1117 session->expected_maxrecs = 0;
1118 session->query[0] = '\0';
1119 session->nmem = nmem_create();
1120 session->wrbuf = wrbuf_alloc();
1121 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1123 session->watchlist[i].data = 0;
1124 session->watchlist[i].fun = 0;
1127 select_targets(session);
1132 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1134 static struct hitsbytarget res[1000]; // FIXME MM
1138 for (cl = se->clients; cl; cl = cl->next)
1140 strcpy(res[*count].id, cl->database->host->hostport);
1141 res[*count].hits = cl->hits;
1142 res[*count].records = cl->records;
1143 res[*count].diagnostic = cl->diagnostic;
1144 res[*count].state = client_states[cl->state];
1145 res[*count].connected = cl->connection ? 1 : 0;
1152 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1156 for (i = 0; i < s->num_termlists; i++)
1157 if (!strcmp(s->termlists[i].name, name))
1158 return termlist_highscore(s->termlists[i].termlist, num);
1163 // conditional compilation by SH: This lead to a warning with currently installed
1164 // YAZ header files on us1
1165 void report_nmem_stats(void)
1167 size_t in_use, is_free;
1169 nmem_get_memory_in_use(&in_use);
1170 nmem_get_memory_free(&is_free);
1172 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1173 (long) in_use, (long) is_free);
1177 struct record **show(struct session *s, int start, int *num, int *total,
1178 int *sumhits, NMEM nmem_show)
1180 struct record **recs = nmem_malloc(nmem_show, *num
1181 * sizeof(struct record *));
1184 yaz_timing_t t = yaz_timing_create();
1186 relevance_prepare_read(s->relevance, s->reclist);
1188 *total = s->reclist->num_records;
1189 *sumhits = s->total_hits;
1191 for (i = 0; i < start; i++)
1192 if (!reclist_read_record(s->reclist))
1199 for (i = 0; i < *num; i++)
1201 struct record *r = reclist_read_record(s->reclist);
1211 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1212 yaz_timing_get_real(t), yaz_timing_get_user(t),
1213 yaz_timing_get_sys(t));
1214 yaz_timing_destroy(&t);
1219 void statistics(struct session *se, struct statistics *stat)
1224 bzero(stat, sizeof(*stat));
1225 for (cl = se->clients; cl; cl = cl->next)
1227 if (!cl->connection)
1228 stat->num_no_connection++;
1231 case Client_Connecting: stat->num_connecting++; break;
1232 case Client_Initializing: stat->num_initializing++; break;
1233 case Client_Searching: stat->num_searching++; break;
1234 case Client_Presenting: stat->num_presenting++; break;
1235 case Client_Idle: stat->num_idle++; break;
1236 case Client_Failed: stat->num_failed++; break;
1237 case Client_Error: stat->num_error++; break;
1242 stat->num_hits = se->total_hits;
1243 stat->num_records = se->total_records;
1245 stat->num_clients = count;
1248 static CCL_bibset load_cclfile(const char *fn)
1250 CCL_bibset res = ccl_qual_mk();
1251 if (ccl_qual_fname(res, fn) < 0)
1253 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1259 int main(int argc, char **argv)
1265 if (signal(SIGPIPE, SIG_IGN) < 0)
1266 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1268 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1270 while ((ret = options("f:x:c:h:p:C:s:d", argv, argc, &arg)) != -2)
1274 if (!read_config(arg))
1278 command_init(atoi(arg));
1286 global_parameters.ccl_filter = load_cclfile(arg);
1289 http_set_proxyaddr(arg);
1292 load_simpletargets(arg);
1295 global_parameters.dump_records = 1;
1298 fprintf(stderr, "Usage: pazpar2\n"
1300 " -h [host:]port (REST protocol listener)\n"
1301 " -c cmdport (telnet-style)\n"
1303 " -s simpletargetfile\n"
1304 " -p hostname[:portno] (HTTP proxy)\n");
1311 fprintf(stderr, "Set command port with -h or -c\n");
1315 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1316 global_parameters.yaz_marc = yaz_marc_create();
1317 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1318 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1319 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1321 event_loop(&channel_list);
1329 * indent-tabs-mode: nil
1331 * vim: shiftwidth=4 tabstop=8 expandtab