1 /* $Id: pazpar2.c,v 1.10 2007-01-04 03:16:14 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>
26 #include "termlists.h"
28 #include "relevance.h"
31 #define PAZPAR2_VERSION "0.1"
34 static void client_fatal(struct client *cl);
35 static void connection_destroy(struct connection *co);
36 static int client_prep_connection(struct client *cl);
37 static void ingest_records(struct client *cl, Z_Records *r);
38 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db);
39 void session_alert_watch(struct session *s, int what);
41 IOCHAN channel_list = 0; // Master list of connections we're handling events to
43 static struct connection *connection_freelist = 0;
44 static struct client *client_freelist = 0;
46 static struct host *hosts = 0; // The hosts we know about
47 static struct database *databases = 0; // The databases we know about
49 static char *client_states[] = {
53 "Client_Initializing",
58 "Client_Disconnected",
62 struct parameters global_parameters =
67 "Index Data PazPar2 (MasterKey)",
80 static int send_apdu(struct client *c, Z_APDU *a)
82 struct connection *co = c->connection;
86 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
88 odr_perror(global_parameters.odr_out, "Encoding APDU");
91 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
92 r = cs_put(co->link, buf, len);
95 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
100 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
103 odr_reset(global_parameters.odr_out); /* release the APDU structure */
104 co->state = Conn_Waiting;
109 static void send_init(IOCHAN i)
111 struct connection *co = iochan_getdata(i);
112 struct client *cl = co->client;
113 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
115 a->u.initRequest->implementationId = global_parameters.implementationId;
116 a->u.initRequest->implementationName = global_parameters.implementationName;
117 a->u.initRequest->implementationVersion =
118 global_parameters.implementationVersion;
119 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
120 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
121 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
123 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
124 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
125 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
126 if (send_apdu(cl, a) >= 0)
128 iochan_setflags(i, EVENT_INPUT);
129 cl->state = Client_Initializing;
132 cl->state = Client_Error;
133 odr_reset(global_parameters.odr_out);
136 static void send_search(IOCHAN i)
138 struct connection *co = iochan_getdata(i);
139 struct client *cl = co->client;
140 struct session *se = cl->session;
141 struct database *db = cl->database;
142 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
143 int ndb, cerror, cpos;
146 struct ccl_rpn_node *cn;
147 int ssub = 0, lslb = 100000, mspn = 10;
149 yaz_log(YLOG_DEBUG, "Sending search");
151 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
154 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
156 zquery->which = Z_Query_type_1;
157 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
160 for (ndb = 0; db->databases[ndb]; ndb++)
162 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
163 for (ndb = 0; db->databases[ndb]; ndb++)
164 databaselist[ndb] = db->databases[ndb];
166 a->u.presentRequest->preferredRecordSyntax =
167 yaz_oidval_to_z3950oid(global_parameters.odr_out,
168 CLASS_RECSYN, VAL_USMARC);
169 a->u.searchRequest->smallSetUpperBound = &ssub;
170 a->u.searchRequest->largeSetLowerBound = &lslb;
171 a->u.searchRequest->mediumSetPresentNumber = &mspn;
172 a->u.searchRequest->resultSetName = "Default";
173 a->u.searchRequest->databaseNames = databaselist;
174 a->u.searchRequest->num_databaseNames = ndb;
176 if (send_apdu(cl, a) >= 0)
178 iochan_setflags(i, EVENT_INPUT);
179 cl->state = Client_Searching;
180 cl->requestid = se->requestid;
183 cl->state = Client_Error;
185 odr_reset(global_parameters.odr_out);
188 static void send_present(IOCHAN i)
190 struct connection *co = iochan_getdata(i);
191 struct client *cl = co->client;
192 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
194 int start = cl->records + 1;
196 toget = global_parameters.chunk;
197 if (toget > cl->hits - cl->records)
198 toget = cl->hits - cl->records;
200 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
202 a->u.presentRequest->resultSetStartPoint = &start;
203 a->u.presentRequest->numberOfRecordsRequested = &toget;
205 a->u.presentRequest->resultSetId = "Default";
207 a->u.presentRequest->preferredRecordSyntax =
208 yaz_oidval_to_z3950oid(global_parameters.odr_out,
209 CLASS_RECSYN, VAL_USMARC);
211 if (send_apdu(cl, a) >= 0)
213 iochan_setflags(i, EVENT_INPUT);
214 cl->state = Client_Presenting;
217 cl->state = Client_Error;
218 odr_reset(global_parameters.odr_out);
221 static void do_initResponse(IOCHAN i, Z_APDU *a)
223 struct connection *co = iochan_getdata(i);
224 struct client *cl = co->client;
225 Z_InitResponse *r = a->u.initResponse;
227 yaz_log(YLOG_DEBUG, "Received init response");
231 cl->state = Client_Idle;
234 cl->state = Client_Failed; // FIXME need to do something to the connection
237 static void do_searchResponse(IOCHAN i, Z_APDU *a)
239 struct connection *co = iochan_getdata(i);
240 struct client *cl = co->client;
241 struct session *se = cl->session;
242 Z_SearchResponse *r = a->u.searchResponse;
244 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
246 if (*r->searchStatus)
248 cl->hits = *r->resultCount;
249 se->total_hits += cl->hits;
250 if (r->presentStatus && !*r->presentStatus && r->records)
252 yaz_log(YLOG_DEBUG, "Records in search response");
253 cl->records += *r->numberOfRecordsReturned;
254 ingest_records(cl, r->records);
256 cl->state = Client_Idle;
261 cl->state = Client_Error;
263 Z_Records *recs = r->records;
264 if (recs->which == Z_Records_NSD)
266 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
267 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
268 cl->state = Client_Error;
274 char *normalize_mergekey(char *buf)
276 char *p = buf, *pout = buf;
280 while (*p && !isalnum(*p))
283 *(pout++) = tolower(*(p++));
286 while (*p && !isalnum(*p))
297 // FIXME needs to be generalized. Should flexibly generate X lists per search
298 static void extract_subject(struct session *s, const char *rec)
300 const char *field, *subfield;
302 while ((field = find_field(rec, "650")))
305 if ((subfield = find_subfield(field, 'a')))
311 ef = index(subfield, '\n');
314 if ((e = index(subfield, '\t')) && e < ef)
316 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
320 memcpy(buf, subfield, len);
324 termlist_insert(s->termlist, buf);
331 static void add_facet(struct session *s, const char *type, const char *value)
335 for (i = 0; i < s->num_termlists; i++)
336 if (!strcmp(s->termlists[i].name, type))
338 if (i == s->num_termlists)
340 if (i == SESSION_MAX_TERMLISTS)
342 yaz_log(YLOG_FATAL, "Too many termlists");
345 s->termlists[i].name = nmem_strdup(s->nmem, type);
346 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
347 s->num_termlists = i + 1;
349 termlist_insert(s->termlists[i].termlist, value);
352 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
354 struct conf_retrievalprofile *rprofile = cl->database->rprofile;
355 struct conf_retrievalmap *m;
359 // First normalize to XML
360 if (rprofile->native_syntax == Nativesyn_iso2709)
364 if (rec->which != Z_External_octet)
366 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
369 buf = (char*) rec->u.octet_aligned->buf;
370 len = rec->u.octet_aligned->len;
371 if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
373 yaz_log(YLOG_WARN, "Failed to decode MARC");
376 if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
377 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
379 yaz_log(YLOG_WARN, "Failed to encode as XML");
382 rdoc = xmlNewDoc("1.0");
383 xmlDocSetRootElement(rdoc, res);
387 yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
390 for (m = rprofile->maplist; m; m = m->next)
393 if (m->type != Map_xslt)
395 yaz_log(YLOG_WARN, "Unknown map type");
398 if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
400 yaz_log(YLOG_WARN, "XSLT transformation failed");
406 if (global_parameters.dump_records)
408 fprintf(stderr, "Record:\n----------------\n");
409 xmlDocFormatDump(stderr, rdoc, 1);
414 static struct record *ingest_record(struct client *cl, Z_External *rec)
416 xmlDoc *xdoc = normalize_record(cl, rec);
418 struct record *res, *head;
419 struct session *se = cl->session;
420 xmlChar *mergekey, *mergekey_norm;
425 root = xmlDocGetRootElement(xdoc);
426 if (!(mergekey = xmlGetProp(root, "mergekey")))
428 yaz_log(YLOG_WARN, "No mergekey found in record");
432 res = nmem_malloc(se->nmem, sizeof(struct record));
433 res->next_cluster = 0;
434 res->target_offset = -1;
435 res->term_frequency_vec = 0;
436 res->title = "Unknown";
439 mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
441 res->merge_key = normalize_mergekey(mergekey_norm);
443 head = reclist_insert(se->reclist, res);
444 relevance_newrec(se->relevance, head);
446 for (n = root->children; n; n = n->next)
448 if (n->type != XML_ELEMENT_NODE)
450 if (!strcmp(n->name, "facet"))
452 xmlChar *type = xmlGetProp(n, "type");
453 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
454 add_facet(se, type, value);
455 relevance_countwords(se->relevance, head, value, 1);
459 else if (!strcmp(n->name, "metadata"))
461 xmlChar *type = xmlGetProp(n, "type"), *value;
462 if (!strcmp(type, "title"))
463 res->title = nmem_strdup(se->nmem,
464 value = xmlNodeListGetString(xdoc, n->children, 0));
466 relevance_countwords(se->relevance, head, value, 4);
471 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
476 relevance_donerecord(se->relevance, head);
482 static void ingest_records(struct client *cl, Z_Records *r)
485 struct session *s = cl->session;
486 Z_NamePlusRecordList *rlist;
489 if (r->which != Z_Records_DBOSD)
491 rlist = r->u.databaseOrSurDiagnostics;
492 for (i = 0; i < rlist->num_records; i++)
494 Z_NamePlusRecord *npr = rlist->records[i];
496 if (npr->which != Z_NamePlusRecord_databaseRecord)
498 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
502 rec = ingest_record(cl, npr->u.databaseRecord);
506 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
507 session_alert_watch(s, SESSION_WATCH_RECORDS);
510 static void do_presentResponse(IOCHAN i, Z_APDU *a)
512 struct connection *co = iochan_getdata(i);
513 struct client *cl = co->client;
514 Z_PresentResponse *r = a->u.presentResponse;
517 Z_Records *recs = r->records;
518 if (recs->which == Z_Records_NSD)
520 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
521 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
522 cl->state = Client_Error;
526 if (!*r->presentStatus && cl->state != Client_Error)
528 yaz_log(YLOG_DEBUG, "Good Present response");
529 cl->records += *r->numberOfRecordsReturned;
530 ingest_records(cl, r->records);
531 cl->state = Client_Idle;
533 else if (*r->presentStatus)
535 yaz_log(YLOG_WARN, "Bad Present response");
536 cl->state = Client_Error;
540 static void handler(IOCHAN i, int event)
542 struct connection *co = iochan_getdata(i);
543 struct client *cl = co->client;
544 struct session *se = 0;
550 yaz_log(YLOG_WARN, "Destroying orphan connection");
551 connection_destroy(co);
555 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
558 socklen_t errlen = sizeof(errcode);
560 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
561 &errlen) < 0 || errcode != 0)
568 yaz_log(YLOG_DEBUG, "Connect OK");
569 co->state = Conn_Open;
571 cl->state = Client_Connected;
575 else if (event & EVENT_INPUT)
577 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
581 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
582 connection_destroy(co);
587 yaz_log(YLOG_WARN, "EOF reading from Z server");
588 connection_destroy(co);
591 else if (len > 1) // We discard input if we have no connection
593 co->state = Conn_Open;
595 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
599 odr_reset(global_parameters.odr_in);
600 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
601 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
608 case Z_APDU_initResponse:
609 do_initResponse(i, a);
611 case Z_APDU_searchResponse:
612 do_searchResponse(i, a);
614 case Z_APDU_presentResponse:
615 do_presentResponse(i, a);
618 yaz_log(YLOG_WARN, "Unexpected result from server");
622 // We aren't expecting staggered output from target
623 // if (cs_more(t->link))
624 // iochan_setevent(i, EVENT_INPUT);
626 else // we throw away response and go to idle mode
628 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
629 cl->state = Client_Idle;
632 /* if len==1 we do nothing but wait for more input */
635 if (cl->state == Client_Connected) {
639 if (cl->state == Client_Idle)
641 if (cl->requestid != se->requestid && *se->query) {
644 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
645 cl->records < cl->hits) {
651 // Disassociate connection from client
652 static void connection_release(struct connection *co)
654 struct client *cl = co->client;
656 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
663 // Close connection and recycle structure
664 static void connection_destroy(struct connection *co)
666 struct host *h = co->host;
668 iochan_destroy(co->iochan);
670 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
671 if (h->connections == co)
672 h->connections = co->next;
675 struct connection *pco;
676 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
679 pco->next = co->next;
685 if (co->client->state != Client_Idle)
686 co->client->state = Client_Disconnected;
687 co->client->connection = 0;
689 co->next = connection_freelist;
690 connection_freelist = co;
693 // Creates a new connection for client, associated with the host of
695 static struct connection *connection_create(struct client *cl)
697 struct connection *new;
702 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
703 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
705 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
709 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
711 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
715 res = cs_connect(link, addr);
718 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
722 if ((new = connection_freelist))
723 connection_freelist = new->next;
726 new = xmalloc(sizeof (struct connection));
730 new->state = Conn_Connecting;
731 new->host = cl->database->host;
732 new->next = new->host->connections;
733 new->host->connections = new;
735 cl->connection = new;
738 new->iochan = iochan_create(cs_fileno(link), handler, 0);
739 iochan_setdata(new->iochan, new);
740 new->iochan->next = channel_list;
741 channel_list = new->iochan;
745 // Close connection and set state to error
746 static void client_fatal(struct client *cl)
748 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
749 connection_destroy(cl->connection);
750 cl->state = Client_Error;
753 // Ensure that client has a connection associated
754 static int client_prep_connection(struct client *cl)
756 struct connection *co;
757 struct session *se = cl->session;
758 struct host *host = cl->database->host;
762 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
766 // See if someone else has an idle connection
767 // We should look at timestamps here to select the longest-idle connection
768 for (co = host->connections; co; co = co->next)
769 if (co->state == Conn_Open && (!co->client || co->client->session != se))
773 connection_release(co);
778 co = connection_create(cl);
782 if (co->state == Conn_Connecting)
784 cl->state = Client_Connecting;
785 iochan_setflag(co->iochan, EVENT_OUTPUT);
787 else if (co->state == Conn_Open)
789 if (cl->state == Client_Error || cl->state == Client_Disconnected)
790 cl->state = Client_Idle;
791 iochan_setflag(co->iochan, EVENT_OUTPUT);
799 // This function will most likely vanish when a proper target profile mechanism is
801 void load_simpletargets(const char *fn)
803 FILE *f = fopen(fn, "r");
808 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
812 while (fgets(line, 255, f))
816 struct database *database;
818 if (strncmp(line, "target ", 7))
821 url[strlen(url) - 1] = '\0';
822 yaz_log(YLOG_DEBUG, "Target: %s", url);
823 if ((db = strchr(url, '/')))
828 for (host = hosts; host; host = host->next)
829 if (!strcmp(url, host->hostport))
833 struct addrinfo *addrinfo, hints;
836 unsigned char addrbuf[4];
839 host = xmalloc(sizeof(struct host));
840 host->hostport = xstrdup(url);
841 host->connections = 0;
843 if ((port = strchr(url, ':')))
849 hints.ai_family = PF_INET;
850 hints.ai_socktype = SOCK_STREAM;
851 hints.ai_protocol = IPPROTO_TCP;
852 hints.ai_addrlen = 0;
854 hints.ai_canonname = 0;
856 // This is not robust code. It assumes that getaddrinfo returns AF_INET
858 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
860 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
861 xfree(host->hostport);
865 assert(addrinfo->ai_family == PF_INET);
866 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
867 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
868 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
869 host->ipport = xstrdup(ipport);
870 freeaddrinfo(addrinfo);
874 database = xmalloc(sizeof(struct database));
875 database->host = host;
876 database->url = xmalloc(strlen(url) + strlen(db) + 2);
877 strcpy(database->url, url);
878 strcat(database->url, "/");
879 strcat(database->url, db);
881 database->databases = xmalloc(2 * sizeof(char *));
882 database->databases[0] = xstrdup(db);
883 database->databases[1] = 0;
884 database->errors = 0;
885 database->qprofile = 0;
886 database->rprofile = database_retrieval_profile(database);
887 database->next = databases;
888 databases = database;
894 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
902 pull_terms(nmem, n->u.p[0], termlist, num);
903 pull_terms(nmem, n->u.p[1], termlist, num);
906 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
913 // Extract terms from query into null-terminated termlist
914 static int extract_terms(NMEM nmem, char *query, char **termlist)
917 struct ccl_rpn_node *n;
920 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
923 pull_terms(nmem, n, termlist, &num);
929 static struct client *client_create(void)
935 client_freelist = client_freelist->next;
938 r = xmalloc(sizeof(struct client));
947 r->state = Client_Disconnected;
952 void client_destroy(struct client *c)
954 struct session *se = c->session;
955 if (c == se->clients)
956 se->clients = c->next;
960 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
966 connection_release(c->connection);
967 c->next = client_freelist;
971 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
973 s->watchlist[what].fun = fun;
974 s->watchlist[what].data = data;
977 void session_alert_watch(struct session *s, int what)
979 if (!s->watchlist[what].fun)
981 (*s->watchlist[what].fun)(s->watchlist[what].data);
982 s->watchlist[what].fun = 0;
983 s->watchlist[what].data = 0;
986 // This needs to be extended with selection criteria
987 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
991 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
994 if (!config->retrievalprofiles)
996 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
998 return config->retrievalprofiles;
1001 // This should be extended with parameters to control selection criteria
1002 // Associates a set of clients with a session;
1003 int select_targets(struct session *se)
1005 struct database *db;
1009 client_destroy(se->clients);
1010 for (db = databases; db; db = db->next)
1012 struct client *cl = client_create();
1015 cl->next = se->clients;
1022 int session_active_clients(struct session *s)
1027 for (c = s->clients; c; c = c->next)
1028 if (c->connection && (c->state == Client_Connecting ||
1029 c->state == Client_Initializing ||
1030 c->state == Client_Searching ||
1031 c->state == Client_Presenting))
1037 char *search(struct session *se, char *query)
1039 int live_channels = 0;
1042 yaz_log(YLOG_DEBUG, "Search");
1044 strcpy(se->query, query);
1046 nmem_reset(se->nmem);
1047 for (cl = se->clients; cl; cl = cl->next)
1053 if (client_prep_connection(cl))
1059 int maxrecs = live_channels * global_parameters.toget;
1060 se->num_termlists = 0;
1061 se->reclist = reclist_create(se->nmem, maxrecs);
1062 extract_terms(se->nmem, query, p);
1063 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1064 se->total_records = se->total_hits = 0;
1065 se->expected_maxrecs = maxrecs;
1073 void destroy_session(struct session *s)
1075 yaz_log(YLOG_LOG, "Destroying session");
1077 client_destroy(s->clients);
1078 nmem_destroy(s->nmem);
1079 wrbuf_free(s->wrbuf, 1);
1082 struct session *new_session()
1085 struct session *session = xmalloc(sizeof(*session));
1087 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1089 session->total_hits = 0;
1090 session->total_records = 0;
1091 session->num_termlists = 0;
1092 session->reclist = 0;
1093 session->requestid = -1;
1094 session->clients = 0;
1095 session->expected_maxrecs = 0;
1096 session->query[0] = '\0';
1097 session->nmem = nmem_create();
1098 session->wrbuf = wrbuf_alloc();
1099 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1101 session->watchlist[i].data = 0;
1102 session->watchlist[i].fun = 0;
1105 select_targets(session);
1110 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1112 static struct hitsbytarget res[1000]; // FIXME MM
1116 for (cl = se->clients; cl; cl = cl->next)
1118 strcpy(res[*count].id, cl->database->host->hostport);
1119 res[*count].hits = cl->hits;
1120 res[*count].records = cl->records;
1121 res[*count].diagnostic = cl->diagnostic;
1122 res[*count].state = client_states[cl->state];
1123 res[*count].connected = cl->connection ? 1 : 0;
1130 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1134 for (i = 0; i < s->num_termlists; i++)
1135 if (!strcmp(s->termlists[i].name, name))
1136 return termlist_highscore(s->termlists[i].termlist, num);
1141 // conditional compilation by SH: This lead to a warning with currently installed
1142 // YAZ header files on us1
1143 void report_nmem_stats(void)
1145 size_t in_use, is_free;
1147 nmem_get_memory_in_use(&in_use);
1148 nmem_get_memory_free(&is_free);
1150 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1151 (long) in_use, (long) is_free);
1155 struct record **show(struct session *s, int start, int *num, int *total,
1156 int *sumhits, NMEM nmem_show)
1158 struct record **recs = nmem_malloc(nmem_show, *num
1159 * sizeof(struct record *));
1162 relevance_prepare_read(s->relevance, s->reclist);
1164 *total = s->reclist->num_records;
1165 *sumhits = s->total_hits;
1167 for (i = 0; i < start; i++)
1168 if (!reclist_read_record(s->reclist))
1174 for (i = 0; i < *num; i++)
1176 struct record *r = reclist_read_record(s->reclist);
1187 void statistics(struct session *se, struct statistics *stat)
1192 bzero(stat, sizeof(*stat));
1193 for (cl = se->clients; cl; cl = cl->next)
1195 if (!cl->connection)
1196 stat->num_no_connection++;
1199 case Client_Connecting: stat->num_connecting++; break;
1200 case Client_Initializing: stat->num_initializing++; break;
1201 case Client_Searching: stat->num_searching++; break;
1202 case Client_Presenting: stat->num_presenting++; break;
1203 case Client_Idle: stat->num_idle++; break;
1204 case Client_Failed: stat->num_failed++; break;
1205 case Client_Error: stat->num_error++; break;
1210 stat->num_hits = se->total_hits;
1211 stat->num_records = se->total_records;
1213 stat->num_clients = count;
1216 static CCL_bibset load_cclfile(const char *fn)
1218 CCL_bibset res = ccl_qual_mk();
1219 if (ccl_qual_fname(res, fn) < 0)
1221 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1227 int main(int argc, char **argv)
1233 if (signal(SIGPIPE, SIG_IGN) < 0)
1234 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1236 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1238 while ((ret = options("f:x:c:h:p:C:s:d", argv, argc, &arg)) != -2)
1242 if (!read_config(arg))
1246 command_init(atoi(arg));
1254 global_parameters.ccl_filter = load_cclfile(arg);
1257 http_set_proxyaddr(arg);
1260 load_simpletargets(arg);
1263 global_parameters.dump_records = 1;
1266 fprintf(stderr, "Usage: pazpar2\n"
1268 " -h [host:]port (REST protocol listener)\n"
1269 " -c cmdport (telnet-style)\n"
1271 " -s simpletargetfile\n"
1272 " -p hostname[:portno] (HTTP proxy)\n");
1279 fprintf(stderr, "Set command port with -h or -c\n");
1283 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1284 global_parameters.yaz_marc = yaz_marc_create();
1285 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1286 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1287 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1289 event_loop(&channel_list);
1297 * indent-tabs-mode: nil
1299 * vim: shiftwidth=4 tabstop=8 expandtab