/*
- * $Id: http_command.c,v 1.2 2006-11-24 20:29:07 quinn Exp $
+ * stat->num_hits = s->total_hits;
+ * stat->num_records = s->total_records;
+ * $Id: http_command.c,v 1.3 2006-11-27 14:35:15 quinn Exp $
*/
#include <stdio.h>
rs->payload = nmem_strdup(c->nmem, tmp);
}
-int make_sessionid()
+int make_sessionid()
{
struct timeval t;
int res;
return 0;
}
+static void cmd_exit(struct http_request *rq, struct http_response *rs)
+{
+ yaz_log(YLOG_WARN, "exit");
+ exit(0);
+}
static void cmd_init(struct http_request *rq, struct http_response *rs)
{
struct record *p;
wrbuf_puts(c->wrbuf, "<hit>\n");
- wrbuf_printf(c->wrbuf, "<merge_key>%s</merge_key>\n", rl[i]->merge_key);
+ wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
for (ccount = 1, p = rl[i]->next_cluster; p; p = p->next_cluster, ccount++)
;
if (ccount > 1)
{
struct http_session *s = locate_session(rq, rs);
char *query = http_argbyname(rq, "query");
+ char *res;
if (!s)
return;
error(rs, "417", "Must supply query", 0);
return;
}
- search(s->psession, query);
+ res = search(s->psession, query);
+ if (res)
+ {
+ error(rs, "417", res, res);
+ return;
+ }
rs->payload = "<search><status>OK</status></search>";
}
static void cmd_stat(struct http_request *rq, struct http_response *rs)
{
+ struct http_session *s = locate_session(rq, rs);
+ struct http_channel *c = rq->channel;
+ struct statistics stat;
+
+ if (!s)
+ return;
+
+ statistics(s->psession, &stat);
+
+ wrbuf_rewind(c->wrbuf);
+ wrbuf_puts(c->wrbuf, "<stat>");
+ wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
+ wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
+ wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
+ wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
+ wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
+ wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
+ wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
+ wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
+ wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
+ wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
+ wrbuf_puts(c->wrbuf, "</stat>");
+ rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
}
static void cmd_load(struct http_request *rq, struct http_response *rs)
{ "show", cmd_show },
{ "search", cmd_search },
{ "termlist", cmd_termlist },
+ { "exit", cmd_exit },
{0,0}
};
-/* $Id: pazpar2.c,v 1.5 2006-11-26 05:15:43 quinn Exp $ */
+/* $Id: pazpar2.c,v 1.6 2006-11-27 14:35:15 quinn Exp $ */;
#include <stdlib.h>
#include <stdio.h>
#include <yaz/pquery.h>
#include <yaz/yaz-util.h>
#include <yaz/ccl.h>
+#include <yaz/yaz-ccl.h>
#include "pazpar2.h"
#include "eventl.h"
#define MAX_DATABASES 512
#define MAX_CHUNK 10
+static void target_destroy(IOCHAN i);
+
struct target
{
struct session *session;
int setno;
int requestid; // ID of current outstanding request
int diagnostic;
+ IOCHAN iochan;
enum target_state
{
No_connection,
Presenting,
Error,
Idle,
+ Stopped,
Failed
} state;
};
t->state = Initializing;
}
else
- {
- iochan_destroy(i);
- t->state = Failed;
- cs_close(t->link);
- }
+ target_destroy(i);
}
static void send_search(IOCHAN i)
struct target *t = iochan_getdata(i);
struct session *s = t->session;
Z_APDU *a = zget_APDU(t->odr_out, Z_APDU_searchRequest);
- int ndb;
+ int ndb, cerror, cpos;
char **databaselist;
Z_Query *zquery;
+ struct ccl_rpn_node *cn;
yaz_log(YLOG_DEBUG, "Sending search");
+
+ cn = ccl_find_str(global_parameters.ccl_filter, s->query, &cerror, &cpos);
+ if (!cn)
+ return;
a->u.searchRequest->query = zquery = odr_malloc(t->odr_out, sizeof(Z_Query));
zquery->which = Z_Query_type_1;
- zquery->u.type_1 = p_query_rpn(t->odr_out, PROTO_Z3950, s->query);
+ zquery->u.type_1 = ccl_rpn_query(t->odr_out, cn);
+ ccl_rpn_delete(cn);
for (ndb = 0; *t->databases[ndb]; ndb++)
;
}
else
{
- iochan_destroy(i);
- t->state = Failed;
- cs_close(t->link);
+ target_destroy(i);
+ return;
}
odr_reset(t->odr_out);
}
a->u.presentRequest->resultSetId = "Default";
+ a->u.presentRequest->preferredRecordSyntax = yaz_oidval_to_z3950oid(t->odr_out,
+ CLASS_RECSYN, VAL_USMARC);
+
if (send_apdu(t, a) >= 0)
{
iochan_setflags(i, EVENT_INPUT);
}
else
{
- iochan_destroy(i);
- t->state = Failed;
- cs_close(t->link);
+ target_destroy(i);
+ return;
}
odr_reset(t->odr_out);
}
t->state = Idle;
}
else
- {
- t->state = Failed;
- iochan_destroy(i);
- cs_close(t->link);
- }
+ target_destroy(i);
}
static void do_searchResponse(IOCHAN i, Z_APDU *a)
{
t->hits = *r->resultCount;
t->state = Idle;
+ t->session->total_hits += t->hits;
}
else
{ /*"FAILED"*/
while (*line)
{
+ const char *eol;
+
if (!strncmp(line, field, 3) && line[3] == ' ')
return line;
- while (*(line++) != '\n')
- ;
+ while (*line && *line != '\n')
+ line++;
+ if (!(eol = strchr(line, '\n')))
+ return 0;
+ line = eol + 1;
}
return 0;
}
}
// Extract 245 $a $b 100 $a
+char *extract_title(struct session *s, const char *rec)
+{
+ const char *field, *subfield;
+ char *e, *ef;
+ unsigned char *obuf, *p;
+
+ wrbuf_rewind(s->wrbuf);
+
+ if (!(field = find_field(rec, "245")))
+ return 0;
+ if (!(subfield = find_subfield(field, 'a')))
+ return 0;
+ ef = index(subfield, '\n');
+ if ((e = index(subfield, '\t')) && e < ef)
+ ef = e;
+ if (ef)
+ {
+ wrbuf_write(s->wrbuf, subfield, ef - subfield);
+ if ((subfield = find_subfield(field, 'b')))
+ {
+ ef = index(subfield, '\n');
+ if ((e = index(subfield, '\t')) && e < ef)
+ ef = e;
+ if (ef)
+ {
+ wrbuf_putc(s->wrbuf, ' ');
+ wrbuf_write(s->wrbuf, subfield, ef - subfield);
+ }
+ }
+ }
+ if ((field = find_field(rec, "100")))
+ {
+ if ((subfield = find_subfield(field, 'a')))
+ {
+ ef = index(subfield, '\n');
+ if ((e = index(subfield, '\t')) && e < ef)
+ ef = e;
+ if (ef)
+ {
+ wrbuf_puts(s->wrbuf, ", by ");
+ wrbuf_write(s->wrbuf, subfield, ef - subfield);
+ }
+ }
+ }
+ wrbuf_putc(s->wrbuf, '\0');
+ obuf = nmem_strdup(s->nmem, wrbuf_buf(s->wrbuf));
+ for (p = obuf; *p; p++)
+ if (*p == '&' || *p == '<' || *p > 122 || *p < ' ')
+ *p = ' ';
+ return obuf;
+}
+
+// Extract 245 $a $b 100 $a
char *extract_mergekey(struct session *s, const char *rec)
{
const char *field, *subfield;
int len;
ef = index(subfield, '\n');
+ if (!ef)
+ return;
if ((e = index(subfield, '\t')) && e < ef)
ef = e;
while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
}
}
+static void pull_relevance_field(struct session *s, struct record *head, const char *rec,
+ char *field, int mult)
+{
+ const char *fb;
+ while ((fb = find_field(rec, field)))
+ {
+ char *ffield = strchr(fb, '\t');
+ if (!ffield)
+ return;
+ char *eol = strchr(ffield, '\n');
+ if (!eol)
+ return;
+ relevance_countwords(s->relevance, head, ffield, eol - ffield, mult);
+ rec = field + 1; // Crude way to cause a loop through repeating fields
+ }
+}
+
static void pull_relevance_keys(struct session *s, struct record *head, struct record *rec)
{
relevance_newrec(s->relevance, head);
- relevance_countwords(s->relevance, head, rec->merge_key, strlen(rec->merge_key));
+ pull_relevance_field(s, head, rec->buf, "100", 2);
+ pull_relevance_field(s, head, rec->buf, "245", 4);
+ //pull_relevance_field(s, head, rec->buf, "530", 1);
+ pull_relevance_field(s, head, rec->buf, "630", 1);
+ pull_relevance_field(s, head, rec->buf, "650", 1);
+ pull_relevance_field(s, head, rec->buf, "700", 1);
relevance_donerecord(s->relevance, head);
}
recbuf = wrbuf_buf(s->wrbuf);
res = nmem_malloc(s->nmem, sizeof(struct record));
+ res->buf = nmem_strdup(s->nmem, recbuf);
- extract_subject(s, recbuf);
+ extract_subject(s, res->buf);
- res->merge_key = extract_mergekey(s, recbuf);
+ res->title = extract_title(s, res->buf);
+ res->merge_key = extract_mergekey(s, res->buf);
if (!res->merge_key)
return 0;
- res->buf = nmem_strdupn(s->nmem, recbuf, wrbuf_len(s->wrbuf));
res->target = t;
res->next_cluster = 0;
res->target_offset = -1;
pull_relevance_keys(s, head, res);
+ s->total_records++;
+
return res;
}
else
{
yaz_log(YLOG_WARN|YLOG_ERRNO, "ERROR %s connect\n", t->hostport);
- cs_close(t->link);
- t->state = Failed;
- iochan_destroy(i);
+ target_destroy(i);
+ return;
}
}
if (getsockopt(cs_fileno(t->link), SOL_SOCKET, SO_ERROR, &errcode,
&errlen) < 0 || errcode != 0)
{
- cs_close(t->link);
- iochan_destroy(i);
- t->state = Failed;
+ target_destroy(i);
return;
}
else
if (len < 0)
{
- cs_close(t->link);
- iochan_destroy(i);
- t->state = Failed;
+ target_destroy(i);
return;
}
if (len == 0)
{
- cs_close(t->link);
- iochan_destroy(i);
- t->state = Failed;
+ target_destroy(i);
return;
}
else if (len > 1)
odr_setbuf(t->odr_in, t->ibuf, len, 0);
if (!z_APDU(t->odr_in, &a, 0, 0))
{
- cs_close(t->link);
- iochan_destroy(i);
- t->state = Failed;
+ target_destroy(i);
return;
}
switch (a->which)
break;
default:
yaz_log(YLOG_WARN, "Unexpected result from server");
- cs_close(t->link);
- iochan_destroy(i);
- t->state = Failed;
+ target_destroy(i);
return;
}
// if (cs_more(t->link))
// iochan_setevent(i, EVENT_INPUT);
}
else // we throw away response and go to idle mode
+ {
+ yaz_log(YLOG_DEBUG, "Ignoring result to previous operation");
t->state = Idle;
+ }
}
/* if len==1 we do nothing but wait for more input */
}
if (t->state == Idle)
{
- if (t->requestid != s->requestid) {
+ if (t->requestid != s->requestid && *s->query) {
send_search(i);
}
else if (t->hits > 0 && t->records < global_parameters.toget &&
}
}
+static void target_destroy(IOCHAN i)
+{
+ struct target *t = iochan_getdata(i);
+ struct session *s = t->session;
+ struct target **p;
+ assert(iochan_getfun(i) == handler);
+
+ yaz_log(YLOG_DEBUG, "Destroying target");
+
+ if (t->ibuf)
+ xfree(t->ibuf);
+ cs_close(t->link);
+ if (t->odr_in)
+ odr_destroy(t->odr_in);
+ if (t->odr_out)
+ odr_destroy(t->odr_out);
+ for (p = &s->targets; *p; p = &(*p)->next)
+ if (*p == t)
+ {
+ *p = (*p)->next;
+ break;
+ }
+ xfree(t);
+ iochan_destroy(i);
+}
+
int load_targets(struct session *s, const char *fn)
{
FILE *f = fopen(fn, "r");
return -1;
}
+ while (s->targets)
+ target_destroy(s->targets->iochan);
+
+ s->query[0] = '\0';
target_p = &s->targets;
while (fgets(line, 255, f))
{
target->state = Failed;
continue;
}
- new = iochan_create(cs_fileno(target->link), handler, 0);
+ target->iochan = new = iochan_create(cs_fileno(target->link), handler, 0);
+ assert(new);
iochan_setdata(new, target);
iochan_setevent(new, EVENT_EXCEPT);
new->next = channel_list;
return 0;
}
-void search(struct session *s, char *query)
+static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
+{
+ switch (n->kind)
+ {
+ case CCL_RPN_AND:
+ case CCL_RPN_OR:
+ case CCL_RPN_NOT:
+ case CCL_RPN_PROX:
+ pull_terms(nmem, n->u.p[0], termlist, num);
+ pull_terms(nmem, n->u.p[1], termlist, num);
+ break;
+ case CCL_RPN_TERM:
+ termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
+ break;
+ default: // NOOP
+ break;
+ }
+}
+
+// Extract terms from query into null-terminated termlist
+static int extract_terms(NMEM nmem, char *query, char **termlist)
+{
+ int error, pos;
+ struct ccl_rpn_node *n;
+ int num = 0;
+
+ n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
+ if (!n)
+ return -1;
+ pull_terms(nmem, n, termlist, &num);
+ termlist[num] = 0;
+ ccl_rpn_delete(n);
+ return 0;
+}
+
+char *search(struct session *s, char *query)
{
IOCHAN c;
int live_channels = 0;
}
if (live_channels)
{
- const char *p[] = { query, 0 };
+ char *p[512];
int maxrecs = live_channels * global_parameters.toget;
s->termlist = termlist_create(s->nmem, maxrecs, 15);
s->reclist = reclist_create(s->nmem, maxrecs);
- s->relevance = relevance_create(s->nmem, p, maxrecs);
+ extract_terms(s->nmem, query, p);
+ s->relevance = relevance_create(s->nmem, (const char **) p, maxrecs);
+ s->total_records = s->total_hits = 0;
}
+ else
+ return "NOTARGETS";
+
+ return 0;
}
struct session *new_session()
yaz_log(YLOG_DEBUG, "New pazpar2 session");
+ session->total_hits = 0;
+ session->total_records = 0;
session->termlist = 0;
session->reclist = 0;
session->requestid = -1;
break;
}
recs[i] = r;
- yaz_log(YLOG_DEBUG, "%d: %s%s", r->relevance, r->merge_key, r->next_cluster ? " (cluster)": "");
}
return recs;
}
default: break;
}
}
+ stat->num_hits = s->total_hits;
+ stat->num_records = s->total_records;
stat->num_connections = i;
}
}
if (!global_parameters.ccl_filter)
- load_cclfile("default.bib");
+ global_parameters.ccl_filter = load_cclfile("default.bib");
event_loop(&channel_list);