1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::fmt::Debug;

use fuel_core_storage::{
    structured_storage::TableWithBlueprint,
    Mappable,
};
use itertools::Itertools;

use crate::{
    config::table_entry::TableEntry,
    AsTable,
    ChainConfig,
    LastBlockConfig,
    StateConfig,
    MAX_GROUP_SIZE,
};

pub struct Groups<T: Mappable> {
    iter: GroupIter<T>,
}

impl<T> Groups<T>
where
    T: Mappable,
{
    pub fn len(&self) -> usize {
        match &self.iter {
            GroupIter::InMemory { groups } => groups.len(),
            #[cfg(feature = "parquet")]
            GroupIter::Parquet { decoder } => decoder.num_groups(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<T> IntoIterator for Groups<T>
where
    T: Mappable,
    GroupIter<T>: Iterator,
{
    type IntoIter = GroupIter<T>;
    type Item = <Self::IntoIter as Iterator>::Item;

    fn into_iter(self) -> Self::IntoIter {
        self.iter
    }
}

pub enum GroupIter<T>
where
    T: Mappable,
{
    InMemory {
        groups: std::vec::IntoIter<anyhow::Result<Vec<TableEntry<T>>>>,
    },
    #[cfg(feature = "parquet")]
    Parquet {
        decoder: super::parquet::decode::Decoder<std::fs::File>,
    },
}

#[cfg(feature = "parquet")]
impl<T> Iterator for GroupIter<T>
where
    T: Mappable,
    TableEntry<T>: serde::de::DeserializeOwned,
{
    type Item = anyhow::Result<Vec<TableEntry<T>>>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            GroupIter::InMemory { groups } => groups.next(),
            GroupIter::Parquet { decoder } => {
                let group = decoder.next()?.and_then(|byte_group| {
                    byte_group
                        .into_iter()
                        .map(|group| {
                            postcard::from_bytes(&group).map_err(|e| anyhow::anyhow!(e))
                        })
                        .collect()
                });
                Some(group)
            }
        }
    }
}

#[cfg(not(feature = "parquet"))]
impl<T> Iterator for GroupIter<T>
where
    T: Mappable,
{
    type Item = anyhow::Result<Vec<TableEntry<T>>>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            GroupIter::InMemory { groups } => groups.next(),
        }
    }
}

#[derive(Clone, Debug)]
enum DataSource {
    #[cfg(feature = "parquet")]
    Parquet {
        tables: std::collections::HashMap<String, std::path::PathBuf>,
        latest_block_config: Option<LastBlockConfig>,
    },
    InMemory {
        state: StateConfig,
        group_size: usize,
    },
}

#[derive(Clone, Debug)]
pub struct SnapshotReader {
    chain_config: ChainConfig,
    data_source: DataSource,
}

impl SnapshotReader {
    pub fn new_in_memory(chain_config: ChainConfig, state: StateConfig) -> Self {
        Self {
            chain_config,
            data_source: DataSource::InMemory {
                state,
                group_size: MAX_GROUP_SIZE,
            },
        }
    }

    #[cfg(feature = "test-helpers")]
    pub fn local_testnet() -> Self {
        let state = StateConfig::local_testnet();
        let chain_config = ChainConfig::local_testnet();
        Self::new_in_memory(chain_config, state)
    }

    pub fn with_chain_config(self, chain_config: ChainConfig) -> Self {
        Self {
            chain_config,
            ..self
        }
    }

    pub fn with_state_config(self, state_config: StateConfig) -> Self {
        Self {
            data_source: DataSource::InMemory {
                state: state_config,
                group_size: MAX_GROUP_SIZE,
            },
            ..self
        }
    }

    #[cfg(feature = "std")]
    fn json(
        state_file: impl AsRef<std::path::Path>,
        chain_config: ChainConfig,
        group_size: usize,
    ) -> anyhow::Result<Self> {
        use anyhow::Context;
        use std::io::Read;
        let state = {
            let path = state_file.as_ref();
            let mut json = String::new();
            std::fs::File::open(path)
                .with_context(|| format!("Could not open snapshot file: {path:?}"))?
                .read_to_string(&mut json)?;
            serde_json::from_str(json.as_str())?
        };

        Ok(Self {
            data_source: DataSource::InMemory { state, group_size },
            chain_config,
        })
    }

    #[cfg(feature = "parquet")]
    fn parquet(
        tables: std::collections::HashMap<String, std::path::PathBuf>,
        latest_block_config: std::path::PathBuf,
        chain_config: ChainConfig,
    ) -> anyhow::Result<Self> {
        let latest_block_config = Self::read_config(&latest_block_config)?;
        Ok(Self {
            data_source: DataSource::Parquet {
                tables,
                latest_block_config,
            },
            chain_config,
        })
    }

    #[cfg(feature = "parquet")]
    fn read_config<Config>(path: &std::path::Path) -> anyhow::Result<Config>
    where
        Config: serde::de::DeserializeOwned,
    {
        use super::parquet::decode::Decoder;

        let file = std::fs::File::open(path)?;
        let group = Decoder::new(file)?
            .next()
            .ok_or_else(|| anyhow::anyhow!("No block height found"))??;
        let config = group
            .into_iter()
            .next()
            .ok_or_else(|| anyhow::anyhow!("No config found"))?;
        postcard::from_bytes(&config).map_err(Into::into)
    }

    #[cfg(feature = "std")]
    pub fn open(
        snapshot_metadata: crate::config::SnapshotMetadata,
    ) -> anyhow::Result<Self> {
        Self::open_w_config(snapshot_metadata, MAX_GROUP_SIZE)
    }

    #[cfg(feature = "std")]
    pub fn open_w_config(
        snapshot_metadata: crate::config::SnapshotMetadata,
        json_group_size: usize,
    ) -> anyhow::Result<Self> {
        use crate::TableEncoding;
        let chain_config = ChainConfig::from_snapshot_metadata(&snapshot_metadata)?;

        match snapshot_metadata.table_encoding {
            TableEncoding::Json { filepath } => {
                Self::json(filepath, chain_config, json_group_size)
            }
            #[cfg(feature = "parquet")]
            TableEncoding::Parquet {
                tables,
                latest_block_config_path,
                ..
            } => Self::parquet(tables, latest_block_config_path, chain_config),
        }
    }

    pub fn read<T>(&self) -> anyhow::Result<Groups<T>>
    where
        T: TableWithBlueprint,
        StateConfig: AsTable<T>,
        TableEntry<T>: serde::de::DeserializeOwned,
    {
        let iter = match &self.data_source {
            #[cfg(feature = "parquet")]
            DataSource::Parquet { tables, .. } => {
                use anyhow::Context;
                use fuel_core_storage::kv_store::StorageColumn;
                let name = T::column().name();
                let Some(path) = tables.get(name) else {
                    return Ok(Groups {
                        iter: GroupIter::InMemory {
                            groups: vec![].into_iter(),
                        },
                    });
                };
                let file = std::fs::File::open(path).with_context(|| {
                    format!("Could not open {path:?} in order to read table '{name}'")
                })?;

                GroupIter::Parquet {
                    decoder: super::parquet::decode::Decoder::new(file)?,
                }
            }
            DataSource::InMemory { state, group_size } => {
                let collection = state
                    .as_table()
                    .into_iter()
                    .chunks(*group_size)
                    .into_iter()
                    .map(|vec_chunk| Ok(vec_chunk.collect()))
                    .collect_vec();
                GroupIter::InMemory {
                    groups: collection.into_iter(),
                }
            }
        };

        Ok(Groups { iter })
    }

    pub fn chain_config(&self) -> &ChainConfig {
        &self.chain_config
    }

    pub fn last_block_config(&self) -> Option<&LastBlockConfig> {
        match &self.data_source {
            DataSource::InMemory { state, .. } => state.last_block.as_ref(),
            #[cfg(feature = "parquet")]
            DataSource::Parquet {
                latest_block_config: block,
                ..
            } => block.as_ref(),
        }
    }
}