Created
July 8, 2013 22:05
-
-
Save ingenthr/5952891 to your computer and use it in GitHub Desktop.
An untested example of how to set CouchbaseClient with a transccoder that will try to compress JSON.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2013 Couchbase, Inc. | |
* | |
*/ | |
package com.couchbase.demo; | |
import java.util.Date; | |
import net.spy.memcached.CachedData; | |
import net.spy.memcached.transcoders.BaseSerializingTranscoder; | |
import net.spy.memcached.transcoders.Transcoder; | |
import net.spy.memcached.transcoders.TranscoderUtils; | |
import net.spy.memcached.util.StringUtils; | |
/** | |
* Transcoder that serializes and compresses objects. | |
*/ | |
public class CompressingJsonTranscoder extends BaseSerializingTranscoder implements | |
Transcoder<Object> { | |
// General flags | |
static final int SERIALIZED = 1; | |
static final int COMPRESSED = 2; | |
// Special flags for specially handled types. | |
private static final int SPECIAL_MASK = 0xff00; | |
static final int SPECIAL_BOOLEAN = (1 << 8); | |
static final int SPECIAL_INT = (2 << 8); | |
static final int SPECIAL_LONG = (3 << 8); | |
static final int SPECIAL_DATE = (4 << 8); | |
static final int SPECIAL_BYTE = (5 << 8); | |
static final int SPECIAL_FLOAT = (6 << 8); | |
static final int SPECIAL_DOUBLE = (7 << 8); | |
static final int SPECIAL_BYTEARRAY = (8 << 8); | |
private final TranscoderUtils tu = new TranscoderUtils(true); | |
/** | |
* Get a serializing transcoder with the default max data size. | |
*/ | |
public CompressingJsonTranscoder() { | |
this(CachedData.MAX_SIZE); | |
} | |
/** | |
* Get a serializing transcoder that specifies the max data size. | |
*/ | |
public CompressingJsonTranscoder(int max) { | |
super(max); | |
} | |
@Override | |
public boolean asyncDecode(CachedData d) { | |
if ((d.getFlags() & COMPRESSED) != 0 || (d.getFlags() & SERIALIZED) != 0) { | |
return true; | |
} | |
return super.asyncDecode(d); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see net.spy.memcached.Transcoder#decode(net.spy.memcached.CachedData) | |
*/ | |
public Object decode(CachedData d) { | |
byte[] data = d.getData(); | |
Object rv = null; | |
if ((d.getFlags() & COMPRESSED) != 0) { | |
data = decompress(d.getData()); | |
} | |
int flags = d.getFlags() & SPECIAL_MASK; | |
if ((d.getFlags() & SERIALIZED) != 0 && data != null) { | |
rv = deserialize(data); | |
} else if (flags != 0 && data != null) { | |
switch (flags) { | |
case SPECIAL_BOOLEAN: | |
rv = Boolean.valueOf(tu.decodeBoolean(data)); | |
break; | |
case SPECIAL_INT: | |
rv = Integer.valueOf(tu.decodeInt(data)); | |
break; | |
case SPECIAL_LONG: | |
rv = Long.valueOf(tu.decodeLong(data)); | |
break; | |
case SPECIAL_DATE: | |
rv = new Date(tu.decodeLong(data)); | |
break; | |
case SPECIAL_BYTE: | |
rv = Byte.valueOf(tu.decodeByte(data)); | |
break; | |
case SPECIAL_FLOAT: | |
rv = new Float(Float.intBitsToFloat(tu.decodeInt(data))); | |
break; | |
case SPECIAL_DOUBLE: | |
rv = new Double(Double.longBitsToDouble(tu.decodeLong(data))); | |
break; | |
case SPECIAL_BYTEARRAY: | |
rv = data; | |
break; | |
default: | |
getLogger().warn("Undecodeable with flags %x", flags); | |
} | |
} else { | |
rv = decodeString(data); | |
} | |
return rv; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see net.spy.memcached.Transcoder#encode(java.lang.Object) | |
*/ | |
public CachedData encode(Object o) { | |
byte[] b = null; | |
int flags = 0; | |
if (o instanceof String) { | |
b = encodeString((String) o); | |
} else if (o instanceof Long) { | |
b = tu.encodeLong((Long) o); | |
flags |= SPECIAL_LONG; | |
} else if (o instanceof Integer) { | |
b = tu.encodeInt((Integer) o); | |
flags |= SPECIAL_INT; | |
} else if (o instanceof Boolean) { | |
b = tu.encodeBoolean((Boolean) o); | |
flags |= SPECIAL_BOOLEAN; | |
} else if (o instanceof Date) { | |
b = tu.encodeLong(((Date) o).getTime()); | |
flags |= SPECIAL_DATE; | |
} else if (o instanceof Byte) { | |
b = tu.encodeByte((Byte) o); | |
flags |= SPECIAL_BYTE; | |
} else if (o instanceof Float) { | |
b = tu.encodeInt(Float.floatToRawIntBits((Float) o)); | |
flags |= SPECIAL_FLOAT; | |
} else if (o instanceof Double) { | |
b = tu.encodeLong(Double.doubleToRawLongBits((Double) o)); | |
flags |= SPECIAL_DOUBLE; | |
} else if (o instanceof byte[]) { | |
b = (byte[]) o; | |
flags |= SPECIAL_BYTEARRAY; | |
} else { | |
b = serialize(o); | |
flags |= SERIALIZED; | |
} | |
assert b != null; | |
if (b.length > compressionThreshold) { | |
byte[] compressed = compress(b); | |
if (compressed.length < b.length) { | |
getLogger().debug("Compressed %s from %d to %d", | |
o.getClass().getName(), b.length, compressed.length); | |
b = compressed; | |
flags |= COMPRESSED; | |
} else { | |
getLogger().info("Compression increased the size of %s from %d to %d", | |
o.getClass().getName(), b.length, compressed.length); | |
} | |
} | |
return new CachedData(flags, b, getMaxSize()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.couchbase.demo; | |
import com.couchbase.client.CouchbaseClient; | |
import com.couchbase.client.CouchbaseConnectionFactory; | |
import com.couchbase.client.CouchbaseConnectionFactoryBuilder; | |
import net.spy.memcached.CachedData; | |
import net.spy.memcached.transcoders.SerializingTranscoder; | |
import net.spy.memcached.transcoders.Transcoder; | |
import net.spy.memcached.util.StringUtils; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: ingenthr | |
* Date: 7/8/13 | |
* Time: 2:31 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class Main { | |
public static void main(String args[]) throws IOException { | |
new Main().run(); | |
} | |
private void run() throws IOException { | |
Transcoder noCompress = new CompressingJsonTranscoder(); | |
List<URI> baseList = Arrays.asList( | |
URI.create("http://127.0.0.1:8091/pools")); | |
CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder(); | |
cfb.setTranscoder(noCompress); | |
CouchbaseConnectionFactory couchbaseConnectionFactory = cfb.buildCouchbaseConnection(baseList, "default", ""); | |
CouchbaseClient client = new CouchbaseClient(couchbaseConnectionFactory); | |
client.set("foo", "bar"); | |
client.get("foo"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment