Repository: accumulo Updated Branches: refs/heads/master 9e631533b -> 991dbded1
ACCUMULO-2970 move Validators to the master module, clean up small nits in Tables Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/991dbded Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/991dbded Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/991dbded Branch: refs/heads/master Commit: 991dbded192f3f1ebce0ca5c2fb7d954195990c2 Parents: 9e63153 Author: Eric C. Newton <eric.new...@gmail.com> Authored: Wed Jul 2 12:45:07 2014 -0400 Committer: Eric C. Newton <eric.new...@gmail.com> Committed: Wed Jul 2 12:45:07 2014 -0400 ---------------------------------------------------------------------- .../accumulo/core/client/impl/Tables.java | 82 ++--------------- .../accumulo/master/FateServiceHandler.java | 33 ++++--- .../accumulo/master/util/TableValidators.java | 97 ++++++++++++++++++++ 3 files changed, 123 insertions(+), 89 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/991dbded/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java b/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java index c99738c..0b522ca 100644 --- a/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java +++ b/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java @@ -33,8 +33,6 @@ import org.apache.accumulo.core.client.NamespaceNotFoundException; import org.apache.accumulo.core.client.TableNotFoundException; import org.apache.accumulo.core.master.state.tables.TableState; import org.apache.accumulo.core.metadata.MetadataTable; -import org.apache.accumulo.core.metadata.RootTable; -import org.apache.accumulo.core.util.Validator; import org.apache.accumulo.core.util.Pair; import org.apache.accumulo.core.zookeeper.ZooUtil; import org.apache.accumulo.fate.zookeeper.ZooCache; @@ -42,79 +40,13 @@ import org.apache.accumulo.fate.zookeeper.ZooCacheFactory; import org.apache.log4j.Logger; public class Tables { - public static final String VALID_NAME_REGEX = "^(\\w+\\.)?(\\w+)$"; - public static final String VALID_ID_REGEX = "^([a-z0-9]+)$"; // BigDecimal base36 - public static final Validator<String> VALID_NAME = new Validator<String>() { - @Override - public boolean isValid(String tableName) { - return tableName != null && tableName.matches(VALID_NAME_REGEX); - } - - @Override - public String invalidMessage(String tableName) { - if (tableName == null) - return "Table name cannot be null"; - return "Table names must only contain word characters (letters, digits, and underscores): " + tableName; - } - }; - - public static final Validator<String> VALID_ID = new Validator<String>() { - @Override - public boolean isValid(String tableId) { - return tableId != null && (RootTable.ID.equals(tableId) || MetadataTable.ID.equals(tableId) || tableId.matches(VALID_ID_REGEX)); - } - - @Override - public String invalidMessage(String tableId) { - if (tableId == null) - return "Table id cannot be null"; - return "Table IDs are base-36 numbers, represented with lowercase alphanumeric digits: " + tableId; - } - }; - - public static final Validator<String> NOT_SYSTEM = new Validator<String>() { - - @Override - public boolean isValid(String tableName) { - return !Namespaces.ACCUMULO_NAMESPACE.equals(Tables.qualify(tableName).getFirst()); - } - - @Override - public String invalidMessage(String tableName) { - return "Table cannot be in the " + Namespaces.ACCUMULO_NAMESPACE + " namespace"; - } - }; - - public static final Validator<String> NOT_ROOT = new Validator<String>() { - - @Override - public boolean isValid(String tableName) { - return !RootTable.NAME.equals(tableName); - } - - @Override - public String invalidMessage(String tableName) { - return "Table cannot be the " + RootTable.NAME + "(Id: " + RootTable.ID + ") table"; - } - }; - - public static final Validator<String> NOT_ROOT_ID = new Validator<String>() { - - @Override - public boolean isValid(String tableId) { - return !RootTable.ID.equals(tableId); - } - - @Override - public String invalidMessage(String tableId) { - return "Table cannot be the " + RootTable.NAME + "(Id: " + RootTable.ID + ") table"; - } - }; - - private static SecurityPermission TABLES_PERMISSION = new SecurityPermission("tablesPermission"); - private static AtomicLong cacheResetCount = new AtomicLong(0); private static final Logger log = Logger.getLogger(Tables.class); + public static final String VALID_NAME_REGEX = "^(\\w+\\.)?(\\w+)$"; + + private static final SecurityPermission TABLES_PERMISSION = new SecurityPermission("tablesPermission"); + private static final AtomicLong cacheResetCount = new AtomicLong(0); + private static ZooCache getZooCache(Instance instance) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { @@ -265,7 +197,7 @@ public class Tables { } public static String qualified(String tableName, String defaultNamespace) { - Pair<String,String> qualifiedTableName = Tables.qualify(tableName, defaultNamespace); + Pair<String,String> qualifiedTableName = qualify(tableName, defaultNamespace); if (Namespaces.DEFAULT_NAMESPACE.equals(qualifiedTableName.getFirst())) return qualifiedTableName.getSecond(); else @@ -277,7 +209,7 @@ public class Tables { } public static Pair<String,String> qualify(String tableName, String defaultNamespace) { - if (!tableName.matches(Tables.VALID_NAME_REGEX)) + if (!tableName.matches(VALID_NAME_REGEX)) throw new IllegalArgumentException("Invalid table name '" + tableName + "'"); if (MetadataTable.OLD_NAME.equals(tableName)) tableName = MetadataTable.NAME; http://git-wip-us.apache.org/repos/asf/accumulo/blob/991dbded/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java ---------------------------------------------------------------------- diff --git a/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java b/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java index e5725f7..5562a6a 100644 --- a/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java +++ b/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java @@ -16,6 +16,11 @@ */ package org.apache.accumulo.master; +import static org.apache.accumulo.master.util.TableValidators.NOT_ROOT_ID; +import static org.apache.accumulo.master.util.TableValidators.NOT_SYSTEM; +import static org.apache.accumulo.master.util.TableValidators.VALID_ID; +import static org.apache.accumulo.master.util.TableValidators.VALID_NAME; + import java.nio.ByteBuffer; import java.util.HashMap; import java.util.HashSet; @@ -26,9 +31,9 @@ import java.util.Set; import org.apache.accumulo.core.client.IteratorSetting; import org.apache.accumulo.core.client.NamespaceNotFoundException; -import org.apache.accumulo.core.client.impl.TableOperationsImpl; import org.apache.accumulo.core.client.admin.TimeType; import org.apache.accumulo.core.client.impl.Namespaces; +import org.apache.accumulo.core.client.impl.TableOperationsImpl; import org.apache.accumulo.core.client.impl.Tables; import org.apache.accumulo.core.client.impl.thrift.SecurityErrorCode; import org.apache.accumulo.core.client.impl.thrift.TableOperation; @@ -123,7 +128,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_CREATE: { TableOperation tableOp = TableOperation.CREATE; - String tableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + String tableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); TimeType timeType = TimeType.valueOf(ByteBufferUtil.toString(arguments.get(1))); String namespaceId; @@ -143,7 +148,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_RENAME: { TableOperation tableOp = TableOperation.RENAME; - final String oldTableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + final String oldTableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); String newTableName = validateTableNameArgument(arguments.get(1), tableOp, new Validator<String>() { @Override @@ -176,8 +181,8 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_CLONE: { TableOperation tableOp = TableOperation.CLONE; - String srcTableId = validateTableIdArgument(arguments.get(0), tableOp, Tables.NOT_ROOT_ID); - String tableName = validateTableNameArgument(arguments.get(1), tableOp, Tables.NOT_SYSTEM); + String srcTableId = validateTableIdArgument(arguments.get(0), tableOp, NOT_ROOT_ID); + String tableName = validateTableNameArgument(arguments.get(1), tableOp, NOT_SYSTEM); String namespaceId; try { namespaceId = Namespaces.getNamespaceId(master.getInstance(), Tables.qualify(tableName).getFirst()); @@ -213,7 +218,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_DELETE: { TableOperation tableOp = TableOperation.DELETE; - String tableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + String tableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); final String tableId = ClientServiceHandler.checkTableId(master.getInstance(), tableName, tableOp); String namespaceId = Tables.getNamespaceId(master.getInstance(), tableId); @@ -225,7 +230,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_ONLINE: { TableOperation tableOp = TableOperation.ONLINE; - final String tableId = validateTableIdArgument(arguments.get(0), tableOp, Tables.NOT_ROOT_ID); + final String tableId = validateTableIdArgument(arguments.get(0), tableOp, NOT_ROOT_ID); String namespaceId = Tables.getNamespaceId(master.getInstance(), tableId); if (!master.security.canOnlineOfflineTable(c, tableId, op, namespaceId)) @@ -236,7 +241,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_OFFLINE: { TableOperation tableOp = TableOperation.OFFLINE; - final String tableId = validateTableIdArgument(arguments.get(0), tableOp, Tables.NOT_ROOT_ID); + final String tableId = validateTableIdArgument(arguments.get(0), tableOp, NOT_ROOT_ID); String namespaceId = Tables.getNamespaceId(master.getInstance(), tableId); if (!master.security.canOnlineOfflineTable(c, tableId, op, namespaceId)) @@ -263,7 +268,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_DELETE_RANGE: { TableOperation tableOp = TableOperation.DELETE_RANGE; - String tableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + String tableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); Text startRow = ByteBufferUtil.toText(arguments.get(1)); Text endRow = ByteBufferUtil.toText(arguments.get(2)); @@ -278,7 +283,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_BULK_IMPORT: { TableOperation tableOp = TableOperation.BULK_IMPORT; - String tableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + String tableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); String dir = ByteBufferUtil.toString(arguments.get(1)); String failDir = ByteBufferUtil.toString(arguments.get(2)); boolean setTime = Boolean.parseBoolean(ByteBufferUtil.toString(arguments.get(3))); @@ -319,7 +324,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_IMPORT: { TableOperation tableOp = TableOperation.IMPORT; - String tableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + String tableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); String exportDir = ByteBufferUtil.toString(arguments.get(1)); String namespaceId; try { @@ -336,7 +341,7 @@ class FateServiceHandler implements FateService.Iface { } case TABLE_EXPORT: { TableOperation tableOp = TableOperation.EXPORT; - String tableName = validateTableNameArgument(arguments.get(0), tableOp, Tables.NOT_SYSTEM); + String tableName = validateTableNameArgument(arguments.get(0), tableOp, NOT_SYSTEM); String exportDir = ByteBufferUtil.toString(arguments.get(1)); String tableId = ClientServiceHandler.checkTableId(master.getInstance(), tableName, tableOp); @@ -393,7 +398,7 @@ class FateServiceHandler implements FateService.Iface { private String validateTableIdArgument(ByteBuffer tableIdArg, TableOperation op, Validator<String> userValidator) throws ThriftTableOperationException { String tableId = tableIdArg == null ? null : ByteBufferUtil.toString(tableIdArg); try { - return Tables.VALID_ID.and(userValidator).validate(tableId); + return VALID_ID.and(userValidator).validate(tableId); } catch (IllegalArgumentException e) { String why = e.getMessage(); log.warn(why); @@ -404,7 +409,7 @@ class FateServiceHandler implements FateService.Iface { // Verify table name arguments are valid, and match any additional restrictions private String validateTableNameArgument(ByteBuffer tableNameArg, TableOperation op, Validator<String> userValidator) throws ThriftTableOperationException { String tableName = tableNameArg == null ? null : ByteBufferUtil.toString(tableNameArg); - return _validateArgument(tableName, op, Tables.VALID_NAME.and(userValidator)); + return _validateArgument(tableName, op, VALID_NAME.and(userValidator)); } // Verify namespace arguments are valid, and match any additional restrictions http://git-wip-us.apache.org/repos/asf/accumulo/blob/991dbded/server/master/src/main/java/org/apache/accumulo/master/util/TableValidators.java ---------------------------------------------------------------------- diff --git a/server/master/src/main/java/org/apache/accumulo/master/util/TableValidators.java b/server/master/src/main/java/org/apache/accumulo/master/util/TableValidators.java new file mode 100644 index 0000000..162b813 --- /dev/null +++ b/server/master/src/main/java/org/apache/accumulo/master/util/TableValidators.java @@ -0,0 +1,97 @@ +/* + * 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.accumulo.master.util; + +import static org.apache.accumulo.core.client.impl.Tables.VALID_NAME_REGEX; +import static org.apache.accumulo.core.client.impl.Tables.qualify; + +import org.apache.accumulo.core.client.impl.Namespaces; +import org.apache.accumulo.core.metadata.MetadataTable; +import org.apache.accumulo.core.metadata.RootTable; +import org.apache.accumulo.core.util.Validator; + +public class TableValidators { + public static final String VALID_ID_REGEX = "^([a-z0-9]+)$"; // BigDecimal base36 + + public static final Validator<String> VALID_NAME = new Validator<String>() { + @Override + public boolean isValid(String tableName) { + return tableName != null && tableName.matches(VALID_NAME_REGEX); + } + + @Override + public String invalidMessage(String tableName) { + if (tableName == null) + return "Table name cannot be null"; + return "Table names must only contain word characters (letters, digits, and underscores): " + tableName; + } + }; + + public static final Validator<String> VALID_ID = new Validator<String>() { + @Override + public boolean isValid(String tableId) { + return tableId != null && (RootTable.ID.equals(tableId) || MetadataTable.ID.equals(tableId) || tableId.matches(VALID_ID_REGEX)); + } + + @Override + public String invalidMessage(String tableId) { + if (tableId == null) + return "Table id cannot be null"; + return "Table IDs are base-36 numbers, represented with lowercase alphanumeric digits: " + tableId; + } + }; + + public static final Validator<String> NOT_SYSTEM = new Validator<String>() { + + @Override + public boolean isValid(String tableName) { + return !Namespaces.ACCUMULO_NAMESPACE.equals(qualify(tableName).getFirst()); + } + + @Override + public String invalidMessage(String tableName) { + return "Table cannot be in the " + Namespaces.ACCUMULO_NAMESPACE + " namespace"; + } + }; + + public static final Validator<String> NOT_ROOT = new Validator<String>() { + + @Override + public boolean isValid(String tableName) { + return !RootTable.NAME.equals(tableName); + } + + @Override + public String invalidMessage(String tableName) { + return "Table cannot be the " + RootTable.NAME + "(Id: " + RootTable.ID + ") table"; + } + }; + + public static final Validator<String> NOT_ROOT_ID = new Validator<String>() { + + @Override + public boolean isValid(String tableId) { + return !RootTable.ID.equals(tableId); + } + + @Override + public String invalidMessage(String tableId) { + return "Table cannot be the " + RootTable.NAME + "(Id: " + RootTable.ID + ") table"; + } + }; + +}