HBASE-7563. Move CompoundConfiguration to common and add string map support (Sergey Shelukhin). Addendum commit for the moved files.
git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1434541 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7adfd368cb
commit
ca430a2314
|
@ -17,7 +17,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.hadoop.hbase.regionserver;
|
||||
package org.apache.hadoop.hbase;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
|
@ -46,13 +46,9 @@ import org.apache.hadoop.util.StringUtils;
|
|||
* configuration objects and have changes reflected everywhere. In contrast to a
|
||||
* deep merge, that requires you to explicitly know all applicable copies to
|
||||
* propagate changes.
|
||||
* <p>
|
||||
* This class is package private because we expect significant refactoring here
|
||||
* on the HBase side when certain HDFS changes are added & ubiquitous. Will
|
||||
* revisit expanding access at that point.
|
||||
*/
|
||||
@InterfaceAudience.Private
|
||||
class CompoundConfiguration extends Configuration {
|
||||
public class CompoundConfiguration extends Configuration {
|
||||
/**
|
||||
* Default Constructor. Initializes empty configuration
|
||||
*/
|
||||
|
@ -71,12 +67,9 @@ class CompoundConfiguration extends Configuration {
|
|||
protected List<ImmutableConfigMap> configs
|
||||
= new ArrayList<ImmutableConfigMap>();
|
||||
|
||||
/****************************************************************************
|
||||
* These initial APIs actually required original thought
|
||||
***************************************************************************/
|
||||
|
||||
/**
|
||||
* Add Hadoop Configuration object to config list
|
||||
* Add Hadoop Configuration object to config list.
|
||||
* The added configuration overrides the previous ones if there are name collisions.
|
||||
* @param conf configuration object
|
||||
* @return this, for builder pattern
|
||||
*/
|
||||
|
@ -121,13 +114,14 @@ class CompoundConfiguration extends Configuration {
|
|||
/**
|
||||
* Add ImmutableBytesWritable map to config list. This map is generally
|
||||
* created by HTableDescriptor or HColumnDescriptor, but can be abstractly
|
||||
* used.
|
||||
* used. The added configuration overrides the previous ones if there are
|
||||
* name collisions.
|
||||
*
|
||||
* @param map
|
||||
* ImmutableBytesWritable map
|
||||
* @return this, for builder pattern
|
||||
*/
|
||||
public CompoundConfiguration add(
|
||||
public CompoundConfiguration addWritableMap(
|
||||
final Map<ImmutableBytesWritable, ImmutableBytesWritable> map) {
|
||||
// put new map at the front of the list (top priority)
|
||||
this.configs.add(0, new ImmutableConfigMap() {
|
||||
|
@ -158,7 +152,47 @@ class CompoundConfiguration extends Configuration {
|
|||
|
||||
@Override
|
||||
public int size() {
|
||||
// TODO Auto-generated method stub
|
||||
return m.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return m.toString();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add String map to config list. This map is generally created by HTableDescriptor
|
||||
* or HColumnDescriptor, but can be abstractly used. The added configuration
|
||||
* overrides the previous ones if there are name collisions.
|
||||
*
|
||||
* @return this, for builder pattern
|
||||
*/
|
||||
public CompoundConfiguration addStringMap(final Map<String, String> map) {
|
||||
// put new map at the front of the list (top priority)
|
||||
this.configs.add(0, new ImmutableConfigMap() {
|
||||
Map<String, String> m = map;
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return m.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRaw(String key) {
|
||||
return get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getClassByName(String name)
|
||||
throws ClassNotFoundException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return m.size();
|
||||
}
|
||||
|
||||
|
@ -205,14 +239,9 @@ class CompoundConfiguration extends Configuration {
|
|||
@Override
|
||||
public Class<?> getClassByName(String name) throws ClassNotFoundException {
|
||||
for (ImmutableConfigMap m : this.configs) {
|
||||
try {
|
||||
Class<?> value = m.getClassByName(name);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
// don't propagate an exception until all configs fail
|
||||
continue;
|
||||
Class<?> value = m.getClassByName(name);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
throw new ClassNotFoundException();
|
|
@ -17,14 +17,13 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.hadoop.hbase.regionserver;
|
||||
package org.apache.hadoop.hbase;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
||||
import org.apache.hadoop.hbase.regionserver.CompoundConfiguration;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.apache.hadoop.hbase.SmallTests;
|
||||
|
||||
|
@ -99,7 +98,7 @@ public class TestCompoundConfiguration extends TestCase {
|
|||
|
||||
CompoundConfiguration compoundConf = new CompoundConfiguration()
|
||||
.add(baseConf)
|
||||
.add(map);
|
||||
.addWritableMap(map);
|
||||
assertEquals("1", compoundConf.get("A"));
|
||||
assertEquals("2b", compoundConf.get("B"));
|
||||
assertEquals(33, compoundConf.getInt("C", 0));
|
||||
|
@ -110,4 +109,41 @@ public class TestCompoundConfiguration extends TestCase {
|
|||
assertNull(compoundConf.get("G"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithStringMap() {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("B", "2b");
|
||||
map.put("C", "33");
|
||||
map.put("D", "4");
|
||||
// unlike config, note that IBW Maps can accept null values
|
||||
map.put("G", null);
|
||||
|
||||
CompoundConfiguration compoundConf = new CompoundConfiguration().addStringMap(map);
|
||||
assertEquals("2b", compoundConf.get("B"));
|
||||
assertEquals(33, compoundConf.getInt("C", 0));
|
||||
assertEquals("4", compoundConf.get("D"));
|
||||
assertEquals(4, compoundConf.getInt("D", 0));
|
||||
assertNull(compoundConf.get("E"));
|
||||
assertEquals(6, compoundConf.getInt("F", 6));
|
||||
assertNull(compoundConf.get("G"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLaterConfigsOverrideEarlier() {
|
||||
Map<String, String> map1 = new HashMap<String, String>();
|
||||
map1.put("A", "2");
|
||||
map1.put("D", "5");
|
||||
Map<String, String> map2 = new HashMap<String, String>();
|
||||
map2.put("A", "3");
|
||||
map2.put("B", "4");
|
||||
|
||||
CompoundConfiguration compoundConf = new CompoundConfiguration()
|
||||
.addStringMap(map1).add(baseConf);
|
||||
assertEquals("1", compoundConf.get("A"));
|
||||
assertEquals("5", compoundConf.get("D"));
|
||||
compoundConf.addStringMap(map2);
|
||||
assertEquals("3", compoundConf.get("A"));
|
||||
assertEquals("4", compoundConf.get("B"));
|
||||
assertEquals("5", compoundConf.get("D"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue