smiklosovic commented on code in PR #4297: URL: https://github.com/apache/cassandra/pull/4297#discussion_r2268763021
########## src/java/org/apache/cassandra/cql3/constraints/ConstraintResolver.java: ########## @@ -0,0 +1,162 @@ +/* + * 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.cassandra.cql3.constraints; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.ServiceLoader; +import java.util.function.Function; + +import com.google.common.annotations.VisibleForTesting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.cql3.constraints.SatisfiabilityChecker.UnaryFunctionSatisfiabilityChecker; +import org.apache.cassandra.utils.LocalizeString; + +public class ConstraintResolver +{ + private static final Logger logger = LoggerFactory.getLogger(ConstraintResolver.class); + + @VisibleForTesting + public static ConstraintProvider customConstraintProvider = ServiceLoader.load(ConstraintProvider.class).findFirst().orElse(null); + + static + { + if (customConstraintProvider != null) + logger.info("Found custom constraint provider {}", customConstraintProvider.getClass().getName()); + } + + public enum UnaryFunctions implements UnaryFunctionSatisfiabilityChecker + { + NOT_NULL(NotNullConstraint::new), + JSON(JsonConstraint::new); + + public final Function<List<String>, ConstraintFunction> functionCreator; + + UnaryFunctions(Function<List<String>, ConstraintFunction> functionCreator) + { + this.functionCreator = functionCreator; + } + } + + public enum Functions + { + LENGTH(LengthConstraint::new), + OCTET_LENGTH(OctetLengthConstraint::new), + REGEXP(RegexpConstraint::new); + + public final Function<List<String>, ConstraintFunction> functionCreator; + + Functions(Function<List<String>, ConstraintFunction> functionCreator) + { + this.functionCreator = functionCreator; + } + } + + /** + * Returns implementation of function constraint. First, it iterates over custom functions, when not found, + * then it will look into built-in ones. If it is not found in either, throws an exception. + * + * @param functionName name of function of get an instance of a constraint of + * @param arguments arguments for constraint + * @return new instance of constraint for given name + * @throws InvalidConstraintDefinitionException in case constraint can not be resolved. + */ + public static ConstraintFunction getConstraintFunction(String functionName, List<String> arguments) + { + if (customConstraintProvider != null) + { + Optional<ConstraintFunction> maybeConstraint = customConstraintProvider.getConstraintFunction(functionName, arguments); Review Comment: Actually, I do not want to do that. Because not finding a constraint among ones provided via custom provider is perfectly valid case, there is really nothing to error out on. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

