/* This file is part of Pazpar2.
- Copyright (C) 2006-2009 Index Data
+ Copyright (C) 2006-2010 Index Data
Pazpar2 is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
#include <yaz/timing.h>
#endif
-#include "pazpar2.h"
-
+#include "session.h"
+#include "parameters.h"
#include "client.h"
#include "connection.h"
#include "settings.h"
+#include "relevance.h"
/** \brief Represents client state for a connection to one search target */
struct client {
struct session *session;
char *pquery; // Current search
char *cqlquery; // used for SRU targets only
- int hits;
+ Odr_int hits;
int record_offset;
+ int maxrecs;
+ int startrecs;
int diagnostic;
enum client_state state;
struct show_raw *show_raw;
"Client_Disconnected"
};
-static struct client *client_freelist = 0;
-
const char *client_get_state_str(struct client *cl)
{
return client_states[cl->state];
return 0;
}
+static void client_show_raw_delete(struct show_raw *r)
+{
+ xfree(r->syntax);
+ xfree(r->esn);
+ xfree(r);
+}
+
void client_show_raw_remove(struct client *cl, void *data)
{
struct show_raw *rr = data;
if (*rrp)
{
*rrp = rr->next;
- xfree(rr);
+ client_show_raw_delete(rr);
}
}
struct show_raw *rr = cl->show_raw;
cl->show_raw = rr->next;
- xfree(rr);
+ client_show_raw_delete(rr);
}
static void client_show_raw_error(struct client *cl, const char *addinfo)
}
else
{
- cl->record_offset = 0;
+ cl->record_offset = cl->startrecs;
cl->hits = ZOOM_resultset_size(resultset);
se->total_hits += cl->hits;
}
else
{
struct session_database *sdb = client_get_database(cl);
+ NMEM nmem = nmem_create();
const char *xmlrec;
char type[80];
if (nativesyntax_to_type(sdb, type, rec))
yaz_log(YLOG_WARN, "Failed to determine record type");
if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
{
- if (ingest_record(cl, xmlrec, cl->record_offset))
+ if (!ingest_record(cl, xmlrec, cl->record_offset, nmem))
{
session_alert_watch(cl->session, SESSION_WATCH_SHOW);
session_alert_watch(cl->session, SESSION_WATCH_RECORD);
}
else
yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
+ nmem_destroy(nmem);
}
}
const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
+ const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
+ char maxrecs_str[24], startrecs_str[24];
assert(link);
ZOOM_connection_option_set(link, "elementSetName", opt_elements);
if (*opt_requestsyn)
ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
- if (*opt_maxrecs)
- ZOOM_connection_option_set(link, "count", opt_maxrecs);
- else
+
+ if (!*opt_maxrecs)
{
- char n[128];
- sprintf(n, "%d", global_parameters.toget);
- ZOOM_connection_option_set(link, "count", n);
+ sprintf(maxrecs_str, "%d", cl->maxrecs);
+ opt_maxrecs = maxrecs_str;
}
+ ZOOM_connection_option_set(link, "count", opt_maxrecs);
+
+
+ if (atoi(opt_maxrecs) > 20)
+ ZOOM_connection_option_set(link, "presentChunk", "20");
+ else
+ ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
+
+ sprintf(startrecs_str, "%d", cl->startrecs);
+ ZOOM_connection_option_set(link, "start", startrecs_str);
+
if (databaseName)
ZOOM_connection_option_set(link, "databaseName", databaseName);
- ZOOM_connection_option_set(link, "presentChunk", "20");
-
if (cl->cqlquery)
{
ZOOM_query q = ZOOM_query_create();
yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
ZOOM_query_cql(q, cl->cqlquery);
+ if (*opt_sort)
+ ZOOM_query_sortby(q, opt_sort);
rs = ZOOM_connection_search(link, q);
ZOOM_query_destroy(q);
}
struct client *client_create(void)
{
- struct client *r;
- if (client_freelist)
- {
- r = client_freelist;
- client_freelist = client_freelist->next;
- }
- else
- r = xmalloc(sizeof(struct client));
+ struct client *r = xmalloc(sizeof(*r));
+ r->maxrecs = 100;
+ r->startrecs = 0;
r->pquery = 0;
r->cqlquery = 0;
r->database = 0;
connection_release(c->connection);
ZOOM_resultset_destroy(c->resultset);
- c->resultset = 0;
- c->next = client_freelist;
- client_freelist = c;
+ xfree(c);
}
void client_set_connection(struct client *cl, struct connection *con)
char *r;
WRBUF wrb = wrbuf_alloc();
int status;
+ ODR odr_out = odr_createmem(ODR_ENCODE);
- zquery = p_query_rpn(global_parameters.odr_out, cl->pquery);
+ zquery = p_query_rpn(odr_out, cl->pquery);
+ yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
{
- yaz_log(YLOG_WARN, "failed to generate CQL query, code=%d", status);
- return 0;
+ yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
+ r = 0;
}
- r = xstrdup(wrbuf_cstr(wrb));
-
+ else
+ {
+ r = xstrdup(wrbuf_cstr(wrb));
+ }
wrbuf_destroy(wrb);
- odr_reset(global_parameters.odr_out); // releases the zquery
+ odr_destroy(odr_out);
cql_transform_close(cqlt);
return r;
}
CCL_bibset ccl_map = prepare_cclmap(cl);
const char *sru = session_setting_oneval(sdb, PZ_SRU);
const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
+ const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
if (!ccl_map)
return -1;
if (!cn)
{
client_set_state(cl, Client_Error);
- yaz_log(YLOG_WARN, "Failed to parse query for %s",
- client_get_database(cl)->database->url);
+ yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
+ query,
+ client_get_database(cl)->database->url);
return -1;
}
wrbuf_rewind(se->wrbuf);
wrbuf_puts(se->wrbuf, pqf_prefix);
wrbuf_puts(se->wrbuf, " ");
}
- ccl_pquery(se->wrbuf, cn);
+ if (!pqf_strftime || !*pqf_strftime)
+ ccl_pquery(se->wrbuf, cn);
+ else
+ {
+ time_t cur_time = time(0);
+ struct tm *tm = localtime(&cur_time);
+ char tmp_str[300];
+ const char *cp = tmp_str;
+
+ /* see man strftime(3) for things .. In particular %% gets converted
+ to %.. And That's our original query .. */
+ strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
+ for (; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ ccl_pquery(se->wrbuf, cn);
+ else
+ wrbuf_putc(se->wrbuf, cp[0]);
+ }
+ }
xfree(cl->pquery);
cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
char *p[512];
extract_terms(se->nmem, cn, p);
se->relevance = relevance_create(
- global_parameters.server->relevance_pct,
- se->nmem, (const char **) p,
- se->expected_maxrecs);
+ se->service->relevance_pct,
+ se->nmem, (const char **) p);
}
ccl_rpn_delete(cn);
}
-int client_get_hits(struct client *cl)
+Odr_int client_get_hits(struct client *cl)
{
return cl->hits;
}
return cl->record_offset;
}
+void client_set_diagnostic(struct client *cl, int diagnostic)
+{
+ cl->diagnostic = diagnostic;
+}
+
int client_get_diagnostic(struct client *cl)
{
return cl->diagnostic;
return client_get_database(cl)->database->url;
}
+void client_set_maxrecs(struct client *cl, int v)
+{
+ cl->maxrecs = v;
+}
+
+void client_set_startrecs(struct client *cl, int v)
+{
+ cl->startrecs = v;
+}
+
/*
* Local variables:
* c-basic-offset: 4