forked from ngaut/unistore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
92 lines (81 loc) · 2.3 KB
/
util.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
// Copyright 2019-present 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 tikv
import (
"bytes"
"sort"
"time"
"github.com/dgryski/go-farm"
"github.com/pingcap/badger/y"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
)
func exceedEndKey(current, endKey []byte) bool {
if len(endKey) == 0 {
return false
}
return bytes.Compare(current, endKey) >= 0
}
func extractPhysicalTime(ts uint64) time.Time {
t := int64(ts >> 18) // 18 is for the logical time.
return time.Unix(t/1e3, (t%1e3)*1e6)
}
func tsSub(tsA, tsB uint64) time.Duration {
tsAPhysical := int64(tsA >> 18)
tsBPhysical := int64(tsB >> 18)
return time.Duration(tsAPhysical-tsBPhysical) * time.Millisecond
}
// SortAndDedupHashVals will change hashVals into sort ascending order and remove duplicates
func sortAndDedupHashVals(hashVals []uint64) []uint64 {
if len(hashVals) > 1 {
sort.Slice(hashVals, func(i, j int) bool {
return hashVals[i] < hashVals[j]
})
idx := 0
for i, v := range hashVals {
if i > 0 && hashVals[i] == hashVals[i-1] {
continue
}
hashVals[idx] = v
idx++
}
hashVals = hashVals[0:idx]
}
return hashVals
}
func mutationsToHashVals(mutations []*kvrpcpb.Mutation) []uint64 {
hashVals := make([]uint64, len(mutations))
for i, mut := range mutations {
hashVals[i] = farm.Fingerprint64(mut.Key)
}
hashVals = sortAndDedupHashVals(hashVals)
return hashVals
}
func keysToHashVals(keys ...[]byte) []uint64 {
hashVals := make([]uint64, len(keys))
for i, key := range keys {
hashVals[i] = farm.Fingerprint64(key)
}
hashVals = sortAndDedupHashVals(hashVals)
return hashVals
}
func userKeysToHashVals(keys ...y.Key) []uint64 {
hashVals := make([]uint64, len(keys))
for i, key := range keys {
hashVals[i] = farm.Fingerprint64(key.UserKey)
}
hashVals = sortAndDedupHashVals(hashVals)
return hashVals
}
func safeCopy(b []byte) []byte {
return append([]byte{}, b...)
}