[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.
This commit is contained in:
parent
aad4082d0f
commit
1fe0b53e29
@ -36,17 +36,14 @@ import static io.netty.buffer.ByteBufUtil.*;
|
||||
* <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 @@ public abstract class AbstractBinaryMemcacheDecoder
|
||||
}
|
||||
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 @@ public abstract class AbstractBinaryMemcacheDecoder
|
||||
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 @@ public abstract class AbstractBinaryMemcacheDecoder
|
||||
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 abstract class AbstractBinaryMemcacheDecoder
|
||||
* 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 @@ import io.netty.util.CharsetUtil;
|
||||
/**
|
||||
* 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 @@ public abstract class AbstractBinaryMemcacheEncoder
|
||||
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 @@ public abstract class AbstractBinaryMemcacheEncoder
|
||||
* 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 @@ import io.netty.handler.codec.memcache.AbstractMemcacheObject;
|
||||
/**
|
||||
* 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 abstract class AbstractBinaryMemcacheMessage<H extends BinaryMemcacheMess
|
||||
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,7 +174,7 @@ public abstract class AbstractBinaryMemcacheMessage<H extends BinaryMemcacheMess
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessage<H> retain() {
|
||||
public BinaryMemcacheMessage retain() {
|
||||
if (extras != null) {
|
||||
extras.retain();
|
||||
}
|
||||
@ -85,7 +182,7 @@ public abstract class AbstractBinaryMemcacheMessage<H extends BinaryMemcacheMess
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessage<H> retain(int increment) {
|
||||
public BinaryMemcacheMessage retain(int increment) {
|
||||
if (extras != null) {
|
||||
extras.retain(increment);
|
||||
}
|
||||
@ -109,12 +206,12 @@ public abstract class AbstractBinaryMemcacheMessage<H extends BinaryMemcacheMess
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
@ -1,119 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
/**
|
||||
* The default implementation of a {@link BinaryMemcacheMessageHeader}.
|
||||
*/
|
||||
public abstract class AbstractBinaryMemcacheMessageHeader implements BinaryMemcacheMessageHeader {
|
||||
|
||||
private byte magic;
|
||||
private byte opcode;
|
||||
private short keyLength;
|
||||
private byte extrasLength;
|
||||
private byte dataType;
|
||||
private int totalBodyLength;
|
||||
private int opaque;
|
||||
private long cas;
|
||||
|
||||
@Override
|
||||
public byte getMagic() {
|
||||
return magic;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setMagic(byte magic) {
|
||||
this.magic = magic;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCAS() {
|
||||
return cas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setCAS(long cas) {
|
||||
this.cas = cas;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpaque() {
|
||||
return opaque;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setOpaque(int opaque) {
|
||||
this.opaque = opaque;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalBodyLength() {
|
||||
return totalBodyLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setTotalBodyLength(int totalBodyLength) {
|
||||
this.totalBodyLength = totalBodyLength;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setDataType(byte dataType) {
|
||||
this.dataType = dataType;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getExtrasLength() {
|
||||
return extrasLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setExtrasLength(byte extrasLength) {
|
||||
this.extrasLength = extrasLength;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getKeyLength() {
|
||||
return keyLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setKeyLength(short keyLength) {
|
||||
this.keyLength = keyLength;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getOpcode() {
|
||||
return opcode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheMessageHeader setOpcode(byte opcode) {
|
||||
this.opcode = opcode;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -25,18 +25,135 @@ import io.netty.handler.codec.memcache.MemcacheMessage;
|
||||
* A {@link BinaryMemcacheMessage} always consists of a header and optional extras or/and
|
||||
* a key.
|
||||
*
|
||||
* @see BinaryMemcacheMessageHeader
|
||||
* @see BinaryMemcacheRequest
|
||||
* @see BinaryMemcacheResponse
|
||||
*/
|
||||
public interface BinaryMemcacheMessage<H extends BinaryMemcacheMessageHeader> extends MemcacheMessage {
|
||||
public interface BinaryMemcacheMessage extends MemcacheMessage {
|
||||
|
||||
/**
|
||||
* Returns the {@link BinaryMemcacheMessageHeader} which contains the full required header.
|
||||
* Returns the magic byte for the message.
|
||||
*
|
||||
* @return the required header.
|
||||
* @return the magic byte.
|
||||
*/
|
||||
H getHeader();
|
||||
byte getMagic();
|
||||
|
||||
/**
|
||||
* Sets the magic byte.
|
||||
*
|
||||
* @param magic the magic byte to use.
|
||||
* @see BinaryMemcacheOpcodes for typesafe opcodes.
|
||||
*/
|
||||
BinaryMemcacheMessage setMagic(byte magic);
|
||||
|
||||
/**
|
||||
* Returns the opcode for the message.
|
||||
*
|
||||
* @return the opcode.
|
||||
*/
|
||||
byte getOpcode();
|
||||
|
||||
/**
|
||||
* Sets the opcode for the message.
|
||||
*
|
||||
* @param code the opcode to use.
|
||||
*/
|
||||
BinaryMemcacheMessage setOpcode(byte code);
|
||||
|
||||
/**
|
||||
* Returns the key length of the message.
|
||||
* <p/>
|
||||
* This may return 0, since the key is optional.
|
||||
*
|
||||
* @return the key length.
|
||||
*/
|
||||
short getKeyLength();
|
||||
|
||||
/**
|
||||
* Set the key length of the message.
|
||||
* <p/>
|
||||
* This may be 0, since the key is optional.
|
||||
*
|
||||
* @param keyLength the key length to use.
|
||||
*/
|
||||
BinaryMemcacheMessage setKeyLength(short keyLength);
|
||||
|
||||
/**
|
||||
* Return the extras length of the message.
|
||||
* <p/>
|
||||
* This may be 0, since the extras content is optional.
|
||||
*
|
||||
* @return the extras length.
|
||||
*/
|
||||
byte getExtrasLength();
|
||||
|
||||
/**
|
||||
* Set the extras length of the message.
|
||||
* <p/>
|
||||
* This may be 0, since the extras content is optional.
|
||||
*
|
||||
* @param extrasLength the extras length.
|
||||
*/
|
||||
BinaryMemcacheMessage setExtrasLength(byte extrasLength);
|
||||
|
||||
/**
|
||||
* Returns the data type of the message.
|
||||
*
|
||||
* @return the data type of the message.
|
||||
*/
|
||||
byte getDataType();
|
||||
|
||||
/**
|
||||
* Sets the data type of the message.
|
||||
*
|
||||
* @param dataType the data type of the message.
|
||||
*/
|
||||
BinaryMemcacheMessage setDataType(byte dataType);
|
||||
|
||||
/**
|
||||
* Returns the total body length.
|
||||
* <p/>
|
||||
* Note that this may be 0, since the body is optional.
|
||||
*
|
||||
* @return the total body length.
|
||||
*/
|
||||
int getTotalBodyLength();
|
||||
|
||||
/**
|
||||
* Sets the total body length.
|
||||
* <p/>
|
||||
* Note that this may be 0, since the body length is optional.
|
||||
*
|
||||
* @param totalBodyLength the total body length.
|
||||
*/
|
||||
BinaryMemcacheMessage setTotalBodyLength(int totalBodyLength);
|
||||
|
||||
/**
|
||||
* Returns the opaque value.
|
||||
*
|
||||
* @return the opaque value.
|
||||
*/
|
||||
int getOpaque();
|
||||
|
||||
/**
|
||||
* Sets the opaque value.
|
||||
*
|
||||
* @param opaque the opqaue value to use.
|
||||
*/
|
||||
BinaryMemcacheMessage setOpaque(int opaque);
|
||||
|
||||
/**
|
||||
* Returns the CAS identifier.
|
||||
*
|
||||
* @return the CAS identifier.
|
||||
*/
|
||||
long getCAS();
|
||||
|
||||
/**
|
||||
* Sets the CAS identifier.
|
||||
*
|
||||
* @param cas the CAS identifier to use.
|
||||
*/
|
||||
BinaryMemcacheMessage setCAS(long cas);
|
||||
|
||||
/**
|
||||
* Returns the optional key of the document.
|
||||
@ -45,6 +162,13 @@ public interface BinaryMemcacheMessage<H extends BinaryMemcacheMessageHeader> ex
|
||||
*/
|
||||
String getKey();
|
||||
|
||||
/**
|
||||
* Sets the key of the document.
|
||||
*
|
||||
* @param key the key of the message.
|
||||
*/
|
||||
BinaryMemcacheMessage setKey(String key);
|
||||
|
||||
/**
|
||||
* Returns a {@link ByteBuf} representation of the optional extras.
|
||||
*
|
||||
@ -52,21 +176,28 @@ public interface BinaryMemcacheMessage<H extends BinaryMemcacheMessageHeader> ex
|
||||
*/
|
||||
ByteBuf getExtras();
|
||||
|
||||
/**
|
||||
* Sets the extras buffer on the message.
|
||||
*
|
||||
* @param extras the extras buffer of the document.
|
||||
*/
|
||||
BinaryMemcacheMessage setExtras(ByteBuf extras);
|
||||
|
||||
/**
|
||||
* Increases the reference count by {@code 1}.
|
||||
*/
|
||||
@Override
|
||||
BinaryMemcacheMessage<H> retain();
|
||||
BinaryMemcacheMessage retain();
|
||||
|
||||
/**
|
||||
* Increases the reference count by the specified {@code increment}.
|
||||
*/
|
||||
@Override
|
||||
BinaryMemcacheMessage<H> retain(int increment);
|
||||
BinaryMemcacheMessage retain(int increment);
|
||||
|
||||
@Override
|
||||
BinaryMemcacheMessage<H> touch();
|
||||
BinaryMemcacheMessage touch();
|
||||
|
||||
@Override
|
||||
BinaryMemcacheMessage<H> touch(Object hint);
|
||||
BinaryMemcacheMessage touch(Object hint);
|
||||
}
|
||||
|
@ -1,157 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
/**
|
||||
* Contains all common header fields in a {@link BinaryMemcacheMessage}.
|
||||
* <p/>
|
||||
* <p>Since the header is different for a {@link BinaryMemcacheRequest} and {@link BinaryMemcacheResponse}, see
|
||||
* {@link BinaryMemcacheRequestHeader} and {@link BinaryMemcacheResponseHeader}.</p>
|
||||
* <p/>
|
||||
* <p>The {@link BinaryMemcacheMessageHeader} is always 24 bytes in length and needs to be filled up if values are
|
||||
* not set.</p>
|
||||
* <p/>
|
||||
* <p>Fore more information, see the official protocol specification
|
||||
* <a href="https://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol">here</a>.</p>
|
||||
*/
|
||||
public interface BinaryMemcacheMessageHeader {
|
||||
|
||||
/**
|
||||
* Returns the magic byte for the message.
|
||||
*
|
||||
* @return the magic byte.
|
||||
*/
|
||||
byte getMagic();
|
||||
|
||||
/**
|
||||
* Sets the magic byte.
|
||||
*
|
||||
* @param magic the magic byte to use.
|
||||
* @see BinaryMemcacheOpcodes for typesafe opcodes.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setMagic(byte magic);
|
||||
|
||||
/**
|
||||
* Returns the opcode for the message.
|
||||
*
|
||||
* @return the opcode.
|
||||
*/
|
||||
byte getOpcode();
|
||||
|
||||
/**
|
||||
* Sets the opcode for the message.
|
||||
*
|
||||
* @param code the opcode to use.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setOpcode(byte code);
|
||||
|
||||
/**
|
||||
* Returns the key length of the message.
|
||||
* <p/>
|
||||
* This may return 0, since the key is optional.
|
||||
*
|
||||
* @return the key length.
|
||||
*/
|
||||
short getKeyLength();
|
||||
|
||||
/**
|
||||
* Set the key length of the message.
|
||||
* <p/>
|
||||
* This may be 0, since the key is optional.
|
||||
*
|
||||
* @param keyLength the key length to use.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setKeyLength(short keyLength);
|
||||
|
||||
/**
|
||||
* Return the extras length of the message.
|
||||
* <p/>
|
||||
* This may be 0, since the extras content is optional.
|
||||
*
|
||||
* @return the extras length.
|
||||
*/
|
||||
byte getExtrasLength();
|
||||
|
||||
/**
|
||||
* Set the extras length of the message.
|
||||
* <p/>
|
||||
* This may be 0, since the extras content is optional.
|
||||
*
|
||||
* @param extrasLength the extras length.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setExtrasLength(byte extrasLength);
|
||||
|
||||
/**
|
||||
* Returns the data type of the message.
|
||||
*
|
||||
* @return the data type of the message.
|
||||
*/
|
||||
byte getDataType();
|
||||
|
||||
/**
|
||||
* Sets the data type of the message.
|
||||
*
|
||||
* @param dataType the data type of the message.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setDataType(byte dataType);
|
||||
|
||||
/**
|
||||
* Returns the total body length.
|
||||
* <p/>
|
||||
* Note that this may be 0, since the body is optional.
|
||||
*
|
||||
* @return the total body length.
|
||||
*/
|
||||
int getTotalBodyLength();
|
||||
|
||||
/**
|
||||
* Sets the total body length.
|
||||
* <p/>
|
||||
* Note that this may be 0, since the body length is optional.
|
||||
*
|
||||
* @param totalBodyLength the total body length.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setTotalBodyLength(int totalBodyLength);
|
||||
|
||||
/**
|
||||
* Returns the opaque value.
|
||||
*
|
||||
* @return the opaque value.
|
||||
*/
|
||||
int getOpaque();
|
||||
|
||||
/**
|
||||
* Sets the opaque value.
|
||||
*
|
||||
* @param opaque the opqaue value to use.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setOpaque(int opaque);
|
||||
|
||||
/**
|
||||
* Returns the CAS identifier.
|
||||
*
|
||||
* @return the CAS identifier.
|
||||
*/
|
||||
long getCAS();
|
||||
|
||||
/**
|
||||
* Sets the CAS identifier.
|
||||
*
|
||||
* @param cas the CAS identifier to use.
|
||||
*/
|
||||
BinaryMemcacheMessageHeader setCAS(long cas);
|
||||
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.CompositeByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@ -59,13 +60,11 @@ public class BinaryMemcacheObjectAggregator extends AbstractMemcacheObjectAggreg
|
||||
}
|
||||
|
||||
if (msg instanceof BinaryMemcacheRequest) {
|
||||
BinaryMemcacheRequest request = (BinaryMemcacheRequest) msg;
|
||||
this.currentMessage = new DefaultFullBinaryMemcacheRequest(request.getHeader(), request.getKey(),
|
||||
request.getExtras(), Unpooled.compositeBuffer(getMaxCumulationBufferComponents()));
|
||||
this.currentMessage = toFullRequest((BinaryMemcacheRequest) msg,
|
||||
Unpooled.compositeBuffer(getMaxCumulationBufferComponents()));
|
||||
} else if (msg instanceof BinaryMemcacheResponse) {
|
||||
BinaryMemcacheResponse response = (BinaryMemcacheResponse) msg;
|
||||
this.currentMessage = new DefaultFullBinaryMemcacheResponse(response.getHeader(), response.getKey(),
|
||||
response.getExtras(), Unpooled.compositeBuffer(getMaxCumulationBufferComponents()));
|
||||
this.currentMessage = toFullResponse((BinaryMemcacheResponse) msg,
|
||||
Unpooled.compositeBuffer(getMaxCumulationBufferComponents()));
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
@ -130,13 +129,9 @@ public class BinaryMemcacheObjectAggregator extends AbstractMemcacheObjectAggreg
|
||||
|
||||
FullMemcacheMessage fullMsg;
|
||||
if (msg instanceof BinaryMemcacheRequest) {
|
||||
BinaryMemcacheRequest req = (BinaryMemcacheRequest) msg;
|
||||
fullMsg = new DefaultFullBinaryMemcacheRequest(req.getHeader(), req.getKey(), req.getExtras(),
|
||||
Unpooled.EMPTY_BUFFER);
|
||||
fullMsg = toFullRequest((BinaryMemcacheRequest) msg, Unpooled.EMPTY_BUFFER);
|
||||
} else if (msg instanceof BinaryMemcacheResponse) {
|
||||
BinaryMemcacheResponse res = (BinaryMemcacheResponse) msg;
|
||||
fullMsg = new DefaultFullBinaryMemcacheResponse(res.getHeader(), res.getKey(), res.getExtras(),
|
||||
Unpooled.EMPTY_BUFFER);
|
||||
fullMsg = toFullResponse((BinaryMemcacheResponse) msg, Unpooled.EMPTY_BUFFER);
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
@ -144,4 +139,38 @@ public class BinaryMemcacheObjectAggregator extends AbstractMemcacheObjectAggreg
|
||||
return fullMsg;
|
||||
}
|
||||
|
||||
private static FullBinaryMemcacheRequest toFullRequest(BinaryMemcacheRequest request, ByteBuf content) {
|
||||
FullBinaryMemcacheRequest fullRequest = new DefaultFullBinaryMemcacheRequest(request.getKey(),
|
||||
request.getExtras(), content);
|
||||
|
||||
fullRequest.setMagic(request.getMagic());
|
||||
fullRequest.setOpcode(request.getOpcode());
|
||||
fullRequest.setKeyLength(request.getKeyLength());
|
||||
fullRequest.setExtrasLength(request.getExtrasLength());
|
||||
fullRequest.setDataType(request.getDataType());
|
||||
fullRequest.setTotalBodyLength(request.getTotalBodyLength());
|
||||
fullRequest.setOpaque(request.getOpaque());
|
||||
fullRequest.setCAS(request.getCAS());
|
||||
fullRequest.setReserved(request.getReserved());
|
||||
|
||||
return fullRequest;
|
||||
}
|
||||
|
||||
private static FullBinaryMemcacheResponse toFullResponse(BinaryMemcacheResponse response, ByteBuf content) {
|
||||
FullBinaryMemcacheResponse fullResponse = new DefaultFullBinaryMemcacheResponse(response.getKey(),
|
||||
response.getExtras(), content);
|
||||
|
||||
fullResponse.setMagic(response.getMagic());
|
||||
fullResponse.setOpcode(response.getOpcode());
|
||||
fullResponse.setKeyLength(response.getKeyLength());
|
||||
fullResponse.setExtrasLength(response.getExtrasLength());
|
||||
fullResponse.setDataType(response.getDataType());
|
||||
fullResponse.setTotalBodyLength(response.getTotalBodyLength());
|
||||
fullResponse.setOpaque(response.getOpaque());
|
||||
fullResponse.setCAS(response.getCAS());
|
||||
fullResponse.setStatus(response.getStatus());
|
||||
|
||||
return fullResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,15 +18,21 @@ package io.netty.handler.codec.memcache.binary;
|
||||
/**
|
||||
* Represents a full {@link BinaryMemcacheRequest}, which contains the header and optional key and extras.
|
||||
*/
|
||||
public interface BinaryMemcacheRequest extends BinaryMemcacheMessage<BinaryMemcacheRequestHeader> {
|
||||
public interface BinaryMemcacheRequest extends BinaryMemcacheMessage {
|
||||
|
||||
/**
|
||||
* Returns the {@link BinaryMemcacheRequestHeader} which contains the full required request header.
|
||||
* Returns the reserved field value.
|
||||
*
|
||||
* @return the required request header.
|
||||
* @return the reserved field value.
|
||||
*/
|
||||
@Override
|
||||
BinaryMemcacheRequestHeader getHeader();
|
||||
short getReserved();
|
||||
|
||||
/**
|
||||
* Sets the reserved field value.
|
||||
*
|
||||
* @param reserved the reserved field value.
|
||||
*/
|
||||
BinaryMemcacheRequest setReserved(short reserved);
|
||||
|
||||
@Override
|
||||
BinaryMemcacheRequest retain();
|
||||
|
@ -22,7 +22,7 @@ import io.netty.buffer.Unpooled;
|
||||
* The decoder part which takes care of decoding the request-specific headers.
|
||||
*/
|
||||
public class BinaryMemcacheRequestDecoder
|
||||
extends AbstractBinaryMemcacheDecoder<BinaryMemcacheRequest, BinaryMemcacheRequestHeader> {
|
||||
extends AbstractBinaryMemcacheDecoder<BinaryMemcacheRequest> {
|
||||
|
||||
public BinaryMemcacheRequestDecoder() {
|
||||
this(DEFAULT_MAX_CHUNK_SIZE);
|
||||
@ -33,8 +33,8 @@ public class BinaryMemcacheRequestDecoder
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BinaryMemcacheRequestHeader decodeHeader(ByteBuf in) {
|
||||
BinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
|
||||
protected BinaryMemcacheRequest decodeHeader(ByteBuf in) {
|
||||
BinaryMemcacheRequest header = new DefaultBinaryMemcacheRequest();
|
||||
header.setMagic(in.readByte());
|
||||
header.setOpcode(in.readByte());
|
||||
header.setKeyLength(in.readShort());
|
||||
@ -47,17 +47,8 @@ public class BinaryMemcacheRequestDecoder
|
||||
return header;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BinaryMemcacheRequest buildMessage(BinaryMemcacheRequestHeader header, ByteBuf extras, String key) {
|
||||
return new DefaultBinaryMemcacheRequest(header, key, extras);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BinaryMemcacheRequest buildInvalidMessage() {
|
||||
return new DefaultBinaryMemcacheRequest(
|
||||
new DefaultBinaryMemcacheRequestHeader(),
|
||||
"",
|
||||
Unpooled.EMPTY_BUFFER
|
||||
);
|
||||
return new DefaultBinaryMemcacheRequest("", Unpooled.EMPTY_BUFFER);
|
||||
}
|
||||
}
|
||||
|
@ -21,19 +21,19 @@ import io.netty.buffer.ByteBuf;
|
||||
* The encoder part which takes care of encoding the request headers.
|
||||
*/
|
||||
public class BinaryMemcacheRequestEncoder
|
||||
extends AbstractBinaryMemcacheEncoder<BinaryMemcacheRequest, BinaryMemcacheRequestHeader> {
|
||||
extends AbstractBinaryMemcacheEncoder<BinaryMemcacheRequest> {
|
||||
|
||||
@Override
|
||||
protected void encodeHeader(ByteBuf buf, BinaryMemcacheRequestHeader header) {
|
||||
buf.writeByte(header.getMagic());
|
||||
buf.writeByte(header.getOpcode());
|
||||
buf.writeShort(header.getKeyLength());
|
||||
buf.writeByte(header.getExtrasLength());
|
||||
buf.writeByte(header.getDataType());
|
||||
buf.writeShort(header.getReserved());
|
||||
buf.writeInt(header.getTotalBodyLength());
|
||||
buf.writeInt(header.getOpaque());
|
||||
buf.writeLong(header.getCAS());
|
||||
protected void encodeHeader(ByteBuf buf, BinaryMemcacheRequest msg) {
|
||||
buf.writeByte(msg.getMagic());
|
||||
buf.writeByte(msg.getOpcode());
|
||||
buf.writeShort(msg.getKeyLength());
|
||||
buf.writeByte(msg.getExtrasLength());
|
||||
buf.writeByte(msg.getDataType());
|
||||
buf.writeShort(msg.getReserved());
|
||||
buf.writeInt(msg.getTotalBodyLength());
|
||||
buf.writeInt(msg.getOpaque());
|
||||
buf.writeLong(msg.getCAS());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
/**
|
||||
* Extends the common {@link BinaryMemcacheMessageHeader} header fields with hose who can only show up in
|
||||
* {@link BinaryMemcacheRequest} messages.
|
||||
* <p/>
|
||||
* <p>Note that while the additional field in the request is called "reserved", it can still be used for a custom
|
||||
* memcached implementation. It will not be mirrored back like the
|
||||
* {@link BinaryMemcacheRequestHeader#getOpaque()} field, because in the
|
||||
* {@link BinaryMemcacheResponseHeader}, the status field is set there instead.</p>
|
||||
*/
|
||||
public interface BinaryMemcacheRequestHeader extends BinaryMemcacheMessageHeader {
|
||||
|
||||
/**
|
||||
* Returns the reserved field value.
|
||||
*
|
||||
* @return the reserved field value.
|
||||
*/
|
||||
short getReserved();
|
||||
|
||||
/**
|
||||
* Sets the reserved field value.
|
||||
*
|
||||
* @param reserved the reserved field value.
|
||||
*/
|
||||
BinaryMemcacheRequestHeader setReserved(short reserved);
|
||||
|
||||
}
|
@ -18,15 +18,21 @@ package io.netty.handler.codec.memcache.binary;
|
||||
/**
|
||||
* Represents a full {@link BinaryMemcacheResponse}, which contains the header and optional key and extras.
|
||||
*/
|
||||
public interface BinaryMemcacheResponse extends BinaryMemcacheMessage<BinaryMemcacheResponseHeader> {
|
||||
public interface BinaryMemcacheResponse extends BinaryMemcacheMessage {
|
||||
|
||||
/**
|
||||
* Returns the {@link BinaryMemcacheResponseHeader} which contains the full required response header.
|
||||
* Returns the status of the response.
|
||||
*
|
||||
* @return the required response header.
|
||||
* @return the status of the response.
|
||||
*/
|
||||
@Override
|
||||
BinaryMemcacheResponseHeader getHeader();
|
||||
short getStatus();
|
||||
|
||||
/**
|
||||
* Sets the status of the response.
|
||||
*
|
||||
* @param status the status to set.
|
||||
*/
|
||||
BinaryMemcacheResponse setStatus(short status);
|
||||
|
||||
@Override
|
||||
BinaryMemcacheResponse retain();
|
||||
|
@ -22,7 +22,7 @@ import io.netty.buffer.Unpooled;
|
||||
* The decoder which takes care of decoding the response headers.
|
||||
*/
|
||||
public class BinaryMemcacheResponseDecoder
|
||||
extends AbstractBinaryMemcacheDecoder<BinaryMemcacheResponse, BinaryMemcacheResponseHeader> {
|
||||
extends AbstractBinaryMemcacheDecoder<BinaryMemcacheResponse> {
|
||||
|
||||
public BinaryMemcacheResponseDecoder() {
|
||||
this(DEFAULT_MAX_CHUNK_SIZE);
|
||||
@ -33,8 +33,8 @@ public class BinaryMemcacheResponseDecoder
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BinaryMemcacheResponseHeader decodeHeader(ByteBuf in) {
|
||||
BinaryMemcacheResponseHeader header = new DefaultBinaryMemcacheResponseHeader();
|
||||
protected BinaryMemcacheResponse decodeHeader(ByteBuf in) {
|
||||
BinaryMemcacheResponse header = new DefaultBinaryMemcacheResponse();
|
||||
header.setMagic(in.readByte());
|
||||
header.setOpcode(in.readByte());
|
||||
header.setKeyLength(in.readShort());
|
||||
@ -47,17 +47,8 @@ public class BinaryMemcacheResponseDecoder
|
||||
return header;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BinaryMemcacheResponse buildMessage(BinaryMemcacheResponseHeader header, ByteBuf extras, String key) {
|
||||
return new DefaultBinaryMemcacheResponse(header, key, extras);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BinaryMemcacheResponse buildInvalidMessage() {
|
||||
return new DefaultBinaryMemcacheResponse(
|
||||
new DefaultBinaryMemcacheResponseHeader(),
|
||||
"",
|
||||
Unpooled.EMPTY_BUFFER
|
||||
);
|
||||
return new DefaultBinaryMemcacheResponse("", Unpooled.EMPTY_BUFFER);
|
||||
}
|
||||
}
|
||||
|
@ -21,19 +21,19 @@ import io.netty.buffer.ByteBuf;
|
||||
* The encoder which takes care of encoding the response headers.
|
||||
*/
|
||||
public class BinaryMemcacheResponseEncoder
|
||||
extends AbstractBinaryMemcacheEncoder<BinaryMemcacheResponse, BinaryMemcacheResponseHeader> {
|
||||
extends AbstractBinaryMemcacheEncoder<BinaryMemcacheResponse> {
|
||||
|
||||
@Override
|
||||
protected void encodeHeader(ByteBuf buf, BinaryMemcacheResponseHeader header) {
|
||||
buf.writeByte(header.getMagic());
|
||||
buf.writeByte(header.getOpcode());
|
||||
buf.writeShort(header.getKeyLength());
|
||||
buf.writeByte(header.getExtrasLength());
|
||||
buf.writeByte(header.getDataType());
|
||||
buf.writeShort(header.getStatus());
|
||||
buf.writeInt(header.getTotalBodyLength());
|
||||
buf.writeInt(header.getOpaque());
|
||||
buf.writeLong(header.getCAS());
|
||||
protected void encodeHeader(ByteBuf buf, BinaryMemcacheResponse msg) {
|
||||
buf.writeByte(msg.getMagic());
|
||||
buf.writeByte(msg.getOpcode());
|
||||
buf.writeShort(msg.getKeyLength());
|
||||
buf.writeByte(msg.getExtrasLength());
|
||||
buf.writeByte(msg.getDataType());
|
||||
buf.writeShort(msg.getStatus());
|
||||
buf.writeInt(msg.getTotalBodyLength());
|
||||
buf.writeInt(msg.getOpaque());
|
||||
buf.writeLong(msg.getCAS());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
/**
|
||||
* Extends the common {@link BinaryMemcacheMessageHeader} header fields with hose who can only show up in
|
||||
* {@link BinaryMemcacheResponse} messages.
|
||||
*
|
||||
* @see BinaryMemcacheResponseStatus
|
||||
*/
|
||||
public interface BinaryMemcacheResponseHeader extends BinaryMemcacheMessageHeader {
|
||||
|
||||
/**
|
||||
* Returns the status of the response.
|
||||
*
|
||||
* @return the status of the response.
|
||||
*/
|
||||
short getStatus();
|
||||
|
||||
/**
|
||||
* Sets the status of the response.
|
||||
*
|
||||
* @param status the status to set.
|
||||
*/
|
||||
BinaryMemcacheResponseHeader setStatus(short status);
|
||||
|
||||
}
|
@ -20,47 +20,60 @@ import io.netty.buffer.ByteBuf;
|
||||
/**
|
||||
* The default implementation of the {@link BinaryMemcacheRequest}.
|
||||
*/
|
||||
public class DefaultBinaryMemcacheRequest extends AbstractBinaryMemcacheMessage<BinaryMemcacheRequestHeader>
|
||||
implements BinaryMemcacheRequest {
|
||||
public class DefaultBinaryMemcacheRequest extends AbstractBinaryMemcacheMessage implements BinaryMemcacheRequest {
|
||||
|
||||
/**
|
||||
* Default magic byte for a request.
|
||||
*/
|
||||
public static final byte REQUEST_MAGIC_BYTE = (byte) 0x80;
|
||||
|
||||
private short reserved;
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheRequest} with the header only.
|
||||
*
|
||||
* @param header the header to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheRequest(BinaryMemcacheRequestHeader header) {
|
||||
this(header, null, null);
|
||||
public DefaultBinaryMemcacheRequest() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheRequest} with the header and key.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheRequest(BinaryMemcacheRequestHeader header, String key) {
|
||||
this(header, key, null);
|
||||
public DefaultBinaryMemcacheRequest(String key) {
|
||||
this(key, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheRequest} with the header and extras.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param extras the extras to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheRequest(BinaryMemcacheRequestHeader header, ByteBuf extras) {
|
||||
this(header, null, extras);
|
||||
public DefaultBinaryMemcacheRequest(ByteBuf extras) {
|
||||
this(null, extras);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheRequest} with the header only.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
* @param extras the extras to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheRequest(BinaryMemcacheRequestHeader header, String key, ByteBuf extras) {
|
||||
super(header, key, extras);
|
||||
public DefaultBinaryMemcacheRequest(String key, ByteBuf extras) {
|
||||
super(key, extras);
|
||||
setMagic(REQUEST_MAGIC_BYTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getReserved() {
|
||||
return reserved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheRequest setReserved(short reserved) {
|
||||
this.reserved = reserved;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
/**
|
||||
* The default implementation of a {@link BinaryMemcacheRequestHeader}.
|
||||
*/
|
||||
public class DefaultBinaryMemcacheRequestHeader extends AbstractBinaryMemcacheMessageHeader
|
||||
implements BinaryMemcacheRequestHeader {
|
||||
|
||||
/**
|
||||
* Default magic byte for a request.
|
||||
*/
|
||||
public static final byte REQUEST_MAGIC_BYTE = (byte) 0x80;
|
||||
|
||||
private short reserved;
|
||||
|
||||
/**
|
||||
* Create a new {@link BinaryMemcacheRequestHeader} and apply default values.
|
||||
*/
|
||||
public DefaultBinaryMemcacheRequestHeader() {
|
||||
setMagic(REQUEST_MAGIC_BYTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getReserved() {
|
||||
return reserved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheRequestHeader setReserved(short reserved) {
|
||||
this.reserved = reserved;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -20,47 +20,60 @@ import io.netty.buffer.ByteBuf;
|
||||
/**
|
||||
* The default implementation of the {@link BinaryMemcacheResponse}.
|
||||
*/
|
||||
public class DefaultBinaryMemcacheResponse extends AbstractBinaryMemcacheMessage<BinaryMemcacheResponseHeader>
|
||||
implements BinaryMemcacheResponse {
|
||||
public class DefaultBinaryMemcacheResponse extends AbstractBinaryMemcacheMessage implements BinaryMemcacheResponse {
|
||||
|
||||
/**
|
||||
* Default magic byte for a request.
|
||||
*/
|
||||
public static final byte RESPONSE_MAGIC_BYTE = (byte) 0x81;
|
||||
|
||||
private short status;
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheResponse} with the header only.
|
||||
*
|
||||
* @param header the header to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheResponse(BinaryMemcacheResponseHeader header) {
|
||||
this(header, null, null);
|
||||
public DefaultBinaryMemcacheResponse() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheResponse} with the header and key.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use
|
||||
*/
|
||||
public DefaultBinaryMemcacheResponse(BinaryMemcacheResponseHeader header, String key) {
|
||||
this(header, key, null);
|
||||
public DefaultBinaryMemcacheResponse(String key) {
|
||||
this(key, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheResponse} with the header and extras.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param extras the extras to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheResponse(BinaryMemcacheResponseHeader header, ByteBuf extras) {
|
||||
this(header, null, extras);
|
||||
public DefaultBinaryMemcacheResponse(ByteBuf extras) {
|
||||
this(null, extras);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheResponse} with the header, key and extras.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
* @param extras the extras to use.
|
||||
*/
|
||||
public DefaultBinaryMemcacheResponse(BinaryMemcacheResponseHeader header, String key, ByteBuf extras) {
|
||||
super(header, key, extras);
|
||||
public DefaultBinaryMemcacheResponse(String key, ByteBuf extras) {
|
||||
super(key, extras);
|
||||
setMagic(RESPONSE_MAGIC_BYTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheResponse setStatus(short status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.memcache.binary;
|
||||
|
||||
/**
|
||||
* The default implementation of a {@link BinaryMemcacheResponseHeader}.
|
||||
*/
|
||||
public class DefaultBinaryMemcacheResponseHeader extends AbstractBinaryMemcacheMessageHeader
|
||||
implements BinaryMemcacheResponseHeader {
|
||||
|
||||
/**
|
||||
* Default magic byte for a request.
|
||||
*/
|
||||
public static final byte RESPONSE_MAGIC_BYTE = (byte) 0x81;
|
||||
|
||||
private short status;
|
||||
|
||||
/**
|
||||
* Create a new {@link BinaryMemcacheRequestHeader} and apply default values.
|
||||
*/
|
||||
public DefaultBinaryMemcacheResponseHeader() {
|
||||
setMagic(RESPONSE_MAGIC_BYTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryMemcacheResponseHeader setStatus(short status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -29,25 +29,23 @@ public class DefaultFullBinaryMemcacheRequest extends DefaultBinaryMemcacheReque
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheRequest} with the header, key and extras.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
* @param extras the extras to use.
|
||||
*/
|
||||
public DefaultFullBinaryMemcacheRequest(BinaryMemcacheRequestHeader header, String key, ByteBuf extras) {
|
||||
this(header, key, extras, Unpooled.buffer(0));
|
||||
public DefaultFullBinaryMemcacheRequest(String key, ByteBuf extras) {
|
||||
this(key, extras, Unpooled.buffer(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultBinaryMemcacheRequest} with the header, key, extras and content.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
* @param extras the extras to use.
|
||||
* @param content the content of the full request.
|
||||
*/
|
||||
public DefaultFullBinaryMemcacheRequest(BinaryMemcacheRequestHeader header, String key, ByteBuf extras,
|
||||
public DefaultFullBinaryMemcacheRequest(String key, ByteBuf extras,
|
||||
ByteBuf content) {
|
||||
super(header, key, extras);
|
||||
super(key, extras);
|
||||
if (content == null) {
|
||||
throw new NullPointerException("Supplied content is null.");
|
||||
}
|
||||
@ -101,11 +99,11 @@ public class DefaultFullBinaryMemcacheRequest extends DefaultBinaryMemcacheReque
|
||||
|
||||
@Override
|
||||
public FullBinaryMemcacheRequest copy() {
|
||||
return new DefaultFullBinaryMemcacheRequest(getHeader(), getKey(), getExtras(), content().copy());
|
||||
return new DefaultFullBinaryMemcacheRequest(getKey(), getExtras(), content().copy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public FullBinaryMemcacheRequest duplicate() {
|
||||
return new DefaultFullBinaryMemcacheRequest(getHeader(), getKey(), getExtras(), content().duplicate());
|
||||
return new DefaultFullBinaryMemcacheRequest(getKey(), getExtras(), content().duplicate());
|
||||
}
|
||||
}
|
||||
|
@ -29,25 +29,23 @@ public class DefaultFullBinaryMemcacheResponse extends DefaultBinaryMemcacheResp
|
||||
/**
|
||||
* Create a new {@link DefaultFullBinaryMemcacheResponse} with the header, key and extras.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
* @param extras the extras to use.
|
||||
*/
|
||||
public DefaultFullBinaryMemcacheResponse(BinaryMemcacheResponseHeader header, String key, ByteBuf extras) {
|
||||
this(header, key, extras, Unpooled.buffer(0));
|
||||
public DefaultFullBinaryMemcacheResponse(String key, ByteBuf extras) {
|
||||
this(key, extras, Unpooled.buffer(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultFullBinaryMemcacheResponse} with the header, key, extras and content.
|
||||
*
|
||||
* @param header the header to use.
|
||||
* @param key the key to use.
|
||||
* @param extras the extras to use.
|
||||
* @param content the content of the full request.
|
||||
*/
|
||||
public DefaultFullBinaryMemcacheResponse(BinaryMemcacheResponseHeader header, String key, ByteBuf extras,
|
||||
ByteBuf content) {
|
||||
super(header, key, extras);
|
||||
public DefaultFullBinaryMemcacheResponse(String key, ByteBuf extras,
|
||||
ByteBuf content) {
|
||||
super(key, extras);
|
||||
if (content == null) {
|
||||
throw new NullPointerException("Supplied content is null.");
|
||||
}
|
||||
@ -101,11 +99,11 @@ public class DefaultFullBinaryMemcacheResponse extends DefaultBinaryMemcacheResp
|
||||
|
||||
@Override
|
||||
public FullBinaryMemcacheResponse copy() {
|
||||
return new DefaultFullBinaryMemcacheResponse(getHeader(), getKey(), getExtras(), content().copy());
|
||||
return new DefaultFullBinaryMemcacheResponse(getKey(), getExtras(), content().copy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public FullBinaryMemcacheResponse duplicate() {
|
||||
return new DefaultFullBinaryMemcacheResponse(getHeader(), getKey(), getExtras(), content().duplicate());
|
||||
return new DefaultFullBinaryMemcacheResponse(getKey(), getExtras(), content().duplicate());
|
||||
}
|
||||
}
|
||||
|
@ -90,14 +90,12 @@ public class BinaryMemcacheDecoderTest {
|
||||
BinaryMemcacheRequest request = channel.readInbound();
|
||||
|
||||
assertThat(request, notNullValue());
|
||||
assertThat(request.getHeader(), notNullValue());
|
||||
assertThat(request.getKey(), notNullValue());
|
||||
assertThat(request.getExtras(), nullValue());
|
||||
|
||||
BinaryMemcacheRequestHeader header = request.getHeader();
|
||||
assertThat(header.getKeyLength(), is((short) 3));
|
||||
assertThat(header.getExtrasLength(), is((byte) 0));
|
||||
assertThat(header.getTotalBodyLength(), is(3));
|
||||
assertThat(request.getKeyLength(), is((short) 3));
|
||||
assertThat(request.getExtrasLength(), is((byte) 0));
|
||||
assertThat(request.getTotalBodyLength(), is(3));
|
||||
|
||||
request.release();
|
||||
assertThat(channel.readInbound(), instanceOf(LastMemcacheContent.class));
|
||||
@ -118,14 +116,12 @@ public class BinaryMemcacheDecoderTest {
|
||||
BinaryMemcacheRequest request = channel.readInbound();
|
||||
|
||||
assertThat(request, notNullValue());
|
||||
assertThat(request.getHeader(), notNullValue());
|
||||
assertThat(request.getKey(), notNullValue());
|
||||
assertThat(request.getExtras(), nullValue());
|
||||
|
||||
BinaryMemcacheRequestHeader header = request.getHeader();
|
||||
assertThat(header.getKeyLength(), is((short) 3));
|
||||
assertThat(header.getExtrasLength(), is((byte) 0));
|
||||
assertThat(header.getTotalBodyLength(), is(11));
|
||||
assertThat(request.getKeyLength(), is((short) 3));
|
||||
assertThat(request.getExtrasLength(), is((byte) 0));
|
||||
assertThat(request.getTotalBodyLength(), is(11));
|
||||
|
||||
request.release();
|
||||
|
||||
@ -157,7 +153,6 @@ public class BinaryMemcacheDecoderTest {
|
||||
BinaryMemcacheRequest request = channel.readInbound();
|
||||
|
||||
assertThat(request, notNullValue());
|
||||
assertThat(request.getHeader(), notNullValue());
|
||||
assertThat(request.getKey(), notNullValue());
|
||||
assertThat(request.getExtras(), nullValue());
|
||||
|
||||
@ -213,8 +208,8 @@ public class BinaryMemcacheDecoderTest {
|
||||
|
||||
// First message
|
||||
BinaryMemcacheResponse response = channel.readInbound();
|
||||
assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
|
||||
assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length()));
|
||||
assertThat(response.getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
|
||||
assertThat(response.getTotalBodyLength(), is(msgBody.length()));
|
||||
response.release();
|
||||
|
||||
// First message first content chunk
|
||||
@ -225,8 +220,8 @@ public class BinaryMemcacheDecoderTest {
|
||||
|
||||
// Second message
|
||||
response = channel.readInbound();
|
||||
assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
|
||||
assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length()));
|
||||
assertThat(response.getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
|
||||
assertThat(response.getTotalBodyLength(), is(msgBody.length()));
|
||||
response.release();
|
||||
|
||||
// Second message first content chunk
|
||||
@ -243,8 +238,8 @@ public class BinaryMemcacheDecoderTest {
|
||||
|
||||
// Third message
|
||||
response = channel.readInbound();
|
||||
assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
|
||||
assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length()));
|
||||
assertThat(response.getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
|
||||
assertThat(response.getTotalBodyLength(), is(msgBody.length()));
|
||||
response.release();
|
||||
|
||||
// Third message first content chunk
|
||||
|
@ -51,8 +51,7 @@ public class BinaryMemcacheEncoderTest {
|
||||
|
||||
@Test
|
||||
public void shouldEncodeDefaultHeader() {
|
||||
BinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(header);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest();
|
||||
|
||||
boolean result = channel.writeOutbound(request);
|
||||
assertThat(result, is(true));
|
||||
@ -66,10 +65,9 @@ public class BinaryMemcacheEncoderTest {
|
||||
|
||||
@Test
|
||||
public void shouldEncodeCustomHeader() {
|
||||
BinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
|
||||
header.setMagic((byte) 0xAA);
|
||||
header.setOpcode(BinaryMemcacheOpcodes.GET);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(header);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest();
|
||||
request.setMagic((byte) 0xAA);
|
||||
request.setOpcode(BinaryMemcacheOpcodes.GET);
|
||||
|
||||
boolean result = channel.writeOutbound(request);
|
||||
assertThat(result, is(true));
|
||||
@ -86,9 +84,9 @@ public class BinaryMemcacheEncoderTest {
|
||||
String extrasContent = "netty<3memcache";
|
||||
ByteBuf extras = Unpooled.copiedBuffer(extrasContent, CharsetUtil.UTF_8);
|
||||
int extrasLength = extras.readableBytes();
|
||||
BinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
|
||||
header.setExtrasLength((byte) extrasLength);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(header, extras);
|
||||
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(extras);
|
||||
request.setExtrasLength((byte) extrasLength);
|
||||
|
||||
boolean result = channel.writeOutbound(request);
|
||||
assertThat(result, is(true));
|
||||
@ -104,9 +102,9 @@ public class BinaryMemcacheEncoderTest {
|
||||
public void shouldEncodeKey() {
|
||||
String key = "netty";
|
||||
int keyLength = key.length();
|
||||
BinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
|
||||
header.setKeyLength((byte) keyLength);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(header, key);
|
||||
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key);
|
||||
request.setKeyLength((byte) keyLength);
|
||||
|
||||
boolean result = channel.writeOutbound(request);
|
||||
assertThat(result, is(true));
|
||||
@ -126,9 +124,8 @@ public class BinaryMemcacheEncoderTest {
|
||||
new DefaultLastMemcacheContent(Unpooled.copiedBuffer(" Rocks!", CharsetUtil.UTF_8));
|
||||
int totalBodyLength = content1.content().readableBytes() + content2.content().readableBytes();
|
||||
|
||||
BinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
|
||||
header.setTotalBodyLength(totalBodyLength);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(header);
|
||||
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest();
|
||||
request.setTotalBodyLength(totalBodyLength);
|
||||
|
||||
boolean result = channel.writeOutbound(request);
|
||||
assertThat(result, is(true));
|
||||
@ -159,6 +156,6 @@ public class BinaryMemcacheEncoderTest {
|
||||
@Test(expected = EncoderException.class)
|
||||
public void shouldFailWithoutLastContent() {
|
||||
channel.writeOutbound(new DefaultMemcacheContent(Unpooled.EMPTY_BUFFER));
|
||||
channel.writeOutbound(new DefaultBinaryMemcacheRequest(new DefaultBinaryMemcacheRequestHeader()));
|
||||
channel.writeOutbound(new DefaultBinaryMemcacheRequest());
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,6 @@ public class BinaryMemcacheObjectAggregatorTest {
|
||||
|
||||
assertThat(request, instanceOf(FullBinaryMemcacheRequest.class));
|
||||
assertThat(request, notNullValue());
|
||||
assertThat(request.getHeader(), notNullValue());
|
||||
assertThat(request.getKey(), notNullValue());
|
||||
assertThat(request.getExtras(), nullValue());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user