Skip to main content

harmos_operations/storage/
append_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::POLICY;
22use harmos_domain::MemorySize;
23use harmos_domain::model::storage::codec::{Bytes, Nested, Unsigned, product};
24use harmos_domain::model::storage::{FileProducer, FileSession, MutationId};
25use harmos_domain::operations::ReplaySafety;
26#[cfg(feature = "runtime")]
27use harmos_storage::{ErasedCatalog, StorageManager};
28
29/* === Constants === */
30
31/* === Macros === */
32
33product!(AppendFile {
34    mutation: Nested<MutationId> => 1,
35    session: Nested<FileSession> => 2,
36    offset: Unsigned => 3,
37    bytes: Bytes => 4,
38});
39
40/* === Type Aliases === */
41
42/* === Structs === */
43
44// Append Bytes At An Offset Of A Producing File Session.
45#[harmos::operation(harmos_path = crate, resources = [StorageManager<ErasedCatalog>], codec = manual,
46    id = "storage.append_file",
47    response = FileProducer,
48    policy = POLICY,
49    visibility = Host,
50    replay = ReplaySafety::Idempotent)]
51#[derive(Clone, Debug, PartialEq, Eq)]
52pub struct AppendFile {
53    pub mutation: MutationId,
54    pub session: FileSession,
55    pub offset: u64,
56    pub bytes: Vec<u8>,
57}
58
59/* === Enums === */
60
61/* === Traits === */
62
63/* === Implementations === */
64
65impl MemorySize for AppendFile {
66    fn memory_size(&self) -> usize {
67        std::mem::size_of::<Self>()
68            .saturating_add(self.mutation.memory_size().saturating_sub(std::mem::size_of_val(&self.mutation)))
69            .saturating_add(self.session.memory_size().saturating_sub(std::mem::size_of_val(&self.session)))
70            .saturating_add(self.offset.memory_size().saturating_sub(std::mem::size_of_val(&self.offset)))
71            .saturating_add(self.bytes.memory_size().saturating_sub(std::mem::size_of_val(&self.bytes)))
72    }
73}
74
75#[cfg(feature = "runtime")]
76mod execution {
77
78    // Standard Library Imports
79
80    // Third Party Imports
81
82    // Local Imports
83
84    use harmos_sdk::operations::Operation;
85    use harmos_storage::{FileChunk, FileProducer};
86
87    use crate::{Error, Result};
88
89    impl Operation for super::AppendFile {
90        type Error = Error;
91
92        #[tracing::instrument(target = "harmos::storage", name = "storage.operation.append_file", level = "trace", skip_all)]
93        async fn run(mut self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<FileProducer>> {
94            let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
95
96            let payload = FileChunk::new(std::mem::take(&mut self.bytes));
97            let result: ::core::result::Result<FileProducer, Self::Error> =
98                async move { storage.append_file_once(&self.mutation, &self.session, self.offset, payload).await.map_err(Into::into) }
99                    .await;
100            result.map(::core::convert::Into::into)
101        }
102
103        fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
104            crate::storage::classify(error)
105        }
106    }
107}
108
109/* === Public Functions === */
110
111/* === Private Functions === */
112
113/* === Tests === */