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
use super::{ChunkDesc, DataBuilder};
use crate::DataSource;
use hff_core::{Chunk, Ecc, Error, Header, Result, Table};
use std::{fmt::Debug, mem::size_of};

/// Helper structure to build a flattened table tree.
pub(crate) struct Flattened {
    /// The root tree at this level.
    pub root: Table,
    /// The children of this tree.
    pub children: Vec<Table>,
    /// The metadata and chunk data for the root and children.
    pub data_builder: DataBuilder,
    /// The tracking information for building the chunk array.
    /// (primary, secondary, length, offset)
    pub chunks: Vec<Chunk>,
}

impl Debug for Flattened {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("Flattened");
        s.field("0: ", &self.root);
        for (index, child) in self.children.iter().enumerate() {
            s.field(&(index + 1).to_string(), &child);
        }
        for (index, chunk) in self.chunks.iter().enumerate() {
            s.field(&index.to_string(), chunk);
        }
        s.field("Data", &self.data_builder);
        s.finish()
    }
}

impl Flattened {
    /// Create a new flattened structure with the given
    /// information.
    pub fn with(
        root: Table,
        children: Vec<Table>,
        chunks: Vec<Chunk>,
        data_builder: DataBuilder,
    ) -> Self {
        Self {
            root,
            children,
            data_builder,
            chunks,
        }
    }

    /// Finish the structure.
    pub fn finish(mut self) -> (Vec<Table>, Vec<Chunk>, DataBuilder) {
        // Adjust root to take into account for itself in regards to siblings.
        *self.root.sibling_mut() += 1;

        // Finish flattening.
        let mut tables = vec![self.root];
        tables.extend(self.children);

        // Get the size of the header data before the chunk data.
        let offset = size_of::<Header>()
            + size_of::<Table>() * tables.len()
            + size_of::<Chunk>() * self.chunks.len();

        // Adjust all offsets in tables and chunks to account for the
        // header.
        for table in &mut tables {
            *table.metadata_offset_mut() += offset as u64;
        }
        for chunk in &mut self.chunks {
            *chunk.offset_mut() += offset as u64;
        }

        // Return it all.
        (tables, self.chunks, self.data_builder)
    }
}

/// A table description.
#[derive(Debug)]
pub struct TableDesc {
    /// The primary identifier of this table.
    primary: Ecc,
    /// The secondary identifier of this table.
    secondary: Ecc,
    /// The tables which are children of this table.
    children: Vec<TableDesc>,
    /// The chunks attached to this table.
    chunks: Vec<ChunkDesc>,
    /// The metadata associated with the table.
    metadata: Option<Box<dyn DataSource>>,
}

impl TableDesc {
    /// Create a new table description.
    pub fn new(primary: Ecc, secondary: Ecc) -> Self {
        Self {
            primary,
            secondary,
            children: vec![],
            chunks: vec![],
            metadata: None,
        }
    }

    /// Get the table count.
    pub fn table_count(&self) -> usize {
        let mut count = 0;
        for child in &self.children {
            count += child.table_count();
        }
        // +1 for the table which self itself represents.
        count + 1
    }

    /// Push a child table.
    pub(crate) fn push_table(&mut self, child: TableDesc) {
        self.children.push(child);
    }

    /// Get the chunk count.
    pub fn chunk_count(&self) -> usize {
        let mut count = self.chunks.len();
        for child in &self.children {
            count += child.chunk_count();
        }
        count
    }

    /// Push a chunk onto the table.
    pub(crate) fn push_chunk(&mut self, chunk: ChunkDesc) {
        self.chunks.push(chunk);
    }

    /// Add metadata.
    pub fn metadata(&mut self, data_source: Box<dyn DataSource>) -> Result<()> {
        if self.metadata.is_none() {
            self.metadata = Some(data_source);
            Ok(())
        } else {
            Err(Error::Invalid(
                "Tables can only contain a single metadata entry.".into(),
            ))
        }
    }

    /// Flatten the table hierarchy.
    pub(crate) fn flatten_tables(self) -> Result<Flattened> {
        // Create a new flattened structure to contain this table and
        // its children.
        let mut data_builder = DataBuilder::new();
        let metadata_length = if let Some(mut metadata) = self.metadata {
            let length = if let Some(length) = metadata.len() {
                length
            } else {
                metadata.prepare()?
            };
            data_builder.push(metadata, length);
            length
        } else {
            0
        };

        // Push the chunk data onto the flattened structure and
        // add to the builder.
        let chunk_count = self.chunks.len();
        let mut chunks = vec![];
        for chunk in self.chunks {
            let primary = chunk.primary();
            let secondary = chunk.secondary();
            let mut data = chunk.data();
            let length = if let Some(length) = data.len() {
                length
            } else {
                data.prepare()?
            };

            // Push the chunk entry.
            chunks.push(Chunk::new(
                primary,
                secondary,
                length,
                data_builder.size_bytes(),
            ));
            // Push the data.
            data_builder.push(data, length);
        }

        // Split the children and their related children.
        let mut children = vec![];
        let child_count = self.children.len();
        for child in self.children {
            // Flatten the children completely.
            let mut child_data = child.flatten_tables()?;

            // Update the root sibling.
            *child_data.root.sibling_mut() = (1 + child_data.children.len()) as u32;

            // Offset and push the root.
            child_data
                .root
                .offset(data_builder.size_bytes(), chunks.len() as u32);
            children.push(child_data.root);

            // Offset the children.
            for table in &mut child_data.children {
                table.offset(data_builder.size_bytes(), chunks.len() as u32);
            }
            // Append the children.
            children.append(&mut child_data.children);

            // Update the child chunk offsets.
            for chunk in &mut child_data.chunks {
                *chunk.offset_mut() += data_builder.size_bytes();
            }

            // Append the child chunks to the total chunks.
            chunks.append(&mut child_data.chunks);

            // Append the child data to the current data.
            data_builder.append(child_data.data_builder);
        }

        Ok(Flattened::with(
            Table::create()
                .primary(self.primary)
                .secondary(self.secondary)
                .metadata_length(metadata_length)
                .metadata_offset(0)
                .child_count(child_count as u32)
                .sibling(children.len() as u32)
                .chunk_count(chunk_count as u32)
                .chunk_index(0)
                .end(),
            children,
            chunks,
            data_builder,
        ))
    }
}