mithril_cardano_node_internal_database/digesters/cache/
provider.rs

1use std::collections::BTreeMap;
2use std::io;
3
4use async_trait::async_trait;
5use thiserror::Error;
6
7use mithril_common::StdError;
8use mithril_common::entities::{HexEncodedDigest, ImmutableFileName};
9
10use crate::entities::ImmutableFile;
11
12/// A specialized result type for [ImmutableFileDigestCacheProvider].
13pub type CacheProviderResult<T> = Result<T, ImmutableDigesterCacheProviderError>;
14
15/// [ImmutableFileDigestCacheProvider] related errors.
16#[derive(Error, Debug)]
17pub enum ImmutableDigesterCacheProviderError {
18    /// Error raised by [ImmutableFileDigestCacheProvider::store].
19    #[error("Could not store immutable file digests cache")]
20    Store(#[from] ImmutableDigesterCacheStoreError),
21
22    /// Error raised by [ImmutableFileDigestCacheProvider::get].
23    #[error("Could not read immutable file digests cache")]
24    Get(#[from] ImmutableDigesterCacheGetError),
25}
26
27/// [ImmutableFileDigestCacheProvider::store] related errors.
28#[derive(Error, Debug)]
29pub enum ImmutableDigesterCacheStoreError {
30    /// Raised when an IO error is raised when storing a cache.
31    #[error("IO error when storing cache")]
32    Io(#[from] io::Error),
33
34    /// Raised when json cache serialization fails.
35    #[error("IO error when serializing json cache")]
36    JsonSerialization(#[from] serde_json::Error),
37
38    /// Raised when the underlying store failed storing a cache.
39    #[error("Underlying store raised an error when storing cache")]
40    StoreError(#[source] StdError),
41}
42
43/// [ImmutableFileDigestCacheProvider::get] related errors.
44#[derive(Error, Debug)]
45pub enum ImmutableDigesterCacheGetError {
46    /// Raised when an IO error is raised when getting a cache.
47    #[error("IO error when getting cache")]
48    Io(#[from] io::Error),
49
50    /// Raised when json cache deserialization fails.
51    #[error("IO error when deserializing json cache")]
52    JsonDeserialization(#[from] serde_json::Error),
53
54    /// Raised when the underlying store failed getting a cache.
55    #[error("Underlying store raised an error when getting cache")]
56    StoreError(#[source] StdError),
57}
58
59/// A cache provider that store individual [ImmutableFile] digests.
60#[cfg_attr(test, mockall::automock)]
61#[async_trait]
62pub trait ImmutableFileDigestCacheProvider: Sync + Send {
63    /// Store the given digests
64    async fn store(
65        &self,
66        digest_per_filenames: Vec<(ImmutableFileName, HexEncodedDigest)>,
67    ) -> CacheProviderResult<()>;
68
69    /// Associate each given [immutable files][ImmutableFile] with a cached value if one exist.
70    async fn get(
71        &self,
72        immutables: Vec<ImmutableFile>,
73    ) -> CacheProviderResult<BTreeMap<ImmutableFile, Option<HexEncodedDigest>>>;
74
75    /// Reset the stored values
76    async fn reset(&self) -> CacheProviderResult<()>;
77}