# `ueid-ruby` Ruby implementation of Universal Entity IDs (UEIDs) for universal, machine-identifiable record identifiers. ## Explanation UEIDs are designed along the same principle as UXIDs, which are human-friendly identifiers that enable easy identification of an entity's type from its ID. However, UXIDs are optimized for human readability, and are typically stored as strings. In contrast, UEIDs are optimized for machine processing and storage efficiency, making them suitable for use in databases and systems where compactness and speed are critical. UEIDs are an implementation of the [UUID Version 8](https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-01.html#section-5.8) standard, combining the timestamp-ordering of UUID version 7, a custom field identifying the entity type, a sequential component for intra-millisecond uniqueness, and a random component for global uniqueness. They can be stored using the native UUID data types of modern databases, such as PostgreSQL's `uuid` type, allowing for more efficient storage and indexing compared to string-based identifiers. ```plaintext 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unix_ts_ms | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unix_ts_ms | ver | model | count | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |var| random | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | random | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ``` ### Fields #### `unix_ts_ms` (48 bits) A 48 bit big-endian unsigned integer of the Unix epoch timestamp in milliseconds, according to the [Timestamp Considerations of the UUID specification](https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-01.html#name-timestamp-considerations). This is identical to the timestamp field of UUIDv7. #### `ver` (4 bits) The 4 bit UUIDv8 version as per the [Version Field format of the UUID specification](https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-01.html#name-version-field). #### `model` (8 bits) An 8 bit unsigned integer uniquely identifying the entity type or model. This may be defined within the model itself, or calculated via a hash function or lookup table. #### `count` (4 bits) A 4 bit unsigned integer counter that increments with each UEID generated within the same millisecond timestamp and for the same model. This guarantees at least 16 unique UEIDs can be generated per model per millisecond. #### `var` (2 bits) The 2 bit UUID variant as per the [Variant Field format of the UUID specification](https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-01.html#name-variant-field). #### `random` (62 bits) The final 62 bit of pseudo-random data to provide uniqueness as per the [Unguessability best practices of the UUID specification](https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-01.html#name-unguessability). ### Limitations - Model identifiers are only unique within a single application or system. Different applications may use the same model identifiers for different entity types. - The 8 bit `model` field allows for a maximum of 256 unique entity types within a single application. - UEIDs can guarantee a maximum of 16 unique IDs per model per millisecond. Applications requiring higher throughput may need to implement additional mechanisms to guarantee uniqueness. - Only 62 bits of randomness are provided, compared to the 74 bits in UUIDv7 and 122 bits in UUIDv4. While this is generally sufficient for most applications, it may not be suitable for scenarios requiring extremely high levels of uniqueness or security. ## Usage This gem provides modules and classes to generate and parse UEIDs in Ruby, using only the Ruby standard library with no external dependencies. It aims to be compatible with Ruby version 3.2 or later, and any Ruby database library that supports UUID, binary, string, or bytea data types, such as ActiveRecord, Sequel, or ROM. ### Installation Add this line to your application's `Gemfile`: ```ruby gem 'ueid' ``` or install it yourself with: ```bash gem install ueid # or bundle add ueid ``` ### Examples ```ruby require 'ueid' class MyModel; end # Generate a UEID for a specific model class... UEID.generate(MyModel) # ...or for a specific object object = MyModel.new() UEID.generate(object.class) ``` #### ActiveRecord Example After configuring your application to use [UUIDs as primary keys](https://guides.rubyonrails.org/v8.0/active_record_migrations.html#using-uuids-instead-of-ids-for-primary-keys), you can configure your base `ApplicationRecord` class to assign UEID primary keys to new records: ```ruby class ApplicationRecord < ActiveRecord::Base # ... before_create :assign_ueid # ... private def assign_ueid self.id ||= UEID.generate(self.class) end end ``` ## Development ### Requirements - Ruby 3.2 or later - (optional) [mise](https://mise.jdx.dev) ### Setup ```bash git clone https://tangled.org/did:plc:hfjjkmleodmclbfae27m3ngt/ueid-ruby.git cd ueid-ruby # If you use mise and don't have ruby installed system-wide: mise trust mise install bin/setup ``` ### Testing ```bash bundle exec rake test ``` ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct Everyone interacting with the UEID project through code contributions and all communication channels must abide by the [code of conduct](CODE_OF_CONDUCT.md). Violations of the code of conduct may result in banishment from the project at the discretion of the project maintainers.