From 17cc4411afa7b2b2c3a163bf4725b56e4e04f117 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 1 Jan 2016 17:07:19 +1100 Subject: [PATCH] Fix compilation and send lz4 compatible clients compressed data --- configure.ac | 2 +- src/connector.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index a805be4d..b99bb405 100644 --- a/configure.ac +++ b/configure.ac @@ -55,7 +55,7 @@ AC_ARG_WITH([ckdb], #AC_SEARCH_LIBS(whatgoeshere?, rt, , echo "Error: Required library realtime not found." && exit 1) AC_SEARCH_LIBS(exp, m, , echo "Error: Required library math not found." && exit 1) AC_SEARCH_LIBS(LZ4_compress, lz4, , echo "Error: Required library liblz4-dev not found." && exit 1) -AC_SEARCH_LIBS(pthread_create, pthread, , "Error: Required library pthreads not found." && exit 1) +AC_SEARCH_LIBS(pthread_mutex_trylock, pthread, , "Error: Required library pthreads not found." && exit 1) if test "x$ckdb" != "xno"; then AC_CHECK_LIB([pq], [main],[PQ=-lpq],echo "Error: Required library libpq-dev diff --git a/src/connector.c b/src/connector.c index f2858a59..e550c9f8 100644 --- a/src/connector.c +++ b/src/connector.c @@ -1000,6 +1000,37 @@ static void send_client(cdata_t *cdata, const int64_t id, char *buf) test_redirector_shares(ckp, client, buf); } + /* Does this client accept compressed data? */ + if (client->lz4) { + char *dest = ckalloc(len + 12); + uint32_t msglen; + int compsize; + + compsize = LZ4_compress(buf, dest + 12, len); + if (unlikely(!compsize)) { + LOGWARNING("Failed to LZ4 compress in send_client, sending uncompressed"); + free(dest); + goto out; + } + if (compsize + 12 >= len) { + /* Only end it compressed if it's smaller */ + free(dest); + goto out; + } + LOGDEBUG("Sending client message compressed %d from %d", compsize, len); + /* Copy lz4 magic header */ + sprintf(dest, "lz4\n"); + /* Copy compressed message length */ + msglen = htole32(compsize); + memcpy(dest + 4, &msglen, 4); + /* Copy decompressed message length */ + msglen = htole32(len); + memcpy(dest + 8, &msglen, 4); + len = compsize + 12; + free(buf); + buf = dest; + } +out: sender_send = ckzalloc(sizeof(sender_send_t)); sender_send->client = client; sender_send->buf = buf;