1 /* $Id: pazpar2.c,v 1.17 2007-01-06 04:54:58 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>
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 > global_parameters.toget - cl->records)
202 toget = global_parameters.toget - cl->records;
203 if (toget > cl->hits - cl->records)
204 toget = cl->hits - cl->records;
206 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
208 a->u.presentRequest->resultSetStartPoint = &start;
209 a->u.presentRequest->numberOfRecordsRequested = &toget;
211 a->u.presentRequest->resultSetId = "Default";
213 a->u.presentRequest->preferredRecordSyntax =
214 yaz_oidval_to_z3950oid(global_parameters.odr_out,
215 CLASS_RECSYN, VAL_USMARC);
217 if (send_apdu(cl, a) >= 0)
219 iochan_setflags(i, EVENT_INPUT);
220 cl->state = Client_Presenting;
223 cl->state = Client_Error;
224 odr_reset(global_parameters.odr_out);
227 static void do_initResponse(IOCHAN i, Z_APDU *a)
229 struct connection *co = iochan_getdata(i);
230 struct client *cl = co->client;
231 Z_InitResponse *r = a->u.initResponse;
233 yaz_log(YLOG_DEBUG, "Received init response");
237 cl->state = Client_Idle;
240 cl->state = Client_Failed; // FIXME need to do something to the connection
243 static void do_searchResponse(IOCHAN i, Z_APDU *a)
245 struct connection *co = iochan_getdata(i);
246 struct client *cl = co->client;
247 struct session *se = cl->session;
248 Z_SearchResponse *r = a->u.searchResponse;
250 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
252 if (*r->searchStatus)
254 cl->hits = *r->resultCount;
255 se->total_hits += cl->hits;
256 if (r->presentStatus && !*r->presentStatus && r->records)
258 yaz_log(YLOG_DEBUG, "Records in search response");
259 cl->records += *r->numberOfRecordsReturned;
260 ingest_records(cl, r->records);
262 cl->state = Client_Idle;
267 cl->state = Client_Error;
269 Z_Records *recs = r->records;
270 if (recs->which == Z_Records_NSD)
272 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
273 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
274 cl->state = Client_Error;
280 char *normalize_mergekey(char *buf)
282 char *p = buf, *pout = buf;
286 while (*p && !isalnum(*p))
289 *(pout++) = tolower(*(p++));
292 while (*p && !isalnum(*p))
303 // FIXME needs to be generalized. Should flexibly generate X lists per search
304 static void extract_subject(struct session *s, const char *rec)
306 const char *field, *subfield;
308 while ((field = find_field(rec, "650")))
311 if ((subfield = find_subfield(field, 'a')))
317 ef = index(subfield, '\n');
320 if ((e = index(subfield, '\t')) && e < ef)
322 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
326 memcpy(buf, subfield, len);
330 termlist_insert(s->termlist, buf);
337 static void add_facet(struct session *s, const char *type, const char *value)
341 for (i = 0; i < s->num_termlists; i++)
342 if (!strcmp(s->termlists[i].name, type))
344 if (i == s->num_termlists)
346 if (i == SESSION_MAX_TERMLISTS)
348 yaz_log(YLOG_FATAL, "Too many termlists");
351 s->termlists[i].name = nmem_strdup(s->nmem, type);
352 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
353 s->num_termlists = i + 1;
355 termlist_insert(s->termlists[i].termlist, value);
358 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
360 struct conf_retrievalprofile *rprofile = cl->database->rprofile;
361 struct conf_retrievalmap *m;
365 // First normalize to XML
366 if (rprofile->native_syntax == Nativesyn_iso2709)
370 if (rec->which != Z_External_octet)
372 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
375 buf = (char*) rec->u.octet_aligned->buf;
376 len = rec->u.octet_aligned->len;
377 if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
379 yaz_log(YLOG_WARN, "Failed to decode MARC");
382 if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
383 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
385 yaz_log(YLOG_WARN, "Failed to encode as XML");
388 rdoc = xmlNewDoc("1.0");
389 xmlDocSetRootElement(rdoc, res);
393 yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
396 for (m = rprofile->maplist; m; m = m->next)
399 if (m->type != Map_xslt)
401 yaz_log(YLOG_WARN, "Unknown map type");
404 if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
406 yaz_log(YLOG_WARN, "XSLT transformation failed");
412 if (global_parameters.dump_records)
414 fprintf(stderr, "Record:\n----------------\n");
415 xmlDocFormatDump(stderr, rdoc, 1);
420 static struct record *ingest_record(struct client *cl, Z_External *rec)
422 xmlDoc *xdoc = normalize_record(cl, rec);
424 struct record *res, *head;
425 struct session *se = cl->session;
426 xmlChar *mergekey, *mergekey_norm;
431 root = xmlDocGetRootElement(xdoc);
432 if (!(mergekey = xmlGetProp(root, "mergekey")))
434 yaz_log(YLOG_WARN, "No mergekey found in record");
439 res = nmem_malloc(se->nmem, sizeof(struct record));
440 res->next_cluster = 0;
441 res->target_offset = -1;
442 res->term_frequency_vec = 0;
443 res->title = "Unknown";
446 mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
448 res->merge_key = normalize_mergekey(mergekey_norm);
450 head = reclist_insert(se->reclist, res);
453 /* no room for record */
457 relevance_newrec(se->relevance, head);
459 for (n = root->children; n; n = n->next)
461 if (n->type != XML_ELEMENT_NODE)
463 if (!strcmp(n->name, "facet"))
465 xmlChar *type = xmlGetProp(n, "type");
466 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
469 add_facet(se, type, value);
470 relevance_countwords(se->relevance, head, value, 1);
475 else if (!strcmp(n->name, "metadata"))
477 xmlChar *type = xmlGetProp(n, "type");
478 if (type && !strcmp(type, "title"))
480 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
483 res->title = nmem_strdup(se->nmem, value);
484 relevance_countwords(se->relevance, head, value, 4);
491 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
496 relevance_donerecord(se->relevance, head);
502 static void ingest_records(struct client *cl, Z_Records *r)
505 yaz_timing_t t = yaz_timing_create();
508 struct session *s = cl->session;
509 Z_NamePlusRecordList *rlist;
512 if (r->which != Z_Records_DBOSD)
514 rlist = r->u.databaseOrSurDiagnostics;
515 for (i = 0; i < rlist->num_records; i++)
517 Z_NamePlusRecord *npr = rlist->records[i];
519 if (npr->which != Z_NamePlusRecord_databaseRecord)
521 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
525 rec = ingest_record(cl, npr->u.databaseRecord);
529 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
530 session_alert_watch(s, SESSION_WATCH_RECORDS);
534 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
535 yaz_timing_get_real(t), yaz_timing_get_user(t),
536 yaz_timing_get_sys(t));
537 yaz_timing_destroy(&t);
541 static void do_presentResponse(IOCHAN i, Z_APDU *a)
543 struct connection *co = iochan_getdata(i);
544 struct client *cl = co->client;
545 Z_PresentResponse *r = a->u.presentResponse;
548 Z_Records *recs = r->records;
549 if (recs->which == Z_Records_NSD)
551 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
552 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
553 cl->state = Client_Error;
557 if (!*r->presentStatus && cl->state != Client_Error)
559 yaz_log(YLOG_DEBUG, "Good Present response");
560 cl->records += *r->numberOfRecordsReturned;
561 ingest_records(cl, r->records);
562 cl->state = Client_Idle;
564 else if (*r->presentStatus)
566 yaz_log(YLOG_WARN, "Bad Present response");
567 cl->state = Client_Error;
571 static void handler(IOCHAN i, int event)
573 struct connection *co = iochan_getdata(i);
574 struct client *cl = co->client;
575 struct session *se = 0;
581 yaz_log(YLOG_WARN, "Destroying orphan connection");
582 connection_destroy(co);
586 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
589 socklen_t errlen = sizeof(errcode);
591 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
592 &errlen) < 0 || errcode != 0)
599 yaz_log(YLOG_DEBUG, "Connect OK");
600 co->state = Conn_Open;
602 cl->state = Client_Connected;
606 else if (event & EVENT_INPUT)
608 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
612 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
613 connection_destroy(co);
618 yaz_log(YLOG_WARN, "EOF reading from Z server");
619 connection_destroy(co);
622 else if (len > 1) // We discard input if we have no connection
624 co->state = Conn_Open;
626 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
630 odr_reset(global_parameters.odr_in);
631 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
632 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
639 case Z_APDU_initResponse:
640 do_initResponse(i, a);
642 case Z_APDU_searchResponse:
643 do_searchResponse(i, a);
645 case Z_APDU_presentResponse:
646 do_presentResponse(i, a);
649 yaz_log(YLOG_WARN, "Unexpected result from server");
653 // We aren't expecting staggered output from target
654 // if (cs_more(t->link))
655 // iochan_setevent(i, EVENT_INPUT);
657 else // we throw away response and go to idle mode
659 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
660 cl->state = Client_Idle;
663 /* if len==1 we do nothing but wait for more input */
666 if (cl->state == Client_Connected) {
670 if (cl->state == Client_Idle)
672 if (cl->requestid != se->requestid && *se->query) {
675 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
676 cl->records < cl->hits) {
682 // Disassociate connection from client
683 static void connection_release(struct connection *co)
685 struct client *cl = co->client;
687 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
694 // Close connection and recycle structure
695 static void connection_destroy(struct connection *co)
697 struct host *h = co->host;
699 iochan_destroy(co->iochan);
701 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
702 if (h->connections == co)
703 h->connections = co->next;
706 struct connection *pco;
707 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
710 pco->next = co->next;
716 if (co->client->state != Client_Idle)
717 co->client->state = Client_Disconnected;
718 co->client->connection = 0;
720 co->next = connection_freelist;
721 connection_freelist = co;
724 // Creates a new connection for client, associated with the host of
726 static struct connection *connection_create(struct client *cl)
728 struct connection *new;
733 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
734 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
736 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
740 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
742 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
746 res = cs_connect(link, addr);
749 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
753 if ((new = connection_freelist))
754 connection_freelist = new->next;
757 new = xmalloc(sizeof (struct connection));
761 new->state = Conn_Connecting;
762 new->host = cl->database->host;
763 new->next = new->host->connections;
764 new->host->connections = new;
766 cl->connection = new;
769 new->iochan = iochan_create(cs_fileno(link), handler, 0);
770 iochan_setdata(new->iochan, new);
771 new->iochan->next = channel_list;
772 channel_list = new->iochan;
776 // Close connection and set state to error
777 static void client_fatal(struct client *cl)
779 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
780 connection_destroy(cl->connection);
781 cl->state = Client_Error;
784 // Ensure that client has a connection associated
785 static int client_prep_connection(struct client *cl)
787 struct connection *co;
788 struct session *se = cl->session;
789 struct host *host = cl->database->host;
793 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
797 // See if someone else has an idle connection
798 // We should look at timestamps here to select the longest-idle connection
799 for (co = host->connections; co; co = co->next)
800 if (co->state == Conn_Open && (!co->client || co->client->session != se))
804 connection_release(co);
809 co = connection_create(cl);
813 if (co->state == Conn_Connecting)
815 cl->state = Client_Connecting;
816 iochan_setflag(co->iochan, EVENT_OUTPUT);
818 else if (co->state == Conn_Open)
820 if (cl->state == Client_Error || cl->state == Client_Disconnected)
821 cl->state = Client_Idle;
822 iochan_setflag(co->iochan, EVENT_OUTPUT);
830 // This function will most likely vanish when a proper target profile mechanism is
832 void load_simpletargets(const char *fn)
834 FILE *f = fopen(fn, "r");
839 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
843 while (fgets(line, 255, f))
847 struct database *database;
849 if (strncmp(line, "target ", 7))
852 url[strlen(url) - 1] = '\0';
853 yaz_log(YLOG_DEBUG, "Target: %s", url);
854 if ((db = strchr(url, '/')))
859 for (host = hosts; host; host = host->next)
860 if (!strcmp(url, host->hostport))
864 struct addrinfo *addrinfo, hints;
867 unsigned char addrbuf[4];
870 host = xmalloc(sizeof(struct host));
871 host->hostport = xstrdup(url);
872 host->connections = 0;
874 if ((port = strchr(url, ':')))
880 hints.ai_family = PF_INET;
881 hints.ai_socktype = SOCK_STREAM;
882 hints.ai_protocol = IPPROTO_TCP;
883 hints.ai_addrlen = 0;
885 hints.ai_canonname = 0;
887 // This is not robust code. It assumes that getaddrinfo returns AF_INET
889 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
891 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
892 xfree(host->hostport);
896 assert(addrinfo->ai_family == PF_INET);
897 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
898 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
899 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
900 host->ipport = xstrdup(ipport);
901 freeaddrinfo(addrinfo);
905 database = xmalloc(sizeof(struct database));
906 database->host = host;
907 database->url = xmalloc(strlen(url) + strlen(db) + 2);
908 strcpy(database->url, url);
909 strcat(database->url, "/");
910 strcat(database->url, db);
912 database->databases = xmalloc(2 * sizeof(char *));
913 database->databases[0] = xstrdup(db);
914 database->databases[1] = 0;
915 database->errors = 0;
916 database->qprofile = 0;
917 database->rprofile = database_retrieval_profile(database);
918 database->next = databases;
919 databases = database;
925 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
933 pull_terms(nmem, n->u.p[0], termlist, num);
934 pull_terms(nmem, n->u.p[1], termlist, num);
937 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
944 // Extract terms from query into null-terminated termlist
945 static int extract_terms(NMEM nmem, char *query, char **termlist)
948 struct ccl_rpn_node *n;
951 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
954 pull_terms(nmem, n, termlist, &num);
960 static struct client *client_create(void)
966 client_freelist = client_freelist->next;
969 r = xmalloc(sizeof(struct client));
978 r->state = Client_Disconnected;
983 void client_destroy(struct client *c)
985 struct session *se = c->session;
986 if (c == se->clients)
987 se->clients = c->next;
991 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
997 connection_release(c->connection);
998 c->next = client_freelist;
1002 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
1004 s->watchlist[what].fun = fun;
1005 s->watchlist[what].data = data;
1008 void session_alert_watch(struct session *s, int what)
1010 if (!s->watchlist[what].fun)
1012 (*s->watchlist[what].fun)(s->watchlist[what].data);
1013 s->watchlist[what].fun = 0;
1014 s->watchlist[what].data = 0;
1017 // This needs to be extended with selection criteria
1018 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
1022 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
1025 if (!config->retrievalprofiles)
1027 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
1029 return config->retrievalprofiles;
1032 // This should be extended with parameters to control selection criteria
1033 // Associates a set of clients with a session;
1034 int select_targets(struct session *se)
1036 struct database *db;
1040 client_destroy(se->clients);
1041 for (db = databases; db; db = db->next)
1043 struct client *cl = client_create();
1046 cl->next = se->clients;
1053 int session_active_clients(struct session *s)
1058 for (c = s->clients; c; c = c->next)
1059 if (c->connection && (c->state == Client_Connecting ||
1060 c->state == Client_Initializing ||
1061 c->state == Client_Searching ||
1062 c->state == Client_Presenting))
1068 char *search(struct session *se, char *query)
1070 int live_channels = 0;
1073 yaz_log(YLOG_DEBUG, "Search");
1075 strcpy(se->query, query);
1077 nmem_reset(se->nmem);
1078 for (cl = se->clients; cl; cl = cl->next)
1084 if (client_prep_connection(cl))
1090 int maxrecs = live_channels * global_parameters.toget;
1091 se->num_termlists = 0;
1092 se->reclist = reclist_create(se->nmem, maxrecs);
1093 extract_terms(se->nmem, query, p);
1094 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1095 se->total_records = se->total_hits = 0;
1096 se->expected_maxrecs = maxrecs;
1104 void destroy_session(struct session *s)
1106 yaz_log(YLOG_LOG, "Destroying session");
1108 client_destroy(s->clients);
1109 nmem_destroy(s->nmem);
1110 wrbuf_free(s->wrbuf, 1);
1113 struct session *new_session()
1116 struct session *session = xmalloc(sizeof(*session));
1118 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1120 session->total_hits = 0;
1121 session->total_records = 0;
1122 session->num_termlists = 0;
1123 session->reclist = 0;
1124 session->requestid = -1;
1125 session->clients = 0;
1126 session->expected_maxrecs = 0;
1127 session->query[0] = '\0';
1128 session->nmem = nmem_create();
1129 session->wrbuf = wrbuf_alloc();
1130 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1132 session->watchlist[i].data = 0;
1133 session->watchlist[i].fun = 0;
1136 select_targets(session);
1141 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1143 static struct hitsbytarget res[1000]; // FIXME MM
1147 for (cl = se->clients; cl; cl = cl->next)
1149 strcpy(res[*count].id, cl->database->host->hostport);
1150 res[*count].hits = cl->hits;
1151 res[*count].records = cl->records;
1152 res[*count].diagnostic = cl->diagnostic;
1153 res[*count].state = client_states[cl->state];
1154 res[*count].connected = cl->connection ? 1 : 0;
1161 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1165 for (i = 0; i < s->num_termlists; i++)
1166 if (!strcmp(s->termlists[i].name, name))
1167 return termlist_highscore(s->termlists[i].termlist, num);
1171 #ifdef MISSING_HEADERS
1172 void report_nmem_stats(void)
1174 size_t in_use, is_free;
1176 nmem_get_memory_in_use(&in_use);
1177 nmem_get_memory_free(&is_free);
1179 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1180 (long) in_use, (long) is_free);
1184 struct record **show(struct session *s, int start, int *num, int *total,
1185 int *sumhits, NMEM nmem_show)
1187 struct record **recs = nmem_malloc(nmem_show, *num
1188 * sizeof(struct record *));
1191 yaz_timing_t t = yaz_timing_create();
1193 relevance_prepare_read(s->relevance, s->reclist);
1195 *total = s->reclist->num_records;
1196 *sumhits = s->total_hits;
1198 for (i = 0; i < start; i++)
1199 if (!reclist_read_record(s->reclist))
1206 for (i = 0; i < *num; i++)
1208 struct record *r = reclist_read_record(s->reclist);
1218 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
1219 yaz_timing_get_real(t), yaz_timing_get_user(t),
1220 yaz_timing_get_sys(t));
1221 yaz_timing_destroy(&t);
1226 void statistics(struct session *se, struct statistics *stat)
1231 bzero(stat, sizeof(*stat));
1232 for (cl = se->clients; cl; cl = cl->next)
1234 if (!cl->connection)
1235 stat->num_no_connection++;
1238 case Client_Connecting: stat->num_connecting++; break;
1239 case Client_Initializing: stat->num_initializing++; break;
1240 case Client_Searching: stat->num_searching++; break;
1241 case Client_Presenting: stat->num_presenting++; break;
1242 case Client_Idle: stat->num_idle++; break;
1243 case Client_Failed: stat->num_failed++; break;
1244 case Client_Error: stat->num_error++; break;
1249 stat->num_hits = se->total_hits;
1250 stat->num_records = se->total_records;
1252 stat->num_clients = count;
1255 static CCL_bibset load_cclfile(const char *fn)
1257 CCL_bibset res = ccl_qual_mk();
1258 if (ccl_qual_fname(res, fn) < 0)
1260 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1266 int main(int argc, char **argv)
1272 if (signal(SIGPIPE, SIG_IGN) < 0)
1273 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1275 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1277 while ((ret = options("f:x:h:p:C:s:d", argv, argc, &arg)) != -2)
1281 if (!read_config(arg))
1289 global_parameters.ccl_filter = load_cclfile(arg);
1292 http_set_proxyaddr(arg);
1295 load_simpletargets(arg);
1298 global_parameters.dump_records = 1;
1301 fprintf(stderr, "Usage: pazpar2\n"
1303 " -h [host:]port (REST protocol listener)\n"
1305 " -s simpletargetfile\n"
1306 " -p hostname[:portno] (HTTP proxy)\n");
1313 fprintf(stderr, "Set command port with -h\n");
1317 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1318 global_parameters.yaz_marc = yaz_marc_create();
1319 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1320 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1321 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1323 event_loop(&channel_list);
1331 * indent-tabs-mode: nil
1333 * vim: shiftwidth=4 tabstop=8 expandtab