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