forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_store.go
145 lines (109 loc) · 2.62 KB
/
scheduler_store.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
package scheduler
import (
"errors"
"fmt"
"strings"
"sync"
)
//Store define the basic operations for storing and managing policy watcher.
type Store interface {
//Put a new policy in.
Put(key string, value *Watcher) error
//Get the corresponding policy with the key.
Get(key string) *Watcher
//Exists is to check if the key existing in the store.
Exists(key string) bool
//Remove the specified policy and return its reference.
Remove(key string) *Watcher
//Size return the total count of items in store.
Size() uint32
//GetAll is to get all the items in the store.
GetAll() []*Watcher
//Clear store.
Clear()
}
//DefaultStore implements Store interface to keep the scheduled policies.
//Not support concurrent sync.
type DefaultStore struct {
//Support sync locking
*sync.RWMutex
//Map used to keep the policy list.
data map[string]*Watcher
}
//NewDefaultStore is used to create a new store and return the pointer reference.
func NewDefaultStore() *DefaultStore {
return &DefaultStore{new(sync.RWMutex), make(map[string]*Watcher)}
}
//Put a policy into store.
func (cs *DefaultStore) Put(key string, value *Watcher) error {
if strings.TrimSpace(key) == "" || value == nil {
return errors.New("Bad arguments")
}
cs.Lock()
defer cs.Unlock()
if _, ok := cs.data[key]; ok {
return fmt.Errorf("Duplicayed policy with name %s", key)
}
cs.data[key] = value
return nil
}
//Get policy via key.
func (cs *DefaultStore) Get(key string) *Watcher {
if strings.TrimSpace(key) == "" {
return nil
}
cs.RLock()
defer cs.RUnlock()
return cs.data[key]
}
//Exists is used to check whether or not the key exists in store.
func (cs *DefaultStore) Exists(key string) bool {
if strings.TrimSpace(key) == "" {
return false
}
cs.RLock()
defer cs.RUnlock()
_, ok := cs.data[key]
return ok
}
//Remove is to delete the specified policy.
func (cs *DefaultStore) Remove(key string) *Watcher {
if strings.TrimSpace(key) == "" {
return nil
}
cs.Lock()
defer cs.Unlock()
if wt, ok := cs.data[key]; ok {
delete(cs.data, key)
return wt
}
return nil
}
//Size return the total count of items in store.
func (cs *DefaultStore) Size() uint32 {
cs.RLock()
defer cs.RUnlock()
return (uint32)(len(cs.data))
}
//GetAll to get all the items of store.
func (cs *DefaultStore) GetAll() []*Watcher {
cs.RLock()
defer cs.RUnlock()
all := []*Watcher{}
for _, v := range cs.data {
all = append(all, v)
}
return all
}
//Clear all the items in store.
func (cs *DefaultStore) Clear() {
cs.Lock()
defer cs.Unlock()
if (uint32)(len(cs.data)) == 0 {
return
}
for k, v := range cs.data {
delete(cs.data, k)
v.Stop()
}
}