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