Decoders optimizations
parent
a347285e99
commit
16741637e5
@ -1,18 +0,0 @@
|
||||
package org.redisson.client.protocol.decoder;
|
||||
|
||||
import org.redisson.client.protocol.Decoder;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
public class BooleanReplayDecoder implements Decoder<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean decode(ByteBuf buf) {
|
||||
if (buf == null) {
|
||||
return false;
|
||||
}
|
||||
return "OK".equals(buf.toString(CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package org.redisson.client.protocol.decoder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.lambdaworks.redis.output.MapScanResult;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
public class MapScanResultReplayDecoder2 implements MultiDecoder<MapScanResult<Object, Object>> {
|
||||
|
||||
public MultiDecoder<?> get() {
|
||||
return (MultiDecoder<?>) this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object decode(ByteBuf buf) {
|
||||
return Long.valueOf(buf.toString(CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MapScanResult<Object, Object> decode(List<Object> parts) {
|
||||
return new MapScanResult<Object, Object>((Long)parts.get(0), (Map<Object, Object>)parts.get(1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(int paramNum) {
|
||||
return paramNum == 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package org.redisson.client.protocol.decoder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class NestedMultiDecoder<T> implements MultiDecoder<Object> {
|
||||
|
||||
private final MultiDecoder<Object> firstDecoder;
|
||||
private final MultiDecoder<Object> secondDecoder;
|
||||
|
||||
private Deque<MultiDecoder<?>> iterator;
|
||||
private Deque<MultiDecoder<?>> flipIterator;
|
||||
|
||||
public NestedMultiDecoder(MultiDecoder<Object> firstDecoder, MultiDecoder<Object> secondDecoder) {
|
||||
this.firstDecoder = firstDecoder;
|
||||
this.secondDecoder = secondDecoder;
|
||||
|
||||
init(firstDecoder, secondDecoder);
|
||||
}
|
||||
|
||||
private void init(MultiDecoder<Object> firstDecoder, MultiDecoder<Object> secondDecoder) {
|
||||
iterator = new ArrayDeque<MultiDecoder<?>>(Arrays.asList(firstDecoder, secondDecoder));
|
||||
flipIterator = new ArrayDeque<MultiDecoder<?>>(Arrays.asList(firstDecoder, secondDecoder, firstDecoder));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object decode(ByteBuf buf) throws IOException {
|
||||
return flipIterator.peek().decode(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiDecoder<?> get() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(int paramNum) {
|
||||
if (paramNum == 0) {
|
||||
flipIterator.poll();
|
||||
if (flipIterator.isEmpty()) {
|
||||
init(firstDecoder, secondDecoder);
|
||||
flipIterator.poll();
|
||||
}
|
||||
}
|
||||
return flipIterator.peek().isApplicable(paramNum);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object decode(List<Object> parts) {
|
||||
return iterator.poll().decode(parts);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue