From 0aa013c1ce3e6ba97611c9c37fba174f0e0a0e92 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 13 Jan 2025 15:54:33 +0000 Subject: [PATCH 1/4] DOC-4450 added hgetall and hvals command examples --- .../io/redis/examples/CmdsHashExample.java | 269 +++--------------- 1 file changed, 42 insertions(+), 227 deletions(-) diff --git a/src/test/java/io/redis/examples/CmdsHashExample.java b/src/test/java/io/redis/examples/CmdsHashExample.java index cfbb0b340a..ca3ae1f0c6 100644 --- a/src/test/java/io/redis/examples/CmdsHashExample.java +++ b/src/test/java/io/redis/examples/CmdsHashExample.java @@ -4,11 +4,14 @@ import org.junit.Assert; import org.junit.Test; +// REMOVE_END import java.util.HashMap; import java.util.Map; +import java.util.List; +import java.util.Collections; +import static java.util.stream.Collectors.*; -// REMOVE_END // HIDE_START import redis.clients.jedis.UnifiedJedis; // HIDE_END @@ -18,64 +21,12 @@ public class CmdsHashExample { @Test public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); - //REMOVE_START // Clear any keys here before using them in tests. jedis.del("myhash"); //REMOVE_END // HIDE_END - - // STEP_START hdel - - // STEP_END - - // Tests for 'hdel' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hexists - - // STEP_END - - // Tests for 'hexists' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hexpire - - // STEP_END - - // Tests for 'hexpire' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hexpireat - - // STEP_END - - // Tests for 'hexpireat' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hexpiretime - - // STEP_END - - // Tests for 'hexpiretime' step. - // REMOVE_START - - // REMOVE_END - - // STEP_START hget Map hGetExampleParams = new HashMap<>(); hGetExampleParams.put("field1", "foo"); @@ -89,156 +40,43 @@ public void run() { String hGetResult3 = jedis.hget("myhash", "field2"); System.out.println(hGetResult3); // >>> null // STEP_END - - // Tests for 'hget' step. // REMOVE_START + // Tests for 'hget' step. Assert.assertEquals(1, hGetResult1); Assert.assertEquals("foo", hGetResult2); Assert.assertNull(hGetResult3); jedis.del("myhash"); // REMOVE_END - // STEP_START hgetall + Map hGetAllExampleParams = new HashMap<>(); + hGetAllExampleParams.put("field1", "Hello"); + hGetAllExampleParams.put("field2", "World"); - // STEP_END - - // Tests for 'hgetall' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hincrby - - // STEP_END - - // Tests for 'hincrby' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hincrbyfloat - - // STEP_END - - // Tests for 'hincrbyfloat' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hkeys - - // STEP_END - - // Tests for 'hkeys' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hlen - - // STEP_END - - // Tests for 'hlen' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hmget - - // STEP_END - - // Tests for 'hmget' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hmset + long hGetAllResult1 = jedis.hset("myhash", hGetAllExampleParams); + System.out.println(hGetAllResult1); // >>> 2 + Map hGetAllResult2 = jedis.hgetAll("myhash"); + System.out.println( + hGetAllResult2.entrySet().stream() + .sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey())) + .collect(toList()) + .toString() + ); + // >>> [field1=Hello, field2=World] // STEP_END - - // Tests for 'hmset' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hpersist - - // STEP_END - - // Tests for 'hpersist' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hpexpire - - // STEP_END - - // Tests for 'hpexpire' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hpexpireat - - // STEP_END - - // Tests for 'hpexpireat' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hpexpiretime - - // STEP_END - - // Tests for 'hpexpiretime' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hpttl - - // STEP_END - - // Tests for 'hpttl' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hrandfield - - // STEP_END - - // Tests for 'hrandfield' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hscan - - // STEP_END - - // Tests for 'hscan' step. // REMOVE_START - + // Tests for 'hgetall' step. + Assert.assertEquals(2, hGetAllResult1); + Assert.assertEquals("[field1=Hello, field2=World]", + hGetAllResult2.entrySet().stream() + .sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey())) + .collect(toList()) + .toString() + ); + jedis.del("myhash"); // REMOVE_END - // STEP_START hset Map hSetExampleParams = new HashMap<>(); hSetExampleParams.put("field1", "Hello"); @@ -269,9 +107,8 @@ public void run() { // >>> Key: field2, Value: Hi // >>> Key: field1, Value: Hello // STEP_END - - // Tests for 'hset' step. // REMOVE_START + // Tests for 'hset' step. Assert.assertEquals(1, hSetResult1); Assert.assertEquals("Hello", hSetResult2); Assert.assertEquals(2, hSetResult3); @@ -284,47 +121,25 @@ public void run() { jedis.del("myhash"); // REMOVE_END - - // STEP_START hsetnx - - // STEP_END - - // Tests for 'hsetnx' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START hstrlen - - // STEP_END - - // Tests for 'hstrlen' step. - // REMOVE_START - - // REMOVE_END - - - // STEP_START httl - - // STEP_END - - // Tests for 'httl' step. - // REMOVE_START - - // REMOVE_END - - // STEP_START hvals + Map hValsExampleParams = new HashMap<>(); + hValsExampleParams.put("field1", "Hello"); + hValsExampleParams.put("field2", "World"); - // STEP_END + long hValsResult1 = jedis.hset("myhash", hValsExampleParams); + System.out.println(hValsResult1); // >>> 2 + List hValsResult2 = jedis.hvals("myhash"); + Collections.sort(hValsResult2); + System.out.println(hValsResult2); + // >>> [Hello, World] + // STEP_END + // REMOVE_START // Tests for 'hvals' step. - // REMOVE_START - + Assert.assertEquals(2, hValsResult1); + Assert.assertEquals("[Hello, World]", hValsResult2.toString()); + jedis.del("myhash"); // REMOVE_END - - // HIDE_START } From 173f251d0f6544e082e278938fde9a7267bc1f32 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 13 Jan 2025 16:51:32 +0000 Subject: [PATCH 2/4] DOC-4450 added double indent for 'chained' stream functions to match other source files --- src/test/java/io/redis/examples/CmdsHashExample.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/io/redis/examples/CmdsHashExample.java b/src/test/java/io/redis/examples/CmdsHashExample.java index ca3ae1f0c6..6da15447bb 100644 --- a/src/test/java/io/redis/examples/CmdsHashExample.java +++ b/src/test/java/io/redis/examples/CmdsHashExample.java @@ -59,9 +59,9 @@ public void run() { Map hGetAllResult2 = jedis.hgetAll("myhash"); System.out.println( hGetAllResult2.entrySet().stream() - .sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey())) - .collect(toList()) - .toString() + .sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey())) + .collect(toList()) + .toString() ); // >>> [field1=Hello, field2=World] // STEP_END @@ -70,9 +70,9 @@ public void run() { Assert.assertEquals(2, hGetAllResult1); Assert.assertEquals("[field1=Hello, field2=World]", hGetAllResult2.entrySet().stream() - .sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey())) - .collect(toList()) - .toString() + .sorted((s1, s2)-> s1.getKey().compareTo(s2.getKey())) + .collect(toList()) + .toString() ); jedis.del("myhash"); // REMOVE_END From 10b0d56289f6f9536b2acfbf046ffedc27c1322f Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 14 Jan 2025 10:42:22 +0000 Subject: [PATCH 3/4] DOC-4450 tidied up imports as suggested in feedback --- src/test/java/io/redis/examples/CmdsHashExample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/redis/examples/CmdsHashExample.java b/src/test/java/io/redis/examples/CmdsHashExample.java index 6da15447bb..62f32cbab1 100644 --- a/src/test/java/io/redis/examples/CmdsHashExample.java +++ b/src/test/java/io/redis/examples/CmdsHashExample.java @@ -5,12 +5,12 @@ import org.junit.Assert; import org.junit.Test; // REMOVE_END +import static java.util.stream.Collectors.toList; import java.util.HashMap; import java.util.Map; import java.util.List; import java.util.Collections; -import static java.util.stream.Collectors.*; // HIDE_START import redis.clients.jedis.UnifiedJedis; From 5e459e93b22413c4f529a32196f850e48dd48c34 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:45:37 +0600 Subject: [PATCH 4/4] Format CmdsHashExample.java imports --- src/test/java/io/redis/examples/CmdsHashExample.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/redis/examples/CmdsHashExample.java b/src/test/java/io/redis/examples/CmdsHashExample.java index 62f32cbab1..78e12cf820 100644 --- a/src/test/java/io/redis/examples/CmdsHashExample.java +++ b/src/test/java/io/redis/examples/CmdsHashExample.java @@ -5,7 +5,6 @@ import org.junit.Assert; import org.junit.Test; // REMOVE_END -import static java.util.stream.Collectors.toList; import java.util.HashMap; import java.util.Map; @@ -16,6 +15,8 @@ import redis.clients.jedis.UnifiedJedis; // HIDE_END +import static java.util.stream.Collectors.toList; + // HIDE_START public class CmdsHashExample { @Test