-
Notifications
You must be signed in to change notification settings - Fork 0
/
int64_key_type.go
53 lines (40 loc) · 1.01 KB
/
int64_key_type.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
package ownmapdb
import (
"encoding/binary"
reflect "reflect"
"github.com/jamesrr39/goutil/errorsx"
"golang.org/x/exp/errors/fmt"
)
type int64ItemType int64
func (t *int64ItemType) MarshalKey() []byte {
bb := make([]byte, 8)
binary.LittleEndian.PutUint64(bb, uint64(*t))
return bb
}
func (t *int64ItemType) UnmarshalKey(data []byte) errorsx.Error {
if reflect.ValueOf(t).Kind() != reflect.Ptr {
return errorsx.Errorf("type must be a pointer")
}
num := binary.LittleEndian.Uint64(data)
v := int64ItemType(num)
*t = v
return nil
}
func (t *int64ItemType) Compare(other KeyType) ComparisonResult {
thisInt64 := *t
otherNumPtr := other.(*int64ItemType)
otherNum := *otherNumPtr
if thisInt64 == otherNum {
return ComparisonResultEqual
}
if thisInt64 > otherNum {
return ComparisonResultAGreaterThanB
}
return ComparisonResultALessThanB
}
func (t *int64ItemType) String() string {
return fmt.Sprintf("%d", *t)
}
func (t *int64ItemType) LowerThanLowestValidValue() KeyType {
return new(int64ItemType)
}