forked from pingcap/go-ycsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.go
200 lines (170 loc) · 5.04 KB
/
raw.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
// Copyright 2018 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package tikv
import (
"context"
"fmt"
"strings"
"github.com/magiconair/properties"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/tikv/client-go/v2/rawkv"
"github.com/yizhuoliang/go-ycsb/pkg/util"
"github.com/yizhuoliang/go-ycsb/pkg/ycsb"
)
type rawDB struct {
db *rawkv.Client
r *util.RowCodec
bufPool *util.BufPool
}
func createRawDB(p *properties.Properties) (ycsb.DB, error) {
pdAddr := p.GetString(tikvPD, "127.0.0.1:2379")
apiVersionStr := strings.ToUpper(p.GetString(tikvAPIVersion, "V1"))
apiVersion, ok := kvrpcpb.APIVersion_value[apiVersionStr]
if !ok {
return nil, errors.Errorf("Invalid tikv apiversion %s.", apiVersionStr)
}
db, err := rawkv.NewClientWithOpts(context.Background(), strings.Split(pdAddr, ","),
rawkv.WithAPIVersion(kvrpcpb.APIVersion(apiVersion)))
if err != nil {
return nil, err
}
bufPool := util.NewBufPool()
return &rawDB{
db: db,
r: util.NewRowCodec(p),
bufPool: bufPool,
}, nil
}
func (db *rawDB) Close() error {
return db.db.Close()
}
func (db *rawDB) InitThread(ctx context.Context, _ int, _ int) context.Context {
return ctx
}
func (db *rawDB) CleanupThread(ctx context.Context) {
}
func (db *rawDB) getRowKey(table string, key string) []byte {
return util.Slice(fmt.Sprintf("%s:%s", table, key))
}
func (db *rawDB) Read(ctx context.Context, table string, key string, fields []string) (map[string][]byte, error) {
row, err := db.db.Get(ctx, db.getRowKey(table, key))
if err != nil {
return nil, err
} else if row == nil {
return nil, nil
}
return db.r.Decode(row, fields)
}
func (db *rawDB) BatchRead(ctx context.Context, table string, keys []string, fields []string) ([]map[string][]byte, error) {
rowKeys := make([][]byte, len(keys))
for i, key := range keys {
rowKeys[i] = db.getRowKey(table, key)
}
values, err := db.db.BatchGet(ctx, rowKeys)
if err != nil {
return nil, err
}
rowValues := make([]map[string][]byte, len(keys))
for i, value := range values {
if len(value) > 0 {
rowValues[i], err = db.r.Decode(value, fields)
} else {
rowValues[i] = nil
}
}
return rowValues, nil
}
func (db *rawDB) Scan(ctx context.Context, table string, startKey string, count int, fields []string) ([]map[string][]byte, error) {
_, rows, err := db.db.Scan(ctx, db.getRowKey(table, startKey), nil, count)
if err != nil {
return nil, err
}
res := make([]map[string][]byte, len(rows))
for i, row := range rows {
if row == nil {
res[i] = nil
continue
}
v, err := db.r.Decode(row, fields)
if err != nil {
return nil, err
}
res[i] = v
}
return res, nil
}
func (db *rawDB) Update(ctx context.Context, table string, key string, values map[string][]byte) error {
row, err := db.db.Get(ctx, db.getRowKey(table, key))
if err != nil {
return nil
}
data, err := db.r.Decode(row, nil)
if err != nil {
return err
}
for field, value := range values {
data[field] = value
}
// Update data and use Insert to overwrite.
return db.Insert(ctx, table, key, data)
}
func (db *rawDB) BatchUpdate(ctx context.Context, table string, keys []string, values []map[string][]byte) error {
var rawKeys [][]byte
var rawValues [][]byte
for i, key := range keys {
// TODO should we check the key exist?
rawKeys = append(rawKeys, db.getRowKey(table, key))
rawData, err := db.r.Encode(nil, values[i])
if err != nil {
return err
}
rawValues = append(rawValues, rawData)
}
return db.db.BatchPut(ctx, rawKeys, rawValues)
}
func (db *rawDB) Insert(ctx context.Context, table string, key string, values map[string][]byte) error {
// Simulate TiDB data
buf := db.bufPool.Get()
defer func() {
db.bufPool.Put(buf)
}()
buf, err := db.r.Encode(buf, values)
if err != nil {
return err
}
return db.db.Put(ctx, db.getRowKey(table, key), buf)
}
func (db *rawDB) BatchInsert(ctx context.Context, table string, keys []string, values []map[string][]byte) error {
var rawKeys [][]byte
var rawValues [][]byte
for i, key := range keys {
rawKeys = append(rawKeys, db.getRowKey(table, key))
rawData, err := db.r.Encode(nil, values[i])
if err != nil {
return err
}
rawValues = append(rawValues, rawData)
}
return db.db.BatchPut(ctx, rawKeys, rawValues)
}
func (db *rawDB) Delete(ctx context.Context, table string, key string) error {
return db.db.Delete(ctx, db.getRowKey(table, key))
}
func (db *rawDB) BatchDelete(ctx context.Context, table string, keys []string) error {
rowKeys := make([][]byte, len(keys))
for i, key := range keys {
rowKeys[i] = db.getRowKey(table, key)
}
return db.db.BatchDelete(ctx, rowKeys)
}