use crate::{
Mappable,
Result as StorageResult,
codec::{
Decode,
Encode,
Encoder,
},
kv_store::{
BatchOperations,
KeyValueInspect,
KeyValueMutate,
},
};
use fuel_vm_private::prelude::MerkleRoot;
pub mod merklized;
pub mod plain;
pub mod sparse;
pub trait BlueprintCodec<M>
where
M: Mappable,
{
type KeyCodec: Encode<M::Key> + Decode<M::OwnedKey>;
type ValueCodec: Encode<M::Value> + Decode<M::OwnedValue>;
}
pub trait BlueprintInspect<M, S>: BlueprintCodec<M>
where
M: Mappable,
S: KeyValueInspect,
{
fn exists(storage: &S, key: &M::Key, column: S::Column) -> StorageResult<bool> {
let key_encoder = Self::KeyCodec::encode(key);
let key_bytes = key_encoder.as_bytes();
storage.exists(key_bytes.as_ref(), column)
}
fn size_of_value(
storage: &S,
key: &M::Key,
column: S::Column,
) -> StorageResult<Option<usize>> {
let key_encoder = Self::KeyCodec::encode(key);
let key_bytes = key_encoder.as_bytes();
storage.size_of_value(key_bytes.as_ref(), column)
}
fn get(
storage: &S,
key: &M::Key,
column: S::Column,
) -> StorageResult<Option<M::OwnedValue>> {
let key_encoder = Self::KeyCodec::encode(key);
let key_bytes = key_encoder.as_bytes();
storage
.get(key_bytes.as_ref(), column)?
.map(|value| {
Self::ValueCodec::decode_from_value(value).map_err(crate::Error::Codec)
})
.transpose()
}
}
pub trait BlueprintMutate<M, S>: BlueprintInspect<M, S>
where
M: Mappable,
S: KeyValueMutate,
{
fn put(
storage: &mut S,
key: &M::Key,
column: S::Column,
value: &M::Value,
) -> StorageResult<()>;
fn replace(
storage: &mut S,
key: &M::Key,
column: S::Column,
value: &M::Value,
) -> StorageResult<Option<M::OwnedValue>>;
fn take(
storage: &mut S,
key: &M::Key,
column: S::Column,
) -> StorageResult<Option<M::OwnedValue>>;
fn delete(storage: &mut S, key: &M::Key, column: S::Column) -> StorageResult<()>;
}
pub trait SupportsBatching<M, S>: BlueprintMutate<M, S>
where
M: Mappable,
S: BatchOperations,
{
fn init<'a, Iter>(storage: &mut S, column: S::Column, set: Iter) -> StorageResult<()>
where
Iter: 'a + Iterator<Item = (&'a M::Key, &'a M::Value)>,
M::Key: 'a,
M::Value: 'a;
fn insert<'a, Iter>(
storage: &mut S,
column: S::Column,
set: Iter,
) -> StorageResult<()>
where
Iter: 'a + Iterator<Item = (&'a M::Key, &'a M::Value)>,
M::Key: 'a,
M::Value: 'a;
fn remove<'a, Iter>(
storage: &mut S,
column: S::Column,
set: Iter,
) -> StorageResult<()>
where
Iter: 'a + Iterator<Item = &'a M::Key>,
M::Key: 'a;
}
pub trait SupportsMerkle<Key, M, S>: BlueprintInspect<M, S>
where
Key: ?Sized,
M: Mappable,
S: KeyValueInspect,
{
fn root(storage: &S, key: &Key) -> StorageResult<MerkleRoot>;
}