forked from lonng/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.go
179 lines (151 loc) · 3.75 KB
/
dictionary.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package message
import (
"errors"
"github.com/aura-studio/nano/cluster/clusterpb"
lua "github.com/yuin/gopher-lua"
)
type Dictionary interface {
IndexRoute(string) (uint32, error)
IndexCode(uint32) (string, error)
}
type MemberDictionary struct {
// Routes is a map from route to code
routes map[string]uint32
// Codes is a map from code to route
codes map[uint32]string
}
func NewMemberDictionary() *MemberDictionary {
return &MemberDictionary{
routes: make(map[string]uint32),
codes: make(map[uint32]string),
}
}
var ErrIndexRouteNotFound = errors.New("dictionary: index route not found")
var ErrIndexCodeNotFound = errors.New("dictionary: index code not found")
var (
memberDictionary = NewMemberDictionary()
EmptyDictionary = NewMemberDictionary()
)
func (d *MemberDictionary) IndexRoute(route string) (uint32, error) {
if d == nil {
return 0, ErrIndexRouteNotFound
}
code, ok := d.routes[route]
if !ok {
return 0, ErrIndexRouteNotFound
}
return code, nil
}
func (d *MemberDictionary) IndexCode(code uint32) (string, error) {
if d == nil {
return "", ErrIndexRouteNotFound
}
route, ok := d.codes[code]
if !ok {
return "", ErrIndexCodeNotFound
}
return route, nil
}
func (d *MemberDictionary) duplicate() *MemberDictionary {
rw.RLock()
defer rw.RUnlock()
memberDictionary := NewMemberDictionary()
for route, code := range d.routes {
memberDictionary.routes[route] = code
}
for code, route := range d.codes {
memberDictionary.codes[code] = route
}
return memberDictionary
}
func (d *MemberDictionary) write(route string, code uint32) {
rw.Lock()
defer rw.Unlock()
if code > 0 {
d.routes[route] = code
d.codes[code] = route
}
}
func (d *MemberDictionary) writeClusterItems(items []*clusterpb.DictionaryItem) {
rw.Lock()
defer rw.Unlock()
for _, item := range items {
if item.Code > 0 {
d.routes[item.Route] = item.Code
d.codes[item.Code] = item.Route
}
}
}
// DuplicateDictionary returns dictionary for compressed route.
func DuplicateDictionary() *MemberDictionary {
return memberDictionary.duplicate()
}
// WriteDictionaryItem is to set dictionary item when server registers.
func WriteDictionaryItem(route string, code uint32) {
memberDictionary.write(route, code)
}
// WriteDictionary is to set dictionary when new route dictionary is found.
func WriteDictionary(items []*clusterpb.DictionaryItem) {
memberDictionary.writeClusterItems(items)
}
type LuaDictionary struct {
// Routes is a map from route to code
routes map[string]uint32
// Codes is a map from code to route
codes map[uint32]string
}
func NewLuaDictionary(file string) *LuaDictionary {
d := &LuaDictionary{
routes: make(map[string]uint32),
codes: make(map[uint32]string),
}
d.LoadLua(file)
return d
}
func (d *LuaDictionary) IndexRoute(route string) (uint32, error) {
if d == nil {
return 0, ErrIndexRouteNotFound
}
code, ok := d.routes[route]
if !ok {
return 0, ErrIndexRouteNotFound
}
return code, nil
}
func (d *LuaDictionary) IndexCode(code uint32) (string, error) {
if d == nil {
return "", ErrIndexRouteNotFound
}
route, ok := d.codes[code]
if !ok {
return "", ErrIndexCodeNotFound
}
return route, nil
}
func (d *LuaDictionary) write(route string, code uint32) {
if code > 0 {
d.routes[route] = code
d.codes[code] = route
}
}
func (d *LuaDictionary) InitModule(l *lua.LState) int {
var exports = map[string]lua.LGFunction{
"Write": func(l *lua.LState) int {
route := l.ToString(1)
code := uint32(l.ToInt(2))
d.write(route, code)
return 0
},
}
mod := l.SetFuncs(l.NewTable(), exports)
l.Push(mod)
return 1
}
func (d *LuaDictionary) LoadLua(file string) {
l := lua.NewState()
defer l.Close()
l.PreloadModule("GoDictionary", d.InitModule)
if err := l.DoFile(file); err != nil {
panic(err)
}
}