-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparator.go
56 lines (48 loc) · 1.12 KB
/
comparator.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
package ssdb
import (
"bytes"
)
type Comparator interface {
Compare(a, b []byte) int
Name() string
FindShortestSeparator(start *[]byte, limit []byte)
FindShortSuccessor(key *[]byte)
}
var BytewiseComparator bytewiseComparator
type bytewiseComparator struct{}
func (_ bytewiseComparator) Compare(a, b []byte) int {
return bytes.Compare(a, b)
}
func (_ bytewiseComparator) Name() string {
return "ssdb.BytewiseComparator"
}
func (c bytewiseComparator) FindShortestSeparator(start *[]byte, limit []byte) {
minLen := len(*start)
if minLen > len(limit) {
minLen = len(limit)
}
diffIndex := 0
for diffIndex < minLen && (*start)[diffIndex] == limit[diffIndex] {
diffIndex++
}
if diffIndex >= minLen {
} else {
diffByte := (*start)[diffIndex]
if diffByte < 0xff && diffByte+1 < limit[diffIndex] {
(*start)[diffIndex]++
*start = (*start)[:diffIndex+1]
if c.Compare(*start, limit) >= 0 {
panic("bytewiseComparator: start >= limit")
}
}
}
}
func (_ bytewiseComparator) FindShortSuccessor(key *[]byte) {
for i, b := range *key {
if b != 0xff {
(*key)[i] = b + 1
*key = (*key)[:i+1]
return
}
}
}