/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 1995-2010 Index Data
* See the file LICENSE for details.
*/
Only terms that looks a numeric is used.. Returns -1 if
no sub tree has a hit count term
*/
-static int get_term_hit(Z_RPNStructure *s)
+static Odr_int get_term_hit(Z_RPNStructure *s)
{
- int h = -1;
+ Odr_int h = -1;
switch(s->which)
{
case Z_RPNStructure_simple:
{
Odr_oct *oct = apt->term->u.general;
if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
- h = atoi_n((const char *) oct->buf, oct->len);
+ {
+ WRBUF hits_str = wrbuf_alloc();
+ wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
+ h = odr_atoi(wrbuf_cstr(hits_str));
+ wrbuf_destroy(hits_str);
+ }
}
}
break;
have a way to trigger a certain hit count. Good for testing of
client applications etc
*/
-static int get_hit_count(Z_Query *q)
+static Odr_int get_hit_count(Z_Query *q)
{
- int h = -1;
+ Odr_int h = -1;
if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
h = get_term_hit(q->u.type_1->RPNStructure);
if (h == -1)
return h;
}
-int ztest_search(void *handle, bend_search_rr *rr)
+/** \brief checks if it's a dummy Slow database
+ \param basename database name to check
+ \param association backend association (or NULL if not available)
+ \retval 1 is slow database
+ \retval 0 is not a slow database
+
+ The Slow database is for testing.. It allows us to simulate
+ a slow server...
+*/
+static int check_slow(const char *basename, bend_association association)
{
- if (rr->num_bases != 1)
- {
- rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
- return 0;
- }
- /* Throw Database unavailable if other than Default or Slow */
- if (!yaz_matchstr(rr->basenames[0], "Default"))
- ; /* Default is OK in our test */
- else if(!yaz_matchstr(rr->basenames[0], "Slow"))
+ if (strncmp(basename, "Slow", 4) == 0)
{
#if HAVE_UNISTD_H
+ int i, w = 3;
+ if (basename[4])
+ sscanf(basename+4, "%d", &w);
/* wait up to 3 seconds and check if connection is still alive */
- int i;
- for (i = 0; i<20; i++)
+ for (i = 0; i < w; i++)
{
- if (!bend_assoc_is_alive(rr->association))
+ if (association && !bend_assoc_is_alive(association))
{
yaz_log(YLOG_LOG, "search aborted");
break;
sleep(1);
}
#endif
+ return 1;
+ }
+ return 0;
+}
+
+int ztest_search(void *handle, bend_search_rr *rr)
+{
+ if (rr->num_bases != 1)
+ {
+ rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
+ return 0;
+ }
+ /* Throw Database unavailable if other than Default or Slow */
+ if (!yaz_matchstr(rr->basenames[0], "Default"))
+ ; /* Default is OK in our test */
+ else if (check_slow(rr->basenames[0], rr->association))
+ {
rr->estimated_hit_count = 1;
}
else
return 0;
}
+ if (rr->extra_args)
+ {
+ Z_SRW_extra_arg *a;
+ WRBUF response_xml = wrbuf_alloc();
+ wrbuf_puts(response_xml, "<extra>");
+ for (a = rr->extra_args; a; a = a->next)
+ {
+ wrbuf_puts(response_xml, "<extra name=\"");
+ wrbuf_xmlputs(response_xml, a->name);
+ wrbuf_puts(response_xml, "\"");
+ if (a->value)
+ {
+ wrbuf_puts(response_xml, " value=\"");
+ wrbuf_xmlputs(response_xml, a->value);
+ wrbuf_puts(response_xml, "\"");
+ }
+ wrbuf_puts(response_xml, "/>");
+ }
+ wrbuf_puts(response_xml, "</extra>");
+ rr->extra_response_data =
+ odr_strdup(rr->stream, wrbuf_cstr(response_xml));
+ wrbuf_destroy(response_xml);
+ }
rr->hits = get_hit_count(rr->query);
return 0;
}
/* Throw Database unavailable if other than Default or Slow */
if (!yaz_matchstr(q->basenames[0], "Default"))
; /* Default is OK in our test */
- else if(!yaz_matchstr(q->basenames[0], "Slow"))
- {
-#if HAVE_UNISTD_H
- sleep(3);
-#endif
+ else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
;
- }
else
{
q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
return 0;
}
len = q->term->term->u.general->len;
- if (len >= sizeof(term))
+ if (len >= (int ) sizeof(term))
len = sizeof(term)-1;
memcpy(term, q->term->term->u.general->buf, len);
term[len] = '\0';