-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.go
More file actions
223 lines (197 loc) · 3.86 KB
/
container.go
File metadata and controls
223 lines (197 loc) · 3.86 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package bulkCache
import (
"crypto/rand"
"fmt"
"sync"
"time"
log "github.com/Sirupsen/logrus"
)
var (
Default *Container
)
const (
KeySize = 32
HashEngine = "hash"
BTreeEngine = "btree"
)
type (
Cached map[string]*Item
EachHandler func(Bulk)
Bulk interface {
Add(string, []byte, time.Duration) error
GetAlive() Cached
GetAliveInBulk() Bulk
Config() *BulkConfig
Stop()
Len() int
Bytes() int
String() string
Analytics() *Analytics
}
BulkConfig struct {
MaxItem int
Eliminate time.Duration
EnabledCache bool
}
Container struct {
Mut *sync.RWMutex
Analytics *Analytics
bulks map[string]Bulk
Log *log.Entry
Name string
Engine string
}
Item struct {
Data []byte
Expire time.Time
}
)
func GenerateKey() (string, error) {
b := make([]byte, KeySize)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return string(b), nil
}
func NewContainer(name string, engine string) *Container {
if name == "" {
name = "Default"
}
if engine == "" {
engine = BTreeEngine
}
c := &Container{
Mut: new(sync.RWMutex),
Analytics: &Analytics{},
Name: name,
Engine: engine,
bulks: make(map[string]Bulk),
Log: log.WithFields(log.Fields{
"Store Engine": fmt.Sprintf("%s Container", name),
}),
}
go c.master()
return c
}
func (c *Container) NewBulk(cfg *BulkConfig) Bulk {
switch c.Engine {
case HashEngine:
return NewHashBulk(cfg)
case BTreeEngine:
return NewBTreeBulk(cfg)
}
return NewBTreeBulk(cfg)
}
func (c *Container) NewBulkFromCached(cfg *BulkConfig, cached Cached) Bulk {
switch c.Engine {
case HashEngine:
return NewHashBulkFromCached(cfg, cached)
case BTreeEngine:
return NewBTreeBulkFromCached(cfg, cached)
}
return NewBTreeBulkFromCached(cfg, cached)
}
func (c *Container) GetBulk(key string) (Bulk, bool) {
c.Mut.RLock()
defer c.Mut.RUnlock()
bulk, ok := c.bulks[key]
return bulk, ok
}
func (c *Container) Get(key string) (Cached, bool) {
defer c.Analytics.Get()
b, ok := c.GetBulk(key)
if !ok {
c.Log.Warning(fmt.Sprintf("Bulk %s is empty", key))
return nil, false
}
return b.GetAlive(), true
}
func (c *Container) GetBulkItems(key string) (bulk Bulk, ok bool) {
b, ok := c.GetBulk(key)
if !ok {
return nil, false
}
its := b.GetAlive()
if len(its) == 0 {
return nil, false
}
return c.NewBulkFromCached(b.Config(), its), true
}
func (c *Container) AddBulk(key string, cfg *BulkConfig) Bulk {
c.Mut.Lock()
defer c.Mut.Unlock()
b, ok := c.bulks[key]
if !ok {
b = c.NewBulk(cfg)
c.bulks[key] = b
}
return b
}
func (c *Container) Add(key, sub string, value []byte, expire time.Duration) error {
defer c.Analytics.Add(value)
var bulk Bulk
if !c.Has(key) {
bulk = c.AddBulk(key, nil)
} else {
b, _ := c.GetBulk(key)
bulk = b
}
//padding key
if sub == "" {
var err error
sub, err = GenerateKey()
if err != nil {
c.Log.Error(fmt.Sprintf("Generate Key[%d byte] error[%s]", KeySize, err.Error()))
return err
}
}
if len(sub) > KeySize {
sub = sub[:KeySize]
}
if len(sub) < KeySize {
sub = sub + string(make([]byte, KeySize-len(sub)))
}
return bulk.Add(sub, value, expire)
}
func (c *Container) Has(key string) bool {
c.Mut.RLock()
defer c.Mut.RUnlock()
_, ok := c.bulks[key]
return ok
}
func (c *Container) Remove(key string) {
bulk, ok := c.GetBulk(key)
if ok {
bulk.Stop()
}
c.Mut.Lock()
defer c.Mut.Unlock()
delete(c.bulks, key)
}
func (c *Container) Flush() {
c.Mut.Lock()
defer c.Mut.Unlock()
c.bulks = map[string]Bulk{}
}
//just for debug
func (c *Container) Each(handler EachHandler) {
c.Mut.RLock()
defer c.Mut.RUnlock()
for _, b := range c.bulks {
handler(b.GetAliveInBulk())
}
}
func (c *Container) master() {
for {
<-time.After(time.Second * 3)
for k, v := range c.bulks {
if v.Len() == 0 {
c.Remove(k)
}
}
}
}
func init() {
Default = NewContainer("", "")
}