-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatch.go
163 lines (121 loc) · 3.12 KB
/
batch.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
// Copyright 2014 Eryx <evorui аt gmаil dοt cοm>, 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 ssdbgo // import "github.com/lynkdb/ssdbgo"
import (
"errors"
"time"
)
const (
// ssdb-server total amount of the commands and arguments must be less than 10MB.
batch_cmds_max int64 = 4194304 // 4 MB
)
type Batch struct {
cmds [][]interface{}
cr *Connector
}
type batch_cmds struct {
num int
req []byte
}
// Batch is in DEVELOPMENT PREVIEW, DO NOT USE IT IN PRODUCTION
//
// This feature is implemented in client side, the ssdb-server does not support
// batch command execution. The server will execute each command as if they are
// separated.
func (cr *Connector) Batch() *Batch {
return &Batch{
cmds: [][]interface{}{},
cr: cr,
}
}
func (b *Batch) Cmd(args ...interface{}) {
b.cmds = append(b.cmds, args)
}
func (b *Batch) Exec() ([]*Result, error) {
var (
rpls = []*Result{}
cmds = []batch_cmds{}
cmds_size int64 = 0
)
if len(b.cmds) < 1 || b.cr == nil {
return rpls, errors.New("client error")
}
for _, args := range b.cmds {
if buf, err := send_buf(args); err == nil {
n := int64(len(buf))
if n > batch_cmds_max {
return rpls, errors.New("client error")
}
if len(cmds) < 1 || (cmds_size+n) > batch_cmds_max {
cmds = append(cmds, batch_cmds{
num: 0,
req: []byte{},
})
cmds_size = 0
}
cmds[len(cmds)-1].num++
cmds[len(cmds)-1].req = append(cmds[len(cmds)-1].req, buf...)
cmds_size += n
} else {
return rpls, errors.New("client error")
}
}
if len(cmds) < 1 {
return rpls, errors.New("client error")
}
cn, _ := b.cr.pull()
tto := b.cr.ctimeout * 10
if tto > (600 * time.Second) {
tto = 600 * time.Second
}
for _, bcmd := range cmds {
tton := time.Now().Add(tto)
cn.sock.SetReadDeadline(tton)
cn.sock.SetWriteDeadline(tton)
if _, err := cn.sock.Write(bcmd.req); err != nil {
b.cr.push(cn)
b.cmds = [][]interface{}{}
b.cr = nil
return rpls, err
}
for i := 0; i < bcmd.num; i++ {
r := &Result{
Status: ResultError,
}
resp, err := cn.recv()
if err != nil {
r.Status = err.Error()
} else if len(resp) < 1 {
r.Status = ResultFail
} else {
switch resp[0].String() {
case ResultOK, ResultNotFound, ResultError, ResultFail, ResultClientError:
r.Status = resp[0].String()
}
if r.Status == ResultOK {
for k, v := range resp {
if k > 0 {
r.Items = append(r.Items, v)
}
}
}
}
rpls = append(rpls, r)
}
}
b.cr.push(cn)
b.cmds = [][]interface{}{}
b.cr = nil
return rpls, nil
}