use std::borrow::Cow;
pub const DEFAULT_NAMESPACE: &str = "default";
pub const NS_NAME_PREFIX: &str = "_meta:nsName:";
#[inline]
pub fn ns_name_key(ns: &str) -> String {
format!("{NS_NAME_PREFIX}{ns}")
}
#[inline]
pub fn db_to_namespace(db: u64) -> Cow<'static, str> {
match db {
0 => Cow::Borrowed("default"),
1 => Cow::Borrowed("db1"),
2 => Cow::Borrowed("db2"),
3 => Cow::Borrowed("db3"),
4 => Cow::Borrowed("db4"),
5 => Cow::Borrowed("db5"),
6 => Cow::Borrowed("db6"),
7 => Cow::Borrowed("db7"),
8 => Cow::Borrowed("db8"),
9 => Cow::Borrowed("db9"),
10 => Cow::Borrowed("db10"),
11 => Cow::Borrowed("db11"),
12 => Cow::Borrowed("db12"),
13 => Cow::Borrowed("db13"),
14 => Cow::Borrowed("db14"),
15 => Cow::Borrowed("db15"),
_ => Cow::Owned(format!("db{db}")),
}
}
#[inline]
pub fn is_default_namespace(ns: &str) -> bool {
ns == DEFAULT_NAMESPACE || ns == "0" || ns == "db0" || ns.is_empty()
}
#[inline]
pub fn is_default_namespace_bytes(ns: &[u8]) -> bool {
ns == DEFAULT_NAMESPACE.as_bytes() || ns == b"0" || ns == b"db0" || ns.is_empty()
}
#[inline]
pub fn namespace_to_db(ns: &str) -> u64 {
if is_default_namespace(ns) {
0
} else if let Some(stripped) = ns.strip_prefix("db") {
stripped.parse::<u64>().unwrap_or(0)
} else {
ns.parse::<u64>().unwrap_or_default()
}
}