This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
/
kvstore.go
128 lines (108 loc) · 2.78 KB
/
kvstore.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package kvstore
import (
"path/filepath"
"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/rancher/longhorn-manager/types"
)
type Backend interface {
Set(key string, obj interface{}) error
Get(key string, obj interface{}) error
Delete(key string) error
Keys(prefix string) ([]string, error)
IsNotFoundError(err error) bool
}
type KVStore struct {
Prefix string
b Backend
}
const (
keyHosts = "hosts"
keySettings = "settings"
)
func NewKVStore(prefix string, backend Backend) (*KVStore, error) {
if backend == nil {
return nil, errors.Errorf("invalid empty backend")
}
return &KVStore{
Prefix: prefix,
b: backend,
}, nil
}
func (s *KVStore) key(key string) string {
// It's not file path, but we use it to deal with '/'
return filepath.Join(s.Prefix, key)
}
func (s *KVStore) hostKey(id string) string {
return filepath.Join(s.key(keyHosts), id)
}
func (s *KVStore) SetHost(host *types.HostInfo) error {
if err := s.b.Set(s.hostKey(host.UUID), host); err != nil {
return err
}
logrus.Infof("Add host %v name %v longhorn-manager address %v", host.UUID, host.Name, host.Address)
return nil
}
func (s *KVStore) GetHost(id string) (*types.HostInfo, error) {
host, err := s.getHostByKey(s.hostKey(id))
if err != nil {
return nil, errors.Wrap(err, "unable to get host")
}
return host, nil
}
func (s *KVStore) getHostByKey(key string) (*types.HostInfo, error) {
host := types.HostInfo{}
if err := s.b.Get(key, &host); err != nil {
if s.b.IsNotFoundError(err) {
return nil, nil
}
return nil, err
}
return &host, nil
}
func (s *KVStore) ListHosts() (map[string]*types.HostInfo, error) {
hostKeys, err := s.b.Keys(s.key(keyHosts))
if err != nil {
return nil, err
}
hosts := make(map[string]*types.HostInfo)
for _, key := range hostKeys {
host, err := s.getHostByKey(key)
if err != nil {
return nil, errors.Wrapf(err, "invalid key %v", key)
}
if host != nil {
hosts[host.UUID] = host
}
}
return hosts, nil
}
func (s *KVStore) settingsKey() string {
return s.key(keySettings)
}
func (s *KVStore) SetSettings(settings *types.SettingsInfo) error {
if err := s.b.Set(s.settingsKey(), settings); err != nil {
return err
}
return nil
}
func (s *KVStore) GetSettings() (*types.SettingsInfo, error) {
settings := &types.SettingsInfo{}
if err := s.b.Get(s.settingsKey(), &settings); err != nil {
if s.b.IsNotFoundError(err) {
return nil, nil
}
return nil, errors.Wrap(err, "unable to get settings")
}
return settings, nil
}
// kuNuclear is test only function, which will wipe all longhorn entries
func (s *KVStore) kvNuclear(nuclearCode string) error {
if nuclearCode != "nuke key value store" {
return errors.Errorf("invalid nuclear code!")
}
if err := s.b.Delete(s.key("")); err != nil {
return err
}
return nil
}