-
Notifications
You must be signed in to change notification settings - Fork 9
/
server_string.go
285 lines (257 loc) · 5.99 KB
/
server_string.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
281
282
283
284
285
package nutshttp
import (
"github.com/gin-gonic/gin"
"github.com/xujiajun/nutsdb"
)
func (s *NutsHTTPServer) Get(c *gin.Context) {
var (
err error
baseUri BaseUri
)
if err = c.ShouldBindUri(&baseUri); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
value, err := s.core.Get(baseUri.Bucket, baseUri.Key)
if err != nil {
switch err {
case nutsdb.ErrNotFoundKey:
WriteError(c, ErrKeyNotFoundInBucket)
default:
WriteError(c, ErrUnknown)
}
return
}
WriteSucc(c, value)
}
func (s *NutsHTTPServer) Update(c *gin.Context) {
type UpdateStringRequest struct {
Value string `json:"value" binding:"required"`
Ttl uint32 `json:"ttl"`
}
var (
err error
baseUri BaseUri
updateStringRequest UpdateStringRequest
)
if err = c.ShouldBindUri(&baseUri); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
if err = c.ShouldBindJSON(&updateStringRequest); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
err = s.core.Update(baseUri.Bucket, baseUri.Key, updateStringRequest.Value, updateStringRequest.Ttl)
if err != nil {
switch err {
case nutsdb.ErrNotFoundKey:
WriteError(c, ErrKeyNotFoundInBucket)
default:
WriteError(c, ErrUnknown)
}
return
}
WriteSucc(c, struct{}{})
}
func (s *NutsHTTPServer) MulDelete(context *gin.Context) {
type MulDeleteRequest struct {
Keys []string `json:"keys" binding:"required"`
}
var (
err error
mulDeleteRequest MulDeleteRequest
)
//get bucket from uri
param := context.Param("bucket")
if param == "" {
WriteError(context, ErrBucketEmpty)
return
}
bucket := param
if err = context.ShouldBindJSON(&mulDeleteRequest); err != nil {
WriteError(context, APIMessage{
Message: err.Error(),
})
return
}
for i := range mulDeleteRequest.Keys {
err = s.core.Delete(bucket, mulDeleteRequest.Keys[i])
//stop delete when error
if err != nil {
switch err {
case nutsdb.ErrKeyEmpty:
WriteError(context, ErrKeyNotFoundInBucket)
default:
WriteError(context, ErrUnknown)
}
return
}
}
WriteSucc(context, struct{}{})
}
func (s *NutsHTTPServer) Delete(c *gin.Context) {
var (
err error
baseUri BaseUri
)
if err = c.ShouldBindUri(&baseUri); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
_, err = s.core.Get(baseUri.Bucket, baseUri.Key)
if err != nil {
switch err {
case nutsdb.ErrNotFoundKey:
WriteError(c, ErrKeyNotFoundInBucket)
default:
WriteError(c, ErrUnknown)
}
return
}
err = s.core.Delete(baseUri.Bucket, baseUri.Key)
if err != nil {
switch err {
case nutsdb.ErrKeyEmpty:
WriteError(c, ErrKeyNotFoundInBucket)
default:
WriteError(c, ErrUnknown)
}
return
}
WriteSucc(c, struct{}{})
}
func (s *NutsHTTPServer) Scan(c *gin.Context) {
const (
PrefixScan = "prefixScan"
PrefixSearchScan = "prefixSearchScan"
RangeScan = "rangeScan"
GetAll = "getAll"
)
type ScanParam struct {
Bucket string `uri:"bucket" binding:"required"`
ScanType string `uri:"scanType" binding:"required"`
}
var (
err error
entries nutsdb.Entries
scanParam ScanParam
)
if err = c.ShouldBindUri(&scanParam); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
switch scanParam.ScanType {
case PrefixScan:
type ScanRequest struct {
OffSet *int `form:"offSet" binding:"required"`
LimitNum *int `form:"limitNum" binding:"required"`
Prefix *string `form:"prefix" binding:"required"`
}
var scanReq ScanRequest
if err = c.ShouldBindQuery(&scanReq); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
entries, err = s.core.PrefixScan(scanParam.Bucket, *scanReq.Prefix, *scanReq.OffSet, *scanReq.LimitNum)
if err != nil {
switch err {
case nutsdb.ErrPrefixScan:
WriteError(c, ErrPrefixScan)
default:
WriteError(c, ErrUnknown)
}
return
}
var res = map[string]string{}
for _, e := range entries {
res[string(e.Key)] = string(e.Value)
}
WriteSucc(c, res)
case PrefixSearchScan:
type ScanSearchReq struct {
OffSet *int `form:"offSet" binding:"required"`
LimitNum *int `form:"limitNum" binding:"required"`
Prefix *string `form:"prefix" binding:"required"`
Reg *string `form:"reg" binding:"required"`
}
var scanSearchReq ScanSearchReq
if err = c.ShouldBindQuery(&scanSearchReq); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
entries, err = s.core.PrefixSearchScan(scanParam.Bucket, *scanSearchReq.Prefix, *scanSearchReq.Reg, *scanSearchReq.OffSet, *scanSearchReq.LimitNum)
if err != nil {
switch err {
case nutsdb.ErrPrefixSearchScan:
WriteError(c, ErrPrefixSearchScan)
default:
WriteError(c, ErrUnknown)
}
return
}
var res = map[string]string{}
for _, e := range entries {
res[string(e.Key)] = string(e.Value)
}
WriteSucc(c, res)
case RangeScan:
type RangeScanReq struct {
Start *string `form:"start" binding:"required"`
End *string `form:"end" binding:"required"`
}
var rangeScanReq RangeScanReq
if err = c.ShouldBindQuery(&rangeScanReq); err != nil {
WriteError(c, APIMessage{
Message: err.Error(),
})
return
}
entries, err = s.core.RangeScan(scanParam.Bucket, *rangeScanReq.Start, *rangeScanReq.End)
if err != nil {
switch err {
case nutsdb.ErrRangeScan:
WriteError(c, ErrRangeScan)
default:
WriteError(c, ErrUnknown)
}
return
}
var res = map[string]string{}
for _, e := range entries {
res[string(e.Key)] = string(e.Value)
}
WriteSucc(c, res)
case GetAll:
entries, err = s.core.GetAll(scanParam.Bucket)
if err != nil {
switch err {
case nutsdb.ErrBucketEmpty:
WriteError(c, ErrBucketEmpty)
default:
WriteError(c, ErrUnknown)
}
return
}
var res = map[string]string{}
for _, e := range entries {
res[string(e.Key)] = string(e.Value)
}
WriteSucc(c, res)
}
return
}