#include <yaz/log.h>
#include <yaz/diagbib1.h>
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+#if HAVE_MEMCACHED_RETURN_T
+#else
+typedef memcached_return memcached_return_t;
+#endif
+#endif
+
void ZOOM_memcached_init(ZOOM_connection c)
{
#if HAVE_LIBMEMCACHED_MEMCACHED_H
#endif
}
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+/* memcached wrapper.. Because memcached function do not exist in older libs */
+static memcached_st *yaz_memcached_wrap(const char *conf)
+{
+#if HAVE_MEMCACHED_FUNC
+ return memcached(conf, strlen(conf));
+#else
+ char **darray;
+ int i, num;
+ memcached_st *mc = memcached_create(0);
+ NMEM nmem = nmem_create();
+ memcached_return_t rc;
+
+ nmem_strsplit_blank(nmem, conf, &darray, &num);
+ for (i = 0; mc && i < num; i++)
+ {
+ if (!yaz_strncasecmp(darray[i], "--SERVER=", 9))
+ {
+ char *host = darray[i] + 9;
+ char *port = strchr(host, ':');
+ char *weight = strstr(host, "/?");
+ if (port)
+ *port++ = '\0';
+ if (weight)
+ {
+ *weight = '\0';
+ weight += 2;
+ }
+ rc = memcached_server_add(mc, host, port ? atoi(port) : 11211);
+ yaz_log(YLOG_LOG, "memcached_server_add host=%s rc=%u %s",
+ host, (unsigned) rc, memcached_strerror(mc, rc));
+ if (rc != MEMCACHED_SUCCESS)
+ {
+ memcached_free(mc);
+ mc = 0;
+ }
+ }
+ else
+ {
+ /* bad directive */
+ memcached_free(mc);
+ mc = 0;
+ }
+ }
+ nmem_destroy(nmem);
+ return mc;
+#endif
+}
+#endif
+
int ZOOM_memcached_configure(ZOOM_connection c)
{
const char *val;
if (val && *val)
{
#if HAVE_LIBMEMCACHED_MEMCACHED_H
- c->mc_st = memcached(val, strlen(val));
+ c->mc_st = yaz_memcached_wrap(val);
if (!c->mc_st)
{
- ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
+ ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED,
+ "could not create memcached");
return -1;
}
memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
return 0;
}
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+static void wrbuf_vary_puts(WRBUF w, const char *v)
+{
+ if (v)
+ {
+ if (strlen(v) > 40)
+ {
+ wrbuf_sha1_puts(w, v, 1);
+ }
+ else
+ {
+ wrbuf_puts(w, v);
+ }
+ }
+}
+#endif
+
void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
{
#if HAVE_LIBMEMCACHED_MEMCACHED_H
ZOOM_connection c = r->connection;
+
r->mc_key = wrbuf_alloc();
- wrbuf_puts(r->mc_key, "0;");
- wrbuf_puts(r->mc_key, c->host_port);
+ wrbuf_puts(r->mc_key, "1;");
+ wrbuf_vary_puts(r->mc_key, c->host_port);
+ wrbuf_puts(r->mc_key, ";");
+ wrbuf_vary_puts(r->mc_key, ZOOM_resultset_option_get(r, "extraArgs"));
wrbuf_puts(r->mc_key, ";");
- if (c->user)
- wrbuf_puts(r->mc_key, c->user);
+ wrbuf_vary_puts(r->mc_key, c->user);
wrbuf_puts(r->mc_key, ";");
- if (c->group)
- wrbuf_puts(r->mc_key, c->group);
+ wrbuf_vary_puts(r->mc_key, c->group);
wrbuf_puts(r->mc_key, ";");
if (c->password)
wrbuf_sha1_puts(r->mc_key, c->password, 1);
wrbuf_destroy(w);
}
wrbuf_puts(r->mc_key, ";");
- if (r->req_facets)
- wrbuf_puts(r->mc_key, r->req_facets);
+ wrbuf_vary_puts(r->mc_key, r->req_facets);
#endif
}
key, strlen(str) + 1 + oi_len, expiration, flags);
yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
- memcached_last_error_message(c->mc_st));
+ memcached_strerror(c->mc_st, rc));
odr_destroy(odr);
}
#endif
!diag && npr->which == Z_NamePlusRecord_databaseRecord)
{
WRBUF k = wrbuf_alloc();
+ WRBUF rec_sha1 = wrbuf_alloc();
uint32_t flags = 0;
memcached_return_t rc;
time_t expiration = 36000;
syntax ? syntax : "",
elementSetName ? elementSetName : "",
schema ? schema : "");
+
+ wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
+
rc = memcached_set(r->connection->mc_st,
- wrbuf_buf(k),wrbuf_len(k),
+ wrbuf_buf(k), wrbuf_len(k),
+ wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
+ expiration, flags);
+
+ yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
+ wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
+ memcached_strerror(r->connection->mc_st, rc));
+
+ rc = memcached_add(r->connection->mc_st,
+ wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
rec_buf, rec_len,
expiration, flags);
- yaz_log(YLOG_LOG, "Store record lkey=%s len=%d rc=%u %s",
- wrbuf_cstr(k), rec_len, (unsigned) rc,
- memcached_last_error_message(r->connection->mc_st));
+ yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
+ wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
+ memcached_strerror(r->connection->mc_st, rc));
+
odr_destroy(odr);
wrbuf_destroy(k);
+ wrbuf_destroy(rec_sha1);
}
#endif
}
if (r->connection && r->connection->mc_st)
{
WRBUF k = wrbuf_alloc();
- size_t v_len;
- char *v_buf;
+ char *sha1_buf;
+ size_t sha1_len;
uint32_t flags;
memcached_return_t rc;
schema ? schema : "");
yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
- v_buf = memcached_get(r->connection->mc_st, wrbuf_buf(k), wrbuf_len(k),
- &v_len, &flags, &rc);
+ sha1_buf = memcached_get(r->connection->mc_st,
+ wrbuf_buf(k), wrbuf_len(k),
+ &sha1_len, &flags, &rc);
+
wrbuf_destroy(k);
- if (v_buf)
+ if (sha1_buf)
{
- Z_NamePlusRecord *npr = 0;
-
- odr_setbuf(r->odr, v_buf, v_len, 0);
- z_NamePlusRecord(r->odr, &npr, 0, 0);
- free(v_buf);
- if (npr)
- yaz_log(YLOG_LOG, "returned memcached copy");
- return npr;
+ size_t v_len;
+ char *v_buf;
+
+ yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
+ v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
+ &v_len, &flags, &rc);
+ free(sha1_buf);
+ if (v_buf)
+ {
+ Z_NamePlusRecord *npr = 0;
+
+ odr_setbuf(r->odr, v_buf, v_len, 0);
+ z_NamePlusRecord(r->odr, &npr, 0, 0);
+ free(v_buf);
+ if (npr)
+ yaz_log(YLOG_LOG, "returned memcached copy");
+ return npr;
+ }
}
}
#endif