use super::IO_POLICY;
use harmos_domain::MemorySize;
use harmos_domain::model::storage::codec::{Nested, product};
use harmos_domain::model::storage::files::{ByteRange, FileHash, FileSource};
use harmos_domain::operations::ReplaySafety;
#[cfg(feature = "runtime")]
use harmos_storage::{ErasedCatalog, StorageManager};
product!(HashFile {
source: Nested<FileSource> => 1,
range: Nested<ByteRange> => 2,
});
#[harmos::operation(harmos_path = crate, resources = [StorageManager<ErasedCatalog>], codec = manual,
id = "storage.hash_file",
response = FileHash,
policy = IO_POLICY,
visibility = Shared,
replay = ReplaySafety::Idempotent,
grpc(package = "harmos.storage.v1", service = "Storage", method = "HashFile"),
grant = Self::grant_for)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HashFile {
pub source: FileSource,
pub range: ByteRange,
}
impl HashFile {
fn grant_for(request: &HashFile) -> Option<harmos_domain::plugin::Grant> {
request.source.grant_scope().map(|scope| harmos_domain::plugin::Grant::of(<Self as harmos_domain::OperationDeclaration>::ID, scope))
}
}
impl MemorySize for HashFile {
fn memory_size(&self) -> usize {
std::mem::size_of::<Self>()
.saturating_add(self.source.memory_size().saturating_sub(std::mem::size_of_val(&self.source)))
.saturating_add(self.range.memory_size().saturating_sub(std::mem::size_of_val(&self.range)))
}
}
#[cfg(feature = "runtime")]
mod execution {
use crate::{Error, Result};
use harmos_domain::model::storage::files::FileHash;
use harmos_sdk::operations::Operation;
impl Operation for super::HashFile {
type Error = Error;
#[tracing::instrument(target = "harmos::storage", name = "storage.operation.hash_file", level = "trace", skip_all)]
async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<FileHash>> {
let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
Ok(storage
.hash_file(harmos_storage::FileRead {
source: self.source.clone(),
range: self.range,
})
.await?
.into())
}
fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
crate::storage::classify(error)
}
}
}