Skip to content

Commit

Permalink
move tikv.NewTxnClient to txnkv.NewClient (#391)
Browse files Browse the repository at this point in the history
Signed-off-by: disksing <i@disksing.com>
  • Loading branch information
disksing authored Dec 10, 2021
1 parent c0e8766 commit 87c1c58
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
6 changes: 3 additions & 3 deletions examples/txnkv/txnkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
30 changes: 0 additions & 30 deletions tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
57 changes: 57 additions & 0 deletions txnkv/client.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 87c1c58

Please sign in to comment.