#[cfg(any(feature = "kv", feature = "backup", feature = "console", feature = "sql", feature = "replication"))]
pub(crate) fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
#[cfg(any(
feature = "kv",
feature = "jobs",
feature = "sql",
feature = "blobs",
feature = "records",
feature = "search",
feature = "channels"
))]
pub(crate) fn is_name(name: &str) -> bool {
let bytes = name.as_bytes();
let first = bytes.first().is_some_and(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit());
let rest =
bytes.iter().all(|&byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_' || byte == b'-');
first && rest && bytes.len() <= 64
}
#[cfg(any(feature = "jobs", feature = "records"))]
pub(crate) fn fnv1a(bytes: impl IntoIterator<Item = u8>) -> u64 {
let mix = |hash: u64, byte: u8| (hash ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3);
bytes.into_iter().fold(0xcbf2_9ce4_8422_2325, mix)
}
#[cfg(any(feature = "records", feature = "metrics"))]
pub(crate) fn span(span: std::time::Duration) -> String {
let millis = span.as_millis();
let units = [("d", 86_400_000), ("h", 3_600_000), ("m", 60_000), ("s", 1000)];
match units.iter().find(|(_, unit)| millis >= *unit && millis.is_multiple_of(*unit)) {
Some((name, unit)) => format!("{}{name}", millis / unit),
None => format!("{millis}ms"),
}
}
#[cfg(any(feature = "config", feature = "console"))]
pub(crate) fn starts_word(chars: &[char], at: usize) -> bool {
if !chars[at].is_uppercase() {
return false;
}
let before = chars[at - 1];
let lower_after = chars.get(at + 1).is_some_and(|c| c.is_lowercase());
before.is_lowercase() || before.is_numeric() || before.is_uppercase() && lower_after
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(any(feature = "records", feature = "metrics"))]
fn a_span_is_said_in_its_largest_whole_unit() {
use std::time::Duration;
let said = [(86_400 * 30, "30d"), (3600 * 12, "12h"), (90, "90s"), (60, "1m")];
for (seconds, text) in said {
assert_eq!(super::span(Duration::from_secs(seconds)), text);
}
assert_eq!(super::span(Duration::from_millis(1500)), "1500ms");
}
}