Skip to content

Commit

Permalink
readAll and readAllAsync methods added to RSetCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Feb 4, 2016
1 parent 8002797 commit edb3bfa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/main/java/org/redisson/RedissonSetCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.redisson.client.codec.Codec;
Expand Down Expand Up @@ -220,7 +221,33 @@ public void remove() {
};
}

private Future<Collection<V>> readAllAsync() {
@Override
public Set<V> readAll() {
return get(readAllAsync());
}

@Override
public Future<Set<V>> readAllAsync() {
return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_SET,
"local expireHead = redis.call('zrange', KEYS[2], 0, 0, 'withscores');" +
"local keys = redis.call('hkeys', KEYS[1]);" +
"local maxDate = ARGV[1]; " +
"local minExpireDate = 92233720368547758;" +
"if #expireHead == 2 and tonumber(expireHead[2]) <= tonumber(maxDate) then " +
"for i = #keys, 1, -1 do " +
"local key = keys[i]; " +
"local expireDate = redis.call('zscore', KEYS[2], key); " +
"if expireDate ~= false and tonumber(expireDate) <= tonumber(maxDate) then " +
"minExpireDate = math.min(tonumber(expireDate), minExpireDate); " +
"table.remove(keys, i); " +
"end;" +
"end;" +
"end; " +
"return redis.call('hmget', KEYS[1], unpack(keys));",
Arrays.<Object>asList(getName(), getTimeoutSetName()), System.currentTimeMillis());
}

private Future<List<Object>> readAllasListAsync() {
return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LIST,
"local expireHead = redis.call('zrange', KEYS[2], 0, 0, 'withscores');" +
"local keys = redis.call('hkeys', KEYS[1]);" +
Expand All @@ -242,13 +269,13 @@ private Future<Collection<V>> readAllAsync() {

@Override
public Object[] toArray() {
List<Object> res = (List<Object>) get(readAllAsync());
List<Object> res = get(readAllasListAsync());
return res.toArray();
}

@Override
public <T> T[] toArray(T[] a) {
List<Object> res = (List<Object>) get(readAllAsync());
List<Object> res = get(readAllasListAsync());
return res.toArray(a);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public interface RedisCommands {
RedisStrictCommand<Long> EVAL_LONG = new RedisStrictCommand<Long>("EVAL");
RedisStrictCommand<Void> EVAL_VOID = new RedisStrictCommand<Void>("EVAL", new VoidReplayConvertor());
RedisCommand<List<Object>> EVAL_LIST = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>());
RedisCommand<Set<Object>> EVAL_SET = new RedisCommand<Set<Object>>("EVAL", new ObjectSetReplayDecoder());
RedisCommand<Object> EVAL_OBJECT = new RedisCommand<Object>("EVAL");
RedisCommand<Object> EVAL_MAP_VALUE = new RedisCommand<Object>("EVAL", ValueType.MAP_VALUE);
RedisCommand<List<Object>> EVAL_MAP_VALUE_LIST = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), ValueType.MAP_VALUE);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/redisson/core/RSetCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ public interface RSetCache<V> extends Set<V>, RExpirable, RSetCacheAsync<V> {
@Override
int size();

/**
* Read all elements at once
*
* @return
*/
Set<V> readAll();

}
8 changes: 8 additions & 0 deletions src/main/java/org/redisson/core/RSetCacheAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.redisson.core;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import io.netty.util.concurrent.Future;
Expand Down Expand Up @@ -51,4 +52,11 @@ public interface RSetCacheAsync<V> extends RCollectionAsync<V> {
@Override
Future<Integer> sizeAsync();

/**
* Read all elements at once
*
* @return
*/
Future<Set<V>> readAllAsync();

}
12 changes: 12 additions & 0 deletions src/test/java/org/redisson/RedissonSetCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.*;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
Expand Down Expand Up @@ -267,6 +268,17 @@ public void testSize() {
Assert.assertEquals(5, set.size());
}

@Test
public void testReadAll() {
RSetCache<Integer> set = redisson.getSetCache("set");
set.add(1, 2, TimeUnit.MINUTES);
set.add(2);
set.add(3);
set.add(4);
set.add(5);

assertThat(set.readAll()).containsOnly(1, 2, 3, 4, 5);
}

@Test
public void testRetainAllEmpty() {
Expand Down

0 comments on commit edb3bfa

Please sign in to comment.