xuzifu666 commented on code in PR #290: URL: https://github.com/apache/paimon-rust/pull/290#discussion_r3155079123
########## crates/paimon/src/table/branch_manager.rs: ########## @@ -0,0 +1,577 @@ +// 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. + +//! Branch manager for reading and managing branch metadata using FileIO. +//! +//! Reference: [org.apache.paimon.utils.BranchManager](https://github.com/apache/paimon/blob/master/paimon-core/src/main/java/org/apache/paimon/utils/BranchManager.java) +//! and [org.apache.paimon.utils.FileSystemBranchManager](https://github.com/apache/paimon/blob/master/paimon-core/src/main/java/org/apache/paimon/utils/FileSystemBranchManager.java) + +use crate::catalog::DEFAULT_MAIN_BRANCH; +use crate::io::FileIO; +use crate::table::{SchemaManager, SnapshotManager, TagManager}; +use opendal::raw::get_basename; + +const BRANCH_DIR: &str = "branch"; +const BRANCH_PREFIX: &str = "branch-"; + +/// Manager for branch directories using unified FileIO. +/// +/// Branches are stored as sub-directories at `{table_path}/branch/branch-{name}`. +/// +/// Reference: [org.apache.paimon.utils.BranchManager](https://github.com/apache/paimon/blob/master/paimon-core/src/main/java/org/apache/paimon/utils/BranchManager.java) +#[derive(Debug, Clone)] +pub struct BranchManager { + file_io: FileIO, + table_path: String, +} + +impl BranchManager { + pub fn new(file_io: FileIO, table_path: String) -> Self { + Self { + file_io, + table_path, + } + } + + /// Path to the branch directory (e.g. `table_path/branch`). + pub fn branch_directory(&self) -> String { + format!("{}/{}", self.table_path, BRANCH_DIR) + } + + /// Path to the branch sub-directory for the given name (e.g. `branch/branch-my_branch`). + pub fn branch_path(&self, branch_name: &str) -> String { + format!( + "{}/{}{}", + self.branch_directory(), + BRANCH_PREFIX, + branch_name + ) + } + + /// Validate branch name format. + /// + /// Rules: + /// - Cannot be "main" + /// - Cannot be blank or whitespace only + /// - Cannot be a pure numeric string + fn validate_branch_name(branch_name: &str) -> crate::Result<()> { + if branch_name == DEFAULT_MAIN_BRANCH { + return Err(crate::Error::DataInvalid { + message: format!( + "Branch name '{}' is the default branch and cannot be used.", + DEFAULT_MAIN_BRANCH + ), + source: None, + }); + } + if branch_name.trim().is_empty() { + return Err(crate::Error::DataInvalid { + message: format!("Branch name '{}' is blank.", branch_name), + source: None, + }); + } + if branch_name.chars().all(|c| c.is_ascii_digit()) { + return Err(crate::Error::DataInvalid { + message: format!( + "Branch name cannot be pure numeric string but is '{}'.", + branch_name + ), + source: None, + }); + } + Ok(()) + } + + /// Validate branch name and ensure it does not already exist. + pub async fn validate_branch(&self, branch_name: &str) -> crate::Result<()> { + Self::validate_branch_name(branch_name)?; + if self.branch_exists(branch_name).await? { + return Err(crate::Error::DataInvalid { + message: format!("Branch name '{}' already exists.", branch_name), + source: None, + }); + } + Ok(()) + } + + /// Check if a branch exists. + pub async fn branch_exists(&self, branch_name: &str) -> crate::Result<bool> { + let names = self.list_all().await?; Review Comment: Yes, it has been changed to determine based on branch name. -- 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]
