forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
187 lines (164 loc) · 4.95 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
package mock
import (
"bufio"
"bytes"
"fmt"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/redis/rueidis"
)
func Result(val rueidis.RedisMessage) rueidis.RedisResult {
r := result{val: val}
return *(*rueidis.RedisResult)(unsafe.Pointer(&r))
}
func ErrorResult(err error) rueidis.RedisResult {
r := result{err: err}
return *(*rueidis.RedisResult)(unsafe.Pointer(&r))
}
func RedisString(v string) rueidis.RedisMessage {
m := message{typ: '+', string: v}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisBlobString(v string) rueidis.RedisMessage {
m := message{typ: '$', string: v}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisError(v string) rueidis.RedisMessage {
m := message{typ: '-', string: v}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisInt64(v int64) rueidis.RedisMessage {
m := message{typ: ':', integer: v}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisFloat64(v float64) rueidis.RedisMessage {
m := message{typ: ',', string: strconv.FormatFloat(v, 'f', -1, 64)}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisBool(v bool) rueidis.RedisMessage {
m := message{typ: '#'}
if v {
m.integer = 1
}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisNil() rueidis.RedisMessage {
m := message{typ: '_'}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisArray(values ...rueidis.RedisMessage) rueidis.RedisMessage {
m := message{typ: '*', values: values}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func RedisMap(kv map[string]rueidis.RedisMessage) rueidis.RedisMessage {
values := make([]rueidis.RedisMessage, 0, 2*len(kv))
for k, v := range kv {
values = append(values, RedisString(k))
values = append(values, v)
}
m := message{typ: '%', values: values}
return *(*rueidis.RedisMessage)(unsafe.Pointer(&m))
}
func serialize(m message, buf *bytes.Buffer) {
switch m.typ {
case '$', '!', '=':
buf.WriteString(fmt.Sprintf("%s%d\r\n%s\r\n", string(m.typ), len(m.string), m.string))
case '+', '-', ',', '(':
buf.WriteString(fmt.Sprintf("%s%s\r\n", string(m.typ), m.string))
case ':', '#':
buf.WriteString(fmt.Sprintf("%s%d\r\n", string(m.typ), m.integer))
case '_':
buf.WriteString(fmt.Sprintf("%s\r\n", string(m.typ)))
case '*':
buf.WriteString(fmt.Sprintf("%s%d\r\n", string(m.typ), len(m.values)))
for _, v := range m.values {
pv := *(*message)(unsafe.Pointer(&v))
serialize(pv, buf)
}
case '%':
buf.WriteString(fmt.Sprintf("%s%d\r\n", string(m.typ), len(m.values)/2))
for _, v := range m.values {
pv := *(*message)(unsafe.Pointer(&v))
serialize(pv, buf)
}
}
}
func RedisResultStreamError(err error) rueidis.RedisResultStream {
s := stream{e: err}
return *(*rueidis.RedisResultStream)(unsafe.Pointer(&s))
}
func RedisResultStream(ms ...rueidis.RedisMessage) rueidis.RedisResultStream {
buf := bytes.NewBuffer(nil)
for _, m := range ms {
pm := *(*message)(unsafe.Pointer(&m))
serialize(pm, buf)
}
s := stream{n: len(ms), p: &pool{size: 1, cond: sync.NewCond(&sync.Mutex{})}, w: &pipe{r: bufio.NewReader(buf)}}
return *(*rueidis.RedisResultStream)(unsafe.Pointer(&s))
}
func MultiRedisResultStream(ms ...rueidis.RedisMessage) rueidis.MultiRedisResultStream {
return RedisResultStream(ms...)
}
func MultiRedisResultStreamError(err error) rueidis.RedisResultStream {
return RedisResultStreamError(err)
}
type message struct {
attrs *rueidis.RedisMessage
string string
values []rueidis.RedisMessage
integer int64
typ byte
ttl [7]byte
}
type result struct {
err error
val rueidis.RedisMessage
}
type pool struct {
dead any
cond *sync.Cond
make func() any
list []any
size int
down bool
}
type pipe struct {
conn net.Conn
error atomic.Value
clhks atomic.Value // closed hook, invoked after the conn is closed
pshks atomic.Value // pubsub hook, registered by the SetPubSubHooks
queue any
cache rueidis.CacheStore
r *bufio.Reader
w *bufio.Writer
close chan struct{}
onInvalidations func([]rueidis.RedisMessage)
r2psFn func() (p *pipe, err error) // func to build pipe for resp2 pubsub
r2pipe *pipe // internal pipe for resp2 pubsub only
ssubs *any // pubsub smessage subscriptions
nsubs *any // pubsub message subscriptions
psubs *any // pubsub pmessage subscriptions
info map[string]rueidis.RedisMessage
timeout time.Duration
pinggap time.Duration
maxFlushDelay time.Duration
once sync.Once
r2mu sync.Mutex
version int32
_ [10]int32
blcksig int32
state int32
waits int32
recvs int32
r2ps bool // identify this pipe is used for resp2 pubsub or not
}
type stream struct {
p *pool
w *pipe
e error
n int
}