Skip to content

Commit a22d001

Browse files
DOC-5226 added probabilistic examples for C#
1 parent 9c6e4c7 commit a22d001

File tree

1 file changed

+208
-18
lines changed
  • content/develop/clients/dotnet

1 file changed

+208
-18
lines changed

content/develop/clients/dotnet/prob.md

Lines changed: 208 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ Assuming that you already have code that supplies you with each IP
3434
address as a string, you could record the addresses in Redis using
3535
a [set]({{< relref "/develop/data-types/sets" >}}):
3636

37-
```py
38-
r.sadd("ip_tracker", new_ip_address)
37+
```cs
38+
db.SetAdd("ip_tracker", new_ip_address);
3939
```
4040

4141
The set can only contain each key once, so if the same address
4242
appears again during the day, the new instance will not change
4343
the set. At the end of the day, you could get the exact number of
44-
distinct addresses using the `scard()` function:
44+
distinct addresses using the `SetLength()` function:
4545

46-
```py
47-
num_distinct_ips = r.scard("ip_tracker")
46+
```cs
47+
var num_distinct_ips = db.SetLength("ip_tracker");
4848
```
4949

5050
This approach is simple, effective, and precise but if your website
@@ -99,16 +99,49 @@ add. The following example adds some names to a Bloom filter representing
9999
a list of users and checks for the presence or absence of users in the list.
100100
Note that you must use the `BF()` method to access the Bloom filter commands.
101101

102-
{{< clients-example home_prob_dts bloom "C#" >}}
103-
{{< /clients-example >}}
102+
<!--< clients-example home_prob_dts bloom "C#" >}}
103+
< /clients-example >}}-->
104+
```cs
105+
bool[] res1 = db.BF().MAdd(
106+
"recorded_users", "andy", "cameron", "david", "michelle"
107+
);
108+
Console.WriteLine(string.Join(", ", res1));
109+
// >>> true, true, true, true
110+
111+
bool res2 = db.BF().Exists("recorded_users", "cameron");
112+
Console.WriteLine(res2); // >>> true
113+
114+
bool res3 = db.BF().Exists("recorded_users", "kaitlyn");
115+
Console.WriteLine(res3); // >>> false
116+
```
104117

105118
A Cuckoo filter has similar features to a Bloom filter, but also supports
106119
a deletion operation to remove hashes from a set, as shown in the example
107120
below. Note that you must use the `CF()` method to access the Cuckoo filter
108121
commands.
109122

110-
{{< clients-example home_prob_dts cuckoo "C#" >}}
111-
{{< /clients-example >}}
123+
<!--< clients-example home_prob_dts cuckoo "C#" >}}
124+
< /clients-example >}}-->
125+
```cs
126+
bool res4 = db.CF().Add("other_users", "paolo");
127+
Console.WriteLine(res4); // >>> true
128+
129+
bool res5 = db.CF().Add("other_users", "kaitlyn");
130+
Console.WriteLine(res5); // >>> true
131+
132+
bool res6 = db.CF().Add("other_users", "rachel");
133+
Console.WriteLine(res6); // >>> true
134+
135+
bool[] res7 = db.CF().MExists("other_users", "paolo", "rachel", "andy");
136+
Console.WriteLine(string.Join(", ", res7));
137+
// >>> true, true, false
138+
139+
bool res8 = db.CF().Del("other_users", "paolo");
140+
Console.WriteLine(res8); // >>> true
141+
142+
bool res9 = db.CF().Exists("other_users", "paolo");
143+
Console.WriteLine(res9); // >>> false
144+
```
112145

113146
Which of these two data types you choose depends on your use case.
114147
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
128161
[union](https://en.wikipedia.org/wiki/Union_(set_theory)) of the sets they
129162
represent.
130163

131-
{{< clients-example home_prob_dts hyperloglog "C#" >}}
132-
{{< /clients-example >}}
164+
<!--< clients-example home_prob_dts hyperloglog "C#" >}}
165+
< /clients-example >}}-->
166+
```cs
167+
bool res10 = db.HyperLogLogAdd(
168+
"group:1",
169+
new RedisValue[] { "andy", "cameron", "david" }
170+
);
171+
Console.WriteLine(res10); // >>> true
172+
173+
long res11 = db.HyperLogLogLength("group:1");
174+
Console.WriteLine(res11); // >>> 3
175+
176+
bool res12 = db.HyperLogLogAdd(
177+
"group:2",
178+
new RedisValue[] { "kaitlyn", "michelle", "paolo", "rachel" }
179+
);
180+
Console.WriteLine(res12); // >>> true
181+
182+
long res13 = db.HyperLogLogLength("group:2");
183+
Console.WriteLine(res13); // >>> 4
184+
185+
db.HyperLogLogMerge(
186+
"both_groups",
187+
"group:1", "group:2"
188+
);
189+
190+
long res14 = db.HyperLogLogLength("both_groups");
191+
Console.WriteLine(res14); // >>> 7
192+
```
133193

134194
The main benefit that HyperLogLogs offer is their very low
135195
memory usage. They can count up to 2^64 items with less than
@@ -169,8 +229,44 @@ a Count-min sketch object, add data to it, and then query it.
169229
Note that you must use the `CMS()` method to access the Count-min
170230
sketch commands.
171231

172-
{{< clients-example home_prob_dts cms "C#" >}}
173-
{{< /clients-example >}}
232+
<!--< clients-example home_prob_dts cms "C#" >}}
233+
< /clients-example >}}-->
234+
```cs
235+
// Specify that you want to keep the counts within 0.01
236+
// (0.1%) of the true value with a 0.005 (0.05%) chance
237+
// of going outside this limit.
238+
bool res15 = db.CMS().InitByProb("items_sold", 0.01, 0.005);
239+
Console.WriteLine(res15); // >>> true
240+
241+
long[] res16 = db.CMS().IncrBy(
242+
"items_sold",
243+
new Tuple<RedisValue, long>[]{
244+
new("bread", 300),
245+
new("tea", 200),
246+
new("coffee", 200),
247+
new("beer", 100)
248+
}
249+
);
250+
Console.WriteLine(string.Join(", ", res16));
251+
// >>> 300, 200, 200, 100
252+
253+
long[] res17 = db.CMS().IncrBy(
254+
"items_sold",
255+
new Tuple<RedisValue, long>[]{
256+
new("bread", 100),
257+
new("coffee", 150),
258+
}
259+
);
260+
Console.WriteLine(string.Join(", ", res17));
261+
// >>> 400, 350
262+
263+
long[] res18 = db.CMS().Query(
264+
"items_sold",
265+
"bread", "tea", "coffee", "beer"
266+
);
267+
Console.WriteLine(string.Join(", ", res18));
268+
// >>> 400, 200, 350, 100
269+
```
174270

175271
The advantage of using a CMS over keeping an exact count with a
176272
[sorted set]({{< relref "/develop/data-types/sorted-sets" >}})
@@ -202,8 +298,53 @@ shows how to merge two or more t-digest objects to query the combined
202298
data set. Note that you must use the `TDIGEST()` method to access the
203299
t-digest commands.
204300

205-
{{< clients-example home_prob_dts tdigest "C#" >}}
206-
{{< /clients-example >}}
301+
<!--< clients-example home_prob_dts tdigest "C#" >}}
302+
< /clients-example >}}-->
303+
```cs
304+
bool res19 = db.TDIGEST().Create("male_heights");
305+
Console.WriteLine(res19); // >>> true
306+
307+
bool res20 = db.TDIGEST().Add(
308+
"male_heights",
309+
175.5, 181, 160.8, 152, 177, 196, 164
310+
);
311+
Console.WriteLine(res20); // >>> true
312+
313+
double res21 = db.TDIGEST().Min("male_heights");
314+
Console.WriteLine(res21); // >>> 152.0
315+
316+
double res22 = db.TDIGEST().Max("male_heights");
317+
Console.WriteLine(res22); // >>> 196.0
318+
319+
double[] res23 = db.TDIGEST().Quantile("male_heights", 0.75);
320+
Console.WriteLine(string.Join(", ", res23)); // >>> 181.0
321+
322+
// Note that the CDF value for 181.0 is not exactly
323+
// 0.75. Both values are estimates.
324+
double[] res24 = db.TDIGEST().CDF("male_heights", 181.0);
325+
Console.WriteLine(string.Join(", ", res24)); // >>> 0.7857142857142857
326+
327+
bool res25 = db.TDIGEST().Create("female_heights");
328+
Console.WriteLine(res25); // >>> true
329+
330+
bool res26 = db.TDIGEST().Add(
331+
"female_heights",
332+
155.5, 161, 168.5, 170, 157.5, 163, 171
333+
);
334+
Console.WriteLine(res26); // >>> true
335+
336+
double[] res27 = db.TDIGEST().Quantile("female_heights", 0.75);
337+
Console.WriteLine(string.Join(", ", res27)); // >>> 170.0
338+
339+
// Specify 0 for `compression` and false for `override`.
340+
bool res28 = db.TDIGEST().Merge(
341+
"all_heights", 0, false, "male_heights", "female_heights"
342+
);
343+
Console.WriteLine(res28); // >>> true
344+
345+
double[] res29 = db.TDIGEST().Quantile("all_heights", 0.75);
346+
Console.WriteLine(string.Join(", ", res29)); // >>> 175.5
347+
```
207348

208349
A t-digest object also supports several other related commands, such
209350
as querying by rank. See the
@@ -220,10 +361,59 @@ top five most popular items sold.
220361

221362
The example below adds several different items to a Top-K object
222363
that tracks the top three items (this is the second parameter to
223-
the `topk().reserve()` method). It also shows how to list the
364+
the `TOPK().Reserve()` method). It also shows how to list the
224365
top *k* items and query whether or not a given item is in the
225366
list. Note that you must use the `TOPK()` method to access the
226367
Top-K commands.
227368

228-
{{< clients-example home_prob_dts topk "C#" >}}
229-
{{< /clients-example >}}
369+
<!--< clients-example home_prob_dts topk "C#" >}}
370+
< /clients-example >}}-->
371+
```cs
372+
bool res30 = db.TOPK().Reserve("top_3_songs", 3, 7, 8, 0.9);
373+
Console.WriteLine(res30); // >>> true
374+
375+
RedisResult[] res31 = db.TOPK().IncrBy(
376+
"top_3_songs",
377+
new Tuple<RedisValue, long>[] {
378+
new("Starfish Trooper", 3000),
379+
new("Only one more time", 1850),
380+
new("Rock me, Handel", 1325),
381+
new("How will anyone know?", 3890),
382+
new("Average lover", 4098),
383+
new("Road to everywhere", 770)
384+
}
385+
);
386+
Console.WriteLine(
387+
string.Join(
388+
", ",
389+
string.Join(
390+
", ",
391+
res31.Select(
392+
r => $"{(r.IsNull ? "Null" : r)}"
393+
)
394+
)
395+
)
396+
);
397+
// >>> Null, Null, Null, Rock me, Handel, Only one more time, Null
398+
399+
RedisResult[] res32 = db.TOPK().List("top_3_songs");
400+
Console.WriteLine(
401+
string.Join(
402+
", ",
403+
string.Join(
404+
", ",
405+
res32.Select(
406+
r => $"{(r.IsNull ? "Null" : r)}"
407+
)
408+
)
409+
)
410+
);
411+
// >>> Average lover, How will anyone know?, Starfish Trooper
412+
413+
bool[] res33 = db.TOPK().Query(
414+
"top_3_songs",
415+
"Starfish Trooper", "Road to everywhere"
416+
);
417+
Console.WriteLine(string.Join(", ", res33));
418+
// >>> true, false
419+
```

0 commit comments

Comments
 (0)