-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
67 lines (56 loc) · 1.49 KB
/
registry.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
package afs
import (
"fmt"
"github.com/viant/afs/file"
"github.com/viant/afs/storage"
"github.com/viant/afs/url"
"sync"
)
//Provider represents manager provider
type Provider func(options ...storage.Option) (storage.Manager, error)
//Registry represents abstract file system service provider registry
type Registry interface {
//Register register schemeURL with storage service
Register(uRLScheme string, provider Provider)
//Get returns service provider for supplied schemeURL
Get(uRLScheme string) (Provider, error)
}
type registry struct {
providers map[string]Provider
*sync.RWMutex
}
func (r *registry) Register(URLScheme string, provider Provider) {
r.Lock()
defer r.Unlock()
r.providers[URLScheme] = provider
}
func (r *registry) Get(uRLScheme string) (Provider, error) {
r.RLock()
defer r.RUnlock()
provider, ok := r.providers[uRLScheme]
if !ok {
return nil, fmt.Errorf("failed to lookup storage provider %v", uRLScheme)
}
return provider, nil
}
var singleton Registry
//GetRegistry return singleton registry
func GetRegistry() Registry {
if singleton != nil {
return singleton
}
singleton = ®istry{
providers: make(map[string]Provider),
RWMutex: &sync.RWMutex{},
}
return singleton
}
//Manager returns a manager for supplied sourceURL
func Manager(URL string, options ...storage.Option) (storage.Manager, error) {
scheme := url.Scheme(URL, file.Scheme)
provider, err := GetRegistry().Get(scheme)
if err != nil {
return nil, err
}
return provider(options)
}