Simple refactor due to yaz_match_xsd_element 0 check YAZ-822
[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     char *charset;
87     char *lang;
88     char *cookie_out;
89     char *cookie_in;
90     char *client_IP;
91     char *sru_version;
92
93     char *user;
94     char *group;
95     char *password;
96     int url_authentication;
97
98     int async;
99     int support_named_resultsets;
100     int last_event;
101
102     int maximum_record_size;
103     int preferred_message_size;
104
105     ZOOM_task tasks;
106     ZOOM_options options;
107     ZOOM_resultset resultsets;
108     ZOOM_Event m_queue_front;
109     ZOOM_Event m_queue_back;
110     zoom_sru_mode sru_mode;
111     int no_redirects; /* 0 for no redirects. >0 for number of redirects */
112     yaz_cookies_t cookies;
113     char *location;
114
115     int log_details;
116     int log_api;
117     WRBUF saveAPDU_wrbuf;
118 #if HAVE_LIBMEMCACHED
119     memcached_st *mc_st;
120 #endif
121 #if HAVE_HIREDIS
122     redisContext *redis_c;
123 #endif
124     int expire_search;
125     int expire_record;
126 };
127
128 typedef struct ZOOM_record_cache_p *ZOOM_record_cache;
129
130 #define RECORD_HASH_SIZE  131
131
132 struct ZOOM_resultset_p {
133     Z_SortKeySpecList *r_sort_spec;
134     ZOOM_query query;
135     int refcount;
136     Odr_int size;
137     int step;
138     int piggyback;
139     char *setname;
140     ODR odr;
141     ZOOM_record_cache record_hash[RECORD_HASH_SIZE];
142     ZOOM_options options;
143     ZOOM_connection connection;
144     char **databaseNames;
145     int num_databaseNames;
146     YAZ_MUTEX mutex;
147 #if SHPTR
148     struct WRBUF_shptr *record_wrbuf;
149 #endif
150     ZOOM_resultset next;
151     char *req_facets;
152     ZOOM_facet_field *res_facets;
153     int num_res_facets;
154     char **facets_names; /* redundant. For ZOOM_resultset_facets_names only */
155     WRBUF mc_key;
156     int live_set; /* 0=no hit count, 1=cached hit, 2=hits + real set */
157 };
158
159 struct facet_term_p {
160     char *term;
161     int frequency;
162 };
163
164 struct ZOOM_facet_field_p {
165     char *facet_name;
166     int num_terms;
167     struct facet_term_p *facet_terms;
168 };
169
170 struct ZOOM_scanset_p {
171     int refcount;
172     ODR odr;
173     ZOOM_options options;
174     ZOOM_connection connection;
175     ZOOM_query query;
176     Z_ScanResponse *scan_response;
177     Z_SRW_scanResponse *srw_scan_response;
178
179     char **databaseNames;
180     int num_databaseNames;
181 };
182
183 struct ZOOM_package_p {
184     int refcount;
185     ODR odr_out;
186     ZOOM_options options;
187     ZOOM_connection connection;
188     char *buf_out;
189     int len_out;
190 };
191
192 struct ZOOM_task_p {
193     int running;
194     int which;
195     union {
196 #define ZOOM_TASK_SEARCH 1
197         struct {
198             int count;
199             int start;
200             ZOOM_resultset resultset;
201             char *syntax;
202             char *elementSetName;
203             char *schema;
204         } search;
205 #define ZOOM_TASK_CONNECT 3
206 #define ZOOM_TASK_SCAN 4
207         struct {
208             ZOOM_scanset scan;
209         } scan;
210 #define ZOOM_TASK_PACKAGE 5
211         ZOOM_package package;
212 #define ZOOM_TASK_SORT 6
213         struct {
214             ZOOM_resultset resultset;
215             ZOOM_query q;
216         } sort;
217     } u;
218     ZOOM_task next;
219 };
220
221 typedef enum {
222     zoom_pending,
223     zoom_complete
224 } zoom_ret;
225
226 void ZOOM_options_addref (ZOOM_options opt);
227
228 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu);
229
230 void ZOOM_set_dset_error(ZOOM_connection c, int error,
231                          const char *dset,
232                          const char *addinfo, const char *addinfo2);
233
234 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo);
235
236 ZOOM_Event ZOOM_Event_create(int kind);
237 void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event);
238
239 zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c);
240 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c);
241 zoom_ret ZOOM_send_buf(ZOOM_connection c);
242 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset);
243 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
244                                      int *num, ODR odr);
245 zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c);
246
247 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which);
248 void ZOOM_connection_remove_task(ZOOM_connection c);
249 int ZOOM_test_reconnect(ZOOM_connection c);
250
251 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
252                                      const char *syntax,
253                                      const char *elementSetName,
254                                      const char *schema);
255 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
256                            int pos,
257                            const char *syntax, const char *elementSetName,
258                            const char *schema,
259                            Z_SRW_diagnostic *diag);
260
261 Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s);
262 Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s);
263 const char *ZOOM_query_get_query_string(ZOOM_query s);
264 const char *ZOOM_query_get_sru11(ZOOM_query s);
265 void ZOOM_query_get_hash(ZOOM_query s, WRBUF w);
266
267 int ZOOM_uri_to_code(const char *uri);
268
269 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c);
270 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c);
271
272 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
273                     zoom_ret *cret, char **addinfo);
274
275 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
276                          const char *addinfo, const char *addinfo2);
277
278 ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c);
279 void ZOOM_connection_remove_events(ZOOM_connection c);
280 void ZOOM_Event_destroy(ZOOM_Event event);
281 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu);
282 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl);
283
284 void ZOOM_memcached_init(ZOOM_connection c);
285 int ZOOM_memcached_configure(ZOOM_connection c);
286 void ZOOM_memcached_destroy(ZOOM_connection c);
287 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q);
288 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset r);
289 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset result,
290                              Z_OtherInformation *oi, const char *precision);
291 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
292                         int pos,
293                         const char *syntax, const char *elementSetName,
294                         const char *schema,
295                         Z_SRW_diagnostic *diag);
296 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
297                                         const char *syntax,
298                                         const char *elementSetName,
299                                         const char *schema);
300 ZOOM_record ZOOM_record_cache_lookup_i(ZOOM_resultset r, int pos,
301                                        const char *syntax,
302                                        const char *elementSetName,
303                                        const char *schema);
304 void ZOOM_handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
305                               Z_OtherInformation *o);
306 void ZOOM_handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
307                                Z_OtherInformation *o);
308
309 /*
310  * Local variables:
311  * c-basic-offset: 4
312  * c-file-style: "Stroustrup"
313  * indent-tabs-mode: nil
314  * End:
315  * vim: shiftwidth=4 tabstop=8 expandtab
316  */
317