Logging channels and mutexes + a few other things
[pazpar2-moved-to-github.git] / src / client.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file client.c
21     \brief Z39.50 client 
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <signal.h>
38 #include <assert.h>
39
40 #include <yaz/marcdisp.h>
41 #include <yaz/comstack.h>
42 #include <yaz/tcpip.h>
43 #include <yaz/proto.h>
44 #include <yaz/readconf.h>
45 #include <yaz/pquery.h>
46 #include <yaz/otherinfo.h>
47 #include <yaz/yaz-util.h>
48 #include <yaz/nmem.h>
49 #include <yaz/query-charset.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/oid_db.h>
52 #include <yaz/diagbib1.h>
53 #include <yaz/snprintf.h>
54 #include <yaz/rpn2cql.h>
55
56 #define USE_TIMING 0
57 #if USE_TIMING
58 #include <yaz/timing.h>
59 #endif
60
61 #include "session.h"
62 #include "parameters.h"
63 #include "client.h"
64 #include "connection.h"
65 #include "settings.h"
66 #include "relevance.h"
67 #include "incref.h"
68
69 /** \brief Represents client state for a connection to one search target */
70 struct client {
71     struct session_database *database;
72     struct connection *connection;
73     struct session *session;
74     char *pquery; // Current search
75     char *cqlquery; // used for SRU targets only
76     Odr_int hits;
77     int record_offset;
78     int maxrecs;
79     int startrecs;
80     int diagnostic;
81     enum client_state state;
82     struct show_raw *show_raw;
83     struct client *next;     // next client in session or next in free list
84     ZOOM_resultset resultset;
85     YAZ_MUTEX mutex;
86     int ref_count;
87 };
88
89 struct show_raw {
90     int active; // whether this request has been sent to the server
91     int position;
92     int binary;
93     char *syntax;
94     char *esn;
95     void (*error_handler)(void *data, const char *addinfo);
96     void (*record_handler)(void *data, const char *buf, size_t sz);
97     void *data;
98     struct show_raw *next;
99 };
100
101 static const char *client_states[] = {
102     "Client_Connecting",
103     "Client_Idle",
104     "Client_Working",
105     "Client_Error",
106     "Client_Failed",
107     "Client_Disconnected"
108 };
109
110 const char *client_get_state_str(struct client *cl)
111 {
112     return client_states[cl->state];
113 }
114
115 enum client_state client_get_state(struct client *cl)
116 {
117     return cl->state;
118 }
119
120 void client_set_state(struct client *cl, enum client_state st)
121 {
122     cl->state = st;
123     /* no need to check for all client being non-active if this one
124        already is. Note that session_active_clients also LOCKS session */
125 #if 0
126     if (!client_is_active(cl) && cl->session)
127     {
128         int no_active = session_active_clients(cl->session);
129         if (no_active == 0)
130             session_alert_watch(cl->session, SESSION_WATCH_SHOW);
131     }
132 #endif
133 }
134
135 static void client_show_raw_error(struct client *cl, const char *addinfo);
136
137 // Close connection and set state to error
138 void client_fatal(struct client *cl)
139 {
140     yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
141     connection_destroy(cl->connection);
142     client_set_state(cl, Client_Error);
143 }
144
145 struct connection *client_get_connection(struct client *cl)
146 {
147     return cl->connection;
148 }
149
150 struct session_database *client_get_database(struct client *cl)
151 {
152     return cl->database;
153 }
154
155 struct session *client_get_session(struct client *cl)
156 {
157     return cl->session;
158 }
159
160 const char *client_get_pquery(struct client *cl)
161 {
162     return cl->pquery;
163 }
164
165 static void client_send_raw_present(struct client *cl);
166 static int nativesyntax_to_type(struct session_database *sdb, char *type,
167                                 ZOOM_record rec);
168
169 static void client_show_immediate(
170     ZOOM_resultset resultset, struct session_database *sdb, int position,
171     void *data,
172     void (*error_handler)(void *data, const char *addinfo),
173     void (*record_handler)(void *data, const char *buf, size_t sz),
174     int binary)
175 {
176     ZOOM_record rec = 0;
177     char type[80];
178     const char *buf;
179     int len;
180
181     if (!resultset)
182     {
183         error_handler(data, "no resultset");
184         return;
185     }
186     rec = ZOOM_resultset_record(resultset, position-1);
187     if (!rec)
188     {
189         error_handler(data, "no record");
190         return;
191     }
192     if (binary)
193         strcpy(type, "raw");
194     else
195         nativesyntax_to_type(sdb, type, rec);
196     buf = ZOOM_record_get(rec, type, &len);
197     if (!buf)
198     {
199         error_handler(data, "no record");
200         return;
201     }
202     record_handler(data, buf, len);
203 }
204
205
206 int client_show_raw_begin(struct client *cl, int position,
207                           const char *syntax, const char *esn,
208                           void *data,
209                           void (*error_handler)(void *data, const char *addinfo),
210                           void (*record_handler)(void *data, const char *buf,
211                                                  size_t sz),
212                           int binary)
213 {
214     if (syntax == 0 && esn == 0)
215         client_show_immediate(cl->resultset, client_get_database(cl),
216                               position, data,
217                               error_handler, record_handler,
218                               binary);
219     else
220     {
221         struct show_raw *rr, **rrp;
222
223         if (!cl->connection)
224             return -1;
225     
226
227         rr = xmalloc(sizeof(*rr));
228         rr->position = position;
229         rr->active = 0;
230         rr->data = data;
231         rr->error_handler = error_handler;
232         rr->record_handler = record_handler;
233         rr->binary = binary;
234         if (syntax)
235             rr->syntax = xstrdup(syntax);
236         else
237             rr->syntax = 0;
238         if (esn)
239             rr->esn = xstrdup(esn);
240         else
241             rr->esn = 0;
242         rr->next = 0;
243         
244         for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
245             ;
246         *rrp = rr;
247         
248         if (cl->state == Client_Failed)
249         {
250             client_show_raw_error(cl, "client failed");
251         }
252         else if (cl->state == Client_Disconnected)
253         {
254             client_show_raw_error(cl, "client disconnected");
255         }
256         else
257         {
258             client_send_raw_present(cl);
259         }
260     }
261     return 0;
262 }
263
264 static void client_show_raw_delete(struct show_raw *r)
265 {
266     xfree(r->syntax);
267     xfree(r->esn);
268     xfree(r);
269 }
270
271 void client_show_raw_remove(struct client *cl, void *data)
272 {
273     struct show_raw *rr = data;
274     struct show_raw **rrp = &cl->show_raw;
275     while (*rrp != rr)
276         rrp = &(*rrp)->next;
277     if (*rrp)
278     {
279         *rrp = rr->next;
280         client_show_raw_delete(rr);
281     }
282 }
283
284 void client_show_raw_dequeue(struct client *cl)
285 {
286     struct show_raw *rr = cl->show_raw;
287
288     cl->show_raw = rr->next;
289     client_show_raw_delete(rr);
290 }
291
292 static void client_show_raw_error(struct client *cl, const char *addinfo)
293 {
294     while (cl->show_raw)
295     {
296         cl->show_raw->error_handler(cl->show_raw->data, addinfo);
297         client_show_raw_dequeue(cl);
298     }
299 }
300
301 static void client_send_raw_present(struct client *cl)
302 {
303     struct session_database *sdb = client_get_database(cl);
304     struct connection *co = client_get_connection(cl);
305     ZOOM_resultset set = cl->resultset;
306
307     int offset = cl->show_raw->position;
308     const char *syntax = 0;
309     const char *elements = 0;
310
311     assert(cl->show_raw);
312     assert(set);
313
314     yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
315             client_get_url(cl), 1, offset);
316
317     if (cl->show_raw->syntax)
318         syntax = cl->show_raw->syntax;
319     else
320         syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
321     ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
322
323     if (cl->show_raw->esn)
324         elements = cl->show_raw->esn;
325     else
326         elements = session_setting_oneval(sdb, PZ_ELEMENTS);
327     if (elements && *elements)
328         ZOOM_resultset_option_set(set, "elementSetName", elements);
329
330     ZOOM_resultset_records(set, 0, offset-1, 1);
331     cl->show_raw->active = 1;
332
333     connection_continue(co);
334 }
335
336 static int nativesyntax_to_type(struct session_database *sdb, char *type,
337                                 ZOOM_record rec)
338 {
339     const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
340
341     if (s && *s)
342     {
343         if (!strncmp(s, "iso2709", 7))
344         {
345             const char *cp = strchr(s, ';');
346             yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
347         }
348         else if (!strncmp(s, "xml", 3))
349         {
350             strcpy(type, "xml");
351         }
352         else
353             return -1;
354         return 0;
355     }
356     else  /* attempt to deduce structure */
357     {
358         const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
359         if (syntax)
360         {
361             if (!strcmp(syntax, "XML"))
362             {
363                 strcpy(type, "xml");
364                 return 0;
365             }
366             else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
367             {
368                 strcpy(type, "xml; charset=marc8-s");
369                 return 0;
370             }
371             else return -1;
372         }
373         else return -1;
374     }
375 }
376
377 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
378 {
379     const char *buf;
380     int len;
381     char type[80];
382
383     if (cl->show_raw->binary)
384         strcpy(type, "raw");
385     else
386     {
387         struct session_database *sdb = client_get_database(cl);
388         nativesyntax_to_type(sdb, type, rec);
389     }
390
391     buf = ZOOM_record_get(rec, type, &len);
392     cl->show_raw->record_handler(cl->show_raw->data,  buf, len);
393     client_show_raw_dequeue(cl);
394 }
395
396 void client_search_response(struct client *cl)
397 {
398     struct connection *co = cl->connection;
399     struct session *se = cl->session;
400     ZOOM_connection link = connection_get_link(co);
401     ZOOM_resultset resultset = cl->resultset;
402     const char *error, *addinfo = 0;
403     
404     if (ZOOM_connection_error(link, &error, &addinfo))
405     {
406         cl->hits = 0;
407         client_set_state(cl, Client_Error);
408         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
409                 error, addinfo, client_get_url(cl));
410     }
411     else
412     {
413         cl->record_offset = cl->startrecs;
414         cl->hits = ZOOM_resultset_size(resultset);
415         if (se)
416             se->total_hits += cl->hits;
417     }
418 }
419
420 void client_got_records(struct client *cl)
421 {
422     if (cl->session)
423     {
424         session_alert_watch(cl->session, SESSION_WATCH_SHOW);
425         session_alert_watch(cl->session, SESSION_WATCH_RECORD);
426     }
427 }
428
429 void client_record_response(struct client *cl)
430 {
431     struct connection *co = cl->connection;
432     ZOOM_connection link = connection_get_link(co);
433     ZOOM_resultset resultset = cl->resultset;
434     const char *error, *addinfo;
435
436     if (ZOOM_connection_error(link, &error, &addinfo))
437     {
438         client_set_state(cl, Client_Error);
439         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
440             error, addinfo, client_get_url(cl));
441     }
442     else
443     {
444         ZOOM_record rec = 0;
445         const char *msg, *addinfo;
446         
447         if (cl->show_raw && cl->show_raw->active)
448         {
449             if ((rec = ZOOM_resultset_record(resultset,
450                                              cl->show_raw->position-1)))
451             {
452                 cl->show_raw->active = 0;
453                 ingest_raw_record(cl, rec);
454             }
455             else
456             {
457                 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
458                         cl->show_raw->position-1);
459             }
460         }
461         else
462         {
463             int offset = cl->record_offset;
464             if ((rec = ZOOM_resultset_record(resultset, offset)))
465             {
466                 cl->record_offset++;
467                 if (cl->session == 0)
468                     ;
469                 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
470                     yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
471                             error, addinfo, client_get_url(cl),
472                             cl->record_offset);
473                 else
474                 {
475                     struct session_database *sdb = client_get_database(cl);
476                     NMEM nmem = nmem_create();
477                     const char *xmlrec;
478                     char type[80];
479                     yaz_log(YLOG_LOG, "Record ingest begin client=%p session=%p", cl, cl->session);
480                     if (nativesyntax_to_type(sdb, type, rec))
481                         yaz_log(YLOG_WARN, "Failed to determine record type");
482                     xmlrec = ZOOM_record_get(rec, type, NULL);
483                     if (!xmlrec)
484                         yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
485                                 client_get_url(cl));
486                     else
487                     {
488                         if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
489                             yaz_log(YLOG_WARN, "Failed to ingest from %s",
490                                     client_get_url(cl));
491                     }
492                     nmem_destroy(nmem);
493                     yaz_log(YLOG_LOG, "Record ingest end client=%p session=%p", cl, cl->session);
494                 }
495             }
496             else
497             {
498                 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
499                         offset);
500             }
501         }
502     }
503 }
504
505 void client_start_search(struct client *cl)
506 {
507     struct session_database *sdb = client_get_database(cl);
508     struct connection *co = client_get_connection(cl);
509     ZOOM_connection link = connection_get_link(co);
510     ZOOM_resultset rs;
511     char *databaseName = sdb->database->databases[0];
512     const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
513     const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
514     const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
515     const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
516     const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
517     const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
518     const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
519     char maxrecs_str[24], startrecs_str[24];
520
521     assert(link);
522
523     cl->hits = -1;
524     cl->record_offset = 0;
525     cl->diagnostic = 0;
526     client_set_state(cl, Client_Working);
527
528     if (*opt_piggyback)
529         ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
530     else
531         ZOOM_connection_option_set(link, "piggyback", "1");
532     if (*opt_queryenc)
533         ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
534     if (*opt_sru && *opt_elements)
535         ZOOM_connection_option_set(link, "schema", opt_elements);
536     else if (*opt_elements)
537         ZOOM_connection_option_set(link, "elementSetName", opt_elements);
538     if (*opt_requestsyn)
539         ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
540
541     if (!*opt_maxrecs)
542     {
543         sprintf(maxrecs_str, "%d", cl->maxrecs);
544         opt_maxrecs = maxrecs_str;
545     }
546     ZOOM_connection_option_set(link, "count", opt_maxrecs);
547
548
549     if (atoi(opt_maxrecs) > 20)
550         ZOOM_connection_option_set(link, "presentChunk", "20");
551     else
552         ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
553
554     sprintf(startrecs_str, "%d", cl->startrecs);
555     ZOOM_connection_option_set(link, "start", startrecs_str);
556
557     if (databaseName)
558         ZOOM_connection_option_set(link, "databaseName", databaseName);
559
560     if (cl->cqlquery)
561     {
562         ZOOM_query q = ZOOM_query_create();
563         yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
564         ZOOM_query_cql(q, cl->cqlquery);
565         if (*opt_sort)
566             ZOOM_query_sortby(q, opt_sort);
567         rs = ZOOM_connection_search(link, q);
568         ZOOM_query_destroy(q);
569     }
570     else
571     {
572         yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
573         rs = ZOOM_connection_search_pqf(link, cl->pquery);
574     }
575     ZOOM_resultset_destroy(cl->resultset);
576     cl->resultset = rs;
577     connection_continue(co);
578 }
579
580 struct client *client_create(void)
581 {
582     struct client *r = xmalloc(sizeof(*r));
583     r->maxrecs = 100;
584     r->startrecs = 0;
585     r->pquery = 0;
586     r->cqlquery = 0;
587     r->database = 0;
588     r->connection = 0;
589     r->session = 0;
590     r->hits = 0;
591     r->record_offset = 0;
592     r->diagnostic = 0;
593     r->state = Client_Disconnected;
594     r->show_raw = 0;
595     r->resultset = 0;
596     r->next = 0;
597     r->mutex = 0;
598     yaz_mutex_create(&r->mutex);
599     yaz_mutex_set_name(r->mutex, "client");
600
601     r->ref_count = 1;
602     
603     return r;
604 }
605
606 void client_incref(struct client *c)
607 {
608     pazpar2_incref(&c->ref_count, c->mutex);
609     yaz_log(YLOG_DEBUG, "client_incref %s %d", client_get_url(c), c->ref_count);
610 }
611
612 int client_destroy(struct client *c)
613 {
614     if (c)
615     {
616         yaz_log(YLOG_DEBUG, "client_destroy %s %d",
617                 client_get_url(c), c->ref_count);
618         if (!pazpar2_decref(&c->ref_count, c->mutex))
619         {
620             c->next = 0;
621             xfree(c->pquery);
622             c->pquery = 0;
623             xfree(c->cqlquery);
624             c->cqlquery = 0;
625
626             ZOOM_resultset_destroy(c->resultset);
627             yaz_mutex_destroy(&c->mutex);
628             xfree(c);
629             return 1;
630         }
631     }
632     return 0;
633 }
634
635 void client_set_connection(struct client *cl, struct connection *con)
636 {
637     if (con)
638     {
639         assert(cl->connection == 0);
640         cl->connection = con;
641         client_incref(cl);
642     }
643     else
644     {
645         cl->connection = con;
646         client_destroy(cl);
647     }
648 }
649
650 void client_disconnect(struct client *cl)
651 {
652     if (cl->state != Client_Idle)
653         client_set_state(cl, Client_Disconnected);
654     client_set_connection(cl, 0);
655 }
656
657 // Extract terms from query into null-terminated termlist
658 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
659 {
660     int num = 0;
661
662     pull_terms(nmem, query, termlist, &num);
663     termlist[num] = 0;
664 }
665
666 // Initialize CCL map for a target
667 static CCL_bibset prepare_cclmap(struct client *cl)
668 {
669     struct session_database *sdb = client_get_database(cl);
670     struct setting *s;
671     CCL_bibset res;
672
673     if (!sdb->settings)
674         return 0;
675     res = ccl_qual_mk();
676     for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
677     {
678         char *p = strchr(s->name + 3, ':');
679         if (!p)
680         {
681             yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
682             ccl_qual_rm(&res);
683             return 0;
684         }
685         p++;
686         ccl_qual_fitem(res, s->value, p);
687     }
688     return res;
689 }
690
691 // returns a xmalloced CQL query corresponding to the pquery in client
692 static char *make_cqlquery(struct client *cl)
693 {
694     cql_transform_t cqlt = cql_transform_create();
695     Z_RPNQuery *zquery;
696     char *r;
697     WRBUF wrb = wrbuf_alloc();
698     int status;
699     ODR odr_out = odr_createmem(ODR_ENCODE);
700
701     zquery = p_query_rpn(odr_out, cl->pquery);
702     yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
703     if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
704     {
705         yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
706         r = 0;
707     }
708     else
709     {
710         r = xstrdup(wrbuf_cstr(wrb));
711     }     
712     wrbuf_destroy(wrb);
713     odr_destroy(odr_out);
714     cql_transform_close(cqlt);
715     return r;
716 }
717
718 // Parse the query given the settings specific to this client
719 int client_parse_query(struct client *cl, const char *query)
720 {
721     struct session *se = client_get_session(cl);
722     struct session_database *sdb = client_get_database(cl);
723     struct ccl_rpn_node *cn;
724     int cerror, cpos;
725     CCL_bibset ccl_map = prepare_cclmap(cl);
726     const char *sru = session_setting_oneval(sdb, PZ_SRU);
727     const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
728     const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
729
730     if (!ccl_map)
731         return -1;
732
733     cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
734     ccl_qual_rm(&ccl_map);
735     if (!cn)
736     {
737         client_set_state(cl, Client_Error);
738         yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
739                 query,
740                 client_get_database(cl)->database->url);
741         return -1;
742     }
743     wrbuf_rewind(se->wrbuf);
744     if (*pqf_prefix)
745     {
746         wrbuf_puts(se->wrbuf, pqf_prefix);
747         wrbuf_puts(se->wrbuf, " ");
748     }
749     if (!pqf_strftime || !*pqf_strftime)
750         ccl_pquery(se->wrbuf, cn);
751     else
752     {
753         time_t cur_time = time(0);
754         struct tm *tm =  localtime(&cur_time);
755         char tmp_str[300];
756         const char *cp = tmp_str;
757
758         /* see man strftime(3) for things .. In particular %% gets converted
759          to %.. And That's our original query .. */
760         strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
761         for (; *cp; cp++)
762         {
763             if (cp[0] == '%')
764                 ccl_pquery(se->wrbuf, cn);
765             else
766                 wrbuf_putc(se->wrbuf, cp[0]);
767         }
768     }
769     xfree(cl->pquery);
770     cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
771
772     xfree(cl->cqlquery);
773     if (*sru)
774     {
775         if (!(cl->cqlquery = make_cqlquery(cl)))
776             return -1;
777     }
778     else
779         cl->cqlquery = 0;
780
781     if (!se->relevance)
782     {
783         // Initialize relevance structure with query terms
784         char *p[512];
785         extract_terms(se->nmem, cn, p);
786         se->relevance = relevance_create(
787             se->service->relevance_pct,
788             se->nmem, (const char **) p);
789     }
790
791     ccl_rpn_delete(cn);
792     return 0;
793 }
794
795
796 void client_remove_from_session(struct client *c)
797 {
798     struct session *se;
799     client_incref(c);
800
801     se = c->session;
802     assert(se);
803     if (se)
804     {
805         struct client **ccp = &se->clients;
806         
807         while (*ccp && *ccp != c)
808             ccp = &(*ccp)->next;
809         assert(*ccp == c);
810         *ccp = c->next;
811         
812         c->database = 0;
813         c->session = 0;
814         c->next = 0;
815     }
816     client_destroy(c);
817 }
818
819 void client_set_session(struct client *cl, struct session *se)
820 {
821     cl->session = se;
822     cl->next = se->clients;
823     se->clients = cl;
824 }
825
826 int client_is_active(struct client *cl)
827 {
828     if (cl->connection && (cl->state == Client_Connecting ||
829                            cl->state == Client_Working))
830         return 1;
831     return 0;
832 }
833
834 struct client *client_next_in_session(struct client *cl)
835 {
836     if (cl)
837         return cl->next;
838     return 0;
839
840 }
841
842 Odr_int client_get_hits(struct client *cl)
843 {
844     return cl->hits;
845 }
846
847 int client_get_num_records(struct client *cl)
848 {
849     return cl->record_offset;
850 }
851
852 void client_set_diagnostic(struct client *cl, int diagnostic)
853 {
854     cl->diagnostic = diagnostic;
855 }
856
857 int client_get_diagnostic(struct client *cl)
858 {
859     return cl->diagnostic;
860 }
861
862 void client_set_database(struct client *cl, struct session_database *db)
863 {
864     cl->database = db;
865 }
866
867 struct host *client_get_host(struct client *cl)
868 {
869     return client_get_database(cl)->database->host;
870 }
871
872 const char *client_get_url(struct client *cl)
873 {
874     if (cl->database)
875         return client_get_database(cl)->database->url;
876     else
877         return "NOURL";
878 }
879
880 void client_set_maxrecs(struct client *cl, int v)
881 {
882     cl->maxrecs = v;
883 }
884
885 void client_set_startrecs(struct client *cl, int v)
886 {
887     cl->startrecs = v;
888 }
889
890 /*
891  * Local variables:
892  * c-basic-offset: 4
893  * c-file-style: "Stroustrup"
894  * indent-tabs-mode: nil
895  * End:
896  * vim: shiftwidth=4 tabstop=8 expandtab
897  */
898