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::table::Table;
23use crate::transaction::action::{ActionCommit, TransactionAction};
24use crate::{Error, ErrorKind, Result, TableUpdate};
25
26/// A transaction action that sets or updates the location of a table.
27///
28/// This action is used to explicitly set a new metadata location during a transaction,
29/// typically as part of advanced commit or recovery flows. The location is optional until
30/// explicitly set via [`UpdateLocationAction::set_location`].
31pub struct UpdateLocationAction {
32 location: Option<String>,
33}
34
35impl UpdateLocationAction {
36 /// Creates a new [`UpdateLocationAction`] with no location set.
37 pub(crate) fn new() -> Self {
38 UpdateLocationAction { location: None }
39 }
40
41 /// Sets the target location for this action and returns the updated instance.
42 ///
43 /// # Arguments
44 ///
45 /// * `location` - A string representing the table's location.
46 ///
47 /// # Returns
48 ///
49 /// The [`UpdateLocationAction`] with the new location set.
50 pub fn set_location(mut self, location: String) -> Self {
51 self.location = Some(location);
52 self
53 }
54}
55
56#[async_trait]
57impl TransactionAction for UpdateLocationAction {
58 async fn commit(self: Arc<Self>, _table: &Table) -> Result<ActionCommit> {
59 let updates: Vec<TableUpdate>;
60 if let Some(location) = self.location.clone() {
61 updates = vec![TableUpdate::SetLocation { location }];
62 } else {
63 return Err(Error::new(
64 ErrorKind::DataInvalid,
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}