-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatenaDb.go
201 lines (161 loc) · 4.26 KB
/
atenaDb.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
193
194
195
196
197
198
199
200
201
package atena
import (
"context"
"crypto/tls"
"errors"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
pb "github.com/mchl-coder/atenadb-go-driver/atena"
)
const (
address = "localhost:5001"
)
// Ops implements atena Db operations
type Ops struct {
// User info
user string
// DB connection info
DB string
url string
// Auth
token string
// gRPC Connection
conn *grpc.ClientConn
c pb.AtenaDBClient
}
// Build creates the DB client
func Build(user string, password string, url string, db string) (*Ops, error) {
client := new(Ops)
log.Println("done")
// Set up a connection to the server.
config := &tls.Config{
InsecureSkipVerify: true,
}
conn, err := grpc.Dial(url, grpc.WithTransportCredentials(credentials.NewTLS(config)), grpc.WithBlock())
if err != nil {
log.Fatalf("Did not connect: %v", err)
return client, err
}
c := pb.NewAtenaDBClient(conn)
client.conn = conn
client.c = c
token, err := client.auth(user, password, url, db)
if err != nil || token == "" {
return client, err
}
client.token = token
client.user = user
client.url = url
client.DB = db
return client, nil
}
// BuildDefault creates the DB client with default url
func BuildDefault(user string, password string, db string) (*Ops, error) {
client := new(Ops)
// Set up a connection to the server.
config := &tls.Config{
InsecureSkipVerify: true,
}
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(credentials.NewTLS(config)), grpc.WithBlock())
if err != nil {
log.Fatalf("Did not connect: %v", err)
return client, err
}
c := pb.NewAtenaDBClient(conn)
client.conn = conn
client.c = c
token, err := client.auth(user, password, address, db)
if err != nil || token == "" {
return client, err
}
client.token = token
client.user = user
client.url = address
client.DB = db
return client, nil
}
// Auth
func (client *Ops) auth(user string, password string, url string, db string) (string, error) {
if user == "" || password == "" || db == "" {
return "", errors.New("Error")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.c.Auth(ctx, &pb.AuthLookupModel{User: user, Password: password, Db: db})
if err != nil || !r.Successful {
return "", errors.New("Error")
}
return r.GetToken(), nil
}
// Set operation
func (client *Ops) Set(key string, value string) bool {
// if string is null or empty or whitespace => Exception
if key == "" || value == "" {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.c.SetRecord(ctx, &pb.AtenaSet{Token: client.token, Key: key, Value: value})
if err != nil {
return false
}
return r.GetSuccessful()
}
// Get operation
func (client *Ops) Get(key string) (string, error) {
if key == "" {
return "", errors.New("Error: Unvalid key")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.c.GetRecord(ctx, &pb.AtenaGet{Token: client.token, Query: key})
if err != nil {
return "", err
}
return r.GetValue(), nil
}
// Del operation
func (client *Ops) Del(key string) bool {
if key == "" {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.c.DeleteRecord(ctx, &pb.AtenaDel{Token: client.token, Key: key})
if err != nil {
return false
}
return r.GetSuccessful()
}
// Incr operation
func (client *Ops) Incr(key string, inc int32) (string, error) {
if inc == 0 {
return "", errors.New("Error: Increment must be different than 0")
}
if key == "" {
return "", errors.New("Error: Unvalid Key")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.c.IncrRecord(ctx, &pb.AtenaIncr{Token: client.token, Key: key, Inc: inc})
if err != nil {
return "", err
}
return r.GetValue(), nil
}
// RemoveAll removes all the K/V records stored
func (client *Ops) RemoveAll() bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.c.RemoveAll(ctx, &pb.RemoveAllRecords{Token: client.token})
if err != nil {
return false
}
return r.GetSuccessful()
}
// Dispose connection
func (client *Ops) Dispose() {
client.conn.Close()
}