-
Notifications
You must be signed in to change notification settings - Fork 3k
CORE: REST Catalog - Response class for server provided configuration #4184
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
rdblue
merged 6 commits into
apache:master
from
kbendick:add-rest-catalog-config-resposne
Feb 25, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27e688b
CORE: REST Catalog - Response class for server-provided configuration
kbendick 3ee4767
Update exception message per PR feedbacck
kbendick 714a84e
Simplify RESTCatalogConfigResponse javadoc
kbendick 19c65dc
Fix test suite
kbendick b55a31f
Filter out values with null when getting the merged properties map
kbendick 4df1d8e
Add in a basic test of merge functionality, including with null values
kbendick 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
154 changes: 154 additions & 0 deletions
154
core/src/main/java/org/apache/iceberg/rest/responses/RESTCatalogConfigResponse.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,154 @@ | ||
| /* | ||
| * 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.rest.responses; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
|
|
||
| /** | ||
| * Represents a response to requesting server-side provided configuration for the REST catalog. | ||
| * This allows client provided values to be overridden by the server or defaulted if not provided by the client. | ||
| * <p> | ||
| * The catalog properties, with overrides and defaults applied, should be used to configure the catalog and for all | ||
| * subsequent requests after this initial config request. | ||
| * <p> | ||
| * Configuration from the server consists of two sets of key/value pairs. | ||
| * <ul> | ||
| * <li> defaults - properties that should be used as default configuration </li> | ||
| * <li> overrides - properties that should be used to override client configuration </li> | ||
| * </ul> | ||
| */ | ||
| public class RESTCatalogConfigResponse { | ||
|
|
||
| private Map<String, String> defaults; | ||
| private Map<String, String> overrides; | ||
|
|
||
| public RESTCatalogConfigResponse() { | ||
| // Required for Jackson deserialization | ||
| } | ||
|
|
||
| private RESTCatalogConfigResponse(Map<String, String> defaults, Map<String, String> overrides) { | ||
| this.defaults = defaults; | ||
| this.overrides = overrides; | ||
| validate(); | ||
| } | ||
|
|
||
| RESTCatalogConfigResponse validate() { | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Properties that should be used as default configuration. {@code defaults} have the lowest priority | ||
| * and should be applied before the client provided configuration. | ||
| * | ||
| * @return properties that should be used as default configuration | ||
| */ | ||
| public Map<String, String> defaults() { | ||
| return defaults != null ? defaults : ImmutableMap.of(); | ||
| } | ||
|
|
||
| /** | ||
| * Properties that should be used to override client configuration. {@code overrides} have the highest priority | ||
| * and should be applied after defaults and any client-provided configuration properties. | ||
| * | ||
| * @return properties that should be given higher precedence than any client provided input | ||
| */ | ||
| public Map<String, String> overrides() { | ||
| return overrides != null ? overrides : ImmutableMap.of(); | ||
| } | ||
|
|
||
| /** | ||
| * Merge client-provided config with server side provided configuration to return a single | ||
| * properties map which will be used for instantiating and configuring the REST catalog. | ||
| * | ||
| * @param clientProperties - Client provided configuration | ||
| * @return Merged configuration, with precedence in the order overrides, then client properties, and then defaults. | ||
| */ | ||
| public Map<String, String> merge(Map<String, String> clientProperties) { | ||
| Preconditions.checkNotNull(clientProperties, | ||
| "Cannot merge client properties with server-provided properties. Invalid client configuration: null"); | ||
| Map<String, String> merged = Maps.newHashMap(defaults); | ||
| merged.putAll(clientProperties); | ||
| merged.putAll(overrides); | ||
| return ImmutableMap.copyOf(Maps.filterValues(merged, Objects::nonNull)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("defaults", defaults) | ||
| .add("overrides", overrides) | ||
| .toString(); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final Map<String, String> defaults; | ||
| private final Map<String, String> overrides; | ||
|
|
||
| private Builder() { | ||
| this.defaults = Maps.newHashMap(); | ||
| this.overrides = Maps.newHashMap(); | ||
| } | ||
|
|
||
| public Builder withDefault(String key, String value) { | ||
| Preconditions.checkNotNull(key, "Invalid default property: null"); | ||
| defaults.put(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withOverride(String key, String value) { | ||
| Preconditions.checkNotNull(key, "Invalid override property: null"); | ||
| overrides.put(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the passed in map entries to the existing `defaults` of this Builder. | ||
| */ | ||
| public Builder withDefaults(Map<String, String> defaultsToAdd) { | ||
| Preconditions.checkNotNull(defaultsToAdd, "Invalid default properties map: null"); | ||
| Preconditions.checkArgument(!defaultsToAdd.containsKey(null), "Invalid default property: null"); | ||
| defaults.putAll(defaultsToAdd); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the passed in map entries to the existing `overrides` of this Builder. | ||
| */ | ||
| public Builder withOverrides(Map<String, String> overridesToAdd) { | ||
| Preconditions.checkNotNull(overridesToAdd, "Invalid override properties map: null"); | ||
| Preconditions.checkArgument(!overridesToAdd.containsKey(null), "Invalid override property: null"); | ||
| overrides.putAll(overridesToAdd); | ||
| return this; | ||
| } | ||
|
|
||
| public RESTCatalogConfigResponse build() { | ||
| return new RESTCatalogConfigResponse(defaults, overrides); | ||
| } | ||
| } | ||
| } | ||
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.