forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
176 lines (151 loc) · 2.83 KB
/
cache.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
package etf
import (
"sync"
)
const (
maxCacheItems = int16(2048)
)
type AtomCache struct {
In *AtomCacheIn
Out *AtomCacheOut
}
type AtomCacheIn struct {
Atoms [maxCacheItems]*Atom
}
// AtomCache
type AtomCacheOut struct {
sync.RWMutex
cacheMap map[Atom]int16
id int16
cacheList [maxCacheItems]Atom
}
// CacheItem
type CacheItem struct {
ID int16
Encoded bool
Name Atom
}
var (
encodingAtomCachePool = &sync.Pool{
New: func() interface{} {
l := &EncodingAtomCache{
L: make([]CacheItem, 0, 255),
added: make(map[Atom]uint8),
}
l.original = l.L
return l
},
}
)
// NewAtomCache
func NewAtomCache() AtomCache {
return AtomCache{
In: &AtomCacheIn{},
Out: &AtomCacheOut{
cacheMap: make(map[Atom]int16),
id: -1,
},
}
}
type AtomMapping struct {
MutexIn sync.RWMutex
In map[Atom]Atom
MutexOut sync.RWMutex
Out map[Atom]Atom
}
// NewAtomMapping
func NewAtomMapping() *AtomMapping {
return &AtomMapping{
In: make(map[Atom]Atom),
Out: make(map[Atom]Atom),
}
}
// Append
func (a *AtomCacheOut) Append(atom Atom) (int16, bool) {
a.Lock()
defer a.Unlock()
if a.id > maxCacheItems-2 {
return 0, false
}
if id, exist := a.cacheMap[atom]; exist {
return id, false
}
a.id++
a.cacheList[a.id] = atom
a.cacheMap[atom] = a.id
return a.id, true
}
// LastID
func (a *AtomCacheOut) LastAdded() (Atom, int16) {
a.RLock()
defer a.RUnlock()
l := len(a.cacheList)
if l == 0 {
return "", -1
}
return a.cacheList[l-1], int16(l - 1)
}
// ListSince
func (a *AtomCacheOut) ListSince(id int16) []Atom {
if id < 0 {
id = 0
}
if int(id) > len(a.cacheList)-1 {
return nil
}
return a.cacheList[id:]
}
// EncodingAtomCache
type EncodingAtomCache struct {
L []CacheItem
original []CacheItem
added map[Atom]uint8
HasLongAtom bool
}
// TakeEncodingAtomCache
func TakeEncodingAtomCache() *EncodingAtomCache {
return encodingAtomCachePool.Get().(*EncodingAtomCache)
}
// ReleaseEncodingAtomCache
func ReleaseEncodingAtomCache(l *EncodingAtomCache) {
l.L = l.original[:0]
if len(l.added) > 0 {
for k, _ := range l.added {
delete(l.added, k)
}
}
encodingAtomCachePool.Put(l)
}
// Reset
func (l *EncodingAtomCache) Reset() {
l.L = l.original[:0]
l.HasLongAtom = false
if len(l.added) > 0 {
for k, _ := range l.added {
delete(l.added, k)
}
}
}
// Append
func (l *EncodingAtomCache) Append(a CacheItem) uint8 {
id, added := l.added[a.Name]
if added {
return id
}
l.L = append(l.L, a)
if !a.Encoded && len(a.Name) > 255 {
l.HasLongAtom = true
}
id = uint8(len(l.L) - 1)
l.added[a.Name] = id
return id
}
// Delete
func (l *EncodingAtomCache) Delete(atom Atom) {
// clean up in order to get rid of map reallocation which is pretty expensive
delete(l.added, atom)
}
// Len
func (l *EncodingAtomCache) Len() int {
return len(l.L)
}