-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
231 lines (190 loc) · 4.33 KB
/
result.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
// Copyright 2018 Eryx <evorui аt gmail dοt com>, All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package localfs
// refer https://raw.githubusercontent.com/lynkdb/kvgo/master/result.go
import (
"fmt"
"github.com/lessos/lessgo/types"
"github.com/lynkdb/iomix/skv"
)
const (
kvobj_t_v1 uint8 = 0x01
value_ns_bytes uint8 = 0x00
value_ns_json uint8 = 20
)
type Result struct {
status uint8
key []byte
data []byte
cap int
items []*Result
}
func newResult(status uint8, err error) *Result {
rs := &Result{
status: status,
}
if err != nil {
if status == 0 {
rs.status = skv.ResultError
}
rs.data = []byte(err.Error())
}
return rs
}
func (rs *Result) Status() uint8 {
return rs.status
}
func (rs *Result) ErrorString() string {
return fmt.Sprintf("ENO: %d, MSG: %s", rs.status, string(rs.data))
}
func (rs *Result) OK() bool {
return rs.status == skv.ResultOK
}
func (rs *Result) NotFound() bool {
return rs.status == skv.ResultNotFound
}
func (rs *Result) Bytes() []byte {
if len(rs.data) > 1 {
if rs.data[0] == kvobj_t_v1 {
offset := int(rs.data[1]) + 2
if offset < len(rs.data) {
return rs.data[offset:]
}
}
}
return rs.data
}
func (rs *Result) Bytex() types.Bytex {
return skv.KvValueBytes(rs.Bytes()).Bytex()
}
func (rs *Result) String() string {
return skv.KvValueBytes(rs.Bytes()).String()
}
func (rs *Result) Bool() bool {
return skv.KvValueBytes(rs.Bytes()).Bool()
}
func (rs *Result) Int() int {
return int(rs.Int64())
}
func (rs *Result) Int8() int8 {
return int8(rs.Int64())
}
func (rs *Result) Int16() int16 {
return int16(rs.Int64())
}
func (rs *Result) Int32() int32 {
return int32(rs.Int64())
}
func (rs *Result) Int64() int64 {
return skv.KvValueBytes(rs.Bytes()).Int64()
}
func (rs *Result) Uint() uint {
return uint(rs.Uint64())
}
func (rs *Result) Uint8() uint8 {
return uint8(rs.Uint64())
}
func (rs *Result) Uint16() uint16 {
return uint16(rs.Uint64())
}
func (rs *Result) Uint32() uint32 {
return uint32(rs.Uint64())
}
func (rs *Result) Uint64() uint64 {
return skv.KvValueBytes(rs.Bytes()).Uint64()
}
func (rs *Result) Float32() float32 {
return float32(rs.Float64())
}
func (rs *Result) Float64() float64 {
return skv.KvValueBytes(rs.Bytes()).Float64()
}
func (rs *Result) ListLen() int {
return len(rs.items)
}
func (rs *Result) List() []skv.Result {
ls := []skv.Result{}
for _, v := range rs.items {
ls = append(ls, v)
}
return ls
}
func (rs *Result) KvLen() int {
return len(rs.items) / 2
}
func (rs *Result) KvEach(fn func(entry *skv.ResultEntry) int) int {
for i := 1; i < len(rs.items); i += 2 {
if fn(&skv.ResultEntry{
Key: rs.items[i-1].data,
Value: rs.items[i].data,
}) != 0 {
return (i + 1) / 2
}
}
return rs.KvLen()
}
func (rs *Result) KvEntry(i int) *skv.ResultEntry {
if i < 0 {
i = 0
} else {
i = i * 2
}
if i+1 < len(rs.items) {
return &skv.ResultEntry{
Key: rs.items[i].data,
Value: rs.items[i+1].data,
}
}
return nil
}
func (rs *Result) KvList() []*skv.ResultEntry {
ls := []*skv.ResultEntry{}
for i := 1; i < len(rs.items); i += 2 {
ls = append(ls, &skv.ResultEntry{
Key: rs.items[i-1].data,
Value: rs.items[i].data,
})
}
return ls
}
func (rs *Result) KvSize() int {
return len(rs.items) / 2
}
func (rs *Result) KvPairs() []skv.Result {
ls := []skv.Result{}
for i := 1; i < len(rs.items); i += 2 {
ls = append(ls, &Result{
key: rs.items[i-1].data,
data: rs.items[i].data,
})
}
return ls
}
func (rs *Result) KvKey() []byte {
return rs.key
}
func (rs *Result) Decode(obj interface{}) error {
return skv.ValueDecode(rs.Bytes(), obj)
}
func (rs *Result) Meta() *skv.KvMeta {
if len(rs.data) > 1 {
if rs.data[0] == kvobj_t_v1 {
offset := int(rs.data[1]) + 2
if offset <= len(rs.data) {
return skv.KvMetaDecode(rs.data[2:offset])
}
}
}
return nil
}