001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.constraint;
019
020import org.apache.hadoop.conf.Configurable;
021import org.apache.hadoop.hbase.client.Put;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Apply a {@link Constraint} (in traditional database terminology) to a Table. Any number of
026 * {@link Constraint Constraints} can be added to the table, in any order.
027 * <p/>
028 * A {@link Constraint} must be added to a table before the table is loaded via
029 * {@link Constraints#add(org.apache.hadoop.hbase.client.TableDescriptorBuilder, Class...)} or
030 * {@link Constraints#add(org.apache.hadoop.hbase.client.TableDescriptorBuilder, org.apache.hadoop.hbase.util.Pair...)}
031 * (if you want to add a configuration with the {@link Constraint}). Constraints will be run in the
032 * order that they are added. Further, a Constraint will be configured before it is run (on load).
033 * <p/>
034 * See
035 * {@link Constraints#enableConstraint(org.apache.hadoop.hbase.client.TableDescriptorBuilder, Class)}
036 * and
037 * {@link Constraints#disableConstraint(org.apache.hadoop.hbase.client.TableDescriptorBuilder, Class)}
038 * for enabling/disabling of a given {@link Constraint} after it has been added.
039 * <p/>
040 * If a {@link Put} is invalid, the Constraint should throw some sort of
041 * {@link org.apache.hadoop.hbase.constraint.ConstraintException}, indicating that the {@link Put}
042 * has failed. When this exception is thrown, not further retries of the {@link Put} are attempted
043 * nor are any other {@link Constraint Constraints} attempted (the {@link Put} is clearly not
044 * valid). Therefore, there are performance implications in the order in which {@link BaseConstraint
045 * Constraints} are specified.
046 * <p/>
047 * If a {@link Constraint} fails to fail the {@link Put} via a
048 * {@link org.apache.hadoop.hbase.constraint.ConstraintException}, but instead throws a
049 * {@link RuntimeException}, the entire constraint processing mechanism
050 * ({@link ConstraintProcessor}) will be unloaded from the table. This ensures that the region
051 * server is still functional, but no more {@link Put Puts} will be checked via {@link Constraint
052 * Constraints}.
053 * <p/>
054 * Further, {@link Constraint Constraints} should probably not be used to enforce cross-table
055 * references as it will cause tremendous write slowdowns, but it is possible.
056 * <p/>
057 * NOTE: Implementing classes must have a nullary (no-args) constructor
058 * @see BaseConstraint
059 * @see Constraints
060 */
061@InterfaceAudience.Private
062public interface Constraint extends Configurable {
063
064  /**
065   * Check a {@link Put} to ensure it is valid for the table. If the {@link Put} is valid, then just
066   * return from the method. Otherwise, throw an {@link Exception} specifying what happened. This
067   * {@link Exception} is propagated back to the client so you can see what caused the {@link Put}
068   * to fail.
069   * @param p {@link Put} to check
070   * @throws org.apache.hadoop.hbase.constraint.ConstraintException when the {@link Put} does not
071   *                                                                match the constraint.
072   */
073  void check(Put p) throws ConstraintException;
074
075}