From 92aee4f540e7480a7daa60291cac3b7467a69cb6 Mon Sep 17 00:00:00 2001 From: Zach York Date: Wed, 6 Sep 2017 17:48:05 -0700 Subject: [PATCH] HBASE-18773 Add utility method to determine if a TableName is a meta table Signed-off-by: Michael Stack --- .../java/org/apache/hadoop/hbase/TableName.java | 7 ++++ .../util/ReadReplicaClustersTableNameUtil.java | 35 ++++++++++++++++++++ .../org/apache/hadoop/hbase/TestTableName.java | 6 ++++ .../util/TestReadReplicaClustersTableNameUtil.java | 37 ++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadReplicaClustersTableNameUtil.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestReadReplicaClustersTableNameUtil.java diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java index 79d264faee..565d9a990c 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java @@ -551,4 +551,11 @@ public final class TableName implements Comparable { } return KeyValue.COMPARATOR; } + + /** + * @return True if this is the hbase:meta table name. + */ + public boolean isMeta() { + return isMetaTableName(this); + } } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadReplicaClustersTableNameUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadReplicaClustersTableNameUtil.java new file mode 100644 index 0000000000..06db8fa44b --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadReplicaClustersTableNameUtil.java @@ -0,0 +1,35 @@ +/** + * 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.hadoop.hbase.util; + +import org.apache.hadoop.hbase.NamespaceDescriptor; +import org.apache.hadoop.hbase.TableName; + +public class ReadReplicaClustersTableNameUtil { + + /** + * Utility method to determine if TableName is a meta TableName without taking hbase.meta.table.suffix into account. + * @param tableName TableName to determine if isMeta + * @return if this TableName contains the default meta table name + */ + public static boolean isMetaTableNameWithoutSuffix(TableName tableName) { + String[] parts = tableName.getNameWithNamespaceInclAsString().split(String.valueOf(TableName.NAMESPACE_DELIM)); + return parts[0].equals(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR) && + parts[1].startsWith(TableName.DEFAULT_META_TABLE_NAME_STR); + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTableName.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTableName.java index 54e25e8c3a..1942d6a6e9 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTableName.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTableName.java @@ -188,4 +188,10 @@ public class TestTableName extends TestWatcher { return expected; } + @Test + public void testIsMeta() { + String userTableContainingMeta = "default:meta"; + assertTrue(TableName.META_TABLE_NAME.isMeta()); + assertFalse(TableName.valueOf(userTableContainingMeta).isMeta()); + } } diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestReadReplicaClustersTableNameUtil.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestReadReplicaClustersTableNameUtil.java new file mode 100644 index 0000000000..bde02023fe --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestReadReplicaClustersTableNameUtil.java @@ -0,0 +1,37 @@ +/** + * 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.hadoop.hbase.util; + +import org.apache.hadoop.hbase.TableName; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class TestReadReplicaClustersTableNameUtil { + + @Test + public void testIsMetaTableNameWithoutSuffix() { + String metaWithSuffix = "hbase:meta_server1"; + String userTableContainingMeta = "default:meta"; + + assertTrue(ReadReplicaClustersTableNameUtil.isMetaTableNameWithoutSuffix(TableName.META_TABLE_NAME)); + assertTrue(ReadReplicaClustersTableNameUtil.isMetaTableNameWithoutSuffix(TableName.valueOf(metaWithSuffix))); + assertFalse(ReadReplicaClustersTableNameUtil.isMetaTableNameWithoutSuffix(TableName.valueOf(userTableContainingMeta))); + } +} -- 2.11.0 (Apple Git-81)