From f4b63a391b75f66c55cdc32ea221142cd7436c54 Mon Sep 17 00:00:00 2001 From: Akira Ajisaka Date: Sat, 9 Jul 2016 21:00:13 -0700 Subject: [PATCH] Revert "HADOOP-10465. Fix use of generics within SortedMapWritable. Contributed by Bertrand Dechoux." This reverts commit 1d175f0f3f80a066a6de71213392d288329d837f. --- .../apache/hadoop/io/SortedMapWritable.java | 47 +++++++++++-------- .../hadoop/io/TestSortedMapWritable.java | 37 +++++++-------- .../typedbytes/TypedBytesWritableInput.java | 13 ++--- .../typedbytes/TypedBytesWritableOutput.java | 6 +-- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/SortedMapWritable.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/SortedMapWritable.java index 3ed2a1dff7d..3b51fc25ffc 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/SortedMapWritable.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/SortedMapWritable.java @@ -36,15 +36,15 @@ */ @InterfaceAudience.Public @InterfaceStability.Stable -public class SortedMapWritable> extends AbstractMapWritable - implements SortedMap { +public class SortedMapWritable extends AbstractMapWritable + implements SortedMap { - private SortedMap instance; + private SortedMap instance; /** default constructor. */ public SortedMapWritable() { super(); - this.instance = new TreeMap(); + this.instance = new TreeMap(); } /** @@ -52,39 +52,45 @@ public SortedMapWritable() { * * @param other the map to copy from */ - public SortedMapWritable(SortedMapWritable other) { + public SortedMapWritable(SortedMapWritable other) { this(); copy(other); } @Override - public Comparator comparator() { + public Comparator comparator() { // Returning null means we use the natural ordering of the keys return null; } @Override - public K firstKey() { + public WritableComparable firstKey() { return instance.firstKey(); } @Override - public SortedMap headMap(K toKey) { + public SortedMap + headMap(WritableComparable toKey) { + return instance.headMap(toKey); } @Override - public K lastKey() { + public WritableComparable lastKey() { return instance.lastKey(); } @Override - public SortedMap subMap(K fromKey, K toKey) { + public SortedMap + subMap(WritableComparable fromKey, WritableComparable toKey) { + return instance.subMap(fromKey, toKey); } @Override - public SortedMap tailMap(K fromKey) { + public SortedMap + tailMap(WritableComparable fromKey) { + return instance.tailMap(fromKey); } @@ -104,7 +110,7 @@ public boolean containsValue(Object value) { } @Override - public Set> entrySet() { + public Set> entrySet() { return instance.entrySet(); } @@ -119,21 +125,22 @@ public boolean isEmpty() { } @Override - public Set keySet() { + public Set keySet() { return instance.keySet(); } @Override - public Writable put(K key, Writable value) { + public Writable put(WritableComparable key, Writable value) { addToMap(key.getClass()); addToMap(value.getClass()); return instance.put(key, value); } @Override - public void putAll(Map t) { - for (Map.Entry e: + public void putAll(Map t) { + for (Map.Entry e: t.entrySet()) { + put(e.getKey(), e.getValue()); } } @@ -165,8 +172,8 @@ public void readFields(DataInput in) throws IOException { // Then read each key/value pair for (int i = 0; i < entries; i++) { - K key = - (K) ReflectionUtils.newInstance(getClass( + WritableComparable key = + (WritableComparable) ReflectionUtils.newInstance(getClass( in.readByte()), getConf()); key.readFields(in); @@ -189,7 +196,7 @@ public void write(DataOutput out) throws IOException { // Then write out each key/value pair - for (Map.Entry e: instance.entrySet()) { + for (Map.Entry e: instance.entrySet()) { out.writeByte(getId(e.getKey().getClass())); e.getKey().write(out); out.writeByte(getId(e.getValue().getClass())); @@ -204,7 +211,7 @@ public boolean equals(Object obj) { } if (obj instanceof SortedMapWritable) { - Map map = (Map) obj; + Map map = (Map) obj; if (size() != map.size()) { return false; } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestSortedMapWritable.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestSortedMapWritable.java index 8c988083848..5ed1db25ffb 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestSortedMapWritable.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestSortedMapWritable.java @@ -45,7 +45,7 @@ public void testSortedMapWritable() { new BytesWritable("value3".getBytes()) }; - SortedMapWritable inMap = new SortedMapWritable(); + SortedMapWritable inMap = new SortedMapWritable(); for (int i = 0; i < keys.length; i++) { inMap.put(keys[i], values[i]); } @@ -53,14 +53,13 @@ public void testSortedMapWritable() { assertEquals(0, inMap.firstKey().compareTo(keys[0])); assertEquals(0, inMap.lastKey().compareTo(keys[2])); - SortedMapWritable outMap = new SortedMapWritable(inMap); + SortedMapWritable outMap = new SortedMapWritable(inMap); assertEquals(inMap.size(), outMap.size()); - for (Map.Entry e: inMap.entrySet()) { + for (Map.Entry e: inMap.entrySet()) { assertTrue(outMap.containsKey(e.getKey())); - WritableComparable> aValue = (WritableComparable>) outMap.get(e.getKey()); - WritableComparable> bValue = (WritableComparable>) e.getValue(); - assertEquals(0, aValue.compareTo(bValue)); + assertEquals(0, ((WritableComparable) outMap.get(e.getKey())).compareTo( + e.getValue())); } // Now for something a little harder... @@ -70,24 +69,24 @@ public void testSortedMapWritable() { new Text("map2") }; - SortedMapWritable mapOfMaps = new SortedMapWritable(); + SortedMapWritable mapOfMaps = new SortedMapWritable(); mapOfMaps.put(maps[0], inMap); mapOfMaps.put(maps[1], outMap); - SortedMapWritable copyOfMapOfMaps = new SortedMapWritable(mapOfMaps); + SortedMapWritable copyOfMapOfMaps = new SortedMapWritable(mapOfMaps); for (int i = 0; i < maps.length; i++) { assertTrue(copyOfMapOfMaps.containsKey(maps[i])); - SortedMapWritable a = (SortedMapWritable) mapOfMaps.get(maps[i]); - SortedMapWritable b = (SortedMapWritable) copyOfMapOfMaps.get(maps[i]); + SortedMapWritable a = (SortedMapWritable) mapOfMaps.get(maps[i]); + SortedMapWritable b = (SortedMapWritable) copyOfMapOfMaps.get(maps[i]); assertEquals(a.size(), b.size()); for (Writable key: a.keySet()) { assertTrue(b.containsKey(key)); // This will work because we know what we put into each set - WritableComparable> aValue = (WritableComparable>) a.get(key); - WritableComparable> bValue = (WritableComparable>) b.get(key); + WritableComparable aValue = (WritableComparable) a.get(key); + WritableComparable bValue = (WritableComparable) b.get(key); assertEquals(0, aValue.compareTo(bValue)); } } @@ -99,11 +98,11 @@ public void testSortedMapWritable() { @Test @SuppressWarnings("deprecation") public void testForeignClass() { - SortedMapWritable inMap = new SortedMapWritable(); + SortedMapWritable inMap = new SortedMapWritable(); inMap.put(new Text("key"), new UTF8("value")); inMap.put(new Text("key2"), new UTF8("value2")); - SortedMapWritable outMap = new SortedMapWritable(inMap); - SortedMapWritable copyOfCopy = new SortedMapWritable(outMap); + SortedMapWritable outMap = new SortedMapWritable(inMap); + SortedMapWritable copyOfCopy = new SortedMapWritable(outMap); assertEquals(1, copyOfCopy.getNewClasses()); } @@ -113,8 +112,8 @@ public void testForeignClass() { @Test public void testEqualsAndHashCode() { String failureReason; - SortedMapWritable mapA = new SortedMapWritable(); - SortedMapWritable mapB = new SortedMapWritable(); + SortedMapWritable mapA = new SortedMapWritable(); + SortedMapWritable mapB = new SortedMapWritable(); // Sanity checks failureReason = "SortedMapWritable couldn't be initialized. Got null reference"; @@ -168,8 +167,8 @@ public void testEqualsAndHashCode() { @Test(timeout = 1000) public void testPutAll() { - SortedMapWritable map1 = new SortedMapWritable(); - SortedMapWritable map2 = new SortedMapWritable(); + SortedMapWritable map1 = new SortedMapWritable(); + SortedMapWritable map2 = new SortedMapWritable(); map1.put(new Text("key"), new Text("value")); map2.putAll(map1); diff --git a/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableInput.java b/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableInput.java index 70b2100361c..a7ca12cf93c 100644 --- a/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableInput.java +++ b/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableInput.java @@ -330,25 +330,22 @@ public MapWritable readMap() throws IOException { return readMap(null); } - public > - SortedMapWritable readSortedMap(SortedMapWritable mw) + public SortedMapWritable readSortedMap(SortedMapWritable mw) throws IOException { if (mw == null) { - mw = new SortedMapWritable(); + mw = new SortedMapWritable(); } int length = in.readMapHeader(); for (int i = 0; i < length; i++) { - @SuppressWarnings("unchecked") - K key = (K) read(); + WritableComparable key = (WritableComparable) read(); Writable value = read(); mw.put(key, value); } return mw; } - public > SortedMapWritable - readSortedMap() throws IOException { - return readSortedMap((SortedMapWritable)null); + public SortedMapWritable readSortedMap() throws IOException { + return readSortedMap(null); } public Writable readWritable(Writable writable) throws IOException { diff --git a/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableOutput.java b/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableOutput.java index e7afe93548c..8dd047271ab 100644 --- a/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableOutput.java +++ b/hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableOutput.java @@ -131,7 +131,7 @@ public void write(Writable w) throws IOException { } else if (w instanceof MapWritable) { writeMap((MapWritable) w); } else if (w instanceof SortedMapWritable) { - writeSortedMap((SortedMapWritable) w); + writeSortedMap((SortedMapWritable) w); } else if (w instanceof Record) { writeRecord((Record) w); } else { @@ -200,9 +200,9 @@ public void writeMap(MapWritable mw) throws IOException { } } - public void writeSortedMap(SortedMapWritable smw) throws IOException { + public void writeSortedMap(SortedMapWritable smw) throws IOException { out.writeMapHeader(smw.size()); - for (Map.Entry, Writable> entry : smw.entrySet()) { + for (Map.Entry entry : smw.entrySet()) { write(entry.getKey()); write(entry.getValue()); }