Fix BloomFilter rename bug.

Signed-off-by: dengliming <liming.d.pro@gmail.com>
pull/2993/head
dengliming 5 years ago
parent 20a8d093bf
commit 1353a8f7e0

@ -66,7 +66,7 @@ public class RedissonBloomFilter<T> extends RedissonExpirable implements RBloomF
private volatile int hashIterations;
private final CommandExecutor commandExecutor;
private final String configName;
private String configName;
protected RedissonBloomFilter(CommandExecutor commandExecutor, String name) {
super(commandExecutor, name);
@ -327,6 +327,42 @@ public class RedissonBloomFilter<T> extends RedissonExpirable implements RBloomF
return check(result);
}
@Override
public RFuture<Void> renameAsync(String newName) {
String newConfigName = suffixName(newName, "config");
RFuture<Void> f = commandExecutor.evalWriteAsync(getName(), StringCodec.INSTANCE, RedisCommands.EVAL_VOID,
"redis.call('rename', KEYS[1], ARGV[1]); "
+ " return redis.call('rename', KEYS[2], ARGV[2]); ",
Arrays.<Object>asList(getName(), configName), newName, newConfigName);
f.onComplete((value, e) -> {
if (e == null) {
this.name = newName;
this.configName = newConfigName;
}
});
return f;
}
@Override
public RFuture<Boolean> renamenxAsync(String newName) {
String newConfigName = suffixName(newName, "config");
RFuture<Boolean> f = commandExecutor.evalWriteAsync(getName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local r = redis.call('renamenx', KEYS[1], ARGV[1]); "
+ "if r == 0 then "
+ " return 0; "
+ "else "
+ " return redis.call('renamenx', KEYS[2], ARGV[2]); "
+ "end; ",
Arrays.<Object>asList(getName(), configName), newName, newConfigName);
f.onComplete((value, e) -> {
if (e == null && value) {
this.name = newName;
this.configName = newConfigName;
}
});
return f;
}
private <V> V check(V result) {
if (result == null) {
throw new IllegalStateException("Bloom filter is not initialized!");

@ -85,4 +85,40 @@ public class RedissonBloomFilterTest extends BaseTest {
assertThat(filter.count()).isEqualTo(2);
}
@Test
public void testRename() {
RBloomFilter<String> filter = redisson.getBloomFilter("filter");
filter.tryInit(550000000L, 0.03);
assertThat(filter.add("123")).isTrue();
filter.rename("new_filter");
RBloomFilter<String> filter2 = redisson.getBloomFilter("new_filter");
assertThat(filter2.count()).isEqualTo(1);
RBloomFilter<String> filter3 = redisson.getBloomFilter("filter");
assertThat(filter3.isExists()).isFalse();
}
@Test
public void testRenamenx() {
RBloomFilter<String> filter = redisson.getBloomFilter("filter");
filter.tryInit(550000000L, 0.03);
assertThat(filter.add("123")).isTrue();
assertThat(filter.contains("123")).isTrue();
RBloomFilter<String> filter2 = redisson.getBloomFilter("filter2");
filter2.tryInit(550000000L, 0.03);
assertThat(filter2.add("234")).isTrue();
assertThat(filter.renamenx("filter2")).isFalse();
assertThat(filter.count()).isEqualTo(1);
assertThat(filter.renamenx("new_filter")).isTrue();
RBloomFilter<String> oldFilter = redisson.getBloomFilter("filter");
assertThat(oldFilter.isExists()).isFalse();
RBloomFilter<String> newFilter = redisson.getBloomFilter("new_filter");
assertThat(newFilter.count()).isEqualTo(1);
assertThat(newFilter.contains("123")).isTrue();
}
}

Loading…
Cancel
Save