/
topic_partition.go
75 lines (66 loc) · 1.2 KB
/
topic_partition.go
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
package kfake
type tps[V any] map[string]map[int32]*V
func (tps *tps[V]) getp(t string, p int32) (*V, bool) {
if *tps == nil {
return nil, false
}
ps := (*tps)[t]
if ps == nil {
return nil, false
}
v, ok := ps[p]
return v, ok
}
func (tps *tps[V]) gett(t string) (map[int32]*V, bool) {
if tps == nil {
return nil, false
}
ps, ok := (*tps)[t]
return ps, ok
}
func (tps *tps[V]) mkt(t string) map[int32]*V {
if *tps == nil {
*tps = make(map[string]map[int32]*V)
}
ps := (*tps)[t]
if ps == nil {
ps = make(map[int32]*V)
(*tps)[t] = ps
}
return ps
}
func (tps *tps[V]) mkp(t string, p int32, newFn func() *V) *V {
ps := tps.mkt(t)
v, ok := ps[p]
if !ok {
v = newFn()
ps[p] = v
}
return v
}
func (tps *tps[V]) mkpDefault(t string, p int32) *V {
return tps.mkp(t, p, func() *V { return new(V) })
}
func (tps *tps[V]) set(t string, p int32, v V) {
*tps.mkpDefault(t, p) = v
}
func (tps *tps[V]) each(fn func(t string, p int32, v *V)) {
for t, ps := range *tps {
for p, v := range ps {
fn(t, p, v)
}
}
}
func (tps *tps[V]) delp(t string, p int32) {
if *tps == nil {
return
}
ps := (*tps)[t]
if ps == nil {
return
}
delete(ps, p)
if len(ps) == 0 {
delete(*tps, t)
}
}