forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
100 lines (85 loc) · 1.72 KB
/
source.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 memory
import (
"sync"
"time"
"github.com/google/uuid"
"github.com/pydio/cells/common/config/source"
"github.com/pydio/go-os/config"
)
type memory struct {
sync.RWMutex
ChangeSet *config.ChangeSet
Watchers map[string]*watcher
}
func (s *memory) Read() (*config.ChangeSet, error) {
s.RLock()
cs := &config.ChangeSet{
// Format: s.ChangeSet.Format,
Timestamp: s.ChangeSet.Timestamp,
Data: s.ChangeSet.Data,
Checksum: s.ChangeSet.Checksum,
Source: s.ChangeSet.Source,
}
s.RUnlock()
return cs, nil
}
func (s *memory) Watch() (config.SourceWatcher, error) {
w := &watcher{
Id: uuid.New().String(),
Updates: make(chan *config.ChangeSet, 100),
Source: s,
}
s.Lock()
s.Watchers[w.Id] = w
s.Unlock()
return w, nil
}
func (m *memory) Write(cs *config.ChangeSet) error {
m.Update(cs)
return nil
}
// Update allows manual updates of the config data.
func (s *memory) Update(c *config.ChangeSet) {
// don't process nil
if c == nil {
return
}
// hash the file
s.Lock()
// update changeset
s.ChangeSet = &config.ChangeSet{
Data: c.Data,
// Format: c.Format,
Source: "memory",
Timestamp: time.Now(),
}
// s.ChangeSet.Checksum = s.ChangeSet.Sum()
// update watchers
for _, w := range s.Watchers {
select {
case w.Updates <- s.ChangeSet:
default:
}
}
s.Unlock()
}
// Name of source
func (m *memory) String() string {
return "memory"
}
func NewSource(opts ...source.Option) config.Source {
var options source.Options
for _, o := range opts {
o(&options)
}
s := &memory{
Watchers: make(map[string]*watcher),
}
if options.Context != nil {
c, ok := options.Context.Value(changeSetKey{}).(*config.ChangeSet)
if ok {
s.Update(c)
}
}
return s
}