-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: Move UpdateRequirement out of rest package #7750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
core/src/main/java/org/apache/iceberg/UpdateRequirement.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.iceberg; | ||
|
|
||
| import org.apache.iceberg.exceptions.CommitFailedException; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
||
| /** Represents a requirement for a {@link MetadataUpdate} */ | ||
| public interface UpdateRequirement { | ||
nastra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void validate(TableMetadata base); | ||
|
|
||
| class AssertTableDoesNotExist implements UpdateRequirement { | ||
| public AssertTableDoesNotExist() {} | ||
nastra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (base != null) { | ||
| throw new CommitFailedException("Requirement failed: table already exists"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertTableUUID implements UpdateRequirement { | ||
| private final String uuid; | ||
|
|
||
| public AssertTableUUID(String uuid) { | ||
| Preconditions.checkArgument(uuid != null, "Invalid required UUID: null"); | ||
| this.uuid = uuid; | ||
| } | ||
|
|
||
| public String uuid() { | ||
| return uuid; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (!uuid.equalsIgnoreCase(base.uuid())) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: UUID does not match: expected %s != %s", base.uuid(), uuid); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertRefSnapshotID implements UpdateRequirement { | ||
| private final String name; | ||
| private final Long snapshotId; | ||
|
|
||
| public AssertRefSnapshotID(String name, Long snapshotId) { | ||
| this.name = name; | ||
| this.snapshotId = snapshotId; | ||
| } | ||
|
|
||
| public String refName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Long snapshotId() { | ||
| return snapshotId; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| SnapshotRef ref = base.ref(name); | ||
| if (ref != null) { | ||
| String type = ref.isBranch() ? "branch" : "tag"; | ||
| if (snapshotId == null) { | ||
| // a null snapshot ID means the ref should not exist already | ||
| throw new CommitFailedException( | ||
| "Requirement failed: %s %s was created concurrently", type, name); | ||
| } else if (snapshotId != ref.snapshotId()) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: %s %s has changed: expected id %s != %s", | ||
| type, name, snapshotId, ref.snapshotId()); | ||
| } | ||
| } else if (snapshotId != null) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: branch or tag %s is missing, expected %s", name, snapshotId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertLastAssignedFieldId implements UpdateRequirement { | ||
| private final int lastAssignedFieldId; | ||
|
|
||
| public AssertLastAssignedFieldId(int lastAssignedFieldId) { | ||
| this.lastAssignedFieldId = lastAssignedFieldId; | ||
| } | ||
|
|
||
| public int lastAssignedFieldId() { | ||
| return lastAssignedFieldId; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (base != null && base.lastColumnId() != lastAssignedFieldId) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: last assigned field id changed: expected id %s != %s", | ||
| lastAssignedFieldId, base.lastColumnId()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertCurrentSchemaID implements UpdateRequirement { | ||
| private final int schemaId; | ||
|
|
||
| public AssertCurrentSchemaID(int schemaId) { | ||
| this.schemaId = schemaId; | ||
| } | ||
|
|
||
| public int schemaId() { | ||
| return schemaId; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (schemaId != base.currentSchemaId()) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: current schema changed: expected id %s != %s", | ||
| schemaId, base.currentSchemaId()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertLastAssignedPartitionId implements UpdateRequirement { | ||
| private final int lastAssignedPartitionId; | ||
|
|
||
| public AssertLastAssignedPartitionId(int lastAssignedPartitionId) { | ||
| this.lastAssignedPartitionId = lastAssignedPartitionId; | ||
| } | ||
|
|
||
| public int lastAssignedPartitionId() { | ||
| return lastAssignedPartitionId; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (base != null && base.lastAssignedPartitionId() != lastAssignedPartitionId) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: last assigned partition id changed: expected id %s != %s", | ||
| lastAssignedPartitionId, base.lastAssignedPartitionId()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertDefaultSpecID implements UpdateRequirement { | ||
| private final int specId; | ||
|
|
||
| public AssertDefaultSpecID(int specId) { | ||
| this.specId = specId; | ||
| } | ||
|
|
||
| public int specId() { | ||
| return specId; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (specId != base.defaultSpecId()) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: default partition spec changed: expected id %s != %s", | ||
| specId, base.defaultSpecId()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class AssertDefaultSortOrderID implements UpdateRequirement { | ||
| private final int sortOrderId; | ||
|
|
||
| public AssertDefaultSortOrderID(int sortOrderId) { | ||
| this.sortOrderId = sortOrderId; | ||
| } | ||
|
|
||
| public int sortOrderId() { | ||
| return sortOrderId; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(TableMetadata base) { | ||
| if (sortOrderId != base.defaultSortOrderId()) { | ||
| throw new CommitFailedException( | ||
| "Requirement failed: default sort order changed: expected id %s != %s", | ||
| sortOrderId, base.defaultSortOrderId()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.