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 async_trait::async_trait;
19
20use crate::error::invalid_data;
21use crate::table::Table;
22use crate::transaction::action::{ActionCommit, StatelessAction};
23use crate::{Result, TableUpdate};
24
25/// A transaction action that sets or updates the location of a table.
26///
27/// This action is used to explicitly set a new metadata location during a transaction,
28/// typically as part of advanced commit or recovery flows. The location is optional until
29/// explicitly set via [`UpdateLocationAction::set_location`].
30#[derive(Clone)]
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 StatelessAction for UpdateLocationAction {
58 async fn commit(&self, _table: &Table) -> Result<ActionCommit> {
59 let updates: Vec<TableUpdate>;
60 if let Some(location) = self.location.as_deref() {
61 updates = vec![TableUpdate::SetLocation {
62 location: location.to_owned(),
63 }];
64 } else {
65 return Err(invalid_data!(
66 "Location is not set for UpdateLocationAction!"
67 ));
68 }
69
70 Ok(ActionCommit::new(updates, vec![]))
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use as_any::Downcast;
77
78 use crate::transaction::Transaction;
79 use crate::transaction::action::{ActionEntry, ApplyTransactionAction};
80 use crate::transaction::tests::make_v2_table;
81 use crate::transaction::update_location::UpdateLocationAction;
82
83 #[test]
84 fn test_set_location() {
85 let table = make_v2_table();
86 let tx = Transaction::new(&table);
87 let tx = tx
88 .update_location()
89 .set_location(String::from("s3://bucket/prefix/new_table"))
90 .apply(tx)
91 .unwrap();
92
93 assert_eq!(tx.actions.len(), 1);
94
95 let action = (*tx.actions[0])
96 .downcast_ref::<ActionEntry<UpdateLocationAction>>()
97 .unwrap()
98 .action();
99
100 assert_eq!(
101 action.location,
102 Some(String::from("s3://bucket/prefix/new_table"))
103 )
104 }
105}