LiosK / uuid7-rs

A Rust implementation of UUID version 7
Apache License 2.0
31 stars 0 forks source link

Implementation of node/machine ID? #2

Closed brsnik closed 1 year ago

brsnik commented 2 years ago

Will it be possible to implement a machine ID as mentioned in the proposal?

LiosK commented 2 years ago

As of the latest draft, the node (machine) ID is a UUIDv8 feature.

brsnik commented 2 years ago

@LiosK The node is also a feature of UUIDv7 as per the same draft:

UUIDv7 implementations, even with very detailed sub-second precision and the optional sequence counter, MAY have leftover bits that will be identified as the Node for this section. The UUIDv7 Node MAY contain any set of data an implementation desires however the node MUST NOT be set to all 0s which does not ensure global uniqueness. In most scenarios the node SHOULD be filled with pseudo-random data.

LiosK commented 2 years ago

You are reading the draft 01. The current draft is 04. Through discussions, UUIDv7 is simplified to a great extent, and some application-specific features like node IDs are squeezed out to UUIDv8.

brsnik commented 2 years ago

Ah I see. Thanks for clarifying.

Um, how about the variant (var) of UUIDv7, can that be used to specify a "node"?

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                           unix_ts_ms                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          unix_ts_ms           |  ver  |       rand_a          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |var|                        rand_b                             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                            rand_b                             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
LiosK commented 2 years ago

No, the var bits must be 0b10 to comply with the standard.

Generally speaking, you don't need a node ID because collision is very unlikely even under distributed use cases, but if you really need one, you can simply resort to UUIDv8 like:

use uuid7::{uuid7, Uuid};

fn uuid8(node_id: u8) -> Uuid {
    let mut bytes: [u8; 16] = uuid7().into();
    bytes[6] = 0x80 | (0xF & bytes[6]); // set version to 8
    bytes[15] = node_id; // dedicate last byte to node ID
    bytes.into()
}

fn main() {
    for _ in 0..8 {
        println!("{}", uuid8(0x42));
    }
}