Skip to main content

iceberg/transaction/
update_location.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use async_trait::async_trait;
21
22use crate::error::invalid_data;
23use crate::table::Table;
24use crate::transaction::action::{ActionCommit, TransactionAction};
25use crate::{Result, TableUpdate};
26
27/// A transaction action that sets or updates the location of a table.
28///
29/// This action is used to explicitly set a new metadata location during a transaction,
30/// typically as part of advanced commit or recovery flows. The location is optional until
31/// explicitly set via [`UpdateLocationAction::set_location`].
32pub struct UpdateLocationAction {
33    location: Option<String>,
34}
35
36impl UpdateLocationAction {
37    /// Creates a new [`UpdateLocationAction`] with no location set.
38    pub(crate) fn new() -> Self {
39        UpdateLocationAction { location: None }
40    }
41
42    /// Sets the target location for this action and returns the updated instance.
43    ///
44    /// # Arguments
45    ///
46    /// * `location` - A string representing the table's location.
47    ///
48    /// # Returns
49    ///
50    /// The [`UpdateLocationAction`] with the new location set.
51    pub fn set_location(mut self, location: String) -> Self {
52        self.location = Some(location);
53        self
54    }
55}
56
57#[async_trait]
58impl TransactionAction for UpdateLocationAction {
59    async fn commit(self: Arc<Self>, _table: &Table) -> Result<ActionCommit> {
60        let updates: Vec<TableUpdate>;
61        if let Some(location) = self.location.clone() {
62            updates = vec![TableUpdate::SetLocation { location }];
63        } else {
64            return Err(invalid_data!(
65                "Location is not set for UpdateLocationAction!"
66            ));
67        }
68
69        Ok(ActionCommit::new(updates, vec![]))
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use as_any::Downcast;
76
77    use crate::transaction::Transaction;
78    use crate::transaction::action::ApplyTransactionAction;
79    use crate::transaction::tests::make_v2_table;
80    use crate::transaction::update_location::UpdateLocationAction;
81
82    #[test]
83    fn test_set_location() {
84        let table = make_v2_table();
85        let tx = Transaction::new(&table);
86        let tx = tx
87            .update_location()
88            .set_location(String::from("s3://bucket/prefix/new_table"))
89            .apply(tx)
90            .unwrap();
91
92        assert_eq!(tx.actions.len(), 1);
93
94        let action = (*tx.actions[0])
95            .downcast_ref::<UpdateLocationAction>()
96            .unwrap();
97
98        assert_eq!(
99            action.location,
100            Some(String::from("s3://bucket/prefix/new_table"))
101        )
102    }
103}