-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
139 lines (119 loc) · 3.64 KB
/
common.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
/*
Copyright 2012 Dmitry Kolesnikov, All Rights Reserved
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package guid
import (
"time"
)
// zero point for drift
const driftZ = 18
// driftBits converts a time drift into number of bits to shift the location
// fraction. E.g. if application allows 2 min time drift in the system than last
// 20 bits of timestamp becomes less significant than location.
//
// The default drift is approximately 5 min, the drift value is encoded as
// 3 bits, which gives 7 possible values (zero drift not allowed)
func driftInBits(drift []time.Duration) uint64 {
switch {
case len(drift) == 0:
return driftZ + 3
// NOTE: allow only 1 - 7 values for drift
// case drift[0] <= 34*time.Second:
// return driftZ
case drift[0] <= 68*time.Second:
return driftZ + 1
case drift[0] <= 137*time.Second:
return driftZ + 2
case drift[0] <= 274*time.Second:
return driftZ + 3
case drift[0] <= 549*time.Second:
return driftZ + 4
case drift[0] <= 1099*time.Second:
return driftZ + 5
case drift[0] <= 2199*time.Second:
return driftZ + 6
default:
return driftZ + 7
}
}
// splits ⟨𝒕⟩ faction (timestamp) to hi and lo bits of K order value
func splitT(t uint64, drift uint64) (uint64, uint64) {
//
// 3 47 - drift 32bit drift 14
// |-|-------------------|--------!-------|-----|-------|
// ^ b ^ a ^
// 96 64 0
//
// 14 bits of time is exchange for seq
// 3 bits is reserved for drift
// initial timestamp is reduced by 17 bits ~ 10⁶ nanoseconds
x := t >> (14 + 3)
a := 64 - 14 - drift
b := 32 - a
lo := (x << (a + 14)) >> a
hi := (x >> drift) << b
dd := (drift - driftZ) << 29
return hi | dd, lo
}
// split ⟨𝒍⟩ faction (location) to hi and lo bits of K order value
func splitNode(node, drift uint64) (uint64, uint64) {
//
// 3 47 - drift 32bit drift 14
// |-|-------------------|--------!-------|-----|-------|
// ^ b ^ a ^
// 96 64 0
//
a := 64 - 14 - drift
b := 32 - a
lo := node << (drift + 14)
hi := node >> (32 - b)
return hi, lo
}
func split(hi, lo, size, n uint64, bytes []byte) {
hilo := uint64(64) // hi | lo division at
mask := uint64(1<<n) - 1
i := 0
for a := size; a >= n; a -= n {
b := a - n
switch {
case a >= hilo && b >= hilo:
bytes[i] = byte(hi >> (b - hilo) & mask)
case a <= hilo && b <= hilo:
bytes[i] = byte(lo >> b & mask)
case a > hilo && b < hilo:
suffix := uint64(1<<(a-hilo)) - 1
hi := byte(hi & suffix)
lo := byte(lo >> b)
bytes[i] = hi<<(hilo-b) | lo
}
i++
}
}
func fold(size, n uint64, bytes []byte) (hi, lo uint64) {
hilo := uint64(64)
mask := uint64(1<<n) - 1
i := 0
for a := size; a >= n; a -= n {
b := a - n
switch {
case a >= hilo && b >= hilo:
hi |= (uint64(bytes[i]) & mask) << (b - hilo)
case a <= hilo && b <= hilo:
lo |= (uint64(bytes[i]) & mask) << b
case a > hilo && b < hilo:
hi |= (uint64(bytes[i]) & mask) >> (hilo - b)
lo |= (uint64(bytes[i]) & mask) << b
}
i++
}
return
}