From 152ba5b568fc9894ba8cf7818fe1a7b602d6f7a6 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 2 Dec 2024 16:46:15 +0000 Subject: [PATCH 1/4] DOC-4528 first two examples --- .../io/redis/examples/async/HashExample.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/test/java/io/redis/examples/async/HashExample.java diff --git a/src/test/java/io/redis/examples/async/HashExample.java b/src/test/java/io/redis/examples/async/HashExample.java new file mode 100644 index 000000000..4815d0f44 --- /dev/null +++ b/src/test/java/io/redis/examples/async/HashExample.java @@ -0,0 +1,134 @@ +// EXAMPLE: hash_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 org.testcontainers.shaded.org.checkerframework.checker.units.qual.s; + +import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +// REMOVE_START +import static org.assertj.core.api.Assertions.assertThat; +// REMOVE_END + +public class HashExample { + + @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("bike:1") + .toCompletableFuture(); + + // REMOVE_END + + //STEP_START set_get_all + Map bike1 = new HashMap<>(); + bike1.put("model", "Deimos"); + bike1.put("brand", "Ergonom"); + bike1.put("type", "Enduro bikes"); + bike1.put("price", "4972"); + + CompletableFuture setGetAll = asyncCommands.hset( + "bike:1", bike1 + ) + .thenCompose(res1 -> { + System.out.println(res1); // >>> 4 + // REMOVE_START + assertThat(res1).isEqualTo(4); + // REMOVE_END + return asyncCommands.hget("bike:1", "model"); + }) + .thenCompose(res2 -> { + System.out.println(res2); // >>> Deimos + // REMOVE_START + assertThat(res2).isEqualTo("Deimos"); + // REMOVE_END + return asyncCommands.hget("bike:1", "price"); + }) + .thenCompose(res3 -> { + System.out.println(res3); // >>> 4972 + // REMOVE_START + assertThat(res3).isEqualTo("4972"); + // REMOVE_END + return asyncCommands.hgetall("bike:1"); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res.get("type")).isEqualTo("Enduro bikes"); + assertThat(res.get("brand")).isEqualTo("Ergonom"); + assertThat(res.get("price")).isEqualTo("4972"); + assertThat(res.get("model")).isEqualTo("Deimos"); + + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) + // >>> {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos} + .toCompletableFuture(); + //STEP_END + + // STEP_START hmget + CompletableFuture hmGet = setGetAll + .thenApply(res4 -> { + return asyncCommands.hmget( + "bike:1","model", "price" + ); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res.toString()).isEqualTo("[KeyValue[model, Deimos], KeyValue[price, 4972]]"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) + // [KeyValue[model, Deimos], KeyValue[price, 4972]] + .toCompletableFuture(); + // STEP_END + + // STEP_START hincrby + CompletableFuture hIncrBy = hmGet + .thenCompose(res5 -> { + return asyncCommands.hincrby( + "bike:1", "price", 100 + ); + }) + .thenCompose(res6 -> { + System.out.println(res6); // >>> 5072 + // REMOVE_START + assertThat(res6).isEqualTo(5072); + // REMOVE_END + return asyncCommands.hincrby("bike:1", "price", -100); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo(4972); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) + // >>> 4972 + .toCompletableFuture(); + // STEP_END + + CompletableFuture.allOf( + // REMOVE_START + delResult, + // REMOVE_END + hmGet + ); + } finally { + redisClient.shutdown(); + } + } +} \ No newline at end of file From 2cf67edf5fa492f98a37f9721459f9303eac6b92 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 3 Dec 2024 10:10:28 +0000 Subject: [PATCH 2/4] DOC-4528 added final example --- .../io/redis/examples/async/HashExample.java | 86 +++++++++++++++++-- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/src/test/java/io/redis/examples/async/HashExample.java b/src/test/java/io/redis/examples/async/HashExample.java index 4815d0f44..1eb5645cf 100644 --- a/src/test/java/io/redis/examples/async/HashExample.java +++ b/src/test/java/io/redis/examples/async/HashExample.java @@ -27,7 +27,7 @@ public void run() { try (StatefulRedisConnection connection = redisClient.connect()) { RedisAsyncCommands asyncCommands = connection.async(); // REMOVE_START - CompletableFuture delResult = asyncCommands.del("bike:1") + CompletableFuture delResult = asyncCommands.del("bike:1", "bike:1:stats") .toCompletableFuture(); // REMOVE_END @@ -80,7 +80,7 @@ public void run() { // STEP_START hmget CompletableFuture hmGet = setGetAll - .thenApply(res4 -> { + .thenCompose(res4 -> { return asyncCommands.hmget( "bike:1","model", "price" ); @@ -98,7 +98,7 @@ public void run() { // STEP_START hincrby CompletableFuture hIncrBy = hmGet - .thenCompose(res5 -> { + .thenCompose(r -> { return asyncCommands.hincrby( "bike:1", "price", 100 ); @@ -106,13 +106,13 @@ public void run() { .thenCompose(res6 -> { System.out.println(res6); // >>> 5072 // REMOVE_START - assertThat(res6).isEqualTo(5072); + assertThat(res6).isEqualTo(5072L); // REMOVE_END return asyncCommands.hincrby("bike:1", "price", -100); }) // REMOVE_START .thenApply(res -> { - assertThat(res).isEqualTo(4972); + assertThat(res).isEqualTo(4972L); return res; }) // REMOVE_END @@ -121,12 +121,82 @@ public void run() { .toCompletableFuture(); // STEP_END + // STEP_START incrby_get_mget + CompletableFuture incrByGetMget = asyncCommands.hincrby( + "bike:1:stats","rides", 1 + ) + .thenCompose(res7 -> { + System.out.println(res7); // >>> 1 + // REMOVE_START + assertThat(res7).isEqualTo(1L); + // REMOVE_END + return asyncCommands.hincrby( + "bike:1:stats","rides", 1 + ); + }) + .thenCompose(res8 -> { + System.out.println(res8); // >>> 2 + // REMOVE_START + assertThat(res8).isEqualTo(2L); + // REMOVE_END + return asyncCommands.hincrby( + "bike:1:stats","rides", 1 + ); + }) + .thenCompose(res9 -> { + System.out.println(res9); // >>> 3 + // REMOVE_START + assertThat(res9).isEqualTo(3L); + // REMOVE_END + return asyncCommands.hincrby( + "bike:1:stats","crashes", 1 + ); + }) + .thenCompose(res10 -> { + System.out.println(res10); // >>> 1 + // REMOVE_START + assertThat(res10).isEqualTo(1L); + // REMOVE_END + return asyncCommands.hincrby( + "bike:1:stats","owners", 1 + ); + }) + .thenCompose(res11 -> { + System.out.println(res11); // >>> 1 + // REMOVE_START + assertThat(res11).isEqualTo(1L); + // REMOVE_END + return asyncCommands.hget( + "bike:1:stats","rides" + ); + }) + .thenCompose(res12 -> { + System.out.println(res12); // >>> 3 + // REMOVE_START + assertThat(res12).isEqualTo("3"); + // REMOVE_END + return asyncCommands.hmget( + "bike:1:stats","crashes", "owners" + ); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res.toString()).isEqualTo("[KeyValue[crashes, 1], KeyValue[owners, 1]]"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) + // >>> [KeyValue[crashes, 1], KeyValue[owners, 1]] + .toCompletableFuture(); + // STEP_END + CompletableFuture.allOf( // REMOVE_START delResult, - // REMOVE_END - hmGet - ); + // REMOVE_END, + hIncrBy, + incrByGetMget + ).join(); } finally { redisClient.shutdown(); } From b74d0c6bc97fec1a1ea5546470886ff58a29cc79 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 3 Dec 2024 10:40:58 +0000 Subject: [PATCH 3/4] DOC-4528 formatting changes --- .../io/redis/examples/async/HashExample.java | 192 +++++++----------- 1 file changed, 79 insertions(+), 113 deletions(-) diff --git a/src/test/java/io/redis/examples/async/HashExample.java b/src/test/java/io/redis/examples/async/HashExample.java index 1eb5645cf..8e93e7fe5 100644 --- a/src/test/java/io/redis/examples/async/HashExample.java +++ b/src/test/java/io/redis/examples/async/HashExample.java @@ -27,64 +27,55 @@ public void run() { try (StatefulRedisConnection connection = redisClient.connect()) { RedisAsyncCommands asyncCommands = connection.async(); // REMOVE_START - CompletableFuture delResult = asyncCommands.del("bike:1", "bike:1:stats") - .toCompletableFuture(); + CompletableFuture delResult = asyncCommands.del("bike:1", "bike:1:stats").toCompletableFuture(); // REMOVE_END - //STEP_START set_get_all + // STEP_START set_get_all Map bike1 = new HashMap<>(); bike1.put("model", "Deimos"); bike1.put("brand", "Ergonom"); bike1.put("type", "Enduro bikes"); bike1.put("price", "4972"); - CompletableFuture setGetAll = asyncCommands.hset( - "bike:1", bike1 - ) - .thenCompose(res1 -> { - System.out.println(res1); // >>> 4 - // REMOVE_START - assertThat(res1).isEqualTo(4); - // REMOVE_END - return asyncCommands.hget("bike:1", "model"); - }) - .thenCompose(res2 -> { - System.out.println(res2); // >>> Deimos - // REMOVE_START - assertThat(res2).isEqualTo("Deimos"); - // REMOVE_END - return asyncCommands.hget("bike:1", "price"); - }) - .thenCompose(res3 -> { - System.out.println(res3); // >>> 4972 - // REMOVE_START - assertThat(res3).isEqualTo("4972"); - // REMOVE_END - return asyncCommands.hgetall("bike:1"); - }) + CompletableFuture setGetAll = asyncCommands.hset("bike:1", bike1).thenCompose(res1 -> { + System.out.println(res1); // >>> 4 + // REMOVE_START + assertThat(res1).isEqualTo(4); + // REMOVE_END + return asyncCommands.hget("bike:1", "model"); + }).thenCompose(res2 -> { + System.out.println(res2); // >>> Deimos + // REMOVE_START + assertThat(res2).isEqualTo("Deimos"); + // REMOVE_END + return asyncCommands.hget("bike:1", "price"); + }).thenCompose(res3 -> { + System.out.println(res3); // >>> 4972 + // REMOVE_START + assertThat(res3).isEqualTo("4972"); + // REMOVE_END + return asyncCommands.hgetall("bike:1"); + }) // REMOVE_START .thenApply(res -> { assertThat(res.get("type")).isEqualTo("Enduro bikes"); assertThat(res.get("brand")).isEqualTo("Ergonom"); assertThat(res.get("price")).isEqualTo("4972"); assertThat(res.get("model")).isEqualTo("Deimos"); - + return res; }) // REMOVE_END .thenAccept(System.out::println) // >>> {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos} .toCompletableFuture(); - //STEP_END + // STEP_END // STEP_START hmget - CompletableFuture hmGet = setGetAll - .thenCompose(res4 -> { - return asyncCommands.hmget( - "bike:1","model", "price" - ); - }) + CompletableFuture hmGet = setGetAll.thenCompose(res4 -> { + return asyncCommands.hmget("bike:1", "model", "price"); + }) // REMOVE_START .thenApply(res -> { assertThat(res.toString()).isEqualTo("[KeyValue[model, Deimos], KeyValue[price, 4972]]"); @@ -97,19 +88,15 @@ public void run() { // STEP_END // STEP_START hincrby - CompletableFuture hIncrBy = hmGet - .thenCompose(r -> { - return asyncCommands.hincrby( - "bike:1", "price", 100 - ); - }) - .thenCompose(res6 -> { - System.out.println(res6); // >>> 5072 - // REMOVE_START - assertThat(res6).isEqualTo(5072L); - // REMOVE_END - return asyncCommands.hincrby("bike:1", "price", -100); - }) + CompletableFuture hIncrBy = hmGet.thenCompose(r -> { + return asyncCommands.hincrby("bike:1", "price", 100); + }).thenCompose(res6 -> { + System.out.println(res6); // >>> 5072 + // REMOVE_START + assertThat(res6).isEqualTo(5072L); + // REMOVE_END + return asyncCommands.hincrby("bike:1", "price", -100); + }) // REMOVE_START .thenApply(res -> { assertThat(res).isEqualTo(4972L); @@ -120,65 +107,45 @@ public void run() { // >>> 4972 .toCompletableFuture(); // STEP_END - + // STEP_START incrby_get_mget - CompletableFuture incrByGetMget = asyncCommands.hincrby( - "bike:1:stats","rides", 1 - ) - .thenCompose(res7 -> { - System.out.println(res7); // >>> 1 - // REMOVE_START - assertThat(res7).isEqualTo(1L); - // REMOVE_END - return asyncCommands.hincrby( - "bike:1:stats","rides", 1 - ); - }) - .thenCompose(res8 -> { - System.out.println(res8); // >>> 2 - // REMOVE_START - assertThat(res8).isEqualTo(2L); - // REMOVE_END - return asyncCommands.hincrby( - "bike:1:stats","rides", 1 - ); - }) - .thenCompose(res9 -> { - System.out.println(res9); // >>> 3 - // REMOVE_START - assertThat(res9).isEqualTo(3L); - // REMOVE_END - return asyncCommands.hincrby( - "bike:1:stats","crashes", 1 - ); - }) - .thenCompose(res10 -> { - System.out.println(res10); // >>> 1 - // REMOVE_START - assertThat(res10).isEqualTo(1L); - // REMOVE_END - return asyncCommands.hincrby( - "bike:1:stats","owners", 1 - ); - }) - .thenCompose(res11 -> { - System.out.println(res11); // >>> 1 - // REMOVE_START - assertThat(res11).isEqualTo(1L); - // REMOVE_END - return asyncCommands.hget( - "bike:1:stats","rides" - ); - }) - .thenCompose(res12 -> { - System.out.println(res12); // >>> 3 - // REMOVE_START - assertThat(res12).isEqualTo("3"); - // REMOVE_END - return asyncCommands.hmget( - "bike:1:stats","crashes", "owners" - ); - }) + CompletableFuture incrByGetMget = asyncCommands.hincrby("bike:1:stats", "rides", 1).thenCompose(res7 -> { + System.out.println(res7); // >>> 1 + // REMOVE_START + assertThat(res7).isEqualTo(1L); + // REMOVE_END + return asyncCommands.hincrby("bike:1:stats", "rides", 1); + }).thenCompose(res8 -> { + System.out.println(res8); // >>> 2 + // REMOVE_START + assertThat(res8).isEqualTo(2L); + // REMOVE_END + return asyncCommands.hincrby("bike:1:stats", "rides", 1); + }).thenCompose(res9 -> { + System.out.println(res9); // >>> 3 + // REMOVE_START + assertThat(res9).isEqualTo(3L); + // REMOVE_END + return asyncCommands.hincrby("bike:1:stats", "crashes", 1); + }).thenCompose(res10 -> { + System.out.println(res10); // >>> 1 + // REMOVE_START + assertThat(res10).isEqualTo(1L); + // REMOVE_END + return asyncCommands.hincrby("bike:1:stats", "owners", 1); + }).thenCompose(res11 -> { + System.out.println(res11); // >>> 1 + // REMOVE_START + assertThat(res11).isEqualTo(1L); + // REMOVE_END + return asyncCommands.hget("bike:1:stats", "rides"); + }).thenCompose(res12 -> { + System.out.println(res12); // >>> 3 + // REMOVE_START + assertThat(res12).isEqualTo("3"); + // REMOVE_END + return asyncCommands.hmget("bike:1:stats", "crashes", "owners"); + }) // REMOVE_START .thenApply(res -> { assertThat(res.toString()).isEqualTo("[KeyValue[crashes, 1], KeyValue[owners, 1]]"); @@ -191,14 +158,13 @@ public void run() { // STEP_END CompletableFuture.allOf( - // REMOVE_START - delResult, - // REMOVE_END, - hIncrBy, - incrByGetMget - ).join(); + // REMOVE_START + delResult, + // REMOVE_END, + hIncrBy, incrByGetMget).join(); } finally { redisClient.shutdown(); } } -} \ No newline at end of file + +} From 92f338d781e3eb8141e8fa852a7a135f58293897 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 3 Dec 2024 11:00:51 +0000 Subject: [PATCH 4/4] DOC-4528 removed unused imports --- src/test/java/io/redis/examples/async/HashExample.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/test/java/io/redis/examples/async/HashExample.java b/src/test/java/io/redis/examples/async/HashExample.java index 8e93e7fe5..eba2f4e0c 100644 --- a/src/test/java/io/redis/examples/async/HashExample.java +++ b/src/test/java/io/redis/examples/async/HashExample.java @@ -8,12 +8,8 @@ // REMOVE_START import org.junit.jupiter.api.Test; // REMOVE_END -import org.testcontainers.shaded.org.checkerframework.checker.units.qual.s; - import java.util.*; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; - // REMOVE_START import static org.assertj.core.api.Assertions.assertThat; // REMOVE_END