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
Modifier and TypeFieldDescriptionprivate static final Pattern
private static final String
private static final Comparator<Constraint>
private static String
private static final int
private static final String
private static final org.slf4j.Logger
private static final long
private static final String
private static final long
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic TableDescriptorBuilder
add
(TableDescriptorBuilder builder, Class<? extends Constraint>... constraints) Add configuration-less constraints to the table.static TableDescriptorBuilder
add
(TableDescriptorBuilder builder, Class<? extends Constraint> constraint, org.apache.hadoop.conf.Configuration conf) Add aConstraint
to the table with the given configurationstatic TableDescriptorBuilder
add
(TableDescriptorBuilder builder, Pair<Class<? extends Constraint>, org.apache.hadoop.conf.Configuration>... constraints) Add constraints and their associated configurations to the table.private static TableDescriptorBuilder
addConstraint
(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 TableDescriptorBuilder
changeConstraintEnabled
(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.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 prioritystatic TableDescriptorBuilder
disable
(TableDescriptorBuilder builder) Turn off processing constraints for a given table, even if constraints have been turned on or added.static void
disableConstraint
(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Disable the givenConstraint
.static TableDescriptorBuilder
enable
(TableDescriptorBuilder builder) Enable constraints on a table.static void
enableConstraint
(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Enable the givenConstraint
.static boolean
enabled
(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.Entry
in the descriptor builder for the specified classgetKeyValueForClass
(TableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entry
in the descriptor for the specified classprivate static long
getNextPriority
(TableDescriptorBuilder builder) static boolean
has
(TableDescriptor desc, Class<? extends Constraint> clazz) Check to see if the Constraint is currently set.private static org.apache.hadoop.conf.Configuration
readConfiguration
(byte[] bytes) Read theConfiguration
stored in the byte stream.private static org.apache.hadoop.conf.Configuration
readConfiguration
(String bytes) Read in the configuration from the String encoded configurationstatic TableDescriptorBuilder
remove
(TableDescriptorBuilder builder) Remove allConstraints
that have been added to the table and turn off the constraint processing.static TableDescriptorBuilder
remove
(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Remove the constraint (and associated information) for the table descriptor.private static String
serializeConfiguration
(org.apache.hadoop.conf.Configuration conf) Write the configuration to a Stringprivate static String
serializeConstraintClass
(Class<? extends Constraint> clazz) Just write the class to a String representation of the class as a key for theTableDescriptor
static TableDescriptorBuilder
setConfiguration
(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 TableDescriptorBuilder
updateLatestPriority
(TableDescriptorBuilder builder, long priority) private static TableDescriptorBuilder
writeConstraint
(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 allConstraints
that have been added to the table and turn off the constraint processing. AllConfigurations
and their associatedConstraint
are removed.- Throws:
IOException
-
has
Check to see if the Constraint is currently set.- Parameters:
desc
-TableDescriptor
to checkclazz
-Constraint
class to check for.- Returns:
- true if the
Constraint
is present, even if it is disabled. false otherwise.
-
getKeyValueForClass
private static Pair<String,String> getKeyValueForClass(TableDescriptor desc, Class<? extends Constraint> clazz) Get the kvMap.Entry
in the descriptor for the specified class- Parameters:
desc
-TableDescriptor
to readclazz
- To search for- Returns:
- The
Pair
of <key, value> in the table, if that class is present.null
otherwise.
-
getKeyValueForClass
private static Pair<String,String> getKeyValueForClass(TableDescriptorBuilder builder, Class<? extends Constraint> clazz) Get the kvMap.Entry
in the descriptor builder for the specified class- Parameters:
builder
-TableDescriptorBuilder
to readclazz
- To search for- Returns:
- The
Pair
of <key, value> in the table, if that class is present.null
otherwise.
-
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 theConstraint
will be run. AConstraint
earlier 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
-TableDescriptorBuilder
to add aConstraint
constraints
-Constraints
to 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
Constraint
will be run. AConstraint
earlier 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
-TableDescriptorBuilder
to add aConstraint
constraints
-Pair
of aConstraint
and 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 aConstraint
to the table with the given configuration Each constraint, when added to the table, will have a specific priority, dictating the order in which theConstraint
will be run. AConstraint
added will run on the regionserver before those added to theTableDescriptorBuilder
later.- Parameters:
builder
-TableDescriptorBuilder
to add aConstraint
constraint
- 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 aTableDescriptor
key- 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 theConfiguration
stored 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
-TableDescriptorBuilder
to updateclazz
-Constraint
to updateconfiguration
- to update theConstraint
with.- 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
-TableDescriptorBuilder
to modifyclazz
-Constraint
class 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
-TableDescriptorBuilder
to modifyclazz
-Constraint
to 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 theConstraint
on the table.- Parameters:
builder
-TableDescriptorBuilder
to modifyclazz
-Constraint
to 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
-TableDescriptor
to check.clazz
-Constraint
to check for- Returns:
- true if the
Constraint
is 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
-