Add lock/unlock for YAZ log writes YAZ-843
[yaz-moved-to-github.git] / src / zoom-p.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /**
28  * \file zoom-p.h
29  * \brief Internal header for ZOOM implementation
30  */
31 #include <yaz/proto.h>
32 #include <yaz/oid_db.h>
33 #include <yaz/comstack.h>
34 #include <yaz/wrbuf.h>
35 #include <yaz/zoom.h>
36 #include <yaz/srw.h>
37 #include <yaz/cookie.h>
38 #include <yaz/mutex.h>
39 #if HAVE_LIBMEMCACHED
40 #include <libmemcached/memcached.h>
41 #endif
42 #if HAVE_HIREDIS
43 #include <hiredis/hiredis.h>
44 #endif
45
46 #define SHPTR 1
47
48 typedef struct ZOOM_Event_p *ZOOM_Event;
49
50 typedef enum {
51     zoom_sru_error,
52     zoom_sru_soap,
53     zoom_sru_get,
54     zoom_sru_post,
55     zoom_sru_solr
56 } zoom_sru_mode;
57
58
59 typedef struct ZOOM_task_p *ZOOM_task;
60
61 #define STATE_IDLE 0
62 #define STATE_CONNECTING 1
63 #define STATE_ESTABLISHED 2
64
65 struct ZOOM_connection_p {
66     enum oid_proto proto;
67     COMSTACK cs;
68     char *host_port;
69     int error;
70     char *addinfo;
71     char *diagset;
72     int state;
73     int mask;
74     int reconnect_ok;
75     ODR odr_in;
76     ODR odr_out;
77     ODR odr_print;
78     ODR odr_save;
79
80     char *buf_in;
81     int len_in;
82     char *buf_out;
83     int len_out;
84     char *proxy;
85     char *tproxy;
86     int proxy_mode;
87     char *charset;
88     char *lang;
89     char *cookie_out;
90     char *cookie_in;
91     char *client_IP;
92     char *sru_version;
93
94     char *user;
95     char *group;
96     char *password;
97     int url_authentication;
98
99     int async;
100     int support_named_resultsets;
101     int last_event;
102
103     int maximum_record_size;
104     int preferred_message_size;
105
106     ZOOM_task tasks;
107     ZOOM_options options;
108     ZOOM_resultset resultsets;
109     ZOOM_Event m_queue_front;
110     ZOOM_Event m_queue_back;
111     zoom_sru_mode sru_mode;
112     int no_redirects; /* 0 for no redirects. >0 for number of redirects */
113     yaz_cookies_t cookies;
114     char *location;
115
116     int log_details;
117     int log_api;
118     WRBUF saveAPDU_wrbuf;
119 #if HAVE_LIBMEMCACHED
120     memcached_st *mc_st;
121 #endif
122 #if HAVE_HIREDIS
123     redisContext *redis_c;
124 #endif
125     int expire_search;
126     int expire_record;
127 };
128
129 typedef struct ZOOM_record_cache_p *ZOOM_record_cache;
130
131 #define RECORD_HASH_SIZE  131
132
133 struct ZOOM_resultset_p {
134     Z_SortKeySpecList *r_sort_spec;
135     ZOOM_query query;
136     int refcount;
137     Odr_int size;
138     int step;
139     int piggyback;
140     char *setname;
141     ODR odr;
142     ZOOM_record_cache record_hash[RECORD_HASH_SIZE];
143     ZOOM_options options;
144     ZOOM_connection connection;
145     char **databaseNames;
146     int num_databaseNames;
147     YAZ_MUTEX mutex;
148 #if SHPTR
149     struct WRBUF_shptr *record_wrbuf;
150 #endif
151     ZOOM_resultset next;
152     char *req_facets;
153     ZOOM_facet_field *res_facets;
154     int num_res_facets;
155     char **facets_names; /* redundant. For ZOOM_resultset_facets_names only */
156     WRBUF mc_key;
157     int live_set; /* 0=no hit count, 1=cached hit, 2=hits + real set */
158 };
159
160 struct facet_term_p {
161     char *term;
162     int frequency;
163 };
164
165 struct ZOOM_facet_field_p {
166     char *facet_name;
167     int num_terms;
168     struct facet_term_p *facet_terms;
169 };
170
171 struct ZOOM_scanset_p {
172     int refcount;
173     ODR odr;
174     ZOOM_options options;
175     ZOOM_connection connection;
176     ZOOM_query query;
177     Z_ScanResponse *scan_response;
178     Z_SRW_scanResponse *srw_scan_response;
179
180     char **databaseNames;
181     int num_databaseNames;
182 };
183
184 struct ZOOM_package_p {
185     int refcount;
186     ODR odr_out;
187     ZOOM_options options;
188     ZOOM_connection connection;
189     char *buf_out;
190     int len_out;
191 };
192
193 struct ZOOM_task_p {
194     int running;
195     int which;
196     union {
197 #define ZOOM_TASK_SEARCH 1
198         struct {
199             int count;
200             int start;
201             ZOOM_resultset resultset;
202             char *syntax;
203             char *elementSetName;
204             char *schema;
205         } search;
206 #define ZOOM_TASK_CONNECT 3
207 #define ZOOM_TASK_SCAN 4
208         struct {
209             ZOOM_scanset scan;
210         } scan;
211 #define ZOOM_TASK_PACKAGE 5
212         ZOOM_package package;
213 #define ZOOM_TASK_SORT 6
214         struct {
215             ZOOM_resultset resultset;
216             ZOOM_query q;
217         } sort;
218     } u;
219     ZOOM_task next;
220 };
221
222 typedef enum {
223     zoom_pending,
224     zoom_complete
225 } zoom_ret;
226
227 void ZOOM_options_addref (ZOOM_options opt);
228
229 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu);
230
231 void ZOOM_set_dset_error(ZOOM_connection c, int error,
232                          const char *dset,
233                          const char *addinfo, const char *addinfo2);
234
235 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo);
236
237 ZOOM_Event ZOOM_Event_create(int kind);
238 void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event);
239
240 zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c);
241 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c);
242 zoom_ret ZOOM_send_buf(ZOOM_connection c);
243 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset);
244 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
245                                      int *num, ODR odr);
246 zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c);
247
248 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which);
249 void ZOOM_connection_remove_task(ZOOM_connection c);
250 int ZOOM_test_reconnect(ZOOM_connection c);
251
252 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
253                                      const char *syntax,
254                                      const char *elementSetName,
255                                      const char *schema);
256 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
257                            int pos,
258                            const char *syntax, const char *elementSetName,
259                            const char *schema,
260                            Z_SRW_diagnostic *diag);
261
262 Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s);
263 Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s);
264 const char *ZOOM_query_get_query_string(ZOOM_query s);
265 const char *ZOOM_query_get_sru11(ZOOM_query s);
266 void ZOOM_query_get_hash(ZOOM_query s, WRBUF w);
267
268 int ZOOM_uri_to_code(const char *uri);
269
270 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c);
271 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c);
272
273 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
274                     zoom_ret *cret, char **addinfo);
275
276 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
277                          const char *addinfo, const char *addinfo2);
278
279 ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c);
280 void ZOOM_connection_remove_events(ZOOM_connection c);
281 void ZOOM_Event_destroy(ZOOM_Event event);
282 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu);
283 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl);
284
285 void ZOOM_memcached_init(ZOOM_connection c);
286 int ZOOM_memcached_configure(ZOOM_connection c);
287 void ZOOM_memcached_destroy(ZOOM_connection c);
288 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q);
289 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset r);
290 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset result,
291                              Z_OtherInformation *oi, const char *precision);
292 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
293                         int pos,
294                         const char *syntax, const char *elementSetName,
295                         const char *schema,
296                         Z_SRW_diagnostic *diag);
297 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
298                                         const char *syntax,
299                                         const char *elementSetName,
300                                         const char *schema);
301 ZOOM_record ZOOM_record_cache_lookup_i(ZOOM_resultset r, int pos,
302                                        const char *syntax,
303                                        const char *elementSetName,
304                                        const char *schema);
305 void ZOOM_handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
306                               Z_OtherInformation *o);
307 void ZOOM_handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
308                                Z_OtherInformation *o);
309
310 /*
311  * Local variables:
312  * c-basic-offset: 4
313  * c-file-style: "Stroustrup"
314  * indent-tabs-mode: nil
315  * End:
316  * vim: shiftwidth=4 tabstop=8 expandtab
317  */
318