Use pthread_atfork to lock/unlock yaz_log system
[yaz-moved-to-github.git] / src / zoom-memcached.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-memcached.c
7  * \brief Implements query/record caching using memcached
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "zoom-p.h"
17
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/log.h>
21 #include <yaz/diagbib1.h>
22
23 #if HAVE_LIBMEMCACHED_MEMCACHED_H
24 #if HAVE_MEMCACHED_RETURN_T
25 #else
26 typedef memcached_return memcached_return_t;
27 #endif
28 #endif
29
30 void ZOOM_memcached_init(ZOOM_connection c)
31 {
32 #if HAVE_LIBMEMCACHED_MEMCACHED_H
33     c->mc_st = 0;
34 #endif
35 }
36
37 void ZOOM_memcached_destroy(ZOOM_connection c)
38 {
39 #if HAVE_LIBMEMCACHED_MEMCACHED_H
40     if (c->mc_st)
41         memcached_free(c->mc_st);
42 #endif
43 }
44
45 #if HAVE_LIBMEMCACHED_MEMCACHED_H
46 /* memcached wrapper.. Because memcached function do not exist in older libs */
47 static memcached_st *yaz_memcached_wrap(const char *conf)
48 {
49 #if HAVE_MEMCACHED_FUNC
50     return memcached(conf, strlen(conf));
51 #else
52     char **darray;
53     int i, num;
54     memcached_st *mc = memcached_create(0);
55     NMEM nmem = nmem_create();
56     memcached_return_t rc;
57
58     nmem_strsplit_blank(nmem, conf, &darray, &num);
59     for (i = 0; mc && i < num; i++)
60     {
61         if (!yaz_strncasecmp(darray[i], "--SERVER=", 9))
62         {
63             char *host = darray[i] + 9;
64             char *port = strchr(host, ':');
65             char *weight = strstr(host, "/?");
66             if (port)
67                 *port++ = '\0';
68             if (weight)
69             {
70                 *weight = '\0';
71                 weight += 2;
72             }
73             rc = memcached_server_add(mc, host, port ? atoi(port) : 11211);
74             yaz_log(YLOG_LOG, "memcached_server_add host=%s rc=%u %s",
75                     host, (unsigned) rc, memcached_strerror(mc, rc));
76             if (rc != MEMCACHED_SUCCESS)
77             {
78                 memcached_free(mc);
79                 mc = 0;
80             }
81         }
82         else
83         {
84             /* bad directive */
85             memcached_free(mc);
86             mc = 0;
87         }
88     }
89     nmem_destroy(nmem);
90     return mc;
91 #endif
92 }
93 #endif
94
95 int ZOOM_memcached_configure(ZOOM_connection c)
96 {
97     const char *val;
98 #if HAVE_LIBMEMCACHED_MEMCACHED_H
99     if (c->mc_st)
100     {
101         memcached_free(c->mc_st);
102         c->mc_st = 0;
103     }
104 #endif
105     val = ZOOM_options_get(c->options, "memcached");
106     if (val && *val)
107     {
108 #if HAVE_LIBMEMCACHED_MEMCACHED_H
109         c->mc_st = yaz_memcached_wrap(val);
110         if (!c->mc_st)
111         {
112             ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED,
113                            "could not create memcached");
114             return -1;
115         }
116         memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
117 #else
118         ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
119         return -1;
120 #endif
121     }
122     return 0;
123 }
124
125 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
126 {
127 #if HAVE_LIBMEMCACHED_MEMCACHED_H
128     ZOOM_connection c = r->connection;
129     r->mc_key = wrbuf_alloc();
130     wrbuf_puts(r->mc_key, "0;");
131     wrbuf_puts(r->mc_key, c->host_port);
132     wrbuf_puts(r->mc_key, ";");
133     if (c->user)
134         wrbuf_puts(r->mc_key, c->user);
135     wrbuf_puts(r->mc_key, ";");
136     if (c->group)
137         wrbuf_puts(r->mc_key, c->group);
138     wrbuf_puts(r->mc_key, ";");
139     if (c->password)
140         wrbuf_sha1_puts(r->mc_key, c->password, 1);
141     wrbuf_puts(r->mc_key, ";");
142     {
143         WRBUF w = wrbuf_alloc();
144         ZOOM_query_get_hash(q, w);
145         wrbuf_sha1_puts(r->mc_key, wrbuf_cstr(w), 1);
146         wrbuf_destroy(w);
147     }
148     wrbuf_puts(r->mc_key, ";");
149     if (r->req_facets)
150         wrbuf_puts(r->mc_key, r->req_facets);
151 #endif
152 }
153
154 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
155 {
156 #if HAVE_LIBMEMCACHED_MEMCACHED_H
157     if (c->mc_st && resultset->live_set == 0)
158     {
159         size_t v_len;
160         uint32_t flags;
161         memcached_return_t rc;
162         char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
163                                 wrbuf_len(resultset->mc_key),
164                                 &v_len, &flags, &rc);
165         /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
166         if (v)
167         {
168             ZOOM_Event event;
169             size_t lead_len = strlen(v) + 1;
170
171             resultset->size = odr_atoi(v);
172
173             yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
174                     wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
175                     (int) v_len);
176             if (v_len > lead_len)
177             {
178                 Z_OtherInformation *oi = 0;
179                 int oi_len = v_len - lead_len;
180                 odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
181                 if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
182                 {
183                     yaz_log(YLOG_WARN, "oi decoding failed");
184                     free(v);
185                     return;
186                 }
187                 ZOOM_handle_search_result(c, resultset, oi);
188                 ZOOM_handle_facet_result(c, resultset, oi);
189             }
190             free(v);
191             event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
192             ZOOM_connection_put_event(c, event);
193             resultset->live_set = 1;
194         }
195     }
196 #endif
197 }
198
199 void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset,
200                              Z_OtherInformation *oi, const char *precision)
201 {
202 #if HAVE_LIBMEMCACHED_MEMCACHED_H
203     if (c->mc_st && resultset->live_set == 0)
204     {
205         uint32_t flags = 0;
206         memcached_return_t rc;
207         time_t expiration = 36000;
208         char *str;
209         ODR odr = odr_createmem(ODR_ENCODE);
210         char *oi_buf = 0;
211         int oi_len = 0;
212         char *key;
213
214         str = odr_malloc(odr, 20 + strlen(precision));
215         /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
216         sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
217         if (oi)
218         {
219             z_OtherInformation(odr, &oi, 0, 0);
220             oi_buf = odr_getbuf(odr, &oi_len, 0);
221         }
222         key = odr_malloc(odr, strlen(str) + 1 + oi_len);
223         strcpy(key, str);
224         if (oi_len)
225             memcpy(key + strlen(str) + 1, oi_buf, oi_len);
226
227         rc = memcached_set(c->mc_st,
228                            wrbuf_buf(resultset->mc_key),
229                            wrbuf_len(resultset->mc_key),
230                            key, strlen(str) + 1 + oi_len, expiration, flags);
231         yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
232                 wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
233                 memcached_strerror(c->mc_st, rc));
234         odr_destroy(odr);
235     }
236 #endif
237 }
238
239 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
240                         int pos,
241                         const char *syntax, const char *elementSetName,
242                         const char *schema,
243                         Z_SRW_diagnostic *diag)
244 {
245 #if HAVE_LIBMEMCACHED_MEMCACHED_H
246     if (r->connection->mc_st &&
247         !diag && npr->which == Z_NamePlusRecord_databaseRecord)
248     {
249         WRBUF k = wrbuf_alloc();
250         WRBUF rec_sha1 = wrbuf_alloc();
251         uint32_t flags = 0;
252         memcached_return_t rc;
253         time_t expiration = 36000;
254         ODR odr = odr_createmem(ODR_ENCODE);
255         char *rec_buf;
256         int rec_len;
257
258         z_NamePlusRecord(odr, &npr, 0, 0);
259         rec_buf = odr_getbuf(odr, &rec_len, 0);
260
261         wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
262         wrbuf_printf(k, ";%d;%s;%s;%s", pos,
263                      syntax ? syntax : "",
264                      elementSetName ? elementSetName : "",
265                      schema ? schema : "");
266
267         wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
268
269         rc = memcached_set(r->connection->mc_st,
270                            wrbuf_buf(k), wrbuf_len(k),
271                            wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
272                            expiration, flags);
273
274         yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
275                 wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
276                 memcached_strerror(r->connection->mc_st, rc));
277
278         rc = memcached_add(r->connection->mc_st,
279                            wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
280                            rec_buf, rec_len,
281                            expiration, flags);
282
283         yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
284                 wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
285                 memcached_strerror(r->connection->mc_st, rc));
286
287         odr_destroy(odr);
288         wrbuf_destroy(k);
289         wrbuf_destroy(rec_sha1);
290     }
291 #endif
292 }
293
294 Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
295                                         const char *syntax,
296                                         const char *elementSetName,
297                                         const char *schema)
298 {
299 #if HAVE_LIBMEMCACHED_MEMCACHED_H
300     if (r->connection && r->connection->mc_st)
301     {
302         WRBUF k = wrbuf_alloc();
303         char *sha1_buf;
304         size_t sha1_len;
305         uint32_t flags;
306         memcached_return_t rc;
307
308         wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
309         wrbuf_printf(k, ";%d;%s;%s;%s", pos,
310                      syntax ? syntax : "",
311                      elementSetName ? elementSetName : "",
312                      schema ? schema : "");
313
314         yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
315         sha1_buf = memcached_get(r->connection->mc_st,
316                                  wrbuf_buf(k), wrbuf_len(k),
317                                  &sha1_len, &flags, &rc);
318
319         wrbuf_destroy(k);
320         if (sha1_buf)
321         {
322             size_t v_len;
323             char *v_buf;
324
325             yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
326             v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
327                                   &v_len, &flags, &rc);
328             free(sha1_buf);
329             if (v_buf)
330             {
331                 Z_NamePlusRecord *npr = 0;
332
333                 odr_setbuf(r->odr, v_buf, v_len, 0);
334                 z_NamePlusRecord(r->odr, &npr, 0, 0);
335                 free(v_buf);
336                 if (npr)
337                     yaz_log(YLOG_LOG, "returned memcached copy");
338                 return npr;
339             }
340         }
341     }
342 #endif
343     return 0;
344
345 }
346 /*
347  * Local variables:
348  * c-basic-offset: 4
349  * c-file-style: "Stroustrup"
350  * indent-tabs-mode: nil
351  * End:
352  * vim: shiftwidth=4 tabstop=8 expandtab
353  */
354