-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
280 lines (221 loc) · 6.34 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2024 Collin Kreklow
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package t38c
import (
"fmt"
"net"
"strconv"
"github.com/mediocregopher/radix/v3"
)
// Database errors.
var (
errUninitialized = newError(nil, "database not initialized")
errResponse = newError(nil, "received error")
errArgs = newError(nil, "invalid arguments")
)
// Database is the primary object for interacting with the database.
// Database should not be created directly, instead use Connect() to
// retrieve a fully-initialized Database ready to be used.
//
// Functions other than Close() accept arguments in the same form as the
// Tile38 CLI. See https://tile38.com/commands/ for further information.
type Database struct {
pool *radix.Pool
}
// Connect establishes a connection and returns a Database object.
func Connect(server string, port string, poolsize int) (db *Database, err error) {
db = new(Database)
db.pool, err = radix.NewPool(
"tcp",
net.JoinHostPort(server, port),
poolsize,
radix.PoolConnFunc(connectJSON),
)
if err != nil {
return nil, newError(err, "error connecting to server")
}
return db, nil
}
// Close closes the database connection.
func (db *Database) Close() error {
if db.pool == nil {
return errUninitialized
}
err := db.pool.Close()
if err != nil {
err = newError(err, "error closing database connection")
}
return err
}
// Set saves an object to the database.
func (db *Database) Set(key string, id string, args ...string) (err error) {
if db.pool == nil {
return errUninitialized
}
if args == nil {
return errArgs
}
cmdargs := append([]string{key, id}, args...)
_, err = db.runcmd("SET", cmdargs...)
if err != nil {
return err
}
return nil
}
// Get returns the requested entry as a response object, or nil if the
// object is not found.
func (db *Database) Get(key string, id string, args ...string) (r *Response, err error) {
if db.pool == nil {
return nil, errUninitialized
}
cmdargs := []string{key, id}
if args != nil {
cmdargs = append(cmdargs, args...)
}
r, err = db.runcmd("GET", cmdargs...)
if err != nil {
if err.Error() == "received error: id not found" {
return nil, nil //nolint:nilnil // nil, nil expected when not found
}
return nil, err
}
return r, nil
}
// Scan iterates through a key returning a set of results.
func (db *Database) Scan(key string, args ...string) (r *Response, err error) {
if db.pool == nil {
return nil, errUninitialized
}
cmdargs := []string{key}
if args != nil {
cmdargs = append(cmdargs, args...)
}
r, err = db.runcmd("SCAN", cmdargs...)
if err != nil {
return nil, err
}
return r, nil
}
// Search iterates through the string values of a key returning a set of
// results.
func (db *Database) Search(key string, args ...string) (r *Response, err error) {
if db.pool == nil {
return nil, errUninitialized
}
cmdargs := []string{key}
if args != nil {
cmdargs = append(cmdargs, args...)
}
r, err = db.runcmd("SEARCH", cmdargs...)
if err != nil {
return nil, err
}
return r, nil
}
// Del deletes the requested entry.
func (db *Database) Del(key string, id string) (err error) {
if db.pool == nil {
return errUninitialized
}
_, err = db.runcmd("DEL", key, id)
if err != nil {
return err
}
return nil
}
// PDel deletes any entries matching the supplied pattern.
func (db *Database) PDel(key string, pattern string) (err error) {
if db.pool == nil {
return errUninitialized
}
_, err = db.runcmd("PDEL", key, pattern)
if err != nil {
return err
}
return nil
}
// Expire sets or resets the timeout value on the requested entry.
func (db *Database) Expire(key string, id string, seconds int) (err error) {
if db.pool == nil {
return errUninitialized
}
_, err = db.runcmd("EXPIRE", key, id, strconv.Itoa(seconds))
if err != nil {
return err
}
return nil
}
// Persist removes the timeout value on the requested entry.
func (db *Database) Persist(key string, id string) (err error) {
if db.pool == nil {
return errUninitialized
}
_, err = db.runcmd("PERSIST", key, id)
if err != nil {
return err
}
return nil
}
// TTL returns the timeout value on the requested entry.
func (db *Database) TTL(key string, id string) (ttl float64, err error) {
if db.pool == nil {
return 0, errUninitialized
}
r, err := db.runcmd("TTL", key, id)
if err != nil {
return 0, err
}
return r.TTL, nil
}
// runcmd runs a command against the database.
func (db *Database) runcmd(cmd string, args ...string) (r *Response, err error) {
if args == nil {
return nil, errArgs
}
r = new(Response)
err = db.pool.Do(radix.Cmd(r, cmd, args...))
if err != nil {
return nil, newError(err, "database error")
}
if !r.Ok {
return nil, fmt.Errorf("%w: %s", errResponse, r.Err)
}
return r, nil
}
// connectJSON creates a connection and sets the output mode to JSON.
func connectJSON(net, addr string) (conn radix.Conn, err error) {
conn, err = radix.Dial(net, addr)
if err != nil {
return nil, newError(err, "error connecting to database")
}
resp := new(Response)
err = conn.Do(radix.Cmd(resp, "OUTPUT", "json"))
if err != nil {
conn.Close() //nolint:errcheck // Close() in error path
return nil, newError(err, "error setting output to JSON")
}
if !resp.Ok {
conn.Close() //nolint:errcheck // Close() in error path
return nil, fmt.Errorf("%w: %s", errResponse, resp.Err)
}
return conn, nil
}