change getMemorySize to getMemorySizeInBytes

Preserves the memory size vs. disk size distinction (powers of two vs. ten)
But specifies that it will be in bytes, for consistency with getMilliseconds
and clarity in general.
This commit is contained in:
Havoc Pennington 2011-11-15 16:37:06 -05:00
parent 598c5d045f
commit 68a22668bd
5 changed files with 27 additions and 23 deletions

View File

@ -247,7 +247,9 @@ public final class Config {
* Parses a memory-size string. If no units are specified in the string, it
* is assumed to be in bytes. The returned value is in bytes. The purpose of
* this function is to implement the memory-size-related methods in the
* ConfigObject interface.
* ConfigObject interface. The units parsed are interpreted as powers of
* two, that is, the convention for memory rather than the convention for
* disk space.
*
* @param input
* the string to parse
@ -259,7 +261,7 @@ public final class Config {
* @throws ConfigException
* if string is invalid
*/
public static long parseMemorySize(String input,
public static long parseMemorySizeInBytes(String input,
ConfigOrigin originForException, String pathForException) {
String s = ConfigUtil.unicodeTrim(input);
String unitStringMaybePlural = getUnits(s);

View File

@ -68,10 +68,12 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
*/
ConfigValue getValue(String path);
/** Get value as a size in bytes (parses special strings like "128M") */
// rename getSizeInBytes ? clearer. allows a megabyte version
// or just getBytes is consistent with getMilliseconds
Long getMemorySize(String path);
/**
* Get value as a size in bytes (parses special strings like "128M"). The
* size units are interpreted as for memory, not as for disk space, so they
* are in powers of two.
*/
Long getMemorySizeInBytes(String path);
/**
* Get value as a duration in milliseconds. If the value is already a
@ -114,7 +116,7 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
List<? extends Object> getAnyRefList(String path);
List<Long> getMemorySizeList(String path);
List<Long> getMemorySizeInBytesList(String path);
List<Long> getMillisecondsList(String path);

View File

@ -414,13 +414,13 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
}
@Override
public Long getMemorySize(String path) {
public Long getMemorySizeInBytes(String path) {
Long size = null;
try {
size = getLong(path);
} catch (ConfigException.WrongType e) {
ConfigValue v = find(path, ConfigValueType.STRING, path);
size = Config.parseMemorySize((String) v.unwrapped(), v.origin(),
size = Config.parseMemorySizeInBytes((String) v.unwrapped(), v.origin(),
path);
}
return size;
@ -534,7 +534,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
}
@Override
public List<Long> getMemorySizeList(String path) {
public List<Long> getMemorySizeInBytesList(String path) {
List<Long> l = new ArrayList<Long>();
List<? extends ConfigValue> list = getList(path);
for (ConfigValue v : list) {
@ -542,7 +542,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
l.add(((Number) v.unwrapped()).longValue());
} else if (v.valueType() == ConfigValueType.STRING) {
String s = (String) v.unwrapped();
Long n = Config.parseMemorySize(s, v.origin(), path);
Long n = Config.parseMemorySizeInBytes(s, v.origin(), path);
l.add(n);
} else {
throw new ConfigException.WrongType(v.origin(), path,

View File

@ -465,7 +465,7 @@ class ConfigTest extends TestUtils {
}
intercept[ConfigException.Null] {
conf.getMemorySize("nulls.null")
conf.getMemorySizeInBytes("nulls.null")
}
// should throw WrongType if key is wrong type and not convertible
@ -494,7 +494,7 @@ class ConfigTest extends TestUtils {
}
intercept[ConfigException.WrongType] {
conf.getMemorySize("ints")
conf.getMemorySizeInBytes("ints")
}
// should throw BadPath on various bad paths
@ -521,7 +521,7 @@ class ConfigTest extends TestUtils {
}
intercept[ConfigException.BadValue] {
conf.getMemorySize("strings.a")
conf.getMemorySizeInBytes("strings.a")
}
}
@ -581,11 +581,11 @@ class ConfigTest extends TestUtils {
assertEquals(500L, conf.getMilliseconds("durations.halfSecond"))
// should get size in bytes
assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.meg"))
assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.megAsNumber"))
assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.meg"))
assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.megAsNumber"))
assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L),
conf.getMemorySizeList("memsizes.megsList").asScala)
assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg"))
conf.getMemorySizeInBytesList("memsizes.megsList").asScala)
assertEquals(512 * 1024L, conf.getMemorySizeInBytes("memsizes.halfMeg"))
}
@Test

View File

@ -38,7 +38,7 @@ class UnitParserTest extends TestUtils {
}
@Test
def parseMemorySize() {
def parseMemorySizeInBytes() {
val oneMegs = List("1048576", "1048576b", "1048576bytes", "1048576byte",
"1048576 b", "1048576 bytes",
" 1048576 b ", " 1048576 bytes ",
@ -47,7 +47,7 @@ class UnitParserTest extends TestUtils {
"1m", "1M", "1 M", "1 megabytes", "1 megabyte",
"0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte")
def parseMem(s: String) = Config.parseMemorySize(s, fakeOrigin(), "test")
def parseMem(s: String) = Config.parseMemorySizeInBytes(s, fakeOrigin(), "test")
for (s <- oneMegs) {
val result = parseMem(s)
@ -61,13 +61,13 @@ class UnitParserTest extends TestUtils {
// bad units
val e = intercept[ConfigException.BadValue] {
Config.parseMemorySize("100 dollars", fakeOrigin(), "test")
Config.parseMemorySizeInBytes("100 dollars", fakeOrigin(), "test")
}
assertTrue(e.getMessage().contains("size unit"))
// bad number
val e2 = intercept[ConfigException.BadValue] {
Config.parseMemorySize("1 00 bytes", fakeOrigin(), "test")
Config.parseMemorySizeInBytes("1 00 bytes", fakeOrigin(), "test")
}
assertTrue(e2.getMessage().contains("size number"))
}