-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.go
34 lines (25 loc) · 888 Bytes
/
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
package combuuid
import (
"time"
"github.com/google/uuid"
)
const BLOCK_SIZE = 65536
const DEFAULT_PERIOD_SEC = 60
/*
NewUuid wraps (and returns) a "github.com/google/uuid.UUID" encoded using the
COMB optimization for UUIDs as primary keys in relational databases as shown in
https://www.2ndquadrant.com/en/blog/sequential-uuid-generators/
These Uuids sacrifice small amounts of collision resistance for improved write
and read performance for databases. They are almost certainly good enough for
"application"-level uniqueness, if not "universal"-level.
*/
func NewUuid() uuid.UUID {
return newUuid(time.Now())
}
func newUuid(t time.Time) uuid.UUID {
id := uuid.New() // id = 0x??, 0x??, ...
prefix := uint16((t.Unix() / DEFAULT_PERIOD_SEC) % BLOCK_SIZE) // say, 0xAAFF
id[0] = byte(prefix >> 8) // id = 0xAA 0x??
id[1] = byte(prefix) // id = 0xAA 0xFF
return id
}