https://github.com/ycd/sarmio
📇 sarmio, A distributed unique ID generator inspired by Snowflake
https://github.com/ycd/sarmio
Last synced: 9 months ago
JSON representation
📇 sarmio, A distributed unique ID generator inspired by Snowflake
- Host: GitHub
- URL: https://github.com/ycd/sarmio
- Owner: ycd
- License: apache-2.0
- Created: 2021-01-08T23:41:54.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-09T23:51:14.000Z (over 5 years ago)
- Last Synced: 2025-02-05T06:49:00.587Z (over 1 year ago)
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-AL
Awesome Lists containing this project
README
# Sarmio
### Distributed unique ID generator, inspired by [Twitter's snowflake](https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html).
Sarmio creates a unique ID that is between the range of `0 > n > (2 ^ 64) -1`, also known as unsigned 64 bit integer.
## Usage
First of all, add `sarmio` as a dependency to your `cargo.toml`.
```toml
[dependencies]
sarmio = "0.1"
```
## Example
```rust
fn main() {
// Create new Sarmio instance with a machine-id of 255.
let mut sarmio_one = sarmio::Sarmio::new(255);
let mut sarmio_two = sarmio::Sarmio::new(555);
// Sarmio implements Iterator
// Which means you can iterate over it to create new IDs.
let id1 = match sarmio_one.next_id() {
Some(s) => s,
None => 0,
};
let id2 = match sarmio_two.next_id() {
Some(s) => s,
None => 0,
};
// Or create a new with next_id() syntax.
// Decompose it, get the values like
// Unix time in that moment, machine id
// and the Unique ID.
let id1_decomposed = sarmio::decompose(id1);
let id2_decomposed = sarmio::decompose(id2);
println!("{:?}", id1_decomposed); // ID { id: 27015264398737663, machine_id: 255, time: 1610235238 }
println!("{:?}", id2_decomposed); // ID { id: 27015264398737963, machine_id: 555, time: 1610235238 }
// Check which ID is older.
let is_older = id2_decomposed.older(&id1_decomposed);
println!("{:?}", is_older); // false
// Check whether the ID's are created in the same machine.
let same_machine = id2_decomposed.same_machine(&id1_decomposed);
println!("{:?}", same_machine) // false
}
```