-
Notifications
You must be signed in to change notification settings - Fork 269
/
table.go
121 lines (104 loc) · 2.9 KB
/
table.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
// Copyright 2020 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 tablepb
import (
"bytes"
"encoding"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"sync/atomic"
)
// Load TableState with THREAD-SAFE
func (s *TableState) Load() TableState {
return TableState(atomic.LoadInt32((*int32)(s)))
}
// Store TableState with THREAD-SAFE
func (s *TableState) Store(new TableState) {
atomic.StoreInt32((*int32)(s), int32(new))
}
// CompareAndSwap is just like sync/atomic.Atomic*.CompareAndSwap.
func (s *TableState) CompareAndSwap(old, new TableState) bool {
oldx := int32(old)
newx := int32(new)
return atomic.CompareAndSwapInt32((*int32)(s), oldx, newx)
}
// TableID is the ID of the table
type TableID = int64
// Ts is the timestamp with a logical count
type Ts = uint64
// Key is a custom type for bytes encoded in memcomparable format.
// Key is read-only, must not be mutated.
type Key []byte
var (
_ fmt.Stringer = Key{}
_ fmt.Stringer = (*Key)(nil)
)
func (k Key) String() string {
return hex.EncodeToString(k)
}
var (
_ json.Marshaler = Key{}
_ json.Marshaler = (*Key)(nil)
)
// MarshalJSON implements json.Marshaler.
// It is helpful to format span in log.
func (k Key) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
}
var (
_ encoding.TextMarshaler = Span{}
_ encoding.TextMarshaler = (*Span)(nil)
)
// MarshalText implements encoding.TextMarshaler (used in proto.CompactTextString).
// It is helpful to format span in log.
func (s Span) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *Span) String() string {
length := len("{table_id:,start_key:,end_key:}")
length += 8 // for TableID
length += len(s.StartKey) + len(s.EndKey)
b := strings.Builder{}
b.Grow(length)
b.Write([]byte("{table_id:"))
b.Write([]byte(strconv.Itoa(int(s.TableID))))
if len(s.StartKey) > 0 {
b.Write([]byte(",start_key:"))
b.Write([]byte(s.StartKey.String()))
}
if len(s.EndKey) > 0 {
b.Write([]byte(",end_key:"))
b.Write([]byte(s.EndKey.String()))
}
b.Write([]byte("}"))
return b.String()
}
// Less compares two Spans, defines the order between spans.
func (s *Span) Less(b *Span) bool {
if s.TableID < b.TableID {
return true
}
if bytes.Compare(s.StartKey, b.StartKey) < 0 {
return true
}
return false
}
// Eq compares two Spans, defines the equality between spans.
func (s *Span) Eq(b *Span) bool {
return s.TableID == b.TableID &&
bytes.Equal(s.StartKey, b.StartKey) &&
bytes.Equal(s.EndKey, b.EndKey)
}