forked from portworx/kvdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvdb_mgr.go
78 lines (68 loc) · 1.92 KB
/
kvdb_mgr.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
package kvdb
import (
"fmt"
"sync"
)
var (
instance Kvdb
datastores = make(map[string]DatastoreInit)
datastoreVersions = make(map[string]DatastoreVersion)
lock sync.RWMutex
)
// Instance returns instance set via SetInstance, nil if none was set.
func Instance() Kvdb {
return instance
}
// SetInstance sets the singleton instance.
func SetInstance(kvdb Kvdb) error {
if instance == nil {
lock.Lock()
defer lock.Unlock()
if instance == nil {
instance = kvdb
return nil
}
}
return fmt.Errorf("Kvdb instance is already set to %q", instance.String())
}
// New return a new instance of KVDB as specified by datastore name.
// If domain is set all requests to KVDB are prefixed by domain.
// options is interpreted by backend KVDB.
func New(
name string,
domain string,
machines []string,
options map[string]string,
errorCB FatalErrorCB,
) (Kvdb, error) {
lock.RLock()
defer lock.RUnlock()
if dsInit, exists := datastores[name]; exists {
kvdb, err := dsInit(domain, machines, options, errorCB)
return kvdb, err
}
return nil, ErrNotSupported
}
// Register adds specified datastore backend to the list of options.
func Register(name string, dsInit DatastoreInit, dsVersion DatastoreVersion) error {
lock.Lock()
defer lock.Unlock()
if _, exists := datastores[name]; exists {
return fmt.Errorf("Datastore provider %q is already registered", name)
}
datastores[name] = dsInit
if _, exists := datastoreVersions[name]; exists {
return fmt.Errorf("Datastore provider's %q version function already registered", name)
}
datastoreVersions[name] = dsVersion
return nil
}
// Version returns the supported version for the provided kvdb endpoint.
func Version(name string, url string, kvdbOptions map[string]string) (string, error) {
lock.RLock()
defer lock.RUnlock()
if dsVersion, exists := datastoreVersions[name]; exists {
return dsVersion(url, kvdbOptions)
}
return "", ErrNotSupported
}