Skip to main content

ic_storage/adapter/
log.rs

1//! Governed log collection implementation.
2use super::{LogIter, LogWriteError, Memory, Storable};
3use crate::{DefaultMemoryImpl, RuntimeMemory, substrate};
4
5///
6/// StableLog
7/// Governed upstream `Log`; initialization and failures follow upstream contracts.
8///
9
10pub struct StableLog<T: Storable, M: Memory = DefaultMemoryImpl> {
11    inner: substrate::Log<T, RuntimeMemory<M>, RuntimeMemory<M>>,
12}
13
14impl<T: Storable, M: Memory> StableLog<T, M> {
15    /// Create a log only when both admitted memories have zero pages.
16    ///
17    /// # Panics
18    /// Refuses either nonempty memory before writing. Growth failures retain
19    /// upstream behavior.
20    #[must_use]
21    pub fn create(index_memory: RuntimeMemory<M>, data_memory: RuntimeMemory<M>) -> Self {
22        assert_eq!(index_memory.size(), 0, "log index is not empty");
23        assert_eq!(data_memory.size(), 0, "log data is not empty");
24
25        Self::reset(index_memory, data_memory)
26    }
27
28    /// Deliberately replace both admitted log allocations.
29    ///
30    /// # Panics
31    /// Growth and backing failures retain upstream behavior.
32    #[must_use]
33    pub fn reset(index_memory: RuntimeMemory<M>, data_memory: RuntimeMemory<M>) -> Self {
34        Self {
35            inner: substrate::Log::new(index_memory, data_memory),
36        }
37    }
38
39    /// Load a retained log without initializing either allocation.
40    ///
41    /// # Panics
42    /// Refuses empty/foreign memory and incompatible layouts without writing.
43    /// Backing and collection corruption failures retain upstream behavior.
44    #[must_use]
45    pub fn load(index_memory: RuntimeMemory<M>, data_memory: RuntimeMemory<M>) -> Self {
46        assert!(
47            index_memory.size() > 0 && data_memory.size() > 0,
48            "log memory is empty"
49        );
50        let mut magic = [0; 3];
51        data_memory.read(0, &mut magic);
52        assert_eq!(&magic, b"GLD", "foreign log data");
53
54        // With recognized data magic upstream refuses every other incompatible
55        // header before writes, including invalid/missing index magic.
56        Self {
57            inner: substrate::Log::init(index_memory, data_memory),
58        }
59    }
60
61    /// Delegates to upstream `Log::first` with the governed allocation.
62    #[must_use]
63    pub fn first(&self) -> Option<T> {
64        self.inner.first()
65    }
66
67    /// Delegates to upstream `Log::last` with the governed allocation.
68    #[must_use]
69    pub fn last(&self) -> Option<T> {
70        self.inner.last()
71    }
72
73    /// Delegates to upstream `Log::get` with the governed allocation.
74    #[must_use]
75    pub fn get(&self, idx: u64) -> Option<T> {
76        self.inner.get(idx)
77    }
78
79    /// Delegates to upstream `Log::len` with the governed allocation.
80    #[must_use]
81    pub fn len(&self) -> u64 {
82        self.inner.len()
83    }
84
85    /// Delegates to upstream `Log::is_empty` with the governed allocation.
86    #[must_use]
87    pub fn is_empty(&self) -> bool {
88        self.inner.is_empty()
89    }
90
91    /// Delegates to upstream `Log::append` with the governed allocation.
92    /// # Errors
93    /// Returns the upstream write error if memory cannot grow.
94    pub fn append(&self, item: &T) -> Result<u64, LogWriteError> {
95        self.inner.append(item)
96    }
97
98    /// Delegates to upstream `Log::iter` with the governed allocation.
99    #[must_use]
100    pub fn iter(&self) -> LogIter<'_, T, M> {
101        self.inner.iter()
102    }
103}
104
105impl<'a, T: Storable, M: Memory> IntoIterator for &'a StableLog<T, M> {
106    type Item = T;
107    type IntoIter = LogIter<'a, T, M>;
108    fn into_iter(self) -> Self::IntoIter {
109        self.iter()
110    }
111}