liurenjie1024 commented on code in PR #29:
URL: https://github.com/apache/iceberg-rust/pull/29#discussion_r1291062058


##########
crates/iceberg/src/spec/table_metadata.rs:
##########
@@ -0,0 +1,996 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+/*!
+Defines the [table metadata](https://iceberg.apache.org/spec/#table-metadata).
+The main struct here is [TableMetadataV2] which defines the data for a table.
+*/
+
+use std::collections::HashMap;
+
+use serde::{Deserialize, Serialize};
+use serde_repr::{Deserialize_repr, Serialize_repr};
+use uuid::Uuid;
+
+use crate::{Error, ErrorKind};
+
+use super::{
+    partition::{PartitionField, PartitionSpec},
+    schema::{self, Schema},
+    snapshot::{Reference, Retention, Snapshot, SnapshotV1, SnapshotV2},
+    sort::SortOrder,
+};
+
+static MAIN_BRANCH: &str = "main";
+
+#[derive(Debug, PartialEq, Serialize, Deserialize, Eq, Clone)]
+#[serde(try_from = "TableMetadataEnum", into = "TableMetadataEnum")]
+/// Fields for the version 2 of the table metadata.
+pub struct TableMetadata {
+    /// Integer Version for the format.
+    format_version: FormatVersion,
+    /// A UUID that identifies the table
+    table_uuid: Uuid,
+    /// Location tables base location
+    location: String,
+    /// The tables highest sequence number
+    last_sequence_number: i64,
+    /// Timestamp in milliseconds from the unix epoch when the table was last 
updated.
+    last_updated_ms: i64,
+    /// An integer; the highest assigned column ID for the table.
+    last_column_id: i32,
+    /// A list of schemas, stored as objects with schema-id.
+    schemas: HashMap<i32, Schema>,
+    /// ID of the table’s current schema.
+    current_schema_id: i32,
+    /// A list of partition specs, stored as full partition spec objects.
+    partition_specs: HashMap<i32, PartitionSpec>,
+    /// ID of the “current” spec that writers should use by default.
+    default_spec_id: i32,
+    /// An integer; the highest assigned partition field ID across all 
partition specs for the table.
+    last_partition_id: i32,
+    ///A string to string map of table properties. This is used to control 
settings that
+    /// affect reading and writing and is not intended to be used for 
arbitrary metadata.
+    /// For example, commit.retry.num-retries is used to control the number of 
commit retries.
+    properties: Option<HashMap<String, String>>,
+    /// long ID of the current table snapshot; must be the same as the current
+    /// ID of the main branch in refs.
+    current_snapshot_id: Option<i64>,
+    ///A list of valid snapshots. Valid snapshots are snapshots for which all
+    /// data files exist in the file system. A data file must not be deleted
+    /// from the file system until the last snapshot in which it was listed is
+    /// garbage collected.
+    snapshots: Option<HashMap<i64, Snapshot>>,
+    /// A list (optional) of timestamp and snapshot ID pairs that encodes 
changes
+    /// to the current snapshot for the table. Each time the 
current-snapshot-id
+    /// is changed, a new entry should be added with the last-updated-ms
+    /// and the new current-snapshot-id. When snapshots are expired from
+    /// the list of valid snapshots, all entries before a snapshot that has
+    /// expired should be removed.
+    snapshot_log: Option<Vec<SnapshotLog>>,
+
+    /// A list (optional) of timestamp and metadata file location pairs
+    /// that encodes changes to the previous metadata files for the table.
+    /// Each time a new metadata file is created, a new entry of the
+    /// previous metadata file location should be added to the list.
+    /// Tables can be configured to remove oldest metadata log entries and
+    /// keep a fixed-size log of the most recent entries after a commit.
+    metadata_log: Option<Vec<MetadataLog>>,
+
+    /// A list of sort orders, stored as full sort order objects.
+    sort_orders: HashMap<i64, SortOrder>,
+    /// Default sort order id of the table. Note that this could be used by
+    /// writers, but is not used when reading because reads use the specs
+    /// stored in manifest files.
+    default_sort_order_id: i64,
+    ///A map of snapshot references. The map keys are the unique snapshot 
reference
+    /// names in the table, and the map values are snapshot reference objects.
+    /// There is always a main branch reference pointing to the 
current-snapshot-id
+    /// even if the refs map is null.
+    refs: HashMap<String, Reference>,
+}
+
+impl TableMetadata {
+    /// Get current schema
+    #[inline]
+    pub fn current_schema(&self) -> Result<&Schema, Error> {
+        self.schemas.get(&self.current_schema_id).ok_or_else(|| {
+            Error::new(
+                ErrorKind::DataInvalid,
+                format!("Schema id {} not found!", self.current_schema_id),
+            )
+        })
+    }
+    /// Get default partition spec
+    #[inline]
+    pub fn default_partition_spec(&self) -> Result<&PartitionSpec, Error> {
+        self.partition_specs
+            .get(&self.default_spec_id)
+            .ok_or_else(|| {
+                Error::new(
+                    ErrorKind::DataInvalid,
+                    format!("Partition spec id {} not found!", 
self.default_spec_id),
+                )
+            })
+    }
+
+    /// Get current snapshot
+    #[inline]
+    pub fn current_snapshot(&self) -> Result<&Snapshot, Error> {

Review Comment:
   ```suggestion
       pub fn current_snapshot(&self) -> Result<Option<&Snapshot>, Error> {
   ```
   The return type should be `Option` here since there is no snapshot with 
empty. Also `snapshot_id == -1` also means empty snapshot.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to