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