use crate::prelude::*;
use bab_rs::generic::storage::units::{ByteCount, ByteIndex};
use derive_more::{Display, Error};
use ufotofu::prelude::*;
pub trait Store {
type InternalError;
async fn create_entry<P>(
&mut self,
namespace_id: &NamespaceId,
subspace_id: &SubspaceId,
path: &Path,
timestamp: Timestamp,
payload_producer: &mut P,
payload_length: u64,
write_capability: &WriteCapability,
secret: &SubspaceSecret,
) -> Result<Option<AuthorisedEntry>, CreateEntryError<Self::InternalError>>
where
P: BulkProducer<Item = u8>;
async fn create_entry_nondestructive<P>(
&mut self,
namespace_id: &NamespaceId,
subspace_id: &SubspaceId,
path: &Path,
timestamp: Timestamp,
payload_producer: &mut P,
payload_length: u64,
write_capability: &WriteCapability,
secret: &SubspaceSecret,
) -> Result<NondestructiveInsert, CreateEntryError<Self::InternalError>>
where
P: BulkProducer<Item = u8>;
async fn insert_entry(&mut self, entry: AuthorisedEntry) -> Result<bool, Self::InternalError>;
async fn forget_entry<K>(
&mut self,
namespace_id: &NamespaceId,
key: &K,
expected_digest: Option<PayloadDigest>,
) -> Result<bool, Self::InternalError>
where
K: Keylike;
async fn forget_area(
&mut self,
namespace_id: &NamespaceId,
area: &Area,
) -> Result<(), Self::InternalError>;
async fn forget_namespace(
&mut self,
namespace_id: &NamespaceId,
) -> Result<(), Self::InternalError>;
async fn get_entry<K>(
&mut self,
namespace_id: &NamespaceId,
key: &K,
expected_digest: Option<PayloadDigest>,
) -> Result<Option<AuthorisedEntry>, Self::InternalError>
where
K: Keylike;
async fn get_entry_and_payload_slice<K, C>(
&mut self,
namespace_id: &NamespaceId,
key: &K,
expected_digest: Option<PayloadDigest>,
payload_slice_start: ByteIndex,
payload_slice_length: ByteCount,
c: &mut C,
) -> Result<
Option<(AuthorisedEntry, ByteCount)>,
StoreOrConsumerError<Self::InternalError, C::Error>,
>
where
K: Keylike,
C: BulkConsumer<Item = u8>;
async fn get_payload_slice<K, C>(
&mut self,
namespace_id: &NamespaceId,
key: &K,
expected_digest: Option<PayloadDigest>,
start: ByteIndex,
length: ByteCount,
c: &mut C,
) -> Result<ByteCount, GetPayloadSliceError<Self::InternalError, C::Error>>
where
K: Keylike,
C: BulkConsumer<Item = u8>;
async fn get_area<C>(
&mut self,
namespace_id: &NamespaceId,
area: &Area,
c: &mut C,
) -> Result<(), StoreOrConsumerError<Self::InternalError, C::Error>>
where
C: Consumer<Item = AuthorisedEntry>;
async fn flush(&mut self) -> Result<(), Self::InternalError>;
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum NondestructiveInsert {
Success(AuthorisedEntry),
Prevented,
Outdated,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Display, Error)]
pub enum CreateEntryError<StoreError> {
#[display("store error")]
StoreError(StoreError),
#[display("does not authorise")]
AuthorisationTokenError,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Display, Error)]
pub enum GetPayloadSliceError<StoreError, ConsumerError> {
#[display("store error")]
StoreError(StoreError),
#[display("consumer error")]
ConsumerError(ConsumerError),
#[display("no such entry")]
NoSuchEntry,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Display, Error)]
pub enum StoreOrConsumerError<StoreError, ConsumerError> {
#[display("store error")]
StoreError(StoreError),
#[display("consumer error")]
ConsumerError(ConsumerError),
}