bee_storage/backend.rs
1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module forms the backend layer which holds the contracts of starting and shutting down the backend, as well as
5//! accessing backend properties.
6
7use serde::de::DeserializeOwned;
8
9use crate::system::StorageHealth;
10
11/// Trait to be implemented on a storage backend.
12/// Determines how to start and shutdown the backend.
13pub trait StorageBackend: Send + Sized + Sync + 'static {
14 /// Helps build the associated `Config`.
15 type ConfigBuilder: Default + DeserializeOwned + Into<Self::Config>;
16 /// Holds the backend options.
17 type Config: Clone + Send + Sync;
18 /// Returned on failed operations.
19 type Error: std::error::Error + Send;
20
21 /// Initializes and starts the backend.
22 fn start(config: Self::Config) -> Result<Self, Self::Error>;
23
24 /// Shutdowns the backend.
25 fn shutdown(self) -> Result<(), Self::Error>;
26
27 /// Returns the size of the database in bytes.
28 /// Not all backends may be able to provide this operation.
29 fn size(&self) -> Result<Option<usize>, Self::Error>;
30
31 /// Returns the health status of the database.
32 /// Not all backends may be able to provide this operation.
33 fn get_health(&self) -> Result<Option<StorageHealth>, Self::Error>;
34
35 /// Sets the health status of the database.
36 /// Not all backends may be able to provide this operation.
37 fn set_health(&self, health: StorageHealth) -> Result<(), Self::Error>;
38}