forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caches.go
96 lines (81 loc) · 2.73 KB
/
caches.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
package bundle
import (
"sort"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/lib/cache"
"github.com/dafanshu/benthos/v3/lib/types"
)
// AllCaches is a set containing every single cache that has been imported.
var AllCaches = &CacheSet{
specs: map[string]cacheSpec{},
}
//------------------------------------------------------------------------------
// CacheAdd adds a new cache to this environment by providing a constructor
// and documentation.
func (e *Environment) CacheAdd(constructor CacheConstructor, spec docs.ComponentSpec) error {
return e.caches.Add(constructor, spec)
}
// CacheInit attempts to initialise a cache from a config.
func (e *Environment) CacheInit(conf cache.Config, mgr NewManagement) (types.Cache, error) {
return e.caches.Init(conf, mgr)
}
// CacheDocs returns a slice of cache specs, which document each method.
func (e *Environment) CacheDocs() []docs.ComponentSpec {
return e.caches.Docs()
}
//------------------------------------------------------------------------------
// CacheConstructor constructs an cache component.
type CacheConstructor func(cache.Config, NewManagement) (types.Cache, error)
type cacheSpec struct {
constructor CacheConstructor
spec docs.ComponentSpec
}
// CacheSet contains an explicit set of caches available to a Benthos service.
type CacheSet struct {
specs map[string]cacheSpec
}
// Add a new cache to this set by providing a spec (name, documentation, and
// constructor).
func (s *CacheSet) Add(constructor CacheConstructor, spec docs.ComponentSpec) error {
if s.specs == nil {
s.specs = map[string]cacheSpec{}
}
s.specs[spec.Name] = cacheSpec{
constructor: constructor,
spec: spec,
}
docs.RegisterDocs(spec)
return nil
}
// Init attempts to initialise an cache from a config.
func (s *CacheSet) Init(conf cache.Config, mgr NewManagement) (types.Cache, error) {
spec, exists := s.specs[conf.Type]
if !exists {
// TODO: V4 Remove this
if ctor, exists := cache.GetDeprecatedPlugin(conf.Type); exists {
return ctor(conf, mgr, mgr.Logger(), mgr.Metrics())
}
return nil, types.ErrInvalidCacheType
}
return spec.constructor(conf, mgr)
}
// Docs returns a slice of cache specs, which document each method.
func (s *CacheSet) Docs() []docs.ComponentSpec {
var docs []docs.ComponentSpec
for _, v := range s.specs {
docs = append(docs, v.spec)
}
sort.Slice(docs, func(i, j int) bool {
return docs[i].Name < docs[j].Name
})
return docs
}
// DocsFor returns the documentation for a given component name, returns a
// boolean indicating whether the component name exists.
func (s *CacheSet) DocsFor(name string) (docs.ComponentSpec, bool) {
c, ok := s.specs[name]
if !ok {
return docs.ComponentSpec{}, false
}
return c.spec, true
}