From 08025dda7bd3daff8d2a34a7083a4ec4f62bb83b Mon Sep 17 00:00:00 2001 From: yuzelin <33053040+yuzelin@users.noreply.github.com> Date: Sun, 7 Apr 2024 13:57:05 +0800 Subject: [PATCH] [core] BranchManager lists branches in the order of create time (#3155) --- .../java/org/apache/paimon/utils/BranchManager.java | 12 +++++++++--- .../apache/paimon/table/system/TagsTableTest.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) 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 6564bd4e56dc..95034e05a6f4 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 @@ -31,7 +31,9 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.PriorityQueue; import java.util.SortedMap; import java.util.stream.Collectors; @@ -164,7 +166,8 @@ public List branches() { listVersionedFileStatus(fileIO, branchDirectory(), BRANCH_PREFIX) .map(status -> Pair.of(status.getPath(), status.getModificationTime())) .collect(Collectors.toList()); - List branches = new ArrayList<>(); + PriorityQueue pq = + new PriorityQueue<>(Comparator.comparingLong(TableBranch::getCreateTime)); for (Pair path : paths) { String branchName = path.getLeft().getName().substring(BRANCH_PREFIX.length()); FileStoreTable branchTable = @@ -175,10 +178,13 @@ public List branches() { Snapshot snapshot = snapshotTags.firstKey(); List tags = snapshotTags.get(snapshot); checkArgument(tags.size() == 1); - branches.add( - new TableBranch(branchName, tags.get(0), snapshot.id(), path.getValue())); + pq.add(new TableBranch(branchName, tags.get(0), snapshot.id(), path.getValue())); } + List branches = new ArrayList<>(pq.size()); + while (!pq.isEmpty()) { + branches.add(pq.poll()); + } return branches; } catch (IOException e) { throw new RuntimeException(e); diff --git a/paimon-core/src/test/java/org/apache/paimon/table/system/TagsTableTest.java b/paimon-core/src/test/java/org/apache/paimon/table/system/TagsTableTest.java index c754aa779aa2..97beecd4de42 100644 --- a/paimon-core/src/test/java/org/apache/paimon/table/system/TagsTableTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/table/system/TagsTableTest.java @@ -110,7 +110,7 @@ void testTagBranchesTable() throws Exception { if (tag.equals("2023-07-17")) { return Collections.singletonList("2023-07-17-branch1"); } else if (tag.equals("2023-07-18")) { - return Arrays.asList("2023-07-18-branch2", "2023-07-18-branch1"); + return Arrays.asList("2023-07-18-branch1", "2023-07-18-branch2"); } else { return new ArrayList<>(); }