-
Notifications
You must be signed in to change notification settings - Fork 0
/
boost.go
82 lines (63 loc) · 1.37 KB
/
boost.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
// Copyright 2017 - 2018 ibelie, Chen Jie, Joungtao. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package uuid
import (
"bytes"
"crypto/md5"
"github.com/ibelie/ruid"
"github.com/ibelie/tygo"
)
type ID UUID
var ZERO = ID{}
func New() ID {
return ID(NewV1())
}
func (u ID) Hash() ruid.ID {
return ID(md5.Sum(u[:]))
}
func (u ID) Lt(o ruid.ID) bool {
i := o.(ID)
return bytes.Compare(u[:], i[:]) < 0
}
func (u ID) Ge(o ruid.ID) bool {
i := o.(ID)
return bytes.Compare(u[:], i[:]) >= 0
}
func (u ID) Nonzero() bool {
return u != ZERO
}
func (u ID) String() string {
return ruid.RUIDEncoding.EncodeToString(u[:])
}
func (u ID) ByteSize() (size int) {
return 16
}
func (u ID) Serialize(output *tygo.ProtoBuf) {
output.Write(u[:])
}
func (u *ID) Deserialize(input *tygo.ProtoBuf) (err error) {
_, err = input.Read(u[:])
return
}
type UUIdentity int
var UUIdent UUIdentity = 0
func (_ UUIdentity) New() ruid.ID {
return ID(NewV1())
}
func (_ UUIdentity) Zero() ruid.ID {
return ZERO
}
func (_ UUIdentity) Deserialize(input *tygo.ProtoBuf) (ruid.ID, error) {
u := ID{}
_, err := input.Read(u[:])
return u, err
}
func (_ UUIdentity) GetIDs(bytes []byte) (ids []ruid.ID) {
for i, n := 0, len(bytes)-16; i <= n; i += 16 {
u := ID{}
copy(u[:], bytes[i:])
ids = append(ids, u)
}
return
}