use crate::digest::ConstraintDigest;
use crate::model::request::Constraint;
use crate::model::result::GenerationResult;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CacheScope {
FullFloor,
Chunk(String),
Export(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct CacheKey {
pub schema_version: String,
pub algorithm_version: String,
pub seed: u64,
pub config_digest: String,
pub constraints_digest: String,
pub scope: CacheScope,
}
impl CacheKey {
pub fn for_full_floor(
result: &GenerationResult,
constraints: &[Constraint],
) -> crate::error::PcgResult<Self> {
Ok(Self {
schema_version: result.metadata.schema_version.clone(),
algorithm_version: result.metadata.algorithm_version.clone(),
seed: result.metadata.seed,
config_digest: result.metadata.config_digest.clone(),
constraints_digest: ConstraintDigest::from_constraints(constraints)?.into_string(),
scope: CacheScope::FullFloor,
})
}
pub fn for_chunk(
result: &GenerationResult,
constraints: &[Constraint],
chunk_id: impl Into<String>,
) -> crate::error::PcgResult<Self> {
Ok(Self {
schema_version: result.metadata.schema_version.clone(),
algorithm_version: result.metadata.algorithm_version.clone(),
seed: result.metadata.seed,
config_digest: result.metadata.config_digest.clone(),
constraints_digest: ConstraintDigest::from_constraints(constraints)?.into_string(),
scope: CacheScope::Chunk(chunk_id.into()),
})
}
pub fn as_string(&self) -> String {
let scope = match &self.scope {
CacheScope::FullFloor => "full-floor".to_string(),
CacheScope::Chunk(chunk_id) => format!("chunk:{chunk_id}"),
CacheScope::Export(format_name) => format!("export:{format_name}"),
};
format!(
"{}:{}:{}:{}:{}:{}",
self.schema_version,
self.algorithm_version,
self.seed,
self.config_digest,
self.constraints_digest,
scope
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::debug::DebugBundle;
use crate::model::result::ResultMetadata;
use crate::model::room::RoomGraph;
fn sample_result() -> GenerationResult {
GenerationResult {
metadata: ResultMetadata {
seed: 42,
config_digest: "digest".to_string(),
schema_version: "1.0.0".to_string(),
algorithm_version: "0.1.0".to_string(),
target_engine_version: None,
trace_id: None,
},
topology: RoomGraph {
nodes: vec![],
edges: vec![],
critical_path: vec![],
branches: vec![],
},
rooms: vec![],
door_anchors: vec![],
corridors: vec![],
terrains: vec![],
item_spawns: vec![],
enemy_spawns: vec![],
chunks: vec![],
debug: Some(DebugBundle::default()),
}
}
#[test]
fn test_cache_key_string_contains_scope() {
let result = sample_result();
let key = CacheKey::for_full_floor(&result, &[]).expect("空约束摘要不应失败");
assert!(key.as_string().contains("full-floor"));
}
#[test]
fn test_cache_key_for_chunk_distinguishes_requested_chunks() {
let result = sample_result();
let chunk_a = CacheKey::for_chunk(&result, &[], "chunk-0-0").expect("空约束摘要不应失败");
let chunk_a_again =
CacheKey::for_chunk(&result, &[], "chunk-0-0").expect("空约束摘要不应失败");
let chunk_b = CacheKey::for_chunk(&result, &[], "chunk-1-0").expect("空约束摘要不应失败");
assert_eq!(chunk_a, chunk_a_again);
assert_eq!(chunk_a.as_string(), chunk_a_again.as_string());
assert_ne!(chunk_a, chunk_b);
assert_ne!(chunk_a.as_string(), chunk_b.as_string());
assert_eq!(chunk_a.scope, CacheScope::Chunk("chunk-0-0".to_string()));
assert!(chunk_a.as_string().contains("chunk:chunk-0-0"));
assert!(chunk_b.as_string().contains("chunk:chunk-1-0"));
}
#[test]
fn test_cache_key_full_floor_format_unchanged_and_distinct_from_chunk() {
let result = sample_result();
let full = CacheKey::for_full_floor(&result, &[]).expect("空约束摘要不应失败");
assert_eq!(full.scope, CacheScope::FullFloor);
assert!(full.as_string().ends_with(":full-floor"));
let chunk = CacheKey::for_chunk(&result, &[], "chunk-0-0").expect("空约束摘要不应失败");
assert_ne!(full, chunk);
assert_ne!(full.as_string(), chunk.as_string());
}
}