Skip to content

Commit 82eeb50

Browse files
committed
HBASE-28600 Introduce StorageSize, StorageUnit from hadoop 3
1 parent 454cd6b commit 82eeb50

File tree

3 files changed

+720
-0
lines changed

3 files changed

+720
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase;
19+
20+
import static org.apache.commons.lang3.StringUtils.isBlank;
21+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
22+
23+
import java.util.Locale;
24+
import org.apache.yetus.audience.InterfaceAudience;
25+
26+
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
27+
28+
/**
29+
* This class is adapted from the Hadoop 3.x source code to HBase as part of a backporting effort to
30+
* support storage size parsing functionality in older versions of HBase.
31+
* <p>
32+
* Source: <a href=
33+
* "https://github.com/apache/hadoop/blob/branch-3.1.0/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/StorageSize.java">
34+
* Hadoop 3.1.0 StorageSize.java </a>
35+
* </p>
36+
*/
37+
@InterfaceAudience.Private
38+
public class StorageSize {
39+
private final StorageUnit unit;
40+
private final double value;
41+
42+
/**
43+
* Constucts a Storage Measure, which contains the value and the unit of measure.
44+
* @param unit - Unit of Measure
45+
* @param value - Numeric value.
46+
*/
47+
public StorageSize(StorageUnit unit, double value) {
48+
this.unit = unit;
49+
this.value = value;
50+
}
51+
52+
private static void checkState(boolean state, String errorString) {
53+
if (!state) {
54+
throw new IllegalStateException(errorString);
55+
}
56+
}
57+
58+
public static double getStorageSize(String value, double defaultValue, StorageUnit targetUnit) {
59+
Preconditions.checkNotNull(targetUnit, "Conversion unit cannot be null.");
60+
61+
if (isBlank(value)) {
62+
return targetUnit.getDefault(defaultValue);
63+
}
64+
65+
final StorageSize measure = parse(value);
66+
double byteValue = measure.getUnit().toBytes(measure.getValue());
67+
return targetUnit.fromBytes(byteValue);
68+
}
69+
70+
public static StorageSize parse(String value) {
71+
checkState(isNotBlank(value), "value cannot be blank");
72+
String sanitizedValue = value.trim().toLowerCase(Locale.ENGLISH);
73+
StorageUnit parsedUnit = null;
74+
for (StorageUnit unit : StorageUnit.values()) {
75+
if (
76+
sanitizedValue.endsWith(unit.getShortName()) || sanitizedValue.endsWith(unit.getLongName())
77+
|| sanitizedValue.endsWith(unit.getSuffixChar())
78+
) {
79+
parsedUnit = unit;
80+
break;
81+
}
82+
}
83+
84+
if (parsedUnit == null) {
85+
throw new IllegalArgumentException(
86+
value + " is not in expected format." + "Expected format is <number><unit>. e.g. 1000MB");
87+
}
88+
89+
String suffix = "";
90+
boolean found = false;
91+
92+
// We are trying to get the longest match first, so the order of
93+
// matching is getLongName, getShortName and then getSuffixChar.
94+
if (!found && sanitizedValue.endsWith(parsedUnit.getLongName())) {
95+
found = true;
96+
suffix = parsedUnit.getLongName();
97+
}
98+
99+
if (!found && sanitizedValue.endsWith(parsedUnit.getShortName())) {
100+
found = true;
101+
suffix = parsedUnit.getShortName();
102+
}
103+
104+
if (!found && sanitizedValue.endsWith(parsedUnit.getSuffixChar())) {
105+
found = true;
106+
suffix = parsedUnit.getSuffixChar();
107+
}
108+
109+
checkState(found, "Something is wrong, we have to find a " + "match. Internal error.");
110+
111+
String valString = sanitizedValue.substring(0, value.length() - suffix.length());
112+
return new StorageSize(parsedUnit, Double.parseDouble(valString));
113+
114+
}
115+
116+
public StorageUnit getUnit() {
117+
return unit;
118+
}
119+
120+
public double getValue() {
121+
return value;
122+
}
123+
}

0 commit comments

Comments
 (0)