Skip to content

Commit

Permalink
Optionally rely on cl-trivial-clock for more accurate timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
ak-coram committed Jul 26, 2023
1 parent 8be3d00 commit 0f1d87d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ function.
The above system is provided to conveniently setup the following:

- Use Ironclad PRNG to generate [[https://github.com/ak-coram/cl-frugal-uuid/blob/main/non-frugal/strong-random.lisp][strong random numbers]].
- Use [[https://github.com/ak-coram/cl-trivial-clock][cl-trivial-clock]] to generate more accurate timestamps by relying
on platform-specific functionality.
- Automatically use a new PRNG and randomize node ID & clock sequence
for generating version 1 UUIDs for each new thread (via
[[https://github.com/ak-coram/cl-frugal-uuid/blob/main/non-frugal/thread-safe.lisp][bordeaux-threads]]).
Expand Down
9 changes: 5 additions & 4 deletions frugal-uuid-v1.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
(timestamp-generator :initarg :v1-timestamp-generator
:accessor v1-timestamp-generator)))

(defvar *v1-generator* nil)

(defun make-v1-generator (&key node-id clock-seq timestamp-generator)
(make-instance 'v1-generator
:v1-node-id (or node-id (random-node-id))
:v1-clock-seq (or clock-seq (random-clock-seq))
:v1-timestamp-generator (or timestamp-generator
(make-timestamp-generator))))

(defvar *v1-generator* nil)
(defvar *v1-generator-init-function* #'make-v1-generator)

(defun initialize-v1-generator (&optional v1-generator)
(setf *v1-generator* (or v1-generator (make-v1-generator)))
(setf *v1-generator* (or v1-generator
(funcall *v1-generator-init-function*)))
nil)

(defmacro with-v1-generator (v1-generator &body body)
Expand Down Expand Up @@ -54,4 +56,3 @@
"Generate uuid value (version 1)."
(unless *v1-generator* (initialize-v1-generator))
(make-v1-from-timestamp (funcall (v1-timestamp-generator *v1-generator*))))

8 changes: 7 additions & 1 deletion frugal-uuid.asd
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@
#:ironclad/digest/sha1)
:components ((:file "non-frugal/name-based")))

(asdf:defsystem #:frugal-uuid/non-frugal/accurate-clock
:depends-on (#:frugal-uuid
#:trivial-clock)
:components ((:file "non-frugal/accurate-clock")))

(asdf:defsystem #:frugal-uuid/non-frugal
:depends-on (#:frugal-uuid/non-frugal/strong-random
#:frugal-uuid/non-frugal/thread-safe
#:frugal-uuid/non-frugal/name-based))
#:frugal-uuid/non-frugal/name-based
#:frugal-uuid/non-frugal/accurate-clock))

(asdf:defsystem #:frugal-uuid/benchmark
:depends-on (#:frugal-uuid
Expand Down
18 changes: 18 additions & 0 deletions non-frugal/accurate-clock.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
;;;; non-frugal/accurate-clock.lisp

(in-package #:frugal-uuid)

(defconstant +unix-epoch-offset-seconds+ 12219292800)

(defun get-accurate-timestamp ()
(multiple-value-bind (seconds nanos) (trivial-clock:now)
(+ (* (+ seconds +unix-epoch-offset-seconds+) 10000000)
(floor nanos 100))))

(defun make-accurate-v1-generator (&key node-id clock-seq)
(make-v1-generator :node-id node-id
:clock-seq clock-seq
:timestamp-generator #'get-accurate-timestamp))

(setf *v1-generator* nil
*v1-generator-init-function* #'make-accurate-v1-generator)
2 changes: 2 additions & 0 deletions package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@

;; Version 1
#:*v1-generator*
#:*v1-generator-init-function*
#:make-v1-generator
#:make-accurate-v1-generator
#:initialize-v1-generator
#:with-v1-generator
#:make-v1-from-timestamp
Expand Down

0 comments on commit 0f1d87d

Please sign in to comment.