use std::{
num::NonZeroU64,
path::{Path, PathBuf},
};
use thiserror::Error;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CacheConfig {
pub(super) directory: PathBuf,
pub(super) file_name: String,
pub(super) payload_version: u64,
pub(super) disk_capacity: u64,
pub(super) memory_capacity: u64,
pub(super) memory_shard_count: usize,
pub(super) access_count_half_life: NonZeroU64,
pub(super) history_entry_limit: usize,
pub(super) reclaim_sample_size: usize,
}
impl CacheConfig {
pub const fn payload_version(&self) -> u64 {
self.payload_version
}
pub const fn reclaim_sample_size(&self) -> usize {
self.reclaim_sample_size
}
pub const fn access_count_half_life(&self) -> NonZeroU64 {
self.access_count_half_life
}
pub const fn history_entry_limit(&self) -> usize {
self.history_entry_limit
}
pub fn directory(&self) -> &Path {
&self.directory
}
pub fn file_name(&self) -> &str {
&self.file_name
}
pub const fn disk_capacity(&self) -> u64 {
self.disk_capacity
}
pub const fn memory_shard_count(&self) -> usize {
self.memory_shard_count
}
pub const fn memory_capacity(&self) -> u64 {
self.memory_capacity
}
}
#[derive(Clone, Copy, Debug, Error, PartialEq, Eq)]
pub enum CacheConfigError {
#[error("cache file name must be a single filename without NUL bytes or the reserved .feuer. prefix")]
InvalidFileName,
#[error("memory capacity must be greater than zero")]
InvalidMemoryCapacity,
#[error("payload version must be a nonnegative number fitting u64; check FEUER_PAYLOAD_VERSION")]
InvalidPayloadVersion,
#[error("memory shard count must be greater than zero; check FEUER_MEMORY_SHARD_COUNT or the explicit setting")]
InvalidMemoryShardCount,
#[error(
"reclaim sample size must be a positive number fitting usize; check FEUER_RECLAIM_SAMPLE_SIZE (e.g. 1KiB) or the explicit setting"
)]
InvalidReclaimSampleSize,
#[error(
"access count half-life must be greater than zero; check FEUER_ACCESS_COUNT_HALF_LIFE or the explicit setting"
)]
InvalidAccessCountHalfLife,
#[error(
"history entry limit must be a nonnegative number fitting usize; check FEUER_HISTORY_ENTRY_LIMIT or the explicit setting"
)]
InvalidHistoryEntryLimit,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Cache, CacheBuilder};
use std::{
env::{current_exe, var},
process::Command,
};
fn builder() -> CacheBuilder<'static> {
Cache::builder().memory_capacity(1)
}
fn check_environment_overrides(test: &str, name: &str, child: &str, cases: &[(Option<&str>, &str)]) {
for &(setting, expected) in cases {
let mut command = Command::new(current_exe().unwrap());
command.args(["--exact", test]).env(child, expected).env_remove(name);
if let Some(setting) = setting {
command.env(name, setting);
}
let output = command.output().unwrap();
assert!(output.status.success(), "{output:?}");
}
}
#[test]
fn memory_shard_count() {
for (capacity, expected) in [(1, 1), (100 * 1024 * 1024 * 1024, 12)] {
assert_eq!(
builder()
.memory_capacity(capacity)
.resolve_config()
.unwrap()
.memory_shard_count(),
expected
);
let cache = builder()
.memory_capacity(capacity)
.memory_shard_count(4)
.open()
.unwrap();
assert_eq!(cache.config().memory_shard_count(), 4);
}
assert_eq!(
builder().memory_shard_count(0).resolve_config(),
Err(CacheConfigError::InvalidMemoryShardCount)
);
}
#[test]
fn memory_shard_count_environment_override() {
if let Ok(expected) = var("FEUER_TEST_MEMORY_SHARD_COUNT_CHILD") {
assert_eq!(
builder().resolve_config().map(|config| config.memory_shard_count()),
expected
.parse::<usize>()
.map_err(|_| CacheConfigError::InvalidMemoryShardCount)
);
assert_eq!(
builder()
.memory_shard_count(4)
.resolve_config()
.unwrap()
.memory_shard_count(),
4
);
return;
}
check_environment_overrides(
"config::tests::memory_shard_count_environment_override",
"FEUER_MEMORY_SHARD_COUNT",
"FEUER_TEST_MEMORY_SHARD_COUNT_CHILD",
&[
(None, "1"),
(Some("16"), "16"),
(Some("0"), "error"),
(Some("bad"), "error"),
(Some("18446744073709551616"), "error"),
],
);
}
#[test]
fn payload_version_environment_override() {
if let Ok(expected) = var("FEUER_TEST_PAYLOAD_VERSION_CHILD") {
assert_eq!(
builder().resolve_config().map(|config| config.payload_version()),
expected
.parse::<u64>()
.map_err(|_| CacheConfigError::InvalidPayloadVersion)
);
for version in [0, 42] {
assert_eq!(
builder()
.payload_version(version)
.resolve_config()
.unwrap()
.payload_version(),
version
);
}
return;
}
check_environment_overrides(
"config::tests::payload_version_environment_override",
"FEUER_PAYLOAD_VERSION",
"FEUER_TEST_PAYLOAD_VERSION_CHILD",
&[
(None, "0"),
(Some("0"), "0"),
(Some("42"), "42"),
(Some("18446744073709551615"), "18446744073709551615"),
(Some("18446744073709551616"), "error"),
(Some("-1"), "error"),
(Some("bad"), "error"),
(Some(""), "error"),
],
);
}
#[test]
fn rejects_invalid_file_names() {
for name in [
"",
".",
"..",
"../data",
"cache/data",
".feuer.lock",
".feuer.images.lock",
"data\0",
] {
assert_eq!(
builder().file_name(name).resolve_config(),
Err(CacheConfigError::InvalidFileName)
);
}
}
#[test]
fn reclaim_sample_size_environment_override() {
if let Ok(expected) = var("FEUER_TEST_RECLAIM_SAMPLE_CHILD") {
let result = builder().resolve_config().map(|config| config.reclaim_sample_size());
let expected = expected
.parse::<usize>()
.map_err(|_| CacheConfigError::InvalidReclaimSampleSize);
assert_eq!(result, expected);
assert_eq!(
builder()
.reclaim_sample_size(128)
.resolve_config()
.unwrap()
.reclaim_sample_size(),
128
);
return;
}
check_environment_overrides(
"config::tests::reclaim_sample_size_environment_override",
"FEUER_RECLAIM_SAMPLE_SIZE",
"FEUER_TEST_RECLAIM_SAMPLE_CHILD",
&[
(None, "64"),
(Some("1KiB"), "1024"),
(Some("0"), "error"),
(Some("bad"), "error"),
],
);
}
#[test]
fn access_count_half_life_environment_override() {
const NAME: &str = "FEUER_ACCESS_COUNT_HALF_LIFE";
const CHILD: &str = "FEUER_TEST_HALF_LIFE_CHILD";
if let Ok(expected) = var(CHILD) {
assert_eq!(
builder()
.resolve_config()
.map(|config| config.access_count_half_life().get()),
expected
.parse::<u64>()
.map_err(|_| CacheConfigError::InvalidAccessCountHalfLife)
);
assert_eq!(
builder()
.access_count_half_life(128)
.resolve_config()
.unwrap()
.access_count_half_life()
.get(),
128
);
return;
}
check_environment_overrides(
"config::tests::access_count_half_life_environment_override",
NAME,
CHILD,
&[
(None, "262144"),
(Some("1"), "1"),
(Some("64KiB"), "65536"),
(Some("18446744073709551615"), "18446744073709551615"),
(Some("0"), "error"),
(Some("bad"), "error"),
],
);
}
#[test]
fn history_entry_limit_environment_override() {
if let Ok(expected) = var("FEUER_TEST_HISTORY_ENTRY_LIMIT_CHILD") {
assert_eq!(
builder().resolve_config().map(|config| config.history_entry_limit()),
expected
.parse::<usize>()
.map_err(|_| CacheConfigError::InvalidHistoryEntryLimit)
);
for limit in [0, 32] {
assert_eq!(
builder()
.history_entry_limit(limit)
.resolve_config()
.unwrap()
.history_entry_limit(),
limit
);
}
return;
}
check_environment_overrides(
"config::tests::history_entry_limit_environment_override",
"FEUER_HISTORY_ENTRY_LIMIT",
"FEUER_TEST_HISTORY_ENTRY_LIMIT_CHILD",
&[
(None, "10000000"),
(Some("1"), "1"),
(Some("1KiB"), "1024"),
(Some("0"), "0"),
(Some("-1"), "error"),
(Some("bad"), "error"),
(Some("18446744073709551616"), "error"),
],
);
}
}