-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
100 lines (86 loc) · 1.86 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
package spdag
import (
"errors"
"sync"
)
// 轻量级缓存库。
type Cache struct {
spdagCache map[int]*spdag
rwmutex *sync.RWMutex
}
// NewCache 初始化缓存库。
func NewCache() *Cache {
return &Cache{
spdagCache: make(map[int]*spdag),
rwmutex: &sync.RWMutex{},
}
}
// Add 添加顶点数据至缓存库。
func (c *Cache) Init(key int, vertex *SubbranchPlan) *spdag {
c.rwmutex.Lock()
d, ok := c.spdagCache[key]
if !ok {
d = &spdag{
vertexsMap: make(map[int]*SubbranchPlan),
rwmutex: &sync.RWMutex{},
}
c.spdagCache[key] = d
}
d.vertexsMap[vertex.SubbranchPlanId] = vertex
c.rwmutex.Unlock()
return d
}
func (c *Cache) Build() {
for _, ca := range c.spdagCache {
for _, cv := range ca.vertexsMap {
ca.Build(cv)
}
}
}
// Get 获取key对应的有向无环图。
func (c *Cache) Get(key int) *spdag {
return c.spdagCache[key]
}
func (c *Cache) GetPlan(vertexID int) *SubbranchPlan {
for _, d := range c.spdagCache {
for _, v := range d.vertexsMap {
if v.SubbranchPlanId == vertexID {
return v
}
}
}
return nil
}
func (c *Cache) SpdagList() []*spdag {
sps := make([]*spdag, 0)
for _, d := range c.spdagCache {
sps = append(sps, d)
}
return sps
}
func (c *Cache) SPListByPlanIDS(seID int, planIDS []int) ([]*SubbranchPlan, error) {
v, ok := c.spdagCache[seID]
if !ok {
return nil, errors.New("子分部不存在。")
}
vertexs := make([]*SubbranchPlan, 0)
for _, sp := range v.vertexsMap {
for _, pid := range planIDS {
if sp.SubbranchPlanId == pid {
vertexs = append(vertexs, sp)
}
}
}
return vertexs, nil
}
func (c *Cache) SPListByPlanID(seID int, planID int) (*SubbranchPlan, error) {
v, ok := c.spdagCache[seID]
if !ok {
return nil, errors.New("子分部不存在。")
}
pv, pok := v.vertexsMap[planID]
if !pok {
return nil, errors.New("计划不存在。")
}
return pv, nil
}