-
Notifications
You must be signed in to change notification settings - Fork 3
/
frugal-uuid-v6.lisp
77 lines (60 loc) · 2.43 KB
/
frugal-uuid-v6.lisp
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
;;;; frugal-uuid-v6.lisp
(in-package #:frugal-uuid)
(declaim (ftype (function (uuid) (values uuid &optional))
make-v6-from-v1))
(defun make-v6-from-v1 (v1)
(let ((v1-time-high-and-version (time-hi-and-version v1))
(v1-time-mid (time-mid v1))
(v1-time-low (time-low v1))
(v6-time-high #x00000000)
(v6-time-mid #x0000)
(v6-time-low-and-version #x0000))
;; Rearrange the timestamp
(setf
;; Put the high 32 bits of the timestamp in v6-time-high:
(ldb (byte 12 20) v6-time-high) (ldb (byte 12 0)
v1-time-high-and-version)
(ldb (byte 16 4) v6-time-high) v1-time-mid
(ldb (byte 4 0) v6-time-high) (ldb (byte 4 28) v1-time-low)
;; Put the high 16 bits of v1-time-low into v6-time-mid:
(ldb (byte 16 0) v6-time-mid) (ldb (byte 16 12) v1-time-low)
;; Put the lowest bits of the timestamp in
;; v6-time-low-and-version and set the version to version 6
(ldb (byte 12 0) v6-time-low-and-version) (ldb (byte 12 0) v1-time-low)
(ldb (byte 4 12) v6-time-low-and-version) #x6)
(make-instance 'uuid
:time-low v6-time-high
:time-mid v6-time-mid
:time-hi-and-version v6-time-low-and-version
:clock-seq-hi-and-res (clock-seq-hi-and-res v1)
:clock-seq-low (clock-seq-hi-and-res v1)
:node (node v1))))
(declaim (ftype (function (uuid) (values (unsigned-byte 32) &optional))
v6-time-high))
(defun v6-time-high (uuid)
(time-low uuid))
(declaim (ftype (function (uuid) (values (unsigned-byte 16) &optional))
v6-time-mid
v6-time-low-and-version))
(defun v6-time-mid (uuid)
(time-mid uuid))
(defun v6-time-low-and-version (uuid)
(time-hi-and-version uuid))
(declaim (ftype (function () (values uuid &optional)) make-v6))
(defun make-v6 ()
"Generate uuid value (version 6).
Implementation is based on generating a version 1 uuid value and
reordering the timestamp. See frugal-uuid-v1.lisp for details."
(make-v6-from-v1 (make-v1)))
(declaim (inline make-v6-integer))
(defun make-v6-integer ()
(to-integer (make-v6)))
(declaim (inline make-v6-string))
(defun make-v6-string ()
(to-string (make-v6)))
(declaim (inline make-v6-octets))
(defun make-v6-octets ()
(to-octets (make-v6)))
(declaim (inline make-v6-sym))
(defun make-v6-sym ()
(to-sym (make-v6)))