use tessarikv_constants as limits;
use super::{DbOptions, KeyspaceOptions};
use crate::{Error, Result};
pub(crate) const DEFAULT_NAME: &str = "default";
impl DbOptions {
#[must_use]
pub fn keyspaces(&self) -> &[(String, KeyspaceOptions)] {
&self.keyspaces
}
#[must_use]
pub const fn create_missing_keyspaces(&self) -> bool {
self.create_missing_keyspaces
}
}
impl super::DbOptionsBuilder {
pub fn keyspace(mut self, name: impl Into<String>, options: KeyspaceOptions) -> Self {
self.options.keyspaces.push((name.into(), options));
self
}
pub const fn create_missing_keyspaces(mut self, value: bool) -> Self {
self.options.create_missing_keyspaces = value;
self
}
}
pub(super) fn check(options: &DbOptions) -> Result<()> {
let refuse = |reason: &str| {
Err(Error::InvalidOptions {
option: "db.keyspaces",
reason: reason.to_owned(),
})
};
let most = usize::try_from(limits::MAX_KEYSPACES).unwrap_or(usize::MAX);
if options.keyspaces.len() >= most {
return refuse("more keyspaces than a database holds");
}
for (at, (name, _)) in options.keyspaces.iter().enumerate() {
if name.is_empty() || name.len() > limits::MAX_KEYSPACE_NAME_BYTES {
return refuse("a keyspace name is empty or too long");
}
if name == DEFAULT_NAME {
return refuse("a keyspace takes the default keyspace's name");
}
if options
.keyspaces
.iter()
.skip(at.saturating_add(1))
.any(|(other, _)| other == name)
{
return refuse("a keyspace is named twice");
}
}
Ok(())
}