-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
67 lines (55 loc) · 2.16 KB
/
Copy pathexample_test.go
File metadata and controls
67 lines (55 loc) · 2.16 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
package clusterpath_test
import (
"fmt"
"github.com/optimiweb/clusterpath"
)
// Basic normalization: the scheme and host are canonicalized, the numeric id is
// masked, tracking parameters are dropped, functional parameters are kept and
// sorted, and the fragment is removed.
func Example() {
c := clusterpath.New(clusterpath.DefaultConfig())
raw := []byte("HTTPS://www.Example.TEST/products/electronics/10293?session=abc123&sort=price_asc#reviews")
fmt.Println(string(c.Normalize(nil, raw)))
// Output: https://example.test/products/electronics/{id}?sort=price_asc
}
// Learned masking: after enough samples share a structural shape, the
// high-cardinality leaf position collapses to a placeholder while the stable
// prefix stays literal. Freeze makes the output stable for a replay pass.
func ExampleClusterer() {
c := clusterpath.New(clusterpath.Config{MinSamples: 4, DistinctLimit: 4})
for i := 0; i < 8; i++ {
c.Normalize(nil, []byte(fmt.Sprintf("/blog/article-%d.html", i)))
}
c.Freeze()
fmt.Println(string(c.Apply(nil, []byte("/blog/article-99.html"))))
// Output: /blog/article-{id}.html
}
// Reusing a destination buffer keeps Normalize allocation-free.
func ExampleClusterer_Normalize() {
c := clusterpath.New(clusterpath.DefaultConfig())
dst := make([]byte, 0, 256)
for _, raw := range [][]byte{
[]byte("/user/550e8400-e29b-41d4-a716-446655440000/orders/42"),
[]byte("/assets/img/ca508a0b52086307ea926f194c702566.png"),
} {
dst = c.Normalize(dst[:0], raw)
fmt.Println(string(dst))
}
// Output:
// /user/{uuid}/orders/{id}
// /assets/img/{hex}.png
}
// Sharding routes every URL of the same structural shape to the same worker, so
// per-shard models stay coherent while workers process independently.
func ExampleSharded() {
s := clusterpath.NewSharded(8, clusterpath.DefaultConfig())
a := s.Shard([]byte("/v6/users/123/newsletters/list"))
b := s.Shard([]byte("/v6/users/999/newsletters/list"))
fmt.Println("same shard:", a == b)
// Each shard is owned by exactly one goroutine:
out := s.At(a).Normalize(nil, []byte("/v6/users/123/newsletters/list"))
fmt.Println(string(out))
// Output:
// same shard: true
// /v6/users/{id}/newsletters/list
}