This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
/
db.go
192 lines (140 loc) · 3.93 KB
/
db.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package etcd
import (
_ "fmt"
_ "log"
"strings"
"time"
cli "github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
//global Constants releated to ETCD
const (
ETC_BASE_DIR = "/MrRedis"
ETC_INST_DIR = ETC_BASE_DIR + "/Instances"
ETC_CONF_DIR = ETC_BASE_DIR + "/Config"
)
type etcdDB struct {
C cli.Client //The client context
Kapi cli.KeysAPI //The api context for Get/Set/Delete/Update/Watcher etc.,
Ctx context.Context //Context for the connection mostly set to context.Background
Cfg cli.Config //Configuration details of the connection should be loaded from a configuration file
isSetup bool //Has this been setup
}
func New() *etcdDB {
return &etcdDB{isSetup: false}
}
func (db *etcdDB) Login() error {
var err error
db.C, err = cli.New(db.Cfg)
if err != nil {
return err
}
db.Kapi = cli.NewKeysAPI(db.C)
db.Ctx = context.Background()
return nil
}
// Setup will create/establish connection with the etcd store and also setup
// the nessary environment if etcd is running for the first time
// MrRedis will look for the following location in the central store
// /MrRedis/Instances/...... -> Will have the entries of all the instances
// /MrRedis/Config/.... -> Will have the entries of all the config information
func (db *etcdDB) Setup(config string) error {
var err error
db.Cfg = cli.Config{
Endpoints: []string{config},
Transport: cli.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
err = db.Login()
if err != nil {
return err
}
err = db.CreateSection(ETC_BASE_DIR)
if err != nil && strings.Contains(err.Error(), "Key already exists") != true {
return err
}
err = db.CreateSection(ETC_INST_DIR)
if err != nil && strings.Contains(err.Error(), "Key already exists") != true {
return err
}
err = db.CreateSection(ETC_CONF_DIR)
if err != nil && strings.Contains(err.Error(), "Key already exists") != true {
return err
}
db.isSetup = true
return nil
}
func (db *etcdDB) IsSetup() bool {
return db.isSetup
}
func (db *etcdDB) Set(Key string, Value string) error {
_, err := db.Kapi.Set(db.Ctx, Key, string(Value), nil)
return err
}
func (db *etcdDB) Get(Key string) (string, error) {
resp, err := db.Kapi.Get(db.Ctx, Key, nil)
if err != nil {
return "", err
} else {
return resp.Node.Value, nil
}
}
func (db *etcdDB) IsDir(Key string) (error, bool) {
resp, err := db.Kapi.Get(db.Ctx, Key, nil)
if err != nil {
return err, false
} else {
return nil, resp.Node.Dir
}
}
func (db *etcdDB) IsKey(Key string) (bool, error) {
_, err := db.Kapi.Get(db.Ctx, Key, nil)
if err != nil {
if strings.Contains(err.Error(), "Key not found") != true {
return false, err
} else {
return false, nil
}
} else {
return true, nil
}
}
func (db *etcdDB) Update(Key string, Value string, Lock bool) error {
return nil
}
func (db *etcdDB) Del(Key string) error {
_, err := db.Kapi.Delete(db.Ctx, Key, nil)
if err != nil {
return nil
}
return nil
}
//CreateSection will create a directory in etcd store
func (db *etcdDB) CreateSection(Key string) error {
_, err := db.Kapi.Set(db.Ctx, Key, "", &cli.SetOptions{Dir: true, PrevExist: cli.PrevNoExist})
if err != nil {
return err
}
return nil
}
// Delete section will delete a directory optionally delete
func (db *etcdDB) DeleteSection(Key string) error {
_, err := db.Kapi.Delete(db.Ctx, Key, &cli.DeleteOptions{Dir: true})
return err
}
func (db *etcdDB) ListSection(Key string, Recursive bool) ([]string, error) {
resp, err := db.Kapi.Get(db.Ctx, Key, &cli.GetOptions{Sort: true})
if err != nil {
return nil, err
}
retStr := make([]string, len(resp.Node.Nodes))
for i, n := range resp.Node.Nodes {
retStr[i] = n.Key
}
return retStr, nil
}
func (db *etcdDB) CleanSlate() error {
_, err := db.Kapi.Delete(db.Ctx, ETC_BASE_DIR, &cli.DeleteOptions{Dir: true, Recursive: true})
return err
}