Class Constraints
java.lang.Object
org.apache.hadoop.hbase.constraint.Constraints
Utilities for adding/removing constraints from a table.
Since
TableDescriptor is immutable now, you should use TableDescriptorBuilder.
And when disabling or removing constraints, you could use
TableDescriptorBuilder.newBuilder(TableDescriptor) to clone the old
TableDescriptor and then pass it the below methods.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Patternprivate static final Stringprivate static final Comparator<Constraint>private static Stringprivate static final intprivate static final Stringprivate static final org.slf4j.Loggerprivate static final longprivate static final Stringprivate static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TableDescriptorBuilderadd(TableDescriptorBuilder builder, Class<? extends Constraint>... constraints) Add configuration-less constraints to the table.static TableDescriptorBuilderadd(TableDescriptorBuilder builder, Class<? extends Constraint> constraint, org.apache.hadoop.conf.Configuration conf) Add aConstraintto the table with the given configurationstatic TableDescriptorBuilderadd(TableDescriptorBuilder builder, Pair<Class<? extends Constraint>, org.apache.hadoop.conf.Configuration>... constraints) Add constraints and their associated configurations to the table.private static TableDescriptorBuilderaddConstraint(TableDescriptorBuilder builder, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration conf, long priority) Write the raw constraint and configuration to the descriptor.private static TableDescriptorBuilderchangeConstraintEnabled(TableDescriptorBuilder builder, Class<? extends Constraint> clazz, boolean enabled) Change the whether the constraint (if it is already present) is enabled or disabled.private static org.apache.hadoop.conf.Configurationconfigure(org.apache.hadoop.conf.Configuration conf, boolean enabled, long priority) Setup the configuration for a constraint as to whether it is enabled and its prioritystatic TableDescriptorBuilderdisable(TableDescriptorBuilder builder) Turn off processing constraints for a given table, even if constraints have been turned on or added.static voiddisableConstraint(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Disable the givenConstraint.static TableDescriptorBuilderenable(TableDescriptorBuilder builder) Enable constraints on a table.static voidenableConstraint(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Enable the givenConstraint.static booleanenabled(TableDescriptor desc, Class<? extends Constraint> clazz) Check to see if the given constraint is enabled.(package private) static List<? extends Constraint>getConstraints(TableDescriptor desc, ClassLoader classloader) Get the constraints stored in the table descriptorgetKeyValueForClass(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Get the kvMap.Entryin the descriptor builder for the specified classgetKeyValueForClass(TableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entryin the descriptor for the specified classprivate static longgetNextPriority(TableDescriptorBuilder builder) static booleanhas(TableDescriptor desc, Class<? extends Constraint> clazz) Check to see if the Constraint is currently set.private static org.apache.hadoop.conf.ConfigurationreadConfiguration(byte[] bytes) Read theConfigurationstored in the byte stream.private static org.apache.hadoop.conf.ConfigurationreadConfiguration(String bytes) Read in the configuration from the String encoded configurationstatic TableDescriptorBuilderremove(TableDescriptorBuilder builder) Remove allConstraintsthat have been added to the table and turn off the constraint processing.static TableDescriptorBuilderremove(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Remove the constraint (and associated information) for the table descriptor.private static StringserializeConfiguration(org.apache.hadoop.conf.Configuration conf) Write the configuration to a Stringprivate static StringserializeConstraintClass(Class<? extends Constraint> clazz) Just write the class to a String representation of the class as a key for theTableDescriptorstatic TableDescriptorBuildersetConfiguration(TableDescriptorBuilder builder, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration configuration) Update the configuration for theConstraint; does not change the order in which the constraint is run.private static TableDescriptorBuilderupdateLatestPriority(TableDescriptorBuilder builder, long priority) private static TableDescriptorBuilderwriteConstraint(TableDescriptorBuilder builder, String key, org.apache.hadoop.conf.Configuration conf) Write the given key and associated configuration to theTableDescriptorBuilder.
-
Field Details
-
DEFAULT_PRIORITY
- See Also:
-
LOG
-
CONSTRAINT_HTD_KEY_PREFIX
- See Also:
-
CONSTRAINT_HTD_ATTR_KEY_PATTERN
-
ENABLED_KEY
- See Also:
-
PRIORITY_KEY
- See Also:
-
MIN_PRIORITY
- See Also:
-
UNSET_PRIORITY
- See Also:
-
COUNTER_KEY
-
constraintComparator
-
-
Constructor Details
-
Constraints
private Constraints()
-
-
Method Details
-
enable
Enable constraints on a table. Currently, if you attempt to add a constraint to the table, then Constraints will automatically be turned on.- Throws:
IOException
-
disable
Turn off processing constraints for a given table, even if constraints have been turned on or added.- Throws:
IOException
-
remove
Remove allConstraintsthat have been added to the table and turn off the constraint processing. AllConfigurationsand their associatedConstraintare removed.- Throws:
IOException
-
has
Check to see if the Constraint is currently set.- Parameters:
desc-TableDescriptorto checkclazz-Constraintclass to check for.- Returns:
- true if the
Constraintis present, even if it is disabled. false otherwise.
-
getKeyValueForClass
private static Pair<String,String> getKeyValueForClass(TableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entryin the descriptor for the specified class- Parameters:
desc-TableDescriptorto readclazz- To search for- Returns:
- The
Pairof <key, value> in the table, if that class is present.nullotherwise.
-
getKeyValueForClass
private static Pair<String,String> getKeyValueForClass(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Get the kvMap.Entryin the descriptor builder for the specified class- Parameters:
builder-TableDescriptorBuilderto readclazz- To search for- Returns:
- The
Pairof <key, value> in the table, if that class is present.nullotherwise.
-
add
@SafeVarargs public static TableDescriptorBuilder add(TableDescriptorBuilder builder, Class<? extends Constraint>... constraints) throws IOException Add configuration-less constraints to the table. This will overwrite any configuration associated with the previous constraint of the same class. Each constraint, when added to the table, will have a specific priority, dictating the order in which theConstraintwill be run. AConstraintearlier in the list will be run before those later in the list. The same logic applies between two Constraints over time (earlier added is run first on the regionserver).- Parameters:
builder-TableDescriptorBuilderto add aConstraintconstraints-Constraintsto add. All constraints are considered automatically enabled on add- Throws:
IOException- If constraint could not be serialized/added to table
-
add
@SafeVarargs public static TableDescriptorBuilder add(TableDescriptorBuilder builder, Pair<Class<? extends Constraint>, org.apache.hadoop.conf.Configuration>... constraints) throws IOExceptionAdd constraints and their associated configurations to the table.Adding the same constraint class twice will overwrite the first constraint's configuration
Each constraint, when added to the table, will have a specific priority, dictating the order in which the
Constraintwill be run. AConstraintearlier in the list will be run before those later in the list. The same logic applies between two Constraints over time (earlier added is run first on the regionserver).- Parameters:
builder-TableDescriptorBuilderto add aConstraintconstraints-Pairof aConstraintand its associatedConfiguration. The Constraint will be configured on load with the specified configuration.All constraints are considered automatically enabled on add- Throws:
IOException- if any constraint could not be deserialized. Assumes if 1 constraint is not loaded properly, something has gone terribly wrong and that all constraints need to be enforced.
-
add
public static TableDescriptorBuilder add(TableDescriptorBuilder builder, Class<? extends Constraint> constraint, org.apache.hadoop.conf.Configuration conf) throws IOException Add aConstraintto the table with the given configuration Each constraint, when added to the table, will have a specific priority, dictating the order in which theConstraintwill be run. AConstraintadded will run on the regionserver before those added to theTableDescriptorBuilderlater.- Parameters:
builder-TableDescriptorBuilderto add aConstraintconstraint- to be addedconf- configuration associated with the constraint- Throws:
IOException- if any constraint could not be deserialized. Assumes if 1 constraint is not loaded properly, something has gone terribly wrong and that all constraints need to be enforced.
-
addConstraint
private static TableDescriptorBuilder addConstraint(TableDescriptorBuilder builder, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration conf, long priority) throws IOException Write the raw constraint and configuration to the descriptor. This method takes care of creating a new configuration based on the passed in configuration and then updating that with enabled and priority of the constraint. When a constraint is added, it is automatically enabled.- Throws:
IOException
-
configure
private static org.apache.hadoop.conf.Configuration configure(org.apache.hadoop.conf.Configuration conf, boolean enabled, long priority) Setup the configuration for a constraint as to whether it is enabled and its priority- Parameters:
conf- on which to base the new configurationenabled- true if it should be runpriority- relative to other constraints- Returns:
- a new configuration, storable in the
TableDescriptor
-
serializeConstraintClass
Just write the class to a String representation of the class as a key for theTableDescriptor- Parameters:
clazz- Constraint class to convert to aTableDescriptorkey- Returns:
- key to store in the
TableDescriptor
-
writeConstraint
private static TableDescriptorBuilder writeConstraint(TableDescriptorBuilder builder, String key, org.apache.hadoop.conf.Configuration conf) throws IOException Write the given key and associated configuration to theTableDescriptorBuilder.- Throws:
IOException
-
serializeConfiguration
private static String serializeConfiguration(org.apache.hadoop.conf.Configuration conf) throws IOException Write the configuration to a String- Parameters:
conf- to write- Returns:
- String representation of that configuration
- Throws:
IOException
-
readConfiguration
private static org.apache.hadoop.conf.Configuration readConfiguration(byte[] bytes) throws IOException Read theConfigurationstored in the byte stream.- Parameters:
bytes- to read from- Returns:
- A valid configuration
- Throws:
IOException
-
readConfiguration
private static org.apache.hadoop.conf.Configuration readConfiguration(String bytes) throws IOException Read in the configuration from the String encoded configuration- Parameters:
bytes- to read from- Returns:
- A valid configuration
- Throws:
IOException- if the configuration could not be read
-
getNextPriority
-
updateLatestPriority
private static TableDescriptorBuilder updateLatestPriority(TableDescriptorBuilder builder, long priority) -
setConfiguration
public static TableDescriptorBuilder setConfiguration(TableDescriptorBuilder builder, Class<? extends Constraint> clazz, org.apache.hadoop.conf.Configuration configuration) throws IOException, IllegalArgumentException Update the configuration for theConstraint; does not change the order in which the constraint is run.- Parameters:
builder-TableDescriptorBuilderto updateclazz-Constraintto updateconfiguration- to update theConstraintwith.- Throws:
IOException- if the Constraint was not stored correctlyIllegalArgumentException- if the Constraint was not present on this table.
-
remove
public static TableDescriptorBuilder remove(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Remove the constraint (and associated information) for the table descriptor.- Parameters:
builder-TableDescriptorBuilderto modifyclazz-Constraintclass to remove
-
enableConstraint
public static void enableConstraint(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) throws IOException Enable the givenConstraint. Retains all the information (e.g. Configuration) for theConstraint, but makes sure that it gets loaded on the table.- Parameters:
builder-TableDescriptorBuilderto modifyclazz-Constraintto enable- Throws:
IOException- If the constraint cannot be properly deserialized
-
disableConstraint
public static void disableConstraint(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) throws IOException Disable the givenConstraint. Retains all the information (e.g. Configuration) for theConstraint, but it just doesn't load theConstrainton the table.- Parameters:
builder-TableDescriptorBuilderto modifyclazz-Constraintto disable.- Throws:
IOException- if the constraint cannot be found
-
changeConstraintEnabled
private static TableDescriptorBuilder changeConstraintEnabled(TableDescriptorBuilder builder, Class<? extends Constraint> clazz, boolean enabled) throws IOException Change the whether the constraint (if it is already present) is enabled or disabled.- Throws:
IOException
-
enabled
public static boolean enabled(TableDescriptor desc, Class<? extends Constraint> clazz) throws IOException Check to see if the given constraint is enabled.- Parameters:
desc-TableDescriptorto check.clazz-Constraintto check for- Returns:
- true if the
Constraintis present and enabled. false otherwise. - Throws:
IOException- If the constraint has improperly stored in the table
-
getConstraints
static List<? extends Constraint> getConstraints(TableDescriptor desc, ClassLoader classloader) throws IOException Get the constraints stored in the table descriptor- Parameters:
desc- To read fromclassloader- To use when loading classes. If a special classloader is used on a region, for instance, then that should be the classloader used to load the constraints. This could also apply to unit-testing situation, where want to ensure that class is reloaded or not.- Returns:
- List of configured
Constraints - Throws:
IOException- if any part of reading/arguments fails
-