|
19 | 19 | package org.apache.paimon.catalog; |
20 | 20 |
|
21 | 21 | import org.apache.paimon.PagedList; |
| 22 | +import org.apache.paimon.Snapshot; |
22 | 23 | import org.apache.paimon.annotation.Public; |
23 | 24 | import org.apache.paimon.partition.Partition; |
| 25 | +import org.apache.paimon.partition.PartitionStatistics; |
24 | 26 | import org.apache.paimon.schema.Schema; |
25 | 27 | import org.apache.paimon.schema.SchemaChange; |
26 | 28 | import org.apache.paimon.table.Table; |
| 29 | +import org.apache.paimon.table.TableSnapshot; |
| 30 | +import org.apache.paimon.table.sink.BatchTableCommit; |
27 | 31 | import org.apache.paimon.view.View; |
28 | 32 |
|
29 | 33 | import javax.annotation.Nullable; |
30 | 34 |
|
31 | 35 | import java.util.Collections; |
32 | 36 | import java.util.List; |
33 | 37 | import java.util.Map; |
| 38 | +import java.util.Optional; |
34 | 39 |
|
35 | 40 | /** |
36 | 41 | * This interface is responsible for reading and writing metadata such as database/table from a |
@@ -458,6 +463,114 @@ default void repairTable(Identifier identifier) throws TableNotExistException { |
458 | 463 | throw new UnsupportedOperationException(); |
459 | 464 | } |
460 | 465 |
|
| 466 | + // ==================== Branch methods ========================== |
| 467 | + |
| 468 | + /** |
| 469 | + * Create a new branch for this table. By default, an empty branch will be created using the |
| 470 | + * latest schema. If you provide {@code #fromTag}, a branch will be created from the tag and the |
| 471 | + * data files will be inherited from it. |
| 472 | + * |
| 473 | + * @param identifier path of the table, cannot be system or branch name. |
| 474 | + * @param branch the branch name |
| 475 | + * @param fromTag from the tag |
| 476 | + * @throws TableNotExistException if the table in identifier doesn't exist |
| 477 | + * @throws BranchAlreadyExistException if the branch already exists |
| 478 | + * @throws TagNotExistException if the tag doesn't exist |
| 479 | + */ |
| 480 | + void createBranch(Identifier identifier, String branch, @Nullable String fromTag) |
| 481 | + throws TableNotExistException, BranchAlreadyExistException, TagNotExistException; |
| 482 | + |
| 483 | + /** |
| 484 | + * Drop the branch for this table. |
| 485 | + * |
| 486 | + * @param identifier path of the table, cannot be system or branch name. |
| 487 | + * @param branch the branch name |
| 488 | + * @throws BranchNotExistException if the branch doesn't exist |
| 489 | + */ |
| 490 | + void dropBranch(Identifier identifier, String branch) throws BranchNotExistException; |
| 491 | + |
| 492 | + /** |
| 493 | + * Fast-forward a branch to main branch. |
| 494 | + * |
| 495 | + * @param identifier path of the table, cannot be system or branch name. |
| 496 | + * @param branch the branch name |
| 497 | + * @throws BranchNotExistException if the branch doesn't exist |
| 498 | + */ |
| 499 | + void fastForward(Identifier identifier, String branch) throws BranchNotExistException; |
| 500 | + |
| 501 | + /** |
| 502 | + * List all branches of the table. |
| 503 | + * |
| 504 | + * @param identifier path of the table, cannot be system or branch name. |
| 505 | + * @throws TableNotExistException if the table in identifier doesn't exist |
| 506 | + */ |
| 507 | + List<String> listBranches(Identifier identifier) throws TableNotExistException; |
| 508 | + |
| 509 | + // ==================== Snapshot Operations ========================== |
| 510 | + |
| 511 | + /** |
| 512 | + * Commit the {@link Snapshot} for table identified by the given {@link Identifier}. |
| 513 | + * |
| 514 | + * @param identifier Path of the table |
| 515 | + * @param snapshot Snapshot to be committed |
| 516 | + * @param statistics statistics information of this change |
| 517 | + * @return Success or not |
| 518 | + * @throws Catalog.TableNotExistException if the target does not exist |
| 519 | + */ |
| 520 | + boolean commitSnapshot( |
| 521 | + Identifier identifier, Snapshot snapshot, List<PartitionStatistics> statistics) |
| 522 | + throws Catalog.TableNotExistException; |
| 523 | + |
| 524 | + /** |
| 525 | + * Return the snapshot of table identified by the given {@link Identifier}. |
| 526 | + * |
| 527 | + * @param identifier Path of the table |
| 528 | + * @return The requested snapshot of the table |
| 529 | + * @throws Catalog.TableNotExistException if the target does not exist |
| 530 | + */ |
| 531 | + Optional<TableSnapshot> loadSnapshot(Identifier identifier) |
| 532 | + throws Catalog.TableNotExistException; |
| 533 | + |
| 534 | + // ==================== Partition Modifications ========================== |
| 535 | + |
| 536 | + /** |
| 537 | + * Create partitions of the specify table. Ignore existing partitions. |
| 538 | + * |
| 539 | + * @param identifier path of the table to create partitions |
| 540 | + * @param partitions partitions to be created |
| 541 | + * @throws TableNotExistException if the table does not exist |
| 542 | + */ |
| 543 | + default void createPartitions(Identifier identifier, List<Map<String, String>> partitions) |
| 544 | + throws TableNotExistException {} |
| 545 | + |
| 546 | + /** |
| 547 | + * Drop partitions of the specify table. Ignore non-existent partitions. |
| 548 | + * |
| 549 | + * @param identifier path of the table to drop partitions |
| 550 | + * @param partitions partitions to be deleted |
| 551 | + * @throws TableNotExistException if the table does not exist |
| 552 | + */ |
| 553 | + default void dropPartitions(Identifier identifier, List<Map<String, String>> partitions) |
| 554 | + throws TableNotExistException { |
| 555 | + Table table = getTable(identifier); |
| 556 | + try (BatchTableCommit commit = table.newBatchWriteBuilder().newCommit()) { |
| 557 | + commit.truncatePartitions(partitions); |
| 558 | + } catch (Exception e) { |
| 559 | + throw new RuntimeException(e); |
| 560 | + } |
| 561 | + } |
| 562 | + |
| 563 | + /** |
| 564 | + * Alter partitions of the specify table. For non-existent partitions, partitions will be |
| 565 | + * created directly. |
| 566 | + * |
| 567 | + * @param identifier path of the table to alter partitions |
| 568 | + * @param partitions partitions to be altered |
| 569 | + * @throws TableNotExistException if the table does not exist |
| 570 | + */ |
| 571 | + default void alterPartitions(Identifier identifier, List<PartitionStatistics> partitions) |
| 572 | + throws TableNotExistException {} |
| 573 | + |
461 | 574 | // ==================== Catalog Information ========================== |
462 | 575 |
|
463 | 576 | /** Catalog options for re-creating this catalog. */ |
@@ -741,4 +854,85 @@ public Identifier identifier() { |
741 | 854 | return identifier; |
742 | 855 | } |
743 | 856 | } |
| 857 | + |
| 858 | + /** Exception for trying to create a branch that already exists. */ |
| 859 | + class BranchAlreadyExistException extends Exception { |
| 860 | + |
| 861 | + private static final String MSG = "Branch %s in table %s already exists."; |
| 862 | + |
| 863 | + private final Identifier identifier; |
| 864 | + private final String branch; |
| 865 | + |
| 866 | + public BranchAlreadyExistException(Identifier identifier, String branch) { |
| 867 | + this(identifier, branch, null); |
| 868 | + } |
| 869 | + |
| 870 | + public BranchAlreadyExistException(Identifier identifier, String branch, Throwable cause) { |
| 871 | + super(String.format(MSG, branch, identifier.getFullName()), cause); |
| 872 | + this.identifier = identifier; |
| 873 | + this.branch = branch; |
| 874 | + } |
| 875 | + |
| 876 | + public Identifier identifier() { |
| 877 | + return identifier; |
| 878 | + } |
| 879 | + |
| 880 | + public String branch() { |
| 881 | + return branch; |
| 882 | + } |
| 883 | + } |
| 884 | + |
| 885 | + /** Exception for trying to operate on a branch that doesn't exist. */ |
| 886 | + class BranchNotExistException extends Exception { |
| 887 | + |
| 888 | + private static final String MSG = "Branch %s in table %s doesn't exist."; |
| 889 | + |
| 890 | + private final Identifier identifier; |
| 891 | + private final String branch; |
| 892 | + |
| 893 | + public BranchNotExistException(Identifier identifier, String branch) { |
| 894 | + this(identifier, branch, null); |
| 895 | + } |
| 896 | + |
| 897 | + public BranchNotExistException(Identifier identifier, String branch, Throwable cause) { |
| 898 | + super(String.format(MSG, branch, identifier.getFullName()), cause); |
| 899 | + this.identifier = identifier; |
| 900 | + this.branch = branch; |
| 901 | + } |
| 902 | + |
| 903 | + public Identifier identifier() { |
| 904 | + return identifier; |
| 905 | + } |
| 906 | + |
| 907 | + public String branch() { |
| 908 | + return branch; |
| 909 | + } |
| 910 | + } |
| 911 | + |
| 912 | + /** Exception for trying to operate on a tag that doesn't exist. */ |
| 913 | + class TagNotExistException extends Exception { |
| 914 | + |
| 915 | + private static final String MSG = "Tag %s in table %s doesn't exist."; |
| 916 | + |
| 917 | + private final Identifier identifier; |
| 918 | + private final String tag; |
| 919 | + |
| 920 | + public TagNotExistException(Identifier identifier, String tag) { |
| 921 | + this(identifier, tag, null); |
| 922 | + } |
| 923 | + |
| 924 | + public TagNotExistException(Identifier identifier, String tag, Throwable cause) { |
| 925 | + super(String.format(MSG, tag, identifier.getFullName()), cause); |
| 926 | + this.identifier = identifier; |
| 927 | + this.tag = tag; |
| 928 | + } |
| 929 | + |
| 930 | + public Identifier identifier() { |
| 931 | + return identifier; |
| 932 | + } |
| 933 | + |
| 934 | + public String tag() { |
| 935 | + return tag; |
| 936 | + } |
| 937 | + } |
744 | 938 | } |
0 commit comments