diff --git a/src/main/java/org/redisson/RedissonKeys.java b/src/main/java/org/redisson/RedissonKeys.java index afab001b7..b13be8756 100644 --- a/src/main/java/org/redisson/RedissonKeys.java +++ b/src/main/java/org/redisson/RedissonKeys.java @@ -35,6 +35,7 @@ import org.redisson.cluster.ClusterSlotRange; import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandBatchService; import org.redisson.core.RKeys; +import org.redisson.core.RType; import org.redisson.misc.CompositeIterable; import io.netty.util.concurrent.Future; @@ -50,6 +51,16 @@ public class RedissonKeys implements RKeys { this.commandExecutor = commandExecutor; } + @Override + public RType getType(String key) { + return commandExecutor.get(getTypeAsync(key)); + } + + @Override + public Future getTypeAsync(String key) { + return commandExecutor.readAsync(key, RedisCommands.TYPE, key); + } + @Override public int getSlot(String key) { return commandExecutor.get(getSlotAsync(key)); diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index d88fe98c4..043c9a41b 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -34,6 +34,7 @@ import org.redisson.client.protocol.convertor.IntegerReplayConvertor; import org.redisson.client.protocol.convertor.KeyValueConvertor; import org.redisson.client.protocol.convertor.LongReplayConvertor; import org.redisson.client.protocol.convertor.TrueReplayConvertor; +import org.redisson.client.protocol.convertor.TypeConvertor; import org.redisson.client.protocol.convertor.VoidReplayConvertor; import org.redisson.client.protocol.decoder.KeyValueObjectDecoder; import org.redisson.client.protocol.decoder.ListResultReplayDecoder; @@ -56,6 +57,7 @@ import org.redisson.client.protocol.decoder.StringListReplayDecoder; import org.redisson.client.protocol.decoder.StringMapDataDecoder; import org.redisson.client.protocol.decoder.StringReplayDecoder; import org.redisson.client.protocol.pubsub.PubSubStatusDecoder; +import org.redisson.core.RType; public interface RedisCommands { @@ -66,6 +68,7 @@ public interface RedisCommands { RedisCommand> GEORADIUSBYMEMBER = new RedisCommand>("GEORADIUSBYMEMBER", new ObjectListReplayDecoder(), 2); RedisStrictCommand KEYSLOT = new RedisStrictCommand("CLUSTER", "KEYSLOT", new IntegerReplayConvertor()); + RedisStrictCommand TYPE = new RedisStrictCommand("TYPE", new TypeConvertor()); RedisStrictCommand GETBIT = new RedisStrictCommand("GETBIT", new BooleanReplayConvertor()); RedisStrictCommand BITS_SIZE = new RedisStrictCommand("STRLEN", new BitsSizeReplayConvertor()); diff --git a/src/main/java/org/redisson/client/protocol/convertor/TypeConvertor.java b/src/main/java/org/redisson/client/protocol/convertor/TypeConvertor.java new file mode 100644 index 000000000..f0249e0a9 --- /dev/null +++ b/src/main/java/org/redisson/client/protocol/convertor/TypeConvertor.java @@ -0,0 +1,47 @@ +/** + * 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.convertor; + +import org.redisson.core.RType; + +public class TypeConvertor extends SingleConvertor { + + @Override + public RType convert(Object obj) { + String val = obj.toString(); + if ("string".equals(val)) { + return RType.OBJECT; + } + if ("list".equals(val)) { + return RType.LIST; + } + if ("set".equals(val)) { + return RType.SET; + } + if ("zset".equals(val)) { + return RType.ZSET; + } + if ("hash".equals(val)) { + return RType.MAP; + } + if ("none".equals(val)) { + return null; + } + + throw new IllegalStateException("Can't recognize redis type: " + obj); + } + +} diff --git a/src/main/java/org/redisson/core/RKeys.java b/src/main/java/org/redisson/core/RKeys.java index a32781a0f..3617bb051 100644 --- a/src/main/java/org/redisson/core/RKeys.java +++ b/src/main/java/org/redisson/core/RKeys.java @@ -19,6 +19,14 @@ import java.util.Collection; public interface RKeys extends RKeysAsync { + /** + * Get Redis object type by key + * + * @param name + * @return + */ + RType getType(String key); + /** * Get hash slot identifier for key. * Available for cluster nodes only diff --git a/src/main/java/org/redisson/core/RKeysAsync.java b/src/main/java/org/redisson/core/RKeysAsync.java index 654d6fff8..591ccd945 100644 --- a/src/main/java/org/redisson/core/RKeysAsync.java +++ b/src/main/java/org/redisson/core/RKeysAsync.java @@ -21,6 +21,14 @@ import io.netty.util.concurrent.Future; public interface RKeysAsync { + /** + * Get Redis object type by key + * + * @param name + * @return + */ + Future getTypeAsync(String key); + /** * Get hash slot identifier for key in async mode. * Available for cluster nodes only diff --git a/src/main/java/org/redisson/core/RType.java b/src/main/java/org/redisson/core/RType.java new file mode 100644 index 000000000..2d32a1fab --- /dev/null +++ b/src/main/java/org/redisson/core/RType.java @@ -0,0 +1,22 @@ +/** + * 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.core; + +public enum RType { + + OBJECT, MAP, LIST, SET, ZSET + +} diff --git a/src/test/java/org/redisson/RedissonKeysTest.java b/src/test/java/org/redisson/RedissonKeysTest.java index 559d8f1fb..ed5d68f23 100644 --- a/src/test/java/org/redisson/RedissonKeysTest.java +++ b/src/test/java/org/redisson/RedissonKeysTest.java @@ -11,9 +11,18 @@ import org.junit.Assert; import org.junit.Test; import org.redisson.core.RBucket; import org.redisson.core.RMap; +import org.redisson.core.RType; public class RedissonKeysTest extends BaseTest { + @Test + public void testType() { + redisson.getSet("test").add("1"); + + assertThat(redisson.getKeys().getType("test")).isEqualTo(RType.SET); + assertThat(redisson.getKeys().getType("test1")).isNull(); + } + @Test public void testKeysIterablePattern() { redisson.getBucket("test1").set("someValue");