1 /* $Id: pazpar2.c,v 1.11 2007-01-04 07:27:29 adam 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);
458 add_facet(se, type, value);
459 relevance_countwords(se->relevance, head, value, 1);
463 else if (!strcmp(n->name, "metadata"))
465 xmlChar *type = xmlGetProp(n, "type"), *value;
466 if (!strcmp(type, "title"))
467 res->title = nmem_strdup(se->nmem,
468 value = xmlNodeListGetString(xdoc, n->children, 0));
470 relevance_countwords(se->relevance, head, value, 4);
475 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
480 relevance_donerecord(se->relevance, head);
486 static void ingest_records(struct client *cl, Z_Records *r)
489 yaz_timing_t t = yaz_timing_create();
492 struct session *s = cl->session;
493 Z_NamePlusRecordList *rlist;
496 if (r->which != Z_Records_DBOSD)
498 rlist = r->u.databaseOrSurDiagnostics;
499 for (i = 0; i < rlist->num_records; i++)
501 Z_NamePlusRecord *npr = rlist->records[i];
503 if (npr->which != Z_NamePlusRecord_databaseRecord)
505 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
509 rec = ingest_record(cl, npr->u.databaseRecord);
513 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
514 session_alert_watch(s, SESSION_WATCH_RECORDS);
518 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
519 yaz_timing_get_real(t), yaz_timing_get_user(t),
520 yaz_timing_get_sys(t));
521 yaz_timing_destroy(&t);
525 static void do_presentResponse(IOCHAN i, Z_APDU *a)
527 struct connection *co = iochan_getdata(i);
528 struct client *cl = co->client;
529 Z_PresentResponse *r = a->u.presentResponse;
532 Z_Records *recs = r->records;
533 if (recs->which == Z_Records_NSD)
535 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
536 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
537 cl->state = Client_Error;
541 if (!*r->presentStatus && cl->state != Client_Error)
543 yaz_log(YLOG_DEBUG, "Good Present response");
544 cl->records += *r->numberOfRecordsReturned;
545 ingest_records(cl, r->records);
546 cl->state = Client_Idle;
548 else if (*r->presentStatus)
550 yaz_log(YLOG_WARN, "Bad Present response");
551 cl->state = Client_Error;
555 static void handler(IOCHAN i, int event)
557 struct connection *co = iochan_getdata(i);
558 struct client *cl = co->client;
559 struct session *se = 0;
565 yaz_log(YLOG_WARN, "Destroying orphan connection");
566 connection_destroy(co);
570 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
573 socklen_t errlen = sizeof(errcode);
575 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
576 &errlen) < 0 || errcode != 0)
583 yaz_log(YLOG_DEBUG, "Connect OK");
584 co->state = Conn_Open;
586 cl->state = Client_Connected;
590 else if (event & EVENT_INPUT)
592 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
596 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
597 connection_destroy(co);
602 yaz_log(YLOG_WARN, "EOF reading from Z server");
603 connection_destroy(co);
606 else if (len > 1) // We discard input if we have no connection
608 co->state = Conn_Open;
610 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
614 odr_reset(global_parameters.odr_in);
615 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
616 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
623 case Z_APDU_initResponse:
624 do_initResponse(i, a);
626 case Z_APDU_searchResponse:
627 do_searchResponse(i, a);
629 case Z_APDU_presentResponse:
630 do_presentResponse(i, a);
633 yaz_log(YLOG_WARN, "Unexpected result from server");
637 // We aren't expecting staggered output from target
638 // if (cs_more(t->link))
639 // iochan_setevent(i, EVENT_INPUT);
641 else // we throw away response and go to idle mode
643 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
644 cl->state = Client_Idle;
647 /* if len==1 we do nothing but wait for more input */
650 if (cl->state == Client_Connected) {
654 if (cl->state == Client_Idle)
656 if (cl->requestid != se->requestid && *se->query) {
659 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
660 cl->records < cl->hits) {
666 // Disassociate connection from client
667 static void connection_release(struct connection *co)
669 struct client *cl = co->client;
671 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
678 // Close connection and recycle structure
679 static void connection_destroy(struct connection *co)
681 struct host *h = co->host;
683 iochan_destroy(co->iochan);
685 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
686 if (h->connections == co)
687 h->connections = co->next;
690 struct connection *pco;
691 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
694 pco->next = co->next;
700 if (co->client->state != Client_Idle)
701 co->client->state = Client_Disconnected;
702 co->client->connection = 0;
704 co->next = connection_freelist;
705 connection_freelist = co;
708 // Creates a new connection for client, associated with the host of
710 static struct connection *connection_create(struct client *cl)
712 struct connection *new;
717 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
718 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
720 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
724 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
726 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
730 res = cs_connect(link, addr);
733 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
737 if ((new = connection_freelist))
738 connection_freelist = new->next;
741 new = xmalloc(sizeof (struct connection));
745 new->state = Conn_Connecting;
746 new->host = cl->database->host;
747 new->next = new->host->connections;
748 new->host->connections = new;
750 cl->connection = new;
753 new->iochan = iochan_create(cs_fileno(link), handler, 0);
754 iochan_setdata(new->iochan, new);
755 new->iochan->next = channel_list;
756 channel_list = new->iochan;
760 // Close connection and set state to error
761 static void client_fatal(struct client *cl)
763 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
764 connection_destroy(cl->connection);
765 cl->state = Client_Error;
768 // Ensure that client has a connection associated
769 static int client_prep_connection(struct client *cl)
771 struct connection *co;
772 struct session *se = cl->session;
773 struct host *host = cl->database->host;
777 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
781 // See if someone else has an idle connection
782 // We should look at timestamps here to select the longest-idle connection
783 for (co = host->connections; co; co = co->next)
784 if (co->state == Conn_Open && (!co->client || co->client->session != se))
788 connection_release(co);
793 co = connection_create(cl);
797 if (co->state == Conn_Connecting)
799 cl->state = Client_Connecting;
800 iochan_setflag(co->iochan, EVENT_OUTPUT);
802 else if (co->state == Conn_Open)
804 if (cl->state == Client_Error || cl->state == Client_Disconnected)
805 cl->state = Client_Idle;
806 iochan_setflag(co->iochan, EVENT_OUTPUT);
814 // This function will most likely vanish when a proper target profile mechanism is
816 void load_simpletargets(const char *fn)
818 FILE *f = fopen(fn, "r");
823 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
827 while (fgets(line, 255, f))
831 struct database *database;
833 if (strncmp(line, "target ", 7))
836 url[strlen(url) - 1] = '\0';
837 yaz_log(YLOG_DEBUG, "Target: %s", url);
838 if ((db = strchr(url, '/')))
843 for (host = hosts; host; host = host->next)
844 if (!strcmp(url, host->hostport))
848 struct addrinfo *addrinfo, hints;
851 unsigned char addrbuf[4];
854 host = xmalloc(sizeof(struct host));
855 host->hostport = xstrdup(url);
856 host->connections = 0;
858 if ((port = strchr(url, ':')))
864 hints.ai_family = PF_INET;
865 hints.ai_socktype = SOCK_STREAM;
866 hints.ai_protocol = IPPROTO_TCP;
867 hints.ai_addrlen = 0;
869 hints.ai_canonname = 0;
871 // This is not robust code. It assumes that getaddrinfo returns AF_INET
873 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
875 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
876 xfree(host->hostport);
880 assert(addrinfo->ai_family == PF_INET);
881 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
882 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
883 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
884 host->ipport = xstrdup(ipport);
885 freeaddrinfo(addrinfo);
889 database = xmalloc(sizeof(struct database));
890 database->host = host;
891 database->url = xmalloc(strlen(url) + strlen(db) + 2);
892 strcpy(database->url, url);
893 strcat(database->url, "/");
894 strcat(database->url, db);
896 database->databases = xmalloc(2 * sizeof(char *));
897 database->databases[0] = xstrdup(db);
898 database->databases[1] = 0;
899 database->errors = 0;
900 database->qprofile = 0;
901 database->rprofile = database_retrieval_profile(database);
902 database->next = databases;
903 databases = database;
909 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
917 pull_terms(nmem, n->u.p[0], termlist, num);
918 pull_terms(nmem, n->u.p[1], termlist, num);
921 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
928 // Extract terms from query into null-terminated termlist
929 static int extract_terms(NMEM nmem, char *query, char **termlist)
932 struct ccl_rpn_node *n;
935 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
938 pull_terms(nmem, n, termlist, &num);
944 static struct client *client_create(void)
950 client_freelist = client_freelist->next;
953 r = xmalloc(sizeof(struct client));
962 r->state = Client_Disconnected;
967 void client_destroy(struct client *c)
969 struct session *se = c->session;
970 if (c == se->clients)
971 se->clients = c->next;
975 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
981 connection_release(c->connection);
982 c->next = client_freelist;
986 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
988 s->watchlist[what].fun = fun;
989 s->watchlist[what].data = data;
992 void session_alert_watch(struct session *s, int what)
994 if (!s->watchlist[what].fun)
996 (*s->watchlist[what].fun)(s->watchlist[what].data);
997 s->watchlist[what].fun = 0;
998 s->watchlist[what].data = 0;
1001 // This needs to be extended with selection criteria
1002 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
1006 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
1009 if (!config->retrievalprofiles)
1011 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
1013 return config->retrievalprofiles;
1016 // This should be extended with parameters to control selection criteria
1017 // Associates a set of clients with a session;
1018 int select_targets(struct session *se)
1020 struct database *db;
1024 client_destroy(se->clients);
1025 for (db = databases; db; db = db->next)
1027 struct client *cl = client_create();
1030 cl->next = se->clients;
1037 int session_active_clients(struct session *s)
1042 for (c = s->clients; c; c = c->next)
1043 if (c->connection && (c->state == Client_Connecting ||
1044 c->state == Client_Initializing ||
1045 c->state == Client_Searching ||
1046 c->state == Client_Presenting))
1052 char *search(struct session *se, char *query)
1054 int live_channels = 0;
1057 yaz_log(YLOG_DEBUG, "Search");
1059 strcpy(se->query, query);
1061 nmem_reset(se->nmem);
1062 for (cl = se->clients; cl; cl = cl->next)
1068 if (client_prep_connection(cl))
1074 int maxrecs = live_channels * global_parameters.toget;
1075 se->num_termlists = 0;
1076 se->reclist = reclist_create(se->nmem, maxrecs);
1077 extract_terms(se->nmem, query, p);
1078 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1079 se->total_records = se->total_hits = 0;
1080 se->expected_maxrecs = maxrecs;
1088 void destroy_session(struct session *s)
1090 yaz_log(YLOG_LOG, "Destroying session");
1092 client_destroy(s->clients);
1093 nmem_destroy(s->nmem);
1094 wrbuf_free(s->wrbuf, 1);
1097 struct session *new_session()
1100 struct session *session = xmalloc(sizeof(*session));
1102 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1104 session->total_hits = 0;
1105 session->total_records = 0;
1106 session->num_termlists = 0;
1107 session->reclist = 0;
1108 session->requestid = -1;
1109 session->clients = 0;
1110 session->expected_maxrecs = 0;
1111 session->query[0] = '\0';
1112 session->nmem = nmem_create();
1113 session->wrbuf = wrbuf_alloc();
1114 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1116 session->watchlist[i].data = 0;
1117 session->watchlist[i].fun = 0;
1120 select_targets(session);
1125 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1127 static struct hitsbytarget res[1000]; // FIXME MM
1131 for (cl = se->clients; cl; cl = cl->next)
1133 strcpy(res[*count].id, cl->database->host->hostport);
1134 res[*count].hits = cl->hits;
1135 res[*count].records = cl->records;
1136 res[*count].diagnostic = cl->diagnostic;
1137 res[*count].state = client_states[cl->state];
1138 res[*count].connected = cl->connection ? 1 : 0;
1145 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1149 for (i = 0; i < s->num_termlists; i++)
1150 if (!strcmp(s->termlists[i].name, name))
1151 return termlist_highscore(s->termlists[i].termlist, num);
1156 // conditional compilation by SH: This lead to a warning with currently installed
1157 // YAZ header files on us1
1158 void report_nmem_stats(void)
1160 size_t in_use, is_free;
1162 nmem_get_memory_in_use(&in_use);
1163 nmem_get_memory_free(&is_free);
1165 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1166 (long) in_use, (long) is_free);
1170 struct record **show(struct session *s, int start, int *num, int *total,
1171 int *sumhits, NMEM nmem_show)
1173 struct record **recs = nmem_malloc(nmem_show, *num
1174 * sizeof(struct record *));
1177 yaz_timing_t t = yaz_timing_create();
1179 relevance_prepare_read(s->relevance, s->reclist);
1181 *total = s->reclist->num_records;
1182 *sumhits = s->total_hits;
1184 for (i = 0; i < start; i++)
1185 if (!reclist_read_record(s->reclist))
1192 for (i = 0; i < *num; i++)
1194 struct record *r = reclist_read_record(s->reclist);
1204 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1205 yaz_timing_get_real(t), yaz_timing_get_user(t),
1206 yaz_timing_get_sys(t));
1207 yaz_timing_destroy(&t);
1212 void statistics(struct session *se, struct statistics *stat)
1217 bzero(stat, sizeof(*stat));
1218 for (cl = se->clients; cl; cl = cl->next)
1220 if (!cl->connection)
1221 stat->num_no_connection++;
1224 case Client_Connecting: stat->num_connecting++; break;
1225 case Client_Initializing: stat->num_initializing++; break;
1226 case Client_Searching: stat->num_searching++; break;
1227 case Client_Presenting: stat->num_presenting++; break;
1228 case Client_Idle: stat->num_idle++; break;
1229 case Client_Failed: stat->num_failed++; break;
1230 case Client_Error: stat->num_error++; break;
1235 stat->num_hits = se->total_hits;
1236 stat->num_records = se->total_records;
1238 stat->num_clients = count;
1241 static CCL_bibset load_cclfile(const char *fn)
1243 CCL_bibset res = ccl_qual_mk();
1244 if (ccl_qual_fname(res, fn) < 0)
1246 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1252 int main(int argc, char **argv)
1258 if (signal(SIGPIPE, SIG_IGN) < 0)
1259 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1261 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1263 while ((ret = options("f:x:c:h:p:C:s:d", argv, argc, &arg)) != -2)
1267 if (!read_config(arg))
1271 command_init(atoi(arg));
1279 global_parameters.ccl_filter = load_cclfile(arg);
1282 http_set_proxyaddr(arg);
1285 load_simpletargets(arg);
1288 global_parameters.dump_records = 1;
1291 fprintf(stderr, "Usage: pazpar2\n"
1293 " -h [host:]port (REST protocol listener)\n"
1294 " -c cmdport (telnet-style)\n"
1296 " -s simpletargetfile\n"
1297 " -p hostname[:portno] (HTTP proxy)\n");
1304 fprintf(stderr, "Set command port with -h or -c\n");
1308 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1309 global_parameters.yaz_marc = yaz_marc_create();
1310 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1311 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1312 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1314 event_loop(&channel_list);
1322 * indent-tabs-mode: nil
1324 * vim: shiftwidth=4 tabstop=8 expandtab