use harmos_domain::operations::storage::WriteRecords;
use std::sync::Arc;
use crate::operations::{Context, Operation, OperationDeclaration, StreamInput, StreamOutput};
use crate::storage::StorageManager;
use crate::storage::writes::{DataBatch, WriteCompletion, WriteKind, WriteReceipt, exchange};
use crate::{Error, Result};
pub(super) struct Handler<C> {
storage: Arc<StorageManager<C>>,
}
impl<C: 'static> Operation for Handler<C> {
type Declaration = WriteRecords;
type Resources = StorageManager<C>;
type Error = Error;
type Context<'a> = Context<'a>;
type Endpoints = (StreamInput<DataBatch>, StreamOutput<WriteReceipt>);
async fn initialize<'ctx>(storage: Arc<StorageManager<C>>, _: Self::Context<'ctx>) -> Result<Self> {
Ok(Self { storage })
}
async fn run<'a>(
&mut self, ctx: Self::Context<'a>, request: &mut WriteRecords, endpoints: &mut Self::Endpoints,
) -> Result<crate::operations::Response<WriteCompletion>> {
let completion = exchange(&self.storage, &ctx, request.scope.as_ref(), endpoints, WriteKind::Records, WriteRecords::ID).await?;
Ok(completion.into())
}
fn classify(error: &Error) -> crate::operations::FailureKind {
super::classify(error)
}
}