Skip to content

Commit

Permalink
[core] Fast return current table if target branch is same with current (
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysses-you authored Sep 23, 2024
1 parent 537e7b1 commit 2749542
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
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;
Expand Down Expand Up @@ -96,7 +95,7 @@ public SchemaManager(FileIO fileIO, Path tableRoot) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ protected AbstractFileStoreTable(
this.catalogEnvironment = catalogEnvironment;
}

public String currentBranch() {
return CoreOptions.branch(options());
}

@Override
public void setManifestCache(SegmentsCache<Path> manifestCache) {
store().setManifestCache(manifestCache);
Expand Down Expand Up @@ -158,9 +162,7 @@ public Identifier identifier() {
Identifier identifier = catalogEnvironment.identifier();
return identifier == null
? SchemaManager.identifierFromPath(
location().toUri().toString(),
true,
options().get(CoreOptions.BRANCH.key()))
location().toUri().toString(), true, currentBranch())
: identifier;
}

Expand Down Expand Up @@ -310,11 +312,9 @@ private FileStoreTable copyInternal(Map<String, String> dynamicOptions, boolean

@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);
Expand All @@ -332,7 +332,7 @@ public FileStoreTable copy(TableSchema newTableSchema) {
}

protected SchemaManager schemaManager() {
return new SchemaManager(fileIO(), path, CoreOptions.branch(options()));
return new SchemaManager(fileIO(), path, currentBranch());
}

@Override
Expand Down Expand Up @@ -629,7 +629,7 @@ public void rollbackTo(String tagName) {

@Override
public TagManager tagManager() {
return new TagManager(fileIO, path, CoreOptions.branch(options()));
return new TagManager(fileIO, path, currentBranch());
}

@Override
Expand All @@ -639,14 +639,20 @@ public BranchManager branchManager() {

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public Path branchDirectory() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public SnapshotManager(FileIO fileIO, Path tablePath) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public TagManager(FileIO fileIO, Path tablePath) {
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) {
Expand Down

0 comments on commit 2749542

Please sign in to comment.