Skip to main content

fuel_core/state/
data_source.rs

1use crate::{
2    database::database_description::DatabaseDescription,
3    state::TransactableStorage,
4};
5use fuel_core_storage::{
6    Result as StorageResult,
7    StorageReadError,
8    iter::{
9        BoxedIter,
10        IterDirection,
11        IterableStore,
12    },
13    kv_store::{
14        KVItem,
15        KeyValueInspect,
16        Value,
17    },
18};
19use std::sync::Arc;
20
21#[allow(type_alias_bounds)]
22pub type DataSourceType<Description>
23where
24    Description: DatabaseDescription,
25= Arc<dyn TransactableStorage<Description::Height, Column = Description::Column>>;
26
27#[derive(Debug, Clone)]
28pub struct DataSource<Description, Stage>
29where
30    Description: DatabaseDescription,
31{
32    pub data: DataSourceType<Description>,
33    pub stage: Stage,
34}
35
36impl<Description, Stage> DataSource<Description, Stage>
37where
38    Description: DatabaseDescription,
39{
40    pub fn new(data: DataSourceType<Description>, stage: Stage) -> Self {
41        Self { data, stage }
42    }
43}
44
45impl<Description, Stage> KeyValueInspect for DataSource<Description, Stage>
46where
47    Description: DatabaseDescription,
48{
49    type Column = Description::Column;
50
51    fn exists(&self, key: &[u8], column: Self::Column) -> StorageResult<bool> {
52        self.data.exists(key, column)
53    }
54
55    fn size_of_value(
56        &self,
57        key: &[u8],
58        column: Self::Column,
59    ) -> StorageResult<Option<usize>> {
60        self.data.size_of_value(key, column)
61    }
62
63    fn get(&self, key: &[u8], column: Self::Column) -> StorageResult<Option<Value>> {
64        self.data.get(key, column)
65    }
66
67    fn read_exact(
68        &self,
69        key: &[u8],
70        column: Self::Column,
71        offset: usize,
72        buf: &mut [u8],
73    ) -> StorageResult<Result<usize, StorageReadError>> {
74        self.data.read_exact(key, column, offset, buf)
75    }
76
77    fn read_zerofill(
78        &self,
79        key: &[u8],
80        column: Self::Column,
81        offset: usize,
82        buf: &mut [u8],
83    ) -> StorageResult<Result<usize, StorageReadError>> {
84        self.data.read_zerofill(key, column, offset, buf)
85    }
86}
87
88impl<Description, Stage> IterableStore for DataSource<Description, Stage>
89where
90    Description: DatabaseDescription,
91{
92    fn iter_store(
93        &self,
94        column: Self::Column,
95        prefix: Option<&[u8]>,
96        start: Option<&[u8]>,
97        direction: IterDirection,
98    ) -> BoxedIter<'_, KVItem> {
99        self.data.iter_store(column, prefix, start, direction)
100    }
101
102    fn iter_store_keys(
103        &self,
104        column: Self::Column,
105        prefix: Option<&[u8]>,
106        start: Option<&[u8]>,
107        direction: IterDirection,
108    ) -> BoxedIter<'_, fuel_core_storage::kv_store::KeyItem> {
109        self.data.iter_store_keys(column, prefix, start, direction)
110    }
111}