-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuuid.go
172 lines (142 loc) · 3.27 KB
/
uuid.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package ron
func (uuid UUID) Value() uint64 {
return uuid[0] & INT60_FULL
}
func (uuid UUID) Origin() uint64 {
return uuid[1] & INT60_FULL
}
func Compare(a, b UUID) int {
diff := int64(a.Value()) - int64(b.Value())
if diff == 0 {
diff = int64(a.Origin()) - int64(b.Origin())
}
if diff < 0 {
return -1
} else if diff > 0 {
return 1
} else {
return 0
}
}
func (t UUID) Equal(b UUID) bool {
return t == b
}
func (uuid UUID) IsTranscendentName() bool {
return uuid[1] == UUID_NAME_UPPER_BITS
}
func (uuid UUID) IsName() bool {
return uuid.Scheme() == UUID_NAME
}
func (a UUID) Compare(b UUID) int64 {
c := int64(a[0]) - int64(b[0])
if c == 0 {
c = int64(a[1]) - int64(b[1])
}
return c
}
func (a UUID) LaterThan(b UUID) bool {
if a.Value() == b.Value() {
return a.Origin() > b.Origin()
} else {
return a.Value() > b.Value()
}
}
func (a UUID) EarlierThan(b UUID) bool {
// FIXME define through Compare
if a.Value() == b.Value() {
return a.Origin() < b.Origin()
} else {
return a.Value() < b.Value()
}
}
func (a UUID) Scheme() uint64 {
return (a[1] >> 60) & 3
}
func (a UUID) Variety() uint {
return uint(a[0] >> 60)
}
func (a UUID) Sign() byte {
return UUID_PUNCT[uint(a.Scheme())]
}
func (a UUID) Replica() uint64 {
return a[1] & INT60_FULL
}
func (a UUID) SameAs(b UUID) bool {
if a.Value() != b.Value() {
return false
} else if a.Origin() == b.Origin() {
return true
} else if (a.Origin()^b.Origin())&INT60_FULL != 0 {
return false
} else {
return a.Origin()&INT60_FULL == b.Origin()&INT60_FULL
}
}
func (uuid UUID) Derived() UUID {
if uuid.Scheme() == UUID_EVENT {
return NewUUID(UUID_DERIVED, uuid.Value(), uuid.Origin())
} else {
return uuid
}
}
func NewRonUUID(scheme, variety uint, value, origin uint64) UUID {
return UUID{value | uint64(variety&15)<<60, origin | uint64(scheme&15)<<60}
}
func NewUUID(scheme uint, value, origin uint64) UUID {
return NewRonUUID(scheme, 0, value, origin)
}
func NewEventUUID(time, origin uint64) UUID {
return NewUUID(UUID_EVENT, time, origin)
}
func NewNameUUID(time, origin uint64) UUID {
return NewUUID(UUID_NAME, time, origin)
}
func NewHashUUID(time, origin uint64) UUID {
return NewUUID(UUID_HASH, time, origin)
}
// use for static strings only - panics on error
func NewName(name string) UUID {
nam, err := ParseUUIDString(name)
if err != nil {
panic("bad name")
}
return nam
}
func NewError(name string) UUID {
nam, err := ParseUUIDString(name)
if err != nil {
panic("bad error name")
}
return NewNameUUID(nam.Value(), INT60_ERROR)
}
func (uuid UUID) IsTemplate() bool {
return uuid.Sign() == UUID_NAME && uuid.Value() == 0 && uuid.Origin() != 0
}
func (uuid UUID) ToScheme(scheme uint) UUID {
return NewUUID(scheme, uuid.Value(), uuid.Origin())
}
func (uuid UUID) IsZero() bool {
return uuid.Value() == 0
}
func (uuid UUID) IsError() bool {
return uuid.Origin() == INT60_ERROR
}
func (uuid UUID) ZipString(context UUID) string {
var arr [INT60LEN*2 + 2]byte
return string(FormatZipUUID(arr[:0], uuid, context))
}
func (uuid UUID) String() (ret string) {
ret = uuid.ZipString(ZERO_UUID)
if len(ret) == 0 {
ret = "0"
}
return
}
func (uuid UUID) Error() string {
if uuid.IsError() {
return uuid.String()
} else {
return ""
}
}
var UUID_NAME_FLAG = ((uint64)(UUID_NAME)) << 60