rafaelsales / ulid Goto Github PK
View Code? Open in Web Editor NEWUniversally Unique Lexicographically Sortable Identifier implementation for Ruby
License: MIT License
Universally Unique Lexicographically Sortable Identifier implementation for Ruby
License: MIT License
This gem depends on sysrandom gem, but it is archived already.
Because Ruby 2.5+ tries to use /dev/urandom before openssl, we can rely on it and remove dependency to sysrandom if we drop support Ruby < 2.5. What do you think?
@rafaelsales any reason there isn't a built-in method to decode to bytes?
How to replace UUIDs with ULIDs in a Rails app that uses UUIDs as primary keys (in PostgreSQL)?
As I read the spec in this repo, the lexicographical sort ordering of ULIDs should respect the timestamp ordering to millisecond precision. However, this does not appear to always be the case:
require 'ulid'
times = Array.new(5) { |i| Time.new(2020, 9, 3, 13, 9, Rational(i, 10**3)) }
puts 'Times:'
puts times.map { |t| t.strftime('%F %T.%N') }
ulids = times.map { |t| ULID.generate(t) }
puts 'ULIDs:'
puts ulids
sorted = ulids.sort
puts 'Sorted:'
puts sorted
puts "Sort respects time? #{ulids == sorted}"
gives
Times:
2020-09-03 13:09:00.000000000
2020-09-03 13:09:00.001000000
2020-09-03 13:09:00.002000000
2020-09-03 13:09:00.003000000
2020-09-03 13:09:00.004000000
ULIDs:
01EH9XXEV0KCZV2TBC86EGT900
01EH9XXEV1NNSE20T5E9PDH4Q9
01EH9XXEV1HSVWPK0E776QR1ZX
01EH9XXEV3XYZH7SVCH8684MVV
01EH9XXEV4205AFCAVKAT9YH2V
Sorted:
01EH9XXEV0KCZV2TBC86EGT900
01EH9XXEV1HSVWPK0E776QR1ZX
01EH9XXEV1NNSE20T5E9PDH4Q9
01EH9XXEV3XYZH7SVCH8684MVV
01EH9XXEV4205AFCAVKAT9YH2V
Sort respects time? false
The problem being that (in this example run) that 01EH9XXEV1NNSE20T5E9PDH4Q9
sorts after 01EH9XXEV1HSVWPK0E776QR1ZX
, despite the timestamp used to generate the former being 1 millisecond before that of the latter. Looking at the timestamp portion of the two ULIDs, they are the same: 01EH9XXEV1
, which means both 2020-09-03 13:09:00.001
and 2020-09-03 13:09:00.002
map to the same ULID prefix.
How would a ULID be converted to a UUID for storage in PostgreSQL?
It has been brought to my attention that ULID has a (sort of) standard associated with it, originating in this javascript repo: https://github.com/alizain/ulid. I honestly thought you had invented it.... Whoops! So, my changes actually break compatibility with the javascript implementation and use an incorrect binary layout in the binary format.
I'm thinking it makes the most sense to roll back the changes and call it a new release... I don't think gems can be un-released, so that may be the only option.
Hello. I assumed that this meant that I could sort the ULID's - in other words, newer ID's should fall lexicographically after earlier ones:
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.
So:
Loading development environment (Rails 5.0.0.rc2)
irb(main):001:0> ULID.generate
=> "7S2SYCSA109VJ4Z2ZVB4PBRCDV"
irb(main):002:0> ULID.generate
=> "6K4SYCSA10EVAK6F5E32R1K50C"
irb(main):003:0> ULID.generate
=> "6J6SYCSA10186DZ41ZBV57PZFV"
irb(main):004:0> ULID.generate
=> "Z98SYCSA1036VDH0ANC31A9YNZ"
irb(main):005:0> ULID.generate
=> "EMASYCSA10NEYETVH1DQPPMKJM"
irb(main):006:0> ULID.generate
=> "8CAVYCSA104HS6BH2VBMBMK4TK"
irb(main):007:0> ULID.generate
=> "EYBVYCSA1078FH66T8TZKYV9EX"
irb(main):008:0> ULID.generate
=> "PWDVYCSA108QKE6F8W36TTDJX8"
irb(main):009:0> ULID.generate
=> "X2FVYCSA1031REQ1M1Y00GH33P"
irb(main):010:0> ULID.generate
=> "XDGVYCSA1088A9DZFK7A042GPX"
irb(main):011:0> ULID.generate
=> "XRHVYCSA10KXJ73TGCBCYPDWR7"
irb(main):012:0> ULID.generate
=> "6VJVYCSA100JB4D804HDBNPMVD"
You can see that those entries are not sortable (6VJVYCSA100JB4D804HDBNPMVD sorts before XRHVYCSA10KXJ73TGCBCYPDWR7, and if I'm wrong on that, then 8CAVYCSA104HS6BH2VBMBMK4TK is out of sequence).
Am I missing something?
renen.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.