new commands added

pull/243/head
Nikita 10 years ago
parent 3204255277
commit a50a2f0154

@ -172,7 +172,10 @@ public class RedisDecoder extends ReplayingDecoder<Void> {
Decoder<Object> decoder = data.getCommand().getReplayDecoder(); Decoder<Object> decoder = data.getCommand().getReplayDecoder();
if (parts != null) { if (parts != null) {
decoder = data.getCommand().getReplayMultiDecoder(); MultiDecoder<Object> multiDecoder = data.getCommand().getReplayMultiDecoder();
if (multiDecoder.isApplicable(parts.size())) {
decoder = multiDecoder;
}
} }
if (decoder == null) { if (decoder == null) {
decoder = data.getCodec(); decoder = data.getCodec();

@ -0,0 +1,44 @@
/**
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
*
* Licensed 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 org.redisson.client.protocol;
import java.io.UnsupportedEncodingException;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
public class IntegerCodec implements Codec {
public static final IntegerCodec INSTANCE = new IntegerCodec();
@Override
public byte[] encode(int paramIndex, Object in) {
try {
return in.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
@Override
public Object decode(ByteBuf buf) {
if (buf == null) {
return null;
}
return Integer.valueOf(buf.toString(CharsetUtil.UTF_8));
}
}

@ -0,0 +1,35 @@
package org.redisson.client.protocol;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
public class JsonCodec implements Codec {
public static final JsonCodec INSTANCE = new JsonCodec();
private final ObjectMapper mapper = new ObjectMapper();
@Override
public byte[] encode(int paramIndex, Object in) {
try {
return mapper.writeValueAsBytes(in);
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
}
@Override
public Object decode(ByteBuf buf) {
try {
return mapper.readValue(new ByteBufInputStream(buf), Object.class);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

@ -0,0 +1,22 @@
package org.redisson.client.protocol;
public class KeyValueMessage<K, V> {
private K key;
private V value;
public KeyValueMessage(K key, V value) {
super();
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}

@ -0,0 +1,29 @@
package org.redisson.client.protocol;
import java.util.List;
import org.redisson.client.protocol.pubsub.MultiDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
public class KeyValueObjectDecoder implements MultiDecoder<Object> {
@Override
public Object decode(ByteBuf buf) {
String status = buf.toString(CharsetUtil.UTF_8);
buf.skipBytes(2);
return status;
}
@Override
public Object decode(List<Object> parts) {
return new KeyValueMessage(parts.get(0), parts.get(1));
}
@Override
public boolean isApplicable(int paramNum) {
return paramNum == 0;
}
}

@ -19,8 +19,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.redisson.client.protocol.decoder.BooleanReplayDecoder; import org.redisson.client.protocol.decoder.BooleanReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectListReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder;
import org.redisson.client.protocol.decoder.StringDataDecoder; import org.redisson.client.protocol.decoder.StringDataDecoder;
import org.redisson.client.protocol.decoder.StringListObjectReplayDecoder;
import org.redisson.client.protocol.decoder.StringListReplayDecoder; import org.redisson.client.protocol.decoder.StringListReplayDecoder;
import org.redisson.client.protocol.decoder.StringMapReplayDecoder; import org.redisson.client.protocol.decoder.StringMapReplayDecoder;
import org.redisson.client.protocol.decoder.StringReplayDecoder; import org.redisson.client.protocol.decoder.StringReplayDecoder;
@ -29,8 +30,24 @@ import org.redisson.client.protocol.pubsub.PubSubStatusMessage;
public interface RedisCommands { public interface RedisCommands {
RedisCommand<Object> LPOP = new RedisCommand<Object>("LPOP");
RedisCommand<Object> LINDEX = new RedisCommand<Object>("LINDEX");
RedisStrictCommand<Long> LLEN = new RedisStrictCommand<Long>("LLEN");
RedisStrictCommand<Boolean> EXPIRE = new RedisStrictCommand<Boolean>("EXPIRE", new BooleanReplayConvertor());
RedisStrictCommand<Boolean> EXPIREAT = new RedisStrictCommand<Boolean>("EXPIREAT", new BooleanReplayConvertor());
RedisStrictCommand<Boolean> PERSIST = new RedisStrictCommand<Boolean>("PERSIST", new BooleanReplayConvertor());
RedisStrictCommand<Long> TTL = new RedisStrictCommand<Long>("TTL");
RedisCommand<Object> BRPOPLPUSH = new RedisCommand<Object>("BRPOPLPUSH");
RedisCommand<Object> BLPOP = new RedisCommand<Object>("BLPOP", new KeyValueObjectDecoder());
RedisCommand<Long> RPUSH = new RedisCommand<Long>("RPUSH");
RedisStrictCommand<Boolean> EVAL_BOOLEAN = new RedisStrictCommand<Boolean>("EVAL", new BooleanReplayConvertor()); RedisStrictCommand<Boolean> EVAL_BOOLEAN = new RedisStrictCommand<Boolean>("EVAL", new BooleanReplayConvertor());
RedisStrictCommand<Long> EVAL_INTEGER = new RedisStrictCommand<Long>("EVAL"); RedisStrictCommand<Long> EVAL_INTEGER = new RedisStrictCommand<Long>("EVAL");
RedisCommand<List<Object>> EVAL_LIST = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder());
RedisCommand<Object> EVAL_OBJECT = new RedisCommand<Object>("EVAL");
RedisStrictCommand<Long> INCR = new RedisStrictCommand<Long>("INCR"); RedisStrictCommand<Long> INCR = new RedisStrictCommand<Long>("INCR");
RedisStrictCommand<Long> INCRBY = new RedisStrictCommand<Long>("INCRBY"); RedisStrictCommand<Long> INCRBY = new RedisStrictCommand<Long>("INCRBY");
@ -44,10 +61,16 @@ public interface RedisCommands {
RedisStrictCommand<List<String>> KEYS = new RedisStrictCommand<List<String>>("KEYS", new StringListReplayDecoder()); RedisStrictCommand<List<String>> KEYS = new RedisStrictCommand<List<String>>("KEYS", new StringListReplayDecoder());
RedisCommand<Map<Object, Object>> HGETALL = new RedisCommand<Map<Object, Object>>("HGETALL", new ObjectMapReplayDecoder());
RedisCommand<List<Object>> HVALS = new RedisCommand<List<Object>>("HVALS", new ObjectListReplayDecoder());
RedisCommand<Boolean> HEXISTS = new RedisCommand<Boolean>("HEXISTS", new BooleanReplayConvertor(), 1);
RedisStrictCommand<Long> HLEN = new RedisStrictCommand<Long>("HLEN");
RedisCommand<List<Object>> HKEYS = new RedisCommand<List<Object>>("HKEYS", new ObjectListReplayDecoder());
RedisCommand<String> HMSET = new RedisCommand<String>("HMSET", new StringReplayDecoder(), 2, 3); RedisCommand<String> HMSET = new RedisCommand<String>("HMSET", new StringReplayDecoder(), 2, 3);
RedisCommand<Object> HMGET = new RedisCommand<Object>("HMGET", new StringListObjectReplayDecoder(), 2, 3); RedisCommand<List<Object>> HMGET = new RedisCommand<List<Object>>("HMGET", new ObjectListReplayDecoder());
RedisCommand<Object> HGET = new RedisCommand<Object>("HMGET", 2);
RedisStrictCommand<Boolean> DEL_ONE = new RedisStrictCommand<Boolean>("DEL", new BooleanReplayConvertor()); RedisStrictCommand<Boolean> DEL_BOOLEAN = new RedisStrictCommand<Boolean>("DEL", new BooleanReplayConvertor());
RedisCommand<Object> GET = new RedisCommand<Object>("GET"); RedisCommand<Object> GET = new RedisCommand<Object>("GET");
RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder(), 1); RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder(), 1);

@ -0,0 +1,26 @@
package org.redisson.client.protocol.decoder;
import java.util.List;
import org.redisson.client.protocol.pubsub.MultiDecoder;
import io.netty.buffer.ByteBuf;
public class ObjectListReplayDecoder implements MultiDecoder<List<Object>> {
@Override
public Object decode(ByteBuf buf) {
throw new UnsupportedOperationException();
}
@Override
public List<Object> decode(List<Object> parts) {
return parts;
}
@Override
public boolean isApplicable(int paramNum) {
return false;
}
}

@ -0,0 +1,34 @@
package org.redisson.client.protocol.decoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.redisson.client.protocol.pubsub.MultiDecoder;
import io.netty.buffer.ByteBuf;
public class ObjectMapReplayDecoder implements MultiDecoder<Map<Object, Object>> {
@Override
public Object decode(ByteBuf buf) {
throw new UnsupportedOperationException();
}
@Override
public Map<Object, Object> decode(List<Object> parts) {
Map<Object, Object> result = new HashMap<Object, Object>(parts.size()/2);
for (int i = 0; i < parts.size(); i++) {
if (i % 2 != 0) {
result.put(parts.get(i-1).toString(), parts.get(i).toString());
}
}
return result;
}
@Override
public boolean isApplicable(int paramNum) {
return false;
}
}

@ -1,22 +0,0 @@
package org.redisson.client.protocol.decoder;
import java.util.List;
import org.redisson.client.protocol.pubsub.MultiDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
public class StringListObjectReplayDecoder implements MultiDecoder<Object> {
@Override
public Object decode(ByteBuf buf) {
return buf.toString(CharsetUtil.UTF_8);
}
@Override
public Object decode(List<Object> parts) {
return parts;
}
}

@ -20,4 +20,9 @@ public class StringListReplayDecoder implements MultiDecoder<List<String>> {
return Arrays.asList(Arrays.copyOf(parts.toArray(), parts.size(), String[].class)); return Arrays.asList(Arrays.copyOf(parts.toArray(), parts.size(), String[].class));
} }
@Override
public boolean isApplicable(int paramNum) {
return true;
}
} }

@ -40,4 +40,9 @@ public class StringMapReplayDecoder implements MultiDecoder<List<Map<String, Str
return Collections.singletonList(result); return Collections.singletonList(result);
} }
@Override
public boolean isApplicable(int paramNum) {
return true;
}
} }

@ -0,0 +1,32 @@
/**
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
*
* Licensed 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 org.redisson.client.protocol.decoder;
import org.redisson.client.protocol.Decoder;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
public class StringObjectDecoder implements Decoder<Object> {
@Override
public String decode(ByteBuf buf) {
String status = buf.readBytes(buf.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8);
buf.skipBytes(2);
return status;
}
}

@ -21,6 +21,8 @@ import org.redisson.client.protocol.Decoder;
public interface MultiDecoder<T> extends Decoder<Object> { public interface MultiDecoder<T> extends Decoder<Object> {
boolean isApplicable(int paramNum);
T decode(List<Object> parts); T decode(List<Object> parts);
} }

@ -15,12 +15,12 @@
*/ */
package org.redisson.client.protocol.pubsub; package org.redisson.client.protocol.pubsub;
public class PubSubMessage { public class PubSubMessage<V> {
private final String channel; private final String channel;
private final Object value; private final V value;
public PubSubMessage(String channel, Object value) { public PubSubMessage(String channel, V value) {
super(); super();
this.channel = channel; this.channel = channel;
this.value = value; this.value = value;
@ -30,13 +30,13 @@ public class PubSubMessage {
return channel; return channel;
} }
public Object getValue() { public V getValue() {
return value; return value;
} }
@Override @Override
public String toString() { public String toString() {
return "PubSubMessage [channel=" + channel + ", value=" + value + "]"; return "Message [channel=" + channel + ", value=" + value + "]";
} }
} }

@ -34,4 +34,9 @@ public class PubSubMessageDecoder implements MultiDecoder<Object> {
return new PubSubMessage(parts.get(1).toString(), parts.get(2).toString()); return new PubSubMessage(parts.get(1).toString(), parts.get(2).toString());
} }
@Override
public boolean isApplicable(int paramNum) {
return true;
}
} }

@ -34,4 +34,9 @@ public class PubSubPatternMessageDecoder implements MultiDecoder<Object> {
return new PubSubPatternMessage(parts.get(1).toString(), parts.get(2).toString(), parts.get(3)); return new PubSubPatternMessage(parts.get(1).toString(), parts.get(2).toString(), parts.get(3));
} }
@Override
public boolean isApplicable(int paramNum) {
return true;
}
} }

@ -39,4 +39,9 @@ public class PubSubStatusDecoder implements MultiDecoder<PubSubStatusMessage> {
return new PubSubStatusMessage(PubSubStatusMessage.Type.valueOf(parts.get(0).toString().toUpperCase()), channels); return new PubSubStatusMessage(PubSubStatusMessage.Type.valueOf(parts.get(0).toString().toUpperCase()), channels);
} }
@Override
public boolean isApplicable(int paramNum) {
return true;
}
} }

Loading…
Cancel
Save