-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclock.go
172 lines (150 loc) · 3.62 KB
/
clock.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
import "time"
import (
"encoding/binary"
"encoding/hex"
)
// hybrid calendar/logical clock
type Clock struct {
offset time.Duration
lastSeen UUID
Mode UUID
MinLength int
}
var MAX_BIT_GRAB uint64 = 1 << 20
func NewClock(replica uint64, mode UUID, minLen int) Clock {
origin := (replica & INT60_FULL) | (uint64(UUID_EVENT) << 60)
return Clock{lastSeen: NewEventUUID(0, origin), Mode: mode, MinLength: minLen}
}
func EncodeCalendar(t time.Time) (i uint64) {
months := (t.Year()-2010)*12 + int(t.Month()) - 1
i |= uint64(months)
days := t.Day() - 1
i <<= 6
i |= uint64(days)
hours := t.Hour()
i <<= 6
i |= uint64(hours)
minutes := t.Minute()
i <<= 6
i |= uint64(minutes)
seconds := t.Second()
i <<= 6
i |= uint64(seconds)
micros := t.Nanosecond() / 100
i <<= 24
i |= uint64(micros)
return i
}
func CalendarToRFC(uuid UUID) (u [16]byte) {
// the formula comes from satori/go.uuid
time := DecodeCalendar(uuid.Value())
timeRfc := uint64(122192928000000000) + uint64(time.UnixNano()/100)
binary.BigEndian.PutUint32(u[0:], uint32(timeRfc))
binary.BigEndian.PutUint16(u[4:], uint16(timeRfc>>32))
binary.BigEndian.PutUint16(u[6:], uint16(timeRfc>>48))
binary.BigEndian.PutUint16(u[8:], 0)
var replicaId [6]byte
orig := uuid.Origin()
orig >>= 60 - 6*8
for i := 0; i < 6; i++ {
replicaId[5-i] = byte(orig & 255)
orig >>= 8
}
copy(u[10:], replicaId[:])
var version byte = 1
u[6] = (u[6] & 0x0f) | (version << 4)
u[8] = (u[8] & 0xbf) | 0x80
return u
}
func CalendarToRFCString(uuid UUID) string {
u := CalendarToRFC(uuid)
buf := make([]byte, 36)
hex.Encode(buf[0:8], u[0:4])
buf[8] = '-'
hex.Encode(buf[9:13], u[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], u[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], u[8:10])
buf[23] = '-'
hex.Encode(buf[24:], u[10:])
return string(buf)
}
func trimTime(full, last uint64, i int) uint64 {
for i < 11 && full&PREFIX_MASKS[i] <= last {
i++
}
return full & PREFIX_MASKS[i]
}
func (clock *Clock) Time() UUID {
var val uint64
last := clock.lastSeen.Value()
switch clock.Mode {
case CLOCK_CALENDAR:
t := time.Now().Add(clock.offset).UTC()
val = EncodeCalendar(t)
case CLOCK_LAMPORT:
val = last + 1
case CLOCK_EPOCH:
t := time.Now().Add(clock.offset).UTC()
val = uint64(t.Unix()) << (4 * 6) // TODO define
}
if val <= last {
val = last + 1
} else {
val = trimTime(val, last, clock.MinLength)
}
ret := NewEventUUID(val, clock.lastSeen.Origin())
clock.See(ret)
return ret
}
func (clock *Clock) Now() time.Time {
return time.Now().Add(clock.offset).UTC()
}
// ...
func (clock *Clock) See(uuid UUID) bool {
if !clock.IsSane(uuid) {
return false
}
if clock.lastSeen.Value() < uuid.Value() {
clock.lastSeen = NewEventUUID(uuid.Value(), clock.lastSeen.Origin())
}
return true
}
func (clock Clock) IsSane(uuid UUID) bool {
switch clock.Mode {
case CLOCK_LAMPORT:
return clock.lastSeen.Value()+MAX_BIT_GRAB > uuid.Value()
case CLOCK_CALENDAR:
return DecodeCalendar(uuid.Value()).Before(clock.Now())
default:
return true
}
}
func (clock Clock) Decode(uuid UUID) time.Time {
switch clock.Mode {
case CLOCK_CALENDAR:
return DecodeCalendar(uuid.Value())
default:
return time.Time{}
}
}
const MASK24 uint64 = 16777215
func DecodeCalendar(v uint64) time.Time {
var ns100 int = int(v & MASK24)
v >>= 24
var secs int = int(v & 63)
v >>= 6
var mins int = int(v & 63)
v >>= 6
var hours int = int(v & 63)
v >>= 6
var days int = int(v & 63)
v >>= 6
var months int = int(v & 4095)
var month = months % 12
var year = months / 12
t := time.Date(year+2010, time.Month(month+1), days+1, hours, mins, secs, ns100*100, time.UTC)
return t
}