This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 274954290 [core] Fast return current table if target branch is same 
with current (#4240)
274954290 is described below

commit 274954290280feb9a91e358c10b6274ee245d0b2
Author: Xiduo You <[email protected]>
AuthorDate: Mon Sep 23 21:18:53 2024 +0800

    [core] Fast return current table if target branch is same with current 
(#4240)
---
 .../org/apache/paimon/schema/SchemaManager.java    |  3 +--
 .../paimon/table/AbstractFileStoreTable.java       | 30 +++++++++++++---------
 .../org/apache/paimon/utils/BranchManager.java     |  4 +++
 .../org/apache/paimon/utils/SnapshotManager.java   |  3 +--
 .../java/org/apache/paimon/utils/TagManager.java   |  2 +-
 5 files changed, 25 insertions(+), 17 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java 
b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
index 7cd5625a8..962cba952 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
@@ -46,7 +46,6 @@ import org.apache.paimon.utils.BranchManager;
 import org.apache.paimon.utils.JsonSerdeUtil;
 import org.apache.paimon.utils.Preconditions;
 import org.apache.paimon.utils.SnapshotManager;
-import org.apache.paimon.utils.StringUtils;
 
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.ThreadSafe;
@@ -96,7 +95,7 @@ public class SchemaManager implements Serializable {
     public SchemaManager(FileIO fileIO, Path tableRoot, String branch) {
         this.fileIO = fileIO;
         this.tableRoot = tableRoot;
-        this.branch = StringUtils.isNullOrWhitespaceOnly(branch) ? 
DEFAULT_MAIN_BRANCH : branch;
+        this.branch = BranchManager.normalizeBranch(branch);
     }
 
     public SchemaManager copyWithBranch(String branchName) {
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java 
b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
index 361442073..a98f368c5 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
@@ -113,6 +113,10 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
         this.catalogEnvironment = catalogEnvironment;
     }
 
+    public String currentBranch() {
+        return CoreOptions.branch(options());
+    }
+
     @Override
     public void setManifestCache(SegmentsCache<Path> manifestCache) {
         store().setManifestCache(manifestCache);
@@ -158,9 +162,7 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
         Identifier identifier = catalogEnvironment.identifier();
         return identifier == null
                 ? SchemaManager.identifierFromPath(
-                        location().toUri().toString(),
-                        true,
-                        options().get(CoreOptions.BRANCH.key()))
+                        location().toUri().toString(), true, currentBranch())
                 : identifier;
     }
 
@@ -310,11 +312,9 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
 
     @Override
     public FileStoreTable copyWithLatestSchema() {
-        Map<String, String> options = tableSchema.options();
-        SchemaManager schemaManager =
-                new SchemaManager(fileIO(), location(), 
CoreOptions.branch(options()));
-        Optional<TableSchema> optionalLatestSchema = schemaManager.latest();
+        Optional<TableSchema> optionalLatestSchema = schemaManager().latest();
         if (optionalLatestSchema.isPresent()) {
+            Map<String, String> options = tableSchema.options();
             TableSchema newTableSchema = optionalLatestSchema.get();
             newTableSchema = newTableSchema.copy(options);
             SchemaValidation.validateTableSchema(newTableSchema);
@@ -332,7 +332,7 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
     }
 
     protected SchemaManager schemaManager() {
-        return new SchemaManager(fileIO(), path, 
CoreOptions.branch(options()));
+        return new SchemaManager(fileIO(), path, currentBranch());
     }
 
     @Override
@@ -629,7 +629,7 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
 
     @Override
     public TagManager tagManager() {
-        return new TagManager(fileIO, path, CoreOptions.branch(options()));
+        return new TagManager(fileIO, path, currentBranch());
     }
 
     @Override
@@ -639,14 +639,20 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
 
     @Override
     public FileStoreTable switchToBranch(String branchName) {
+        String currentBranch = BranchManager.normalizeBranch(currentBranch());
+        String targetBranch = BranchManager.normalizeBranch(branchName);
+        if (currentBranch.equals(targetBranch)) {
+            return this;
+        }
+
         Optional<TableSchema> optionalSchema =
-                new SchemaManager(fileIO(), location(), branchName).latest();
+                new SchemaManager(fileIO(), location(), targetBranch).latest();
         Preconditions.checkArgument(
-                optionalSchema.isPresent(), "Branch " + branchName + " does 
not exist");
+                optionalSchema.isPresent(), "Branch " + targetBranch + " does 
not exist");
 
         TableSchema branchSchema = optionalSchema.get();
         Options branchOptions = new Options(branchSchema.options());
-        branchOptions.set(CoreOptions.BRANCH, branchName);
+        branchOptions.set(CoreOptions.BRANCH, targetBranch);
         branchSchema = branchSchema.copy(branchOptions.toMap());
         return FileStoreTableFactory.create(
                 fileIO(), location(), branchSchema, new Options(), 
catalogEnvironment());
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/utils/BranchManager.java 
b/paimon-core/src/main/java/org/apache/paimon/utils/BranchManager.java
index d398ca9c3..c2793de37 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/BranchManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/BranchManager.java
@@ -68,6 +68,10 @@ public class BranchManager {
         return new Path(tablePath + "/branch");
     }
 
+    public static String normalizeBranch(String branch) {
+        return StringUtils.isNullOrWhitespaceOnly(branch) ? 
DEFAULT_MAIN_BRANCH : branch;
+    }
+
     public static boolean isMainBranch(String branch) {
         return branch.equals(DEFAULT_MAIN_BRANCH);
     }
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java 
b/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
index c32ba9ef0..4dda63960 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
@@ -79,8 +79,7 @@ public class SnapshotManager implements Serializable {
     public SnapshotManager(FileIO fileIO, Path tablePath, String branchName) {
         this.fileIO = fileIO;
         this.tablePath = tablePath;
-        this.branch =
-                StringUtils.isNullOrWhitespaceOnly(branchName) ? 
DEFAULT_MAIN_BRANCH : branchName;
+        this.branch = BranchManager.normalizeBranch(branchName);
     }
 
     public SnapshotManager copyWithBranch(String branchName) {
diff --git a/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java 
b/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java
index 089e87c86..f6e69fb28 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java
@@ -71,7 +71,7 @@ public class TagManager {
     public TagManager(FileIO fileIO, Path tablePath, String branch) {
         this.fileIO = fileIO;
         this.tablePath = tablePath;
-        this.branch = StringUtils.isNullOrWhitespaceOnly(branch) ? 
DEFAULT_MAIN_BRANCH : branch;
+        this.branch = BranchManager.normalizeBranch(branch);
     }
 
     public TagManager copyWithBranch(String branchName) {

Reply via email to