dimas-b commented on code in PR #1417: URL: https://github.com/apache/polaris/pull/1417#discussion_r2080623337
########## service/common/src/main/java/org/apache/polaris/service/config/ReservedProperties.java: ########## @@ -0,0 +1,137 @@ +/* + * 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.polaris.service.config; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.iceberg.MetadataUpdate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Used to track entity properties reserved for use by the catalog. These properties may not be + * overridden by the end user. + */ +public interface ReservedProperties { + Logger LOGGER = LoggerFactory.getLogger(ReservedProperties.class); + + /** + * Provides a {@link ReservedProperties} implementation that reserves nothing. Used for testing. + */ + ReservedProperties NONE = + new ReservedProperties() { + @Override + public List<String> prefixes() { + return List.of(); + } + }; + + /** + * A list of prefixes that are considered reserved. Any property starting with one of these + * prefixes is a reserved property. + */ + List<String> prefixes(); + + /** If true, attempts to modify a reserved property should throw an exception. */ + default boolean shouldThrow() { + return true; + } + + /** + * Removes reserved properties from a planned change to an entity. If `shouldThrow` returns true, + * this will throw an IllegalArgumentException. + * + * @param existingProperties The properties currently present for an entity + * @param updateProperties The properties present in an update to an entity + * @return The properties in the update, with changes to reserved properties removed + */ + default Map<String, String> removeReservedPropertiesFromUpdate( + Map<String, String> existingProperties, Map<String, String> updateProperties) + throws IllegalArgumentException { + Map<String, String> updatePropertiesWithoutReservedProperties = + removeReservedProperties(updateProperties); + for (var entry : updateProperties.entrySet()) { + // If a key was removed from the update, we substitute back the existing value + if (!updatePropertiesWithoutReservedProperties.containsKey(entry.getKey())) { + if (existingProperties.containsKey(entry.getKey())) { + updatePropertiesWithoutReservedProperties.put( + entry.getKey(), existingProperties.get(entry.getKey())); + } + } + } + return updatePropertiesWithoutReservedProperties; + } + + /** + * Removes reserved properties from a list of input property keys. If `shouldThrow`returns true, + * this will throw an IllegalArgumentException. + * + * @param properties A map of properties to remove reserved properties from + * @return The keys from the input list which are not reserved properties + */ + default Map<String, String> removeReservedProperties(Map<String, String> properties) + throws IllegalArgumentException { + Map<String, String> results = new HashMap<>(); + List<String> prefixes = prefixes(); + for (var entry : properties.entrySet()) { + boolean isReserved = false; + for (String prefix : prefixes) { + if (entry.getKey().startsWith(prefix)) { + isReserved = true; + String message = + String.format("Property '%s' matches reserved prefix '%s'", entry.getKey(), prefix); + if (shouldThrow()) { + throw new IllegalArgumentException(message); + } else { + LOGGER.debug(message); Review Comment: why not `break` here? It does not make sense to continue the prefix loop, we know the outcome. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org