90 Star 482 Fork 142

平凯星辰(北京)科技有限公司 / tidb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tikv.go 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
Evgeniy Kulikov 提交于 2018-02-12 18:17 . *: Cleanup code (#5852)
// 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 mockstore
import (
"net/url"
"strings"
"github.com/juju/errors"
"github.com/pingcap/pd/pd-client"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/store/tikv"
)
// MockDriver is in memory mock TiKV driver.
type MockDriver struct {
}
// Open creates a MockTiKV storage.
func (d MockDriver) Open(path string) (kv.Storage, error) {
u, err := url.Parse(path)
if err != nil {
return nil, errors.Trace(err)
}
if !strings.EqualFold(u.Scheme, "mocktikv") {
return nil, errors.Errorf("Uri scheme expected(mocktikv) but found (%s)", u.Scheme)
}
return NewMockTikvStore(WithPath(u.Path))
}
type mockOptions struct {
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
clientHijack func(tikv.Client) tikv.Client
pdClientHijack func(pd.Client) pd.Client
path string
}
// MockTiKVStoreOption is used to control some behavior of mock tikv.
type MockTiKVStoreOption func(*mockOptions)
// WithHijackClient hijacks KV client's behavior, makes it easy to simulate the network
// problem between TiDB and TiKV.
func WithHijackClient(wrap func(tikv.Client) tikv.Client) MockTiKVStoreOption {
return func(c *mockOptions) {
c.clientHijack = wrap
}
}
// WithCluster provides the customized cluster.
func WithCluster(cluster *mocktikv.Cluster) MockTiKVStoreOption {
return func(c *mockOptions) {
c.cluster = cluster
}
}
// WithMVCCStore provides the customized mvcc store.
func WithMVCCStore(store mocktikv.MVCCStore) MockTiKVStoreOption {
return func(c *mockOptions) {
c.mvccStore = store
}
}
// WithPath specifies the mocktikv path.
func WithPath(path string) MockTiKVStoreOption {
return func(c *mockOptions) {
c.path = path
}
}
// NewMockTikvStore creates a mocked tikv store, the path is the file path to store the data.
// If path is an empty string, a memory storage will be created.
func NewMockTikvStore(options ...MockTiKVStoreOption) (kv.Storage, error) {
var opt mockOptions
for _, f := range options {
f(&opt)
}
client, pdClient, err := mocktikv.NewTestClient(opt.cluster, opt.mvccStore, opt.path)
if err != nil {
return nil, errors.Trace(err)
}
return tikv.NewTestTiKVStore(client, pdClient, opt.clientHijack, opt.pdClientHijack)
}
Go
1
https://gitee.com/pingcap/tidb.git
git@gitee.com:pingcap/tidb.git
pingcap
tidb
tidb
v2.0.11

搜索帮助