kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8efaa66f722bbbd695bbf7a3e8a1f661331e2dca28e9334be9f3fb777a6f29f0 |
|---|---|
| Author | Olve Sæther Hansen <olve@vimond.com> · 2013-12-10 10:38 +01:00 |
| Committer | Olve Sæther Hansen <olve@vimond.com> · 2013-12-10 10:38 +01:00 |
| Parents | d3b12943134d |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Codec.java +10 −8
| @@ -18,7 +18,6 @@ | ||
| 18 | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | 19 | import com.google.common.collect.Maps; |
| 20 | 20 | import org.apache.cassandra.db.marshal.*; |
| 21 | -import org.apache.cassandra.utils.Pair; | |
| 22 | 21 | |
| 23 | 22 | import java.util.Map; |
| 24 | 23 | |
| @@ -48,21 +47,24 @@ | ||
| 48 | 47 | |
| 49 | 48 | private static Map<DataType.Name, SetType<?>> SETS = buildSets(); |
| 50 | 49 | private static Map<DataType.Name, ListType<?>> LISTS = buildLists(); |
| 51 | - private static Map<Pair<DataType.Name, DataType.Name>, MapType<?, ?>> MAPS = buildMaps(); | |
| 50 | + private static Map<DataType.Name, Map<DataType.Name, MapType<?, ?>>> MAPS = buildMaps(); | |
| 52 | 51 | |
| 53 | 52 | |
| 54 | - protected static ImmutableMap<Pair<DataType.Name, DataType.Name>, MapType<?, ?>> buildMaps() { | |
| 53 | + protected static Map<DataType.Name, Map<DataType.Name, MapType<?, ?>>> buildMaps() { | |
| 54 | + | |
| 55 | + ImmutableMap.Builder<DataType.Name, Map<DataType.Name, MapType<?, ?>>> mapsBuilder = ImmutableMap.builder(); | |
| 55 | 56 | |
| 56 | - ImmutableMap.Builder<Pair<DataType.Name,DataType.Name>, MapType<?,?>> mapsBuilder = ImmutableMap.builder(); | |
| 57 | 57 | for (DataType typeArg1 : DataType.allPrimitiveTypes()) { |
| 58 | + ImmutableMap.Builder<DataType.Name, MapType<?, ?>> innerMapsBuilder = ImmutableMap.builder(); | |
| 58 | 59 | for (DataType typeArg2 : DataType.allPrimitiveTypes()) { |
| 59 | - mapsBuilder.put( | |
| 60 | - Pair.create(typeArg1.getName(),typeArg2.getName()), | |
| 60 | + innerMapsBuilder.put( | |
| 61 | + typeArg2.getName(), | |
| 61 | 62 | MapType.getInstance(getCodec(typeArg1), getCodec(typeArg2)) |
| 62 | 63 | ); |
| 63 | 64 | } |
| 65 | + mapsBuilder.put(typeArg1.getName(), Maps.newEnumMap(innerMapsBuilder.build())); | |
| 64 | 66 | } |
| 65 | - return mapsBuilder.build(); | |
| 67 | + return Maps.newEnumMap(mapsBuilder.build()); | |
| 66 | 68 | } |
| 67 | 69 | |
| 68 | 70 | protected static ImmutableMap<DataType.Name, ListType<?>> buildLists() { |
| @@ -124,7 +126,7 @@ | ||
| 124 | 126 | if(dataTypeArg0.getName() == DataType.Name.CUSTOM || dateTypeArg1.getName() == DataType.Name.CUSTOM) { |
| 125 | 127 | return MapType.getInstance(getCodecInternal(dataTypeArg0), getCodecInternal(dateTypeArg1)); |
| 126 | 128 | } |
| 127 | - return MAPS.get(Pair.create(dataTypeArg0.getName(), dateTypeArg1.getName())); | |
| 129 | + return MAPS.get(dataTypeArg0.getName()).get(dateTypeArg1.getName()); | |
| 128 | 130 | } |
| 129 | 131 | |
| 130 | 132 | private static SetType<?> getSetType(DataType type) { |
modified driver-core/src/test/java/com/datastax/driver/core/CodecTest.java +20 −20
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import org.apache.cassandra.utils.Pair; |
| 20 | 20 | import org.testng.Assert; |
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | +import org.testng.collections.Maps; | |
| 22 | 23 | |
| 23 | 24 | import java.util.*; |
| 24 | 25 | |
| @@ -49,7 +50,7 @@ | ||
| 49 | 50 | public void testCustomKeyMap() throws Exception { |
| 50 | 51 | MapType mapType = (MapType) Codec.getCodec(DataType.map(CUSTOM_FOO, text())); |
| 51 | 52 | Assert.assertNotNull(mapType); |
| 52 | - Assert.assertEquals(mapType.nameComparator().getClass(),BytesType.class); | |
| 53 | + Assert.assertEquals(mapType.nameComparator().getClass(), BytesType.class); | |
| 53 | 54 | Assert.assertEquals(mapType.valueComparator().getClass(), UTF8Type.class); |
| 54 | 55 | } |
| 55 | 56 | |
| @@ -106,18 +107,20 @@ | ||
| 106 | 107 | |
| 107 | 108 | @Test(groups = "unit") |
| 108 | 109 | public void testMapTypeCodec() throws Exception { |
| 109 | - Map<Pair<Name,Name>,MapType<?,?>> dateTypeSetTypeMap = | |
| 110 | - new HashMap<Pair<Name, Name>, MapType<?, ?>>(Codec.buildMaps()); | |
| 111 | - | |
| 112 | - for (DataType dataTypeArg1 : allPrimitiveTypes()) { | |
| 113 | - for (DataType dataTypeArg2 : allPrimitiveTypes()) { | |
| 114 | - final Pair<DataType, DataType> pair = Pair.create(dataTypeArg1, dataTypeArg2); | |
| 115 | - final MapType<?, ?> mapType = dateTypeSetTypeMap.remove(convertToNamePair(pair)); | |
| 116 | - | |
| 117 | - testPairAgainstMapType(pair, mapType); | |
| 110 | + final Map<Name, Map<Name, MapType<?, ?>>> dateTypeMapTypeMap = Maps.newHashMap(Codec.buildMaps()); | |
| 111 | + | |
| 112 | + for (DataType dataTypeArg0 : allPrimitiveTypes()) { | |
| 113 | + final Map<Name, MapType<?, ?>> innerNameToMapType = Maps.newHashMap(dateTypeMapTypeMap.get(dataTypeArg0.getName())); | |
| 114 | + | |
| 115 | + for (DataType dataTypeArg1 : allPrimitiveTypes()) { | |
| 116 | + final MapType<?, ?> mapType = innerNameToMapType.remove(dataTypeArg1.getName()); | |
| 117 | + testPairAgainstMapType(dataTypeArg0, dataTypeArg1, mapType); | |
| 118 | 118 | } |
| 119 | + Assert.assertTrue(innerNameToMapType.isEmpty(), | |
| 120 | + "All inner map types should have been tested for outer type '"+dataTypeArg0+"': " + dateTypeMapTypeMap); | |
| 121 | + dateTypeMapTypeMap.remove(dataTypeArg0.getName()); | |
| 119 | 122 | } |
| 120 | - Assert.assertTrue(dateTypeSetTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeSetTypeMap); | |
| 123 | + Assert.assertTrue(dateTypeMapTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeMapTypeMap); | |
| 121 | 124 | } |
| 122 | 125 | |
| 123 | 126 | private Pair<Name, Name> convertToNamePair(Pair<DataType, DataType> pair) { |
| @@ -125,19 +128,16 @@ | ||
| 125 | 128 | } |
| 126 | 129 | |
| 127 | 130 | |
| 128 | - private void testPairAgainstMapType(Pair<DataType, DataType> pair, MapType<?, ?> mapType) { | |
| 129 | - DataType dataType = pair.left; | |
| 130 | - DataType dataType1 = pair.right; | |
| 131 | - | |
| 131 | + private void testPairAgainstMapType(DataType dataTypeArg0, DataType dataTypeArg1, MapType<?, ?> mapType) { | |
| 132 | 132 | //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail |
| 133 | 133 | //Solution: Remove DataType.varchar and check that it yields DataType.text |
| 134 | 134 | |
| 135 | 135 | //In the map case this would be quite many combinations of text/varchar so I translate varchar to text |
| 136 | 136 | //in order to expect text instead of varchar back from those cases: |
| 137 | - if(dataType.equals(varchar())) dataType = text(); | |
| 138 | - if(dataType1.equals(varchar())) dataType1 = text(); | |
| 139 | - | |
| 140 | - Assert.assertEquals(Codec.rawTypeToDataType(mapType.nameComparator()), dataType) ; | |
| 141 | - Assert.assertEquals(Codec.rawTypeToDataType(mapType.valueComparator()),dataType1) ; | |
| 137 | + if(dataTypeArg0.equals(varchar())) dataTypeArg0 = text(); | |
| 138 | + if(dataTypeArg1.equals(varchar())) dataTypeArg1 = text(); | |
| 139 | + | |
| 140 | + Assert.assertEquals(Codec.rawTypeToDataType(mapType.nameComparator()), dataTypeArg0) ; | |
| 141 | + Assert.assertEquals(Codec.rawTypeToDataType(mapType.valueComparator()),dataTypeArg1) ; | |
| 142 | 142 | } |
| 143 | 143 | } |