1 /* $Id: pazpar2.c,v 1.18 2007-01-08 12:43:41 adam 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>
29 #include <yaz/timing.h>
35 #include "termlists.h"
37 #include "relevance.h"
42 static void client_fatal(struct client *cl);
43 static void connection_destroy(struct connection *co);
44 static int client_prep_connection(struct client *cl);
45 static void ingest_records(struct client *cl, Z_Records *r);
46 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db);
47 void session_alert_watch(struct session *s, int what);
49 IOCHAN channel_list = 0; // Master list of connections we're handling events to
51 static struct connection *connection_freelist = 0;
52 static struct client *client_freelist = 0;
54 static struct host *hosts = 0; // The hosts we know about
55 static struct database *databases = 0; // The databases we know about
57 static char *client_states[] = {
61 "Client_Initializing",
66 "Client_Disconnected",
70 struct parameters global_parameters =
75 "Index Data PazPar2 (MasterKey)",
87 static int send_apdu(struct client *c, Z_APDU *a)
89 struct connection *co = c->connection;
93 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
95 odr_perror(global_parameters.odr_out, "Encoding APDU");
98 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
99 r = cs_put(co->link, buf, len);
102 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
107 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
110 odr_reset(global_parameters.odr_out); /* release the APDU structure */
111 co->state = Conn_Waiting;
116 static void send_init(IOCHAN i)
118 struct connection *co = iochan_getdata(i);
119 struct client *cl = co->client;
120 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
122 a->u.initRequest->implementationId = global_parameters.implementationId;
123 a->u.initRequest->implementationName = global_parameters.implementationName;
124 a->u.initRequest->implementationVersion =
125 global_parameters.implementationVersion;
126 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
127 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
128 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
130 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
131 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
132 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
133 if (send_apdu(cl, a) >= 0)
135 iochan_setflags(i, EVENT_INPUT);
136 cl->state = Client_Initializing;
139 cl->state = Client_Error;
140 odr_reset(global_parameters.odr_out);
143 static void send_search(IOCHAN i)
145 struct connection *co = iochan_getdata(i);
146 struct client *cl = co->client;
147 struct session *se = cl->session;
148 struct database *db = cl->database;
149 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
150 int ndb, cerror, cpos;
153 struct ccl_rpn_node *cn;
154 int ssub = 0, lslb = 100000, mspn = 10;
156 yaz_log(YLOG_DEBUG, "Sending search");
158 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
161 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
163 zquery->which = Z_Query_type_1;
164 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
167 for (ndb = 0; db->databases[ndb]; ndb++)
169 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
170 for (ndb = 0; db->databases[ndb]; ndb++)
171 databaselist[ndb] = db->databases[ndb];
173 a->u.presentRequest->preferredRecordSyntax =
174 yaz_oidval_to_z3950oid(global_parameters.odr_out,
175 CLASS_RECSYN, VAL_USMARC);
176 a->u.searchRequest->smallSetUpperBound = &ssub;
177 a->u.searchRequest->largeSetLowerBound = &lslb;
178 a->u.searchRequest->mediumSetPresentNumber = &mspn;
179 a->u.searchRequest->resultSetName = "Default";
180 a->u.searchRequest->databaseNames = databaselist;
181 a->u.searchRequest->num_databaseNames = ndb;
183 if (send_apdu(cl, a) >= 0)
185 iochan_setflags(i, EVENT_INPUT);
186 cl->state = Client_Searching;
187 cl->requestid = se->requestid;
190 cl->state = Client_Error;
192 odr_reset(global_parameters.odr_out);
195 static void send_present(IOCHAN i)
197 struct connection *co = iochan_getdata(i);
198 struct client *cl = co->client;
199 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
201 int start = cl->records + 1;
203 toget = global_parameters.chunk;
204 if (toget > global_parameters.toget - cl->records)
205 toget = global_parameters.toget - cl->records;
206 if (toget > cl->hits - cl->records)
207 toget = cl->hits - cl->records;
209 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
211 a->u.presentRequest->resultSetStartPoint = &start;
212 a->u.presentRequest->numberOfRecordsRequested = &toget;
214 a->u.presentRequest->resultSetId = "Default";
216 a->u.presentRequest->preferredRecordSyntax =
217 yaz_oidval_to_z3950oid(global_parameters.odr_out,
218 CLASS_RECSYN, VAL_USMARC);
220 if (send_apdu(cl, a) >= 0)
222 iochan_setflags(i, EVENT_INPUT);
223 cl->state = Client_Presenting;
226 cl->state = Client_Error;
227 odr_reset(global_parameters.odr_out);
230 static void do_initResponse(IOCHAN i, Z_APDU *a)
232 struct connection *co = iochan_getdata(i);
233 struct client *cl = co->client;
234 Z_InitResponse *r = a->u.initResponse;
236 yaz_log(YLOG_DEBUG, "Received init response");
240 cl->state = Client_Idle;
243 cl->state = Client_Failed; // FIXME need to do something to the connection
246 static void do_searchResponse(IOCHAN i, Z_APDU *a)
248 struct connection *co = iochan_getdata(i);
249 struct client *cl = co->client;
250 struct session *se = cl->session;
251 Z_SearchResponse *r = a->u.searchResponse;
253 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
255 if (*r->searchStatus)
257 cl->hits = *r->resultCount;
258 se->total_hits += cl->hits;
259 if (r->presentStatus && !*r->presentStatus && r->records)
261 yaz_log(YLOG_DEBUG, "Records in search response");
262 cl->records += *r->numberOfRecordsReturned;
263 ingest_records(cl, r->records);
265 cl->state = Client_Idle;
270 cl->state = Client_Error;
272 Z_Records *recs = r->records;
273 if (recs->which == Z_Records_NSD)
275 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
276 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
277 cl->state = Client_Error;
283 char *normalize_mergekey(char *buf)
285 char *p = buf, *pout = buf;
289 while (*p && !isalnum(*p))
292 *(pout++) = tolower(*(p++));
295 while (*p && !isalnum(*p))
306 // FIXME needs to be generalized. Should flexibly generate X lists per search
307 static void extract_subject(struct session *s, const char *rec)
309 const char *field, *subfield;
311 while ((field = find_field(rec, "650")))
314 if ((subfield = find_subfield(field, 'a')))
320 ef = index(subfield, '\n');
323 if ((e = index(subfield, '\t')) && e < ef)
325 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
329 memcpy(buf, subfield, len);
333 termlist_insert(s->termlist, buf);
340 static void add_facet(struct session *s, const char *type, const char *value)
344 for (i = 0; i < s->num_termlists; i++)
345 if (!strcmp(s->termlists[i].name, type))
347 if (i == s->num_termlists)
349 if (i == SESSION_MAX_TERMLISTS)
351 yaz_log(YLOG_FATAL, "Too many termlists");
354 s->termlists[i].name = nmem_strdup(s->nmem, type);
355 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
356 s->num_termlists = i + 1;
358 termlist_insert(s->termlists[i].termlist, value);
361 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
363 struct conf_retrievalprofile *rprofile = cl->database->rprofile;
364 struct conf_retrievalmap *m;
368 // First normalize to XML
369 if (rprofile->native_syntax == Nativesyn_iso2709)
373 if (rec->which != Z_External_octet)
375 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
378 buf = (char*) rec->u.octet_aligned->buf;
379 len = rec->u.octet_aligned->len;
380 if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
382 yaz_log(YLOG_WARN, "Failed to decode MARC");
385 if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
386 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
388 yaz_log(YLOG_WARN, "Failed to encode as XML");
391 rdoc = xmlNewDoc("1.0");
392 xmlDocSetRootElement(rdoc, res);
396 yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
399 for (m = rprofile->maplist; m; m = m->next)
402 if (m->type != Map_xslt)
404 yaz_log(YLOG_WARN, "Unknown map type");
407 if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
409 yaz_log(YLOG_WARN, "XSLT transformation failed");
415 if (global_parameters.dump_records)
417 fprintf(stderr, "Record:\n----------------\n");
418 xmlDocFormatDump(stderr, rdoc, 1);
423 static struct record *ingest_record(struct client *cl, Z_External *rec)
425 xmlDoc *xdoc = normalize_record(cl, rec);
427 struct record *res, *head;
428 struct session *se = cl->session;
429 xmlChar *mergekey, *mergekey_norm;
434 root = xmlDocGetRootElement(xdoc);
435 if (!(mergekey = xmlGetProp(root, "mergekey")))
437 yaz_log(YLOG_WARN, "No mergekey found in record");
442 res = nmem_malloc(se->nmem, sizeof(struct record));
443 res->next_cluster = 0;
444 res->target_offset = -1;
445 res->term_frequency_vec = 0;
446 res->title = "Unknown";
449 mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
451 res->merge_key = normalize_mergekey(mergekey_norm);
453 head = reclist_insert(se->reclist, res);
456 /* no room for record */
460 relevance_newrec(se->relevance, head);
462 for (n = root->children; n; n = n->next)
464 if (n->type != XML_ELEMENT_NODE)
466 if (!strcmp(n->name, "facet"))
468 xmlChar *type = xmlGetProp(n, "type");
469 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
472 add_facet(se, type, value);
473 relevance_countwords(se->relevance, head, value, 1);
478 else if (!strcmp(n->name, "metadata"))
480 xmlChar *type = xmlGetProp(n, "type");
481 if (type && !strcmp(type, "title"))
483 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
486 res->title = nmem_strdup(se->nmem, value);
487 relevance_countwords(se->relevance, head, value, 4);
494 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
499 relevance_donerecord(se->relevance, head);
505 static void ingest_records(struct client *cl, Z_Records *r)
508 yaz_timing_t t = yaz_timing_create();
511 struct session *s = cl->session;
512 Z_NamePlusRecordList *rlist;
515 if (r->which != Z_Records_DBOSD)
517 rlist = r->u.databaseOrSurDiagnostics;
518 for (i = 0; i < rlist->num_records; i++)
520 Z_NamePlusRecord *npr = rlist->records[i];
522 if (npr->which != Z_NamePlusRecord_databaseRecord)
524 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
528 rec = ingest_record(cl, npr->u.databaseRecord);
532 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
533 session_alert_watch(s, SESSION_WATCH_RECORDS);
537 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
538 yaz_timing_get_real(t), yaz_timing_get_user(t),
539 yaz_timing_get_sys(t));
540 yaz_timing_destroy(&t);
544 static void do_presentResponse(IOCHAN i, Z_APDU *a)
546 struct connection *co = iochan_getdata(i);
547 struct client *cl = co->client;
548 Z_PresentResponse *r = a->u.presentResponse;
551 Z_Records *recs = r->records;
552 if (recs->which == Z_Records_NSD)
554 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
555 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
556 cl->state = Client_Error;
560 if (!*r->presentStatus && cl->state != Client_Error)
562 yaz_log(YLOG_DEBUG, "Good Present response");
563 cl->records += *r->numberOfRecordsReturned;
564 ingest_records(cl, r->records);
565 cl->state = Client_Idle;
567 else if (*r->presentStatus)
569 yaz_log(YLOG_WARN, "Bad Present response");
570 cl->state = Client_Error;
574 static void handler(IOCHAN i, int event)
576 struct connection *co = iochan_getdata(i);
577 struct client *cl = co->client;
578 struct session *se = 0;
584 yaz_log(YLOG_WARN, "Destroying orphan connection");
585 connection_destroy(co);
589 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
592 socklen_t errlen = sizeof(errcode);
594 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
595 &errlen) < 0 || errcode != 0)
602 yaz_log(YLOG_DEBUG, "Connect OK");
603 co->state = Conn_Open;
605 cl->state = Client_Connected;
609 else if (event & EVENT_INPUT)
611 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
615 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
616 connection_destroy(co);
621 yaz_log(YLOG_WARN, "EOF reading from Z server");
622 connection_destroy(co);
625 else if (len > 1) // We discard input if we have no connection
627 co->state = Conn_Open;
629 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
633 odr_reset(global_parameters.odr_in);
634 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
635 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
642 case Z_APDU_initResponse:
643 do_initResponse(i, a);
645 case Z_APDU_searchResponse:
646 do_searchResponse(i, a);
648 case Z_APDU_presentResponse:
649 do_presentResponse(i, a);
652 yaz_log(YLOG_WARN, "Unexpected result from server");
656 // We aren't expecting staggered output from target
657 // if (cs_more(t->link))
658 // iochan_setevent(i, EVENT_INPUT);
660 else // we throw away response and go to idle mode
662 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
663 cl->state = Client_Idle;
666 /* if len==1 we do nothing but wait for more input */
669 if (cl->state == Client_Connected) {
673 if (cl->state == Client_Idle)
675 if (cl->requestid != se->requestid && *se->query) {
678 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
679 cl->records < cl->hits) {
685 // Disassociate connection from client
686 static void connection_release(struct connection *co)
688 struct client *cl = co->client;
690 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
697 // Close connection and recycle structure
698 static void connection_destroy(struct connection *co)
700 struct host *h = co->host;
702 iochan_destroy(co->iochan);
704 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
705 if (h->connections == co)
706 h->connections = co->next;
709 struct connection *pco;
710 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
713 pco->next = co->next;
719 if (co->client->state != Client_Idle)
720 co->client->state = Client_Disconnected;
721 co->client->connection = 0;
723 co->next = connection_freelist;
724 connection_freelist = co;
727 // Creates a new connection for client, associated with the host of
729 static struct connection *connection_create(struct client *cl)
731 struct connection *new;
736 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
737 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
739 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
743 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
745 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
749 res = cs_connect(link, addr);
752 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
756 if ((new = connection_freelist))
757 connection_freelist = new->next;
760 new = xmalloc(sizeof (struct connection));
764 new->state = Conn_Connecting;
765 new->host = cl->database->host;
766 new->next = new->host->connections;
767 new->host->connections = new;
769 cl->connection = new;
772 new->iochan = iochan_create(cs_fileno(link), handler, 0);
773 iochan_setdata(new->iochan, new);
774 new->iochan->next = channel_list;
775 channel_list = new->iochan;
779 // Close connection and set state to error
780 static void client_fatal(struct client *cl)
782 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
783 connection_destroy(cl->connection);
784 cl->state = Client_Error;
787 // Ensure that client has a connection associated
788 static int client_prep_connection(struct client *cl)
790 struct connection *co;
791 struct session *se = cl->session;
792 struct host *host = cl->database->host;
796 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
800 // See if someone else has an idle connection
801 // We should look at timestamps here to select the longest-idle connection
802 for (co = host->connections; co; co = co->next)
803 if (co->state == Conn_Open && (!co->client || co->client->session != se))
807 connection_release(co);
812 co = connection_create(cl);
816 if (co->state == Conn_Connecting)
818 cl->state = Client_Connecting;
819 iochan_setflag(co->iochan, EVENT_OUTPUT);
821 else if (co->state == Conn_Open)
823 if (cl->state == Client_Error || cl->state == Client_Disconnected)
824 cl->state = Client_Idle;
825 iochan_setflag(co->iochan, EVENT_OUTPUT);
833 // This function will most likely vanish when a proper target profile mechanism is
835 void load_simpletargets(const char *fn)
837 FILE *f = fopen(fn, "r");
842 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
846 while (fgets(line, 255, f))
850 struct database *database;
852 if (strncmp(line, "target ", 7))
855 url[strlen(url) - 1] = '\0';
856 yaz_log(YLOG_DEBUG, "Target: %s", url);
857 if ((db = strchr(url, '/')))
862 for (host = hosts; host; host = host->next)
863 if (!strcmp(url, host->hostport))
867 struct addrinfo *addrinfo, hints;
870 unsigned char addrbuf[4];
873 host = xmalloc(sizeof(struct host));
874 host->hostport = xstrdup(url);
875 host->connections = 0;
877 if ((port = strchr(url, ':')))
883 hints.ai_family = PF_INET;
884 hints.ai_socktype = SOCK_STREAM;
885 hints.ai_protocol = IPPROTO_TCP;
886 hints.ai_addrlen = 0;
888 hints.ai_canonname = 0;
890 // This is not robust code. It assumes that getaddrinfo returns AF_INET
892 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
894 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
895 xfree(host->hostport);
899 assert(addrinfo->ai_family == PF_INET);
900 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
901 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
902 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
903 host->ipport = xstrdup(ipport);
904 freeaddrinfo(addrinfo);
908 database = xmalloc(sizeof(struct database));
909 database->host = host;
910 database->url = xmalloc(strlen(url) + strlen(db) + 2);
911 strcpy(database->url, url);
912 strcat(database->url, "/");
913 strcat(database->url, db);
915 database->databases = xmalloc(2 * sizeof(char *));
916 database->databases[0] = xstrdup(db);
917 database->databases[1] = 0;
918 database->errors = 0;
919 database->qprofile = 0;
920 database->rprofile = database_retrieval_profile(database);
921 database->next = databases;
922 databases = database;
928 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
936 pull_terms(nmem, n->u.p[0], termlist, num);
937 pull_terms(nmem, n->u.p[1], termlist, num);
940 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
947 // Extract terms from query into null-terminated termlist
948 static int extract_terms(NMEM nmem, char *query, char **termlist)
951 struct ccl_rpn_node *n;
954 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
957 pull_terms(nmem, n, termlist, &num);
963 static struct client *client_create(void)
969 client_freelist = client_freelist->next;
972 r = xmalloc(sizeof(struct client));
981 r->state = Client_Disconnected;
986 void client_destroy(struct client *c)
988 struct session *se = c->session;
989 if (c == se->clients)
990 se->clients = c->next;
994 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
1000 connection_release(c->connection);
1001 c->next = client_freelist;
1002 client_freelist = c;
1005 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
1007 s->watchlist[what].fun = fun;
1008 s->watchlist[what].data = data;
1011 void session_alert_watch(struct session *s, int what)
1013 if (!s->watchlist[what].fun)
1015 (*s->watchlist[what].fun)(s->watchlist[what].data);
1016 s->watchlist[what].fun = 0;
1017 s->watchlist[what].data = 0;
1020 // This needs to be extended with selection criteria
1021 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
1025 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
1028 if (!config->retrievalprofiles)
1030 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
1032 return config->retrievalprofiles;
1035 // This should be extended with parameters to control selection criteria
1036 // Associates a set of clients with a session;
1037 int select_targets(struct session *se)
1039 struct database *db;
1043 client_destroy(se->clients);
1044 for (db = databases; db; db = db->next)
1046 struct client *cl = client_create();
1049 cl->next = se->clients;
1056 int session_active_clients(struct session *s)
1061 for (c = s->clients; c; c = c->next)
1062 if (c->connection && (c->state == Client_Connecting ||
1063 c->state == Client_Initializing ||
1064 c->state == Client_Searching ||
1065 c->state == Client_Presenting))
1071 char *search(struct session *se, char *query)
1073 int live_channels = 0;
1076 yaz_log(YLOG_DEBUG, "Search");
1078 strcpy(se->query, query);
1080 nmem_reset(se->nmem);
1081 for (cl = se->clients; cl; cl = cl->next)
1087 if (client_prep_connection(cl))
1093 int maxrecs = live_channels * global_parameters.toget;
1094 se->num_termlists = 0;
1095 se->reclist = reclist_create(se->nmem, maxrecs);
1096 extract_terms(se->nmem, query, p);
1097 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1098 se->total_records = se->total_hits = 0;
1099 se->expected_maxrecs = maxrecs;
1107 void destroy_session(struct session *s)
1109 yaz_log(YLOG_LOG, "Destroying session");
1111 client_destroy(s->clients);
1112 nmem_destroy(s->nmem);
1113 wrbuf_free(s->wrbuf, 1);
1116 struct session *new_session()
1119 struct session *session = xmalloc(sizeof(*session));
1121 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1123 session->total_hits = 0;
1124 session->total_records = 0;
1125 session->num_termlists = 0;
1126 session->reclist = 0;
1127 session->requestid = -1;
1128 session->clients = 0;
1129 session->expected_maxrecs = 0;
1130 session->query[0] = '\0';
1131 session->nmem = nmem_create();
1132 session->wrbuf = wrbuf_alloc();
1133 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1135 session->watchlist[i].data = 0;
1136 session->watchlist[i].fun = 0;
1139 select_targets(session);
1144 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1146 static struct hitsbytarget res[1000]; // FIXME MM
1150 for (cl = se->clients; cl; cl = cl->next)
1152 strcpy(res[*count].id, cl->database->host->hostport);
1153 res[*count].hits = cl->hits;
1154 res[*count].records = cl->records;
1155 res[*count].diagnostic = cl->diagnostic;
1156 res[*count].state = client_states[cl->state];
1157 res[*count].connected = cl->connection ? 1 : 0;
1164 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1168 for (i = 0; i < s->num_termlists; i++)
1169 if (!strcmp(s->termlists[i].name, name))
1170 return termlist_highscore(s->termlists[i].termlist, num);
1174 #ifdef MISSING_HEADERS
1175 void report_nmem_stats(void)
1177 size_t in_use, is_free;
1179 nmem_get_memory_in_use(&in_use);
1180 nmem_get_memory_free(&is_free);
1182 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1183 (long) in_use, (long) is_free);
1187 struct record **show(struct session *s, int start, int *num, int *total,
1188 int *sumhits, NMEM nmem_show)
1190 struct record **recs = nmem_malloc(nmem_show, *num
1191 * sizeof(struct record *));
1194 yaz_timing_t t = yaz_timing_create();
1196 relevance_prepare_read(s->relevance, s->reclist);
1198 *total = s->reclist->num_records;
1199 *sumhits = s->total_hits;
1201 for (i = 0; i < start; i++)
1202 if (!reclist_read_record(s->reclist))
1209 for (i = 0; i < *num; i++)
1211 struct record *r = reclist_read_record(s->reclist);
1221 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1222 yaz_timing_get_real(t), yaz_timing_get_user(t),
1223 yaz_timing_get_sys(t));
1224 yaz_timing_destroy(&t);
1229 void statistics(struct session *se, struct statistics *stat)
1234 bzero(stat, sizeof(*stat));
1235 for (cl = se->clients; cl; cl = cl->next)
1237 if (!cl->connection)
1238 stat->num_no_connection++;
1241 case Client_Connecting: stat->num_connecting++; break;
1242 case Client_Initializing: stat->num_initializing++; break;
1243 case Client_Searching: stat->num_searching++; break;
1244 case Client_Presenting: stat->num_presenting++; break;
1245 case Client_Idle: stat->num_idle++; break;
1246 case Client_Failed: stat->num_failed++; break;
1247 case Client_Error: stat->num_error++; break;
1252 stat->num_hits = se->total_hits;
1253 stat->num_records = se->total_records;
1255 stat->num_clients = count;
1258 static CCL_bibset load_cclfile(const char *fn)
1260 CCL_bibset res = ccl_qual_mk();
1261 if (ccl_qual_fname(res, fn) < 0)
1263 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1269 int main(int argc, char **argv)
1275 if (signal(SIGPIPE, SIG_IGN) < 0)
1276 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1278 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1280 while ((ret = options("f:x:h:p:C:s:d", argv, argc, &arg)) != -2)
1284 if (!read_config(arg))
1292 global_parameters.ccl_filter = load_cclfile(arg);
1295 http_set_proxyaddr(arg);
1298 load_simpletargets(arg);
1301 global_parameters.dump_records = 1;
1304 fprintf(stderr, "Usage: pazpar2\n"
1306 " -h [host:]port (REST protocol listener)\n"
1308 " -s simpletargetfile\n"
1309 " -p hostname[:portno] (HTTP proxy)\n");
1316 fprintf(stderr, "Set command port with -h\n");
1320 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1321 global_parameters.yaz_marc = yaz_marc_create();
1322 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1323 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1324 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1326 event_loop(&channel_list);
1334 * indent-tabs-mode: nil
1336 * vim: shiftwidth=4 tabstop=8 expandtab