Skip to content

Commit

Permalink
feat: add object id(test) (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
HHongSeungWoo authored Dec 27, 2023
1 parent 8b7977d commit 038cbd8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastid"
version = "0.0.3"
version = "0.0.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -15,6 +15,8 @@ ulid="1.1.0"
pyo3="0.20.0"
rs-snowflake = "0.6.0"
once_cell = "1.19.0"
rand = "0.8.5"
hex = "0.4.3"

[dependencies.uuid]
version = "1.6.1"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Created for the easy and fast generation of various IDs
# Supported IDs
- ulid
- snowflake
- uuid v7 (If you intend to use uuid in your database, this can be a great choice)
- uuid v7 (If you intend to use uuid in your database, this can be a great choice)
4 changes: 4 additions & 0 deletions fastid.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ def snowflake_int(machine_id: int = 1, node_id: int = 1) -> int:

def uuid_v7() -> str:
...


def object_id() -> str:
"""test"""
11 changes: 9 additions & 2 deletions python/test/test_fastid.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import timeit
import uuid

import bson
import fastid
from ulid import microsecond
import ulid


def test_ulid():
print()
print("ulid-py ", timeit.timeit(lambda: microsecond.new().str, number=10000))
print("ulid-py ", timeit.timeit(lambda: ulid.microsecond.new().str, number=10000))
print("fastid.ulid", timeit.timeit(lambda: fastid.ulid(), number=10000))


Expand All @@ -21,3 +22,9 @@ def test_uuid():
print()
print("uuid.uuid4 ", timeit.timeit(lambda: uuid.uuid4(), number=10000))
print("fastid.uuid_v7", timeit.timeit(lambda: fastid.uuid_v7(), number=10000))


def test_objectid():
print()
print("bson.ObjectId", timeit.timeit(lambda: bson.ObjectId(), number=10000))
print("fastid.object_id", timeit.timeit(lambda: fastid.object_id(), number=10000))
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::sync::Mutex;

use once_cell::sync::Lazy;
use pyo3::prelude::*;
use snowflake::SnowflakeIdGenerator;
use ulid::Ulid;
use uuid::Uuid;

use crate::objectid::ObjectId;

mod objectid;

static SNOWFLAKE_GENERATOR: Lazy<Mutex<SnowflakeIdGenerator>> = Lazy::new(|| {
Mutex::new(SnowflakeIdGenerator::new(1, 1))
});
Expand Down Expand Up @@ -40,6 +45,10 @@ fn uuid_v7() -> PyResult<String> {
Ok(Uuid::now_v7().to_string())
}

#[pyfunction]
fn object_id() -> PyResult<String> {
Ok(ObjectId::new().to_hex_string())
}

#[pymodule]
fn fastid(_py: Python, m: &PyModule) -> PyResult<()> {
Expand All @@ -50,5 +59,7 @@ fn fastid(_py: Python, m: &PyModule) -> PyResult<()> {

m.add_function(wrap_pyfunction!(uuid_v7, m)?)?;

m.add_function(wrap_pyfunction!(object_id, m)?)?;

Ok(())
}
29 changes: 29 additions & 0 deletions src/objectid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::time::{SystemTime, UNIX_EPOCH};
use rand::Rng;

#[derive(Debug)]
pub struct ObjectId([u8; 12]);

impl ObjectId {
pub fn new() -> Self {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!")
.as_secs() as u32;

let mut rng = rand::thread_rng();
let random_bytes = rng.gen::<[u8; 5]>();
let counter = rng.gen::<[u8; 3]>();

let mut bytes = [0; 12];
bytes[0..4].copy_from_slice(&timestamp.to_be_bytes());
bytes[4..9].copy_from_slice(&random_bytes);
bytes[9..12].copy_from_slice(&counter);

ObjectId(bytes)
}

pub fn to_hex_string(&self) -> String {
hex::encode(self.0)
}
}

0 comments on commit 038cbd8

Please sign in to comment.