Skip to content

Commit df66f9c

Browse files
DOC-5226 probabilistic data type examples
1 parent 519c9c2 commit df66f9c

File tree

1 file changed

+311
-0
lines changed

1 file changed

+311
-0
lines changed

tests/Doc/HomeProbExample.cs

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
// EXAMPLE: home_prob_dts
2+
using NRedisStack.RedisStackCommands;
3+
using StackExchange.Redis;
4+
5+
// REMOVE_START
6+
using NRedisStack.Tests;
7+
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Serialization;
8+
using Org.BouncyCastle.Crypto.Generators;
9+
10+
namespace Doc;
11+
12+
[Collection("DocsTests")]
13+
// REMOVE_END
14+
15+
// HIDE_START
16+
public class HomeProbExample
17+
// REMOVE_START
18+
: AbstractNRedisStackTest, IDisposable
19+
// REMOVE_END
20+
{
21+
// REMOVE_START
22+
public HomeProbExample(EndpointsFixture fixture) : base(fixture) { }
23+
24+
[SkippableFact]
25+
// REMOVE_END
26+
public void run()
27+
{
28+
// REMOVE_START
29+
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
30+
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
31+
var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
32+
// REMOVE_END
33+
34+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
35+
var db = muxer.GetDatabase();
36+
// REMOVE_START
37+
// Clear any keys here before using them in tests.
38+
db.KeyDelete(new RedisKey[] {
39+
"recorded_users", "other_users",
40+
"group:1", "group:2", "both_groups",
41+
"items_sold",
42+
"male_heights", "female_heights", "all_heights",
43+
"top_3_songs"
44+
});
45+
// REMOVE_END
46+
// HIDE_END
47+
48+
// STEP_START bloom
49+
bool[] res1 = db.BF().MAdd(
50+
"recorded_users", "andy", "cameron", "david", "michelle"
51+
);
52+
Console.WriteLine(string.Join(", ", res1));
53+
// >>> true, true, true, true
54+
55+
bool res2 = db.BF().Exists("recorded_users", "cameron");
56+
Console.WriteLine(res2); // >>> true
57+
58+
bool res3 = db.BF().Exists("recorded_users", "kaitlyn");
59+
Console.WriteLine(res3); // >>> false
60+
// STEP_END
61+
// REMOVE_START
62+
Assert.Equal(new bool[] { true, true, true, true }, res1);
63+
// REMOVE_END
64+
65+
// STEP_START cuckoo
66+
bool res4 = db.CF().Add("other_users", "paolo");
67+
Console.WriteLine(res4); // >>> true
68+
69+
bool res5 = db.CF().Add("other_users", "kaitlyn");
70+
Console.WriteLine(res5); // >>> true
71+
72+
bool res6 = db.CF().Add("other_users", "rachel");
73+
Console.WriteLine(res6); // >>> true
74+
75+
bool[] res7 = db.CF().MExists("other_users", "paolo", "rachel", "andy");
76+
Console.WriteLine(string.Join(", ", res7));
77+
// >>> true, true, false
78+
79+
bool res8 = db.CF().Del("other_users", "paolo");
80+
Console.WriteLine(res8); // >>> true
81+
82+
bool res9 = db.CF().Exists("other_users", "paolo");
83+
Console.WriteLine(res9); // >>> false
84+
// STEP_END
85+
// REMOVE_START
86+
Assert.True(res4);
87+
Assert.True(res5);
88+
Assert.True(res6);
89+
Assert.Equal(new bool[] { true, true, false }, res7);
90+
Assert.True(res8);
91+
Assert.False(res9);
92+
// REMOVE_END
93+
94+
// STEP_START hyperloglog
95+
bool res10 = db.HyperLogLogAdd(
96+
"group:1",
97+
new RedisValue[] { "andy", "cameron", "david" }
98+
);
99+
Console.WriteLine(res10); // >>> true
100+
101+
long res11 = db.HyperLogLogLength("group:1");
102+
Console.WriteLine(res11); // >>> 3
103+
104+
bool res12 = db.HyperLogLogAdd(
105+
"group:2",
106+
new RedisValue[] { "kaitlyn", "michelle", "paolo", "rachel" }
107+
);
108+
Console.WriteLine(res12); // >>> true
109+
110+
long res13 = db.HyperLogLogLength("group:2");
111+
Console.WriteLine(res13); // >>> 4
112+
113+
db.HyperLogLogMerge(
114+
"both_groups",
115+
"group:1", "group:2"
116+
);
117+
118+
long res14 = db.HyperLogLogLength("both_groups");
119+
Console.WriteLine(res14); // >>> 7
120+
// STEP_END
121+
// REMOVE_START
122+
Assert.True(res10);
123+
Assert.Equal(3, res11);
124+
Assert.True(res12);
125+
Assert.Equal(4, res13);
126+
Assert.Equal(7, res14);
127+
// REMOVE_END
128+
129+
// STEP_START cms
130+
// Specify that you want to keep the counts within 0.01
131+
// (0.1%) of the true value with a 0.005 (0.05%) chance
132+
// of going outside this limit.
133+
bool res15 = db.CMS().InitByProb("items_sold", 0.01, 0.005);
134+
Console.WriteLine(res15); // >>> true
135+
136+
long[] res16 = db.CMS().IncrBy(
137+
"items_sold",
138+
new Tuple<RedisValue, long>[]{
139+
new("bread", 300),
140+
new("tea", 200),
141+
new("coffee", 200),
142+
new("beer", 100)
143+
}
144+
);
145+
Console.WriteLine(string.Join(", ", res16));
146+
// >>> 300, 200, 200, 100
147+
148+
long[] res17 = db.CMS().IncrBy(
149+
"items_sold",
150+
new Tuple<RedisValue, long>[]{
151+
new("bread", 100),
152+
new("coffee", 150),
153+
}
154+
);
155+
Console.WriteLine(string.Join(", ", res17));
156+
// >>> 400, 350
157+
158+
long[] res18 = db.CMS().Query(
159+
"items_sold",
160+
"bread", "tea", "coffee", "beer"
161+
);
162+
Console.WriteLine(string.Join(", ", res18));
163+
// >>> 400, 200, 350, 100
164+
// STEP_END
165+
// REMOVE_START
166+
Assert.True(res15);
167+
Assert.Equal(new long[] { 300, 200, 200, 100 }, res16);
168+
Assert.Equal(new long[] { 400, 350 }, res17);
169+
Assert.Equal(new long[] { 400, 200, 350, 100 }, res18);
170+
// REMOVE_END
171+
172+
// STEP_START tdigest
173+
bool res19 = db.TDIGEST().Create("male_heights");
174+
Console.WriteLine(res19); // >>> true
175+
176+
bool res20 = db.TDIGEST().Add(
177+
"male_heights",
178+
175.5, 181, 160.8, 152, 177, 196, 164
179+
);
180+
Console.WriteLine(res20); // >>> true
181+
182+
double res21 = db.TDIGEST().Min("male_heights");
183+
Console.WriteLine(res21); // >>> 152.0
184+
185+
double res22 = db.TDIGEST().Max("male_heights");
186+
Console.WriteLine(res22); // >>> 196.0
187+
188+
double[] res23 = db.TDIGEST().Quantile("male_heights", 0.75);
189+
Console.WriteLine(string.Join(", ", res23)); // >>> 181.0
190+
191+
// Note that the CDF value for 181.0 is not exactly
192+
// 0.75. Both values are estimates.
193+
double[] res24 = db.TDIGEST().CDF("male_heights", 181.0);
194+
Console.WriteLine(string.Join(", ", res24)); // >>> 0.7857142857142857
195+
196+
bool res25 = db.TDIGEST().Create("female_heights");
197+
Console.WriteLine(res25); // >>> true
198+
199+
bool res26 = db.TDIGEST().Add(
200+
"female_heights",
201+
155.5, 161, 168.5, 170, 157.5, 163, 171
202+
);
203+
Console.WriteLine(res26); // >>> true
204+
205+
double[] res27 = db.TDIGEST().Quantile("female_heights", 0.75);
206+
Console.WriteLine(string.Join(", ", res27)); // >>> 170.0
207+
208+
// Specify 0 for `compression` and false for `override`.
209+
bool res28 = db.TDIGEST().Merge(
210+
"all_heights", 0, false, "male_heights", "female_heights"
211+
);
212+
Console.WriteLine(res28); // >>> true
213+
214+
double[] res29 = db.TDIGEST().Quantile("all_heights", 0.75);
215+
Console.WriteLine(string.Join(", ", res29)); // >>> 175.5
216+
// STEP_END
217+
// REMOVE_START
218+
Assert.True(res19);
219+
Assert.True(res20);
220+
Assert.Equal(152.0, res21);
221+
Assert.Equal(196.0, res22);
222+
Assert.Equal(new double[] { 181.0 }, res23);
223+
Assert.Equal(new double[] { 0.7857142857142857 }, res24);
224+
Assert.True(res25);
225+
Assert.True(res26);
226+
Assert.Equal(new double[] { 170.0 }, res27);
227+
Assert.True(res28);
228+
Assert.Equal(new double[] { 175.5 }, res29);
229+
// REMOVE_END
230+
231+
// STEP_START topk
232+
bool res30 = db.TOPK().Reserve("top_3_songs", 3, 7, 8, 0.9);
233+
Console.WriteLine(res30); // >>> true
234+
235+
RedisResult[] res31 = db.TOPK().IncrBy(
236+
"top_3_songs",
237+
new Tuple<RedisValue, long>[] {
238+
new("Starfish Trooper", 3000),
239+
new("Only one more time", 1850),
240+
new("Rock me, Handel", 1325),
241+
new("How will anyone know?", 3890),
242+
new("Average lover", 4098),
243+
new("Road to everywhere", 770)
244+
}
245+
);
246+
Console.WriteLine(
247+
string.Join(
248+
", ",
249+
string.Join(
250+
", ",
251+
res31.Select(
252+
r => $"{(r.IsNull ? "Null" : r)}"
253+
)
254+
)
255+
)
256+
);
257+
// >>> Null, Null, Null, Rock me, Handel, Only one more time, Null
258+
259+
RedisResult[] res32 = db.TOPK().List("top_3_songs");
260+
Console.WriteLine(
261+
string.Join(
262+
", ",
263+
string.Join(
264+
", ",
265+
res32.Select(
266+
r => $"{(r.IsNull ? "Null" : r)}"
267+
)
268+
)
269+
)
270+
);
271+
// >>> Average lover, How will anyone know?, Starfish Trooper
272+
273+
bool[] res33 = db.TOPK().Query(
274+
"top_3_songs",
275+
"Starfish Trooper", "Road to everywhere"
276+
);
277+
Console.WriteLine(string.Join(", ", res33));
278+
// >>> true, false
279+
// STEP_END
280+
// REMOVE_START
281+
Assert.True(res30);
282+
Assert.Equal(
283+
"Null, Null, Null, Rock me, Handel, Only one more time, Null",
284+
string.Join(
285+
", ",
286+
string.Join(
287+
", ",
288+
res31.Select(
289+
r => $"{(r.IsNull ? "Null" : r)}"
290+
)
291+
)
292+
)
293+
);
294+
295+
Assert.Equal(
296+
"Average lover, How will anyone know?, Starfish Trooper",
297+
string.Join(
298+
", ",
299+
string.Join(
300+
", ",
301+
res32.Select(
302+
r => $"{(r.IsNull ? "Null" : r)}"
303+
)
304+
)
305+
)
306+
);
307+
308+
Assert.Equal(new bool[] { true, false }, res33);
309+
// REMOVE_END
310+
}
311+
}

0 commit comments

Comments
 (0)