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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*!
Defining the [Table] struct that represents an iceberg table.
*/

use std::{collections::HashMap, io::Cursor, sync::Arc, time::SystemTime};

use anyhow::{anyhow, Result};
use apache_avro::types::Value as AvroValue;
use futures::StreamExt;
use object_store::{path::Path, ObjectStore};

use crate::{
    catalog::{identifier::Identifier, Catalog},
    model::{
        data_types::StructType,
        manifest_list::{ManifestFile, ManifestFileV1, ManifestFileV2},
        snapshot::{Operation, SnapshotV1, SnapshotV2, Summary},
        table_metadata::{FormatVersion, TableMetadata},
    },
    table::transaction::TableTransaction,
    util,
};

pub mod files;
pub mod table_builder;
pub mod transaction;

/// Tables can be either one of following types:
/// - FileSystem(https://iceberg.apache.org/spec/#file-system-tables)
/// - Metastore(https://iceberg.apache.org/spec/#metastore-tables)
pub enum TableType {
    /// Filesystem table
    FileSystem(Arc<dyn ObjectStore>),
    /// Metastore table
    Metastore(Identifier, Arc<dyn Catalog>),
}

/// Iceberg table
pub struct Table {
    table_type: TableType,
    metadata: TableMetadata,
    metadata_location: String,
    manifests: Vec<ManifestFile>,
}

/// Public interface of the table.
impl Table {
    /// Create a new metastore Table
    pub async fn new_metastore_table(
        identifier: Identifier,
        catalog: Arc<dyn Catalog>,
        metadata: TableMetadata,
        metadata_location: &str,
    ) -> Result<Self> {
        let manifests = get_manifests(&metadata, catalog.object_store()).await?;
        Ok(Table {
            table_type: TableType::Metastore(identifier, catalog),
            metadata,
            metadata_location: metadata_location.to_string(),
            manifests,
        })
    }
    /// Load a filesystem table from an objectstore
    pub async fn load_file_system_table(
        location: &str,
        object_store: &Arc<dyn ObjectStore>,
    ) -> Result<Self> {
        let path: Path = (location.to_string() + "/metadata/").into();
        let files = object_store
            .list(Some(&path))
            .await
            .map_err(|err| anyhow!(err.to_string()))?;
        let version = files
            .fold(Ok::<i64, anyhow::Error>(0), |acc, x| async move {
                match (acc, x) {
                    (Ok(acc), Ok(object_meta)) => {
                        let name = object_meta
                            .location
                            .parts()
                            .last()
                            .ok_or_else(|| anyhow!("Metadata location path is empty."))?;
                        if name.as_ref().ends_with(".metadata.json") {
                            let version: i64 = name
                                .as_ref()
                                .trim_start_matches('v')
                                .trim_end_matches(".metadata.json")
                                .parse()?;
                            if version > acc {
                                Ok(version)
                            } else {
                                Ok(acc)
                            }
                        } else {
                            Ok(acc)
                        }
                    }
                    (Err(err), _) => Err(anyhow!(err.to_string())),
                    (_, Err(err)) => Err(anyhow!(err.to_string())),
                }
            })
            .await?;
        let metadata_location = path.to_string() + "/v" + &version.to_string() + ".metadata.json";
        let bytes = &object_store
            .get(&metadata_location.clone().into())
            .await
            .map_err(|err| anyhow!(err.to_string()))?
            .bytes()
            .await
            .map_err(|err| anyhow!(err.to_string()))?;
        let metadata: TableMetadata = serde_json::from_str(
            std::str::from_utf8(bytes).map_err(|err| anyhow!(err.to_string()))?,
        )
        .map_err(|err| anyhow!(err.to_string()))?;
        let manifests = get_manifests(&metadata, Arc::clone(object_store)).await?;
        Ok(Table {
            metadata,
            table_type: TableType::FileSystem(Arc::clone(object_store)),
            metadata_location,
            manifests,
        })
    }
    /// Get the table identifier in the catalog. Returns None of it is a filesystem table.
    pub fn identifier(&self) -> Option<&Identifier> {
        match &self.table_type {
            TableType::FileSystem(_) => None,
            TableType::Metastore(identifier, _) => Some(identifier),
        }
    }
    /// Get the catalog associated to the table. Returns None if the table is a filesystem table
    pub fn catalog(&self) -> Option<&Arc<dyn Catalog>> {
        match &self.table_type {
            TableType::FileSystem(_) => None,
            TableType::Metastore(_, catalog) => Some(catalog),
        }
    }
    /// Get the object_store associated to the table
    pub fn object_store(&self) -> Arc<dyn ObjectStore> {
        match &self.table_type {
            TableType::FileSystem(object_store) => Arc::clone(object_store),
            TableType::Metastore(_, catalog) => catalog.object_store(),
        }
    }
    /// Get the metadata of the table
    pub fn schema(&self) -> &StructType {
        self.metadata.current_schema()
    }
    /// Get the metadata of the table
    pub fn metadata(&self) -> &TableMetadata {
        &self.metadata
    }
    /// Get the location of the current metadata file
    pub fn metadata_location(&self) -> &str {
        &self.metadata_location
    }
    /// Get the location of the current metadata file
    pub fn manifests(&self) -> &[ManifestFile] {
        &self.manifests
    }
    /// Create a new transaction for this table
    pub fn new_transaction(&mut self) -> TableTransaction {
        TableTransaction::new(self)
    }
}

/// Private interface of the table.
impl Table {
    /// Increment the sequence number of the table. Is typically used when commiting a new table transaction.
    pub(crate) fn increment_sequence_number(&mut self) {
        match &mut self.metadata {
            TableMetadata::V1(_) => (),
            TableMetadata::V2(metadata) => {
                metadata.last_sequence_number += 1;
            }
        }
    }

    /// Create a new table snapshot based on the manifest_list file of the previous snapshot.
    pub(crate) async fn new_snapshot(&mut self) -> Result<()> {
        let mut bytes: [u8; 8] = [0u8; 8];
        getrandom::getrandom(&mut bytes).unwrap();
        let snapshot_id = i64::from_le_bytes(bytes);
        let object_store = self.object_store();
        let old_manifest_list_location = self.metadata.manifest_list().map(|st| st.to_string());
        match &mut self.metadata {
            TableMetadata::V1(metadata) => {
                let new_manifest_list_location = metadata.location.to_string()
                    + "/metadata/snap-"
                    + &snapshot_id.to_string()
                    + &uuid::Uuid::new_v4().to_string()
                    + ".avro";
                // If there is a previous snapshot with a manifest_list file, that file gets copied for the new snapshot. If not, a new empty file is created.
                match old_manifest_list_location {
                    Some(old_manifest_list_location) => {
                        object_store
                            .copy(
                                &old_manifest_list_location.into(),
                                &new_manifest_list_location.clone().into(),
                            )
                            .await?
                    }
                    None => {
                        object_store
                            .put(
                                &new_manifest_list_location.clone().into(),
                                Vec::new().into(),
                            )
                            .await?;
                    }
                };
                let snapshot = SnapshotV1 {
                    snapshot_id,
                    parent_snapshot_id: metadata.current_snapshot_id,
                    timestamp_ms: SystemTime::now()
                        .duration_since(SystemTime::UNIX_EPOCH)
                        .unwrap()
                        .as_millis() as i64,
                    manifest_list: Some(new_manifest_list_location),
                    manifests: None,
                    summary: None,
                    schema_id: metadata.current_schema_id.map(|id| id as i64),
                };
                if let Some(snapshots) = &mut metadata.snapshots {
                    snapshots.push(snapshot);
                    metadata.current_snapshot_id = Some(snapshot_id)
                } else {
                    metadata.snapshots = Some(vec![snapshot]);
                    metadata.current_snapshot_id = Some(snapshot_id)
                };
                Ok(())
            }
            TableMetadata::V2(metadata) => {
                let new_manifest_list_location = metadata.location.to_string()
                    + "/metadata/snap-"
                    + &snapshot_id.to_string()
                    + &uuid::Uuid::new_v4().to_string()
                    + ".avro";
                // If there is a previous snapshot with a manifest_list file, that file gets copied for the new snapshot. If not, a new empty file is created.
                match old_manifest_list_location {
                    Some(old_manifest_list_location) => {
                        object_store
                            .copy(
                                &old_manifest_list_location.into(),
                                &new_manifest_list_location.clone().into(),
                            )
                            .await?
                    }
                    None => {
                        object_store
                            .put(
                                &new_manifest_list_location.clone().into(),
                                Vec::new().into(),
                            )
                            .await?;
                    }
                };
                let snapshot = SnapshotV2 {
                    snapshot_id,
                    parent_snapshot_id: metadata.current_snapshot_id,
                    sequence_number: metadata.last_sequence_number + 1,
                    timestamp_ms: SystemTime::now()
                        .duration_since(SystemTime::UNIX_EPOCH)
                        .unwrap()
                        .as_millis() as i64,
                    manifest_list: new_manifest_list_location,
                    summary: Summary {
                        operation: Operation::Append,
                        other: HashMap::new(),
                    },
                    schema_id: Some(metadata.current_schema_id as i64),
                };
                if let Some(snapshots) = &mut metadata.snapshots {
                    snapshots.push(snapshot);
                    metadata.current_snapshot_id = Some(snapshot_id)
                } else {
                    metadata.snapshots = Some(vec![snapshot]);
                    metadata.current_snapshot_id = Some(snapshot_id)
                };
                Ok(())
            }
        }
    }
}

// Return all manifest files associated to the latest table snapshot. Reads the related manifest_list file and returns its entries.
// If the manifest list file is empty returns an empty vector.
pub(crate) async fn get_manifests(
    metadata: &TableMetadata,
    object_store: Arc<dyn ObjectStore>,
) -> Result<Vec<ManifestFile>> {
    match metadata.manifest_list() {
        Some(manifest_list) => {
            let bytes: Cursor<Vec<u8>> = Cursor::new(
                object_store
                    .get(&util::strip_prefix(manifest_list).into())
                    .await
                    .map_err(anyhow::Error::msg)?
                    .bytes()
                    .await?
                    .into(),
            );
            // Read the file content only if the bytes are not empty otherwise return an empty vector
            if !bytes.get_ref().is_empty() {
                let reader = apache_avro::Reader::new(bytes)?;
                reader
                    .map(|record| avro_value_to_manifest_file(record, metadata.format_version()))
                    .collect()
            } else {
                Ok(Vec::new())
            }
        }
        None => Ok(Vec::new()),
    }
}

/// Convert an avro value to a [ManifestFile] according to the provided format version
fn avro_value_to_manifest_file(
    entry: Result<AvroValue, apache_avro::Error>,
    format_version: FormatVersion,
) -> Result<ManifestFile, anyhow::Error> {
    entry
        .and_then(|value| match format_version {
            FormatVersion::V1 => {
                apache_avro::from_value::<ManifestFileV1>(&value).map(ManifestFile::V1)
            }
            FormatVersion::V2 => {
                apache_avro::from_value::<ManifestFileV2>(&value).map(ManifestFile::V2)
            }
        })
        .map_err(anyhow::Error::msg)
}

#[cfg(test)]
mod tests {

    use std::sync::Arc;

    use object_store::{memory::InMemory, ObjectStore};

    use crate::{
        model::{
            data_types::{PrimitiveType, StructField, StructType, Type},
            schema::SchemaV2,
        },
        table::table_builder::TableBuilder,
    };

    #[tokio::test]
    async fn test_increment_sequence_number() {
        let object_store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let schema = SchemaV2 {
            schema_id: 1,
            identifier_field_ids: Some(vec![1, 2]),
            fields: StructType {
                fields: vec![
                    StructField {
                        id: 1,
                        name: "one".to_string(),
                        required: false,
                        field_type: Type::Primitive(PrimitiveType::String),
                        doc: None,
                    },
                    StructField {
                        id: 2,
                        name: "two".to_string(),
                        required: false,
                        field_type: Type::Primitive(PrimitiveType::String),
                        doc: None,
                    },
                ],
            },
        };
        let mut table =
            TableBuilder::new_filesystem_table("test/table1", schema, Arc::clone(&object_store))
                .unwrap()
                .commit()
                .await
                .unwrap();

        let metadata_location = table.metadata_location();
        assert_eq!(metadata_location, "test/table1/metadata/v1.metadata.json");

        let transaction = table.new_transaction();
        transaction.commit().await.unwrap();
        let metadata_location = table.metadata_location();
        assert_eq!(metadata_location, "test/table1/metadata/v2.metadata.json");
    }
}