Permalink
Browse files

[codec-memcache] Simplify object hierachy and remove Headers.

This changeset removes the separate message headers and merges the
field directly into the messages. This greatly simplifies the
object hierachy and also saves header allocations in the pipeline.
  • Loading branch information...
daschl committed Feb 28, 2014
1 parent aad4082 commit 1fe0b53e292a860bbc415dca21fda8f729ca9a9e
Showing with 466 additions and 674 deletions.
  1. +16 −32 ...-memcache/src/main/java/io/netty/handler/codec/memcache/binary/AbstractBinaryMemcacheDecoder.java
  2. +5 −6 ...-memcache/src/main/java/io/netty/handler/codec/memcache/binary/AbstractBinaryMemcacheEncoder.java
  3. +118 −21 ...-memcache/src/main/java/io/netty/handler/codec/memcache/binary/AbstractBinaryMemcacheMessage.java
  4. +0 −119 ...che/src/main/java/io/netty/handler/codec/memcache/binary/AbstractBinaryMemcacheMessageHeader.java
  5. +140 −9 codec-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheMessage.java
  6. +0 −157 codec-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheMessageHeader.java
  7. +41 −12 ...memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheObjectAggregator.java
  8. +11 −5 codec-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheRequest.java
  9. +4 −13 ...c-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheRequestDecoder.java
  10. +11 −11 ...c-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheRequestEncoder.java
  11. +0 −43 codec-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheRequestHeader.java
  12. +11 −5 codec-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheResponse.java
  13. +4 −13 ...-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheResponseDecoder.java
  14. +11 −11 ...-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheResponseEncoder.java
  15. +0 −40 ...c-memcache/src/main/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheResponseHeader.java
  16. +28 −15 ...c-memcache/src/main/java/io/netty/handler/codec/memcache/binary/DefaultBinaryMemcacheRequest.java
  17. +0 −48 ...ache/src/main/java/io/netty/handler/codec/memcache/binary/DefaultBinaryMemcacheRequestHeader.java
  18. +28 −15 ...-memcache/src/main/java/io/netty/handler/codec/memcache/binary/DefaultBinaryMemcacheResponse.java
  19. +0 −48 ...che/src/main/java/io/netty/handler/codec/memcache/binary/DefaultBinaryMemcacheResponseHeader.java
  20. +6 −8 ...mcache/src/main/java/io/netty/handler/codec/memcache/binary/DefaultFullBinaryMemcacheRequest.java
  21. +7 −9 ...cache/src/main/java/io/netty/handler/codec/memcache/binary/DefaultFullBinaryMemcacheResponse.java
  22. +12 −17 codec-memcache/src/test/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheDecoderTest.java
  23. +13 −16 codec-memcache/src/test/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheEncoderTest.java
  24. +0 −1 ...ache/src/test/java/io/netty/handler/codec/memcache/binary/BinaryMemcacheObjectAggregatorTest.java
@@ -36,17 +36,14 @@
* <p/>
* The difference in the protocols (header) is implemented by the subclasses.
*/
-public abstract class AbstractBinaryMemcacheDecoder
- <M extends BinaryMemcacheMessage<H>, H extends BinaryMemcacheMessageHeader>
+public abstract class AbstractBinaryMemcacheDecoder<M extends BinaryMemcacheMessage>
extends AbstractMemcacheObjectDecoder {
public static final int DEFAULT_MAX_CHUNK_SIZE = 8192;
private final int chunkSize;
- private H currentHeader;
- private ByteBuf currentExtras;
- private String currentKey;
+ private M currentMessage;
private int alreadyReadChunkSize;
private State state = State.READ_HEADER;
@@ -80,20 +77,20 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
}
resetDecoder();
- currentHeader = decodeHeader(in);
+ currentMessage = decodeHeader(in);
state = State.READ_EXTRAS;
} catch (Exception e) {
out.add(invalidMessage(e));
return;
}
case READ_EXTRAS: try {
- byte extrasLength = currentHeader.getExtrasLength();
+ byte extrasLength = currentMessage.getExtrasLength();
if (extrasLength > 0) {
if (in.readableBytes() < extrasLength) {
return;
}
- currentExtras = readBytes(ctx.alloc(), in, extrasLength);
+ currentMessage.setExtras(readBytes(ctx.alloc(), in, extrasLength));
}
state = State.READ_KEY;
@@ -102,27 +99,26 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
return;
}
case READ_KEY: try {
- short keyLength = currentHeader.getKeyLength();
+ short keyLength = currentMessage.getKeyLength();
if (keyLength > 0) {
if (in.readableBytes() < keyLength) {
return;
}
- currentKey = in.toString(in.readerIndex(), keyLength, CharsetUtil.UTF_8);
+ currentMessage.setKey(in.toString(in.readerIndex(), keyLength, CharsetUtil.UTF_8));
in.skipBytes(keyLength);
}
- out.add(buildMessage(currentHeader, currentExtras, currentKey));
- currentExtras = null;
+ out.add(currentMessage);
state = State.READ_CONTENT;
} catch (Exception e) {
out.add(invalidMessage(e));
return;
}
case READ_CONTENT: try {
- int valueLength = currentHeader.getTotalBodyLength()
- - currentHeader.getKeyLength()
- - currentHeader.getExtrasLength();
+ int valueLength = currentMessage.getTotalBodyLength()
+ - currentMessage.getKeyLength()
+ - currentMessage.getExtrasLength();
int toRead = in.readableBytes();
if (valueLength > 0) {
if (toRead == 0) {
@@ -205,8 +201,8 @@ private MemcacheContent invalidChunk(Exception cause) {
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
- if (currentExtras != null) {
- currentExtras.release();
+ if (currentMessage.getExtras() != null) {
+ currentMessage.getExtras().release();
}
resetDecoder();
@@ -216,29 +212,17 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
* Prepare for next decoding iteration.
*/
protected void resetDecoder() {
- currentHeader = null;
- currentExtras = null;
- currentKey = null;
+ currentMessage = null;
alreadyReadChunkSize = 0;
}
/**
- * Decode and return the parsed {@link BinaryMemcacheMessageHeader}.
+ * Decode and return the parsed {@link BinaryMemcacheMessage}.
*
* @param in the incoming buffer.
* @return the decoded header.
*/
- protected abstract H decodeHeader(ByteBuf in);
-
- /**
- * Build the complete message, based on the information decoded.
- *
- * @param header the header of the message.
- * @param extras possible extras.
- * @param key possible key.
- * @return the decoded message.
- */
- protected abstract M buildMessage(H header, ByteBuf extras, String key);
+ protected abstract M decodeHeader(ByteBuf in);
/**
* Helper method to create a upstream message when the incoming parsing did fail.
@@ -24,8 +24,7 @@
/**
* A {@link MessageToByteEncoder} that encodes binary memache messages into bytes.
*/
-public abstract class AbstractBinaryMemcacheEncoder
- <M extends BinaryMemcacheMessage<H>, H extends BinaryMemcacheMessageHeader>
+public abstract class AbstractBinaryMemcacheEncoder<M extends BinaryMemcacheMessage>
extends AbstractMemcacheObjectEncoder<M> {
/**
@@ -37,7 +36,7 @@
protected ByteBuf encodeMessage(ChannelHandlerContext ctx, M msg) {
ByteBuf buf = ctx.alloc().buffer(DEFAULT_BUFFER_SIZE);
- encodeHeader(buf, msg.getHeader());
+ encodeHeader(buf, msg);
encodeExtras(buf, msg.getExtras());
encodeKey(buf, msg.getKey());
@@ -78,9 +77,9 @@ private static void encodeKey(ByteBuf buf, String key) {
* This methods needs to be implemented by a sub class because the header is different
* for both requests and responses.
*
- * @param buf the {@link ByteBuf} to write into.
- * @param header the header to encode.
+ * @param buf the {@link ByteBuf} to write into.
+ * @param msg the message to encode.
*/
- protected abstract void encodeHeader(ByteBuf buf, H header);
+ protected abstract void encodeHeader(ByteBuf buf, M msg);
}
@@ -21,43 +21,40 @@
/**
* Default implementation of a {@link BinaryMemcacheMessage}.
*/
-public abstract class AbstractBinaryMemcacheMessage<H extends BinaryMemcacheMessageHeader>
+public abstract class AbstractBinaryMemcacheMessage
extends AbstractMemcacheObject
- implements BinaryMemcacheMessage<H> {
-
- /**
- * Contains the message header.
- */
- private final H header;
+ implements BinaryMemcacheMessage {
/**
* Contains the optional key.
*/
- private final String key;
+ private String key;
/**
* Contains the optional extras.
*/
- private final ByteBuf extras;
+ private ByteBuf extras;
+
+ private byte magic;
+ private byte opcode;
+ private short keyLength;
+ private byte extrasLength;
+ private byte dataType;
+ private int totalBodyLength;
+ private int opaque;
+ private long cas;
/**
* Create a new instance with all properties set.
*
- * @param header the message header.
* @param key the message key.
* @param extras the message extras.
*/
- protected AbstractBinaryMemcacheMessage(H header, String key, ByteBuf extras) {
- this.header = header;
+ protected AbstractBinaryMemcacheMessage(String key, ByteBuf extras) {
this.key = key;
this.extras = extras;
}
- @Override
- public H getHeader() {
- return header;
- }
-
@Override
public String getKey() {
return key;
@@ -68,6 +65,106 @@ public ByteBuf getExtras() {
return extras;
}
+ @Override
+ public BinaryMemcacheMessage setKey(String key) {
+ this.key = key;
+ return this;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setExtras(ByteBuf extras) {
+ this.extras = extras;
+ return this;
+ }
+
+ @Override
+ public byte getMagic() {
+ return magic;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setMagic(byte magic) {
+ this.magic = magic;
+ return this;
+ }
+
+ @Override
+ public long getCAS() {
+ return cas;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setCAS(long cas) {
+ this.cas = cas;
+ return this;
+ }
+
+ @Override
+ public int getOpaque() {
+ return opaque;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setOpaque(int opaque) {
+ this.opaque = opaque;
+ return this;
+ }
+
+ @Override
+ public int getTotalBodyLength() {
+ return totalBodyLength;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setTotalBodyLength(int totalBodyLength) {
+ this.totalBodyLength = totalBodyLength;
+ return this;
+ }
+
+ @Override
+ public byte getDataType() {
+ return dataType;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setDataType(byte dataType) {
+ this.dataType = dataType;
+ return this;
+ }
+
+ @Override
+ public byte getExtrasLength() {
+ return extrasLength;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setExtrasLength(byte extrasLength) {
+ this.extrasLength = extrasLength;
+ return this;
+ }
+
+ @Override
+ public short getKeyLength() {
+ return keyLength;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setKeyLength(short keyLength) {
+ this.keyLength = keyLength;
+ return this;
+ }
+
+ @Override
+ public byte getOpcode() {
+ return opcode;
+ }
+
+ @Override
+ public BinaryMemcacheMessage setOpcode(byte opcode) {
+ this.opcode = opcode;
+ return this;
+ }
+
@Override
public int refCnt() {
if (extras != null) {
@@ -77,15 +174,15 @@ public int refCnt() {
}
@Override
- public BinaryMemcacheMessage<H> retain() {
+ public BinaryMemcacheMessage retain() {
if (extras != null) {
extras.retain();
}
return this;
}
@Override
- public BinaryMemcacheMessage<H> retain(int increment) {
+ public BinaryMemcacheMessage retain(int increment) {
if (extras != null) {
extras.retain(increment);
}
@@ -109,12 +206,12 @@ public boolean release(int decrement) {
}
@Override
- public BinaryMemcacheMessage<H> touch() {
+ public BinaryMemcacheMessage touch() {
return touch(null);
}
@Override
- public BinaryMemcacheMessage<H> touch(Object hint) {
+ public BinaryMemcacheMessage touch(Object hint) {
if (extras != null) {
extras.touch(hint);
}
Oops, something went wrong.

0 comments on commit 1fe0b53

Please sign in to comment.