HLC ID is a Rust library designed to generate, manage, and encode Hybrid Logical Clock (HLC)-based identifiers. These unique, distributed, and compact identifiers facilitate event ordering in decentralized systems.
- Hybrid Logical Clock (HLC): Generates timestamped event IDs based on logical time.
- Compact Representation: Stores IDs as 128-bit integers.
- Base64 Encoding: Encodes IDs for easier transmission and storage.
- Custom Node IDs: Assigns unique identifiers per instance.
- Persistence Support: Save and restore clocks for continuity.
- Event Comparison: Compare events to determine their relative order.
Add hlc_id
to your Cargo.toml
:
[dependencies]
hlc_id = "0.1.4"
Run cargo build
to download and compile the library.
use hlc_id::{clock::HybridLogicalClock, id::HLCId};
use chrono::Utc;
fn main() {
let mut clock = HybridLogicalClock::new(42);
let timestamp = Utc::now().timestamp_millis() as u64;
let hlc_id = HLCId::generate(&mut clock, timestamp);
println!("Generated ID: {:?}", hlc_id);
let id_as_u128 = hlc_id.to_u128();
println!("ID as 128-bit integer: {}", id_as_u128);
let encoded = hlc_id.encode_base64();
println!("Encoded ID (Base64): {}", encoded);
let decoded_id = HLCId::decode_base64(&encoded).unwrap();
println!("Decoded ID: {:?}", decoded_id);
}
Run the built-in tests:
cargo test
Run the example program:
cargo run --example generate_id
new(node_id: u16) -> HybridLogicalClock
: Creates a new logical clock.update(&mut self, external_timestamp: u64)
: Updates the clock with an external timestamp.process_timestamp(&mut self, received_timestamp: u64)
: Adjusts the clock based on incoming event timestamps.save_state(&self, path: &str) -> std::io::Result<()>
: Saves clock state to a file.load_state(path: &str) -> std::io::Result<Self>
: Loads a clock from a saved state.
generate(clock: &mut HybridLogicalClock, timestamp: u64) -> HLCId
: Generates a new HLC-based identifier.to_u128(&self) -> u128
: Converts an ID to a 128-bit integer.from_u128(id: u128) -> HLCId
: Reconstructs an ID from a 128-bit integer.encode_base64(&self) -> String
: Encodes the ID in Base64.decode_base64(encoded: &str) -> Result<HLCId, String>
: Decodes a Base64-encoded ID.is_before(&self, other: &HLCId) -> bool
: Compares two IDs to determine event order.
Full API documentation is available at docs.rs.
This project is licensed under the MIT License.
Contributions are welcome. Submit issues, pull requests, or suggest improvements.
Developed by Malo Henry
GitHub: MaloHenry