1 /* $Id: pazpar2.c,v 1.4 2006-12-21 04:27:48 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>
25 #include "termlists.h"
27 #include "relevance.h"
29 #define PAZPAR2_VERSION "0.1"
32 static void client_fatal(struct client *cl);
33 static void connection_destroy(struct connection *co);
34 static int client_prep_connection(struct client *cl);
35 static void ingest_records(struct client *cl, Z_Records *r);
36 void session_alert_watch(struct session *s, int what);
38 IOCHAN channel_list = 0; // Master list of connections we're handling events to
40 static struct connection *connection_freelist = 0;
41 static struct client *client_freelist = 0;
43 static struct host *hosts = 0; // The hosts we know about
44 static struct database *databases = 0; // The databases we know about
46 static char *client_states[] = {
50 "Client_Initializing",
55 "Client_Disconnected",
59 struct parameters global_parameters =
63 "Index Data PazPar2 (MasterKey)",
76 static int send_apdu(struct client *c, Z_APDU *a)
78 struct connection *co = c->connection;
82 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
84 odr_perror(global_parameters.odr_out, "Encoding APDU");
87 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
88 r = cs_put(co->link, buf, len);
91 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
96 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
99 odr_reset(global_parameters.odr_out); /* release the APDU structure */
100 co->state = Conn_Waiting;
105 static void send_init(IOCHAN i)
107 struct connection *co = iochan_getdata(i);
108 struct client *cl = co->client;
109 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
111 a->u.initRequest->implementationId = global_parameters.implementationId;
112 a->u.initRequest->implementationName = global_parameters.implementationName;
113 a->u.initRequest->implementationVersion =
114 global_parameters.implementationVersion;
115 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
116 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
117 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
119 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
120 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
121 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
122 if (send_apdu(cl, a) >= 0)
124 iochan_setflags(i, EVENT_INPUT);
125 cl->state = Client_Initializing;
128 cl->state = Client_Error;
129 odr_reset(global_parameters.odr_out);
132 static void send_search(IOCHAN i)
134 struct connection *co = iochan_getdata(i);
135 struct client *cl = co->client;
136 struct session *se = cl->session;
137 struct database *db = cl->database;
138 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
139 int ndb, cerror, cpos;
142 struct ccl_rpn_node *cn;
143 int ssub = 0, lslb = 100000, mspn = 10;
145 yaz_log(YLOG_DEBUG, "Sending search");
147 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
150 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
152 zquery->which = Z_Query_type_1;
153 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
156 for (ndb = 0; db->databases[ndb]; ndb++)
158 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
159 for (ndb = 0; db->databases[ndb]; ndb++)
160 databaselist[ndb] = db->databases[ndb];
162 a->u.presentRequest->preferredRecordSyntax =
163 yaz_oidval_to_z3950oid(global_parameters.odr_out,
164 CLASS_RECSYN, VAL_USMARC);
165 a->u.searchRequest->smallSetUpperBound = &ssub;
166 a->u.searchRequest->largeSetLowerBound = &lslb;
167 a->u.searchRequest->mediumSetPresentNumber = &mspn;
168 a->u.searchRequest->resultSetName = "Default";
169 a->u.searchRequest->databaseNames = databaselist;
170 a->u.searchRequest->num_databaseNames = ndb;
172 if (send_apdu(cl, a) >= 0)
174 iochan_setflags(i, EVENT_INPUT);
175 cl->state = Client_Searching;
176 cl->requestid = se->requestid;
179 cl->state = Client_Error;
181 odr_reset(global_parameters.odr_out);
184 static void send_present(IOCHAN i)
186 struct connection *co = iochan_getdata(i);
187 struct client *cl = co->client;
188 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
190 int start = cl->records + 1;
192 toget = global_parameters.chunk;
193 if (toget > cl->hits - cl->records)
194 toget = cl->hits - cl->records;
196 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
198 a->u.presentRequest->resultSetStartPoint = &start;
199 a->u.presentRequest->numberOfRecordsRequested = &toget;
201 a->u.presentRequest->resultSetId = "Default";
203 a->u.presentRequest->preferredRecordSyntax =
204 yaz_oidval_to_z3950oid(global_parameters.odr_out,
205 CLASS_RECSYN, VAL_USMARC);
207 if (send_apdu(cl, a) >= 0)
209 iochan_setflags(i, EVENT_INPUT);
210 cl->state = Client_Presenting;
213 cl->state = Client_Error;
214 odr_reset(global_parameters.odr_out);
217 static void do_initResponse(IOCHAN i, Z_APDU *a)
219 struct connection *co = iochan_getdata(i);
220 struct client *cl = co->client;
221 Z_InitResponse *r = a->u.initResponse;
223 yaz_log(YLOG_DEBUG, "Received init response");
227 cl->state = Client_Idle;
230 cl->state = Client_Failed; // FIXME need to do something to the connection
233 static void do_searchResponse(IOCHAN i, Z_APDU *a)
235 struct connection *co = iochan_getdata(i);
236 struct client *cl = co->client;
237 struct session *se = cl->session;
238 Z_SearchResponse *r = a->u.searchResponse;
240 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
242 if (*r->searchStatus)
244 cl->hits = *r->resultCount;
245 se->total_hits += cl->hits;
246 if (r->presentStatus && !*r->presentStatus && r->records)
248 yaz_log(YLOG_DEBUG, "Records in search response");
249 cl->records += *r->numberOfRecordsReturned;
250 ingest_records(cl, r->records);
252 cl->state = Client_Idle;
257 cl->state = Client_Error;
259 Z_Records *recs = r->records;
260 if (recs->which == Z_Records_NSD)
262 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
263 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
264 cl->state = Client_Error;
270 const char *find_field(const char *rec, const char *field)
276 strcpy(lbuf + 1, field);
278 if ((line = strstr(rec, lbuf)))
284 const char *find_subfield(const char *field, char subfield)
286 const char *p = field;
288 while (*p && *p != '\n')
290 while (*p != '\n' && *p != '\t')
292 if (*p == '\t' && *(++p) == subfield) {
304 // Extract 245 $a $b 100 $a
305 char *extract_title(struct session *s, const char *rec)
307 const char *field, *subfield;
309 unsigned char *obuf, *p;
311 wrbuf_rewind(s->wrbuf);
313 if (!(field = find_field(rec, "245")))
315 if (!(subfield = find_subfield(field, 'a')))
317 ef = index(subfield, '\n');
318 if ((e = index(subfield, '\t')) && e < ef)
322 wrbuf_write(s->wrbuf, subfield, ef - subfield);
323 if ((subfield = find_subfield(field, 'b')))
325 ef = index(subfield, '\n');
326 if ((e = index(subfield, '\t')) && e < ef)
330 wrbuf_putc(s->wrbuf, ' ');
331 wrbuf_write(s->wrbuf, subfield, ef - subfield);
335 if ((field = find_field(rec, "100")))
337 if ((subfield = find_subfield(field, 'a')))
339 ef = index(subfield, '\n');
340 if ((e = index(subfield, '\t')) && e < ef)
344 wrbuf_puts(s->wrbuf, ", by ");
345 wrbuf_write(s->wrbuf, subfield, ef - subfield);
349 wrbuf_putc(s->wrbuf, '\0');
350 obuf = (unsigned char*) nmem_strdup(s->nmem, wrbuf_buf(s->wrbuf));
351 for (p = obuf; *p; p++)
352 if (*p == '&' || *p == '<' || *p > 122 || *p < ' ')
357 // Extract 245 $a $b 100 $a
358 char *extract_mergekey(struct session *s, const char *rec)
360 const char *field, *subfield;
362 char *out, *p, *pout;
364 wrbuf_rewind(s->wrbuf);
366 if (!(field = find_field(rec, "245")))
368 if (!(subfield = find_subfield(field, 'a')))
370 ef = index(subfield, '\n');
371 if ((e = index(subfield, '\t')) && e < ef)
375 wrbuf_write(s->wrbuf, subfield, ef - subfield);
376 if ((subfield = find_subfield(field, 'b')))
378 ef = index(subfield, '\n');
379 if ((e = index(subfield, '\t')) && e < ef)
383 wrbuf_puts(s->wrbuf, " field ");
384 wrbuf_write(s->wrbuf, subfield, ef - subfield);
388 if ((field = find_field(rec, "100")))
390 if ((subfield = find_subfield(field, 'a')))
392 ef = index(subfield, '\n');
393 if ((e = index(subfield, '\t')) && e < ef)
397 wrbuf_puts(s->wrbuf, " field ");
398 wrbuf_write(s->wrbuf, subfield, ef - subfield);
402 wrbuf_putc(s->wrbuf, '\0');
403 p = wrbuf_buf(s->wrbuf);
404 out = pout = nmem_malloc(s->nmem, strlen(p) + 1);
409 *(pout++) = tolower(*(p++));
410 while (*p && !isalnum(*p))
421 static void push_record(struct session *s, struct record *r)
424 assert(s->recheap_max + 1 < s->recheap_size);
426 s->recheap[p = ++s->recheap_max] = r;
429 int parent = (p - 1) >> 1;
430 if (strcmp(s->recheap[p]->merge_key, s->recheap[parent]->merge_key) < 0)
433 tmp = s->recheap[parent];
434 s->recheap[parent] = s->recheap[p];
443 static struct record *top_record(struct session *s)
445 return s-> recheap_max >= 0 ? s->recheap[0] : 0;
448 static struct record *pop_record(struct session *s)
452 int lastnonleaf = (s->recheap_max - 1) >> 1;
454 if (s->recheap_max < 0)
459 s->recheap[p] = s->recheap[s->recheap_max--];
461 while (p <= lastnonleaf)
463 int right = (p + 1) << 1;
464 int left = right - 1;
467 if (right < s->recheap_max &&
468 strcmp(s->recheap[right]->merge_key, s->recheap[left]->merge_key) < 0)
470 if (strcmp(s->recheap[min]->merge_key, s->recheap[p]->merge_key) < 0)
472 struct record *tmp = s->recheap[min];
473 s->recheap[min] = s->recheap[p];
483 // Like pop_record but collapses identical (merge_key) records
484 // The heap will contain multiple independent matching records and possibly
485 // one cluster, created the last time the list was scanned
486 static struct record *pop_mrecord(struct session *s)
491 if (!(this = pop_record(s)))
494 // Collapse identical records
495 while ((next = top_record(s)))
497 struct record *p, *tmpnext;
498 if (strcmp(this->merge_key, next->merge_key))
500 // Absorb record (and clustersiblings) into a supercluster
501 for (p = next; p; p = tmpnext) {
502 tmpnext = p->next_cluster;
503 p->next_cluster = this->next_cluster;
504 this->next_cluster = p;
512 // Reads records in sort order. Store records in top of heapspace until rewind is called.
513 static struct record *read_recheap(struct session *s)
515 struct record *r = pop_mrecord(s);
519 if (s->recheap_scratch < 0)
520 s->recheap_scratch = s->recheap_size;
521 s->recheap[--s->recheap_scratch] = r;
527 // Return records to heap after read
528 static void rewind_recheap(struct session *s)
530 while (s->recheap_scratch >= 0) {
531 push_record(s, s->recheap[s->recheap_scratch++]);
532 if (s->recheap_scratch >= s->recheap_size)
533 s->recheap_scratch = -1;
539 // FIXME needs to be generalized. Should flexibly generate X lists per search
540 static void extract_subject(struct session *s, const char *rec)
542 const char *field, *subfield;
544 while ((field = find_field(rec, "650")))
547 if ((subfield = find_subfield(field, 'a')))
553 ef = index(subfield, '\n');
556 if ((e = index(subfield, '\t')) && e < ef)
558 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
562 memcpy(buf, subfield, len);
565 termlist_insert(s->termlist, buf);
570 static void pull_relevance_field(struct session *s, struct record *head, const char *rec,
571 char *field, int mult)
574 while ((fb = find_field(rec, field)))
576 char *ffield = strchr(fb, '\t');
579 char *eol = strchr(ffield, '\n');
582 relevance_countwords(s->relevance, head, ffield, eol - ffield, mult);
583 rec = field + 1; // Crude way to cause a loop through repeating fields
587 static void pull_relevance_keys(struct session *s, struct record *head, struct record *rec)
589 relevance_newrec(s->relevance, head);
590 pull_relevance_field(s, head, rec->buf, "100", 2);
591 pull_relevance_field(s, head, rec->buf, "245", 4);
592 //pull_relevance_field(s, head, rec->buf, "530", 1);
593 pull_relevance_field(s, head, rec->buf, "630", 1);
594 pull_relevance_field(s, head, rec->buf, "650", 1);
595 pull_relevance_field(s, head, rec->buf, "700", 1);
596 relevance_donerecord(s->relevance, head);
599 static struct record *ingest_record(struct client *cl, char *buf, int len)
601 struct session *se = cl->session;
606 wrbuf_rewind(se->wrbuf);
607 yaz_marc_xml(global_parameters.yaz_marc, YAZ_MARC_LINE);
608 if (yaz_marc_decode_wrbuf(global_parameters.yaz_marc, buf, len, se->wrbuf) < 0)
610 yaz_log(YLOG_WARN, "Failed to decode MARC record");
613 wrbuf_putc(se->wrbuf, '\0');
614 recbuf = wrbuf_buf(se->wrbuf);
616 res = nmem_malloc(se->nmem, sizeof(struct record));
617 res->buf = nmem_strdup(se->nmem, recbuf);
619 extract_subject(se, res->buf);
621 res->title = extract_title(se, res->buf);
622 res->merge_key = extract_mergekey(se, res->buf);
626 res->next_cluster = 0;
627 res->target_offset = -1;
628 res->term_frequency_vec = 0;
630 head = reclist_insert(se->reclist, res);
632 pull_relevance_keys(se, head, res);
639 static void ingest_records(struct client *cl, Z_Records *r)
642 struct session *s = cl->session;
643 Z_NamePlusRecordList *rlist;
646 if (r->which != Z_Records_DBOSD)
648 rlist = r->u.databaseOrSurDiagnostics;
649 for (i = 0; i < rlist->num_records; i++)
651 Z_NamePlusRecord *npr = rlist->records[i];
656 if (npr->which != Z_NamePlusRecord_databaseRecord)
658 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
661 e = npr->u.databaseRecord;
662 if (e->which != Z_External_octet)
664 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
667 buf = (char*) e->u.octet_aligned->buf;
668 len = e->u.octet_aligned->len;
670 rec = ingest_record(cl, buf, len);
674 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
675 session_alert_watch(s, SESSION_WATCH_RECORDS);
678 static void do_presentResponse(IOCHAN i, Z_APDU *a)
680 struct connection *co = iochan_getdata(i);
681 struct client *cl = co->client;
682 Z_PresentResponse *r = a->u.presentResponse;
685 Z_Records *recs = r->records;
686 if (recs->which == Z_Records_NSD)
688 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
689 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
690 cl->state = Client_Error;
694 if (!*r->presentStatus && cl->state != Client_Error)
696 yaz_log(YLOG_DEBUG, "Good Present response");
697 cl->records += *r->numberOfRecordsReturned;
698 ingest_records(cl, r->records);
699 cl->state = Client_Idle;
701 else if (*r->presentStatus)
703 yaz_log(YLOG_WARN, "Bad Present response");
704 cl->state = Client_Error;
708 static void handler(IOCHAN i, int event)
710 struct connection *co = iochan_getdata(i);
711 struct client *cl = co->client;
712 struct session *se = 0;
718 yaz_log(YLOG_WARN, "Destroying orphan connection");
719 connection_destroy(co);
723 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
726 socklen_t errlen = sizeof(errcode);
728 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
729 &errlen) < 0 || errcode != 0)
736 yaz_log(YLOG_DEBUG, "Connect OK");
737 co->state = Conn_Open;
739 cl->state = Client_Connected;
743 else if (event & EVENT_INPUT)
745 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
749 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
750 connection_destroy(co);
755 yaz_log(YLOG_WARN, "EOF reading from Z server");
756 connection_destroy(co);
759 else if (len > 1) // We discard input if we have no connection
761 co->state = Conn_Open;
763 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
767 odr_reset(global_parameters.odr_in);
768 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
769 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
776 case Z_APDU_initResponse:
777 do_initResponse(i, a);
779 case Z_APDU_searchResponse:
780 do_searchResponse(i, a);
782 case Z_APDU_presentResponse:
783 do_presentResponse(i, a);
786 yaz_log(YLOG_WARN, "Unexpected result from server");
790 // We aren't expecting staggered output from target
791 // if (cs_more(t->link))
792 // iochan_setevent(i, EVENT_INPUT);
794 else // we throw away response and go to idle mode
796 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
797 cl->state = Client_Idle;
800 /* if len==1 we do nothing but wait for more input */
803 if (cl->state == Client_Connected) {
807 if (cl->state == Client_Idle)
809 if (cl->requestid != se->requestid && *se->query) {
812 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
813 cl->records < cl->hits) {
819 // Disassociate connection from client
820 static void connection_release(struct connection *co)
822 struct client *cl = co->client;
824 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
831 // Close connection and recycle structure
832 static void connection_destroy(struct connection *co)
834 struct host *h = co->host;
836 iochan_destroy(co->iochan);
838 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
839 if (h->connections == co)
840 h->connections = co->next;
843 struct connection *pco;
844 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
847 pco->next = co->next;
853 if (co->client->state != Client_Idle)
854 co->client->state = Client_Disconnected;
855 co->client->connection = 0;
857 co->next = connection_freelist;
858 connection_freelist = co;
861 // Creates a new connection for client, associated with the host of
863 static struct connection *connection_create(struct client *cl)
865 struct connection *new;
870 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
871 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
873 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
877 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
879 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
883 res = cs_connect(link, addr);
886 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
890 if ((new = connection_freelist))
891 connection_freelist = new->next;
894 new = xmalloc(sizeof (struct connection));
898 new->state = Conn_Connecting;
899 new->host = cl->database->host;
900 new->next = new->host->connections;
901 new->host->connections = new;
903 cl->connection = new;
906 new->iochan = iochan_create(cs_fileno(link), handler, 0);
907 iochan_setdata(new->iochan, new);
908 new->iochan->next = channel_list;
909 channel_list = new->iochan;
913 // Close connection and set state to error
914 static void client_fatal(struct client *cl)
916 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
917 connection_destroy(cl->connection);
918 cl->state = Client_Error;
921 // Ensure that client has a connection associated
922 static int client_prep_connection(struct client *cl)
924 struct connection *co;
925 struct session *se = cl->session;
926 struct host *host = cl->database->host;
930 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
934 // See if someone else has an idle connection
935 // We should look at timestamps here to select the longest-idle connection
936 for (co = host->connections; co; co = co->next)
937 if (co->state == Conn_Open && (!co->client || co->client->session != se))
941 connection_release(co);
946 co = connection_create(cl);
950 if (co->state == Conn_Connecting)
951 cl->state = Client_Connecting;
952 else if (co->state == Conn_Open)
954 if (cl->state == Client_Error || cl->state == Client_Disconnected)
955 cl->state = Client_Idle;
957 iochan_setflag(co->iochan, EVENT_OUTPUT);
964 void load_simpletargets(const char *fn)
966 FILE *f = fopen(fn, "r");
971 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
975 while (fgets(line, 255, f))
979 struct database *database;
981 if (strncmp(line, "target ", 7))
984 url[strlen(url) - 1] = '\0';
985 yaz_log(YLOG_DEBUG, "Target: %s", url);
986 if ((db = strchr(url, '/')))
991 for (host = hosts; host; host = host->next)
992 if (!strcmp(url, host->hostport))
996 struct addrinfo *addrinfo, hints;
999 unsigned char addrbuf[4];
1002 host = xmalloc(sizeof(struct host));
1003 host->hostport = xstrdup(url);
1004 host->connections = 0;
1006 if ((port = strchr(url, ':')))
1012 hints.ai_family = PF_INET;
1013 hints.ai_socktype = SOCK_STREAM;
1014 hints.ai_protocol = IPPROTO_TCP;
1015 hints.ai_addrlen = 0;
1017 hints.ai_canonname = 0;
1019 // This is not robust code. It assumes that getaddrinfo returns AF_INET
1021 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
1023 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
1024 xfree(host->hostport);
1028 assert(addrinfo->ai_family == PF_INET);
1029 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
1030 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
1031 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
1032 host->ipport = xstrdup(ipport);
1033 freeaddrinfo(addrinfo);
1037 database = xmalloc(sizeof(struct database));
1038 database->host = host;
1039 database->url = xmalloc(strlen(url) + strlen(db) + 2);
1040 strcpy(database->url, url);
1041 strcat(database->url, "/");
1042 strcat(database->url, db);
1044 database->databases = xmalloc(2 * sizeof(char *));
1045 database->databases[0] = xstrdup(db);
1046 database->databases[1] = 0;
1047 database->errors = 0;
1048 database->next = databases;
1049 databases = database;
1055 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
1063 pull_terms(nmem, n->u.p[0], termlist, num);
1064 pull_terms(nmem, n->u.p[1], termlist, num);
1067 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
1074 // Extract terms from query into null-terminated termlist
1075 static int extract_terms(NMEM nmem, char *query, char **termlist)
1078 struct ccl_rpn_node *n;
1081 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
1084 pull_terms(nmem, n, termlist, &num);
1090 static struct client *client_create(void)
1093 if (client_freelist)
1095 r = client_freelist;
1096 client_freelist = client_freelist->next;
1099 r = xmalloc(sizeof(struct client));
1108 r->state = Client_Disconnected;
1113 void client_destroy(struct client *c)
1115 struct session *se = c->session;
1116 if (c == se->clients)
1117 se->clients = c->next;
1121 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
1127 connection_release(c->connection);
1128 c->next = client_freelist;
1129 client_freelist = c;
1132 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
1134 s->watchlist[what].fun = fun;
1135 s->watchlist[what].data = data;
1138 void session_alert_watch(struct session *s, int what)
1140 if (!s->watchlist[what].fun)
1142 (*s->watchlist[what].fun)(s->watchlist[what].data);
1143 s->watchlist[what].fun = 0;
1144 s->watchlist[what].data = 0;
1147 // This should be extended with parameters to control selection criteria
1148 // Associates a set of clients with a session;
1149 int select_targets(struct session *se)
1151 struct database *db;
1155 client_destroy(se->clients);
1156 for (db = databases; db; db = db->next)
1158 struct client *cl = client_create();
1161 cl->next = se->clients;
1168 char *search(struct session *se, char *query)
1170 int live_channels = 0;
1173 yaz_log(YLOG_DEBUG, "Search");
1175 strcpy(se->query, query);
1177 nmem_reset(se->nmem);
1178 for (cl = se->clients; cl; cl = cl->next)
1184 if (client_prep_connection(cl))
1190 int maxrecs = live_channels * global_parameters.toget;
1191 se->termlist = termlist_create(se->nmem, maxrecs, 15);
1192 se->reclist = reclist_create(se->nmem, maxrecs);
1193 extract_terms(se->nmem, query, p);
1194 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1195 se->total_records = se->total_hits = 0;
1203 void destroy_session(struct session *s)
1205 yaz_log(YLOG_LOG, "Destroying session");
1207 client_destroy(s->clients);
1208 nmem_destroy(s->nmem);
1209 wrbuf_free(s->wrbuf, 1);
1212 struct session *new_session()
1215 struct session *session = xmalloc(sizeof(*session));
1217 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1219 session->total_hits = 0;
1220 session->total_records = 0;
1221 session->termlist = 0;
1222 session->reclist = 0;
1223 session->requestid = -1;
1224 session->clients = 0;
1225 session->query[0] = '\0';
1226 session->nmem = nmem_create();
1227 session->wrbuf = wrbuf_alloc();
1228 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1230 session->watchlist[i].data = 0;
1231 session->watchlist[i].fun = 0;
1234 select_targets(session);
1239 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1241 static struct hitsbytarget res[1000]; // FIXME MM
1245 for (cl = se->clients; cl; cl = cl->next)
1247 strcpy(res[*count].id, cl->database->host->hostport);
1248 res[*count].hits = cl->hits;
1249 res[*count].records = cl->records;
1250 res[*count].diagnostic = cl->diagnostic;
1251 res[*count].state = client_states[cl->state];
1252 res[*count].connected = cl->connection ? 1 : 0;
1259 struct termlist_score **termlist(struct session *s, int *num)
1261 return termlist_highscore(s->termlist, num);
1264 void report_nmem_stats(void)
1266 size_t in_use, is_free;
1268 nmem_get_memory_in_use(&in_use);
1269 nmem_get_memory_free(&is_free);
1271 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1272 (long) in_use, (long) is_free);
1275 struct record **show(struct session *s, int start, int *num, int *total,
1276 int *sumhits, NMEM nmem_show)
1278 struct record **recs = nmem_malloc(nmem_show, *num
1279 * sizeof(struct record *));
1282 relevance_prepare_read(s->relevance, s->reclist);
1284 *total = s->reclist->num_records;
1285 *sumhits = s->total_hits;
1287 for (i = 0; i < start; i++)
1288 if (!reclist_read_record(s->reclist))
1294 for (i = 0; i < *num; i++)
1296 struct record *r = reclist_read_record(s->reclist);
1307 void statistics(struct session *se, struct statistics *stat)
1312 bzero(stat, sizeof(*stat));
1313 for (cl = se->clients; cl; cl = cl->next)
1315 if (!cl->connection)
1316 stat->num_no_connection++;
1319 case Client_Connecting: stat->num_connecting++; break;
1320 case Client_Initializing: stat->num_initializing++; break;
1321 case Client_Searching: stat->num_searching++; break;
1322 case Client_Presenting: stat->num_presenting++; break;
1323 case Client_Idle: stat->num_idle++; break;
1324 case Client_Failed: stat->num_failed++; break;
1325 case Client_Error: stat->num_error++; break;
1330 stat->num_hits = se->total_hits;
1331 stat->num_records = se->total_records;
1333 stat->num_clients = count;
1336 static CCL_bibset load_cclfile(const char *fn)
1338 CCL_bibset res = ccl_qual_mk();
1339 if (ccl_qual_fname(res, fn) < 0)
1341 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1347 int main(int argc, char **argv)
1353 if (signal(SIGPIPE, SIG_IGN) < 0)
1354 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1356 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1358 while ((ret = options("c:h:p:C:s:", argv, argc, &arg)) != -2)
1362 command_init(atoi(arg));
1370 global_parameters.ccl_filter = load_cclfile(arg);
1373 http_set_proxyaddr(arg);
1376 load_simpletargets(arg);
1379 fprintf(stderr, "Usage: pazpar2\n"
1380 " -h [host:]port (REST protocol listener)\n"
1381 " -c cmdport (telnet-style)\n"
1383 " -s simpletargetfile\n"
1384 " -p hostname[:portno] (HTTP proxy)\n");
1391 fprintf(stderr, "Set command port with -h or -c\n");
1395 global_parameters.ccl_filter = load_cclfile("default.bib");
1396 global_parameters.yaz_marc = yaz_marc_create();
1397 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1398 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1399 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1401 event_loop(&channel_list);
1409 * indent-tabs-mode: nil
1411 * vim: shiftwidth=4 tabstop=8 expandtab