1 /* $Id: pazpar2.c,v 1.14 2007-01-04 22:04:25 quinn Exp $ */;
8 #include <sys/socket.h>
14 #include <yaz/marcdisp.h>
15 #include <yaz/comstack.h>
16 #include <yaz/tcpip.h>
17 #include <yaz/proto.h>
18 #include <yaz/readconf.h>
19 #include <yaz/pquery.h>
20 #include <yaz/yaz-util.h>
25 #include <yaz/timing.h>
32 #include "termlists.h"
34 #include "relevance.h"
37 #define PAZPAR2_VERSION "0.1"
40 static void client_fatal(struct client *cl);
41 static void connection_destroy(struct connection *co);
42 static int client_prep_connection(struct client *cl);
43 static void ingest_records(struct client *cl, Z_Records *r);
44 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db);
45 void session_alert_watch(struct session *s, int what);
47 IOCHAN channel_list = 0; // Master list of connections we're handling events to
49 static struct connection *connection_freelist = 0;
50 static struct client *client_freelist = 0;
52 static struct host *hosts = 0; // The hosts we know about
53 static struct database *databases = 0; // The databases we know about
55 static char *client_states[] = {
59 "Client_Initializing",
64 "Client_Disconnected",
68 struct parameters global_parameters =
73 "Index Data PazPar2 (MasterKey)",
85 static int send_apdu(struct client *c, Z_APDU *a)
87 struct connection *co = c->connection;
91 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
93 odr_perror(global_parameters.odr_out, "Encoding APDU");
96 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
97 r = cs_put(co->link, buf, len);
100 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
105 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
108 odr_reset(global_parameters.odr_out); /* release the APDU structure */
109 co->state = Conn_Waiting;
114 static void send_init(IOCHAN i)
116 struct connection *co = iochan_getdata(i);
117 struct client *cl = co->client;
118 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
120 a->u.initRequest->implementationId = global_parameters.implementationId;
121 a->u.initRequest->implementationName = global_parameters.implementationName;
122 a->u.initRequest->implementationVersion =
123 global_parameters.implementationVersion;
124 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
125 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
126 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
128 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
129 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
130 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
131 if (send_apdu(cl, a) >= 0)
133 iochan_setflags(i, EVENT_INPUT);
134 cl->state = Client_Initializing;
137 cl->state = Client_Error;
138 odr_reset(global_parameters.odr_out);
141 static void send_search(IOCHAN i)
143 struct connection *co = iochan_getdata(i);
144 struct client *cl = co->client;
145 struct session *se = cl->session;
146 struct database *db = cl->database;
147 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
148 int ndb, cerror, cpos;
151 struct ccl_rpn_node *cn;
152 int ssub = 0, lslb = 100000, mspn = 10;
154 yaz_log(YLOG_DEBUG, "Sending search");
156 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
159 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
161 zquery->which = Z_Query_type_1;
162 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
165 for (ndb = 0; db->databases[ndb]; ndb++)
167 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
168 for (ndb = 0; db->databases[ndb]; ndb++)
169 databaselist[ndb] = db->databases[ndb];
171 a->u.presentRequest->preferredRecordSyntax =
172 yaz_oidval_to_z3950oid(global_parameters.odr_out,
173 CLASS_RECSYN, VAL_USMARC);
174 a->u.searchRequest->smallSetUpperBound = &ssub;
175 a->u.searchRequest->largeSetLowerBound = &lslb;
176 a->u.searchRequest->mediumSetPresentNumber = &mspn;
177 a->u.searchRequest->resultSetName = "Default";
178 a->u.searchRequest->databaseNames = databaselist;
179 a->u.searchRequest->num_databaseNames = ndb;
181 if (send_apdu(cl, a) >= 0)
183 iochan_setflags(i, EVENT_INPUT);
184 cl->state = Client_Searching;
185 cl->requestid = se->requestid;
188 cl->state = Client_Error;
190 odr_reset(global_parameters.odr_out);
193 static void send_present(IOCHAN i)
195 struct connection *co = iochan_getdata(i);
196 struct client *cl = co->client;
197 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
199 int start = cl->records + 1;
201 toget = global_parameters.chunk;
202 if (toget > cl->hits - cl->records)
203 toget = cl->hits - cl->records;
205 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
207 a->u.presentRequest->resultSetStartPoint = &start;
208 a->u.presentRequest->numberOfRecordsRequested = &toget;
210 a->u.presentRequest->resultSetId = "Default";
212 a->u.presentRequest->preferredRecordSyntax =
213 yaz_oidval_to_z3950oid(global_parameters.odr_out,
214 CLASS_RECSYN, VAL_USMARC);
216 if (send_apdu(cl, a) >= 0)
218 iochan_setflags(i, EVENT_INPUT);
219 cl->state = Client_Presenting;
222 cl->state = Client_Error;
223 odr_reset(global_parameters.odr_out);
226 static void do_initResponse(IOCHAN i, Z_APDU *a)
228 struct connection *co = iochan_getdata(i);
229 struct client *cl = co->client;
230 Z_InitResponse *r = a->u.initResponse;
232 yaz_log(YLOG_DEBUG, "Received init response");
236 cl->state = Client_Idle;
239 cl->state = Client_Failed; // FIXME need to do something to the connection
242 static void do_searchResponse(IOCHAN i, Z_APDU *a)
244 struct connection *co = iochan_getdata(i);
245 struct client *cl = co->client;
246 struct session *se = cl->session;
247 Z_SearchResponse *r = a->u.searchResponse;
249 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
251 if (*r->searchStatus)
253 cl->hits = *r->resultCount;
254 se->total_hits += cl->hits;
255 if (r->presentStatus && !*r->presentStatus && r->records)
257 yaz_log(YLOG_DEBUG, "Records in search response");
258 cl->records += *r->numberOfRecordsReturned;
259 ingest_records(cl, r->records);
261 cl->state = Client_Idle;
266 cl->state = Client_Error;
268 Z_Records *recs = r->records;
269 if (recs->which == Z_Records_NSD)
271 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
272 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
273 cl->state = Client_Error;
279 char *normalize_mergekey(char *buf)
281 char *p = buf, *pout = buf;
285 while (*p && !isalnum(*p))
288 *(pout++) = tolower(*(p++));
291 while (*p && !isalnum(*p))
302 // FIXME needs to be generalized. Should flexibly generate X lists per search
303 static void extract_subject(struct session *s, const char *rec)
305 const char *field, *subfield;
307 while ((field = find_field(rec, "650")))
310 if ((subfield = find_subfield(field, 'a')))
316 ef = index(subfield, '\n');
319 if ((e = index(subfield, '\t')) && e < ef)
321 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
325 memcpy(buf, subfield, len);
329 termlist_insert(s->termlist, buf);
336 static void add_facet(struct session *s, const char *type, const char *value)
340 for (i = 0; i < s->num_termlists; i++)
341 if (!strcmp(s->termlists[i].name, type))
343 if (i == s->num_termlists)
345 if (i == SESSION_MAX_TERMLISTS)
347 yaz_log(YLOG_FATAL, "Too many termlists");
350 s->termlists[i].name = nmem_strdup(s->nmem, type);
351 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
352 s->num_termlists = i + 1;
354 termlist_insert(s->termlists[i].termlist, value);
357 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
359 struct conf_retrievalprofile *rprofile = cl->database->rprofile;
360 struct conf_retrievalmap *m;
364 // First normalize to XML
365 if (rprofile->native_syntax == Nativesyn_iso2709)
369 if (rec->which != Z_External_octet)
371 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
374 buf = (char*) rec->u.octet_aligned->buf;
375 len = rec->u.octet_aligned->len;
376 if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
378 yaz_log(YLOG_WARN, "Failed to decode MARC");
381 if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
382 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
384 yaz_log(YLOG_WARN, "Failed to encode as XML");
387 rdoc = xmlNewDoc("1.0");
388 xmlDocSetRootElement(rdoc, res);
392 yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
395 for (m = rprofile->maplist; m; m = m->next)
398 if (m->type != Map_xslt)
400 yaz_log(YLOG_WARN, "Unknown map type");
403 if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
405 yaz_log(YLOG_WARN, "XSLT transformation failed");
411 if (global_parameters.dump_records)
413 fprintf(stderr, "Record:\n----------------\n");
414 xmlDocFormatDump(stderr, rdoc, 1);
419 static struct record *ingest_record(struct client *cl, Z_External *rec)
421 xmlDoc *xdoc = normalize_record(cl, rec);
423 struct record *res, *head;
424 struct session *se = cl->session;
425 xmlChar *mergekey, *mergekey_norm;
430 root = xmlDocGetRootElement(xdoc);
431 if (!(mergekey = xmlGetProp(root, "mergekey")))
433 yaz_log(YLOG_WARN, "No mergekey found in record");
437 res = nmem_malloc(se->nmem, sizeof(struct record));
438 res->next_cluster = 0;
439 res->target_offset = -1;
440 res->term_frequency_vec = 0;
441 res->title = "Unknown";
444 mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
446 res->merge_key = normalize_mergekey(mergekey_norm);
448 head = reclist_insert(se->reclist, res);
449 relevance_newrec(se->relevance, head);
451 for (n = root->children; n; n = n->next)
453 if (n->type != XML_ELEMENT_NODE)
455 if (!strcmp(n->name, "facet"))
457 xmlChar *type = xmlGetProp(n, "type");
458 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
461 add_facet(se, type, value);
462 relevance_countwords(se->relevance, head, value, 1);
467 else if (!strcmp(n->name, "metadata"))
469 xmlChar *type = xmlGetProp(n, "type");
470 if (type && !strcmp(type, "title"))
472 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
475 res->title = nmem_strdup(se->nmem, value);
476 relevance_countwords(se->relevance, head, value, 4);
483 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
488 relevance_donerecord(se->relevance, head);
494 static void ingest_records(struct client *cl, Z_Records *r)
497 yaz_timing_t t = yaz_timing_create();
500 struct session *s = cl->session;
501 Z_NamePlusRecordList *rlist;
504 if (r->which != Z_Records_DBOSD)
506 rlist = r->u.databaseOrSurDiagnostics;
507 for (i = 0; i < rlist->num_records; i++)
509 Z_NamePlusRecord *npr = rlist->records[i];
511 if (npr->which != Z_NamePlusRecord_databaseRecord)
513 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
517 rec = ingest_record(cl, npr->u.databaseRecord);
521 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
522 session_alert_watch(s, SESSION_WATCH_RECORDS);
526 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
527 yaz_timing_get_real(t), yaz_timing_get_user(t),
528 yaz_timing_get_sys(t));
529 yaz_timing_destroy(&t);
533 static void do_presentResponse(IOCHAN i, Z_APDU *a)
535 struct connection *co = iochan_getdata(i);
536 struct client *cl = co->client;
537 Z_PresentResponse *r = a->u.presentResponse;
540 Z_Records *recs = r->records;
541 if (recs->which == Z_Records_NSD)
543 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
544 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
545 cl->state = Client_Error;
549 if (!*r->presentStatus && cl->state != Client_Error)
551 yaz_log(YLOG_DEBUG, "Good Present response");
552 cl->records += *r->numberOfRecordsReturned;
553 ingest_records(cl, r->records);
554 cl->state = Client_Idle;
556 else if (*r->presentStatus)
558 yaz_log(YLOG_WARN, "Bad Present response");
559 cl->state = Client_Error;
563 static void handler(IOCHAN i, int event)
565 struct connection *co = iochan_getdata(i);
566 struct client *cl = co->client;
567 struct session *se = 0;
573 yaz_log(YLOG_WARN, "Destroying orphan connection");
574 connection_destroy(co);
578 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
581 socklen_t errlen = sizeof(errcode);
583 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
584 &errlen) < 0 || errcode != 0)
591 yaz_log(YLOG_DEBUG, "Connect OK");
592 co->state = Conn_Open;
594 cl->state = Client_Connected;
598 else if (event & EVENT_INPUT)
600 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
604 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
605 connection_destroy(co);
610 yaz_log(YLOG_WARN, "EOF reading from Z server");
611 connection_destroy(co);
614 else if (len > 1) // We discard input if we have no connection
616 co->state = Conn_Open;
618 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
622 odr_reset(global_parameters.odr_in);
623 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
624 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
631 case Z_APDU_initResponse:
632 do_initResponse(i, a);
634 case Z_APDU_searchResponse:
635 do_searchResponse(i, a);
637 case Z_APDU_presentResponse:
638 do_presentResponse(i, a);
641 yaz_log(YLOG_WARN, "Unexpected result from server");
645 // We aren't expecting staggered output from target
646 // if (cs_more(t->link))
647 // iochan_setevent(i, EVENT_INPUT);
649 else // we throw away response and go to idle mode
651 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
652 cl->state = Client_Idle;
655 /* if len==1 we do nothing but wait for more input */
658 if (cl->state == Client_Connected) {
662 if (cl->state == Client_Idle)
664 if (cl->requestid != se->requestid && *se->query) {
667 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
668 cl->records < cl->hits) {
674 // Disassociate connection from client
675 static void connection_release(struct connection *co)
677 struct client *cl = co->client;
679 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
686 // Close connection and recycle structure
687 static void connection_destroy(struct connection *co)
689 struct host *h = co->host;
691 iochan_destroy(co->iochan);
693 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
694 if (h->connections == co)
695 h->connections = co->next;
698 struct connection *pco;
699 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
702 pco->next = co->next;
708 if (co->client->state != Client_Idle)
709 co->client->state = Client_Disconnected;
710 co->client->connection = 0;
712 co->next = connection_freelist;
713 connection_freelist = co;
716 // Creates a new connection for client, associated with the host of
718 static struct connection *connection_create(struct client *cl)
720 struct connection *new;
725 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
726 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
728 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
732 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
734 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
738 res = cs_connect(link, addr);
741 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
745 if ((new = connection_freelist))
746 connection_freelist = new->next;
749 new = xmalloc(sizeof (struct connection));
753 new->state = Conn_Connecting;
754 new->host = cl->database->host;
755 new->next = new->host->connections;
756 new->host->connections = new;
758 cl->connection = new;
761 new->iochan = iochan_create(cs_fileno(link), handler, 0);
762 iochan_setdata(new->iochan, new);
763 new->iochan->next = channel_list;
764 channel_list = new->iochan;
768 // Close connection and set state to error
769 static void client_fatal(struct client *cl)
771 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
772 connection_destroy(cl->connection);
773 cl->state = Client_Error;
776 // Ensure that client has a connection associated
777 static int client_prep_connection(struct client *cl)
779 struct connection *co;
780 struct session *se = cl->session;
781 struct host *host = cl->database->host;
785 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
789 // See if someone else has an idle connection
790 // We should look at timestamps here to select the longest-idle connection
791 for (co = host->connections; co; co = co->next)
792 if (co->state == Conn_Open && (!co->client || co->client->session != se))
796 connection_release(co);
801 co = connection_create(cl);
805 if (co->state == Conn_Connecting)
807 cl->state = Client_Connecting;
808 iochan_setflag(co->iochan, EVENT_OUTPUT);
810 else if (co->state == Conn_Open)
812 if (cl->state == Client_Error || cl->state == Client_Disconnected)
813 cl->state = Client_Idle;
814 iochan_setflag(co->iochan, EVENT_OUTPUT);
822 // This function will most likely vanish when a proper target profile mechanism is
824 void load_simpletargets(const char *fn)
826 FILE *f = fopen(fn, "r");
831 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
835 while (fgets(line, 255, f))
839 struct database *database;
841 if (strncmp(line, "target ", 7))
844 url[strlen(url) - 1] = '\0';
845 yaz_log(YLOG_DEBUG, "Target: %s", url);
846 if ((db = strchr(url, '/')))
851 for (host = hosts; host; host = host->next)
852 if (!strcmp(url, host->hostport))
856 struct addrinfo *addrinfo, hints;
859 unsigned char addrbuf[4];
862 host = xmalloc(sizeof(struct host));
863 host->hostport = xstrdup(url);
864 host->connections = 0;
866 if ((port = strchr(url, ':')))
872 hints.ai_family = PF_INET;
873 hints.ai_socktype = SOCK_STREAM;
874 hints.ai_protocol = IPPROTO_TCP;
875 hints.ai_addrlen = 0;
877 hints.ai_canonname = 0;
879 // This is not robust code. It assumes that getaddrinfo returns AF_INET
881 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
883 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
884 xfree(host->hostport);
888 assert(addrinfo->ai_family == PF_INET);
889 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
890 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
891 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
892 host->ipport = xstrdup(ipport);
893 freeaddrinfo(addrinfo);
897 database = xmalloc(sizeof(struct database));
898 database->host = host;
899 database->url = xmalloc(strlen(url) + strlen(db) + 2);
900 strcpy(database->url, url);
901 strcat(database->url, "/");
902 strcat(database->url, db);
904 database->databases = xmalloc(2 * sizeof(char *));
905 database->databases[0] = xstrdup(db);
906 database->databases[1] = 0;
907 database->errors = 0;
908 database->qprofile = 0;
909 database->rprofile = database_retrieval_profile(database);
910 database->next = databases;
911 databases = database;
917 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
925 pull_terms(nmem, n->u.p[0], termlist, num);
926 pull_terms(nmem, n->u.p[1], termlist, num);
929 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
936 // Extract terms from query into null-terminated termlist
937 static int extract_terms(NMEM nmem, char *query, char **termlist)
940 struct ccl_rpn_node *n;
943 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
946 pull_terms(nmem, n, termlist, &num);
952 static struct client *client_create(void)
958 client_freelist = client_freelist->next;
961 r = xmalloc(sizeof(struct client));
970 r->state = Client_Disconnected;
975 void client_destroy(struct client *c)
977 struct session *se = c->session;
978 if (c == se->clients)
979 se->clients = c->next;
983 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
989 connection_release(c->connection);
990 c->next = client_freelist;
994 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
996 s->watchlist[what].fun = fun;
997 s->watchlist[what].data = data;
1000 void session_alert_watch(struct session *s, int what)
1002 if (!s->watchlist[what].fun)
1004 (*s->watchlist[what].fun)(s->watchlist[what].data);
1005 s->watchlist[what].fun = 0;
1006 s->watchlist[what].data = 0;
1009 // This needs to be extended with selection criteria
1010 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
1014 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
1017 if (!config->retrievalprofiles)
1019 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
1021 return config->retrievalprofiles;
1024 // This should be extended with parameters to control selection criteria
1025 // Associates a set of clients with a session;
1026 int select_targets(struct session *se)
1028 struct database *db;
1032 client_destroy(se->clients);
1033 for (db = databases; db; db = db->next)
1035 struct client *cl = client_create();
1038 cl->next = se->clients;
1045 int session_active_clients(struct session *s)
1050 for (c = s->clients; c; c = c->next)
1051 if (c->connection && (c->state == Client_Connecting ||
1052 c->state == Client_Initializing ||
1053 c->state == Client_Searching ||
1054 c->state == Client_Presenting))
1060 char *search(struct session *se, char *query)
1062 int live_channels = 0;
1065 yaz_log(YLOG_DEBUG, "Search");
1067 strcpy(se->query, query);
1069 nmem_reset(se->nmem);
1070 for (cl = se->clients; cl; cl = cl->next)
1076 if (client_prep_connection(cl))
1082 int maxrecs = live_channels * global_parameters.toget;
1083 se->num_termlists = 0;
1084 se->reclist = reclist_create(se->nmem, maxrecs);
1085 extract_terms(se->nmem, query, p);
1086 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1087 se->total_records = se->total_hits = 0;
1088 se->expected_maxrecs = maxrecs;
1096 void destroy_session(struct session *s)
1098 yaz_log(YLOG_LOG, "Destroying session");
1100 client_destroy(s->clients);
1101 nmem_destroy(s->nmem);
1102 wrbuf_free(s->wrbuf, 1);
1105 struct session *new_session()
1108 struct session *session = xmalloc(sizeof(*session));
1110 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1112 session->total_hits = 0;
1113 session->total_records = 0;
1114 session->num_termlists = 0;
1115 session->reclist = 0;
1116 session->requestid = -1;
1117 session->clients = 0;
1118 session->expected_maxrecs = 0;
1119 session->query[0] = '\0';
1120 session->nmem = nmem_create();
1121 session->wrbuf = wrbuf_alloc();
1122 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1124 session->watchlist[i].data = 0;
1125 session->watchlist[i].fun = 0;
1128 select_targets(session);
1133 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1135 static struct hitsbytarget res[1000]; // FIXME MM
1139 for (cl = se->clients; cl; cl = cl->next)
1141 strcpy(res[*count].id, cl->database->host->hostport);
1142 res[*count].hits = cl->hits;
1143 res[*count].records = cl->records;
1144 res[*count].diagnostic = cl->diagnostic;
1145 res[*count].state = client_states[cl->state];
1146 res[*count].connected = cl->connection ? 1 : 0;
1153 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1157 for (i = 0; i < s->num_termlists; i++)
1158 if (!strcmp(s->termlists[i].name, name))
1159 return termlist_highscore(s->termlists[i].termlist, num);
1163 #ifdef MISSING_HEADERS
1164 void report_nmem_stats(void)
1166 size_t in_use, is_free;
1168 nmem_get_memory_in_use(&in_use);
1169 nmem_get_memory_free(&is_free);
1171 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1172 (long) in_use, (long) is_free);
1176 struct record **show(struct session *s, int start, int *num, int *total,
1177 int *sumhits, NMEM nmem_show)
1179 struct record **recs = nmem_malloc(nmem_show, *num
1180 * sizeof(struct record *));
1183 yaz_timing_t t = yaz_timing_create();
1185 relevance_prepare_read(s->relevance, s->reclist);
1187 *total = s->reclist->num_records;
1188 *sumhits = s->total_hits;
1190 for (i = 0; i < start; i++)
1191 if (!reclist_read_record(s->reclist))
1198 for (i = 0; i < *num; i++)
1200 struct record *r = reclist_read_record(s->reclist);
1210 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1211 yaz_timing_get_real(t), yaz_timing_get_user(t),
1212 yaz_timing_get_sys(t));
1213 yaz_timing_destroy(&t);
1218 void statistics(struct session *se, struct statistics *stat)
1223 bzero(stat, sizeof(*stat));
1224 for (cl = se->clients; cl; cl = cl->next)
1226 if (!cl->connection)
1227 stat->num_no_connection++;
1230 case Client_Connecting: stat->num_connecting++; break;
1231 case Client_Initializing: stat->num_initializing++; break;
1232 case Client_Searching: stat->num_searching++; break;
1233 case Client_Presenting: stat->num_presenting++; break;
1234 case Client_Idle: stat->num_idle++; break;
1235 case Client_Failed: stat->num_failed++; break;
1236 case Client_Error: stat->num_error++; break;
1241 stat->num_hits = se->total_hits;
1242 stat->num_records = se->total_records;
1244 stat->num_clients = count;
1247 static CCL_bibset load_cclfile(const char *fn)
1249 CCL_bibset res = ccl_qual_mk();
1250 if (ccl_qual_fname(res, fn) < 0)
1252 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1258 int main(int argc, char **argv)
1264 if (signal(SIGPIPE, SIG_IGN) < 0)
1265 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1267 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1269 while ((ret = options("f:x:c:h:p:C:s:d", argv, argc, &arg)) != -2)
1273 if (!read_config(arg))
1277 command_init(atoi(arg));
1285 global_parameters.ccl_filter = load_cclfile(arg);
1288 http_set_proxyaddr(arg);
1291 load_simpletargets(arg);
1294 global_parameters.dump_records = 1;
1297 fprintf(stderr, "Usage: pazpar2\n"
1299 " -h [host:]port (REST protocol listener)\n"
1300 " -c cmdport (telnet-style)\n"
1302 " -s simpletargetfile\n"
1303 " -p hostname[:portno] (HTTP proxy)\n");
1310 fprintf(stderr, "Set command port with -h or -c\n");
1314 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1315 global_parameters.yaz_marc = yaz_marc_create();
1316 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1317 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1318 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1320 event_loop(&channel_list);
1328 * indent-tabs-mode: nil
1330 * vim: shiftwidth=4 tabstop=8 expandtab