harmos_operations/storage/
capture_file.rs1use super::IO_POLICY;
22use harmos_domain::MemorySize;
23use harmos_domain::model::storage::codec::{Code, Nested, product};
24use harmos_domain::model::storage::files::{ByteRange, CaptureConsistency, FileMetadata, FileReference, FileSource};
25use harmos_domain::operations::ReplaySafety;
26#[cfg(feature = "runtime")]
27use harmos_storage::{ErasedCatalog, StorageManager};
28
29product!(CaptureFile {
34 source: Nested<FileSource> => 1,
35 range: Nested<ByteRange> => 2,
36 metadata: Nested<FileMetadata> => 3,
37 consistency: Code<CaptureConsistency> => 4,
38});
39
40#[harmos::operation(harmos_path = crate, resources = [StorageManager<ErasedCatalog>], codec = manual,
45 id = "storage.capture_file",
46 response = FileReference,
47 policy = IO_POLICY,
48 visibility = Shared,
49 replay = ReplaySafety::Unknown,
50 grpc(package = "harmos.storage.v1", service = "Storage", method = "CaptureFile"),
51 grant = Self::grant_for)]
52#[derive(Clone, Debug, PartialEq, Eq)]
53pub struct CaptureFile {
54 pub source: FileSource,
55 pub range: ByteRange,
56 pub metadata: FileMetadata,
57 pub consistency: CaptureConsistency,
58}
59
60impl CaptureFile {
67 fn grant_for(request: &CaptureFile) -> Option<harmos_domain::plugin::Grant> {
68 request.source.grant_scope().map(|scope| harmos_domain::plugin::Grant::of(<Self as harmos_domain::OperationDeclaration>::ID, scope))
69 }
70}
71
72impl MemorySize for CaptureFile {
73 fn memory_size(&self) -> usize {
74 std::mem::size_of::<Self>()
75 .saturating_add(self.source.memory_size().saturating_sub(std::mem::size_of_val(&self.source)))
76 .saturating_add(self.range.memory_size().saturating_sub(std::mem::size_of_val(&self.range)))
77 .saturating_add(self.metadata.memory_size().saturating_sub(std::mem::size_of_val(&self.metadata)))
78 .saturating_add(self.consistency.memory_size().saturating_sub(std::mem::size_of_val(&self.consistency)))
79 }
80}
81
82#[cfg(feature = "runtime")]
83mod execution {
84
85 use crate::{Error, Result};
91 use harmos_domain::model::storage::files::FileReference;
92 use harmos_sdk::operations::Operation;
93
94 impl Operation for super::CaptureFile {
95 type Error = Error;
96
97 #[tracing::instrument(target = "harmos::storage", name = "storage.operation.capture_file", level = "trace", skip_all)]
98 async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<FileReference>> {
99 let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
100
101 Ok(storage
102 .capture_file(harmos_storage::FileCapture {
103 source: self.source.clone(),
104 range: self.range,
105 metadata: self.metadata.clone(),
106 consistency: self.consistency,
107 })
108 .await?
109 .into())
110 }
111
112 fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
113 crate::storage::classify(error)
114 }
115 }
116}
117
118