Last active
August 29, 2015 14:04
-
-
Save vonwenm/71f5f20d8ccb5be8a627 to your computer and use it in GitHub Desktop.
SQUUID - a sequential UUID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| A very handy tool for sequential UUIDS fro https://github.com/clojure-cookbook/clojure-cookbook/blob/master/01_primitive-data/1-24_uuids.asciidoc | |
| Here is the definition: | |
| (defn squuid [] | |
| (let [uuid (java.util.UUID/randomUUID) | |
| time (System/currentTimeMillis) | |
| secs (quot time 1000) | |
| lsb (.getLeastSignificantBits uuid) | |
| msb (.getMostSignificantBits uuid) | |
| timed-msb (bit-or (bit-shift-left secs 32) | |
| (bit-and 0x00000000ffffffff msb))] | |
| (java.util.UUID. timed-msb lsb))) | |
| Here is how to test it: | |
| (def u1 (squuid)) | |
| u1 | |
| ;; -> #uuid "527bf210-dfae-4c73-8b7a-302d3b511f41" | |
| (def u2 (squuid)) | |
| u2 | |
| ;; -> #uuid "527bf219-65f0-4241-a165-c5c541cb98ea" | |
| (def u3 (squuid)) | |
| u3 | |
| ;; -> #uuid "527bf232-42b2-44bc-8dd7-ddae2abfcb87" | |
| (sort [u1 u2 u3]) | |
| ;; -> (#uuid "527bf210-dfae-4c73-8b7a-302d3b511f41" | |
| ;; #uuid "527bf219-65f0-4241-a165-c5c541cb98ea" | |
| ;; #uuid "527bf232-42b2-44bc-8dd7-ddae2abfcb87") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment