From 3214c19e46032212d16767dd5c51b542dee1c322 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 13 Dec 2024 15:57:36 +0000 Subject: [PATCH 1/2] DOC-4531 added examples for sets page --- .../io/redis/examples/async/SetExample.java | 250 ++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 src/test/java/io/redis/examples/async/SetExample.java diff --git a/src/test/java/io/redis/examples/async/SetExample.java b/src/test/java/io/redis/examples/async/SetExample.java new file mode 100644 index 000000000..1c3ad7d3f --- /dev/null +++ b/src/test/java/io/redis/examples/async/SetExample.java @@ -0,0 +1,250 @@ +// EXAMPLE: sets_tutorial +package io.redis.examples.async; + +import io.lettuce.core.*; +import io.lettuce.core.api.async.RedisAsyncCommands; +import io.lettuce.core.api.StatefulRedisConnection; + +// REMOVE_START +import org.junit.jupiter.api.Test; + +// REMOVE_END +import java.util.*; +import static java.util.stream.Collectors.*; +import java.util.concurrent.CompletableFuture; + +// REMOVE_START +import static org.assertj.core.api.Assertions.assertThat; +// REMOVE_END + +public class SetExample { + + @Test + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisAsyncCommands asyncCommands = connection.async(); + // REMOVE_START + CompletableFuture delResult = asyncCommands + .del("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").toCompletableFuture(); + // REMOVE_END + + // STEP_START sadd + CompletableFuture sAdd = asyncCommands.sadd("bikes:racing:france", "bike:1").thenCompose(res1 -> { + System.out.println(res1); // >>> 1 + + // REMOVE_START + assertThat(res1).isEqualTo(1); + // REMOVE_END + return asyncCommands.sadd("bikes:racing:france", "bike:1"); + }).thenCompose(res2 -> { + System.out.println(res2); // >>> 0 + + // REMOVE_START + assertThat(res2).isEqualTo(0); + // REMOVE_END + return asyncCommands.sadd("bikes:racing:france", "bike:2", "bike:3"); + }).thenCompose(res3 -> { + System.out.println(res3); // >>> 2 + + // REMOVE_START + assertThat(res3).isEqualTo(2); + // REMOVE_END + return asyncCommands.sadd("bikes:racing:usa", "bike:1", "bike:4"); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo(2); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) + // >>> 2 + .toCompletableFuture(); + // STEP_END + + // STEP_START sismember + CompletableFuture sIsMember = sAdd.thenCompose(r -> { + return asyncCommands.sismember("bikes:racing:usa", "bike:1"); + }).thenCompose(res4 -> { + System.out.println(res4); // >>> true + + // REMOVE_START + assertThat(res4).isTrue(); + // REMOVE_END + return asyncCommands.sismember("bikes:racing:usa", "bike:2"); + }) + // REMOVE_START + .thenApply(r -> { + assertThat(r).isFalse(); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) // >>> false + .toCompletableFuture(); + // STEP_END + + // STEP_START sinter + CompletableFuture sInter = sIsMember.thenCompose(r -> { + return asyncCommands.sinter("bikes:racing:france", "bikes:racing:usa"); + }) + // REMOVE_START + .thenApply(r -> { + assertThat(r.toString()).isEqualTo("[bike:1]"); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) // >>> ["bike:1"] + .toCompletableFuture(); + // STEP_END + + // STEP_START scard + CompletableFuture sCard = sInter.thenCompose(r -> { + return asyncCommands.scard("bikes:racing:france"); + }) + // REMOVE_START + .thenApply(r -> { + assertThat(r).isEqualTo(3); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) // >>> 3 + .toCompletableFuture(); + // STEP_END + + // STEP_START sadd_smembers + CompletableFuture sAddSMembers = sCard.thenCompose(r -> { + return asyncCommands.del("bikes:racing:france"); + }).thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + }).thenCompose(res5 -> { + System.out.println(res5); // >>> 3 + return asyncCommands.smembers("bikes:racing:france"); + }) + // REMOVE_START + .thenApply((Set r) -> { + assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:1, bike:2, bike:3]"); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) + // >>> [bike:1, bike:2, bike:3] + .toCompletableFuture(); + // STEP_END + + // STEP_START smismember + CompletableFuture sMIsMember = sAddSMembers.thenCompose(r -> { + return asyncCommands.sismember("bikes:racing:france", "bike:1"); + }).thenCompose(res6 -> { + System.out.println(res6); // >>> True + // REMOVE_START + assertThat(res6).isTrue(); + // REMOVE_END + return asyncCommands.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4"); + }) + // REMOVE_START + .thenApply(r -> { + assertThat(r).containsSequence(true, true, false); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) // >>> [true, true, false] + .toCompletableFuture(); + // STEP_END + + // STEP_START sdiff + CompletableFuture sDiff = sMIsMember.thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + }).thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:usa", "bike:1", "bike:4"); + }).thenCompose(r -> { + return asyncCommands.sdiff("bikes:racing:france", "bikes:racing:usa"); + }) + // REMOVE_START + .thenApply(r -> { + assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:2, bike:3]"); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) // >>> [bike:2, bike:3] + .toCompletableFuture(); + // STEP_END + + // STEP_START multisets + CompletableFuture multisets = sDiff.thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + }).thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:usa", "bike:1", "bike:4"); + }).thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4"); + }).thenCompose(r -> { + return asyncCommands.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); + }).thenCompose(res7 -> { + System.out.println(res7); // >>> [bike:1] + // REMOVE_START + assertThat(res7.toString()).isEqualTo("[bike:1]"); + // REMOVE_END + return asyncCommands.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); + }).thenCompose(res8 -> { + System.out.println(res8); + // >>> [bike:1, bike:2, bike:3, bike:4] + // REMOVE_START + assertThat(res8.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:1, bike:2, bike:3, bike:4]"); + // REMOVE_END + return asyncCommands.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); + }).thenCompose(res9 -> { + System.out.println(res9); // >>> [] + // REMOVE_START + assertThat(res9.toString()).isEqualTo("[]"); + // REMOVE_END + return asyncCommands.sdiff("bikes:racing:usa", "bikes:racing:france"); + }).thenCompose(res10 -> { + System.out.println(res10); // >>> [bike:4] + // REMOVE_START + assertThat(res10.toString()).isEqualTo("[bike:4]"); + // REMOVE_END + return asyncCommands.sdiff("bikes:racing:france", "bikes:racing:usa"); + }) + // REMOVE_START + .thenApply(r -> { + assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:2, bike:3]"); + return r; + }) + // REMOVE_END + .thenAccept(System.out::println) // >>> [bike:2, bike:3] + .toCompletableFuture(); + // STEP_END + + // STEP_START srem + CompletableFuture sRem = multisets.thenCompose(r -> { + return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); + }).thenCompose(r -> { + return asyncCommands.srem("bikes:racing:france", "bike:1"); + }).thenCompose(res11 -> { + System.out.println(res11); // >>> 1 + // REMOVE_START + assertThat(res11).isEqualTo(1); + // REMOVE_END + return asyncCommands.spop("bikes:racing:france"); + }).thenCompose(res12 -> { + System.out.println(res12); // >>> bike:3 (for example) + return asyncCommands.smembers("bikes:racing:france"); + }).thenCompose(res13 -> { + System.out.println(res13); // >>> [bike:2, bike:4, bike:5] + return asyncCommands.srandmember("bikes:racing:france"); + }).thenAccept(System.out::println) // >>> bike:4 + .toCompletableFuture(); + // STEP_END + + CompletableFuture.allOf( + // REMOVE_START + delResult, + // REMOVE_END, + sRem).join(); + } finally { + redisClient.shutdown(); + } + } + +} From 60768b8428488e45a4139e39c8f88937532a91ac Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 13 Dec 2024 15:58:18 +0000 Subject: [PATCH 2/2] DOC-4531 copied strings example from doctest branch --- .../redis/examples/async/StringExample.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/test/java/io/redis/examples/async/StringExample.java diff --git a/src/test/java/io/redis/examples/async/StringExample.java b/src/test/java/io/redis/examples/async/StringExample.java new file mode 100644 index 000000000..9be29395b --- /dev/null +++ b/src/test/java/io/redis/examples/async/StringExample.java @@ -0,0 +1,127 @@ +// EXAMPLE: set_tutorial +package io.redis.examples.async; + +import io.lettuce.core.*; +import io.lettuce.core.api.async.RedisAsyncCommands; +import io.lettuce.core.api.StatefulRedisConnection; + +// REMOVE_START +import org.junit.jupiter.api.Test; +// REMOVE_END + +import java.util.*; +import java.util.concurrent.CompletableFuture; + +// REMOVE_START +import static org.assertj.core.api.Assertions.assertThat; +// REMOVE_END + +public class StringExample { + + // REMOVE_START + @Test + // REMOVE_END + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisAsyncCommands asyncCommands = connection.async(); + + // STEP_START set_get + CompletableFuture setAndGet = asyncCommands.set("bike:1", "Deimos").thenCompose(v -> { + System.out.println(v); // OK + // REMOVE_START + assertThat(v).isEqualTo("OK"); + // REMOVE_END + return asyncCommands.get("bike:1"); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo("Deimos"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // Deimos + .toCompletableFuture(); + // STEP_END + + // STEP_START setnx_xx + CompletableFuture setnx = asyncCommands.setnx("bike:1", "bike").thenCompose(v -> { + System.out.println(v); // false (because key already exists) + // REMOVE_START + assertThat(v).isFalse(); + // REMOVE_END + return asyncCommands.get("bike:1"); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo("Deimos"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // Deimos (value is unchanged) + .toCompletableFuture(); + + // set the value to "bike" if it already exists + CompletableFuture setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx()) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo("OK"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // OK + .toCompletableFuture(); + // STEP_END + + // STEP_START mset + Map bikeMap = new HashMap<>(); + bikeMap.put("bike:1", "Deimos"); + bikeMap.put("bike:2", "Ares"); + bikeMap.put("bike:3", "Vanth"); + + CompletableFuture mset = asyncCommands.mset(bikeMap).thenCompose(v -> { + System.out.println(v); // OK + return asyncCommands.mget("bike:1", "bike:2", "bike:3"); + }) + // REMOVE_START + .thenApply(res -> { + List> expected = new ArrayList<>( + Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"), + KeyValue.just("bike:3", "Vanth"))); + assertThat(res).isEqualTo(expected); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, + // Vanth]] + .toCompletableFuture(); + // STEP_END + + // STEP_START incr + CompletableFuture incrby = asyncCommands.set("total_crashes", "0") + .thenCompose(v -> asyncCommands.incr("total_crashes")).thenCompose(v -> { + System.out.println(v); // 1 + // REMOVE_START + assertThat(v).isEqualTo(1L); + // REMOVE_END + return asyncCommands.incrby("total_crashes", 10); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo(11L); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // 11 + .toCompletableFuture(); + // STEP_END + + CompletableFuture.allOf(setAndGet, setnx, setxx, mset, incrby).join(); + + } finally { + redisClient.shutdown(); + } + } + +}