Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the binary format also lexicographically sorted? #5

Open
druud opened this issue May 16, 2022 · 1 comment
Open

Is the binary format also lexicographically sorted? #5

druud opened this issue May 16, 2022 · 1 comment

Comments

@druud
Copy link

druud commented May 16, 2022

I haven't checked it yet, so am leaving it as a question here:

Is the binary format also lexicographically sorted?

As I rather store them as a BINARY(16) than a CHAR(26), but can only use them if the binary format also grows lexically over time.

See also:
https://en.wikipedia.org/wiki/Universally_unique_identifier#As_database_keys

@bbrtj
Copy link
Collaborator

bbrtj commented May 27, 2022

Hello @druud,

Binary format is encoded as follows on 64 bit: first six bytes hold the timestamp, where four bytes belong to a 32-bit number encoded big endian (upper 32 bits of the timestamp), and the following two bytes belong to a 16-bit number also encoded big endian (lower 16 bits of the timestamp).

On 32 bit perls, the timestamp is transformed into bytestring with the help of Math::BigInt::to_bytes method.

If the timestamp part is shorter than 6 bytes, it is left-padded with zero bytes. The rest of the bytestring is just random 10 bytes.

Additionally, textual ULID is just binary ULID encoded into base32, 5 bits at a time left to right - so it shares same characteristics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants