mirror of
https://github.com/lightbend/config.git
synced 2025-01-16 07:10:21 +08:00
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:
parent
598c5d045f
commit
68a22668bd
@ -247,8 +247,10 @@ public final class Config {
|
|||||||
* Parses a memory-size string. If no units are specified in the string, it
|
* 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
|
* 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
|
* 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
|
* @param input
|
||||||
* the string to parse
|
* the string to parse
|
||||||
* @param originForException
|
* @param originForException
|
||||||
@ -259,7 +261,7 @@ public final class Config {
|
|||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* if string is invalid
|
* if string is invalid
|
||||||
*/
|
*/
|
||||||
public static long parseMemorySize(String input,
|
public static long parseMemorySizeInBytes(String input,
|
||||||
ConfigOrigin originForException, String pathForException) {
|
ConfigOrigin originForException, String pathForException) {
|
||||||
String s = ConfigUtil.unicodeTrim(input);
|
String s = ConfigUtil.unicodeTrim(input);
|
||||||
String unitStringMaybePlural = getUnits(s);
|
String unitStringMaybePlural = getUnits(s);
|
||||||
|
@ -68,10 +68,12 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
|
|||||||
*/
|
*/
|
||||||
ConfigValue getValue(String path);
|
ConfigValue getValue(String path);
|
||||||
|
|
||||||
/** Get value as a size in bytes (parses special strings like "128M") */
|
/**
|
||||||
// rename getSizeInBytes ? clearer. allows a megabyte version
|
* Get value as a size in bytes (parses special strings like "128M"). The
|
||||||
// or just getBytes is consistent with getMilliseconds
|
* size units are interpreted as for memory, not as for disk space, so they
|
||||||
Long getMemorySize(String path);
|
* are in powers of two.
|
||||||
|
*/
|
||||||
|
Long getMemorySizeInBytes(String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get value as a duration in milliseconds. If the value is already a
|
* 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<? extends Object> getAnyRefList(String path);
|
||||||
|
|
||||||
List<Long> getMemorySizeList(String path);
|
List<Long> getMemorySizeInBytesList(String path);
|
||||||
|
|
||||||
List<Long> getMillisecondsList(String path);
|
List<Long> getMillisecondsList(String path);
|
||||||
|
|
||||||
|
@ -414,13 +414,13 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getMemorySize(String path) {
|
public Long getMemorySizeInBytes(String path) {
|
||||||
Long size = null;
|
Long size = null;
|
||||||
try {
|
try {
|
||||||
size = getLong(path);
|
size = getLong(path);
|
||||||
} catch (ConfigException.WrongType e) {
|
} catch (ConfigException.WrongType e) {
|
||||||
ConfigValue v = find(path, ConfigValueType.STRING, path);
|
ConfigValue v = find(path, ConfigValueType.STRING, path);
|
||||||
size = Config.parseMemorySize((String) v.unwrapped(), v.origin(),
|
size = Config.parseMemorySizeInBytes((String) v.unwrapped(), v.origin(),
|
||||||
path);
|
path);
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
@ -534,7 +534,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Long> getMemorySizeList(String path) {
|
public List<Long> getMemorySizeInBytesList(String path) {
|
||||||
List<Long> l = new ArrayList<Long>();
|
List<Long> l = new ArrayList<Long>();
|
||||||
List<? extends ConfigValue> list = getList(path);
|
List<? extends ConfigValue> list = getList(path);
|
||||||
for (ConfigValue v : list) {
|
for (ConfigValue v : list) {
|
||||||
@ -542,7 +542,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
|
|||||||
l.add(((Number) v.unwrapped()).longValue());
|
l.add(((Number) v.unwrapped()).longValue());
|
||||||
} else if (v.valueType() == ConfigValueType.STRING) {
|
} else if (v.valueType() == ConfigValueType.STRING) {
|
||||||
String s = (String) v.unwrapped();
|
String s = (String) v.unwrapped();
|
||||||
Long n = Config.parseMemorySize(s, v.origin(), path);
|
Long n = Config.parseMemorySizeInBytes(s, v.origin(), path);
|
||||||
l.add(n);
|
l.add(n);
|
||||||
} else {
|
} else {
|
||||||
throw new ConfigException.WrongType(v.origin(), path,
|
throw new ConfigException.WrongType(v.origin(), path,
|
||||||
|
@ -465,7 +465,7 @@ class ConfigTest extends TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
intercept[ConfigException.Null] {
|
intercept[ConfigException.Null] {
|
||||||
conf.getMemorySize("nulls.null")
|
conf.getMemorySizeInBytes("nulls.null")
|
||||||
}
|
}
|
||||||
|
|
||||||
// should throw WrongType if key is wrong type and not convertible
|
// should throw WrongType if key is wrong type and not convertible
|
||||||
@ -494,7 +494,7 @@ class ConfigTest extends TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
intercept[ConfigException.WrongType] {
|
intercept[ConfigException.WrongType] {
|
||||||
conf.getMemorySize("ints")
|
conf.getMemorySizeInBytes("ints")
|
||||||
}
|
}
|
||||||
|
|
||||||
// should throw BadPath on various bad paths
|
// should throw BadPath on various bad paths
|
||||||
@ -521,7 +521,7 @@ class ConfigTest extends TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
intercept[ConfigException.BadValue] {
|
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"))
|
assertEquals(500L, conf.getMilliseconds("durations.halfSecond"))
|
||||||
|
|
||||||
// should get size in bytes
|
// should get size in bytes
|
||||||
assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.meg"))
|
assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.meg"))
|
||||||
assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.megAsNumber"))
|
assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.megAsNumber"))
|
||||||
assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L),
|
assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L),
|
||||||
conf.getMemorySizeList("memsizes.megsList").asScala)
|
conf.getMemorySizeInBytesList("memsizes.megsList").asScala)
|
||||||
assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg"))
|
assertEquals(512 * 1024L, conf.getMemorySizeInBytes("memsizes.halfMeg"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -38,7 +38,7 @@ class UnitParserTest extends TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def parseMemorySize() {
|
def parseMemorySizeInBytes() {
|
||||||
val oneMegs = List("1048576", "1048576b", "1048576bytes", "1048576byte",
|
val oneMegs = List("1048576", "1048576b", "1048576bytes", "1048576byte",
|
||||||
"1048576 b", "1048576 bytes",
|
"1048576 b", "1048576 bytes",
|
||||||
" 1048576 b ", " 1048576 bytes ",
|
" 1048576 b ", " 1048576 bytes ",
|
||||||
@ -47,7 +47,7 @@ class UnitParserTest extends TestUtils {
|
|||||||
"1m", "1M", "1 M", "1 megabytes", "1 megabyte",
|
"1m", "1M", "1 M", "1 megabytes", "1 megabyte",
|
||||||
"0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte")
|
"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) {
|
for (s <- oneMegs) {
|
||||||
val result = parseMem(s)
|
val result = parseMem(s)
|
||||||
@ -61,13 +61,13 @@ class UnitParserTest extends TestUtils {
|
|||||||
|
|
||||||
// bad units
|
// bad units
|
||||||
val e = intercept[ConfigException.BadValue] {
|
val e = intercept[ConfigException.BadValue] {
|
||||||
Config.parseMemorySize("100 dollars", fakeOrigin(), "test")
|
Config.parseMemorySizeInBytes("100 dollars", fakeOrigin(), "test")
|
||||||
}
|
}
|
||||||
assertTrue(e.getMessage().contains("size unit"))
|
assertTrue(e.getMessage().contains("size unit"))
|
||||||
|
|
||||||
// bad number
|
// bad number
|
||||||
val e2 = intercept[ConfigException.BadValue] {
|
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"))
|
assertTrue(e2.getMessage().contains("size number"))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user