@@ -99,16 +99,49 @@ add. The following example adds some names to a Bloom filter representing
99
99
a list of users and checks for the presence or absence of users in the list.
100
100
Note that you must use the ` BF() ` method to access the Bloom filter commands.
101
101
102
- {{< clients-example home_prob_dts bloom "C#" >}}
103
- {{< /clients-example >}}
102
+ ``` cs
103
+ bool [] res1 = db .BF ().MAdd (
104
+ " recorded_users" , " andy" , " cameron" , " david" , " michelle"
105
+ );
106
+ Console .WriteLine (string .Join (" , " , res1 ));
107
+ // >>> true, true, true, true
108
+
109
+ bool res2 = db .BF ().Exists (" recorded_users" , " cameron" );
110
+ Console .WriteLine (res2 ); // >>> true
111
+
112
+ bool res3 = db .BF ().Exists (" recorded_users" , " kaitlyn" );
113
+ Console .WriteLine (res3 ); // >>> false
114
+ ```
115
+ <!-- < clients-example home_prob_dts bloom "C#" >}}
116
+ < /clients-example >}} -->
104
117
105
118
A Cuckoo filter has similar features to a Bloom filter, but also supports
106
119
a deletion operation to remove hashes from a set, as shown in the example
107
120
below. Note that you must use the ` CF() ` method to access the Cuckoo filter
108
121
commands.
109
122
110
- {{< clients-example home_prob_dts cuckoo "C#" >}}
111
- {{< /clients-example >}}
123
+ ``` cs
124
+ bool res4 = db .CF ().Add (" other_users" , " paolo" );
125
+ Console .WriteLine (res4 ); // >>> true
126
+
127
+ bool res5 = db .CF ().Add (" other_users" , " kaitlyn" );
128
+ Console .WriteLine (res5 ); // >>> true
129
+
130
+ bool res6 = db .CF ().Add (" other_users" , " rachel" );
131
+ Console .WriteLine (res6 ); // >>> true
132
+
133
+ bool [] res7 = db .CF ().MExists (" other_users" , " paolo" , " rachel" , " andy" );
134
+ Console .WriteLine (string .Join (" , " , res7 ));
135
+ // >>> true, true, false
136
+
137
+ bool res8 = db .CF ().Del (" other_users" , " paolo" );
138
+ Console .WriteLine (res8 ); // >>> true
139
+
140
+ bool res9 = db .CF ().Exists (" other_users" , " paolo" );
141
+ Console .WriteLine (res9 ); // >>> false
142
+ ```
143
+ <!-- < clients-example home_prob_dts cuckoo "C#" >}}
144
+ < /clients-example >}} -->
112
145
113
146
Which of these two data types you choose depends on your use case.
114
147
Bloom filters are generally faster than Cuckoo filters when adding new items,
@@ -128,8 +161,35 @@ You can also merge two or more HyperLogLogs to find the cardinality of the
128
161
[ union] ( https://en.wikipedia.org/wiki/Union_(set_theory) ) of the sets they
129
162
represent.
130
163
131
- {{< clients-example home_prob_dts hyperloglog "C#" >}}
132
- {{< /clients-example >}}
164
+ ``` cs
165
+ bool res10 = db .HyperLogLogAdd (
166
+ " group:1" ,
167
+ new RedisValue [] { " andy" , " cameron" , " david" }
168
+ );
169
+ Console .WriteLine (res10 ); // >>> true
170
+
171
+ long res11 = db .HyperLogLogLength (" group:1" );
172
+ Console .WriteLine (res11 ); // >>> 3
173
+
174
+ bool res12 = db .HyperLogLogAdd (
175
+ " group:2" ,
176
+ new RedisValue [] { " kaitlyn" , " michelle" , " paolo" , " rachel" }
177
+ );
178
+ Console .WriteLine (res12 ); // >>> true
179
+
180
+ long res13 = db .HyperLogLogLength (" group:2" );
181
+ Console .WriteLine (res13 ); // >>> 4
182
+
183
+ db .HyperLogLogMerge (
184
+ " both_groups" ,
185
+ " group:1" , " group:2"
186
+ );
187
+
188
+ long res14 = db .HyperLogLogLength (" both_groups" );
189
+ Console .WriteLine (res14 ); // >>> 7
190
+ ```
191
+ <!-- < clients-example home_prob_dts hyperloglog "C#" >}}
192
+ < /clients-example >}} -->
133
193
134
194
The main benefit that HyperLogLogs offer is their very low
135
195
memory usage. They can count up to 2^64 items with less than
@@ -168,9 +228,44 @@ of going outside this limit. The example below shows how to create
168
228
a Count-min sketch object, add data to it, and then query it.
169
229
Note that you must use the ` CMS() ` method to access the Count-min
170
230
sketch commands.
171
-
172
- {{< clients-example home_prob_dts cms "C#" >}}
173
- {{< /clients-example >}}
231
+ ``` cs
232
+ // Specify that you want to keep the counts within 0.01
233
+ // (1%) of the true value with a 0.005 (0.5%) chance
234
+ // of going outside this limit.
235
+ bool res15 = db .CMS ().InitByProb (" items_sold" , 0 . 01 , 0 . 005 );
236
+ Console .WriteLine (res15 ); // >>> true
237
+
238
+ long [] res16 = db .CMS ().IncrBy (
239
+ " items_sold" ,
240
+ new Tuple <RedisValue , long >[]{
241
+ new (" bread" , 300 ),
242
+ new (" tea" , 200 ),
243
+ new (" coffee" , 200 ),
244
+ new (" beer" , 100 )
245
+ }
246
+ );
247
+ Console .WriteLine (string .Join (" , " , res16 ));
248
+ // >>> 300, 200, 200, 100
249
+
250
+ long [] res17 = db .CMS ().IncrBy (
251
+ " items_sold" ,
252
+ new Tuple <RedisValue , long >[]{
253
+ new (" bread" , 100 ),
254
+ new (" coffee" , 150 ),
255
+ }
256
+ );
257
+ Console .WriteLine (string .Join (" , " , res17 ));
258
+ // >>> 400, 350
259
+
260
+ long [] res18 = db .CMS ().Query (
261
+ " items_sold" ,
262
+ " bread" , " tea" , " coffee" , " beer"
263
+ );
264
+ Console .WriteLine (string .Join (" , " , res18 ));
265
+ // >>> 400, 200, 350, 100
266
+ ```
267
+ <!-- < clients-example home_prob_dts cms "C#" >}}
268
+ < /clients-example >}} -->
174
269
175
270
The advantage of using a CMS over keeping an exact count with a
176
271
[ sorted set] ({{< relref "/develop/data-types/sorted-sets" >}})
@@ -202,8 +297,53 @@ shows how to merge two or more t-digest objects to query the combined
202
297
data set. Note that you must use the ` TDIGEST() ` method to access the
203
298
t-digest commands.
204
299
205
- {{< clients-example home_prob_dts tdigest "C#" >}}
206
- {{< /clients-example >}}
300
+ ``` cs
301
+ bool res19 = db .TDIGEST ().Create (" male_heights" );
302
+ Console .WriteLine (res19 ); // >>> true
303
+
304
+ bool res20 = db .TDIGEST ().Add (
305
+ " male_heights" ,
306
+ 175 . 5 , 181 , 160 . 8 , 152 , 177 , 196 , 164
307
+ );
308
+ Console .WriteLine (res20 ); // >>> true
309
+
310
+ double res21 = db .TDIGEST ().Min (" male_heights" );
311
+ Console .WriteLine (res21 ); // >>> 152.0
312
+
313
+ double res22 = db .TDIGEST ().Max (" male_heights" );
314
+ Console .WriteLine (res22 ); // >>> 196.0
315
+
316
+ double [] res23 = db .TDIGEST ().Quantile (" male_heights" , 0 . 75 );
317
+ Console .WriteLine (string .Join (" , " , res23 )); // >>> 181.0
318
+
319
+ // Note that the CDF value for 181.0 is not exactly
320
+ // 0.75. Both values are estimates.
321
+ double [] res24 = db .TDIGEST ().CDF (" male_heights" , 181 . 0 );
322
+ Console .WriteLine (string .Join (" , " , res24 )); // >>> 0.7857142857142857
323
+
324
+ bool res25 = db .TDIGEST ().Create (" female_heights" );
325
+ Console .WriteLine (res25 ); // >>> true
326
+
327
+ bool res26 = db .TDIGEST ().Add (
328
+ " female_heights" ,
329
+ 155 . 5 , 161 , 168 . 5 , 170 , 157 . 5 , 163 , 171
330
+ );
331
+ Console .WriteLine (res26 ); // >>> true
332
+
333
+ double [] res27 = db .TDIGEST ().Quantile (" female_heights" , 0 . 75 );
334
+ Console .WriteLine (string .Join (" , " , res27 )); // >>> 170.0
335
+
336
+ // Specify 0 for `compression` and false for `override`.
337
+ bool res28 = db .TDIGEST ().Merge (
338
+ " all_heights" , 0 , false , " male_heights" , " female_heights"
339
+ );
340
+ Console .WriteLine (res28 ); // >>> true
341
+
342
+ double [] res29 = db .TDIGEST ().Quantile (" all_heights" , 0 . 75 );
343
+ Console .WriteLine (string .Join (" , " , res29 )); // >>> 175.5
344
+ ```
345
+ <!-- < clients-example home_prob_dts tdigest "C#" >}}
346
+ < /clients-example >}} -->
207
347
208
348
A t-digest object also supports several other related commands, such
209
349
as querying by rank. See the
@@ -225,5 +365,54 @@ top *k* items and query whether or not a given item is in the
225
365
list. Note that you must use the ` TOPK() ` method to access the
226
366
Top-K commands.
227
367
228
- {{< clients-example home_prob_dts topk "C#" >}}
229
- {{< /clients-example >}}
368
+ ``` cs
369
+ bool res30 = db .TOPK ().Reserve (" top_3_songs" , 3 , 7 , 8 , 0 . 9 );
370
+ Console .WriteLine (res30 ); // >>> true
371
+
372
+ RedisResult [] res31 = db .TOPK ().IncrBy (
373
+ " top_3_songs" ,
374
+ new Tuple <RedisValue , long >[] {
375
+ new (" Starfish Trooper" , 3000 ),
376
+ new (" Only one more time" , 1850 ),
377
+ new (" Rock me, Handel" , 1325 ),
378
+ new (" How will anyone know?" , 3890 ),
379
+ new (" Average lover" , 4098 ),
380
+ new (" Road to everywhere" , 770 )
381
+ }
382
+ );
383
+ Console .WriteLine (
384
+ string .Join (
385
+ " , " ,
386
+ string .Join (
387
+ " , " ,
388
+ res31 .Select (
389
+ r => $" {(r .IsNull ? " Null" : r )}"
390
+ )
391
+ )
392
+ )
393
+ );
394
+ // >>> Null, Null, Null, Rock me, Handel, Only one more time, Null
395
+
396
+ RedisResult [] res32 = db .TOPK ().List (" top_3_songs" );
397
+ Console .WriteLine (
398
+ string .Join (
399
+ " , " ,
400
+ string .Join (
401
+ " , " ,
402
+ res32 .Select (
403
+ r => $" {(r .IsNull ? " Null" : r )}"
404
+ )
405
+ )
406
+ )
407
+ );
408
+ // >>> Average lover, How will anyone know?, Starfish Trooper
409
+
410
+ bool [] res33 = db .TOPK ().Query (
411
+ " top_3_songs" ,
412
+ " Starfish Trooper" , " Road to everywhere"
413
+ );
414
+ Console .WriteLine (string .Join (" , " , res33 ));
415
+ // >>> true, false
416
+ ```
417
+ <!-- < clients-example home_prob_dts topk "C#" >}}
418
+ < /clients-example >}} -->
0 commit comments