pub struct BufferManager<'a, V> { /* private fields */ }
Implementations§
Source§impl<'a, V> BufferManager<'a, V>where
V: Serialize + DeserializeOwned + 'a,
impl<'a, V> BufferManager<'a, V>where
V: Serialize + DeserializeOwned + 'a,
Sourcepub fn init(
store: &mut dyn Storage,
store_iface: CircularBuffer<'a, V>,
capacity: u32,
) -> BufferResult<()>
pub fn init( store: &mut dyn Storage, store_iface: CircularBuffer<'a, V>, capacity: u32, ) -> BufferResult<()>
Static function to initialize buffer in storage. Intended to be called during contract initialization.
Sourcepub fn new(
store: &dyn Storage,
store_iface: CircularBuffer<'a, V>,
) -> BufferResult<Self>
pub fn new( store: &dyn Storage, store_iface: CircularBuffer<'a, V>, ) -> BufferResult<Self>
Initialize buffer manager.
In case buffer is not initialized it throws BufferError::BufferNotInitialized
error.
Sourcepub fn instant_push(
&mut self,
store: &mut dyn Storage,
value: &'a V,
) -> BufferResult<()>
pub fn instant_push( &mut self, store: &mut dyn Storage, value: &'a V, ) -> BufferResult<()>
Push value to precommit buffer and commit it to storage.
Sourcepub fn commit(&mut self, store: &mut dyn Storage) -> BufferResult<()>
pub fn commit(&mut self, store: &mut dyn Storage) -> BufferResult<()>
Commit in storage current state and precommit buffer. Buffer is erased after commit.
Sourcepub fn read(
&self,
store: &dyn Storage,
indexes: impl IntoIterator<Item = impl Into<u32> + Display>,
stop_if_empty: bool,
) -> BufferResult<Vec<V>>
pub fn read( &self, store: &dyn Storage, indexes: impl IntoIterator<Item = impl Into<u32> + Display>, stop_if_empty: bool, ) -> BufferResult<Vec<V>>
Read values from storage by indexes. If stop_if_empty
is true,
reading will stop when first empty value is encountered.
Otherwise, BufferError::IndexNotFound
error will be thrown.
§Examples:
let values = buffer.read(&store, 0u32..=9, false).unwrap();
let values = buffer.read(&store, vec![0u32, 5, 7], false).unwrap();
let values = buffer.read(&store, (0u32..buffer.capacity()).step_by(2), false).unwrap();
Sourcepub fn read_all(&self, store: &dyn Storage) -> BufferResult<Vec<V>>
pub fn read_all(&self, store: &dyn Storage) -> BufferResult<Vec<V>>
Read all available values from storage.
Sourcepub fn read_last(&self, store: &dyn Storage) -> BufferResult<Option<V>>
pub fn read_last(&self, store: &dyn Storage) -> BufferResult<Option<V>>
Read last saved value from storage. Returns None if buffer is empty.
Sourcepub fn read_single(
&self,
store: &dyn Storage,
index: impl Into<u32>,
) -> BufferResult<Option<V>>
pub fn read_single( &self, store: &dyn Storage, index: impl Into<u32>, ) -> BufferResult<Option<V>>
Looped read. Returns None if value in buffer does not exist.
Sourcepub fn clear_buffer(&self, store: &mut dyn Storage)
pub fn clear_buffer(&self, store: &mut dyn Storage)
This operation is gas consuming. However, it might be helpful in rare cases.