Improvement - Copy command execute through multi db

Signed-off-by: seakider <seakider@gmail.com>
pull/6094/head
seakider 7 months ago
parent c46d8fc674
commit 1823bc6eff

@ -193,7 +193,7 @@ public abstract class RedissonObject implements RObject {
List<Object> args = new LinkedList<>(); List<Object> args = new LinkedList<>();
args.add(keys.get(0)); args.add(keys.get(0));
args.add(keys.get(1)); args.add(keys.get(1));
if (database > 0) { if (database >= 0) {
args.add("DB"); args.add("DB");
args.add(database); args.add(database);
} }
@ -213,13 +213,14 @@ public abstract class RedissonObject implements RObject {
+ "else " + "else "
+ "res = res + redis.call('copy', KEYS[j], KEYS[newKeysIndex + j], 'db', ARGV[1]); " + "res = res + redis.call('copy', KEYS[j], KEYS[newKeysIndex + j], 'db', ARGV[1]); "
+ "end; " + "end; "
+ "end; " + "else "
+ "if ARGV[2] == '1' then " + "if ARGV[2] == '1' then "
+ "res = res + redis.call('copy', KEYS[j], KEYS[newKeysIndex + j], 'replace'); " + "res = res + redis.call('copy', KEYS[j], KEYS[newKeysIndex + j], 'replace'); "
+ "else " + "else "
+ "res = res + redis.call('copy', KEYS[j], KEYS[newKeysIndex + j]); " + "res = res + redis.call('copy', KEYS[j], KEYS[newKeysIndex + j]); "
+ "end; " + "end; "
+ "end; " + "end; "
+ "end; "
+ "return math.min(res, 1); ", + "return math.min(res, 1); ",
keys, keys,
database, Boolean.compare(replace, false)); database, Boolean.compare(replace, false));

@ -88,6 +88,20 @@ public class RedisDockerTest {
return Redisson.create(config); return Redisson.create(config);
} }
protected void testTwoDatabase(BiConsumer<RedissonClient, RedissonClient> consumer) {
Config config1 = createConfig();
config1.useSingleServer().setDatabase(0);
RedissonClient r1 = Redisson.create(config1);
Config config2 = createConfig();
config2.useSingleServer().setDatabase(1);
RedissonClient r2 = Redisson.create(config2);
consumer.accept(r1, r2);
r1.shutdown();
r2.shutdown();
}
protected void testWithParams(Consumer<RedissonClient> redissonCallback, String... params) { protected void testWithParams(Consumer<RedissonClient> redissonCallback, String... params) {
GenericContainer<?> redis = createRedis(params); GenericContainer<?> redis = createRedis(params);
redis.start(); redis.start();

@ -639,6 +639,33 @@ public class RedissonBucketTest extends RedisDockerTest {
assertThat(bucket2.get()).isEqualTo("someValue"); assertThat(bucket2.get()).isEqualTo("someValue");
} }
@Test
public void testCopy3() {
testTwoDatabase((r1, r2) -> {
// normal test
RBucket<String> bucket1 = r1.getBucket("test");
bucket1.set("someValue");
bucket1.copy("test", 1);
RBucket<String> bucket2 = r2.getBucket("test");
assertThat(bucket2.get()).isEqualTo("someValue");
});
}
@Test
public void testCopy4() {
testTwoDatabase((r1, r2) -> {
// database1 copy to database0
// this will cause a RedisException
RBucket<String> bucket2 = r2.getBucket("test");
bucket2.set("database1");
bucket2.copy("test", 0);
RBucket<String> bucket1 = r1.getBucket("test");
assertThat(bucket1.get()).isEqualTo("database1");
});
}
@Test @Test
public void testRename() { public void testRename() {
RBucket<String> bucket = redisson.getBucket("test"); RBucket<String> bucket = redisson.getBucket("test");

@ -31,4 +31,16 @@ public class RedissonIdGeneratorTest extends RedisDockerTest {
} }
} }
@Test
public void testCopy() {
testTwoDatabase((r1, r2) -> {
RIdGenerator generator = r1.getIdGenerator("test");
generator.tryInit(12, 2931);
generator.copy("test1", 1);
assertThat(r1.getKeys().count()).isEqualTo(2);
assertThat(r2.getKeys().count()).isEqualTo(2);
});
}
} }

Loading…
Cancel
Save