-
Notifications
You must be signed in to change notification settings - Fork 466
/
tablestore.go
236 lines (184 loc) · 5.97 KB
/
tablestore.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
/*
Copyright 2021 The Dapr Authors
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 tablestore
import (
"context"
"reflect"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
jsoniter "github.com/json-iterator/go"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/state"
"github.com/dapr/kit/logger"
"github.com/dapr/kit/ptr"
)
const (
stateKey = "stateKey"
stateValue = "stateValue"
sateEtag = "sateEtag"
)
type AliCloudTableStore struct {
state.BulkStore
logger logger.Logger
client tablestore.TableStoreApi
metadata tablestoreMetadata
features []state.Feature
}
type tablestoreMetadata struct {
Endpoint string `json:"endpoint"`
AccessKeyID string `json:"accessKeyID"`
AccessKey string `json:"accessKey"`
InstanceName string `json:"instanceName"`
TableName string `json:"tableName"`
}
func NewAliCloudTableStore(logger logger.Logger) state.Store {
s := &AliCloudTableStore{
features: []state.Feature{state.FeatureETag},
logger: logger,
}
s.BulkStore = state.NewDefaultBulkStore(s)
return s
}
func (s *AliCloudTableStore) Init(_ context.Context, metadata state.Metadata) error {
m, err := s.parse(metadata)
if err != nil {
return err
}
s.metadata = *m
s.client = tablestore.NewClient(m.Endpoint, m.InstanceName, m.AccessKeyID, m.AccessKey)
return nil
}
func (s *AliCloudTableStore) Features() []state.Feature {
return s.features
}
func (s *AliCloudTableStore) Get(ctx context.Context, req *state.GetRequest) (*state.GetResponse, error) {
criteria := &tablestore.SingleRowQueryCriteria{
PrimaryKey: s.primaryKey(req.Key),
TableName: s.metadata.TableName,
MaxVersion: 1,
}
rowGetReq := &tablestore.GetRowRequest{
SingleRowQueryCriteria: criteria,
}
resp, err := s.client.GetRow(rowGetReq)
if err != nil {
return nil, err
}
getResp := s.getResp(resp.Columns)
return getResp, nil
}
func (s *AliCloudTableStore) getResp(columns []*tablestore.AttributeColumn) *state.GetResponse {
getResp := &state.GetResponse{}
for _, column := range columns {
if column.ColumnName == stateValue {
getResp.Data = unmarshal(column.Value)
} else if column.ColumnName == sateEtag {
getResp.ETag = ptr.Of(column.Value.(string))
}
}
return getResp
}
// Options are ignored because this component requests all values in a single operation.
func (s *AliCloudTableStore) BulkGet(ctx context.Context, reqs []state.GetRequest, _ state.BulkGetOpts) ([]state.BulkGetResponse, error) {
// "len == 0": empty request, directly return empty response
if len(reqs) == 0 {
return []state.BulkGetResponse{}, nil
}
mqCriteria := &tablestore.MultiRowQueryCriteria{
TableName: s.metadata.TableName,
MaxVersion: 1,
}
for _, req := range reqs {
mqCriteria.AddRow(s.primaryKey(req.Key))
}
batchGetReq := &tablestore.BatchGetRowRequest{
MultiRowQueryCriteria: []*tablestore.MultiRowQueryCriteria{
mqCriteria,
},
}
batchGetResp, err := s.client.BatchGetRow(batchGetReq)
if err != nil {
return nil, err
}
responseList := make([]state.BulkGetResponse, len(batchGetResp.TableToRowsResult[mqCriteria.TableName]))
for i, row := range batchGetResp.TableToRowsResult[mqCriteria.TableName] {
resp := s.getResp(row.Columns)
responseList[i] = state.BulkGetResponse{
Data: resp.Data,
ETag: resp.ETag,
Key: row.PrimaryKey.PrimaryKeys[0].Value.(string),
}
}
return responseList, nil
}
func (s *AliCloudTableStore) Set(ctx context.Context, req *state.SetRequest) error {
change := s.updateRowChange(req)
request := &tablestore.UpdateRowRequest{
UpdateRowChange: change,
}
_, err := s.client.UpdateRow(request)
return err
}
func (s *AliCloudTableStore) updateRowChange(req *state.SetRequest) *tablestore.UpdateRowChange {
change := &tablestore.UpdateRowChange{
PrimaryKey: s.primaryKey(req.Key),
TableName: s.metadata.TableName,
}
value, _ := marshal(req.Value)
change.PutColumn(stateValue, value)
if req.HasETag() {
change.PutColumn(sateEtag, *req.ETag)
}
change.SetCondition(tablestore.RowExistenceExpectation_IGNORE) //nolint:nosnakecase
return change
}
func marshal(value interface{}) ([]byte, error) {
v, _ := jsoniter.MarshalToString(value)
return []byte(v), nil
}
func unmarshal(val interface{}) []byte {
var output string
jsoniter.UnmarshalFromString(string(val.([]byte)), &output)
return []byte(output)
}
func (s *AliCloudTableStore) Delete(ctx context.Context, req *state.DeleteRequest) error {
change := s.deleteRowChange(req)
deleteRowReq := &tablestore.DeleteRowRequest{
DeleteRowChange: change,
}
_, err := s.client.DeleteRow(deleteRowReq)
return err
}
func (s *AliCloudTableStore) deleteRowChange(req *state.DeleteRequest) *tablestore.DeleteRowChange {
change := &tablestore.DeleteRowChange{
PrimaryKey: s.primaryKey(req.Key),
TableName: s.metadata.TableName,
}
change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST) //nolint:nosnakecase
return change
}
func (s *AliCloudTableStore) parse(meta state.Metadata) (*tablestoreMetadata, error) {
var m tablestoreMetadata
err := metadata.DecodeMetadata(meta.Properties, &m)
return &m, err
}
func (s *AliCloudTableStore) primaryKey(key string) *tablestore.PrimaryKey {
pk := &tablestore.PrimaryKey{}
pk.AddPrimaryKeyColumn(stateKey, key)
return pk
}
func (s *AliCloudTableStore) GetComponentMetadata() map[string]string {
metadataStruct := tablestoreMetadata{}
metadataInfo := map[string]string{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.StateStoreType)
return metadataInfo
}