use std::{collections::HashMap, sync::Arc};
use futures::{
future::{BoxFuture, LocalBoxFuture},
lock::Mutex,
FutureExt,
};
use crate::{
blob::Blob,
future::{FutureKind, Local, Sendable},
Digest,
};
use super::{Fragment, LooseCommit};
pub trait Storage<K: FutureKind> {
type Error: core::error::Error;
fn load_loose_commits(&self) -> K::Future<'_, Result<Vec<LooseCommit>, Self::Error>>;
fn save_loose_commit(
&self,
loose_commit: LooseCommit,
) -> K::Future<'_, Result<(), Self::Error>>;
fn save_fragment(&self, fragment: Fragment) -> K::Future<'_, Result<(), Self::Error>>;
fn load_fragments(&self) -> K::Future<'_, Result<Vec<Fragment>, Self::Error>>;
fn save_blob(&self, blob: Blob) -> K::Future<'_, Result<Digest, Self::Error>>;
fn load_blob(&self, blob_digest: Digest) -> K::Future<'_, Result<Option<Blob>, Self::Error>>;
}
#[derive(Debug, thiserror::Error)]
pub enum LoadTreeData {
#[error("error from storage: {0}")]
Storage(String),
#[error("missing blob: {0}")]
MissingBlob(Digest),
}
#[derive(Debug, Clone, Default)]
pub struct MemoryStorage {
fragments: Arc<Mutex<HashMap<Digest, Fragment>>>,
commits: Arc<Mutex<HashMap<Digest, LooseCommit>>>,
blobs: Arc<Mutex<HashMap<Digest, Blob>>>,
}
impl Storage<Sendable> for MemoryStorage {
type Error = std::convert::Infallible;
fn load_loose_commits(&self) -> BoxFuture<'_, Result<Vec<LooseCommit>, Self::Error>> {
async move {
let commits = self.commits.lock().await.values().cloned().collect();
Ok(commits)
}
.boxed()
}
fn save_loose_commit(
&self,
loose_commit: LooseCommit,
) -> BoxFuture<'_, Result<(), Self::Error>> {
async move {
let digest = loose_commit.blob().digest();
self.commits.lock().await.insert(digest, loose_commit);
Ok(())
}
.boxed()
}
fn save_fragment(&self, fragment: Fragment) -> BoxFuture<'_, Result<(), Self::Error>> {
async move {
let digest = fragment.summary().blob_meta().digest();
self.fragments.lock().await.insert(digest, fragment);
Ok(())
}
.boxed()
}
fn load_fragments(&self) -> BoxFuture<'_, Result<Vec<Fragment>, Self::Error>> {
async move {
let fragments = self.fragments.lock().await.values().cloned().collect();
Ok(fragments)
}
.boxed()
}
fn save_blob(&self, blob: Blob) -> BoxFuture<'_, Result<Digest, Self::Error>> {
async move {
let digest = Digest::hash(blob.contents());
self.blobs.lock().await.insert(digest, blob);
Ok(digest)
}
.boxed()
}
fn load_blob(&self, blob_digest: Digest) -> BoxFuture<'_, Result<Option<Blob>, Self::Error>> {
async move {
let maybe_blob = self.blobs.lock().await.get(&blob_digest).cloned();
Ok(maybe_blob)
}
.boxed()
}
}
impl Storage<Local> for MemoryStorage {
type Error = std::convert::Infallible;
fn load_loose_commits(&self) -> LocalBoxFuture<'_, Result<Vec<LooseCommit>, Self::Error>> {
async move {
let commits = self.commits.lock().await.values().cloned().collect();
Ok(commits)
}
.boxed_local()
}
fn save_loose_commit(
&self,
loose_commit: LooseCommit,
) -> LocalBoxFuture<'_, Result<(), Self::Error>> {
async move {
let digest = loose_commit.blob().digest();
self.commits.lock().await.insert(digest, loose_commit);
Ok(())
}
.boxed_local()
}
fn save_fragment(&self, fragment: Fragment) -> LocalBoxFuture<'_, Result<(), Self::Error>> {
async move {
let digest = fragment.summary().blob_meta().digest();
self.fragments.lock().await.insert(digest, fragment);
Ok(())
}
.boxed_local()
}
fn load_fragments(&self) -> LocalBoxFuture<'_, Result<Vec<Fragment>, Self::Error>> {
async move {
let fragments = self.fragments.lock().await.values().cloned().collect();
Ok(fragments)
}
.boxed_local()
}
fn save_blob(&self, blob: Blob) -> LocalBoxFuture<'_, Result<Digest, Self::Error>> {
async move {
let digest = Digest::hash(blob.contents());
self.blobs.lock().await.insert(digest, blob);
Ok(digest)
}
.boxed_local()
}
fn load_blob(
&self,
blob_digest: Digest,
) -> LocalBoxFuture<'_, Result<Option<Blob>, Self::Error>> {
async move {
let maybe_blob = self.blobs.lock().await.get(&blob_digest).cloned();
Ok(maybe_blob)
}
.boxed_local()
}
}