diff --git a/README.md b/README.md index 1eee7ec..9f2a540 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,157 @@ -# Ueid +# `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 | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +``` -TODO: Delete this and the text below, and describe your gem +### Fields -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ueid`. To experiment with that code, run `bin/console` for an interactive prompt. +#### `unix_ts_ms` (48 bits) -## Installation +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. -TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. +#### `ver` (4 bits) -Install the gem and add to the application's Gemfile by executing: +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). -```bash -bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG +#### `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. + +## Implementation + +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' ``` -If bundler is not being used to manage dependencies, install the gem by executing: +or install it yourself with: ```bash -gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG +gem install ueid +# or +bundle add ueid ``` -## Usage +### Usage -TODO: Write usage instructions here +```ruby +require 'ueid' -## Development +class MyModel; end -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +# Generate a new UEID for a specific object +object = MyModel.new() +ueid = UEID.generate(object) +``` + +#### 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: -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). +```ruby +class ApplicationRecord < ActiveRecord::Base + # ... -## Contributing + before_create :assign_ueid + + # ... -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ueid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ueid/blob/trunk/CODE_OF_CONDUCT.md). + private + + def assign_ueid + self.id = UEID.generate(self) if self.id.nil? + end +end +``` ## License -The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). +The gem is available as open source under the terms of the [MIT +License](https://opensource.org/licenses/MIT). ## Code of Conduct -Everyone interacting in the Ueid project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ueid/blob/trunk/CODE_OF_CONDUCT.md). +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.