Type support added. #529

pull/537/head
Nikita 9 years ago
parent a821f0e2d3
commit 63f0ae708b

@ -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<RType> getTypeAsync(String key) {
return commandExecutor.readAsync(key, RedisCommands.TYPE, key);
}
@Override
public int getSlot(String key) {
return commandExecutor.get(getSlotAsync(key));

@ -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<List<Object>> GEORADIUSBYMEMBER = new RedisCommand<List<Object>>("GEORADIUSBYMEMBER", new ObjectListReplayDecoder<Object>(), 2);
RedisStrictCommand<Integer> KEYSLOT = new RedisStrictCommand<Integer>("CLUSTER", "KEYSLOT", new IntegerReplayConvertor());
RedisStrictCommand<RType> TYPE = new RedisStrictCommand<RType>("TYPE", new TypeConvertor());
RedisStrictCommand<Boolean> GETBIT = new RedisStrictCommand<Boolean>("GETBIT", new BooleanReplayConvertor());
RedisStrictCommand<Integer> BITS_SIZE = new RedisStrictCommand<Integer>("STRLEN", new BitsSizeReplayConvertor());

@ -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<RType> {
@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);
}
}

@ -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

@ -21,6 +21,14 @@ import io.netty.util.concurrent.Future;
public interface RKeysAsync {
/**
* Get Redis object type by key
*
* @param name
* @return
*/
Future<RType> getTypeAsync(String key);
/**
* Get hash slot identifier for key in async mode.
* Available for cluster nodes only

@ -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
}

@ -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");

Loading…
Cancel
Save