ZOOM: initial experiments with memcached
[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_MEMCACHED_H
40 #include <libmemcached/memcached.h>
41 #endif
42
43 #define SHPTR 1
44
45 typedef struct ZOOM_Event_p *ZOOM_Event;
46
47 typedef enum {
48     zoom_sru_error,
49     zoom_sru_soap,
50     zoom_sru_get,
51     zoom_sru_post,
52     zoom_sru_solr
53 } zoom_sru_mode;
54
55
56 typedef struct ZOOM_task_p *ZOOM_task;
57
58 #define STATE_IDLE 0
59 #define STATE_CONNECTING 1
60 #define STATE_ESTABLISHED 2
61
62 struct ZOOM_connection_p {
63     enum oid_proto proto;
64     COMSTACK cs;
65     char *host_port;
66     int error;
67     char *addinfo;
68     char *diagset;
69     int state;
70     int mask;
71     int reconnect_ok;
72     ODR odr_in;
73     ODR odr_out;
74     ODR odr_print;
75     ODR odr_save;
76
77     char *buf_in;
78     int len_in;
79     char *buf_out;
80     int len_out;
81     char *proxy;
82     char *tproxy;
83     char *charset;
84     char *lang;
85     char *cookie_out;
86     char *cookie_in;
87     char *client_IP;
88     char *sru_version;
89
90     char *user;
91     char *group;
92     char *password;
93     int url_authentication;
94
95     int async;
96     int support_named_resultsets;
97     int last_event;
98
99     int maximum_record_size;
100     int preferred_message_size;
101
102     ZOOM_task tasks;
103     ZOOM_options options;
104     ZOOM_resultset resultsets;
105     ZOOM_Event m_queue_front;
106     ZOOM_Event m_queue_back;
107     zoom_sru_mode sru_mode;
108     int no_redirects; /* 0 for no redirects. >0 for number of redirects */
109     yaz_cookies_t cookies;
110
111     int log_details;
112     int log_api;
113     WRBUF saveAPDU_wrbuf;
114 #if HAVE_LIBMEMCACHED_MEMCACHED_H
115     memcached_st *mc_st;
116 #endif
117 };
118
119 typedef struct ZOOM_record_cache_p *ZOOM_record_cache;
120
121 #define RECORD_HASH_SIZE  131
122
123 struct ZOOM_resultset_p {
124     Z_SortKeySpecList *r_sort_spec;
125     ZOOM_query query;
126     int refcount;
127     Odr_int size;
128     int step;
129     int piggyback;
130     char *setname;
131     ODR odr;
132     ZOOM_record_cache record_hash[RECORD_HASH_SIZE];
133     ZOOM_options options;
134     ZOOM_connection connection;
135     char **databaseNames;
136     int num_databaseNames;
137     YAZ_MUTEX mutex;
138 #if SHPTR
139     struct WRBUF_shptr *record_wrbuf;
140 #endif
141     ZOOM_resultset next;
142     ZOOM_facet_field *facets;
143     int num_facets;
144     char **facets_names;
145     WRBUF mc_key;
146 };
147
148 struct facet_term_p {
149     char *term;
150     int frequency;
151 };
152
153 struct ZOOM_facet_field_p {
154     char *facet_name;
155     int num_terms;
156     struct facet_term_p *facet_terms;
157 };
158
159 struct ZOOM_scanset_p {
160     int refcount;
161     ODR odr;
162     ZOOM_options options;
163     ZOOM_connection connection;
164     ZOOM_query query;
165     Z_ScanResponse *scan_response;
166     Z_SRW_scanResponse *srw_scan_response;
167
168     char **databaseNames;
169     int num_databaseNames;
170 };
171
172 struct ZOOM_package_p {
173     int refcount;
174     ODR odr_out;
175     ZOOM_options options;
176     ZOOM_connection connection;
177     char *buf_out;
178     int len_out;
179 };
180
181 struct ZOOM_task_p {
182     int running;
183     int which;
184     union {
185 #define ZOOM_TASK_SEARCH 1
186         struct {
187             int count;
188             int start;
189             ZOOM_resultset resultset;
190             char *syntax;
191             char *elementSetName;
192             char *schema;
193             int recv_search_fired;
194         } search;
195 #define ZOOM_TASK_RETRIEVE 2
196         struct {
197             int start;
198             ZOOM_resultset resultset;
199             int count;
200             char *syntax;
201             char *elementSetName;
202             char *schema;
203         } retrieve;
204 #define ZOOM_TASK_CONNECT 3
205 #define ZOOM_TASK_SCAN 4
206         struct {
207             ZOOM_scanset scan;
208         } scan;
209 #define ZOOM_TASK_PACKAGE 5
210         ZOOM_package package;
211 #define ZOOM_TASK_SORT 6
212         struct {
213             ZOOM_resultset resultset;
214             ZOOM_query q;
215         } sort;
216     } u;
217     ZOOM_task next;
218 };
219
220 typedef enum {
221     zoom_pending,
222     zoom_complete
223 } zoom_ret;
224
225 void ZOOM_options_addref (ZOOM_options opt);
226
227 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu);
228
229 void ZOOM_set_dset_error(ZOOM_connection c, int error,
230                          const char *dset,
231                          const char *addinfo, const char *addinfo2);
232
233 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo);
234
235 ZOOM_Event ZOOM_Event_create(int kind);
236 void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event);
237
238 zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c);
239 zoom_ret send_Z3950_present(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
266 int ZOOM_uri_to_code(const char *uri);
267
268 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c);
269 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c);
270
271 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
272                     zoom_ret *cret, char **addinfo);
273
274 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
275                          const char *addinfo, const char *addinfo2);
276
277 ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c);
278 void ZOOM_connection_remove_events(ZOOM_connection c);
279 void ZOOM_Event_destroy(ZOOM_Event event);
280 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu);
281 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl);
282
283 /*
284  * Local variables:
285  * c-basic-offset: 4
286  * c-file-style: "Stroustrup"
287  * indent-tabs-mode: nil
288  * End:
289  * vim: shiftwidth=4 tabstop=8 expandtab
290  */
291