-
Notifications
You must be signed in to change notification settings - Fork 1k
DOC-4531 set data type examples #3076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3214c19
DOC-4531 added examples for sets page
andy-stark-redis 60768b8
DOC-4531 copied strings example from doctest branch
andy-stark-redis c3abe0f
Merge branch 'main' into DOC-4531-set-examples
andy-stark-redis ddb6b9c
Revert "DOC-4531 copied strings example from doctest branch"
andy-stark-redis 6482c10
Merge branch 'DOC-4531-set-examples' of github.com:andy-stark-redis/l…
andy-stark-redis 131bdbf
Merge branch 'main' into DOC-4531-set-examples
andy-stark-redis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> connection = redisClient.connect()) { | ||
RedisAsyncCommands<String, String> asyncCommands = connection.async(); | ||
// REMOVE_START | ||
CompletableFuture<Long> delResult = asyncCommands | ||
.del("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").toCompletableFuture(); | ||
// REMOVE_END | ||
|
||
// STEP_START sadd | ||
CompletableFuture<Void> 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<Void> 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<Void> 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<Void> 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<Void> 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<String> 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<Void> 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<Void> 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<Void> 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<Void> 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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.