forked from tikv/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
62 lines (53 loc) · 1.73 KB
/
client.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
// Copyright 2019 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 txnkv
import (
"context"
"github.com/journeymidnight/client-go/config"
"github.com/journeymidnight/client-go/retry"
"github.com/journeymidnight/client-go/txnkv/store"
)
// Client is a transactional client of TiKV server.
type Client struct {
tikvStore *store.TiKVStore
}
// NewClient creates a client with PD addresses.
func NewClient(ctx context.Context, pdAddrs []string, config config.Config) (*Client, error) {
tikvStore, err := store.NewStore(ctx, pdAddrs, config)
if err != nil {
return nil, err
}
return &Client{
tikvStore: tikvStore,
}, nil
}
// Close stop the client.
func (c *Client) Close() error {
return c.tikvStore.Close()
}
// Begin creates a transaction for read/write.
func (c *Client) Begin(ctx context.Context) (*Transaction, error) {
ts, err := c.GetTS(ctx)
if err != nil {
return nil, err
}
return c.BeginWithTS(ctx, ts), nil
}
// BeginWithTS creates a transaction which is normally readonly.
func (c *Client) BeginWithTS(ctx context.Context, ts uint64) *Transaction {
return newTransaction(c.tikvStore, ts)
}
// GetTS returns a latest timestamp.
func (c *Client) GetTS(ctx context.Context) (uint64, error) {
return c.tikvStore.GetTimestampWithRetry(retry.NewBackoffer(ctx, retry.TsoMaxBackoff))
}