Skip to main content

harmos_operations/storage/
capture_file.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17
18// Third Party Imports
19
20// Local Imports
21use 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
29/* === Constants === */
30
31/* === Macros === */
32
33product!(CaptureFile {
34    source: Nested<FileSource> => 1,
35    range: Nested<ByteRange> => 2,
36    metadata: Nested<FileMetadata> => 3,
37    consistency: Code<CaptureConsistency> => 4,
38});
39
40/* === Type Aliases === */
41
42/* === Structs === */
43
44#[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
60/* === Enums === */
61
62/* === Traits === */
63
64/* === Implementations === */
65
66impl 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    // Standard Library Imports
86
87    // Third Party Imports
88
89    // Local Imports
90    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/* === Public Functions === */
119
120/* === Private Functions === */
121
122/* === Tests === */