From ff847978ad9e8d01a66ad6327f924662a9b0d5e8 Mon Sep 17 00:00:00 2001 From: stack Date: Thu, 2 Oct 2014 09:45:02 -0700 Subject: [PATCH] HBASE-12156 TableName cache doesn't used for once of valueOf methods (Andrey Stepachev) --- .../org/apache/hadoop/hbase/TableName.java | 2 +- .../hadoop/hbase/util/TestTableName.java | 90 ++++++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java index eb0f7a9ad4d..802319e5953 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java @@ -446,7 +446,7 @@ public final class TableName implements Comparable { } for (TableName tn : tableCache) { - if (Arrays.equals(tn.getQualifier(), namespace) && + if (Arrays.equals(tn.getQualifier(), qualifier) && Arrays.equals(tn.getNamespace(), namespace)) { return tn; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestTableName.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestTableName.java index 629e1ef328e..452cd62deae 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestTableName.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestTableName.java @@ -17,6 +17,14 @@ */ package org.apache.hadoop.hbase.util; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import org.apache.hadoop.hbase.testclassification.MiscTests; @@ -91,5 +99,85 @@ public class TestTableName extends TestWatcher { } } } - + + class Names { + String ns; + byte[] nsb; + String tn; + byte[] tnb; + String nn; + byte[] nnb; + + Names(String ns, String tn) { + this.ns = ns; + nsb = ns.getBytes(); + this.tn = tn; + tnb = tn.getBytes(); + nn = this.ns + ":" + this.tn; + nnb = nn.getBytes(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Names names = (Names) o; + + if (!ns.equals(names.ns)) return false; + if (!tn.equals(names.tn)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = ns.hashCode(); + result = 31 * result + tn.hashCode(); + return result; + } + } + + Names[] names = new Names[] { + new Names("n1", "n1"), + new Names("n2", "n2"), + new Names("table1", "table1"), + new Names("table2", "table2"), + new Names("table2", "table1"), + new Names("table1", "table2"), + new Names("n1", "table1"), + new Names("n1", "table1"), + new Names("n2", "table2"), + new Names("n2", "table2") + }; + + @Test + public void testValueOf() { + + Map inCache = new HashMap<>(); + // fill cache + for (Names name : names) { + inCache.put(name.nn, TableName.valueOf(name.ns, name.tn)); + } + for (Names name : names) { + assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.ns, name.tn), name)); + assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nsb, name.tnb), name)); + assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nn), name)); + assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nnb), name)); + assertSame(inCache.get(name.nn), validateNames(TableName.valueOf( + ByteBuffer.wrap(name.nsb), ByteBuffer.wrap(name.tnb)), name)); + } + + } + + private TableName validateNames(TableName expected, Names names) { + assertEquals(expected.getNameAsString(), names.nn); + assertArrayEquals(expected.getName(), names.nnb); + assertEquals(expected.getQualifierAsString(), names.tn); + assertArrayEquals(expected.getQualifier(), names.tnb); + assertEquals(expected.getNamespaceAsString(), names.ns); + assertArrayEquals(expected.getNamespace(), names.nsb); + return expected; + } + }