forked from timbray/quamina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_pattern_state.go
94 lines (79 loc) · 1.93 KB
/
live_pattern_state.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package quamina
import (
"sync"
)
// LivePatternsState represents the required capabilities for maintaining the
// set of live patterns.
type LivePatternsState interface {
// Add adds a new pattern or updates an old pattern.
//
// Note that multiple patterns can be associated with the same X.
Add(x X, pattern string) error
// Delete removes all patterns associated with the given X and returns the
// number of removed patterns.
Delete(x X) (int, error)
// Iterate calls the given function for every stored pattern.
Iterate(func(x X, pattern string) error) error
// Contains returns true if x is in the live set; false otherwise.
Contains(x X) (bool, error)
}
type (
stringSet map[string]nothing
nothing struct{}
)
var na = nothing{}
// memState is a LivePatternsState that is just a map (with a RWMutex).
//
// Since the LivePatternsState implementation can be provided to the
// application, we're keeping things simple here initially.
type memState struct {
lock sync.RWMutex
m map[X]stringSet
}
func newMemState() *memState {
// Accept initial size as a parameter?
return &memState{
m: make(map[X]stringSet),
}
}
// var ErrExists = fmt.Errorf("pattern already exists for that X")
func (s *memState) Add(x X, pattern string) error {
s.lock.Lock()
ps, have := s.m[x]
if !have {
ps = make(stringSet)
s.m[x] = ps
}
ps[pattern] = na
s.lock.Unlock()
return nil
}
func (s *memState) Contains(x X) (bool, error) {
s.lock.RLock()
_, have := s.m[x]
s.lock.RUnlock()
return have, nil
}
func (s *memState) Delete(x X) (int, error) {
s.lock.Lock()
cardinality := 0
if xs, have := s.m[x]; have {
cardinality = len(xs)
delete(s.m, x)
}
s.lock.Unlock()
return cardinality, nil
}
func (s *memState) Iterate(f func(x X, pattern string) error) error {
s.lock.RLock()
var err error
for x, ps := range s.m {
for p := range ps {
if err = f(x, p); err != nil {
break
}
}
}
s.lock.RUnlock()
return err
}