Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
115 lines (90 loc) · 2.64 KB
/
Copy pathexample_test.go
File metadata and controls
115 lines (90 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package hyperloglog
import (
"fmt"
"log"
)
// Example demonstrates basic usage.
func Example() {
sketch := New()
sketch.Insert([]byte("alice"))
sketch.Insert([]byte("bob"))
sketch.Insert([]byte("charlie"))
sketch.Insert([]byte("alice")) // Duplicate
fmt.Printf("Estimated unique elements: %d\n", sketch.Estimate())
// Output:
// Estimated unique elements: 3
}
// Example_serialization demonstrates binary marshaling and error handling.
func Example_serialization() {
original := New14()
for i := range 100 {
buf := fmt.Appendf(nil, "user-%d", i)
original.Insert(buf)
}
data, err := original.MarshalBinary()
if err != nil {
log.Fatal(err)
}
restored := New14()
if err := restored.UnmarshalBinary(data); err != nil {
log.Fatal(err)
}
fmt.Printf("Original estimate: %d\n", original.Estimate())
fmt.Printf("Restored estimate: %d\n", restored.Estimate())
fmt.Println("Serialization successful")
// Output:
// Original estimate: 100
// Restored estimate: 100
// Serialization successful
}
// Example_merge shows merging sketches for distributed counting.
func Example_merge() {
sketch1 := New14()
sketch2 := New14()
for i := range 500 {
buf := fmt.Appendf(nil, "item-%d", i)
sketch1.Insert(buf)
}
for i := range 500 {
buf := fmt.Appendf(nil, "item-%d", i+250)
sketch2.Insert(buf)
}
fmt.Printf("Sketch 1 estimate: %d\n", sketch1.Estimate())
fmt.Printf("Sketch 2 estimate: %d\n", sketch2.Estimate())
if err := sketch1.Merge(sketch2); err != nil {
log.Fatal(err)
}
fmt.Printf("Merged estimate: %d\n", sketch1.Estimate())
// Output:
// Sketch 1 estimate: 500
// Sketch 2 estimate: 500
// Merged estimate: 750
}
// Example_precision shows different precision levels and their trade-offs.
func Example_precision() {
sketch14 := New14() // 16 KB
sketch16 := New16() // 64 KB
for i := range 100 {
buf := fmt.Appendf(nil, "element-%d", i)
sketch14.Insert(buf)
sketch16.Insert(buf)
}
fmt.Printf("Precision 14: %d unique elements\n", sketch14.Estimate())
fmt.Printf("Precision 16: %d unique elements\n", sketch16.Estimate())
// Output:
// Precision 14: 100 unique elements
// Precision 16: 100 unique elements
}
// Example_insertHash shows using InsertHash when you already have hash values.
func Example_insertHash() {
sketch := New()
// InsertHash requires values from a high-quality hash function with a
// uniform distribution across all 64 bits. Do not pass sequential IDs
// directly.
sketch.InsertHash(0x1234567890abcdef)
sketch.InsertHash(0xfedcba0987654321)
sketch.InsertHash(0x1111111111111111)
fmt.Printf("Estimated unique elements: %d\n", sketch.Estimate())
// Output:
// Estimated unique elements: 3
}