-
Notifications
You must be signed in to change notification settings - Fork 113
/
watcher.go
57 lines (48 loc) · 897 Bytes
/
watcher.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
package database
// Access enum of type of access
type Access int
// const .
const (
Read Access = iota
Write
)
// Watcher of db access
type Watcher struct {
m map[string]Access
database
}
// Get record and get
func (r *Watcher) Get(key string) (value string) {
if r.m[key] != Write {
r.m[key] = Read
}
return r.database.Get(key)
}
// Put ...
func (r *Watcher) Put(key, value string) {
r.m[key] = Write
r.database.Put(key, value)
}
// Del ...
func (r *Watcher) Del(key string) {
r.m[key] = Write
r.database.Del(key)
}
// Has ...
func (r *Watcher) Has(key string) bool {
if r.m[key] != Write {
r.m[key] = Read
}
return r.database.Has(key)
}
// Map map the access of this watcher
func (r *Watcher) Map() map[string]Access {
return r.m
}
// NewWatcher ...
func NewWatcher(db database) *Watcher {
return &Watcher{
m: make(map[string]Access),
database: db,
}
}