/
batchget_req.go
162 lines (136 loc) · 4.67 KB
/
batchget_req.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
package request
import (
"github.com/tencentyun/tcaplusdb-go-sdk/tdr/common"
"github.com/tencentyun/tcaplusdb-go-sdk/tdr/logger"
"github.com/tencentyun/tcaplusdb-go-sdk/tdr/protocol/tcaplus_protocol_cs"
"github.com/tencentyun/tcaplusdb-go-sdk/tdr/record"
"github.com/tencentyun/tcaplusdb-go-sdk/tdr/terror"
)
type batchGetRequest struct {
appId uint64
zoneId uint32
tableName string
cmd int
seq uint32
record []*record.Record
pkg *tcaplus_protocol_cs.TCaplusPkg
valueNameMap map[string]bool
idx int
}
func newBatchGetRequest(appId uint64, zoneId uint32, tableName string, cmd int,
seq uint32, pkg *tcaplus_protocol_cs.TCaplusPkg) (*batchGetRequest, error) {
if pkg == nil || pkg.Body == nil || pkg.Body.BatchGetReq == nil {
return nil, &terror.ErrorCode{Code: terror.API_ERR_PARAMETER_INVALID, Message: "pkg init fail"}
}
pkg.Head.KeyInfo.FieldNum = 0
req := &batchGetRequest{
appId: appId,
zoneId: zoneId,
tableName: tableName,
cmd: cmd,
seq: seq,
record: nil,
pkg: pkg,
valueNameMap: make(map[string]bool),
}
return req, nil
}
func (req *batchGetRequest) AddRecord(index int32) (*record.Record, error) {
//batchGetReq := req.pkg.Body.BatchGetReq
rec := &record.Record{
AppId: req.appId,
ZoneId: req.zoneId,
TableName: req.tableName,
Cmd: req.cmd,
KeyMap: make(map[string][]byte),
ValueMap: make(map[string][]byte),
Version: -1,
KeySet: nil,
ValueSet: nil,
UpdFieldSet: nil,
SplitTableKeyBuff: nil,
}
rec.KeySet = new(tcaplus_protocol_cs.TCaplusKeySet)
rec.SplitTableKeyBuff = new(tcaplus_protocol_cs.SplitTableKeyBuff)
req.pkg.Body.BatchGetReq.SplitTableKeyBuffs = append(req.pkg.Body.BatchGetReq.SplitTableKeyBuffs,
rec.SplitTableKeyBuff)
req.record = append(req.record, rec)
return rec, nil
}
func (req *batchGetRequest) SetAsyncId(id uint64) {
req.pkg.Head.AsynID = id
}
func (req *batchGetRequest) SetVersionPolicy(p uint8) error {
return &terror.ErrorCode{Code: terror.ParameterInvalid, Message: "BatchGet not Support VersionPolicy"}
}
func (req *batchGetRequest) SetResultFlag(flag int) error {
return &terror.ErrorCode{Code: terror.ParameterInvalid, Message: "BatchGet not Support ResultFlag"}
}
func (req *batchGetRequest) Pack() ([]byte, error) {
if len(req.record) == 0 {
return nil, &terror.ErrorCode{Code: terror.RequestHasNoRecord}
}
for _, rec := range req.record {
if err := rec.PackKey(); err != nil {
logger.ERR("record pack key failed, %s", err.Error())
return nil, err
}
req.pkg.Body.BatchGetReq.RecordNum += 1
req.pkg.Body.BatchGetReq.KeyInfo = append(req.pkg.Body.BatchGetReq.KeyInfo, rec.KeySet)
}
req.pkg.Body.BatchGetReq.ValueInfo.FieldNum = 0
for key, _ := range req.valueNameMap {
req.pkg.Body.BatchGetReq.ValueInfo.FieldNum += 1
req.pkg.Body.BatchGetReq.ValueInfo.FieldName = append(req.pkg.Body.BatchGetReq.ValueInfo.FieldName, key)
}
logger.DEBUG("pack request %s", common.CsHeadVisualize(req.pkg.Head))
data, err := req.pkg.Pack(tcaplus_protocol_cs.TCaplusPkgCurrentVersion)
if err != nil {
logger.ERR("batchGetRequest pack failed, %s", err.Error())
return nil, err
}
logger.DEBUG("record pack success, app %d zone %d table %s", req.appId, req.zoneId, req.tableName)
return data, nil
}
func (req *batchGetRequest) GetZoneId() uint32 {
return req.zoneId
}
func (req *batchGetRequest) GetKeyHash() (uint32, error) {
if len(req.record) == 0 {
return 0, &terror.ErrorCode{Code: terror.RequestHasNoRecord}
}
return keyHashCode(req.record[0].KeySet)
}
func (req *batchGetRequest) SetFieldNames(valueNameList []string) error {
for _, v := range valueNameList {
req.valueNameMap[v] = true
}
return nil
}
func (req *batchGetRequest) SetUserBuff(userBuffer []byte) error {
return setUserBuffer(req.pkg, userBuffer)
}
func (req *batchGetRequest) GetSeq() int32 {
return req.pkg.Head.Seq
}
func (req *batchGetRequest) SetSeq(seq int32) {
req.pkg.Head.Seq = seq
}
func (req *batchGetRequest) SetResultLimit(limit int32, offset int32) int32 {
return int32(terror.API_ERR_OPERATION_TYPE_NOT_MATCH)
}
func (req *batchGetRequest) SetAddableIncreaseFlag(increase_flag byte) int32 {
return int32(terror.GEN_ERR_SUC)
}
func (req *batchGetRequest) SetMultiResponseFlag(multi_flag byte) int32 {
if 0 != multi_flag {
req.pkg.Body.BatchGetReq.AllowMultiResponses = 1
}
return int32(terror.GEN_ERR_SUC)
}
func (req *batchGetRequest) SetResultFlagForSuccess(result_flag byte) int {
return terror.GEN_ERR_SUC
}
func (req *batchGetRequest) SetResultFlagForFail(result_flag byte) int {
return terror.GEN_ERR_SUC
}