RBucketsAsync interface added. #1269

pull/1283/head
Nikita 7 years ago
parent 116d36ee8f
commit 1b54139f3a

@ -30,11 +30,16 @@ import org.redisson.api.RFuture;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.DelegateDecoderCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommand.ValueType;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandExecutor;
import org.redisson.connection.decoder.MapGetAllDecoder;
import org.redisson.misc.RedissonPromise;
/**
*
* @author Nikita Koksharov
*
*/
public class RedissonBuckets implements RBuckets {
private final Codec codec;
@ -67,19 +72,36 @@ public class RedissonBuckets implements RBuckets {
@Override
public <V> Map<String, V> get(String... keys) {
RFuture<Map<String, V>> future = getAsync(keys);
return commandExecutor.get(future);
}
@Override
public boolean trySet(Map<String, ?> buckets) {
RFuture<Boolean> future = trySetAsync(buckets);
return commandExecutor.get(future);
}
@Override
public void set(Map<String, ?> buckets) {
commandExecutor.get(setAsync(buckets));
}
@Override
public <V> RFuture<Map<String, V>> getAsync(String... keys) {
if (keys.length == 0) {
return Collections.emptyMap();
Map<String, V> emptyMap = Collections.emptyMap();
return RedissonPromise.<Map<String, V>>newSucceededFuture(emptyMap);
}
RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("MGET", new MapGetAllDecoder(Arrays.<Object>asList(keys), 0));
RFuture<Map<String, V>> future = commandExecutor.readAsync(keys[0], new DelegateDecoderCodec(codec), command, keys);
return commandExecutor.get(future);
return commandExecutor.readAsync(keys[0], new DelegateDecoderCodec(codec), command, keys);
}
@Override
public boolean trySet(Map<String, ?> buckets) {
public RFuture<Boolean> trySetAsync(Map<String, ?> buckets) {
if (buckets.isEmpty()) {
return false;
return RedissonPromise.newSucceededFuture(false);
}
List<Object> params = new ArrayList<Object>(buckets.size());
@ -92,13 +114,13 @@ public class RedissonBuckets implements RBuckets {
}
}
return commandExecutor.write(params.get(0).toString(), RedisCommands.MSETNX, params.toArray());
return commandExecutor.writeAsync(params.get(0).toString(), RedisCommands.MSETNX, params.toArray());
}
@Override
public void set(Map<String, ?> buckets) {
public RFuture<Void> setAsync(Map<String, ?> buckets) {
if (buckets.isEmpty()) {
return;
return RedissonPromise.newSucceededFuture(null);
}
List<Object> params = new ArrayList<Object>(buckets.size());
@ -111,7 +133,7 @@ public class RedissonBuckets implements RBuckets {
}
}
commandExecutor.write(params.get(0).toString(), RedisCommands.MSET, params.toArray());
return commandExecutor.writeAsync(params.get(0).toString(), RedisCommands.MSET, params.toArray());
}
}

@ -18,23 +18,17 @@ package org.redisson.api;
import java.util.List;
import java.util.Map;
public interface RBuckets {
/**
*
* @author Nikita Koksharov
*
*/
public interface RBuckets extends RBucketsAsync {
/**
* <p>Returns a list of object holder instances by a key pattern.
*
* <pre>Supported glob-style patterns:
* h?llo subscribes to hello, hallo and hxllo
* h*llo subscribes to hllo and heeeello
* h[ae]llo subscribes to hello and hallo, but not hillo
* h[^e]llo matches hallo, hbllo, ... but not hello
* h[a-b]llo matches hallo and hbllo</pre>
* <p>Use \ to escape special characters if you want to match them verbatim.
*
* @param <V> type of value
* @param pattern - pattern of key
* @return List of bucket objects
/*
* Use RKeys.findKeysByPattern method instead
*/
@Deprecated
<V> List<RBucket<V>> find(String pattern);
/**

@ -0,0 +1,54 @@
/**
* Copyright 2016 Nikita Koksharov
*
* 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.api;
import java.util.Map;
/**
*
* @author Nikita Koksharov
*
*/
public interface RBucketsAsync {
/**
* Returns Redis object mapped by key. Result Map is not contains
* key-value entry for null values.
*
* @param <V> type of value
* @param keys - keys
* @return Map with name of bucket as key and bucket as value
*/
<V> RFuture<Map<String, V>> getAsync(String ... keys);
/**
* Try to save objects mapped by Redis key.
* If at least one of them is already exist then
* don't set none of them.
*
* @param buckets - map of buckets
* @return <code>true</code> if object has been set overwise <code>false</code>
*/
RFuture<Boolean> trySetAsync(Map<String, ?> buckets);
/**
* Saves objects mapped by Redis key.
*
* @param buckets - map of buckets
*/
RFuture<Void> setAsync(Map<String, ?> buckets);
}

@ -15,7 +15,6 @@
*/
package org.redisson.command;
import java.net.InetSocketAddress;
import java.util.List;
import org.redisson.api.RFuture;
@ -32,10 +31,6 @@ public interface CommandSyncExecutor {
<V> V get(RFuture<V> future);
<T, R> R write(String key, Codec codec, RedisCommand<T> command, Object ... params);
<T, R> R write(String key, RedisCommand<T> command, Object ... params);
<T, R> R read(String key, RedisCommand<T> command, Object ... params);
<T, R> R read(String key, Codec codec, RedisCommand<T> command, Object ... params);

@ -15,7 +15,6 @@
*/
package org.redisson.command;
import java.net.InetSocketAddress;
import java.util.List;
import org.redisson.api.RFuture;
@ -71,16 +70,4 @@ public class CommandSyncService extends CommandAsyncService implements CommandEx
return get(res);
}
@Override
public <T, R> R write(String key, Codec codec, RedisCommand<T> command, Object ... params) {
RFuture<R> res = writeAsync(key, codec, command, params);
return get(res);
}
@Override
public <T, R> R write(String key, RedisCommand<T> command, Object ... params) {
RFuture<R> res = writeAsync(key, command, params);
return get(res);
}
}

@ -1,13 +1,10 @@
package org.redisson;
import java.util.Arrays;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.*;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.api.RBucket;
@ -29,26 +26,6 @@ public class RedissonBucketsTest extends BaseTest {
Assert.assertEquals(expected, result);
}
@Test
public void testFind() {
Collection<String> names = Arrays.asList("test:testGetPattern:one", "test:testGetPattern:two");
Collection<String> vals = Arrays.asList("one-val", "two-val");
redisson.getBucket("test:testGetPattern:one").set("one-val");
redisson.getBucket("test:testGetPattern:two").set("two-val");
List<RBucket<String>> buckets = redisson.getBuckets().find("test:testGetPattern:*");
Assert.assertEquals(2, buckets.size());
Assert.assertTrue(names.contains(buckets.get(0).getName()));
Assert.assertTrue(names.contains(buckets.get(1).getName()));
Assert.assertTrue(vals.contains(buckets.get(0).get()));
Assert.assertTrue(vals.contains(buckets.get(1).get()));
for (RBucket<String> bucket : buckets) {
bucket.delete();
}
}
@Test
public void testSet() {
Map<String, Integer> buckets = new HashMap<String, Integer>();

Loading…
Cancel
Save