mirror of https://github.com/tikv/client-go.git
move tikv.NewTxnClient to txnkv.NewClient (#391)
Signed-off-by: disksing <i@disksing.com>
This commit is contained in:
parent
c0e8766154
commit
87c1c58064
|
|
@ -20,7 +20,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/tikv/client-go/v2/tikv"
|
||||
"github.com/tikv/client-go/v2/txnkv"
|
||||
)
|
||||
|
||||
// KV represents a Key-Value pair.
|
||||
|
|
@ -33,14 +33,14 @@ func (kv KV) String() string {
|
|||
}
|
||||
|
||||
var (
|
||||
client *tikv.KVStore
|
||||
client *txnkv.Client
|
||||
pdAddr = flag.String("pd", "127.0.0.1:2379", "pd address")
|
||||
)
|
||||
|
||||
// Init initializes information.
|
||||
func initStore() {
|
||||
var err error
|
||||
client, err = tikv.NewTxnClient([]string{*pdAddr})
|
||||
client, err = txnkv.NewClient([]string{*pdAddr})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
30
tikv/kv.go
30
tikv/kv.go
|
|
@ -190,36 +190,6 @@ func NewKVStore(uuid string, pdClient pd.Client, spkv SafePointKV, tikvclient Cl
|
|||
return store, nil
|
||||
}
|
||||
|
||||
// NewTxnClient creates a txn client with pdAddrs.
|
||||
func NewTxnClient(pdAddrs []string) (*KVStore, error) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
pdClient, err := NewPDClient(pdAddrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// init uuid
|
||||
// FIXME: uuid will be a very long and ugly string, simplify it.
|
||||
uuid := fmt.Sprintf("tikv-%v", pdClient.GetClusterID(context.TODO()))
|
||||
tlsConfig, err := cfg.Security.ToTLSConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
spkv, err := NewEtcdSafePointKV(pdAddrs, tlsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s, err := NewKVStore(uuid, pdClient, spkv, NewRPCClient(WithSecurity(cfg.Security)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.TxnLocalLatches.Enabled {
|
||||
s.EnableTxnLocalLatches(cfg.TxnLocalLatches.Capacity)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// NewPDClient creates pd.Client with pdAddrs.
|
||||
func NewPDClient(pdAddrs []string) (pd.Client, error) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2021 TiKV 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 txnkv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/tikv/client-go/v2/config"
|
||||
"github.com/tikv/client-go/v2/tikv"
|
||||
)
|
||||
|
||||
// Client is a txn client.
|
||||
type Client struct {
|
||||
*tikv.KVStore
|
||||
}
|
||||
|
||||
// NewClient creates a txn client with pdAddrs.
|
||||
func NewClient(pdAddrs []string) (*Client, error) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
pdClient, err := tikv.NewPDClient(pdAddrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// init uuid
|
||||
uuid := fmt.Sprintf("tikv-%v", pdClient.GetClusterID(context.TODO()))
|
||||
tlsConfig, err := cfg.Security.ToTLSConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
spkv, err := tikv.NewEtcdSafePointKV(pdAddrs, tlsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s, err := tikv.NewKVStore(uuid, pdClient, spkv, tikv.NewRPCClient(tikv.WithSecurity(cfg.Security)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.TxnLocalLatches.Enabled {
|
||||
s.EnableTxnLocalLatches(cfg.TxnLocalLatches.Capacity)
|
||||
}
|
||||
return &Client{KVStore: s}, nil
|
||||
}
|
||||
Loading…
Reference in New Issue