Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cff0057

Browse files
author
Ryan Neufeld
committed
Re-add squuid example removed during indexing
1 parent b70220e commit cff0057

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

01_primitive-data/1-24_uuids.asciidoc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ unique ID (UUID):
2727
Oftentimes when building systems, you want to assign unique IDs to
2828
objects and records. IDs are usually simple integers that
2929
monotonically increase with time. This isn't without its problems,
30-
though.
30+
though.
3131

3232
You can't mingle IDs of objects from different origins; and
3333
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
4343
them. This is a reader literal tag. It acts as a shortcut for the
4444
Clojure reader to read and initialize UUID objects. Reader literals
4545
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.
4747

4848
This makes it possible for
4949
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
5858
if you could generate UUIDs that were both unique _and_ sortable?
5959
Datomic does something similar with its +datomic.api/squuid+ function.(((Datomic database, UUID generation)))((("functions", "datomic.api/squuid")))
6060
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+
6174
This approximation of Datomic's +squuid+ splits and reassembles a(((range="endofrange", startref="ix_PDnumer")))
6275
random UUID, using +bit-or+ to merge the current time with the most
6376
significant 32 bits of the UUID. The two halves of the UUID

0 commit comments

Comments
 (0)