diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java index e888e6a..064dcc6 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java @@ -39,6 +39,7 @@ import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ColumnFamilySchema import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Human; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparable; @@ -102,6 +103,13 @@ public class HColumnDescriptor implements WritableComparable public static final String ENCRYPTION_KEY = "ENCRYPTION_KEY"; /** + * TTL related constat for human readable representation + */ + + public static final Long SECONDS_PER_DAY = 60 * 60 * 24L; + + + /** * Default compression type. */ public static final String DEFAULT_COMPRESSION = @@ -929,6 +937,7 @@ public class HColumnDescriptor implements WritableComparable @Override public String toString() { StringBuilder s = new StringBuilder(); + s.append('{'); s.append(HConstants.NAME); s.append(" => '"); @@ -973,7 +982,7 @@ public class HColumnDescriptor implements WritableComparable s.append(", "); s.append(key); s.append(" => "); - s.append('\'').append(value).append('\''); + s.append('\'').append(Human.toHumanString(key, value)).append('\''); } } @@ -995,7 +1004,7 @@ public class HColumnDescriptor implements WritableComparable printComma = true; s.append('\'').append(key).append('\''); s.append(" => "); - s.append('\'').append(value).append('\''); + s.append('\'').append(Human.toHumanString(key, value)).append('\''); } s.append('}'); } @@ -1010,7 +1019,7 @@ public class HColumnDescriptor implements WritableComparable printCommaForConfiguration = true; s.append('\'').append(e.getKey()).append('\''); s.append(" => "); - s.append('\'').append(e.getValue()).append('\''); + s.append('\'').append(Human.toHumanString(e.getKey(), e.getValue())).append('\''); } s.append("}"); } diff --git hbase-common/src/main/java/org/apache/hadoop/hbase/util/Human.java hbase-common/src/main/java/org/apache/hadoop/hbase/util/Human.java new file mode 100644 index 0000000..44013fb --- /dev/null +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/Human.java @@ -0,0 +1,85 @@ +/** + * + * 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.hadoop.hbase.util; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; + +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class Human { + /** + * TTL related constants for human readable representation + */ + public static final Long SECONDS_PER_DAY = 60 * 60 * 24L; + public static final String TTL = "TTL"; + + public enum Unit { + TIME_INTERVAL, + NONE + } + + public static String toHumanString(final String key, final String value) { + StringBuilder human = new StringBuilder(); + switch (getUnit(key)) { + case TIME_INTERVAL: + human.append(humanReadableTTL(Long.valueOf(value))); + break; + default: + human.append(value); + } + return human.toString(); + } + + private static Unit getUnit(String key) { + Unit unit; + /* TTL for now, we can add more as we neeed */ + if (key.equals(TTL)) { + unit = Unit.TIME_INTERVAL; + } else { + unit = Unit.NONE; + } + return unit; + } + + private static String humanReadableTTL(final long interval){ + StringBuilder sb = new StringBuilder(); + + // edge cases first + if (interval == Integer.MAX_VALUE) { + sb.append("FOREVER"); + return sb.toString(); + } + if (interval < SECONDS_PER_DAY) { + sb.append(interval); + sb.append(" SECOND").append(interval == 1 ? "" : "S"); + return sb.toString(); + } + if (interval >= SECONDS_PER_DAY) { + sb.append(interval); + sb.append(" SECONDS ("); + sb.append((int) (interval / SECONDS_PER_DAY)); + sb.append(" DAY").append(interval == SECONDS_PER_DAY ? "" : "S"); + sb.append(")"); + } + return sb.toString(); + } + +} diff --git hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestHuman.java hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestHuman.java new file mode 100644 index 0000000..d31fc79 --- /dev/null +++ hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestHuman.java @@ -0,0 +1,49 @@ +/** + * + * 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.hadoop.hbase.util; + +import junit.framework.TestCase; + +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.hadoop.hbase.SmallTests; +import static org.apache.hadoop.hbase.util.Human.*; + +@Category(SmallTests.class) +public class TestHuman extends TestCase { + + @Test + public void testTTLToHumanString() { + String out; + Long[] values = + { 0L, 1L, 1000L, 86400L, 259200L, Long.valueOf(Integer.MAX_VALUE) }; + String[] messages = + { "0 SECONDS", "1 SECOND", "1000 SECONDS", "86400 SECONDS (1 DAY)", + "259200 SECONDS (3 DAYS)", "FOREVER" }; + + for (int i = 0; i < values.length; i++) { + out = toHumanString(TTL, Long.toString(values[i])); + assertEquals( + String.format( + "Expected <%s> ", messages[i]), messages[i], out); + } + } +}