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 +4 -4
- data/README.md +24 -2
- data/ext/snowflaked/Cargo.toml +1 -1
- data/ext/snowflaked/src/lib.rs +38 -13
- data/lib/snowflaked/model_extensions.rb +7 -3
- data/lib/snowflaked/schema_definitions.rb +5 -10
- data/lib/snowflaked/version.rb +1 -1
- data/lib/snowflaked.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2d0d8f2af68a50e38cc1aa32c1adc0641fbcdb2d0f01d0bc6e942520da33089f
|
|
4
|
+
data.tar.gz: 47f1b23d9a38e2f60ab43f7059c8efd27b783012fbdc8f510ed220b221e3200d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0278267978cd6a55be0f569659f36ff841c874cbeb2e813cc7f88c1789cf6d8b7ef21886d0bffe1f3268b9c34e1dede2b2eea75ba77468760acbf956d069d57f'
|
|
7
|
+
data.tar.gz: 8f2dc54ef2896404ee4698644844ca2611ccb7b6339735ffeb0a7e2dc38854b1df73035c9e7f66a92202c6b5001760b814907303007db2c327dd32195b1d917f
|
data/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Snowflaked
|
|
2
2
|
|
|
3
|
+
[](https://github.com/luizkowalski/snowflaked/actions/workflows/ci.yml)
|
|
4
|
+
[](https://badge.fury.io/rb/snowflaked)
|
|
5
|
+
[](https://rubygems.org/gems/snowflaked)
|
|
6
|
+
[](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 :
|
|
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(
|
|
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
|
data/ext/snowflaked/Cargo.toml
CHANGED
data/ext/snowflaked/src/lib.rs
CHANGED
|
@@ -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::
|
|
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:
|
|
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
|
|
17
|
+
let current_pid = std::process::id();
|
|
18
|
+
let epoch_offset = epoch_ms.unwrap_or(0);
|
|
16
19
|
|
|
17
|
-
STATE.
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
36
|
+
true
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
fn generate(ruby: &Ruby) -> Result<u64, Error> {
|
|
27
|
-
let state = STATE.
|
|
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
|
-
|
|
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
|
|
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.
|
|
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.
|
|
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]
|
|
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
|
|
20
|
+
return @_snowflake_columns_from_comments if defined?(@_snowflake_columns_from_comments)
|
|
21
21
|
|
|
22
|
-
|
|
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
|
|
8
|
-
def snowflake(name, **
|
|
9
|
-
|
|
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
|
-
|
|
15
|
-
|
|
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
|
data/lib/snowflaked/version.rb
CHANGED
data/lib/snowflaked.rb
CHANGED