-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10f1c85
commit 4123d6c
Showing
5 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
def ulid() -> str: | ||
... | ||
... | ||
|
||
def snowflake_str(machine_id: int = 1, node_id: int = 1) -> str: | ||
... | ||
|
||
def snowflake_int(machine_id: int = 1, node_id: int = 1) -> int: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,44 @@ | ||
use std::sync::Mutex; | ||
use once_cell::sync::Lazy; | ||
use pyo3::prelude::*; | ||
use ulid::{Ulid}; | ||
use snowflake::SnowflakeIdGenerator; | ||
use ulid::Ulid; | ||
|
||
static SNOWFLAKE_GENERATOR: Lazy<Mutex<SnowflakeIdGenerator>> = Lazy::new(|| { | ||
Mutex::new(SnowflakeIdGenerator::new(1, 1)) | ||
}); | ||
#[pyfunction] | ||
#[pyo3(name="ulid")] | ||
#[pyo3(name = "ulid")] | ||
fn get_ulid() -> PyResult<String> { | ||
Ok(Ulid::new().to_string()) | ||
} | ||
|
||
|
||
fn get_snowflake(machine_id: Option<i32>, node_id: Option<i32>) -> i64 { | ||
let mut sg = SNOWFLAKE_GENERATOR.lock().unwrap(); | ||
sg.node_id = node_id.unwrap_or(1); | ||
sg.machine_id = machine_id.unwrap_or(1); | ||
sg.real_time_generate() | ||
} | ||
#[pyfunction] | ||
#[pyo3(name = "snowflake_str")] | ||
fn get_snowflake_str(machine_id: Option<i32>, node_id: Option<i32>) -> PyResult<String> { | ||
Ok(format!("{:X}", get_snowflake(machine_id, node_id))) | ||
} | ||
|
||
#[pyfunction] | ||
#[pyo3(name = "snowflake_int")] | ||
fn get_snowflake_int(machine_id: Option<i32>, node_id: Option<i32>) -> PyResult<i64> { | ||
Ok(get_snowflake(machine_id, node_id)) | ||
} | ||
|
||
|
||
#[pymodule] | ||
fn fastid(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(get_ulid, m)?)?; | ||
|
||
m.add_function(wrap_pyfunction!(get_snowflake_int, m)?)?; | ||
m.add_function(wrap_pyfunction!(get_snowflake_str, m)?)?; | ||
|
||
Ok(()) | ||
} |