snowflaked 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b821e9cbccec942c789a9d8b0afb547c1cba389283f46169623c4f185753bbd
4
- data.tar.gz: 763c5b9c22393698b115742b50f51fbbbc122810e8c6a2942ee14032171ecb5b
3
+ metadata.gz: 2d0d8f2af68a50e38cc1aa32c1adc0641fbcdb2d0f01d0bc6e942520da33089f
4
+ data.tar.gz: 47f1b23d9a38e2f60ab43f7059c8efd27b783012fbdc8f510ed220b221e3200d
5
5
  SHA512:
6
- metadata.gz: dc619f463e94136072da4ecc99532e33a21616b86bf32828fbe91ee24206e9bdcb64b384b425d631b6c386317d48f8edb490ca7a5c21de8cdd55fa65586ae486
7
- data.tar.gz: 932623948b2a6cffa8cced8864e4fdd7efc837fb3f1f06051e4634ccad74840cfc49720377cf4ac20cf6dce19fd1c83895138d57d8af28a5ecf5eabb40b8886c
6
+ metadata.gz: '0278267978cd6a55be0f569659f36ff841c874cbeb2e813cc7f88c1789cf6d8b7ef21886d0bffe1f3268b9c34e1dede2b2eea75ba77468760acbf956d069d57f'
7
+ data.tar.gz: 8f2dc54ef2896404ee4698644844ca2611ccb7b6339735ffeb0a7e2dc38854b1df73035c9e7f66a92202c6b5001760b814907303007db2c327dd32195b1d917f
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Snowflaked
2
2
 
3
+ [![CI](https://github.com/luizkowalski/snowflaked/actions/workflows/ci.yml/badge.svg)](https://github.com/luizkowalski/snowflaked/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/snowflaked.svg)](https://badge.fury.io/rb/snowflaked)
5
+ [![Downloads](https://img.shields.io/gem/dt/snowflaked.svg)](https://rubygems.org/gems/snowflaked)
6
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
7
+
3
8
  A high-performance, thread-safe Snowflake ID generator for Ruby, powered by Rust.
4
9
 
5
10
  Snowflake IDs are 64-bit unique identifiers that encode a timestamp, machine ID, and sequence number. They're time-sortable (IDs created later are always larger), making them ideal for distributed systems where you need unique IDs without coordination between machines. Unlike UUIDs, Snowflake IDs are smaller, sortable, and index-friendly for databases.
@@ -38,16 +43,22 @@ class CreateUsers < ActiveRecord::Migration[8.1]
38
43
  def change
39
44
  create_table :users do |t|
40
45
  t.snowflake :external_id
46
+ t.bigint :uid
41
47
  end
42
48
  end
43
49
  end
44
50
  ```
45
51
 
52
+ Columns created with `t.snowflake` are automatically detected and will have Snowflake IDs generated for them.
53
+
54
+ > [!WARNING]
55
+ > SQLite does not support column comments, which Snowflaked uses to auto-detect snowflake columns other than `:id`. When using SQLite, you must explicitly declare snowflake columns using the `snowflake_id` helper in your model.
56
+
46
57
  If you want to generate Snowflake IDs for additional columns, you can do so by using the `snowflake_id` method, without having to migrate the table:
47
58
 
48
59
  ```ruby
49
60
  class User < ApplicationRecord
50
- snowflake_id :external_id
61
+ snowflake_id :uid
51
62
  end
52
63
  ```
53
64
 
@@ -72,10 +83,17 @@ end
72
83
  ```ruby
73
84
  Snowflaked.configure do |config|
74
85
  config.machine_id = 42
75
- config.epoch = Time.utc(2020, 1, 1)
86
+ config.epoch = Time.utc(1989, 1, 3) # When not configured, the epoch is set to the Unix epoch (January 1, 1970)
76
87
  end
77
88
  ```
78
89
 
90
+ ### Machine ID
91
+
92
+ > [!TIP]
93
+ > For multi-process servers like Puma, it is recommended to **not** configure `machine_id` explicitly. The gem automatically calculates a unique machine ID using `(hostname.hash ^ pid) % 1024`, which ensures each forked worker process gets a different ID and avoids duplicate Snowflake IDs.
94
+
95
+ If you must set `machine_id` explicitly, use environment variables that differ per worker process.
96
+
79
97
  ### Machine ID Resolution
80
98
 
81
99
  If `machine_id` is not explicitly configured, it resolves in this order:
@@ -140,6 +158,10 @@ bundle install
140
158
  bundle exec rake
141
159
  ```
142
160
 
161
+ ## Acknowledgments
162
+
163
+ - [snowflaked-rs](https://github.com/MrGunflame/snowflaked-rs) - the Rust implementation of Snowflake IDs
164
+
143
165
  ## License
144
166
 
145
167
  MIT
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "snowflaked"
3
- version = "0.1.0"
3
+ version = "0.1.3"
4
4
  edition = "2021"
5
5
  publish = false
6
6
 
@@ -1,42 +1,65 @@
1
1
  use magnus::{function, prelude::*, Error, RHash, Ruby};
2
2
  use snowflaked::sync::Generator;
3
- use snowflaked::Snowflake;
4
- use std::sync::OnceLock;
3
+ use snowflaked::{Builder, Snowflake};
4
+ use std::sync::RwLock;
5
+ use std::time::UNIX_EPOCH;
5
6
 
6
7
  struct GeneratorState {
7
8
  generator: Generator,
8
9
  epoch_offset: u64,
9
10
  machine_id: u16,
11
+ init_pid: u32,
10
12
  }
11
13
 
12
- static STATE: OnceLock<GeneratorState> = OnceLock::new();
14
+ static STATE: RwLock<Option<GeneratorState>> = RwLock::new(None);
13
15
 
14
16
  fn init_generator(machine_id: u16, epoch_ms: Option<u64>) -> bool {
15
- let was_empty = STATE.get().is_none();
17
+ let current_pid = std::process::id();
18
+ let epoch_offset = epoch_ms.unwrap_or(0);
16
19
 
17
- STATE.get_or_init(|| GeneratorState {
18
- generator: Generator::new(machine_id),
19
- epoch_offset: epoch_ms.unwrap_or(0),
20
+ let mut state = STATE.write().unwrap();
21
+
22
+ if state.as_ref().is_some_and(|s| s.init_pid == current_pid) {
23
+ return false;
24
+ }
25
+
26
+ let epoch = UNIX_EPOCH + std::time::Duration::from_millis(epoch_offset);
27
+ let generator = Builder::new().instance(machine_id).epoch(epoch).build();
28
+
29
+ *state = Some(GeneratorState {
30
+ generator,
31
+ epoch_offset,
20
32
  machine_id,
33
+ init_pid: current_pid,
21
34
  });
22
35
 
23
- was_empty
36
+ true
24
37
  }
25
38
 
26
39
  fn generate(ruby: &Ruby) -> Result<u64, Error> {
27
- let state = STATE.get().ok_or_else(|| {
40
+ let state = STATE.read().unwrap();
41
+
42
+ let s = state.as_ref().ok_or_else(|| {
28
43
  Error::new(
29
44
  ruby.exception_runtime_error(),
30
45
  "Generator not initialized. Call Snowflaked.configure or Snowflaked.id first.",
31
46
  )
32
47
  })?;
33
48
 
34
- Ok(state.generator.generate())
49
+ if s.init_pid != std::process::id() {
50
+ return Err(Error::new(
51
+ ruby.exception_runtime_error(),
52
+ "Fork detected: generator was initialized in a different process. This should not happen if using Snowflaked.id - please report this bug.",
53
+ ));
54
+ }
55
+
56
+ Ok(s.generator.generate())
35
57
  }
36
58
 
37
59
  fn timestamp_ms(id: u64) -> u64 {
38
60
  let timestamp_raw = id.timestamp();
39
- let epoch_offset = STATE.get().map(|s| s.epoch_offset).unwrap_or(0);
61
+ let state = STATE.read().unwrap();
62
+ let epoch_offset = state.as_ref().map(|s| s.epoch_offset).unwrap_or(0);
40
63
  timestamp_raw.saturating_add(epoch_offset)
41
64
  }
42
65
 
@@ -59,11 +82,13 @@ fn sequence(id: u64) -> u64 {
59
82
  }
60
83
 
61
84
  fn is_initialized() -> bool {
62
- STATE.get().is_some()
85
+ let state = STATE.read().unwrap();
86
+ state.as_ref().is_some_and(|s| s.init_pid == std::process::id())
63
87
  }
64
88
 
65
89
  fn configured_machine_id() -> Option<u16> {
66
- STATE.get().map(|s| s.machine_id)
90
+ let state = STATE.read().unwrap();
91
+ state.as_ref().and_then(|s| if s.init_pid == std::process::id() { Some(s.machine_id) } else { None })
67
92
  }
68
93
 
69
94
  #[magnus::init]
@@ -5,7 +5,7 @@ module Snowflaked
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- class_attribute :_snowflake_attributes, instance_writer: false, default: [:id] # rubocop:disable ThreadSafety/ClassAndModuleAttributes -- false positive
8
+ class_attribute :_snowflake_attributes, instance_writer: false, default: [:id]
9
9
  before_validation :_generate_snowflake_ids, on: :create
10
10
  end
11
11
 
@@ -17,9 +17,13 @@ module Snowflaked
17
17
  end
18
18
 
19
19
  def _snowflake_columns_from_comments
20
- return [] unless table_exists?
20
+ return @_snowflake_columns_from_comments if defined?(@_snowflake_columns_from_comments)
21
21
 
22
- columns.select { |c| c.comment == Snowflaked::SchemaDefinitions::COMMENT }.map { |c| c.name.to_sym }
22
+ @_snowflake_columns_from_comments = if table_exists?
23
+ columns.filter_map { |col| col.name.to_sym if col.comment == Snowflaked::SchemaDefinitions::COMMENT }
24
+ else
25
+ []
26
+ end
23
27
  end
24
28
  end
25
29
 
@@ -4,18 +4,13 @@ module Snowflaked
4
4
  module SchemaDefinitions
5
5
  COMMENT = "snowflaked"
6
6
 
7
- module TableDefinition
8
- def snowflake(name, **options)
9
- options[:comment] = Snowflaked::SchemaDefinitions::COMMENT
10
- column(name, :snowflake, **options)
7
+ module SnowflakeColumn
8
+ def snowflake(name, **)
9
+ column(name, :snowflake, comment: COMMENT, **)
11
10
  end
12
11
  end
13
12
 
14
- module Table
15
- def snowflake(name, **options)
16
- options[:comment] = Snowflaked::SchemaDefinitions::COMMENT
17
- column(name, :snowflake, **options)
18
- end
19
- end
13
+ TableDefinition = SnowflakeColumn
14
+ Table = SnowflakeColumn
20
15
  end
21
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Snowflaked
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/snowflaked.rb CHANGED
@@ -57,7 +57,7 @@ module Snowflaked
57
57
 
58
58
  class << self
59
59
  def configuration
60
- @configuration ||= Configuration.new # rubocop:disable ThreadSafety/ClassInstanceVariable -- not changed after initialization
60
+ @configuration ||= Configuration.new
61
61
  end
62
62
 
63
63
  def configure
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snowflaked
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Eduardo Kowalski