Updated Branches: refs/heads/1.5.1-SNAPSHOT f624d402e -> f43ba3bb7
ACCUMULO-1933 Make unit on memory parameters case-insensitive. 1. Added support for both cases for G, M, K and B 2. Added warning message for lower case b and that the code will consider this bytes 3. Added meaningful error message for any number formatting issues 4. Added unit test that test 1 and 3 from above. modified: src/core/src/main/java/org/apache/accumulo/core/conf/AccumuloConfiguration.java new file: src/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/f43ba3bb Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/f43ba3bb Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/f43ba3bb Branch: refs/heads/1.5.1-SNAPSHOT Commit: f43ba3bb77f3cdcff365348252f9504be9dc3a60 Parents: f624d40 Author: tmalaska <ted.mala...@cloudera.com> Authored: Thu Jan 2 15:43:08 2014 -0500 Committer: John Vines <vi...@apache.org> Committed: Mon Jan 6 18:27:29 2014 -0500 ---------------------------------------------------------------------- .../core/conf/AccumuloConfiguration.java | 35 +++++++++----- .../core/conf/AccumuloConfigurationTest.java | 48 ++++++++++++++++++++ 2 files changed, 72 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/f43ba3bb/core/src/main/java/org/apache/accumulo/core/conf/AccumuloConfiguration.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/conf/AccumuloConfiguration.java b/core/src/main/java/org/apache/accumulo/core/conf/AccumuloConfiguration.java index 28f24ef..0a1c7fd 100644 --- a/core/src/main/java/org/apache/accumulo/core/conf/AccumuloConfiguration.java +++ b/core/src/main/java/org/apache/accumulo/core/conf/AccumuloConfiguration.java @@ -71,17 +71,30 @@ public abstract class AccumuloConfiguration implements Iterable<Entry<String,Str static public long getMemoryInBytes(String str) { int multiplier = 0; - switch (str.charAt(str.length() - 1)) { - case 'G': - multiplier += 10; - case 'M': - multiplier += 10; - case 'K': - multiplier += 10; - case 'B': - return Long.parseLong(str.substring(0, str.length() - 1)) << multiplier; - default: - return Long.parseLong(str); + char lastChar = str.charAt(str.length() - 1); + + if (lastChar == 'b') { + log.warn("The 'b' in " + str + + " is being considered as bytes. " + + "Setting memory by bits is not supported"); + } + try { + switch (Character.toUpperCase(lastChar)) { + case 'G': + multiplier += 10; + case 'M': + multiplier += 10; + case 'K': + multiplier += 10; + case 'B': + return Long.parseLong(str.substring(0, str.length() - 1)) << multiplier; + default: + return Long.parseLong(str); + } + } catch (Exception ex) { + throw new IllegalArgumentException("The value '" + str + + "' is not a valid memory setting. A valid value would a number " + + "possibily followed by an optional 'G', 'M', 'K', or 'B'."); } } http://git-wip-us.apache.org/repos/asf/accumulo/blob/f43ba3bb/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java b/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java new file mode 100644 index 0000000..a115215 --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java @@ -0,0 +1,48 @@ +/* + * 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.core.conf; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class AccumuloConfigurationTest { + + @Test + public void testGetMemoryInBytes() throws Exception { + assertEquals(42l, AccumuloConfiguration.getMemoryInBytes("42")); + assertEquals(42l, AccumuloConfiguration.getMemoryInBytes("42b")); + assertEquals(42l, AccumuloConfiguration.getMemoryInBytes("42B")); + assertEquals(42l * 1024l, AccumuloConfiguration.getMemoryInBytes("42K")); + assertEquals(42l * 1024l, AccumuloConfiguration.getMemoryInBytes("42k")); + assertEquals(42l * 1024l * 1024l, AccumuloConfiguration.getMemoryInBytes("42M")); + assertEquals(42l * 1024l * 1024l, AccumuloConfiguration.getMemoryInBytes("42m")); + assertEquals(42l * 1024l * 1024l * 1024l, AccumuloConfiguration.getMemoryInBytes("42G")); + assertEquals(42l * 1024l * 1024l * 1024l, AccumuloConfiguration.getMemoryInBytes("42g")); + + } + + @Test(expected = IllegalArgumentException.class) + public void testGetMemoryInBytesFailureCases1() throws Exception { + AccumuloConfiguration.getMemoryInBytes("42x"); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetMemoryInBytesFailureCases2() throws Exception { + AccumuloConfiguration.getMemoryInBytes("FooBar"); + } +} \ No newline at end of file