Skip to content

Commit 5911303

Browse files
DOC-4531 set data type examples (#3076)
* DOC-4531 added examples for sets page * DOC-4531 copied strings example from doctest branch * Revert "DOC-4531 copied strings example from doctest branch" This reverts commit 60768b8.
1 parent 6a05e17 commit 5911303

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed
+250
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
// EXAMPLE: sets_tutorial
2+
package io.redis.examples.async;
3+
4+
import io.lettuce.core.*;
5+
import io.lettuce.core.api.async.RedisAsyncCommands;
6+
import io.lettuce.core.api.StatefulRedisConnection;
7+
8+
// REMOVE_START
9+
import org.junit.jupiter.api.Test;
10+
11+
// REMOVE_END
12+
import java.util.*;
13+
import static java.util.stream.Collectors.*;
14+
import java.util.concurrent.CompletableFuture;
15+
16+
// REMOVE_START
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
// REMOVE_END
19+
20+
public class SetExample {
21+
22+
@Test
23+
public void run() {
24+
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
25+
26+
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
27+
RedisAsyncCommands<String, String> asyncCommands = connection.async();
28+
// REMOVE_START
29+
CompletableFuture<Long> delResult = asyncCommands
30+
.del("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").toCompletableFuture();
31+
// REMOVE_END
32+
33+
// STEP_START sadd
34+
CompletableFuture<Void> sAdd = asyncCommands.sadd("bikes:racing:france", "bike:1").thenCompose(res1 -> {
35+
System.out.println(res1); // >>> 1
36+
37+
// REMOVE_START
38+
assertThat(res1).isEqualTo(1);
39+
// REMOVE_END
40+
return asyncCommands.sadd("bikes:racing:france", "bike:1");
41+
}).thenCompose(res2 -> {
42+
System.out.println(res2); // >>> 0
43+
44+
// REMOVE_START
45+
assertThat(res2).isEqualTo(0);
46+
// REMOVE_END
47+
return asyncCommands.sadd("bikes:racing:france", "bike:2", "bike:3");
48+
}).thenCompose(res3 -> {
49+
System.out.println(res3); // >>> 2
50+
51+
// REMOVE_START
52+
assertThat(res3).isEqualTo(2);
53+
// REMOVE_END
54+
return asyncCommands.sadd("bikes:racing:usa", "bike:1", "bike:4");
55+
})
56+
// REMOVE_START
57+
.thenApply(res -> {
58+
assertThat(res).isEqualTo(2);
59+
return res;
60+
})
61+
// REMOVE_END
62+
.thenAccept(System.out::println)
63+
// >>> 2
64+
.toCompletableFuture();
65+
// STEP_END
66+
67+
// STEP_START sismember
68+
CompletableFuture<Void> sIsMember = sAdd.thenCompose(r -> {
69+
return asyncCommands.sismember("bikes:racing:usa", "bike:1");
70+
}).thenCompose(res4 -> {
71+
System.out.println(res4); // >>> true
72+
73+
// REMOVE_START
74+
assertThat(res4).isTrue();
75+
// REMOVE_END
76+
return asyncCommands.sismember("bikes:racing:usa", "bike:2");
77+
})
78+
// REMOVE_START
79+
.thenApply(r -> {
80+
assertThat(r).isFalse();
81+
return r;
82+
})
83+
// REMOVE_END
84+
.thenAccept(System.out::println) // >>> false
85+
.toCompletableFuture();
86+
// STEP_END
87+
88+
// STEP_START sinter
89+
CompletableFuture<Void> sInter = sIsMember.thenCompose(r -> {
90+
return asyncCommands.sinter("bikes:racing:france", "bikes:racing:usa");
91+
})
92+
// REMOVE_START
93+
.thenApply(r -> {
94+
assertThat(r.toString()).isEqualTo("[bike:1]");
95+
return r;
96+
})
97+
// REMOVE_END
98+
.thenAccept(System.out::println) // >>> ["bike:1"]
99+
.toCompletableFuture();
100+
// STEP_END
101+
102+
// STEP_START scard
103+
CompletableFuture<Void> sCard = sInter.thenCompose(r -> {
104+
return asyncCommands.scard("bikes:racing:france");
105+
})
106+
// REMOVE_START
107+
.thenApply(r -> {
108+
assertThat(r).isEqualTo(3);
109+
return r;
110+
})
111+
// REMOVE_END
112+
.thenAccept(System.out::println) // >>> 3
113+
.toCompletableFuture();
114+
// STEP_END
115+
116+
// STEP_START sadd_smembers
117+
CompletableFuture<Void> sAddSMembers = sCard.thenCompose(r -> {
118+
return asyncCommands.del("bikes:racing:france");
119+
}).thenCompose(r -> {
120+
return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
121+
}).thenCompose(res5 -> {
122+
System.out.println(res5); // >>> 3
123+
return asyncCommands.smembers("bikes:racing:france");
124+
})
125+
// REMOVE_START
126+
.thenApply((Set<String> r) -> {
127+
assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:1, bike:2, bike:3]");
128+
return r;
129+
})
130+
// REMOVE_END
131+
.thenAccept(System.out::println)
132+
// >>> [bike:1, bike:2, bike:3]
133+
.toCompletableFuture();
134+
// STEP_END
135+
136+
// STEP_START smismember
137+
CompletableFuture<Void> sMIsMember = sAddSMembers.thenCompose(r -> {
138+
return asyncCommands.sismember("bikes:racing:france", "bike:1");
139+
}).thenCompose(res6 -> {
140+
System.out.println(res6); // >>> True
141+
// REMOVE_START
142+
assertThat(res6).isTrue();
143+
// REMOVE_END
144+
return asyncCommands.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4");
145+
})
146+
// REMOVE_START
147+
.thenApply(r -> {
148+
assertThat(r).containsSequence(true, true, false);
149+
return r;
150+
})
151+
// REMOVE_END
152+
.thenAccept(System.out::println) // >>> [true, true, false]
153+
.toCompletableFuture();
154+
// STEP_END
155+
156+
// STEP_START sdiff
157+
CompletableFuture<Void> sDiff = sMIsMember.thenCompose(r -> {
158+
return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
159+
}).thenCompose(r -> {
160+
return asyncCommands.sadd("bikes:racing:usa", "bike:1", "bike:4");
161+
}).thenCompose(r -> {
162+
return asyncCommands.sdiff("bikes:racing:france", "bikes:racing:usa");
163+
})
164+
// REMOVE_START
165+
.thenApply(r -> {
166+
assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:2, bike:3]");
167+
return r;
168+
})
169+
// REMOVE_END
170+
.thenAccept(System.out::println) // >>> [bike:2, bike:3]
171+
.toCompletableFuture();
172+
// STEP_END
173+
174+
// STEP_START multisets
175+
CompletableFuture<Void> multisets = sDiff.thenCompose(r -> {
176+
return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
177+
}).thenCompose(r -> {
178+
return asyncCommands.sadd("bikes:racing:usa", "bike:1", "bike:4");
179+
}).thenCompose(r -> {
180+
return asyncCommands.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4");
181+
}).thenCompose(r -> {
182+
return asyncCommands.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
183+
}).thenCompose(res7 -> {
184+
System.out.println(res7); // >>> [bike:1]
185+
// REMOVE_START
186+
assertThat(res7.toString()).isEqualTo("[bike:1]");
187+
// REMOVE_END
188+
return asyncCommands.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
189+
}).thenCompose(res8 -> {
190+
System.out.println(res8);
191+
// >>> [bike:1, bike:2, bike:3, bike:4]
192+
// REMOVE_START
193+
assertThat(res8.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:1, bike:2, bike:3, bike:4]");
194+
// REMOVE_END
195+
return asyncCommands.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
196+
}).thenCompose(res9 -> {
197+
System.out.println(res9); // >>> []
198+
// REMOVE_START
199+
assertThat(res9.toString()).isEqualTo("[]");
200+
// REMOVE_END
201+
return asyncCommands.sdiff("bikes:racing:usa", "bikes:racing:france");
202+
}).thenCompose(res10 -> {
203+
System.out.println(res10); // >>> [bike:4]
204+
// REMOVE_START
205+
assertThat(res10.toString()).isEqualTo("[bike:4]");
206+
// REMOVE_END
207+
return asyncCommands.sdiff("bikes:racing:france", "bikes:racing:usa");
208+
})
209+
// REMOVE_START
210+
.thenApply(r -> {
211+
assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[bike:2, bike:3]");
212+
return r;
213+
})
214+
// REMOVE_END
215+
.thenAccept(System.out::println) // >>> [bike:2, bike:3]
216+
.toCompletableFuture();
217+
// STEP_END
218+
219+
// STEP_START srem
220+
CompletableFuture<Void> sRem = multisets.thenCompose(r -> {
221+
return asyncCommands.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
222+
}).thenCompose(r -> {
223+
return asyncCommands.srem("bikes:racing:france", "bike:1");
224+
}).thenCompose(res11 -> {
225+
System.out.println(res11); // >>> 1
226+
// REMOVE_START
227+
assertThat(res11).isEqualTo(1);
228+
// REMOVE_END
229+
return asyncCommands.spop("bikes:racing:france");
230+
}).thenCompose(res12 -> {
231+
System.out.println(res12); // >>> bike:3 (for example)
232+
return asyncCommands.smembers("bikes:racing:france");
233+
}).thenCompose(res13 -> {
234+
System.out.println(res13); // >>> [bike:2, bike:4, bike:5]
235+
return asyncCommands.srandmember("bikes:racing:france");
236+
}).thenAccept(System.out::println) // >>> bike:4
237+
.toCompletableFuture();
238+
// STEP_END
239+
240+
CompletableFuture.allOf(
241+
// REMOVE_START
242+
delResult,
243+
// REMOVE_END,
244+
sRem).join();
245+
} finally {
246+
redisClient.shutdown();
247+
}
248+
}
249+
250+
}

0 commit comments

Comments
 (0)