readAllKeySet, readAllValues, readAllEntry methods with async variation added to RMap. #389

pull/395/head
Nikita 9 years ago
parent 6b75799e92
commit 5b625d3b2e

@ -189,6 +189,36 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return new EntrySet();
}
@Override
public Set<K> readAllKeySet() {
return get(readAllKeySetAsync());
}
@Override
public Future<Set<K>> readAllKeySetAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.HKEYS, getName());
}
@Override
public Collection<V> readAllValues() {
return get(readAllValuesAsync());
}
@Override
public Future<Collection<V>> readAllValuesAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.HVALS, getName());
}
@Override
public Set<Entry<K, V>> readAllEntrySet() {
return get(readAllEntrySetAsync());
}
@Override
public Future<Set<Entry<K, V>>> readAllEntrySetAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.HGETALL_ENTRY, getName());
}
@Override
public V putIfAbsent(K key, V value) {
return get(putIfAbsentAsync(key, value));

@ -18,6 +18,7 @@ package org.redisson.client.protocol;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.redisson.client.protocol.RedisCommand.ValueType;
import org.redisson.client.protocol.convertor.BitSetReplayConvertor;
@ -42,6 +43,7 @@ import org.redisson.client.protocol.decoder.NestedMultiDecoder;
import org.redisson.client.protocol.decoder.NestedMultiDecoder2;
import org.redisson.client.protocol.decoder.ObjectFirstResultReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectListReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectMapEntryReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectSetReplayDecoder;
import org.redisson.client.protocol.decoder.ScoredSortedSetReplayDecoder;
@ -185,6 +187,7 @@ public interface RedisCommands {
RedisCommand<Boolean> HSET = new RedisCommand<Boolean>("HSET", new BooleanReplayConvertor(), 2, ValueType.MAP);
RedisCommand<MapScanResult<Object, Object>> HSCAN = new RedisCommand<MapScanResult<Object, Object>>("HSCAN", new NestedMultiDecoder(new ObjectMapReplayDecoder(), new MapScanResultReplayDecoder()), ValueType.MAP);
RedisCommand<Map<Object, Object>> HGETALL = new RedisCommand<Map<Object, Object>>("HGETALL", new ObjectMapReplayDecoder(), ValueType.MAP);
RedisCommand<Set<Entry<Object, Object>>> HGETALL_ENTRY = new RedisCommand<Set<Entry<Object, Object>>>("HGETALL", new ObjectMapEntryReplayDecoder(), ValueType.MAP);
RedisCommand<List<Object>> HVALS = new RedisCommand<List<Object>>("HVALS", new ObjectListReplayDecoder<Object>(), ValueType.MAP_VALUE);
RedisCommand<Boolean> HEXISTS = new RedisCommand<Boolean>("HEXISTS", new BooleanReplayConvertor(), 2, ValueType.MAP_KEY);
RedisStrictCommand<Integer> HLEN = new RedisStrictCommand<Integer>("HLEN", new IntegerReplayConvertor());

@ -0,0 +1,51 @@
/**
* 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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.redisson.client.handler.State;
import io.netty.buffer.ByteBuf;
public class ObjectMapEntryReplayDecoder implements MultiDecoder<Set<Entry<Object, Object>>> {
@Override
public Object decode(ByteBuf buf, State state) {
throw new UnsupportedOperationException();
}
@Override
public Set<Entry<Object, Object>> decode(List<Object> parts, State state) {
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), parts.get(i));
}
}
return result.entrySet();
}
@Override
public boolean isApplicable(int paramNum, State state) {
return false;
}
}

@ -15,6 +15,7 @@
*/
package org.redisson.core;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@ -116,6 +117,27 @@ public interface RMap<K, V> extends ConcurrentMap<K, V>, RExpirable, RMapAsync<K
boolean fastPutIfAbsent(K key, V value);
/**
* Read all keys at once
*
* @return
*/
Set<K> readAllKeySet();
/**
* Read all values at once
*
* @return
*/
Collection<V> readAllValues();
/**
* Read all map entries at once
*
* @return
*/
Set<Entry<K, V>> readAllEntrySet();
/**
* Use {@link #entrySet().iterator()}
*

@ -15,7 +15,9 @@
*/
package org.redisson.core;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import io.netty.util.concurrent.Future;
@ -69,6 +71,27 @@ public interface RMapAsync<K, V> extends RExpirableAsync {
Future<Boolean> fastPutIfAbsentAsync(K key, V value);
/**
* Read all keys at once
*
* @return
*/
Future<Set<K>> readAllKeySetAsync();
/**
* Read all values at once
*
* @return
*/
Future<Collection<V>> readAllValuesAsync();
/**
* Read all map entries at once
*
* @return
*/
Future<Set<Entry<K, V>>> readAllEntrySetAsync();
Future<V> getAsync(K key);
Future<V> putAsync(K key, V value);

@ -10,6 +10,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import org.assertj.core.data.MapEntry;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
@ -255,10 +256,21 @@ public class RedissonMapTest extends BaseTest {
map.put(2, "33");
map.put(3, "43");
Assert.assertEquals(3, map.entrySet().size());
MatcherAssert.assertThat(map, Matchers.hasEntry(Matchers.equalTo(1), Matchers.equalTo("12")));
MatcherAssert.assertThat(map, Matchers.hasEntry(Matchers.equalTo(2), Matchers.equalTo("33")));
MatcherAssert.assertThat(map, Matchers.hasEntry(Matchers.equalTo(3), Matchers.equalTo("43")));
assertThat(map.entrySet().size()).isEqualTo(3);
Map<Integer, String> testMap = new HashMap<Integer, String>(map);
assertThat(map.entrySet()).containsOnlyElementsOf(testMap.entrySet());
}
@Test
public void testReadAllEntrySet() {
RMap<Integer, String> map = redisson.getMap("simple12");
map.put(1, "12");
map.put(2, "33");
map.put(3, "43");
assertThat(map.readAllEntrySet().size()).isEqualTo(3);
Map<Integer, String> testMap = new HashMap<Integer, String>(map);
assertThat(map.readAllEntrySet()).containsOnlyElementsOf(testMap.entrySet());
}
@Test
@ -312,6 +324,30 @@ public class RedissonMapTest extends BaseTest {
Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
}
@Test
public void testReadAllKeySet() {
RMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.readAllKeySet().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
}
@Test
public void testReadAllValues() {
RMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.readAllValues().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllValues()).containsOnlyElementsOf(testMap.values());
}
@Test
public void testContainsValue() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");

Loading…
Cancel
Save