bbotella commented on code in PR #4297: URL: https://github.com/apache/cassandra/pull/4297#discussion_r2267596461
########## examples/constraints/src/org/apache/cassandra/cql3/constraints/CustomConstraintsProvider.java: ########## @@ -0,0 +1,100 @@ +/** + * 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.List; +import java.util.Optional; + +import org.apache.cassandra.cql3.constraints.ColumnConstraint; +import org.apache.cassandra.cql3.constraints.ConstraintFunction; +import org.apache.cassandra.cql3.constraints.ConstraintProvider; +import org.apache.cassandra.cql3.constraints.ConstraintResolver; +import org.apache.cassandra.cql3.constraints.InvalidConstraintDefinitionException; +import org.apache.cassandra.cql3.constraints.JsonConstraint; +import org.apache.cassandra.cql3.constraints.LengthConstraint; +import org.apache.cassandra.cql3.constraints.SatisfiabilityChecker; +import org.apache.cassandra.cql3.constraints.SatisfiabilityChecker.UnaryFunctionSatisfiabilityChecker; +import org.apache.cassandra.cql3.constraints.UnaryConstraintFunction; +import org.apache.cassandra.db.marshal.UTF8Type; +import org.apache.cassandra.schema.ColumnMetadata; + +import static org.apache.cassandra.cql3.constraints.AbstractFunctionSatisfiabilityChecker.FUNCTION_SATISFIABILITY_CHECKER; +import static org.apache.cassandra.cql3.constraints.ConstraintResolver.getConstraintFunction; +import static org.apache.cassandra.cql3.constraints.ConstraintResolver.getUnaryConstraintFunction; + +public class CustomConstraintsProvider implements ConstraintProvider Review Comment: Nice! And really useful as an example. I am worried tho that it may look as actual production code if we place it where it is now? ########## src/java/org/apache/cassandra/cql3/constraints/ConstraintProvider.java: ########## @@ -0,0 +1,63 @@ +/* + * 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.List; +import java.util.Optional; + +/** + * Users implementing this interface and integrating it with SPI (putting JAR on a class path and + * adding it to META-INF/services/ConstraintProvider) will enrich Cassandra with their custom constraints. + */ +public interface ConstraintProvider +{ + /** + * Tries to instantiate {@link UnaryConstraintFunction} with given arguments. + * <p> + * An implementation of this method should always return new object for each method call. Do not + * cache constraint instances and do not return them! Create a new instance every time. Do not re-use it. + * + * @param functionName name of function + * @param arguments arguments to the function + * @return unary constraint function when possible to create with this provider, empty optional otherwise. + */ + Optional<UnaryConstraintFunction> getUnaryConstraint(String functionName, List<String> arguments); + + /** + * Tries to instantiate {@link ConstraintFunction} with given arguments. + * <p> + * An implementation of this method should always return new object for each method call. Do not + * cache constraint instances and do not return them! Create a new instance every time. Do not re-use it. + * + * @param functionName name of function + * @param arguments arguments to the function + * @return constraint function when possible to create with this provider, empty optional otherwise. + */ + Optional<ConstraintFunction> getConstraintFunction(String functionName, List<String> arguments); + + /** + * @return list of satistifability checkers for all unary constraints this provider is responsible for + */ + List<? extends SatisfiabilityChecker> getUnaryConstraintSatisfiabilityCheckers(); + + /** + * @return list of satistifability checkers for all function constraints this provider is responsible for Review Comment: ```suggestion * @return list of satisfiability checkers for all function constraints this provider is responsible for ``` ########## src/java/org/apache/cassandra/cql3/constraints/ConstraintProvider.java: ########## @@ -0,0 +1,63 @@ +/* + * 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.List; +import java.util.Optional; + +/** + * Users implementing this interface and integrating it with SPI (putting JAR on a class path and + * adding it to META-INF/services/ConstraintProvider) will enrich Cassandra with their custom constraints. + */ +public interface ConstraintProvider +{ + /** + * Tries to instantiate {@link UnaryConstraintFunction} with given arguments. + * <p> + * An implementation of this method should always return new object for each method call. Do not + * cache constraint instances and do not return them! Create a new instance every time. Do not re-use it. + * + * @param functionName name of function + * @param arguments arguments to the function + * @return unary constraint function when possible to create with this provider, empty optional otherwise. + */ + Optional<UnaryConstraintFunction> getUnaryConstraint(String functionName, List<String> arguments); + + /** + * Tries to instantiate {@link ConstraintFunction} with given arguments. + * <p> + * An implementation of this method should always return new object for each method call. Do not + * cache constraint instances and do not return them! Create a new instance every time. Do not re-use it. + * + * @param functionName name of function + * @param arguments arguments to the function + * @return constraint function when possible to create with this provider, empty optional otherwise. + */ + Optional<ConstraintFunction> getConstraintFunction(String functionName, List<String> arguments); + + /** + * @return list of satistifability checkers for all unary constraints this provider is responsible for Review Comment: ```suggestion * @return list of satisfiability checkers for all unary constraints this provider is responsible for ``` ########## 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: Maybe wrap this into a try/catch with nice error logging? -- 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]

