snowflaked 0.1.1 → 0.1.2
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 +13 -2
- data/ext/snowflaked/Cargo.toml +1 -1
- data/ext/snowflaked/src/lib.rs +13 -5
- data/lib/snowflaked/model_extensions.rb +1 -1
- data/lib/snowflaked/schema_definitions.rb +5 -10
- data/lib/snowflaked/version.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: 2d467289c785dd03c1de80d82d2ad25657b43677affd2c3112090f42109f5ef5
|
|
4
|
+
data.tar.gz: 701ea640497cbf5555570ca8eb03c3d47cb88742d4b80b0a331fd72cdb345168
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f46bb795c7a598091deb5983e0ef3cec686dc515e129712258d67e4d7858a6e0ac07127275b60477a5d01170ebd11ff6c5fd9ebac195934c44935f675e03ff70
|
|
7
|
+
data.tar.gz: f3ac6a668615e9c4bbc0657c3bd360e1e5da63e982880bbdce91d2d97fa562d01547c7e8af3b3ba732aeb88a84e230ba09affd61f6e95244718161f4fe6cf999
|
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,7 +83,7 @@ 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
|
|
data/ext/snowflaked/Cargo.toml
CHANGED
data/ext/snowflaked/src/lib.rs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
use magnus::{function, prelude::*, Error, RHash, Ruby};
|
|
2
2
|
use snowflaked::sync::Generator;
|
|
3
|
-
use snowflaked::Snowflake;
|
|
3
|
+
use snowflaked::{Builder, Snowflake};
|
|
4
4
|
use std::sync::OnceLock;
|
|
5
|
+
use std::time::UNIX_EPOCH;
|
|
5
6
|
|
|
6
7
|
struct GeneratorState {
|
|
7
8
|
generator: Generator,
|
|
@@ -13,11 +14,18 @@ static STATE: OnceLock<GeneratorState> = OnceLock::new();
|
|
|
13
14
|
|
|
14
15
|
fn init_generator(machine_id: u16, epoch_ms: Option<u64>) -> bool {
|
|
15
16
|
let was_empty = STATE.get().is_none();
|
|
17
|
+
let epoch_offset = epoch_ms.unwrap_or(0);
|
|
16
18
|
|
|
17
|
-
STATE.get_or_init(||
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
machine_id
|
|
19
|
+
STATE.get_or_init(|| {
|
|
20
|
+
let epoch = UNIX_EPOCH + std::time::Duration::from_millis(epoch_offset);
|
|
21
|
+
|
|
22
|
+
let generator = Builder::new().instance(machine_id).epoch(epoch).build();
|
|
23
|
+
|
|
24
|
+
GeneratorState {
|
|
25
|
+
generator,
|
|
26
|
+
epoch_offset,
|
|
27
|
+
machine_id,
|
|
28
|
+
}
|
|
21
29
|
});
|
|
22
30
|
|
|
23
31
|
was_empty
|
|
@@ -19,7 +19,7 @@ module Snowflaked
|
|
|
19
19
|
def _snowflake_columns_from_comments
|
|
20
20
|
return [] unless table_exists?
|
|
21
21
|
|
|
22
|
-
columns.
|
|
22
|
+
columns.filter_map { |col| col.name.to_sym if col.comment == Snowflaked::SchemaDefinitions::COMMENT }
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
@@ -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