pub struct KafkaUuid(pub Uuid);Expand description
A Kafka Uuid: a cluster id, topic id, or log-directory id.
This is the value type of org.apache.kafka.common.Uuid. Kafka prints
these ids as 22 characters of unpadded base64url, for example
UXyU9i5ARn6W00ON2taeWA, not as the hyphenated hex form that
uuid::Uuid prints. Display and FromStr use the
Kafka form. So does serde, which always writes the id as that string.
FromStr matches Uuid.fromString in Apache Kafka 4.3.1. It accepts
exactly the inputs that Kafka accepts and gives the same error messages.
Two consequences are not obvious:
- Kafka ignores the unused low bits of the last character, so
_____________________xparses to the same id as_____________________w. - Kafka also accepts the 22 characters followed by
==.
Parsing never rejects a reserved id. Kafka’s fromString does not reject
them, and kafka-storage format --cluster-id stores any string. Check
is_reserved where a caller must refuse one.
The ordering is Kafka’s compareTo: it compares the two halves as signed
64-bit integers, most significant half first. This is not the unsigned
byte order of uuid::Uuid.
§Examples
use krabka_ids::KafkaUuid;
let id: KafkaUuid = "UXyU9i5ARn6W00ON2taeWA".parse()?;
assert2::assert!(id.to_string() == "UXyU9i5ARn6W00ON2taeWA");
assert2::assert!(KafkaUuid::METADATA_TOPIC_ID.to_string() == "AAAAAAAAAAAAAAAAAAAAAQ");Tuple Fields§
§0: UuidImplementations§
Source§impl KafkaUuid
impl KafkaUuid
Sourcepub const METADATA_TOPIC_ID: Self = Self::ONE
pub const METADATA_TOPIC_ID: Self = Self::ONE
The topic id of the metadata topic, __cluster_metadata.
Sourcepub const DIRECTORY_MIGRATING: Self = Self::ZERO
pub const DIRECTORY_MIGRATING: Self = Self::ZERO
The directory id of a replica that is in a log directory that the
broker has not yet reported (Kafka’s DirectoryId.MIGRATING).
Sourcepub const DIRECTORY_UNASSIGNED: Self = Self::ONE
pub const DIRECTORY_UNASSIGNED: Self = Self::ONE
The directory id of a replica that is not yet placed in a log directory
(Kafka’s DirectoryId.UNASSIGNED).
Sourcepub const DIRECTORY_LOST: Self
pub const DIRECTORY_LOST: Self
The directory id of a replica in an offline log directory that has no
known id (Kafka’s DirectoryId.LOST).
Sourcepub const fn from_bits(most_significant: i64, least_significant: i64) -> Self
pub const fn from_bits(most_significant: i64, least_significant: i64) -> Self
Builds an id from its two halves, as Kafka’s Uuid(long, long) does.
Sourcepub const fn get(self) -> Uuid
pub const fn get(self) -> Uuid
The inner uuid::Uuid.
Sourcepub const fn most_significant_bits(self) -> i64
pub const fn most_significant_bits(self) -> i64
The most significant 64 bits, as Kafka’s getMostSignificantBits
returns them.
Sourcepub const fn least_significant_bits(self) -> i64
pub const fn least_significant_bits(self) -> i64
The least significant 64 bits, as Kafka’s getLeastSignificantBits
returns them.
Sourcepub fn random() -> Self
pub fn random() -> Self
Returns a random version 4 id, as Kafka’s Uuid.randomUuid does.
The result is never in RESERVED, and its string
form never starts with -. A leading - would make a command-line
parser read the id as an option.
Sourcepub fn random_directory_id() -> Self
pub fn random_directory_id() -> Self
Returns a random log-directory id, as Kafka’s DirectoryId.random
does.
The result obeys the rules of random, and it is never
one of the 100 reserved directory ids (see
is_reserved_directory_id).
Sourcepub fn is_reserved(self) -> bool
pub fn is_reserved(self) -> bool
Returns true for an id in RESERVED.
Sourcepub const fn is_reserved_directory_id(self) -> bool
pub const fn is_reserved_directory_id(self) -> bool
Returns true for one of the 100 lowest ids, which Kafka reserves as
directory ids (Kafka’s DirectoryId.reserved).
Sourcepub fn parse_kafka_or_hyphenated(input: &str) -> Result<Self, KafkaUuidError>
pub fn parse_kafka_or_hyphenated(input: &str) -> Result<Self, KafkaUuidError>
Parses the Kafka form, or the 36-character hyphenated hex form.
Kafka’s Uuid.fromString rejects the hyphenated form, so
FromStr does too. Krabka’s own tools also accept it, and they
parse through this function. An input with a - at each of the four
hyphen positions of a 36-character string is parsed as hex. Any other
input is parsed as FromStr parses it.
§Errors
Returns KafkaUuidError::Hyphenated when the input has the shape of
the hyphenated form but is not valid hex. Otherwise returns the error
of FromStr.