@@ -27,7 +27,7 @@ unique ID (UUID):
27
27
Oftentimes when building systems, you want to assign unique IDs to
28
28
objects and records. IDs are usually simple integers that
29
29
monotonically increase with time. This isn't without its problems,
30
- though.
30
+ though.
31
31
32
32
You can't mingle IDs of objects from different origins; and
33
33
worse, they reveal information about the amount and input volume of
@@ -43,7 +43,7 @@ You may have noticed Clojure prints UUIDs with a +#uuid+ in front of
43
43
them. This is a reader literal tag. It acts as a shortcut for the
44
44
Clojure reader to read and initialize UUID objects. Reader literals
45
45
are a lot like string or number literals like +"Hi"+ or +42+, but they
46
- can capture more complex data types.
46
+ can capture more complex data types.
47
47
48
48
This makes it possible for
49
49
formats like https://github.com/edn-format/edn[edn] (extensible data notation) to communicate in
@@ -58,6 +58,19 @@ the implicit sortability of a chronologically increasing number. What
58
58
if you could generate UUIDs that were both unique _and_ sortable?
59
59
Datomic does something similar with its +datomic.api/squuid+ function.(((Datomic database, UUID generation)))((("functions", "datomic.api/squuid")))
60
60
61
+ [source,clojure]
62
+ ----
63
+ (defn squuid []
64
+ (let [uuid (java.util.UUID/randomUUID)
65
+ time (System/currentTimeMillis)
66
+ secs (quot time 1000)
67
+ lsb (.getLeastSignificantBits uuid)
68
+ msb (.getMostSignificantBits uuid)
69
+ timed-msb (bit-or (bit-shift-left secs 32)
70
+ (bit-and 0x00000000ffffffff msb))]
71
+ (java.util.UUID. timed-msb lsb)))
72
+ ----
73
+
61
74
This approximation of Datomic's +squuid+ splits and reassembles a(((range="endofrange", startref="ix_PDnumer")))
62
75
random UUID, using +bit-or+ to merge the current time with the most
63
76
significant 32 bits of the UUID. The two halves of the UUID
0 commit comments